aboutsummaryrefslogtreecommitdiff
path: root/tests/suites/test_suite_pkcs7.function
blob: 4c8bf233ef9d74417200ac111471ab18ab63f2db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* BEGIN_HEADER */
#include "mbedtls/bignum.h"
#include "mbedtls/pkcs7.h"
#include "mbedtls/x509.h"
#include "mbedtls/x509_crt.h"
#include "mbedtls/x509_crl.h"
#include "x509_internal.h"
#include "mbedtls/oid.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "mbedtls/rsa.h"
#include "mbedtls/error.h"
/* END_HEADER */

/* BEGIN_DEPENDENCIES
 * depends_on:MBEDTLS_PKCS7_C
 * END_DEPENDENCIES
 */
/* BEGIN_SUITE_HELPERS */
int pkcs7_parse_buffer(unsigned char *pkcs7_buf, int buflen)
{
    int res;
    mbedtls_pkcs7 pkcs7;

    mbedtls_pkcs7_init(&pkcs7);
    res = mbedtls_pkcs7_parse_der(&pkcs7, pkcs7_buf, buflen);
    mbedtls_pkcs7_free(&pkcs7);
    return res;
}
/* END_SUITE_HELPERS */

/* BEGIN_CASE */
void pkcs7_asn1_fail(data_t *pkcs7_buf)
{
    int res;
    res = pkcs7_parse_buffer(pkcs7_buf->x, pkcs7_buf->len);
    TEST_ASSERT(res != MBEDTLS_PKCS7_SIGNED_DATA);

}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
void pkcs7_parse(char *pkcs7_file, int res_expect)
{
    unsigned char *pkcs7_buf = NULL;
    size_t buflen;
    int res;

    res = mbedtls_pk_load_file(pkcs7_file, &pkcs7_buf, &buflen);
    TEST_EQUAL(res, 0);

    res = pkcs7_parse_buffer(pkcs7_buf, buflen);
    TEST_EQUAL(res, res_expect);

exit:
    mbedtls_free(pkcs7_buf);
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C */
void pkcs7_verify(char *pkcs7_file,
                  char *crt_files,
                  char *filetobesigned,
                  int do_hash_alg,
                  int res_expect)
{
    unsigned char *pkcs7_buf = NULL;
    size_t buflen, i, k, cnt = 0, n_crts = 1;
    unsigned char *data = NULL;
    char **crt_files_arr = NULL;
    unsigned char *hash = NULL;
    struct stat st;
    size_t datalen;
    int res;
    FILE *file;
    const mbedtls_md_info_t *md_info;
    mbedtls_pkcs7 pkcs7;
    mbedtls_x509_crt **crts = NULL;

    MD_OR_USE_PSA_INIT();

    mbedtls_pkcs7_init(&pkcs7);

    /* crt_files are space seprated list */
    for (i = 0; i < strlen(crt_files); i++) {
        if (crt_files[i] == ' ') {
            n_crts++;
        }
    }

    TEST_CALLOC(crts, n_crts);
    TEST_CALLOC(crt_files_arr, n_crts);

    for (i = 0; i < strlen(crt_files); i++) {
        for (k = i; k < strlen(crt_files); k++) {
            if (crt_files[k] == ' ') {
                break;
            }
        }
        TEST_CALLOC(crt_files_arr[cnt], (k-i)+1);
        crt_files_arr[cnt][k-i] = '\0';
        memcpy(crt_files_arr[cnt++], crt_files + i, k-i);
        i = k;
    }

    for (i = 0; i < n_crts; i++) {
        TEST_CALLOC(crts[i], 1);
        mbedtls_x509_crt_init(crts[i]);
    }

    res = mbedtls_pk_load_file(pkcs7_file, &pkcs7_buf, &buflen);
    TEST_EQUAL(res, 0);

    res = mbedtls_pkcs7_parse_der(&pkcs7, pkcs7_buf, buflen);
    TEST_EQUAL(res, MBEDTLS_PKCS7_SIGNED_DATA);

    TEST_EQUAL(pkcs7.signed_data.no_of_signers, n_crts);

    for (i = 0; i < n_crts; i++) {
        res = mbedtls_x509_crt_parse_file(crts[i], crt_files_arr[i]);
        TEST_EQUAL(res, 0);
    }

    res = stat(filetobesigned, &st);
    TEST_EQUAL(res, 0);

    file = fopen(filetobesigned, "rb");
    TEST_ASSERT(file != NULL);

    datalen = st.st_size;
    /* Special-case for zero-length input so that data will be non-NULL */
    TEST_CALLOC(data, datalen == 0 ? 1 : datalen);
    buflen = fread((void *) data, sizeof(unsigned char), datalen, file);
    TEST_EQUAL(buflen, datalen);

    fclose(file);

    if (do_hash_alg) {
        md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) do_hash_alg);
        TEST_CALLOC(hash, mbedtls_md_get_size(md_info));
        res = mbedtls_md(md_info, data, datalen, hash);
        TEST_EQUAL(res, 0);

        for (i = 0; i < n_crts; i++) {
            res =
                mbedtls_pkcs7_signed_hash_verify(&pkcs7, crts[i], hash,
                                                 mbedtls_md_get_size(md_info));
            TEST_EQUAL(res, res_expect);
        }
    } else {
        for (i = 0; i < n_crts; i++) {
            res = mbedtls_pkcs7_signed_data_verify(&pkcs7, crts[i], data, datalen);
            TEST_EQUAL(res, res_expect);
        }
    }

exit:
    for (i = 0; i < n_crts; i++) {
        mbedtls_x509_crt_free(crts[i]);
        mbedtls_free(crts[i]);
        mbedtls_free(crt_files_arr[i]);
    }
    mbedtls_free(hash);
    mbedtls_pkcs7_free(&pkcs7);
    mbedtls_free(crt_files_arr);
    mbedtls_free(crts);
    mbedtls_free(data);
    mbedtls_free(pkcs7_buf);
    MD_OR_USE_PSA_DONE();
}
/* END_CASE */