blob: 0c4a00b9e33348e39793f56142d3ff74ea791684 [file] [log] [blame]
Nayna Jainc9deb182020-11-16 19:03:12 +00001/* BEGIN_HEADER */
2#include "mbedtls/bignum.h"
3#include "mbedtls/pkcs7.h"
4#include "mbedtls/x509.h"
5#include "mbedtls/x509_crt.h"
6#include "mbedtls/x509_crl.h"
Valerio Setti25b282e2024-01-17 10:55:32 +01007#include "x509_internal.h"
Nayna Jainc9deb182020-11-16 19:03:12 +00008#include "mbedtls/oid.h"
9#include "sys/types.h"
10#include "sys/stat.h"
Dave Rodgman651fb522023-03-12 10:00:44 +000011#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020012#include "mbedtls/error.h"
Nayna Jainc9deb182020-11-16 19:03:12 +000013/* END_HEADER */
14
15/* BEGIN_DEPENDENCIES
Dave Rodgman651fb522023-03-12 10:00:44 +000016 * depends_on:MBEDTLS_PKCS7_C
Nayna Jainc9deb182020-11-16 19:03:12 +000017 * END_DEPENDENCIES
18 */
Nick Childb7817702022-12-12 15:49:35 -060019/* BEGIN_SUITE_HELPERS */
Michael Schuster54300d42024-06-04 02:30:22 +020020static int pkcs7_parse_buffer(unsigned char *pkcs7_buf, int buflen)
Nick Childb7817702022-12-12 15:49:35 -060021{
22 int res;
23 mbedtls_pkcs7 pkcs7;
24
25 mbedtls_pkcs7_init(&pkcs7);
26 res = mbedtls_pkcs7_parse_der(&pkcs7, pkcs7_buf, buflen);
27 mbedtls_pkcs7_free(&pkcs7);
28 return res;
29}
30/* END_SUITE_HELPERS */
Nayna Jainc9deb182020-11-16 19:03:12 +000031
Nick Child4983ddf2022-12-14 15:04:40 -060032/* BEGIN_CASE */
33void pkcs7_asn1_fail(data_t *pkcs7_buf)
34{
35 int res;
Valerio Setti03a86e72025-05-28 12:01:14 +020036
37 /* PKCS7 uses X509 which itself relies on PK under the hood and the latter
38 * can use PSA to store keys and perform operations so psa_crypto_init()
39 * must be called before. */
40 USE_PSA_INIT();
41
Nick Child4983ddf2022-12-14 15:04:40 -060042 res = pkcs7_parse_buffer(pkcs7_buf->x, pkcs7_buf->len);
43 TEST_ASSERT(res != MBEDTLS_PKCS7_SIGNED_DATA);
44
Valerio Setti03a86e72025-05-28 12:01:14 +020045exit:
46 USE_PSA_DONE();
Nick Child4983ddf2022-12-14 15:04:40 -060047}
48/* END_CASE */
49
Nick Child45525d32022-02-25 11:54:34 -060050/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010051void pkcs7_parse(char *pkcs7_file, int res_expect)
Nayna Jainc9deb182020-11-16 19:03:12 +000052{
53 unsigned char *pkcs7_buf = NULL;
54 size_t buflen;
55 int res;
56
Valerio Setti03a86e72025-05-28 12:01:14 +020057 /* PKCS7 uses X509 which itself relies on PK under the hood and the latter
58 * can use PSA to store keys and perform operations so psa_crypto_init()
59 * must be called before. */
60 USE_PSA_INIT();
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062 res = mbedtls_pk_load_file(pkcs7_file, &pkcs7_buf, &buflen);
63 TEST_EQUAL(res, 0);
Nayna Jainc9deb182020-11-16 19:03:12 +000064
Nick Childb7817702022-12-12 15:49:35 -060065 res = pkcs7_parse_buffer(pkcs7_buf, buflen);
Gilles Peskine449bd832023-01-11 14:50:10 +010066 TEST_EQUAL(res, res_expect);
Nayna Jainc9deb182020-11-16 19:03:12 +000067
68exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010069 mbedtls_free(pkcs7_buf);
Valerio Setti03a86e72025-05-28 12:01:14 +020070 USE_PSA_DONE();
Nayna Jainc9deb182020-11-16 19:03:12 +000071}
72/* END_CASE */
73
Nick Child45525d32022-02-25 11:54:34 -060074/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C */
Nick Childc5474472023-01-27 21:06:39 +000075void pkcs7_verify(char *pkcs7_file,
76 char *crt_files,
77 char *filetobesigned,
78 int do_hash_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +010079 int res_expect)
Nayna Jainc9deb182020-11-16 19:03:12 +000080{
81 unsigned char *pkcs7_buf = NULL;
Nick Childc5474472023-01-27 21:06:39 +000082 size_t buflen, i, k, cnt = 0, n_crts = 1;
Nayna Jainc9deb182020-11-16 19:03:12 +000083 unsigned char *data = NULL;
Nick Childc5474472023-01-27 21:06:39 +000084 char **crt_files_arr = NULL;
Nick Child951f7002023-01-30 16:35:58 +000085 unsigned char *hash = NULL;
Nayna Jainc9deb182020-11-16 19:03:12 +000086 struct stat st;
87 size_t datalen;
88 int res;
89 FILE *file;
90 const mbedtls_md_info_t *md_info;
Nayna Jainc9deb182020-11-16 19:03:12 +000091 mbedtls_pkcs7 pkcs7;
Nick Childc5474472023-01-27 21:06:39 +000092 mbedtls_x509_crt **crts = NULL;
Nayna Jainc9deb182020-11-16 19:03:12 +000093
Valerio Setti03a86e72025-05-28 12:01:14 +020094 USE_PSA_INIT();
Nayna Jainc9deb182020-11-16 19:03:12 +000095
Paul Elliott45b6e5e2023-09-12 11:29:16 +010096 mbedtls_pkcs7_init(&pkcs7);
97
Nick Childc5474472023-01-27 21:06:39 +000098 /* crt_files are space seprated list */
99 for (i = 0; i < strlen(crt_files); i++) {
100 if (crt_files[i] == ' ') {
101 n_crts++;
102 }
Nick Child8a94de42022-09-14 10:51:51 -0500103 }
Nayna Jainc9deb182020-11-16 19:03:12 +0000104
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100105 TEST_CALLOC(crts, n_crts);
106 TEST_CALLOC(crt_files_arr, n_crts);
Nayna Jainc9deb182020-11-16 19:03:12 +0000107
Nick Childc5474472023-01-27 21:06:39 +0000108 for (i = 0; i < strlen(crt_files); i++) {
109 for (k = i; k < strlen(crt_files); k++) {
110 if (crt_files[k] == ' ') {
111 break;
112 }
113 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100114 TEST_CALLOC(crt_files_arr[cnt], (k-i)+1);
Nick Childc5474472023-01-27 21:06:39 +0000115 crt_files_arr[cnt][k-i] = '\0';
116 memcpy(crt_files_arr[cnt++], crt_files + i, k-i);
117 i = k;
118 }
Nick Child62b2d7e2022-07-14 16:24:59 -0500119
Nick Childc5474472023-01-27 21:06:39 +0000120 for (i = 0; i < n_crts; i++) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100121 TEST_CALLOC(crts[i], 1);
Nick Childc5474472023-01-27 21:06:39 +0000122 mbedtls_x509_crt_init(crts[i]);
123 }
Nick Child62b2d7e2022-07-14 16:24:59 -0500124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 res = mbedtls_pk_load_file(pkcs7_file, &pkcs7_buf, &buflen);
126 TEST_EQUAL(res, 0);
Nick Child62b2d7e2022-07-14 16:24:59 -0500127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 res = mbedtls_pkcs7_parse_der(&pkcs7, pkcs7_buf, buflen);
129 TEST_EQUAL(res, MBEDTLS_PKCS7_SIGNED_DATA);
Nick Child62b2d7e2022-07-14 16:24:59 -0500130
Nick Childc5474472023-01-27 21:06:39 +0000131 TEST_EQUAL(pkcs7.signed_data.no_of_signers, n_crts);
Nick Child62b2d7e2022-07-14 16:24:59 -0500132
Nick Childc5474472023-01-27 21:06:39 +0000133 for (i = 0; i < n_crts; i++) {
134 res = mbedtls_x509_crt_parse_file(crts[i], crt_files_arr[i]);
135 TEST_EQUAL(res, 0);
136 }
Nick Child62b2d7e2022-07-14 16:24:59 -0500137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 res = stat(filetobesigned, &st);
139 TEST_EQUAL(res, 0);
Nick Child62b2d7e2022-07-14 16:24:59 -0500140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 file = fopen(filetobesigned, "rb");
142 TEST_ASSERT(file != NULL);
Nick Child62b2d7e2022-07-14 16:24:59 -0500143
144 datalen = st.st_size;
Dave Rodgman716163e2023-02-20 14:46:51 +0000145 /* Special-case for zero-length input so that data will be non-NULL */
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100146 TEST_CALLOC(data, datalen == 0 ? 1 : datalen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 buflen = fread((void *) data, sizeof(unsigned char), datalen, file);
148 TEST_EQUAL(buflen, datalen);
Nick Child62b2d7e2022-07-14 16:24:59 -0500149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 fclose(file);
Nick Child62b2d7e2022-07-14 16:24:59 -0500151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (do_hash_alg) {
Nick Childff2746f2022-12-15 13:06:21 -0600153 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) do_hash_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100154 TEST_CALLOC(hash, mbedtls_md_get_size(md_info));
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 res = mbedtls_md(md_info, data, datalen, hash);
156 TEST_EQUAL(res, 0);
Nick Child62b2d7e2022-07-14 16:24:59 -0500157
Nick Childc5474472023-01-27 21:06:39 +0000158 for (i = 0; i < n_crts; i++) {
159 res =
160 mbedtls_pkcs7_signed_hash_verify(&pkcs7, crts[i], hash,
161 mbedtls_md_get_size(md_info));
162 TEST_EQUAL(res, res_expect);
163 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 } else {
Nick Childc5474472023-01-27 21:06:39 +0000165 for (i = 0; i < n_crts; i++) {
166 res = mbedtls_pkcs7_signed_data_verify(&pkcs7, crts[i], data, datalen);
167 TEST_EQUAL(res, res_expect);
168 }
Nick Child8a94de42022-09-14 10:51:51 -0500169 }
Nick Child62b2d7e2022-07-14 16:24:59 -0500170
Nick Child62b2d7e2022-07-14 16:24:59 -0500171exit:
Nick Childc5474472023-01-27 21:06:39 +0000172 for (i = 0; i < n_crts; i++) {
173 mbedtls_x509_crt_free(crts[i]);
174 mbedtls_free(crts[i]);
175 mbedtls_free(crt_files_arr[i]);
176 }
Nick Child951f7002023-01-30 16:35:58 +0000177 mbedtls_free(hash);
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 mbedtls_pkcs7_free(&pkcs7);
Nick Childc5474472023-01-27 21:06:39 +0000179 mbedtls_free(crt_files_arr);
180 mbedtls_free(crts);
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_free(data);
182 mbedtls_free(pkcs7_buf);
Valerio Setti03a86e72025-05-28 12:01:14 +0200183 USE_PSA_DONE();
Nick Child62b2d7e2022-07-14 16:24:59 -0500184}
185/* END_CASE */