2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2017-06-09 05:32:58 +02:00
|
|
|
#include "mbedtls/bignum.h"
|
2016-09-23 14:16:02 +02:00
|
|
|
#include "mbedtls/x509.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/x509_crt.h"
|
|
|
|
#include "mbedtls/x509_crl.h"
|
|
|
|
#include "mbedtls/x509_csr.h"
|
|
|
|
#include "mbedtls/pem.h"
|
|
|
|
#include "mbedtls/oid.h"
|
|
|
|
#include "mbedtls/base64.h"
|
2021-04-14 13:12:09 +02:00
|
|
|
#include "mbedtls/error.h"
|
2023-02-13 10:04:28 +01:00
|
|
|
#include "mbedtls/pk.h"
|
2017-04-06 12:55:43 +02:00
|
|
|
#include "string.h"
|
2011-01-13 18:54:59 +01:00
|
|
|
|
2017-07-28 13:15:13 +02:00
|
|
|
#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
|
2017-07-26 14:38:02 +02:00
|
|
|
#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
|
2023-01-11 14:50:10 +01:00
|
|
|
than the current threshold 19. To test larger values, please \
|
|
|
|
adapt the script tests/data_files/dir-max/long.sh."
|
2017-07-26 14:38:02 +02:00
|
|
|
#endif
|
|
|
|
|
2019-06-03 15:27:03 +02:00
|
|
|
/* Test-only profile allowing all digests, PK algorithms, and curves. */
|
|
|
|
const mbedtls_x509_crt_profile profile_all =
|
|
|
|
{
|
|
|
|
0xFFFFFFFF, /* Any MD */
|
|
|
|
0xFFFFFFFF, /* Any PK alg */
|
|
|
|
0xFFFFFFFF, /* Any curve */
|
|
|
|
1024,
|
|
|
|
};
|
|
|
|
|
2017-05-05 18:59:02 +02:00
|
|
|
/* Profile for backward compatibility. Allows SHA-1, unlike the default
|
|
|
|
profile. */
|
2015-10-23 14:08:48 +02:00
|
|
|
const mbedtls_x509_crt_profile compat_profile =
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
|
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
|
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
|
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
|
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
|
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
|
2022-04-21 09:12:59 +02:00
|
|
|
0xFFFFFFFF, /* Any PK alg */
|
|
|
|
0xFFFFFFFF, /* Any curve */
|
2015-10-23 14:08:48 +02:00
|
|
|
1024,
|
|
|
|
};
|
|
|
|
|
2017-05-23 11:29:29 +02:00
|
|
|
const mbedtls_x509_crt_profile profile_rsa3072 =
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
|
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
|
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
|
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA),
|
2017-05-23 11:29:29 +02:00
|
|
|
0,
|
|
|
|
3072,
|
|
|
|
};
|
|
|
|
|
|
|
|
const mbedtls_x509_crt_profile profile_sha512 =
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
|
2022-04-21 09:12:59 +02:00
|
|
|
0xFFFFFFFF, /* Any PK alg */
|
|
|
|
0xFFFFFFFF, /* Any curve */
|
2017-05-23 11:29:29 +02:00
|
|
|
1024,
|
|
|
|
};
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
|
2011-01-13 18:54:59 +01:00
|
|
|
{
|
2011-01-18 17:31:52 +01:00
|
|
|
((void) data);
|
|
|
|
((void) crt);
|
|
|
|
((void) certificate_depth);
|
2015-04-20 13:19:02 +02:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_OTHER;
|
2013-09-18 13:46:23 +02:00
|
|
|
|
2012-09-28 09:10:55 +02:00
|
|
|
return 0;
|
2011-01-13 18:54:59 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
|
2011-01-13 18:54:59 +01:00
|
|
|
{
|
2011-01-18 17:31:52 +01:00
|
|
|
((void) data);
|
|
|
|
((void) crt);
|
|
|
|
((void) certificate_depth);
|
2012-09-28 09:10:55 +02:00
|
|
|
*flags = 0;
|
2011-01-18 17:31:52 +01:00
|
|
|
|
2011-01-13 18:54:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-27 14:45:04 +01:00
|
|
|
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
|
2023-01-11 14:50:10 +01:00
|
|
|
int ca_callback_fail(void *data, mbedtls_x509_crt const *child, mbedtls_x509_crt **candidates)
|
2019-03-27 16:08:29 +01:00
|
|
|
{
|
|
|
|
((void) data);
|
|
|
|
((void) child);
|
|
|
|
((void) candidates);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2022-04-27 15:33:43 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int ca_callback(void *data, mbedtls_x509_crt const *child,
|
|
|
|
mbedtls_x509_crt **candidates)
|
2019-03-27 14:45:04 +01:00
|
|
|
{
|
2019-03-28 15:14:22 +01:00
|
|
|
int ret = 0;
|
2019-03-27 14:45:04 +01:00
|
|
|
mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
|
2019-03-28 15:14:22 +01:00
|
|
|
mbedtls_x509_crt *first;
|
|
|
|
|
|
|
|
/* This is a test-only implementation of the CA callback
|
|
|
|
* which always returns the entire list of trusted certificates.
|
|
|
|
* Production implementations managing a large number of CAs
|
|
|
|
* should use an efficient presentation and lookup for the
|
|
|
|
* set of trusted certificates (such as a hashtable) and only
|
|
|
|
* return those trusted certificates which satisfy basic
|
|
|
|
* parental checks, such as the matching of child `Issuer`
|
|
|
|
* and parent `Subject` field. */
|
|
|
|
((void) child);
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
|
|
|
|
if (first == NULL) {
|
2019-03-28 15:14:22 +01:00
|
|
|
ret = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(first);
|
2019-03-28 15:14:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
|
2019-03-28 15:14:22 +01:00
|
|
|
ret = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (ca->next != NULL) {
|
2019-03-27 14:45:04 +01:00
|
|
|
ca = ca->next;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
|
2019-03-28 15:14:22 +01:00
|
|
|
ret = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2019-03-27 14:45:04 +01:00
|
|
|
}
|
2019-03-28 15:14:22 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_x509_crt_free(first);
|
|
|
|
mbedtls_free(first);
|
2019-03-28 15:14:22 +01:00
|
|
|
first = NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-27 14:45:04 +01:00
|
|
|
*candidates = first;
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2019-03-27 14:45:04 +01:00
|
|
|
}
|
2022-04-27 15:33:43 +02:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2019-03-27 14:45:04 +01:00
|
|
|
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
|
2017-05-23 12:26:58 +02:00
|
|
|
{
|
|
|
|
int *levels = (int *) data;
|
|
|
|
|
|
|
|
((void) crt);
|
|
|
|
((void) certificate_depth);
|
|
|
|
|
|
|
|
/* Simulate a fatal error in the callback */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*levels & (1 << certificate_depth)) {
|
|
|
|
*flags |= (1 << certificate_depth);
|
|
|
|
return -1 - certificate_depth;
|
2017-05-23 12:26:58 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2017-05-23 12:26:58 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 22:34:46 +01:00
|
|
|
/* strsep() not available on Windows */
|
|
|
|
char *mystrsep(char **stringp, const char *delim)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
char *ret = *stringp;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*stringp == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-11-01 22:34:46 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (;; (*stringp)++) {
|
|
|
|
if (**stringp == '\0') {
|
2015-11-01 22:34:46 +01:00
|
|
|
*stringp = NULL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (p = delim; *p != '\0'; p++) {
|
|
|
|
if (**stringp == *p) {
|
2015-11-01 22:34:46 +01:00
|
|
|
**stringp = '\0';
|
|
|
|
(*stringp)++;
|
|
|
|
goto done;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2015-11-01 22:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2015-11-01 22:34:46 +01:00
|
|
|
}
|
|
|
|
|
2015-09-01 11:59:24 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
typedef struct {
|
|
|
|
char buf[512];
|
|
|
|
char *p;
|
|
|
|
} verify_print_context;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void verify_print_init(verify_print_context *ctx)
|
2015-09-01 11:59:24 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ctx, 0, sizeof(verify_print_context));
|
2015-09-01 11:59:24 +02:00
|
|
|
ctx->p = ctx->buf;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
|
2015-09-01 11:59:24 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
verify_print_context *ctx = (verify_print_context *) data;
|
|
|
|
char *p = ctx->p;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
|
2015-09-01 11:59:24 +02:00
|
|
|
((void) flags);
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
|
2015-09-01 11:59:24 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
|
2015-09-01 11:59:24 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, " - subject ");
|
2015-09-01 11:59:24 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
|
2015-09-01 11:59:24 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
|
2015-09-01 11:59:24 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
|
|
|
ctx->p = p;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2015-09-01 11:59:24 +02:00
|
|
|
}
|
2019-03-21 12:40:13 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
|
|
|
|
char **buf, size_t *size)
|
2019-03-21 12:40:13 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t i;
|
|
|
|
char *p = *buf;
|
|
|
|
size_t n = *size;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, "type : %d", san->type);
|
2019-03-21 12:40:13 +01:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (san->type) {
|
|
|
|
case (MBEDTLS_X509_SAN_OTHER_NAME):
|
|
|
|
ret = mbedtls_snprintf(p, n, "\notherName :");
|
2022-09-29 16:34:23 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
|
|
|
|
&san->san.other_name.value.hardware_module_name.oid) != 0) {
|
|
|
|
ret = mbedtls_snprintf(p, n, " hardware module name :");
|
2022-09-29 16:34:23 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, " hardware type : ");
|
2022-09-29 16:34:23 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_oid_get_numeric_string(p,
|
|
|
|
n,
|
|
|
|
&san->san.other_name.value.hardware_module_name.oid);
|
2022-09-29 16:34:23 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
|
2022-09-29 16:34:23 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
|
|
|
|
ret = mbedtls_snprintf(p,
|
|
|
|
n,
|
|
|
|
"%02X",
|
|
|
|
san->san.other_name.value.hardware_module_name.val.p[i]);
|
2022-09-29 16:34:23 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
}
|
2019-03-21 12:40:13 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
|
|
|
|
case (MBEDTLS_X509_SAN_DNS_NAME):
|
|
|
|
ret = mbedtls_snprintf(p, n, "\ndNSName : ");
|
2019-03-21 12:40:13 +01:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (san->san.unstructured_name.len >= n) {
|
2019-03-21 12:40:13 +01:00
|
|
|
*p = '\0';
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
2019-03-21 12:40:13 +01:00
|
|
|
}
|
|
|
|
n -= san->san.unstructured_name.len;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < san->san.unstructured_name.len; i++) {
|
2019-03-21 12:40:13 +01:00
|
|
|
*p++ = san->san.unstructured_name.p[i];
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
break;/* MBEDTLS_X509_SAN_DNS_NAME */
|
2023-02-09 14:47:50 +01:00
|
|
|
case (MBEDTLS_X509_SAN_RFC822_NAME):
|
|
|
|
ret = mbedtls_snprintf(p, n, "\nrfc822Name : ");
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
if (san->san.unstructured_name.len >= n) {
|
|
|
|
*p = '\0';
|
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
|
|
|
}
|
|
|
|
n -= san->san.unstructured_name.len;
|
|
|
|
for (i = 0; i < san->san.unstructured_name.len; i++) {
|
|
|
|
*p++ = san->san.unstructured_name.p[i];
|
|
|
|
}
|
|
|
|
break;/* MBEDTLS_X509_SAN_RFC822_NAME */
|
2023-01-10 12:47:38 +01:00
|
|
|
case (MBEDTLS_X509_SAN_DIRECTORY_NAME):
|
|
|
|
ret = mbedtls_snprintf(p, n, "\ndirectoryName : ");
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
ret = mbedtls_x509_dn_gets(p, n, &san->san.directory_name);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
p += ret;
|
|
|
|
n -= ret;
|
|
|
|
break;/* MBEDTLS_X509_SAN_DIRECTORY_NAME */
|
2019-03-21 12:40:13 +01:00
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
/*
|
|
|
|
* Should not happen.
|
|
|
|
*/
|
|
|
|
return -1;
|
2019-03-21 12:40:13 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, "\n");
|
2019-03-21 12:40:13 +01:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
|
|
|
*size = n;
|
|
|
|
*buf = p;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2019-03-21 12:40:13 +01:00
|
|
|
}
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
|
|
|
|
int critical, const unsigned char *cp, const unsigned char *end)
|
2020-05-28 23:04:15 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
(void) crt;
|
|
|
|
(void) critical;
|
|
|
|
mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
|
|
|
|
if (oid->tag == MBEDTLS_ASN1_OID &&
|
|
|
|
MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
|
2020-06-13 11:08:16 +02:00
|
|
|
/* Handle unknown certificate policy */
|
|
|
|
int ret, parse_ret = 0;
|
|
|
|
size_t len;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char **p = (unsigned char **) &cp;
|
2020-06-13 11:08:16 +02:00
|
|
|
|
|
|
|
/* Get main sequence tag */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_tag(p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
|
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
2020-06-13 11:08:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*p + len != end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
2020-06-13 11:08:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Cannot be an empty sequence.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len == 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
2020-06-13 11:08:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (*p < end) {
|
2020-06-13 11:08:16 +02:00
|
|
|
const unsigned char *policy_end;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the policy sequence
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
|
|
|
|
0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
2020-06-13 11:08:16 +02:00
|
|
|
|
|
|
|
policy_end = *p + len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
|
|
|
|
MBEDTLS_ASN1_OID)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
2020-06-13 11:08:16 +02:00
|
|
|
|
2020-06-17 17:57:36 +02:00
|
|
|
/*
|
|
|
|
* Recognize exclusively the policy with OID 1
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len != 1 || *p[0] != 1) {
|
2020-06-13 11:08:16 +02:00
|
|
|
parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-06-13 11:08:16 +02:00
|
|
|
|
|
|
|
*p += len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
/*
|
|
|
|
* If there is an optional qualifier, then *p < policy_end
|
|
|
|
* Check the Qualifier len to verify it doesn't exceed policy_end.
|
|
|
|
*/
|
|
|
|
if (*p < policy_end) {
|
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
2020-06-13 11:08:16 +02:00
|
|
|
/*
|
|
|
|
* Skip the optional policy qualifiers.
|
|
|
|
*/
|
|
|
|
*p += len;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*p != policy_end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
2020-06-13 11:08:16 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*p != end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
2020-06-13 11:08:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return parse_ret;
|
|
|
|
} else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
|
|
|
|
memcmp(new_oid->p, oid->p, oid->len) == 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
2020-06-13 11:08:16 +02:00
|
|
|
}
|
2020-05-28 23:04:15 +02:00
|
|
|
}
|
2015-09-01 11:59:24 +02:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_BIGNUM_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 15:16:06 +02:00
|
|
|
|
2022-06-06 17:36:43 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509_accessor_ext_types(int ext_type, int has_ext_type)
|
2022-05-31 15:16:42 +02:00
|
|
|
{
|
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
int expected_result = ext_type & has_ext_type;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2022-05-31 15:16:42 +02:00
|
|
|
|
|
|
|
crt.ext_types = ext_type;
|
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type), expected_result);
|
2022-05-31 15:16:42 +02:00
|
|
|
|
2023-04-20 11:59:52 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2022-05-31 15:16:42 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-10-25 21:02:14 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_TEST_HOOKS */
|
|
|
|
void x509_crt_parse_cn_inet_pton(const char *cn, data_t *exp, int ref_ret)
|
|
|
|
{
|
|
|
|
uint32_t addr[4];
|
|
|
|
size_t addrlen = mbedtls_x509_crt_parse_cn_inet_pton(cn, addr);
|
|
|
|
TEST_EQUAL(addrlen, (size_t) ref_ret);
|
|
|
|
|
|
|
|
if (addrlen) {
|
|
|
|
ASSERT_COMPARE(exp->x, exp->len, addr, addrlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-03-21 12:40:13 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-20 13:08:57 +01:00
|
|
|
void x509_parse_san(char *crt_file, char *result_str, int parse_result)
|
2019-03-21 12:40:13 +01:00
|
|
|
{
|
2019-05-13 18:03:04 +02:00
|
|
|
int ret;
|
2019-03-21 12:40:13 +01:00
|
|
|
mbedtls_x509_crt crt;
|
2019-05-13 18:03:04 +02:00
|
|
|
mbedtls_x509_subject_alternative_name san;
|
|
|
|
mbedtls_x509_sequence *cur = NULL;
|
2019-03-21 12:40:13 +01:00
|
|
|
char buf[2000];
|
|
|
|
char *p = buf;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t n = sizeof(buf);
|
2019-03-21 12:40:13 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, 2000);
|
2019-03-21 12:40:13 +01:00
|
|
|
|
2023-01-20 13:08:57 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result);
|
2019-05-13 18:03:04 +02:00
|
|
|
|
2023-01-20 13:08:57 +01:00
|
|
|
if (parse_result != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
|
2019-05-13 18:03:04 +02:00
|
|
|
cur = &crt.subject_alt_names;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (cur != NULL) {
|
|
|
|
ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
|
|
|
|
TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
|
2019-05-13 18:03:04 +02:00
|
|
|
/*
|
|
|
|
* If san type not supported, ignore.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
2023-02-13 13:01:59 +01:00
|
|
|
ret = verify_parse_san(&san, &p, &n);
|
|
|
|
mbedtls_x509_free_subject_alt_name(&san);
|
|
|
|
TEST_EQUAL(ret, 0);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-05-13 18:03:04 +02:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
2019-03-21 12:40:13 +01:00
|
|
|
}
|
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp(buf, result_str), 0);
|
2019-03-21 12:40:13 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2019-03-21 12:40:13 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-10-09 10:19:39 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509_cert_info(char *crt_file, char *result_str)
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2009-07-11 00:38:58 +02:00
|
|
|
char buf[2000];
|
2009-07-11 21:15:20 +02:00
|
|
|
int res;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, 2000);
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(res != -1);
|
|
|
|
TEST_ASSERT(res != -2);
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp(buf, result_str), 0);
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2020-10-09 10:19:39 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_crl_info(char *crl_file, char *result_str)
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crl crl;
|
2009-07-11 00:38:58 +02:00
|
|
|
char buf[2000];
|
2009-07-11 21:15:20 +02:00
|
|
|
int res;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crl_init(&crl);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, 2000);
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(res != -1);
|
|
|
|
TEST_ASSERT(res != -2);
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp(buf, result_str), 0);
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crl_free(&crl);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2016-12-08 18:10:38 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_crl_parse(char *crl_file, int result)
|
2016-12-08 18:10:38 +01:00
|
|
|
{
|
|
|
|
mbedtls_x509_crl crl;
|
|
|
|
char buf[2000];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crl_init(&crl);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, 2000);
|
2016-12-08 18:10:38 +01:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), result);
|
2016-12-08 18:10:38 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crl_free(&crl);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2016-12-08 18:10:38 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-10-09 10:19:39 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_csr_info(char *csr_file, char *result_str)
|
2014-01-24 17:34:26 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr csr;
|
2014-01-24 17:34:26 +01:00
|
|
|
char buf[2000];
|
|
|
|
int res;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_csr_init(&csr);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, 2000);
|
2014-01-24 17:34:26 +01:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_csr_parse_file(&csr, csr_file), 0);
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
|
2014-01-24 17:34:26 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(res != -1);
|
|
|
|
TEST_ASSERT(res != -2);
|
2014-01-24 17:34:26 +01:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp(buf, result_str), 0);
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_csr_free(&csr);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2014-01-24 17:34:26 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-10-09 10:19:39 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509_verify_info(int flags, char *prefix, char *result_str)
|
2015-04-20 11:38:13 +02:00
|
|
|
{
|
|
|
|
char buf[2000];
|
|
|
|
int res;
|
|
|
|
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2015-04-20 11:38:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
|
2015-04-20 11:38:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(res >= 0);
|
2015-04-20 11:38:13 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp(buf, result_str), 0);
|
2023-04-20 11:59:52 +02:00
|
|
|
|
|
|
|
exit:
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2015-04-20 11:38:13 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2017-07-14 11:05:59 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509_verify_restart(char *crt_file, char *ca_file,
|
|
|
|
int result, int flags_result,
|
|
|
|
int max_ops, int min_restart, int max_restart)
|
2017-07-14 11:05:59 +02:00
|
|
|
{
|
|
|
|
int ret, cnt_restart;
|
|
|
|
mbedtls_x509_crt_restart_ctx rs_ctx;
|
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
mbedtls_x509_crt ca;
|
|
|
|
uint32_t flags = 0;
|
|
|
|
|
2017-08-14 18:04:19 +02:00
|
|
|
/*
|
|
|
|
* See comments on ecp_test_vect_restart() for op count precision.
|
|
|
|
*
|
|
|
|
* For reference, with mbed TLS 2.6 and default settings:
|
|
|
|
* - ecdsa_verify() for P-256: ~ 6700
|
|
|
|
* - ecdsa_verify() for P-384: ~ 18800
|
|
|
|
* - x509_verify() for server5 -> test-ca2: ~ 18800
|
|
|
|
* - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_restart_init(&rs_ctx);
|
|
|
|
mbedtls_x509_crt_init(&crt);
|
|
|
|
mbedtls_x509_crt_init(&ca);
|
2023-04-20 11:59:52 +02:00
|
|
|
MD_OR_USE_PSA_INIT();
|
2017-07-14 11:05:59 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
|
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
|
2017-07-14 11:05:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_set_max_ops(max_ops);
|
2017-07-14 11:05:59 +02:00
|
|
|
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
|
|
|
|
&mbedtls_x509_crt_profile_default, NULL, &flags,
|
|
|
|
NULL, NULL, &rs_ctx);
|
|
|
|
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
|
2017-07-14 11:05:59 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(ret, result);
|
|
|
|
TEST_EQUAL(flags, (uint32_t) flags_result);
|
2017-07-14 11:05:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(cnt_restart >= min_restart);
|
|
|
|
TEST_ASSERT(cnt_restart <= max_restart);
|
2017-07-14 11:05:59 +02:00
|
|
|
|
|
|
|
/* Do we leak memory when aborting? */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
|
|
|
|
&mbedtls_x509_crt_profile_default, NULL, &flags,
|
|
|
|
NULL, NULL, &rs_ctx);
|
|
|
|
TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
|
2017-07-14 11:05:59 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_restart_free(&rs_ctx);
|
|
|
|
mbedtls_x509_crt_free(&crt);
|
|
|
|
mbedtls_x509_crt_free(&ca);
|
2023-03-17 14:02:49 +01:00
|
|
|
MD_OR_USE_PSA_DONE();
|
2017-07-14 11:05:59 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509_verify(char *crt_file, char *ca_file, char *crl_file,
|
|
|
|
char *cn_name_str, int result, int flags_result,
|
|
|
|
char *profile_str,
|
|
|
|
char *verify_callback)
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
mbedtls_x509_crt ca;
|
|
|
|
mbedtls_x509_crl crl;
|
2015-05-11 19:54:43 +02:00
|
|
|
uint32_t flags = 0;
|
2009-07-11 21:15:20 +02:00
|
|
|
int res;
|
2015-05-11 19:54:43 +02:00
|
|
|
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
char *cn_name = NULL;
|
2017-05-05 18:59:02 +02:00
|
|
|
const mbedtls_x509_crt_profile *profile;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
|
|
|
mbedtls_x509_crt_init(&ca);
|
|
|
|
mbedtls_x509_crl_init(&crl);
|
2023-04-20 11:59:52 +02:00
|
|
|
MD_OR_USE_PSA_INIT();
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(cn_name_str, "NULL") != 0) {
|
2013-08-20 11:48:36 +02:00
|
|
|
cn_name = cn_name_str;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-08-16 13:38:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(profile_str, "") == 0) {
|
2017-05-05 18:59:02 +02:00
|
|
|
profile = &mbedtls_x509_crt_profile_default;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(profile_str, "next") == 0) {
|
2018-02-06 17:47:17 +01:00
|
|
|
profile = &mbedtls_x509_crt_profile_next;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(profile_str, "suite_b") == 0) {
|
2018-02-06 17:47:17 +01:00
|
|
|
profile = &mbedtls_x509_crt_profile_suiteb;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(profile_str, "compat") == 0) {
|
2017-05-05 18:59:02 +02:00
|
|
|
profile = &compat_profile;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(profile_str, "all") == 0) {
|
2019-06-03 15:27:03 +02:00
|
|
|
profile = &profile_all;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
TEST_ASSERT("Unknown algorithm profile" == 0);
|
|
|
|
}
|
2017-05-05 18:59:02 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(verify_callback, "NULL") == 0) {
|
2013-08-16 13:38:47 +02:00
|
|
|
f_vrfy = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(verify_callback, "verify_none") == 0) {
|
2013-08-16 13:38:47 +02:00
|
|
|
f_vrfy = verify_none;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(verify_callback, "verify_all") == 0) {
|
2013-08-16 13:38:47 +02:00
|
|
|
f_vrfy = verify_all;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
TEST_ASSERT("No known verify callback selected" == 0);
|
|
|
|
}
|
2013-08-16 13:38:47 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
|
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
|
|
|
|
TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_x509_crt_verify_with_profile(&crt,
|
|
|
|
&ca,
|
|
|
|
&crl,
|
|
|
|
profile,
|
|
|
|
cn_name,
|
|
|
|
&flags,
|
|
|
|
f_vrfy,
|
|
|
|
NULL);
|
2015-10-23 14:08:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(res, result);
|
|
|
|
TEST_EQUAL(flags, (uint32_t) flags_result);
|
2014-07-10 15:26:12 +02:00
|
|
|
|
2019-03-27 14:45:04 +01:00
|
|
|
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
|
2019-03-28 15:23:36 +01:00
|
|
|
/* CRLs aren't supported with CA callbacks, so skip the CA callback
|
2019-04-01 14:18:53 +02:00
|
|
|
* version of the test if CRLs are in use. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (crl_file == NULL || strcmp(crl_file, "") == 0) {
|
2019-03-28 15:23:36 +01:00
|
|
|
flags = 0;
|
2019-03-27 14:45:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
|
|
|
|
ca_callback,
|
|
|
|
&ca,
|
|
|
|
profile,
|
|
|
|
cn_name,
|
|
|
|
&flags,
|
|
|
|
f_vrfy,
|
|
|
|
NULL);
|
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(res, result);
|
|
|
|
TEST_EQUAL(flags, (uint32_t) (flags_result));
|
2019-03-28 15:23:36 +01:00
|
|
|
}
|
2019-03-27 14:45:04 +01:00
|
|
|
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
|
|
|
mbedtls_x509_crt_free(&ca);
|
|
|
|
mbedtls_x509_crl_free(&crl);
|
2023-03-17 14:02:49 +01:00
|
|
|
MD_OR_USE_PSA_DONE();
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2019-03-27 16:08:29 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
|
|
|
|
int exp_ret)
|
2019-03-27 16:08:29 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
mbedtls_x509_crt ca;
|
|
|
|
uint32_t flags = 0;
|
2019-03-28 18:06:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
|
|
|
mbedtls_x509_crt_init(&ca);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2019-03-27 16:08:29 +01:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
|
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
|
2019-03-27 16:08:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(name, "NULL") == 0) {
|
2019-03-27 16:08:29 +01:00
|
|
|
name = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-03-28 15:14:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
|
|
|
|
&compat_profile, name, &flags,
|
|
|
|
NULL, NULL);
|
2019-03-27 16:08:29 +01:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(ret, exp_ret);
|
|
|
|
TEST_EQUAL(flags, (uint32_t) (-1));
|
2019-03-27 16:08:29 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
|
|
|
mbedtls_x509_crt_free(&ca);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2019-03-27 16:08:29 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-09-01 11:59:24 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509_verify_callback(char *crt_file, char *ca_file, char *name,
|
|
|
|
int exp_ret, char *exp_vrfy_out)
|
2015-09-01 11:59:24 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
mbedtls_x509_crt ca;
|
|
|
|
uint32_t flags = 0;
|
|
|
|
verify_print_context vrfy_ctx;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
|
|
|
mbedtls_x509_crt_init(&ca);
|
2023-04-20 11:59:52 +02:00
|
|
|
MD_OR_USE_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
verify_print_init(&vrfy_ctx);
|
2015-09-01 11:59:24 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
|
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
|
2015-09-01 11:59:24 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(name, "NULL") == 0) {
|
2017-07-05 18:14:38 +02:00
|
|
|
name = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-07-05 18:14:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
|
|
|
|
&compat_profile,
|
|
|
|
name, &flags,
|
|
|
|
verify_print, &vrfy_ctx);
|
2015-09-01 11:59:24 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(ret, exp_ret);
|
|
|
|
TEST_EQUAL(strcmp(vrfy_ctx.buf, exp_vrfy_out), 0);
|
2015-09-01 11:59:24 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
|
|
|
mbedtls_x509_crt_free(&ca);
|
2023-03-17 14:02:49 +01:00
|
|
|
MD_OR_USE_PSA_DONE();
|
2015-09-01 11:59:24 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-06-17 16:51:55 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
|
|
|
|
char *new_subject_ou,
|
|
|
|
char *result_str,
|
|
|
|
int ret)
|
2022-06-17 16:51:55 +02:00
|
|
|
{
|
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
char buf[2000];
|
|
|
|
int res = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, 2000);
|
2022-06-17 16:51:55 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
|
2022-06-17 16:51:55 +02:00
|
|
|
crt.subject.next->val.p = (unsigned char *) new_subject_ou;
|
2023-01-11 14:50:10 +01:00
|
|
|
crt.subject.next->val.len = strlen(new_subject_ou);
|
2022-06-17 16:51:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
|
2022-06-17 16:51:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(res, ret);
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
TEST_ASSERT(res != -1);
|
|
|
|
TEST_ASSERT(res != -2);
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp(buf, result_str), 0);
|
2022-06-17 16:51:55 +02:00
|
|
|
}
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2022-06-17 16:51:55 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-10-09 10:19:39 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2009-07-11 00:38:58 +02:00
|
|
|
char buf[2000];
|
2013-08-16 13:38:47 +02:00
|
|
|
int res = 0;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, 2000);
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(entity, "subject") == 0) {
|
|
|
|
res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
|
|
|
|
} else if (strcmp(entity, "issuer") == 0) {
|
|
|
|
res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT("Unknown entity" == 0);
|
|
|
|
}
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(res != -1);
|
|
|
|
TEST_ASSERT(res != -2);
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp(buf, result_str), 0);
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2022-10-04 17:49:16 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
|
2022-10-04 17:49:16 +02:00
|
|
|
{
|
2023-04-20 14:48:19 +02:00
|
|
|
unsigned char *name = NULL;
|
2022-10-04 17:49:16 +02:00
|
|
|
unsigned char *p;
|
|
|
|
size_t name_len;
|
2022-10-20 11:18:37 +02:00
|
|
|
mbedtls_x509_name head;
|
2022-10-17 18:59:10 +02:00
|
|
|
int ret;
|
2022-10-04 17:49:16 +02:00
|
|
|
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&head, 0, sizeof(head));
|
2022-10-20 11:18:37 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
|
2022-10-04 17:49:16 +02:00
|
|
|
p = name;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
|
|
|
|
if (ret == 0) {
|
|
|
|
mbedtls_asn1_free_named_data_list_shallow(head.next);
|
|
|
|
}
|
2022-10-04 17:49:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(ret, exp_ret);
|
2022-10-04 17:49:16 +02:00
|
|
|
|
2023-04-20 11:59:52 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(name);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2022-10-04 17:49:16 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-06-17 16:59:58 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_dn_get_next(char *name_str,
|
|
|
|
int next_merged,
|
|
|
|
char *expected_oids,
|
|
|
|
int exp_count,
|
|
|
|
char *exp_dn_gets)
|
2022-06-17 16:59:58 +02:00
|
|
|
{
|
|
|
|
int ret = 0, i;
|
2022-06-23 12:58:02 +02:00
|
|
|
size_t len = 0, out_size;
|
2022-06-17 16:59:58 +02:00
|
|
|
mbedtls_asn1_named_data *names = NULL;
|
2022-06-27 01:32:09 +02:00
|
|
|
mbedtls_x509_name parsed, *parsed_cur;
|
2022-06-23 12:58:02 +02:00
|
|
|
// Size of buf is maximum required for test cases
|
|
|
|
unsigned char buf[80], *out = NULL, *c;
|
2022-06-17 16:59:58 +02:00
|
|
|
const char *short_name;
|
|
|
|
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&parsed, 0, sizeof(parsed));
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
c = buf + sizeof(buf);
|
2022-06-23 12:58:02 +02:00
|
|
|
// Additional size required for trailing space
|
2023-01-11 14:50:10 +01:00
|
|
|
out_size = strlen(expected_oids) + 2;
|
|
|
|
ASSERT_ALLOC(out, out_size);
|
2022-06-17 16:59:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
|
2022-06-17 16:59:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_write_names(&c, buf, names);
|
|
|
|
TEST_LE_S(0, ret);
|
2022-06-17 16:59:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
|
|
|
|
TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
|
2022-06-17 16:59:58 +02:00
|
|
|
|
|
|
|
// Iterate over names and set next_merged nodes
|
|
|
|
parsed_cur = &parsed;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
|
2022-06-20 12:47:57 +02:00
|
|
|
parsed_cur->next_merged = next_merged & 0x01;
|
2022-06-17 16:59:58 +02:00
|
|
|
parsed_cur = parsed_cur->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over RDN nodes and print OID of first element to buffer
|
|
|
|
parsed_cur = &parsed;
|
|
|
|
len = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; parsed_cur != NULL; i++) {
|
|
|
|
TEST_EQUAL(mbedtls_oid_get_attr_short_name(&parsed_cur->oid,
|
|
|
|
&short_name), 0);
|
|
|
|
len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
|
|
|
|
parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
|
2022-06-17 16:59:58 +02:00
|
|
|
}
|
|
|
|
out[len-1] = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(exp_count, i);
|
|
|
|
TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
|
|
|
|
mbedtls_free(out);
|
2022-06-23 12:58:02 +02:00
|
|
|
out = NULL;
|
2022-06-17 16:59:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
out_size = strlen(exp_dn_gets) + 1;
|
|
|
|
ASSERT_ALLOC(out, out_size);
|
2022-06-23 12:58:02 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
|
|
|
|
TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
|
2022-06-17 16:59:58 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(out);
|
|
|
|
mbedtls_asn1_free_named_data_list(&names);
|
|
|
|
mbedtls_asn1_free_named_data_list_shallow(parsed.next);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2022-06-17 16:59:58 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
|
2013-08-16 13:38:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(entity, "valid_from") == 0) {
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_from), result);
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(entity, "valid_to") == 0) {
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_to), result);
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
TEST_ASSERT("Unknown entity" == 0);
|
|
|
|
}
|
2012-02-11 19:43:20 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
|
2014-03-10 12:26:11 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(entity, "valid_from") == 0) {
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_from), result);
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(entity, "valid_to") == 0) {
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_to), result);
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
TEST_ASSERT("Unknown entity" == 0);
|
|
|
|
}
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2014-03-10 12:26:11 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509parse_crt_file(char *crt_file, int result)
|
2014-09-26 14:53:04 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2014-09-26 14:53:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2014-09-26 14:53:04 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), result);
|
2014-09-26 14:53:04 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2014-09-26 14:53:04 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509parse_crt(data_t *buf, char *result_str, int result)
|
2009-07-19 21:36:15 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2020-10-09 10:19:39 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2020-10-09 10:45:19 +02:00
|
|
|
unsigned char output[2000] = { 0 };
|
2017-05-30 15:23:15 +02:00
|
|
|
int res;
|
2020-10-09 10:36:01 +02:00
|
|
|
#else
|
|
|
|
((void) result_str);
|
2018-12-11 20:55:56 +01:00
|
|
|
#endif
|
2009-07-19 21:36:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2009-07-19 21:36:15 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len), result);
|
2020-10-09 10:19:39 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((result) == 0) {
|
|
|
|
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
|
|
|
|
TEST_ASSERT(res != -1);
|
|
|
|
TEST_ASSERT(res != -2);
|
2019-01-31 10:15:53 +01:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp((char *) output, result_str), 0);
|
2019-01-31 10:15:53 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output, 0, 2000);
|
2018-12-11 20:55:56 +01:00
|
|
|
#endif
|
2019-01-31 10:15:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
|
|
|
mbedtls_x509_crt_init(&crt);
|
2009-07-19 21:36:15 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len), result);
|
2020-10-09 10:19:39 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((result) == 0) {
|
|
|
|
memset(output, 0, 2000);
|
2018-12-11 20:55:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
|
2013-08-20 11:48:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(res != -1);
|
|
|
|
TEST_ASSERT(res != -2);
|
2009-07-19 21:36:15 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp((char *) output, result_str), 0);
|
2020-05-28 23:04:15 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output, 0, 2000);
|
2020-10-09 10:36:01 +02:00
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_INFO */
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
|
|
|
mbedtls_x509_crt_init(&crt);
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL),
|
|
|
|
result);
|
2020-10-09 10:36:01 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((result) == 0) {
|
|
|
|
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(res != -1);
|
|
|
|
TEST_ASSERT(res != -2);
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp((char *) output, result_str), 0);
|
2020-05-28 23:04:15 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output, 0, 2000);
|
2020-10-09 10:36:01 +02:00
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_INFO */
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
|
|
|
mbedtls_x509_crt_init(&crt);
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL),
|
|
|
|
result);
|
2020-10-09 10:36:01 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((result) == 0) {
|
|
|
|
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(res != -1);
|
|
|
|
TEST_ASSERT(res != -2);
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp((char *) output, result_str), 0);
|
2020-05-28 23:04:15 +02:00
|
|
|
}
|
2020-10-09 10:36:01 +02:00
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_INFO */
|
2020-05-28 23:04:15 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2020-05-28 23:04:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509parse_crt_cb(data_t *buf, char *result_str, int result)
|
2020-05-28 23:04:15 +02:00
|
|
|
{
|
|
|
|
mbedtls_x509_crt crt;
|
2020-05-29 22:58:25 +02:00
|
|
|
mbedtls_x509_buf oid;
|
2020-10-09 10:45:19 +02:00
|
|
|
|
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
|
|
|
unsigned char output[2000] = { 0 };
|
2020-05-28 23:04:15 +02:00
|
|
|
int res;
|
2020-10-09 10:45:19 +02:00
|
|
|
#else
|
|
|
|
((void) result_str);
|
|
|
|
#endif
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2020-05-29 22:58:25 +02:00
|
|
|
oid.tag = MBEDTLS_ASN1_OID;
|
|
|
|
oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
|
2023-01-11 14:50:10 +01:00
|
|
|
oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
|
2020-05-29 22:58:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
|
|
|
|
&oid), result);
|
2020-10-09 10:45:19 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((result) == 0) {
|
|
|
|
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(res != -1);
|
|
|
|
TEST_ASSERT(res != -2);
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp((char *) output, result_str), 0);
|
2020-05-28 23:04:15 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output, 0, 2000);
|
2020-10-09 10:45:19 +02:00
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_INFO */
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
|
|
|
mbedtls_x509_crt_init(&crt);
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
|
|
|
|
&oid), (result));
|
2020-10-09 10:45:19 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((result) == 0) {
|
|
|
|
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(res != -1);
|
|
|
|
TEST_ASSERT(res != -2);
|
2020-05-28 23:04:15 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp((char *) output, result_str), 0);
|
2009-07-19 21:36:15 +02:00
|
|
|
}
|
2020-10-09 10:45:19 +02:00
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_INFO */
|
2012-02-11 19:43:20 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2009-07-19 21:36:15 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-19 21:36:15 +02:00
|
|
|
|
2020-10-09 10:19:39 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509parse_crl(data_t *buf, char *result_str, int result)
|
2009-07-20 22:35:41 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crl crl;
|
2009-07-20 22:35:41 +02:00
|
|
|
unsigned char output[2000];
|
2017-05-30 15:23:15 +02:00
|
|
|
int res;
|
2009-07-20 22:35:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crl_init(&crl);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output, 0, 2000);
|
2009-07-20 22:35:41 +02:00
|
|
|
|
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crl_parse(&crl, buf->x, buf->len), (result));
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((result) == 0) {
|
|
|
|
res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
|
2013-08-20 11:48:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(res != -1);
|
|
|
|
TEST_ASSERT(res != -2);
|
2009-07-20 22:35:41 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp((char *) output, result_str), 0);
|
2009-07-20 22:35:41 +02:00
|
|
|
}
|
2012-02-11 19:43:20 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crl_free(&crl);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2009-07-20 22:35:41 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-20 22:35:41 +02:00
|
|
|
|
2020-10-09 10:19:39 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
|
2014-06-13 11:13:15 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr csr;
|
2014-06-13 11:13:15 +02:00
|
|
|
char my_out[1000];
|
|
|
|
int my_ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_csr_init(&csr);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(my_out, 0, sizeof(my_out));
|
2014-06-13 11:13:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(my_ret, ref_ret);
|
2014-06-13 11:13:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ref_ret == 0) {
|
|
|
|
size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(my_out_len, strlen(ref_out));
|
|
|
|
TEST_EQUAL(strcmp(my_out, ref_out), 0);
|
2014-06-13 11:13:15 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_csr_free(&csr);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2014-06-13 11:13:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-01-25 16:19:50 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
|
|
|
void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
|
|
|
|
{
|
|
|
|
mbedtls_x509_csr csr;
|
|
|
|
char my_out[1000];
|
|
|
|
int my_ret;
|
|
|
|
|
|
|
|
mbedtls_x509_csr_init(&csr);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
|
|
|
|
2023-01-25 16:19:50 +01:00
|
|
|
memset(my_out, 0, sizeof(my_out));
|
|
|
|
|
|
|
|
my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(my_ret, ref_ret);
|
2023-01-25 16:19:50 +01:00
|
|
|
|
|
|
|
if (ref_ret == 0) {
|
|
|
|
size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(my_out_len, strlen(ref_out));
|
|
|
|
TEST_EQUAL(strcmp(my_out, ref_out), 0);
|
2023-01-25 16:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_x509_csr_free(&csr);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2023-01-25 16:19:50 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
|
2013-11-26 16:43:39 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt chain, *cur;
|
2013-11-26 16:43:39 +01:00
|
|
|
int i;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&chain);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2013-11-26 16:43:39 +01:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_path(&chain, crt_path), ret);
|
2013-11-26 16:43:39 +01:00
|
|
|
|
|
|
|
/* Check how many certs we got */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
|
|
|
|
if (cur->raw.p != NULL) {
|
2013-11-26 16:43:39 +01:00
|
|
|
i++;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-26 16:43:39 +01:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(i, nb_crt);
|
2013-11-26 16:43:39 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&chain);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2013-11-26 16:43:39 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2017-06-05 13:49:44 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
|
|
|
|
int ret_chk, int flags_chk)
|
2017-06-05 13:49:44 +02:00
|
|
|
{
|
|
|
|
char file_buf[128];
|
|
|
|
int ret;
|
|
|
|
uint32_t flags;
|
|
|
|
mbedtls_x509_crt trusted, chain;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
|
|
|
|
* with NN.crt signed by NN-1.crt
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&trusted);
|
|
|
|
mbedtls_x509_crt_init(&chain);
|
2023-04-20 11:59:52 +02:00
|
|
|
MD_OR_USE_PSA_INIT();
|
2017-06-05 13:49:44 +02:00
|
|
|
|
|
|
|
/* Load trusted root */
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, ca_file), 0);
|
2017-06-05 13:49:44 +02:00
|
|
|
|
|
|
|
/* Load a chain with nb_int intermediates (from 01 to nb_int),
|
|
|
|
* plus one "end-entity" cert (nb_int + 1) */
|
2023-02-02 13:40:50 +01:00
|
|
|
ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
|
2023-01-11 14:50:10 +01:00
|
|
|
nb_int + 1);
|
2023-02-02 13:40:50 +01:00
|
|
|
TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, file_buf), 0);
|
2017-06-05 13:49:44 +02:00
|
|
|
|
|
|
|
/* Try to verify that chain */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
|
|
|
|
NULL, NULL);
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(ret, ret_chk);
|
|
|
|
TEST_EQUAL(flags, (uint32_t) flags_chk);
|
2017-06-05 13:49:44 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&chain);
|
|
|
|
mbedtls_x509_crt_free(&trusted);
|
2023-03-17 14:02:49 +01:00
|
|
|
MD_OR_USE_PSA_DONE();
|
2017-06-05 13:49:44 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-10-11 10:25:22 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
|
|
|
|
int flags_result, int result,
|
|
|
|
char *profile_name, int vrfy_fatal_lvls)
|
2015-10-11 10:25:22 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
char *act;
|
2015-10-11 10:25:22 +02:00
|
|
|
uint32_t flags;
|
2017-05-22 12:04:25 +02:00
|
|
|
int res;
|
2015-10-30 09:23:19 +01:00
|
|
|
mbedtls_x509_crt trusted, chain;
|
2017-05-22 12:04:25 +02:00
|
|
|
const mbedtls_x509_crt_profile *profile = NULL;
|
2015-10-11 16:17:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&chain);
|
|
|
|
mbedtls_x509_crt_init(&trusted);
|
2023-04-20 11:59:52 +02:00
|
|
|
MD_OR_USE_PSA_INIT();
|
2015-10-11 10:25:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while ((act = mystrsep(&chain_paths, " ")) != NULL) {
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, act), 0);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, trusted_ca), 0);
|
2015-10-11 10:25:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(profile_name, "") == 0) {
|
2017-05-22 12:04:25 +02:00
|
|
|
profile = &mbedtls_x509_crt_profile_default;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(profile_name, "next") == 0) {
|
2017-05-22 12:04:25 +02:00
|
|
|
profile = &mbedtls_x509_crt_profile_next;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(profile_name, "suiteb") == 0) {
|
2017-05-22 12:04:25 +02:00
|
|
|
profile = &mbedtls_x509_crt_profile_suiteb;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(profile_name, "rsa3072") == 0) {
|
2017-05-23 11:29:29 +02:00
|
|
|
profile = &profile_rsa3072;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(profile_name, "sha512") == 0) {
|
2017-05-23 11:29:29 +02:00
|
|
|
profile = &profile_sha512;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-05-22 12:04:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
|
|
|
|
NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
|
2015-10-11 16:17:27 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(res, (result));
|
|
|
|
TEST_EQUAL(flags, (uint32_t) (flags_result));
|
2015-10-11 10:25:22 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&trusted);
|
|
|
|
mbedtls_x509_crt_free(&chain);
|
2023-03-17 14:02:49 +01:00
|
|
|
MD_OR_USE_PSA_DONE();
|
2015-10-11 10:25:22 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-10-09 10:19:39 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509_oid_desc(data_t *buf, char *ref_desc)
|
2014-03-28 16:06:35 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_buf oid;
|
2015-03-20 19:14:26 +01:00
|
|
|
const char *desc = NULL;
|
|
|
|
int ret;
|
2014-03-28 16:06:35 +01:00
|
|
|
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_INIT();
|
2023-04-20 11:59:52 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
oid.tag = MBEDTLS_ASN1_OID;
|
2017-06-09 05:32:58 +02:00
|
|
|
oid.p = buf->x;
|
|
|
|
oid.len = buf->len;
|
2014-03-28 16:06:35 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_oid_get_extended_key_usage(&oid, &desc);
|
2014-03-28 16:06:35 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(ref_desc, "notfound") == 0) {
|
|
|
|
TEST_ASSERT(ret != 0);
|
|
|
|
TEST_ASSERT(desc == NULL);
|
|
|
|
} else {
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(ret, 0);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(desc != NULL);
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(strcmp(desc, ref_desc), 0);
|
2014-03-28 16:06:35 +01:00
|
|
|
}
|
2023-04-20 11:59:52 +02:00
|
|
|
|
|
|
|
exit:
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2014-03-28 16:06:35 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
|
2014-03-28 16:06:35 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_buf oid;
|
2014-03-28 16:06:35 +01:00
|
|
|
char num_buf[100];
|
|
|
|
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_INIT();
|
2023-04-20 11:59:52 +02:00
|
|
|
|
2023-02-02 13:40:50 +01:00
|
|
|
memset(num_buf, 0x2a, sizeof(num_buf));
|
2014-03-28 16:06:35 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
oid.tag = MBEDTLS_ASN1_OID;
|
2017-06-09 05:32:58 +02:00
|
|
|
oid.p = oid_buf->x;
|
|
|
|
oid.len = oid_buf->len;
|
2014-03-28 16:06:35 +01:00
|
|
|
|
2023-02-02 13:40:50 +01:00
|
|
|
TEST_ASSERT((size_t) blen <= sizeof(num_buf));
|
2014-03-28 16:06:35 +01:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_oid_get_numeric_string(num_buf, blen, &oid), ret);
|
2014-03-28 16:06:35 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret >= 0) {
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(num_buf[ret], 0);
|
|
|
|
TEST_EQUAL(strcmp(num_buf, numstr), 0);
|
2014-03-28 16:06:35 +01:00
|
|
|
}
|
2023-04-20 11:59:52 +02:00
|
|
|
|
|
|
|
exit:
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2014-03-28 16:06:35 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-07 13:52:23 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509_check_key_usage(char *crt_file, int usage, int ret)
|
2014-04-09 09:50:03 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2014-04-09 09:50:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2014-04-09 09:50:03 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
|
2014-04-09 09:50:03 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_check_key_usage(&crt, usage), ret);
|
2014-04-09 09:50:03 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2014-04-09 09:50:03 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-07 13:52:23 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
|
|
|
|
)
|
2014-04-10 17:53:56 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2014-04-10 17:53:56 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&crt);
|
2023-04-20 11:59:52 +02:00
|
|
|
USE_PSA_INIT();
|
2014-04-10 17:53:56 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
|
2014-04-10 17:53:56 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x, oid->len),
|
|
|
|
ret);
|
2014-04-10 17:53:56 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&crt);
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2014-04-10 17:53:56 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2016-09-23 14:16:02 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
|
|
|
|
int day, int hour, int min, int sec)
|
2016-09-23 14:16:02 +02:00
|
|
|
{
|
|
|
|
mbedtls_x509_time time;
|
2017-02-08 15:13:02 +01:00
|
|
|
unsigned char buf[21];
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char *start = buf;
|
|
|
|
unsigned char *end = buf;
|
2016-09-23 14:16:02 +02:00
|
|
|
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&time, 0x00, sizeof(time));
|
|
|
|
*end = (unsigned char) tag; end++;
|
|
|
|
*end = strlen(time_str);
|
|
|
|
TEST_ASSERT(*end < 20);
|
2016-09-23 14:16:02 +02:00
|
|
|
end++;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(end, time_str, (size_t) *(end - 1));
|
2016-09-23 14:16:02 +02:00
|
|
|
end += *(end - 1);
|
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(mbedtls_x509_get_time(&start, end, &time), ret);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(year, time.year);
|
|
|
|
TEST_EQUAL(mon, time.mon);
|
|
|
|
TEST_EQUAL(day, time.day);
|
|
|
|
TEST_EQUAL(hour, time.hour);
|
|
|
|
TEST_EQUAL(min, time.min);
|
|
|
|
TEST_EQUAL(sec, time.sec);
|
2016-09-23 14:16:02 +02:00
|
|
|
}
|
2023-04-20 11:59:52 +02:00
|
|
|
exit:
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2016-09-23 14:16:02 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2023-01-11 14:50:10 +01:00
|
|
|
void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
|
|
|
|
int ref_msg_md, int ref_mgf_md,
|
|
|
|
int ref_salt_len, int ref_ret)
|
2014-06-06 14:48:38 +02:00
|
|
|
{
|
|
|
|
int my_ret;
|
2020-06-26 14:33:03 +02:00
|
|
|
mbedtls_x509_buf buf;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_type_t my_msg_md, my_mgf_md;
|
2014-06-06 14:48:38 +02:00
|
|
|
int my_salt_len;
|
|
|
|
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_INIT();
|
|
|
|
|
2020-06-26 14:33:03 +02:00
|
|
|
buf.p = params->x;
|
|
|
|
buf.len = params->len;
|
|
|
|
buf.tag = params_tag;
|
2014-06-06 14:48:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
|
|
|
|
&my_salt_len);
|
2014-06-06 14:48:38 +02:00
|
|
|
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(my_ret, ref_ret);
|
2014-06-06 14:48:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ref_ret == 0) {
|
2022-11-27 04:19:48 +01:00
|
|
|
TEST_EQUAL(my_msg_md, (mbedtls_md_type_t) ref_msg_md);
|
|
|
|
TEST_EQUAL(my_mgf_md, (mbedtls_md_type_t) ref_mgf_md);
|
|
|
|
TEST_EQUAL(my_salt_len, ref_salt_len);
|
2014-06-06 14:48:38 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-04-19 14:53:36 +02:00
|
|
|
USE_PSA_DONE();
|
2014-06-06 14:48:38 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-02-12 16:11:17 +01:00
|
|
|
|
2023-05-16 16:24:44 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
|
2023-04-06 11:11:58 +02:00
|
|
|
void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret)
|
2021-02-12 16:11:17 +01:00
|
|
|
{
|
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
|
|
|
|
mbedtls_x509_crt_init(&crt);
|
|
|
|
|
2023-04-24 10:18:52 +02:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
|
2021-02-12 16:11:17 +01:00
|
|
|
|
2021-05-04 15:41:35 +02:00
|
|
|
if (ref_ret == 0) {
|
2023-04-24 10:18:52 +02:00
|
|
|
TEST_EQUAL(crt.subject_key_id.tag, MBEDTLS_ASN1_OCTET_STRING);
|
|
|
|
TEST_EQUAL(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len), 0);
|
|
|
|
TEST_EQUAL(crt.subject_key_id.len, subjectKeyId->len);
|
2021-05-04 15:41:35 +02:00
|
|
|
} else {
|
2023-04-24 10:18:52 +02:00
|
|
|
TEST_EQUAL(crt.subject_key_id.tag, 0);
|
|
|
|
TEST_EQUAL(crt.subject_key_id.len, 0);
|
2021-02-12 16:11:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_x509_crt_free(&crt);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-05-16 16:24:44 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
|
2023-04-06 09:23:25 +02:00
|
|
|
void x509_crt_parse_authoritykeyid(char *file,
|
2023-05-16 19:14:19 +02:00
|
|
|
data_t *keyId,
|
2021-05-06 08:22:17 +02:00
|
|
|
char *authorityKeyId_issuer,
|
2023-05-16 19:14:19 +02:00
|
|
|
data_t *serial,
|
2021-05-06 08:22:17 +02:00
|
|
|
int ref_ret)
|
2021-02-12 16:11:17 +01:00
|
|
|
{
|
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
int bufferCounter = 0;
|
|
|
|
size_t issuerCounter = 0;
|
2021-05-04 15:41:35 +02:00
|
|
|
unsigned int result = 0;
|
2023-04-03 10:19:22 +02:00
|
|
|
mbedtls_x509_subject_alternative_name san;
|
2023-02-09 09:29:34 +01:00
|
|
|
mbedtls_x509_name *pname = NULL;
|
2021-02-12 16:11:17 +01:00
|
|
|
|
|
|
|
mbedtls_x509_crt_init(&crt);
|
|
|
|
|
2023-04-24 10:18:52 +02:00
|
|
|
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
|
2021-02-12 16:11:17 +01:00
|
|
|
|
2021-05-04 15:41:35 +02:00
|
|
|
if (ref_ret == 0) {
|
2021-02-12 16:11:17 +01:00
|
|
|
/* KeyId test */
|
2023-05-16 19:14:19 +02:00
|
|
|
if (keyId->len > 0) {
|
2023-04-24 10:18:52 +02:00
|
|
|
TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, MBEDTLS_ASN1_OCTET_STRING);
|
|
|
|
TEST_EQUAL(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len), 0);
|
|
|
|
TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, keyId->len);
|
2023-05-16 16:24:44 +02:00
|
|
|
} else {
|
|
|
|
TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
|
|
|
|
TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
|
2023-04-18 08:38:16 +02:00
|
|
|
}
|
2021-02-12 16:11:17 +01:00
|
|
|
|
2023-05-16 16:24:44 +02:00
|
|
|
|
2021-02-12 16:11:17 +01:00
|
|
|
/* Issuer test */
|
2023-05-16 19:14:19 +02:00
|
|
|
if (strlen(authorityKeyId_issuer) > 0) {
|
2023-04-18 08:38:16 +02:00
|
|
|
mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer;
|
2023-02-09 09:29:34 +01:00
|
|
|
|
2023-04-24 10:18:52 +02:00
|
|
|
TEST_EQUAL(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san), 0);
|
2023-02-09 09:29:34 +01:00
|
|
|
|
2023-04-18 08:38:16 +02:00
|
|
|
pname = &san.san.directory_name;
|
2023-02-09 09:29:34 +01:00
|
|
|
|
2023-04-18 08:38:16 +02:00
|
|
|
while (pname != NULL) {
|
|
|
|
for (issuerCounter = 0; issuerCounter < pname->val.len; issuerCounter++) {
|
|
|
|
result |=
|
|
|
|
(authorityKeyId_issuer[bufferCounter++] != pname->val.p[issuerCounter]);
|
|
|
|
}
|
|
|
|
bufferCounter++; /* Skipping the slash */
|
|
|
|
pname = pname->next;
|
2021-02-12 16:11:17 +01:00
|
|
|
}
|
2023-04-18 08:38:16 +02:00
|
|
|
mbedtls_x509_free_subject_alt_name(&san);
|
2023-04-24 10:18:52 +02:00
|
|
|
TEST_EQUAL(result, 0);
|
2021-02-12 16:11:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Serial test */
|
2023-05-16 19:14:19 +02:00
|
|
|
if (serial->len > 0) {
|
2023-04-24 10:18:52 +02:00
|
|
|
TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag,
|
2023-05-16 19:14:19 +02:00
|
|
|
MBEDTLS_ASN1_INTEGER);
|
2023-04-24 10:18:52 +02:00
|
|
|
TEST_EQUAL(memcmp(crt.authority_key_id.authorityCertSerialNumber.p,
|
2023-05-16 19:14:19 +02:00
|
|
|
serial->x, serial->len), 0);
|
2023-04-24 10:18:52 +02:00
|
|
|
TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, serial->len);
|
2023-05-16 16:24:44 +02:00
|
|
|
} else {
|
|
|
|
TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
|
|
|
|
TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
|
2023-04-18 08:38:16 +02:00
|
|
|
}
|
2023-04-06 11:11:58 +02:00
|
|
|
|
2021-05-04 15:41:35 +02:00
|
|
|
} else {
|
2023-04-24 10:18:52 +02:00
|
|
|
TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
|
|
|
|
TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
|
2021-02-12 16:11:17 +01:00
|
|
|
|
2023-04-24 10:18:52 +02:00
|
|
|
TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
|
|
|
|
TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
|
2021-02-12 16:11:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_x509_crt_free(&crt);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|