2020-11-16 20:03:12 +01:00
|
|
|
/* 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 "mbedtls/oid.h"
|
|
|
|
#include "sys/types.h"
|
|
|
|
#include "sys/stat.h"
|
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2022-11-04 12:33:04 +01:00
|
|
|
* depends_on:MBEDTLS_PKCS7_C:MBEDTLS_RSA_C
|
2020-11-16 20:03:12 +01:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2022-12-12 22:49:35 +01:00
|
|
|
/* 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 */
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-12-14 22:04:40 +01:00
|
|
|
/* 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 */
|
|
|
|
|
2022-02-25 18:54:34 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void pkcs7_parse(char *pkcs7_file, int res_expect)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
|
|
|
unsigned char *pkcs7_buf = NULL;
|
|
|
|
size_t buflen;
|
|
|
|
int res;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_pk_load_file(pkcs7_file, &pkcs7_buf, &buflen);
|
|
|
|
TEST_EQUAL(res, 0);
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-12-12 22:49:35 +01:00
|
|
|
res = pkcs7_parse_buffer(pkcs7_buf, buflen);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(res, res_expect);
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(pkcs7_buf);
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-02-25 18:54:34 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C */
|
2023-01-27 22:06:39 +01:00
|
|
|
void pkcs7_verify(char *pkcs7_file,
|
|
|
|
char *crt_files,
|
|
|
|
char *filetobesigned,
|
|
|
|
int do_hash_alg,
|
2023-01-11 14:50:10 +01:00
|
|
|
int res_expect)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
|
|
|
unsigned char *pkcs7_buf = NULL;
|
2023-01-27 22:06:39 +01:00
|
|
|
size_t buflen, i, k, cnt = 0, n_crts = 1;
|
2020-11-16 20:03:12 +01:00
|
|
|
unsigned char *data = NULL;
|
2023-01-27 22:06:39 +01:00
|
|
|
char **crt_files_arr = NULL;
|
2023-01-30 17:35:58 +01:00
|
|
|
unsigned char *hash = NULL;
|
2020-11-16 20:03:12 +01:00
|
|
|
struct stat st;
|
|
|
|
size_t datalen;
|
|
|
|
int res;
|
|
|
|
FILE *file;
|
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_pkcs7 pkcs7;
|
2023-01-27 22:06:39 +01:00
|
|
|
mbedtls_x509_crt **crts = NULL;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
|
2023-01-27 22:06:39 +01:00
|
|
|
/* crt_files are space seprated list */
|
|
|
|
for (i = 0; i < strlen(crt_files); i++) {
|
|
|
|
if (crt_files[i] == ' ') {
|
|
|
|
n_crts++;
|
|
|
|
}
|
2022-09-14 17:51:51 +02:00
|
|
|
}
|
2022-07-14 23:24:59 +02:00
|
|
|
|
2023-01-27 22:06:39 +01:00
|
|
|
ASSERT_ALLOC(crts, sizeof(*crts)*n_crts);
|
|
|
|
ASSERT_ALLOC(crt_files_arr, sizeof(*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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_ALLOC(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;
|
|
|
|
}
|
2022-07-14 23:24:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pkcs7_init(&pkcs7);
|
2023-01-27 22:06:39 +01:00
|
|
|
for (i = 0; i < n_crts; i++) {
|
|
|
|
ASSERT_ALLOC(crts[i], sizeof(*crts[i]));
|
|
|
|
mbedtls_x509_crt_init(crts[i]);
|
|
|
|
}
|
2022-07-14 23:24:59 +02:00
|
|
|
|
2022-11-27 21:32:37 +01:00
|
|
|
USE_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_pk_load_file(pkcs7_file, &pkcs7_buf, &buflen);
|
|
|
|
TEST_EQUAL(res, 0);
|
2022-07-14 23:24:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_pkcs7_parse_der(&pkcs7, pkcs7_buf, buflen);
|
|
|
|
TEST_EQUAL(res, MBEDTLS_PKCS7_SIGNED_DATA);
|
2022-07-14 23:24:59 +02:00
|
|
|
|
2023-01-27 22:06:39 +01:00
|
|
|
TEST_EQUAL(pkcs7.signed_data.no_of_signers, n_crts);
|
2022-07-14 23:24:59 +02:00
|
|
|
|
2023-01-27 22:06:39 +01:00
|
|
|
for (i = 0; i < n_crts; i++) {
|
|
|
|
res = mbedtls_x509_crt_parse_file(crts[i], crt_files_arr[i]);
|
|
|
|
TEST_EQUAL(res, 0);
|
|
|
|
}
|
2022-07-14 23:24:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
res = stat(filetobesigned, &st);
|
|
|
|
TEST_EQUAL(res, 0);
|
2022-07-14 23:24:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
file = fopen(filetobesigned, "rb");
|
|
|
|
TEST_ASSERT(file != NULL);
|
2022-07-14 23:24:59 +02:00
|
|
|
|
|
|
|
datalen = st.st_size;
|
2023-02-16 17:14:46 +01:00
|
|
|
/* Add 1 so that data is non-NULL for zero length input */
|
|
|
|
ASSERT_ALLOC(data, datalen + 1);
|
2023-01-11 14:50:10 +01:00
|
|
|
buflen = fread((void *) data, sizeof(unsigned char), datalen, file);
|
|
|
|
TEST_EQUAL(buflen, datalen);
|
2022-07-14 23:24:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
fclose(file);
|
2022-07-14 23:24:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (do_hash_alg) {
|
2022-12-15 20:06:21 +01:00
|
|
|
md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) do_hash_alg);
|
2023-01-30 17:35:58 +01:00
|
|
|
ASSERT_ALLOC(hash, mbedtls_md_get_size(md_info));
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_md(md_info, data, datalen, hash);
|
|
|
|
TEST_EQUAL(res, 0);
|
2022-07-14 23:24:59 +02:00
|
|
|
|
2023-01-27 22:06:39 +01:00
|
|
|
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);
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2023-01-27 22:06:39 +01:00
|
|
|
for (i = 0; i < n_crts; i++) {
|
|
|
|
res = mbedtls_pkcs7_signed_data_verify(&pkcs7, crts[i], data, datalen);
|
|
|
|
TEST_EQUAL(res, res_expect);
|
|
|
|
}
|
2022-09-14 17:51:51 +02:00
|
|
|
}
|
2022-07-14 23:24:59 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-27 22:06:39 +01:00
|
|
|
for (i = 0; i < n_crts; i++) {
|
|
|
|
mbedtls_x509_crt_free(crts[i]);
|
|
|
|
mbedtls_free(crts[i]);
|
|
|
|
mbedtls_free(crt_files_arr[i]);
|
|
|
|
}
|
2023-01-30 17:35:58 +01:00
|
|
|
mbedtls_free(hash);
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pkcs7_free(&pkcs7);
|
2023-01-27 22:06:39 +01:00
|
|
|
mbedtls_free(crt_files_arr);
|
|
|
|
mbedtls_free(crts);
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(data);
|
|
|
|
mbedtls_free(pkcs7_buf);
|
2022-07-14 23:24:59 +02:00
|
|
|
USE_PSA_DONE();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|