| 1 |
/* BEGIN_HEADER */ |
| 2 |
#include "mbedtls/private/bignum.h" |
| 3 |
#include "mbedtls/x509.h" |
| 4 |
#include "mbedtls/x509_crt.h" |
| 5 |
#include "mbedtls/x509_crl.h" |
| 6 |
#include "mbedtls/x509_csr.h" |
| 7 |
#include "x509_internal.h" |
| 8 |
#include "mbedtls/pem.h" |
| 9 |
#include "mbedtls/oid.h" |
| 10 |
#include "x509_oid.h" |
| 11 |
#include "mbedtls/base64.h" |
| 12 |
#include "mbedtls/error.h" |
| 13 |
#include "mbedtls/pk.h" |
| 14 |
#if defined(MBEDTLS_PK_HAVE_PRIVATE_HEADER) |
| 15 |
#include <mbedtls/private/pk_private.h> |
| 16 |
#endif /* MBEDTLS_PK_HAVE_PRIVATE_HEADER */ |
| 17 |
#include "mbedtls/asn1.h" |
| 18 |
#include "mbedtls/asn1write.h" |
| 19 |
#include "string.h" |
| 20 |
|
| 21 |
#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19 |
| 22 |
#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \ |
| 23 |
than the current threshold 19. To test larger values, please \ |
| 24 |
adapt the script framework/data_files/dir-max/long.sh." |
| 25 |
#endif |
| 26 |
|
| 27 |
/* Test-only profile allowing all digests, PK algorithms, and curves. */ |
| 28 |
const mbedtls_x509_crt_profile profile_all = |
| 29 |
{ |
| 30 |
0xFFFFFFFF, /* Any MD */ |
| 31 |
0xFFFFFFFF, /* Any PK alg */ |
| 32 |
0xFFFFFFFF, /* Any curve */ |
| 33 |
1024, |
| 34 |
}; |
| 35 |
|
| 36 |
/* Profile for backward compatibility. Allows SHA-1, unlike the default |
| 37 |
profile. */ |
| 38 |
const mbedtls_x509_crt_profile compat_profile = |
| 39 |
{ |
| 40 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) | |
| 41 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) | |
| 42 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) | |
| 43 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) | |
| 44 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) | |
| 45 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512), |
| 46 |
0xFFFFFFFF, /* Any PK alg */ |
| 47 |
0xFFFFFFFF, /* Any curve */ |
| 48 |
1024, |
| 49 |
}; |
| 50 |
|
| 51 |
const mbedtls_x509_crt_profile profile_rsa3072 = |
| 52 |
{ |
| 53 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) | |
| 54 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) | |
| 55 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512), |
| 56 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA), |
| 57 |
0, |
| 58 |
3072, |
| 59 |
}; |
| 60 |
|
| 61 |
const mbedtls_x509_crt_profile profile_sha512 = |
| 62 |
{ |
| 63 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512), |
| 64 |
0xFFFFFFFF, /* Any PK alg */ |
| 65 |
0xFFFFFFFF, /* Any curve */ |
| 66 |
1024, |
| 67 |
}; |
| 68 |
|
| 69 |
#if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 70 |
|
| 71 |
#if defined(MBEDTLS_FS_IO) |
| 72 |
static int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags) |
| 73 |
{ |
| 74 |
((void) data); |
| 75 |
((void) crt); |
| 76 |
((void) certificate_depth); |
| 77 |
*flags |= MBEDTLS_X509_BADCERT_OTHER; |
| 78 |
|
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
static int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags) |
| 83 |
{ |
| 84 |
((void) data); |
| 85 |
((void) crt); |
| 86 |
((void) certificate_depth); |
| 87 |
*flags = 0; |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
#if defined(MBEDTLS_X509_CRL_PARSE_C) && \ |
| 93 |
defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 94 |
static int ca_callback_fail(void *data, mbedtls_x509_crt const *child, |
| 95 |
mbedtls_x509_crt **candidates) |
| 96 |
{ |
| 97 |
((void) data); |
| 98 |
((void) child); |
| 99 |
((void) candidates); |
| 100 |
|
| 101 |
return -1; |
| 102 |
} |
| 103 |
|
| 104 |
static int ca_callback(void *data, mbedtls_x509_crt const *child, |
| 105 |
mbedtls_x509_crt **candidates) |
| 106 |
{ |
| 107 |
int ret = 0; |
| 108 |
mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data; |
| 109 |
mbedtls_x509_crt *first; |
| 110 |
|
| 111 |
/* This is a test-only implementation of the CA callback |
| 112 |
* which always returns the entire list of trusted certificates. |
| 113 |
* Production implementations managing a large number of CAs |
| 114 |
* should use an efficient presentation and lookup for the |
| 115 |
* set of trusted certificates (such as a hashtable) and only |
| 116 |
* return those trusted certificates which satisfy basic |
| 117 |
* parental checks, such as the matching of child `Issuer` |
| 118 |
* and parent `Subject` field. */ |
| 119 |
((void) child); |
| 120 |
|
| 121 |
first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 122 |
if (first == NULL) { |
| 123 |
ret = -1; |
| 124 |
goto exit; |
| 125 |
} |
| 126 |
mbedtls_x509_crt_init(first); |
| 127 |
|
| 128 |
if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) { |
| 129 |
ret = -1; |
| 130 |
goto exit; |
| 131 |
} |
| 132 |
|
| 133 |
while (ca->next != NULL) { |
| 134 |
ca = ca->next; |
| 135 |
if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) { |
| 136 |
ret = -1; |
| 137 |
goto exit; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
exit: |
| 142 |
|
| 143 |
if (ret != 0) { |
| 144 |
mbedtls_x509_crt_free(first); |
| 145 |
mbedtls_free(first); |
| 146 |
first = NULL; |
| 147 |
} |
| 148 |
|
| 149 |
*candidates = first; |
| 150 |
return ret; |
| 151 |
} |
| 152 |
#endif /* MBEDTLS_X509_CRL_PARSE_C && MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 153 |
|
| 154 |
static int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags) |
| 155 |
{ |
| 156 |
int *levels = (int *) data; |
| 157 |
|
| 158 |
((void) crt); |
| 159 |
((void) certificate_depth); |
| 160 |
|
| 161 |
/* Simulate a fatal error in the callback */ |
| 162 |
if (*levels & (1 << certificate_depth)) { |
| 163 |
*flags |= (1 << certificate_depth); |
| 164 |
return -1 - certificate_depth; |
| 165 |
} |
| 166 |
|
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
/* strsep() not available on Windows */ |
| 171 |
static char *mystrsep(char **stringp, const char *delim) |
| 172 |
{ |
| 173 |
const char *p; |
| 174 |
char *ret = *stringp; |
| 175 |
|
| 176 |
if (*stringp == NULL) { |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
|
| 180 |
for (;; (*stringp)++) { |
| 181 |
if (**stringp == '\0') { |
| 182 |
*stringp = NULL; |
| 183 |
goto done; |
| 184 |
} |
| 185 |
|
| 186 |
for (p = delim; *p != '\0'; p++) { |
| 187 |
if (**stringp == *p) { |
| 188 |
**stringp = '\0'; |
| 189 |
(*stringp)++; |
| 190 |
goto done; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
done: |
| 196 |
return ret; |
| 197 |
} |
| 198 |
|
| 199 |
typedef struct { |
| 200 |
char buf[512]; |
| 201 |
char *p; |
| 202 |
} verify_print_context; |
| 203 |
|
| 204 |
static void verify_print_init(verify_print_context *ctx) |
| 205 |
{ |
| 206 |
memset(ctx, 0, sizeof(verify_print_context)); |
| 207 |
ctx->p = ctx->buf; |
| 208 |
} |
| 209 |
|
| 210 |
static int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags) |
| 211 |
{ |
| 212 |
int ret; |
| 213 |
verify_print_context *ctx = (verify_print_context *) data; |
| 214 |
char *p = ctx->p; |
| 215 |
size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p; |
| 216 |
((void) flags); |
| 217 |
|
| 218 |
ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth); |
| 219 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 220 |
|
| 221 |
ret = mbedtls_x509_serial_gets(p, n, &crt->serial); |
| 222 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 223 |
|
| 224 |
ret = mbedtls_snprintf(p, n, " - subject "); |
| 225 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 226 |
|
| 227 |
ret = mbedtls_x509_dn_gets(p, n, &crt->subject); |
| 228 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 229 |
|
| 230 |
ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags); |
| 231 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 232 |
|
| 233 |
ctx->p = p; |
| 234 |
|
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
static int verify_parse_san(mbedtls_x509_subject_alternative_name *san, |
| 239 |
char **buf, size_t *size) |
| 240 |
{ |
| 241 |
int ret; |
| 242 |
size_t i; |
| 243 |
char *p = *buf; |
| 244 |
size_t n = *size; |
| 245 |
|
| 246 |
ret = mbedtls_snprintf(p, n, "type : %d", san->type); |
| 247 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 248 |
|
| 249 |
switch (san->type) { |
| 250 |
case (MBEDTLS_X509_SAN_OTHER_NAME): |
| 251 |
ret = mbedtls_snprintf(p, n, "\notherName :"); |
| 252 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 253 |
|
| 254 |
if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, |
| 255 |
&san->san.other_name.type_id) == 0) { |
| 256 |
ret = mbedtls_snprintf(p, n, " hardware module name :"); |
| 257 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 258 |
ret = mbedtls_snprintf(p, n, " hardware type : "); |
| 259 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 260 |
|
| 261 |
ret = mbedtls_oid_get_numeric_string(p, |
| 262 |
n, |
| 263 |
&san->san.other_name.value.hardware_module_name |
| 264 |
.oid); |
| 265 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 266 |
|
| 267 |
ret = mbedtls_snprintf(p, n, ", hardware serial number : "); |
| 268 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 269 |
|
| 270 |
for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) { |
| 271 |
ret = mbedtls_snprintf(p, |
| 272 |
n, |
| 273 |
"%02X", |
| 274 |
san->san.other_name.value.hardware_module_name.val.p[i]); |
| 275 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 276 |
} |
| 277 |
} |
| 278 |
break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */ |
| 279 |
case (MBEDTLS_X509_SAN_DNS_NAME): |
| 280 |
ret = mbedtls_snprintf(p, n, "\ndNSName : "); |
| 281 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 282 |
if (san->san.unstructured_name.len >= n) { |
| 283 |
*p = '\0'; |
| 284 |
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 285 |
} |
| 286 |
n -= san->san.unstructured_name.len; |
| 287 |
for (i = 0; i < san->san.unstructured_name.len; i++) { |
| 288 |
*p++ = san->san.unstructured_name.p[i]; |
| 289 |
} |
| 290 |
break;/* MBEDTLS_X509_SAN_DNS_NAME */ |
| 291 |
case (MBEDTLS_X509_SAN_RFC822_NAME): |
| 292 |
ret = mbedtls_snprintf(p, n, "\nrfc822Name : "); |
| 293 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 294 |
if (san->san.unstructured_name.len >= n) { |
| 295 |
*p = '\0'; |
| 296 |
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 297 |
} |
| 298 |
n -= san->san.unstructured_name.len; |
| 299 |
for (i = 0; i < san->san.unstructured_name.len; i++) { |
| 300 |
*p++ = san->san.unstructured_name.p[i]; |
| 301 |
} |
| 302 |
break;/* MBEDTLS_X509_SAN_RFC822_NAME */ |
| 303 |
case (MBEDTLS_X509_SAN_DIRECTORY_NAME): |
| 304 |
ret = mbedtls_snprintf(p, n, "\ndirectoryName : "); |
| 305 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 306 |
ret = mbedtls_x509_dn_gets(p, n, &san->san.directory_name); |
| 307 |
if (ret < 0) { |
| 308 |
return ret; |
| 309 |
} |
| 310 |
|
| 311 |
p += ret; |
| 312 |
n -= ret; |
| 313 |
break;/* MBEDTLS_X509_SAN_DIRECTORY_NAME */ |
| 314 |
default: |
| 315 |
/* |
| 316 |
* Should not happen. |
| 317 |
*/ |
| 318 |
return -1; |
| 319 |
} |
| 320 |
ret = mbedtls_snprintf(p, n, "\n"); |
| 321 |
MBEDTLS_X509_SAFE_SNPRINTF; |
| 322 |
|
| 323 |
*size = n; |
| 324 |
*buf = p; |
| 325 |
|
| 326 |
return 0; |
| 327 |
} |
| 328 |
#endif /* MBEDTLS_FS_IO */ |
| 329 |
|
| 330 |
static int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid, |
| 331 |
int critical, const unsigned char *cp, const unsigned char *end) |
| 332 |
{ |
| 333 |
(void) crt; |
| 334 |
(void) critical; |
| 335 |
mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx; |
| 336 |
if (oid->tag == MBEDTLS_ASN1_OID && |
| 337 |
MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) { |
| 338 |
/* Handle unknown certificate policy */ |
| 339 |
int ret, parse_ret = 0; |
| 340 |
size_t len; |
| 341 |
unsigned char **p = (unsigned char **) &cp; |
| 342 |
|
| 343 |
/* Get main sequence tag */ |
| 344 |
ret = mbedtls_asn1_get_tag(p, end, &len, |
| 345 |
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); |
| 346 |
if (ret != 0) { |
| 347 |
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 348 |
} |
| 349 |
|
| 350 |
if (*p + len != end) { |
| 351 |
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 352 |
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 353 |
} |
| 354 |
|
| 355 |
/* |
| 356 |
* Cannot be an empty sequence. |
| 357 |
*/ |
| 358 |
if (len == 0) { |
| 359 |
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 360 |
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 361 |
} |
| 362 |
|
| 363 |
while (*p < end) { |
| 364 |
const unsigned char *policy_end; |
| 365 |
|
| 366 |
/* |
| 367 |
* Get the policy sequence |
| 368 |
*/ |
| 369 |
if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 370 |
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != |
| 371 |
0) { |
| 372 |
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 373 |
} |
| 374 |
|
| 375 |
policy_end = *p + len; |
| 376 |
|
| 377 |
if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len, |
| 378 |
MBEDTLS_ASN1_OID)) != 0) { |
| 379 |
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 380 |
} |
| 381 |
|
| 382 |
/* |
| 383 |
* Recognize exclusively the policy with OID 1 |
| 384 |
*/ |
| 385 |
if (len != 1 || *p[0] != 1) { |
| 386 |
parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; |
| 387 |
} |
| 388 |
|
| 389 |
*p += len; |
| 390 |
|
| 391 |
/* |
| 392 |
* If there is an optional qualifier, then *p < policy_end |
| 393 |
* Check the Qualifier len to verify it doesn't exceed policy_end. |
| 394 |
*/ |
| 395 |
if (*p < policy_end) { |
| 396 |
if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len, |
| 397 |
MBEDTLS_ASN1_CONSTRUCTED | |
| 398 |
MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 399 |
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 400 |
} |
| 401 |
/* |
| 402 |
* Skip the optional policy qualifiers. |
| 403 |
*/ |
| 404 |
*p += len; |
| 405 |
} |
| 406 |
|
| 407 |
if (*p != policy_end) { |
| 408 |
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 409 |
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
if (*p != end) { |
| 414 |
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 415 |
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 416 |
} |
| 417 |
|
| 418 |
return parse_ret; |
| 419 |
} else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len && |
| 420 |
memcmp(new_oid->p, oid->p, oid->len) == 0) { |
| 421 |
return 0; |
| 422 |
} else { |
| 423 |
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 424 |
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 425 |
} |
| 426 |
} |
| 427 |
#endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 428 |
|
| 429 |
#if defined(MBEDTLS_X509_CSR_PARSE_C) && \ |
| 430 |
!defined(MBEDTLS_X509_REMOVE_INFO) |
| 431 |
static int parse_csr_ext_accept_cb(void *p_ctx, |
| 432 |
mbedtls_x509_csr const *csr, |
| 433 |
mbedtls_x509_buf const *oid, |
| 434 |
int critical, |
| 435 |
const unsigned char *cp, |
| 436 |
const unsigned char *end) |
| 437 |
{ |
| 438 |
(void) p_ctx; |
| 439 |
(void) csr; |
| 440 |
(void) oid; |
| 441 |
(void) critical; |
| 442 |
(void) cp; |
| 443 |
(void) end; |
| 444 |
|
| 445 |
return 0; |
| 446 |
} |
| 447 |
|
| 448 |
static int parse_csr_ext_reject_cb(void *p_ctx, |
| 449 |
mbedtls_x509_csr const *csr, |
| 450 |
mbedtls_x509_buf const *oid, |
| 451 |
int critical, |
| 452 |
const unsigned char *cp, |
| 453 |
const unsigned char *end) |
| 454 |
{ |
| 455 |
(void) p_ctx; |
| 456 |
(void) csr; |
| 457 |
(void) oid; |
| 458 |
(void) critical; |
| 459 |
(void) cp; |
| 460 |
(void) end; |
| 461 |
|
| 462 |
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 463 |
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 464 |
} |
| 465 |
#endif /* MBEDTLS_X509_CSR_PARSE_C && !MBEDTLS_X509_REMOVE_INFO */ |
| 466 |
/* END_HEADER */ |
| 467 |
|
| 468 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
| 469 |
void x509_accessor_ext_types(int ext_type, int has_ext_type) |
| 470 |
{ |
| 471 |
mbedtls_x509_crt crt; |
| 472 |
int expected_result = ext_type & has_ext_type; |
| 473 |
|
| 474 |
mbedtls_x509_crt_init(&crt); |
| 475 |
USE_PSA_INIT(); |
| 476 |
|
| 477 |
crt.ext_types = ext_type; |
| 478 |
|
| 479 |
TEST_EQUAL(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type), expected_result); |
| 480 |
|
| 481 |
exit: |
| 482 |
mbedtls_x509_crt_free(&crt); |
| 483 |
USE_PSA_DONE(); |
| 484 |
} |
| 485 |
/* END_CASE */ |
| 486 |
|
| 487 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_TEST_HOOKS */ |
| 488 |
void x509_crt_parse_cn_inet_pton(const char *cn, data_t *exp, int ref_ret) |
| 489 |
{ |
| 490 |
uint32_t addr[4]; |
| 491 |
|
| 492 |
char *cn_local = NULL; |
| 493 |
size_t cn_local_len = strlen(cn) + 1; |
| 494 |
TEST_CALLOC(cn_local, cn_local_len); |
| 495 |
memcpy(cn_local, cn, cn_local_len); |
| 496 |
|
| 497 |
size_t addrlen = mbedtls_x509_crt_parse_cn_inet_pton(cn_local, addr); |
| 498 |
TEST_EQUAL(addrlen, (size_t) ref_ret); |
| 499 |
|
| 500 |
if (addrlen) { |
| 501 |
TEST_MEMORY_COMPARE(exp->x, exp->len, addr, addrlen); |
| 502 |
} |
| 503 |
|
| 504 |
exit: |
| 505 |
mbedtls_free(cn_local); |
| 506 |
|
| 507 |
} |
| 508 |
/* END_CASE */ |
| 509 |
|
| 510 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| 511 |
void x509_parse_san(char *crt_file, char *result_str, int parse_result) |
| 512 |
{ |
| 513 |
int ret; |
| 514 |
mbedtls_x509_crt crt; |
| 515 |
mbedtls_x509_subject_alternative_name san; |
| 516 |
mbedtls_x509_sequence *cur = NULL; |
| 517 |
char buf[2000]; |
| 518 |
char *p = buf; |
| 519 |
size_t n = sizeof(buf); |
| 520 |
|
| 521 |
mbedtls_x509_crt_init(&crt); |
| 522 |
USE_PSA_INIT(); |
| 523 |
memset(buf, 0, 2000); |
| 524 |
|
| 525 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result); |
| 526 |
|
| 527 |
if (parse_result != 0) { |
| 528 |
goto exit; |
| 529 |
} |
| 530 |
if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) { |
| 531 |
cur = &crt.subject_alt_names; |
| 532 |
while (cur != NULL) { |
| 533 |
ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san); |
| 534 |
TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE); |
| 535 |
/* |
| 536 |
* If san type not supported, ignore. |
| 537 |
*/ |
| 538 |
if (ret == 0) { |
| 539 |
ret = verify_parse_san(&san, &p, &n); |
| 540 |
mbedtls_x509_free_subject_alt_name(&san); |
| 541 |
TEST_EQUAL(ret, 0); |
| 542 |
} |
| 543 |
cur = cur->next; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
TEST_EQUAL(strcmp(buf, result_str), 0); |
| 548 |
|
| 549 |
exit: |
| 550 |
mbedtls_x509_crt_free(&crt); |
| 551 |
USE_PSA_DONE(); |
| 552 |
} |
| 553 |
/* END_CASE */ |
| 554 |
|
| 555 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */ |
| 556 |
void x509_cert_info(char *crt_file, char *result_str) |
| 557 |
{ |
| 558 |
mbedtls_x509_crt crt; |
| 559 |
char buf[2000]; |
| 560 |
int res; |
| 561 |
|
| 562 |
mbedtls_x509_crt_init(&crt); |
| 563 |
USE_PSA_INIT(); |
| 564 |
memset(buf, 0, 2000); |
| 565 |
|
| 566 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0); |
| 567 |
res = mbedtls_x509_crt_info(buf, 2000, "", &crt); |
| 568 |
|
| 569 |
TEST_ASSERT(res != -1); |
| 570 |
TEST_ASSERT(res != -2); |
| 571 |
|
| 572 |
TEST_EQUAL(strcmp(buf, result_str), 0); |
| 573 |
|
| 574 |
exit: |
| 575 |
mbedtls_x509_crt_free(&crt); |
| 576 |
USE_PSA_DONE(); |
| 577 |
} |
| 578 |
/* END_CASE */ |
| 579 |
|
| 580 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
| 581 |
void mbedtls_x509_crl_info(char *crl_file, char *result_str) |
| 582 |
{ |
| 583 |
mbedtls_x509_crl crl; |
| 584 |
char buf[2000]; |
| 585 |
int res; |
| 586 |
|
| 587 |
mbedtls_x509_crl_init(&crl); |
| 588 |
USE_PSA_INIT(); |
| 589 |
memset(buf, 0, 2000); |
| 590 |
|
| 591 |
TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0); |
| 592 |
res = mbedtls_x509_crl_info(buf, 2000, "", &crl); |
| 593 |
|
| 594 |
TEST_ASSERT(res != -1); |
| 595 |
TEST_ASSERT(res != -2); |
| 596 |
|
| 597 |
TEST_EQUAL(strcmp(buf, result_str), 0); |
| 598 |
|
| 599 |
exit: |
| 600 |
mbedtls_x509_crl_free(&crl); |
| 601 |
USE_PSA_DONE(); |
| 602 |
} |
| 603 |
/* END_CASE */ |
| 604 |
|
| 605 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */ |
| 606 |
void mbedtls_x509_crl_parse(char *crl_file, int result) |
| 607 |
{ |
| 608 |
mbedtls_x509_crl crl; |
| 609 |
char buf[2000]; |
| 610 |
|
| 611 |
mbedtls_x509_crl_init(&crl); |
| 612 |
USE_PSA_INIT(); |
| 613 |
memset(buf, 0, 2000); |
| 614 |
|
| 615 |
TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), result); |
| 616 |
|
| 617 |
exit: |
| 618 |
mbedtls_x509_crl_free(&crl); |
| 619 |
USE_PSA_DONE(); |
| 620 |
} |
| 621 |
/* END_CASE */ |
| 622 |
|
| 623 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
| 624 |
void mbedtls_x509_csr_info(char *csr_file, char *result_str) |
| 625 |
{ |
| 626 |
mbedtls_x509_csr csr; |
| 627 |
char buf[2000]; |
| 628 |
int res; |
| 629 |
|
| 630 |
mbedtls_x509_csr_init(&csr); |
| 631 |
USE_PSA_INIT(); |
| 632 |
memset(buf, 0, 2000); |
| 633 |
|
| 634 |
TEST_EQUAL(mbedtls_x509_csr_parse_file(&csr, csr_file), 0); |
| 635 |
res = mbedtls_x509_csr_info(buf, 2000, "", &csr); |
| 636 |
|
| 637 |
TEST_ASSERT(res != -1); |
| 638 |
TEST_ASSERT(res != -2); |
| 639 |
|
| 640 |
TEST_EQUAL(strcmp(buf, result_str), 0); |
| 641 |
|
| 642 |
exit: |
| 643 |
mbedtls_x509_csr_free(&csr); |
| 644 |
USE_PSA_DONE(); |
| 645 |
} |
| 646 |
/* END_CASE */ |
| 647 |
|
| 648 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
| 649 |
void x509_verify_info(int flags, char *prefix, char *result_str) |
| 650 |
{ |
| 651 |
char buf[2000]; |
| 652 |
int res; |
| 653 |
|
| 654 |
USE_PSA_INIT(); |
| 655 |
memset(buf, 0, sizeof(buf)); |
| 656 |
|
| 657 |
res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags); |
| 658 |
|
| 659 |
TEST_ASSERT(res >= 0); |
| 660 |
|
| 661 |
TEST_EQUAL(strcmp(buf, result_str), 0); |
| 662 |
|
| 663 |
exit: |
| 664 |
USE_PSA_DONE(); |
| 665 |
} |
| 666 |
/* END_CASE */ |
| 667 |
|
| 668 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:PSA_WANT_ALG_ECDSA */ |
| 669 |
void x509_verify_restart(char *crt_file, char *ca_file, |
| 670 |
int result, int flags_result, |
| 671 |
int max_ops, int min_restart, int max_restart) |
| 672 |
{ |
| 673 |
int ret, cnt_restart; |
| 674 |
mbedtls_x509_crt_restart_ctx rs_ctx; |
| 675 |
mbedtls_x509_crt crt; |
| 676 |
mbedtls_x509_crt ca; |
| 677 |
uint32_t flags = 0; |
| 678 |
|
| 679 |
/* |
| 680 |
* See comments on ecp_test_vect_restart() for op count precision. |
| 681 |
* |
| 682 |
* For reference, with Mbed TLS 2.6 and default settings: |
| 683 |
* - ecdsa_verify() for P-256: ~ 6700 |
| 684 |
* - ecdsa_verify() for P-384: ~ 18800 |
| 685 |
* - x509_verify() for server5 -> test-ca2: ~ 18800 |
| 686 |
* - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500 |
| 687 |
*/ |
| 688 |
mbedtls_x509_crt_restart_init(&rs_ctx); |
| 689 |
mbedtls_x509_crt_init(&crt); |
| 690 |
mbedtls_x509_crt_init(&ca); |
| 691 |
MD_OR_USE_PSA_INIT(); |
| 692 |
|
| 693 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0); |
| 694 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0); |
| 695 |
|
| 696 |
psa_interruptible_set_max_ops(max_ops); |
| 697 |
|
| 698 |
cnt_restart = 0; |
| 699 |
do { |
| 700 |
ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL, |
| 701 |
&mbedtls_x509_crt_profile_default, NULL, &flags, |
| 702 |
NULL, NULL, &rs_ctx); |
| 703 |
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart); |
| 704 |
|
| 705 |
TEST_EQUAL(ret, result); |
| 706 |
TEST_EQUAL(flags, (uint32_t) flags_result); |
| 707 |
|
| 708 |
TEST_ASSERT(cnt_restart >= min_restart); |
| 709 |
TEST_ASSERT(cnt_restart <= max_restart); |
| 710 |
|
| 711 |
/* Do we leak memory when aborting? */ |
| 712 |
ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL, |
| 713 |
&mbedtls_x509_crt_profile_default, NULL, &flags, |
| 714 |
NULL, NULL, &rs_ctx); |
| 715 |
TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS); |
| 716 |
|
| 717 |
exit: |
| 718 |
mbedtls_x509_crt_restart_free(&rs_ctx); |
| 719 |
mbedtls_x509_crt_free(&crt); |
| 720 |
mbedtls_x509_crt_free(&ca); |
| 721 |
MD_OR_USE_PSA_DONE(); |
| 722 |
} |
| 723 |
/* END_CASE */ |
| 724 |
|
| 725 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */ |
| 726 |
void x509_verify(char *crt_file, char *ca_file, char *crl_file, |
| 727 |
char *cn_name_str, int result, int flags_result, |
| 728 |
char *profile_str, |
| 729 |
char *verify_callback) |
| 730 |
{ |
| 731 |
mbedtls_x509_crt crt; |
| 732 |
mbedtls_x509_crt ca; |
| 733 |
mbedtls_x509_crl crl; |
| 734 |
uint32_t flags = 0; |
| 735 |
int res; |
| 736 |
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL; |
| 737 |
char *cn_name = NULL; |
| 738 |
const mbedtls_x509_crt_profile *profile; |
| 739 |
|
| 740 |
mbedtls_x509_crt_init(&crt); |
| 741 |
mbedtls_x509_crt_init(&ca); |
| 742 |
mbedtls_x509_crl_init(&crl); |
| 743 |
MD_OR_USE_PSA_INIT(); |
| 744 |
|
| 745 |
if (strcmp(cn_name_str, "NULL") != 0) { |
| 746 |
cn_name = cn_name_str; |
| 747 |
} |
| 748 |
|
| 749 |
if (strcmp(profile_str, "") == 0) { |
| 750 |
profile = &mbedtls_x509_crt_profile_default; |
| 751 |
} else if (strcmp(profile_str, "next") == 0) { |
| 752 |
profile = &mbedtls_x509_crt_profile_next; |
| 753 |
} else if (strcmp(profile_str, "suite_b") == 0) { |
| 754 |
profile = &mbedtls_x509_crt_profile_suiteb; |
| 755 |
} else if (strcmp(profile_str, "compat") == 0) { |
| 756 |
profile = &compat_profile; |
| 757 |
} else if (strcmp(profile_str, "all") == 0) { |
| 758 |
profile = &profile_all; |
| 759 |
} else { |
| 760 |
TEST_FAIL("Unknown algorithm profile"); |
| 761 |
} |
| 762 |
|
| 763 |
if (strcmp(verify_callback, "NULL") == 0) { |
| 764 |
f_vrfy = NULL; |
| 765 |
} else if (strcmp(verify_callback, "verify_none") == 0) { |
| 766 |
f_vrfy = verify_none; |
| 767 |
} else if (strcmp(verify_callback, "verify_all") == 0) { |
| 768 |
f_vrfy = verify_all; |
| 769 |
} else { |
| 770 |
TEST_FAIL("No known verify callback selected"); |
| 771 |
} |
| 772 |
|
| 773 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0); |
| 774 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0); |
| 775 |
TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0); |
| 776 |
|
| 777 |
res = mbedtls_x509_crt_verify_with_profile(&crt, |
| 778 |
&ca, |
| 779 |
&crl, |
| 780 |
profile, |
| 781 |
cn_name, |
| 782 |
&flags, |
| 783 |
f_vrfy, |
| 784 |
NULL); |
| 785 |
|
| 786 |
TEST_EQUAL(res, result); |
| 787 |
TEST_EQUAL(flags, (uint32_t) flags_result); |
| 788 |
|
| 789 |
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 790 |
/* CRLs aren't supported with CA callbacks, so skip the CA callback |
| 791 |
* version of the test if CRLs are in use. */ |
| 792 |
if (strcmp(crl_file, "") == 0) { |
| 793 |
flags = 0; |
| 794 |
|
| 795 |
res = mbedtls_x509_crt_verify_with_ca_cb(&crt, |
| 796 |
ca_callback, |
| 797 |
&ca, |
| 798 |
profile, |
| 799 |
cn_name, |
| 800 |
&flags, |
| 801 |
f_vrfy, |
| 802 |
NULL); |
| 803 |
|
| 804 |
TEST_EQUAL(res, result); |
| 805 |
TEST_EQUAL(flags, (uint32_t) (flags_result)); |
| 806 |
} |
| 807 |
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 808 |
exit: |
| 809 |
mbedtls_x509_crt_free(&crt); |
| 810 |
mbedtls_x509_crt_free(&ca); |
| 811 |
mbedtls_x509_crl_free(&crl); |
| 812 |
MD_OR_USE_PSA_DONE(); |
| 813 |
} |
| 814 |
/* END_CASE */ |
| 815 |
|
| 816 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 817 |
void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name, |
| 818 |
int exp_ret) |
| 819 |
{ |
| 820 |
int ret; |
| 821 |
mbedtls_x509_crt crt; |
| 822 |
mbedtls_x509_crt ca; |
| 823 |
uint32_t flags = 0; |
| 824 |
|
| 825 |
mbedtls_x509_crt_init(&crt); |
| 826 |
mbedtls_x509_crt_init(&ca); |
| 827 |
USE_PSA_INIT(); |
| 828 |
|
| 829 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0); |
| 830 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0); |
| 831 |
|
| 832 |
if (strcmp(name, "NULL") == 0) { |
| 833 |
name = NULL; |
| 834 |
} |
| 835 |
|
| 836 |
ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca, |
| 837 |
&compat_profile, name, &flags, |
| 838 |
NULL, NULL); |
| 839 |
|
| 840 |
TEST_EQUAL(ret, exp_ret); |
| 841 |
TEST_EQUAL(flags, (uint32_t) (-1)); |
| 842 |
exit: |
| 843 |
mbedtls_x509_crt_free(&crt); |
| 844 |
mbedtls_x509_crt_free(&ca); |
| 845 |
USE_PSA_DONE(); |
| 846 |
} |
| 847 |
/* END_CASE */ |
| 848 |
|
| 849 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| 850 |
void x509_verify_callback(char *crt_file, char *ca_file, char *name, |
| 851 |
int exp_ret, char *exp_vrfy_out) |
| 852 |
{ |
| 853 |
int ret; |
| 854 |
mbedtls_x509_crt crt; |
| 855 |
mbedtls_x509_crt ca; |
| 856 |
uint32_t flags = 0; |
| 857 |
verify_print_context vrfy_ctx; |
| 858 |
|
| 859 |
mbedtls_x509_crt_init(&crt); |
| 860 |
mbedtls_x509_crt_init(&ca); |
| 861 |
MD_OR_USE_PSA_INIT(); |
| 862 |
|
| 863 |
verify_print_init(&vrfy_ctx); |
| 864 |
|
| 865 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0); |
| 866 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0); |
| 867 |
|
| 868 |
if (strcmp(name, "NULL") == 0) { |
| 869 |
name = NULL; |
| 870 |
} |
| 871 |
|
| 872 |
ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL, |
| 873 |
&compat_profile, |
| 874 |
name, &flags, |
| 875 |
verify_print, &vrfy_ctx); |
| 876 |
|
| 877 |
TEST_EQUAL(ret, exp_ret); |
| 878 |
TEST_EQUAL(strcmp(vrfy_ctx.buf, exp_vrfy_out), 0); |
| 879 |
|
| 880 |
exit: |
| 881 |
mbedtls_x509_crt_free(&crt); |
| 882 |
mbedtls_x509_crt_free(&ca); |
| 883 |
MD_OR_USE_PSA_DONE(); |
| 884 |
} |
| 885 |
/* END_CASE */ |
| 886 |
|
| 887 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
| 888 |
void mbedtls_x509_dn_gets_subject_replace(char *crt_file, |
| 889 |
char *new_subject_ou, |
| 890 |
char *result_str, |
| 891 |
int ret) |
| 892 |
{ |
| 893 |
mbedtls_x509_crt crt; |
| 894 |
char buf[2000]; |
| 895 |
int res = 0; |
| 896 |
|
| 897 |
mbedtls_x509_crt_init(&crt); |
| 898 |
USE_PSA_INIT(); |
| 899 |
|
| 900 |
memset(buf, 0, 2000); |
| 901 |
|
| 902 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0); |
| 903 |
crt.subject.next->val.p = (unsigned char *) new_subject_ou; |
| 904 |
crt.subject.next->val.len = strlen(new_subject_ou); |
| 905 |
|
| 906 |
res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject); |
| 907 |
|
| 908 |
if (ret != 0) { |
| 909 |
TEST_EQUAL(res, ret); |
| 910 |
} else { |
| 911 |
TEST_ASSERT(res != -1); |
| 912 |
TEST_ASSERT(res != -2); |
| 913 |
TEST_EQUAL(strcmp(buf, result_str), 0); |
| 914 |
} |
| 915 |
exit: |
| 916 |
mbedtls_x509_crt_free(&crt); |
| 917 |
USE_PSA_DONE(); |
| 918 |
} |
| 919 |
/* END_CASE */ |
| 920 |
|
| 921 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
| 922 |
void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str) |
| 923 |
{ |
| 924 |
mbedtls_x509_crt crt; |
| 925 |
char buf[2000]; |
| 926 |
int res = 0; |
| 927 |
|
| 928 |
mbedtls_x509_crt_init(&crt); |
| 929 |
USE_PSA_INIT(); |
| 930 |
|
| 931 |
memset(buf, 0, 2000); |
| 932 |
|
| 933 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0); |
| 934 |
if (strcmp(entity, "subject") == 0) { |
| 935 |
res = mbedtls_x509_dn_gets(buf, 2000, &crt.subject); |
| 936 |
} else if (strcmp(entity, "issuer") == 0) { |
| 937 |
res = mbedtls_x509_dn_gets(buf, 2000, &crt.issuer); |
| 938 |
} else { |
| 939 |
TEST_FAIL("Unknown entity"); |
| 940 |
} |
| 941 |
|
| 942 |
TEST_ASSERT(res != -1); |
| 943 |
TEST_ASSERT(res != -2); |
| 944 |
|
| 945 |
TEST_EQUAL(strcmp(buf, result_str), 0); |
| 946 |
|
| 947 |
exit: |
| 948 |
mbedtls_x509_crt_free(&crt); |
| 949 |
USE_PSA_DONE(); |
| 950 |
} |
| 951 |
/* END_CASE */ |
| 952 |
|
| 953 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
| 954 |
void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret) |
| 955 |
{ |
| 956 |
unsigned char *name = NULL; |
| 957 |
unsigned char *p; |
| 958 |
size_t name_len; |
| 959 |
mbedtls_x509_name head; |
| 960 |
int ret; |
| 961 |
|
| 962 |
USE_PSA_INIT(); |
| 963 |
memset(&head, 0, sizeof(head)); |
| 964 |
|
| 965 |
name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len); |
| 966 |
p = name; |
| 967 |
|
| 968 |
ret = mbedtls_x509_get_name(&p, (name + name_len), &head); |
| 969 |
if (ret == 0) { |
| 970 |
mbedtls_asn1_free_named_data_list_shallow(head.next); |
| 971 |
} |
| 972 |
|
| 973 |
TEST_EQUAL(ret, exp_ret); |
| 974 |
|
| 975 |
exit: |
| 976 |
mbedtls_free(name); |
| 977 |
USE_PSA_DONE(); |
| 978 |
} |
| 979 |
/* END_CASE */ |
| 980 |
|
| 981 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
| 982 |
void mbedtls_x509_dn_get_next(char *name_str, |
| 983 |
int next_merged, |
| 984 |
char *expected_oids, |
| 985 |
int exp_count, |
| 986 |
char *exp_dn_gets) |
| 987 |
{ |
| 988 |
int ret = 0, i; |
| 989 |
size_t len = 0, out_size; |
| 990 |
mbedtls_asn1_named_data *names = NULL; |
| 991 |
mbedtls_x509_name parsed; |
| 992 |
memset(&parsed, 0, sizeof(parsed)); |
| 993 |
mbedtls_x509_name *parsed_cur; |
| 994 |
// Size of buf is maximum required for test cases |
| 995 |
unsigned char buf[80] = { 0 }; |
| 996 |
unsigned char *out = NULL; |
| 997 |
unsigned char *c = buf + sizeof(buf); |
| 998 |
const char *short_name; |
| 999 |
|
| 1000 |
USE_PSA_INIT(); |
| 1001 |
|
| 1002 |
// Additional size required for trailing space |
| 1003 |
out_size = strlen(expected_oids) + 2; |
| 1004 |
TEST_CALLOC(out, out_size); |
| 1005 |
|
| 1006 |
TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0); |
| 1007 |
|
| 1008 |
ret = mbedtls_x509_write_names(&c, buf, names); |
| 1009 |
TEST_LE_S(0, ret); |
| 1010 |
|
| 1011 |
TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len, |
| 1012 |
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0); |
| 1013 |
TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0); |
| 1014 |
|
| 1015 |
// Iterate over names and set next_merged nodes |
| 1016 |
parsed_cur = &parsed; |
| 1017 |
for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) { |
| 1018 |
parsed_cur->next_merged = next_merged & 0x01; |
| 1019 |
parsed_cur = parsed_cur->next; |
| 1020 |
} |
| 1021 |
|
| 1022 |
// Iterate over RDN nodes and print OID of first element to buffer |
| 1023 |
parsed_cur = &parsed; |
| 1024 |
len = 0; |
| 1025 |
for (i = 0; parsed_cur != NULL; i++) { |
| 1026 |
TEST_EQUAL(mbedtls_x509_oid_get_attr_short_name(&parsed_cur->oid, |
| 1027 |
&short_name), 0); |
| 1028 |
len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name); |
| 1029 |
parsed_cur = mbedtls_x509_dn_get_next(parsed_cur); |
| 1030 |
} |
| 1031 |
out[len-1] = 0; |
| 1032 |
|
| 1033 |
TEST_EQUAL(exp_count, i); |
| 1034 |
TEST_EQUAL(strcmp((char *) out, expected_oids), 0); |
| 1035 |
mbedtls_free(out); |
| 1036 |
out = NULL; |
| 1037 |
|
| 1038 |
out_size = strlen(exp_dn_gets) + 1; |
| 1039 |
TEST_CALLOC(out, out_size); |
| 1040 |
|
| 1041 |
TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed)); |
| 1042 |
TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0); |
| 1043 |
exit: |
| 1044 |
mbedtls_free(out); |
| 1045 |
mbedtls_asn1_free_named_data_list(&names); |
| 1046 |
mbedtls_asn1_free_named_data_list_shallow(parsed.next); |
| 1047 |
USE_PSA_DONE(); |
| 1048 |
} |
| 1049 |
/* END_CASE */ |
| 1050 |
|
| 1051 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| 1052 |
void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result) |
| 1053 |
{ |
| 1054 |
mbedtls_x509_crt crt; |
| 1055 |
|
| 1056 |
mbedtls_x509_crt_init(&crt); |
| 1057 |
USE_PSA_INIT(); |
| 1058 |
|
| 1059 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0); |
| 1060 |
|
| 1061 |
if (strcmp(entity, "valid_from") == 0) { |
| 1062 |
TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_from), result); |
| 1063 |
} else if (strcmp(entity, "valid_to") == 0) { |
| 1064 |
TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_to), result); |
| 1065 |
} else { |
| 1066 |
TEST_FAIL("Unknown entity"); |
| 1067 |
} |
| 1068 |
|
| 1069 |
exit: |
| 1070 |
mbedtls_x509_crt_free(&crt); |
| 1071 |
USE_PSA_DONE(); |
| 1072 |
} |
| 1073 |
/* END_CASE */ |
| 1074 |
|
| 1075 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| 1076 |
void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result) |
| 1077 |
{ |
| 1078 |
mbedtls_x509_crt crt; |
| 1079 |
|
| 1080 |
mbedtls_x509_crt_init(&crt); |
| 1081 |
USE_PSA_INIT(); |
| 1082 |
|
| 1083 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0); |
| 1084 |
|
| 1085 |
if (strcmp(entity, "valid_from") == 0) { |
| 1086 |
TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_from), result); |
| 1087 |
} else if (strcmp(entity, "valid_to") == 0) { |
| 1088 |
TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_to), result); |
| 1089 |
} else { |
| 1090 |
TEST_FAIL("Unknown entity"); |
| 1091 |
} |
| 1092 |
|
| 1093 |
exit: |
| 1094 |
mbedtls_x509_crt_free(&crt); |
| 1095 |
USE_PSA_DONE(); |
| 1096 |
} |
| 1097 |
/* END_CASE */ |
| 1098 |
|
| 1099 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */ |
| 1100 |
void x509parse_crt_file(char *crt_file, int result) |
| 1101 |
{ |
| 1102 |
mbedtls_x509_crt crt; |
| 1103 |
|
| 1104 |
mbedtls_x509_crt_init(&crt); |
| 1105 |
USE_PSA_INIT(); |
| 1106 |
|
| 1107 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), result); |
| 1108 |
|
| 1109 |
exit: |
| 1110 |
mbedtls_x509_crt_free(&crt); |
| 1111 |
USE_PSA_DONE(); |
| 1112 |
} |
| 1113 |
/* END_CASE */ |
| 1114 |
|
| 1115 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */ |
| 1116 |
void mbedtls_x509_get_ca_istrue(char *crt_file, int result) |
| 1117 |
{ |
| 1118 |
mbedtls_x509_crt crt; |
| 1119 |
mbedtls_x509_crt_init(&crt); |
| 1120 |
USE_PSA_INIT(); |
| 1121 |
|
| 1122 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0); |
| 1123 |
TEST_EQUAL(mbedtls_x509_crt_get_ca_istrue(&crt), result); |
| 1124 |
exit: |
| 1125 |
mbedtls_x509_crt_free(&crt); |
| 1126 |
USE_PSA_DONE(); |
| 1127 |
} |
| 1128 |
/* END_CASE */ |
| 1129 |
|
| 1130 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
| 1131 |
void x509parse_crt(data_t *buf, char *result_str, int result) |
| 1132 |
{ |
| 1133 |
mbedtls_x509_crt crt; |
| 1134 |
#if !defined(MBEDTLS_X509_REMOVE_INFO) |
| 1135 |
unsigned char output[2000] = { 0 }; |
| 1136 |
#else |
| 1137 |
((void) result_str); |
| 1138 |
#endif |
| 1139 |
/* Tests whose result is MBEDTLS_ERR_PK_INVALID_PUBKEY might return |
| 1140 |
* MBEDTLS_ERR_ASN1_UNEXPECTED_TAG until psa#308 is merged. This variable |
| 1141 |
* is therefore used for backward compatiblity and will be removed in |
| 1142 |
* mbedtls#10229. */ |
| 1143 |
int result_back_comp = result; |
| 1144 |
int res; |
| 1145 |
|
| 1146 |
mbedtls_x509_crt_init(&crt); |
| 1147 |
USE_PSA_INIT(); |
| 1148 |
|
| 1149 |
res = mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len); |
| 1150 |
TEST_ASSERT((res == result) || (res == result_back_comp)); |
| 1151 |
#if !defined(MBEDTLS_X509_REMOVE_INFO) |
| 1152 |
if ((result) == 0) { |
| 1153 |
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt); |
| 1154 |
TEST_ASSERT(res != -1); |
| 1155 |
TEST_ASSERT(res != -2); |
| 1156 |
|
| 1157 |
TEST_EQUAL(strcmp((char *) output, result_str), 0); |
| 1158 |
} |
| 1159 |
memset(output, 0, 2000); |
| 1160 |
#endif |
| 1161 |
|
| 1162 |
mbedtls_x509_crt_free(&crt); |
| 1163 |
mbedtls_x509_crt_init(&crt); |
| 1164 |
|
| 1165 |
res = mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len); |
| 1166 |
TEST_ASSERT((res == result) || (res == result_back_comp)); |
| 1167 |
#if !defined(MBEDTLS_X509_REMOVE_INFO) |
| 1168 |
if ((result) == 0) { |
| 1169 |
memset(output, 0, 2000); |
| 1170 |
|
| 1171 |
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt); |
| 1172 |
|
| 1173 |
TEST_ASSERT(res != -1); |
| 1174 |
TEST_ASSERT(res != -2); |
| 1175 |
|
| 1176 |
TEST_EQUAL(strcmp((char *) output, result_str), 0); |
| 1177 |
} |
| 1178 |
memset(output, 0, 2000); |
| 1179 |
#endif /* !MBEDTLS_X509_REMOVE_INFO */ |
| 1180 |
|
| 1181 |
mbedtls_x509_crt_free(&crt); |
| 1182 |
mbedtls_x509_crt_init(&crt); |
| 1183 |
|
| 1184 |
res = mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL); |
| 1185 |
TEST_ASSERT((res == result) || (res == result_back_comp)); |
| 1186 |
#if !defined(MBEDTLS_X509_REMOVE_INFO) |
| 1187 |
if ((result) == 0) { |
| 1188 |
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt); |
| 1189 |
|
| 1190 |
TEST_ASSERT(res != -1); |
| 1191 |
TEST_ASSERT(res != -2); |
| 1192 |
|
| 1193 |
TEST_EQUAL(strcmp((char *) output, result_str), 0); |
| 1194 |
} |
| 1195 |
memset(output, 0, 2000); |
| 1196 |
#endif /* !MBEDTLS_X509_REMOVE_INFO */ |
| 1197 |
|
| 1198 |
mbedtls_x509_crt_free(&crt); |
| 1199 |
mbedtls_x509_crt_init(&crt); |
| 1200 |
|
| 1201 |
res = mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL); |
| 1202 |
TEST_ASSERT((res == result) || (res == result_back_comp)); |
| 1203 |
#if !defined(MBEDTLS_X509_REMOVE_INFO) |
| 1204 |
if ((result) == 0) { |
| 1205 |
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt); |
| 1206 |
|
| 1207 |
TEST_ASSERT(res != -1); |
| 1208 |
TEST_ASSERT(res != -2); |
| 1209 |
|
| 1210 |
TEST_EQUAL(strcmp((char *) output, result_str), 0); |
| 1211 |
} |
| 1212 |
#endif /* !MBEDTLS_X509_REMOVE_INFO */ |
| 1213 |
|
| 1214 |
exit: |
| 1215 |
mbedtls_x509_crt_free(&crt); |
| 1216 |
USE_PSA_DONE(); |
| 1217 |
} |
| 1218 |
/* END_CASE */ |
| 1219 |
|
| 1220 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
| 1221 |
void x509parse_crt_cb(data_t *buf, char *result_str, int result) |
| 1222 |
{ |
| 1223 |
mbedtls_x509_crt crt; |
| 1224 |
mbedtls_x509_buf oid; |
| 1225 |
|
| 1226 |
#if !defined(MBEDTLS_X509_REMOVE_INFO) |
| 1227 |
unsigned char output[2000] = { 0 }; |
| 1228 |
int res; |
| 1229 |
#else |
| 1230 |
((void) result_str); |
| 1231 |
#endif |
| 1232 |
|
| 1233 |
oid.tag = MBEDTLS_ASN1_OID; |
| 1234 |
oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F"); |
| 1235 |
oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F"; |
| 1236 |
|
| 1237 |
mbedtls_x509_crt_init(&crt); |
| 1238 |
USE_PSA_INIT(); |
| 1239 |
|
| 1240 |
TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb, |
| 1241 |
&oid), result); |
| 1242 |
#if !defined(MBEDTLS_X509_REMOVE_INFO) |
| 1243 |
if ((result) == 0) { |
| 1244 |
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt); |
| 1245 |
|
| 1246 |
TEST_ASSERT(res != -1); |
| 1247 |
TEST_ASSERT(res != -2); |
| 1248 |
|
| 1249 |
TEST_EQUAL(strcmp((char *) output, result_str), 0); |
| 1250 |
} |
| 1251 |
memset(output, 0, 2000); |
| 1252 |
#endif /* !MBEDTLS_X509_REMOVE_INFO */ |
| 1253 |
|
| 1254 |
mbedtls_x509_crt_free(&crt); |
| 1255 |
mbedtls_x509_crt_init(&crt); |
| 1256 |
|
| 1257 |
TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb, |
| 1258 |
&oid), (result)); |
| 1259 |
#if !defined(MBEDTLS_X509_REMOVE_INFO) |
| 1260 |
if ((result) == 0) { |
| 1261 |
res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt); |
| 1262 |
|
| 1263 |
TEST_ASSERT(res != -1); |
| 1264 |
TEST_ASSERT(res != -2); |
| 1265 |
|
| 1266 |
TEST_EQUAL(strcmp((char *) output, result_str), 0); |
| 1267 |
} |
| 1268 |
#endif /* !MBEDTLS_X509_REMOVE_INFO */ |
| 1269 |
|
| 1270 |
exit: |
| 1271 |
mbedtls_x509_crt_free(&crt); |
| 1272 |
USE_PSA_DONE(); |
| 1273 |
} |
| 1274 |
/* END_CASE */ |
| 1275 |
|
| 1276 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
| 1277 |
void x509parse_crl(data_t *buf, char *result_str, int result) |
| 1278 |
{ |
| 1279 |
mbedtls_x509_crl crl; |
| 1280 |
unsigned char output[2000]; |
| 1281 |
int res; |
| 1282 |
|
| 1283 |
mbedtls_x509_crl_init(&crl); |
| 1284 |
USE_PSA_INIT(); |
| 1285 |
|
| 1286 |
memset(output, 0, 2000); |
| 1287 |
|
| 1288 |
|
| 1289 |
TEST_EQUAL(mbedtls_x509_crl_parse(&crl, buf->x, buf->len), (result)); |
| 1290 |
if ((result) == 0) { |
| 1291 |
res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl); |
| 1292 |
|
| 1293 |
TEST_ASSERT(res != -1); |
| 1294 |
TEST_ASSERT(res != -2); |
| 1295 |
|
| 1296 |
TEST_EQUAL(strcmp((char *) output, result_str), 0); |
| 1297 |
} |
| 1298 |
|
| 1299 |
exit: |
| 1300 |
mbedtls_x509_crl_free(&crl); |
| 1301 |
USE_PSA_DONE(); |
| 1302 |
} |
| 1303 |
/* END_CASE */ |
| 1304 |
|
| 1305 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
| 1306 |
void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret) |
| 1307 |
{ |
| 1308 |
mbedtls_x509_csr csr; |
| 1309 |
char my_out[1000]; |
| 1310 |
int my_ret; |
| 1311 |
|
| 1312 |
mbedtls_x509_csr_init(&csr); |
| 1313 |
USE_PSA_INIT(); |
| 1314 |
|
| 1315 |
memset(my_out, 0, sizeof(my_out)); |
| 1316 |
|
| 1317 |
my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len); |
| 1318 |
TEST_EQUAL(my_ret, ref_ret); |
| 1319 |
|
| 1320 |
if (ref_ret == 0) { |
| 1321 |
size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr); |
| 1322 |
TEST_EQUAL(my_out_len, strlen(ref_out)); |
| 1323 |
TEST_EQUAL(strcmp(my_out, ref_out), 0); |
| 1324 |
} |
| 1325 |
|
| 1326 |
exit: |
| 1327 |
mbedtls_x509_csr_free(&csr); |
| 1328 |
USE_PSA_DONE(); |
| 1329 |
} |
| 1330 |
/* END_CASE */ |
| 1331 |
|
| 1332 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
| 1333 |
void mbedtls_x509_csr_parse_with_ext_cb(data_t *csr_der, char *ref_out, int ref_ret, int accept) |
| 1334 |
{ |
| 1335 |
mbedtls_x509_csr csr; |
| 1336 |
char my_out[1000]; |
| 1337 |
int my_ret; |
| 1338 |
|
| 1339 |
mbedtls_x509_csr_init(&csr); |
| 1340 |
USE_PSA_INIT(); |
| 1341 |
|
| 1342 |
memset(my_out, 0, sizeof(my_out)); |
| 1343 |
|
| 1344 |
my_ret = mbedtls_x509_csr_parse_der_with_ext_cb(&csr, csr_der->x, csr_der->len, |
| 1345 |
accept ? parse_csr_ext_accept_cb : |
| 1346 |
parse_csr_ext_reject_cb, |
| 1347 |
NULL); |
| 1348 |
TEST_EQUAL(my_ret, ref_ret); |
| 1349 |
|
| 1350 |
if (ref_ret == 0) { |
| 1351 |
size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr); |
| 1352 |
TEST_EQUAL(my_out_len, strlen(ref_out)); |
| 1353 |
TEST_EQUAL(strcmp(my_out, ref_out), 0); |
| 1354 |
} |
| 1355 |
|
| 1356 |
exit: |
| 1357 |
mbedtls_x509_csr_free(&csr); |
| 1358 |
USE_PSA_DONE(); |
| 1359 |
} |
| 1360 |
/* END_CASE */ |
| 1361 |
|
| 1362 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
| 1363 |
void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret) |
| 1364 |
{ |
| 1365 |
mbedtls_x509_csr csr; |
| 1366 |
char my_out[1000]; |
| 1367 |
int my_ret; |
| 1368 |
|
| 1369 |
mbedtls_x509_csr_init(&csr); |
| 1370 |
USE_PSA_INIT(); |
| 1371 |
|
| 1372 |
memset(my_out, 0, sizeof(my_out)); |
| 1373 |
|
| 1374 |
my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file); |
| 1375 |
TEST_EQUAL(my_ret, ref_ret); |
| 1376 |
|
| 1377 |
if (ref_ret == 0) { |
| 1378 |
size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr); |
| 1379 |
TEST_EQUAL(my_out_len, strlen(ref_out)); |
| 1380 |
TEST_EQUAL(strcmp(my_out, ref_out), 0); |
| 1381 |
} |
| 1382 |
|
| 1383 |
exit: |
| 1384 |
mbedtls_x509_csr_free(&csr); |
| 1385 |
USE_PSA_DONE(); |
| 1386 |
} |
| 1387 |
/* END_CASE */ |
| 1388 |
|
| 1389 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| 1390 |
void mbedtls_x509_crt_parse_file(char *crt_path, int ret, int nb_crt) |
| 1391 |
{ |
| 1392 |
mbedtls_x509_crt chain, *cur; |
| 1393 |
int i; |
| 1394 |
|
| 1395 |
mbedtls_x509_crt_init(&chain); |
| 1396 |
USE_PSA_INIT(); |
| 1397 |
|
| 1398 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, crt_path), ret); |
| 1399 |
|
| 1400 |
/* Check how many certs we got */ |
| 1401 |
for (i = 0, cur = &chain; cur != NULL; cur = cur->next) { |
| 1402 |
if (cur->raw.p != NULL) { |
| 1403 |
i++; |
| 1404 |
} |
| 1405 |
} |
| 1406 |
|
| 1407 |
TEST_EQUAL(i, nb_crt); |
| 1408 |
|
| 1409 |
exit: |
| 1410 |
mbedtls_x509_crt_free(&chain); |
| 1411 |
USE_PSA_DONE(); |
| 1412 |
} |
| 1413 |
/* END_CASE */ |
| 1414 |
|
| 1415 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| 1416 |
void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt) |
| 1417 |
{ |
| 1418 |
mbedtls_x509_crt chain, *cur; |
| 1419 |
int i; |
| 1420 |
|
| 1421 |
mbedtls_x509_crt_init(&chain); |
| 1422 |
USE_PSA_INIT(); |
| 1423 |
|
| 1424 |
TEST_EQUAL(mbedtls_x509_crt_parse_path(&chain, crt_path), ret); |
| 1425 |
|
| 1426 |
/* Check how many certs we got */ |
| 1427 |
for (i = 0, cur = &chain; cur != NULL; cur = cur->next) { |
| 1428 |
if (cur->raw.p != NULL) { |
| 1429 |
i++; |
| 1430 |
} |
| 1431 |
} |
| 1432 |
|
| 1433 |
TEST_EQUAL(i, nb_crt); |
| 1434 |
|
| 1435 |
exit: |
| 1436 |
mbedtls_x509_crt_free(&chain); |
| 1437 |
USE_PSA_DONE(); |
| 1438 |
} |
| 1439 |
/* END_CASE */ |
| 1440 |
|
| 1441 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| 1442 |
void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int, |
| 1443 |
int ret_chk, int flags_chk) |
| 1444 |
{ |
| 1445 |
char file_buf[128]; |
| 1446 |
int ret; |
| 1447 |
uint32_t flags; |
| 1448 |
mbedtls_x509_crt trusted, chain; |
| 1449 |
|
| 1450 |
/* |
| 1451 |
* We expect chain_dir to contain certificates 00.crt, 01.crt, etc. |
| 1452 |
* with NN.crt signed by NN-1.crt |
| 1453 |
*/ |
| 1454 |
mbedtls_x509_crt_init(&trusted); |
| 1455 |
mbedtls_x509_crt_init(&chain); |
| 1456 |
MD_OR_USE_PSA_INIT(); |
| 1457 |
|
| 1458 |
/* Load trusted root */ |
| 1459 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, ca_file), 0); |
| 1460 |
|
| 1461 |
/* Load a chain with nb_int intermediates (from 01 to nb_int), |
| 1462 |
* plus one "end-entity" cert (nb_int + 1) */ |
| 1463 |
ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir, |
| 1464 |
nb_int + 1); |
| 1465 |
TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf)); |
| 1466 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, file_buf), 0); |
| 1467 |
|
| 1468 |
/* Try to verify that chain */ |
| 1469 |
ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags, |
| 1470 |
NULL, NULL); |
| 1471 |
TEST_EQUAL(ret, ret_chk); |
| 1472 |
TEST_EQUAL(flags, (uint32_t) flags_chk); |
| 1473 |
|
| 1474 |
exit: |
| 1475 |
mbedtls_x509_crt_free(&chain); |
| 1476 |
mbedtls_x509_crt_free(&trusted); |
| 1477 |
MD_OR_USE_PSA_DONE(); |
| 1478 |
} |
| 1479 |
/* END_CASE */ |
| 1480 |
|
| 1481 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| 1482 |
void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca, |
| 1483 |
int flags_result, int result, |
| 1484 |
char *profile_name, int vrfy_fatal_lvls) |
| 1485 |
{ |
| 1486 |
char *act; |
| 1487 |
uint32_t flags; |
| 1488 |
int res; |
| 1489 |
mbedtls_x509_crt trusted, chain; |
| 1490 |
const mbedtls_x509_crt_profile *profile = NULL; |
| 1491 |
|
| 1492 |
mbedtls_x509_crt_init(&chain); |
| 1493 |
mbedtls_x509_crt_init(&trusted); |
| 1494 |
MD_OR_USE_PSA_INIT(); |
| 1495 |
|
| 1496 |
while ((act = mystrsep(&chain_paths, " ")) != NULL) { |
| 1497 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, act), 0); |
| 1498 |
} |
| 1499 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, trusted_ca), 0); |
| 1500 |
|
| 1501 |
if (strcmp(profile_name, "") == 0) { |
| 1502 |
profile = &mbedtls_x509_crt_profile_default; |
| 1503 |
} else if (strcmp(profile_name, "next") == 0) { |
| 1504 |
profile = &mbedtls_x509_crt_profile_next; |
| 1505 |
} else if (strcmp(profile_name, "suiteb") == 0) { |
| 1506 |
profile = &mbedtls_x509_crt_profile_suiteb; |
| 1507 |
} else if (strcmp(profile_name, "rsa3072") == 0) { |
| 1508 |
profile = &profile_rsa3072; |
| 1509 |
} else if (strcmp(profile_name, "sha512") == 0) { |
| 1510 |
profile = &profile_sha512; |
| 1511 |
} |
| 1512 |
|
| 1513 |
res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile, |
| 1514 |
NULL, &flags, verify_fatal, &vrfy_fatal_lvls); |
| 1515 |
|
| 1516 |
TEST_EQUAL(res, (result)); |
| 1517 |
TEST_EQUAL(flags, (uint32_t) (flags_result)); |
| 1518 |
|
| 1519 |
exit: |
| 1520 |
mbedtls_x509_crt_free(&trusted); |
| 1521 |
mbedtls_x509_crt_free(&chain); |
| 1522 |
MD_OR_USE_PSA_DONE(); |
| 1523 |
} |
| 1524 |
/* END_CASE */ |
| 1525 |
|
| 1526 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
| 1527 |
void x509_oid_desc(data_t *buf, char *ref_desc) |
| 1528 |
{ |
| 1529 |
mbedtls_x509_buf oid; |
| 1530 |
const char *desc = NULL; |
| 1531 |
int ret; |
| 1532 |
|
| 1533 |
USE_PSA_INIT(); |
| 1534 |
|
| 1535 |
oid.tag = MBEDTLS_ASN1_OID; |
| 1536 |
oid.p = buf->x; |
| 1537 |
oid.len = buf->len; |
| 1538 |
|
| 1539 |
ret = mbedtls_x509_oid_get_extended_key_usage(&oid, &desc); |
| 1540 |
|
| 1541 |
if (strcmp(ref_desc, "notfound") == 0) { |
| 1542 |
TEST_ASSERT(ret != 0); |
| 1543 |
TEST_ASSERT(desc == NULL); |
| 1544 |
} else { |
| 1545 |
TEST_EQUAL(ret, 0); |
| 1546 |
TEST_ASSERT(desc != NULL); |
| 1547 |
TEST_EQUAL(strcmp(desc, ref_desc), 0); |
| 1548 |
} |
| 1549 |
|
| 1550 |
exit: |
| 1551 |
USE_PSA_DONE(); |
| 1552 |
} |
| 1553 |
/* END_CASE */ |
| 1554 |
|
| 1555 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */ |
| 1556 |
void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret) |
| 1557 |
{ |
| 1558 |
mbedtls_x509_buf oid; |
| 1559 |
char num_buf[100]; |
| 1560 |
|
| 1561 |
USE_PSA_INIT(); |
| 1562 |
|
| 1563 |
memset(num_buf, 0x2a, sizeof(num_buf)); |
| 1564 |
|
| 1565 |
oid.tag = MBEDTLS_ASN1_OID; |
| 1566 |
oid.p = oid_buf->x; |
| 1567 |
oid.len = oid_buf->len; |
| 1568 |
|
| 1569 |
TEST_ASSERT((size_t) blen <= sizeof(num_buf)); |
| 1570 |
|
| 1571 |
TEST_EQUAL(mbedtls_oid_get_numeric_string(num_buf, blen, &oid), ret); |
| 1572 |
|
| 1573 |
if (ret >= 0) { |
| 1574 |
TEST_EQUAL(num_buf[ret], 0); |
| 1575 |
TEST_EQUAL(strcmp(num_buf, numstr), 0); |
| 1576 |
} |
| 1577 |
|
| 1578 |
exit: |
| 1579 |
USE_PSA_DONE(); |
| 1580 |
} |
| 1581 |
/* END_CASE */ |
| 1582 |
|
| 1583 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| 1584 |
void x509_check_key_usage(char *crt_file, int usage, int ret) |
| 1585 |
{ |
| 1586 |
mbedtls_x509_crt crt; |
| 1587 |
|
| 1588 |
mbedtls_x509_crt_init(&crt); |
| 1589 |
USE_PSA_INIT(); |
| 1590 |
|
| 1591 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0); |
| 1592 |
|
| 1593 |
TEST_EQUAL(mbedtls_x509_crt_check_key_usage(&crt, usage), ret); |
| 1594 |
|
| 1595 |
exit: |
| 1596 |
mbedtls_x509_crt_free(&crt); |
| 1597 |
USE_PSA_DONE(); |
| 1598 |
} |
| 1599 |
/* END_CASE */ |
| 1600 |
|
| 1601 |
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| 1602 |
void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret |
| 1603 |
) |
| 1604 |
{ |
| 1605 |
mbedtls_x509_crt crt; |
| 1606 |
|
| 1607 |
mbedtls_x509_crt_init(&crt); |
| 1608 |
USE_PSA_INIT(); |
| 1609 |
|
| 1610 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0); |
| 1611 |
|
| 1612 |
TEST_EQUAL(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x, oid->len), |
| 1613 |
ret); |
| 1614 |
|
| 1615 |
exit: |
| 1616 |
mbedtls_x509_crt_free(&crt); |
| 1617 |
USE_PSA_DONE(); |
| 1618 |
} |
| 1619 |
/* END_CASE */ |
| 1620 |
|
| 1621 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */ |
| 1622 |
void x509_get_time(int tag, char *time_str, int ret, int year, int mon, |
| 1623 |
int day, int hour, int min, int sec) |
| 1624 |
{ |
| 1625 |
mbedtls_x509_time time; |
| 1626 |
unsigned char buf[21]; |
| 1627 |
unsigned char *start = buf; |
| 1628 |
unsigned char *end = buf; |
| 1629 |
|
| 1630 |
USE_PSA_INIT(); |
| 1631 |
memset(&time, 0x00, sizeof(time)); |
| 1632 |
*end = (unsigned char) tag; end++; |
| 1633 |
*end = strlen(time_str); |
| 1634 |
TEST_ASSERT(*end < 20); |
| 1635 |
end++; |
| 1636 |
memcpy(end, time_str, (size_t) *(end - 1)); |
| 1637 |
end += *(end - 1); |
| 1638 |
|
| 1639 |
TEST_EQUAL(mbedtls_x509_get_time(&start, end, &time), ret); |
| 1640 |
if (ret == 0) { |
| 1641 |
TEST_EQUAL(year, time.year); |
| 1642 |
TEST_EQUAL(mon, time.mon); |
| 1643 |
TEST_EQUAL(day, time.day); |
| 1644 |
TEST_EQUAL(hour, time.hour); |
| 1645 |
TEST_EQUAL(min, time.min); |
| 1646 |
TEST_EQUAL(sec, time.sec); |
| 1647 |
} |
| 1648 |
exit: |
| 1649 |
USE_PSA_DONE(); |
| 1650 |
} |
| 1651 |
/* END_CASE */ |
| 1652 |
|
| 1653 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
| 1654 |
void x509_parse_rsassa_pss_params(data_t *params, int params_tag, |
| 1655 |
int ref_msg_md, int ref_mgf_md, |
| 1656 |
int ref_salt_len, int ref_ret) |
| 1657 |
{ |
| 1658 |
int my_ret; |
| 1659 |
mbedtls_x509_buf buf; |
| 1660 |
mbedtls_md_type_t my_msg_md, my_mgf_md; |
| 1661 |
int my_salt_len; |
| 1662 |
|
| 1663 |
USE_PSA_INIT(); |
| 1664 |
|
| 1665 |
buf.p = params->x; |
| 1666 |
buf.len = params->len; |
| 1667 |
buf.tag = params_tag; |
| 1668 |
|
| 1669 |
my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md, |
| 1670 |
&my_salt_len); |
| 1671 |
|
| 1672 |
TEST_EQUAL(my_ret, ref_ret); |
| 1673 |
|
| 1674 |
if (ref_ret == 0) { |
| 1675 |
TEST_EQUAL(my_msg_md, (mbedtls_md_type_t) ref_msg_md); |
| 1676 |
TEST_EQUAL(my_mgf_md, (mbedtls_md_type_t) ref_mgf_md); |
| 1677 |
TEST_EQUAL(my_salt_len, ref_salt_len); |
| 1678 |
} |
| 1679 |
|
| 1680 |
exit: |
| 1681 |
USE_PSA_DONE(); |
| 1682 |
} |
| 1683 |
/* END_CASE */ |
| 1684 |
|
| 1685 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */ |
| 1686 |
void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret) |
| 1687 |
{ |
| 1688 |
mbedtls_x509_crt crt; |
| 1689 |
|
| 1690 |
mbedtls_x509_crt_init(&crt); |
| 1691 |
/* X509 relies on PK under the hood and the latter can use PSA to store keys |
| 1692 |
* and perform operations so psa_crypto_init() must be called before. */ |
| 1693 |
USE_PSA_INIT(); |
| 1694 |
|
| 1695 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret); |
| 1696 |
|
| 1697 |
if (ref_ret == 0) { |
| 1698 |
TEST_EQUAL(crt.subject_key_id.tag, MBEDTLS_ASN1_OCTET_STRING); |
| 1699 |
TEST_EQUAL(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len), 0); |
| 1700 |
TEST_EQUAL(crt.subject_key_id.len, subjectKeyId->len); |
| 1701 |
} else { |
| 1702 |
TEST_EQUAL(crt.subject_key_id.tag, 0); |
| 1703 |
TEST_EQUAL(crt.subject_key_id.len, 0); |
| 1704 |
} |
| 1705 |
|
| 1706 |
exit: |
| 1707 |
mbedtls_x509_crt_free(&crt); |
| 1708 |
USE_PSA_DONE(); |
| 1709 |
} |
| 1710 |
/* END_CASE */ |
| 1711 |
|
| 1712 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */ |
| 1713 |
void x509_crt_parse_authoritykeyid(char *file, |
| 1714 |
data_t *keyId, |
| 1715 |
char *authorityKeyId_issuer, |
| 1716 |
data_t *serial, |
| 1717 |
int ref_ret) |
| 1718 |
{ |
| 1719 |
mbedtls_x509_crt crt; |
| 1720 |
mbedtls_x509_subject_alternative_name san; |
| 1721 |
char name_buf[128]; |
| 1722 |
|
| 1723 |
mbedtls_x509_crt_init(&crt); |
| 1724 |
/* X509 relies on PK under the hood and the latter can use PSA to store keys |
| 1725 |
* and perform operations so psa_crypto_init() must be called before. */ |
| 1726 |
USE_PSA_INIT(); |
| 1727 |
|
| 1728 |
TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret); |
| 1729 |
|
| 1730 |
if (ref_ret == 0) { |
| 1731 |
/* KeyId test */ |
| 1732 |
if (keyId->len > 0) { |
| 1733 |
TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, MBEDTLS_ASN1_OCTET_STRING); |
| 1734 |
TEST_EQUAL(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len), 0); |
| 1735 |
TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, keyId->len); |
| 1736 |
} else { |
| 1737 |
TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0); |
| 1738 |
TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0); |
| 1739 |
} |
| 1740 |
|
| 1741 |
|
| 1742 |
/* Issuer test */ |
| 1743 |
if (strlen(authorityKeyId_issuer) > 0) { |
| 1744 |
mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer; |
| 1745 |
|
| 1746 |
TEST_EQUAL(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san), 0); |
| 1747 |
|
| 1748 |
TEST_ASSERT(mbedtls_x509_dn_gets(name_buf, sizeof(name_buf), |
| 1749 |
&san.san.directory_name) |
| 1750 |
> 0); |
| 1751 |
TEST_EQUAL(strcmp(name_buf, authorityKeyId_issuer), 0); |
| 1752 |
|
| 1753 |
mbedtls_x509_free_subject_alt_name(&san); |
| 1754 |
} |
| 1755 |
|
| 1756 |
/* Serial test */ |
| 1757 |
if (serial->len > 0) { |
| 1758 |
TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, |
| 1759 |
MBEDTLS_ASN1_INTEGER); |
| 1760 |
TEST_EQUAL(memcmp(crt.authority_key_id.authorityCertSerialNumber.p, |
| 1761 |
serial->x, serial->len), 0); |
| 1762 |
TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, serial->len); |
| 1763 |
} else { |
| 1764 |
TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0); |
| 1765 |
TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0); |
| 1766 |
} |
| 1767 |
|
| 1768 |
} else { |
| 1769 |
TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0); |
| 1770 |
TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0); |
| 1771 |
|
| 1772 |
TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0); |
| 1773 |
TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0); |
| 1774 |
} |
| 1775 |
|
| 1776 |
exit: |
| 1777 |
mbedtls_x509_crt_free(&crt); |
| 1778 |
USE_PSA_DONE(); |
| 1779 |
} |
| 1780 |
/* END_CASE */ |
| 1781 |
|
| 1782 |
/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */ |
| 1783 |
void oid_get_numeric_string(data_t *oid, int error_ret, char *result_str) |
| 1784 |
{ |
| 1785 |
char buf[256]; |
| 1786 |
mbedtls_asn1_buf input_oid = { 0, 0, NULL }; |
| 1787 |
int ret; |
| 1788 |
|
| 1789 |
input_oid.tag = MBEDTLS_ASN1_OID; |
| 1790 |
/* Test that an empty OID is not dereferenced */ |
| 1791 |
input_oid.p = oid->len ? oid->x : (void *) 1; |
| 1792 |
input_oid.len = oid->len; |
| 1793 |
|
| 1794 |
ret = mbedtls_oid_get_numeric_string(buf, sizeof(buf), &input_oid); |
| 1795 |
|
| 1796 |
if (error_ret == 0) { |
| 1797 |
TEST_EQUAL(ret, strlen(result_str)); |
| 1798 |
TEST_ASSERT(ret >= 3); |
| 1799 |
TEST_EQUAL(strcmp(buf, result_str), 0); |
| 1800 |
} else { |
| 1801 |
TEST_EQUAL(ret, error_ret); |
| 1802 |
} |
| 1803 |
} |
| 1804 |
/* END_CASE */ |