22 #if defined(MBEDTLS_X509_CRT_PARSE_C)
32 #if defined(MBEDTLS_PEM_PARSE_C)
36 #if defined(MBEDTLS_USE_PSA_CRYPTO)
45 #if defined(MBEDTLS_THREADING_C)
49 #if defined(MBEDTLS_HAVE_TIME)
50 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
51 #define WIN32_LEAN_AND_MEAN
58 #if defined(MBEDTLS_FS_IO)
60 #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
61 #include <sys/types.h>
64 #include <platform/mbed_retarget.h>
78 } x509_crt_verify_chain_item;
83 #define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
95 #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
120 #if defined(MBEDTLS_ECP_C)
146 #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
212 #if defined(MBEDTLS_RSA_C)
222 #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
246 static int x509_memcasecmp(
const void *s1,
const void *s2,
size_t len)
250 const unsigned char *n1 = s1, *n2 = s2;
252 for (
i = 0;
i <
len;
i++) {
253 diff = n1[
i] ^ n2[
i];
260 ((n1[
i] >=
'a' && n1[
i] <=
'z') ||
261 (n1[
i] >=
'A' && n1[
i] <=
'Z'))) {
274 static int x509_check_wildcard(
const char *cn,
const mbedtls_x509_buf *name)
277 size_t cn_idx = 0, cn_len = strlen(cn);
280 if (name->
len < 3 || name->
p[0] !=
'*' || name->
p[1] !=
'.') {
284 for (
i = 0;
i < cn_len; ++
i) {
295 if (cn_len - cn_idx == name->
len - 1 &&
296 x509_memcasecmp(name->
p + 1, cn + cn_idx, name->
len - 1) == 0) {
311 if (
a->tag ==
b->tag &&
313 memcmp(
a->p,
b->p,
b->len) == 0) {
320 x509_memcasecmp(
a->p,
b->p,
b->len) == 0) {
346 if (
a->oid.tag !=
b->oid.tag ||
347 a->oid.len !=
b->oid.len ||
348 memcmp(
a->oid.p,
b->oid.p,
b->oid.len) != 0) {
353 if (x509_string_cmp(&
a->val, &
b->val) != 0) {
358 if (
a->next_merged !=
b->next_merged) {
373 static void x509_crt_verify_chain_reset(
379 ver_chain->items[
i].crt =
NULL;
380 ver_chain->items[
i].flags = (
uint32_t) -1;
385 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
386 ver_chain->trust_ca_cb_result =
NULL;
393 static int x509_get_version(
unsigned char **p,
394 const unsigned char *end,
430 static int x509_get_dates(
unsigned char **p,
431 const unsigned char *end,
464 static int x509_get_uid(
unsigned char **p,
465 const unsigned char *end,
492 static int x509_get_basic_constraints(
unsigned char **p,
493 const unsigned char *end,
526 if (*ca_istrue != 0) {
546 if (*max_pathlen == INT_MAX) {
561 static int x509_get_ext_key_usage(
unsigned char **p,
562 const unsigned char *end,
585 static int x509_get_subject_key_id(
unsigned char **p,
586 const unsigned char *end,
597 subject_key_id->
len =
len;
599 subject_key_id->
p = *p;
618 static int x509_get_authority_key_id(
unsigned char **p,
630 if (*p +
len != end) {
737 static int x509_get_certificate_policies(
unsigned char **p,
738 const unsigned char *end,
741 int ret, parse_ret = 0;
753 if (*p +
len != end) {
768 const unsigned char *policy_end;
778 policy_end = *p +
len;
817 buf->tag = policy_oid.
tag;
818 buf->p = policy_oid.
p;
819 buf->len = policy_oid.
len;
827 if (*p < policy_end) {
839 if (*p != policy_end) {
860 static int x509_get_crt_ext(
unsigned char **p,
861 const unsigned char *end,
863 mbedtls_x509_crt_ext_cb_t cb,
868 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
895 end_ext_data = *p +
len;
919 start_ext_octet = *p;
920 end_ext_octet = *p +
len;
922 if (end_ext_octet != end_ext_data) {
935 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
936 if (ret != 0 && is_critical) {
955 if ((crt->ext_types & ext_type) != 0) {
959 crt->ext_types |= ext_type;
964 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
965 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
973 &crt->key_usage)) != 0) {
980 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
988 if ((ret = x509_get_subject_key_id(p, end_ext_data,
996 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1014 &crt->ns_cert_type)) != 0) {
1021 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1026 cb(p_ctx, crt, &extn_oid, is_critical,
1027 start_ext_octet, end_ext_octet) == 0) {
1072 const unsigned char *
buf,
1075 mbedtls_x509_crt_ext_cb_t cb,
1080 unsigned char *p, *end, *crt_end;
1095 p = (
unsigned char *)
buf;
1111 end = crt_end = p +
len;
1112 crt->
raw.
len = (size_t) (crt_end -
buf);
1113 if (make_copy != 0) {
1121 crt->own_buffer = 1;
1124 end = crt_end = p +
len;
1126 crt->
raw.
p = (
unsigned char *)
buf;
1127 crt->own_buffer = 0;
1151 if ((ret = x509_get_version(&p, end, &crt->
version)) != 0 ||
1154 &sig_params1)) != 0) {
1167 &crt->sig_md, &crt->sig_pk,
1168 &crt->sig_opts)) != 0) {
1197 if ((ret = x509_get_dates(&p, end, &crt->
valid_from,
1240 ret = x509_get_uid(&p, end, &crt->
issuer_id, 1);
1248 ret = x509_get_uid(&p, end, &crt->
subject_id, 2);
1256 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1285 sig_params1.
tag != sig_params2.
tag ||
1286 sig_params1.
len != sig_params2.
len ||
1287 (sig_params1.
len != 0 &&
1288 memcmp(sig_params1.
p, sig_params2.
p, sig_params1.
len) != 0)) {
1312 const unsigned char *
buf,
1315 mbedtls_x509_crt_ext_cb_t cb,
1348 ret = x509_crt_parse_der_core(crt,
buf, buflen, make_copy, cb, p_ctx);
1365 const unsigned char *
buf,
1368 return mbedtls_x509_crt_parse_der_internal(chain,
buf, buflen, 0,
NULL,
NULL);
1372 const unsigned char *
buf,
1375 mbedtls_x509_crt_ext_cb_t cb,
1378 return mbedtls_x509_crt_parse_der_internal(chain,
buf, buflen, make_copy, cb, p_ctx);
1382 const unsigned char *
buf,
1385 return mbedtls_x509_crt_parse_der_internal(chain,
buf, buflen, 1,
NULL,
NULL);
1393 const unsigned char *
buf,
1396 #if defined(MBEDTLS_PEM_PARSE_C)
1397 int success = 0, first_error = 0, total_failed = 0;
1412 #if defined(MBEDTLS_PEM_PARSE_C)
1413 if (buflen != 0 &&
buf[buflen - 1] ==
'\0' &&
1414 strstr((
const char *)
buf,
"-----BEGIN CERTIFICATE-----") !=
NULL) {
1425 #if defined(MBEDTLS_PEM_PARSE_C)
1428 mbedtls_pem_context pem;
1431 while (buflen > 1) {
1437 "-----BEGIN CERTIFICATE-----",
1438 "-----END CERTIFICATE-----",
1458 if (first_error == 0) {
1480 if (first_error == 0) {
1493 return total_failed;
1494 }
else if (first_error) {
1502 #if defined(MBEDTLS_FS_IO)
1526 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
1528 WCHAR szDir[MAX_PATH];
1529 char filename[MAX_PATH];
1531 size_t len = strlen(path);
1533 WIN32_FIND_DATAW file_data;
1536 if (
len > MAX_PATH - 3) {
1540 memset(szDir, 0,
sizeof(szDir));
1541 memset(filename, 0, MAX_PATH);
1542 memcpy(filename, path,
len);
1543 filename[
len++] =
'\\';
1545 filename[
len++] =
'*';
1552 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (
int)
len, szDir,
1558 hFind = FindFirstFileW(szDir, &file_data);
1567 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1570 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
1583 }
while (FindNextFileW(hFind, &file_data) != 0);
1585 if (GetLastError() != ERROR_NO_MORE_FILES) {
1595 struct dirent *entry;
1597 DIR *dir = opendir(path);
1603 #if defined(MBEDTLS_THREADING_C)
1610 memset(&sb, 0,
sizeof(sb));
1612 while ((entry = readdir(dir)) !=
NULL) {
1614 "%s/%s", path, entry->d_name);
1616 if (snp_ret < 0 || (
size_t) snp_ret >=
sizeof(entry_name)) {
1619 }
else if (stat(entry_name, &sb) == -1) {
1620 if (errno == ENOENT) {
1635 if (!S_ISREG(sb.st_mode)) {
1652 #if defined(MBEDTLS_THREADING_C)
1664 #if !defined(MBEDTLS_X509_REMOVE_INFO)
1665 #define PRINT_ITEM(i) \
1667 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1668 MBEDTLS_X509_SAFE_SNPRINTF; \
1672 #define CERT_TYPE(type, name) \
1674 if (ns_cert_type & (type)) { \
1679 #define KEY_USAGE(code, name) \
1681 if (key_usage & (code)) { \
1686 static int x509_info_ext_key_usage(
char **
buf,
size_t *
size,
1694 const char *sep =
"";
1696 while (cur !=
NULL) {
1715 static int x509_info_cert_policies(
char **
buf,
size_t *
size,
1723 const char *sep =
"";
1725 while (cur !=
NULL) {
1747 #define BEFORE_COLON 18
1755 char key_size_str[BEFORE_COLON];
1764 return (
int) (
size -
n);
1788 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1795 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1805 crt->sig_md, crt->sig_opts);
1824 crt->ca_istrue ?
"true" :
"false");
1827 if (crt->max_pathlen > 0) {
1866 if ((ret = x509_info_ext_key_usage(&p, &
n,
1876 if ((ret = x509_info_cert_policies(&p, &
n,
1885 return (
int) (
size -
n);
1888 struct x509_crt_verify_string {
1893 #define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
1894 static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
1898 #undef X509_CRT_ERROR_INFO
1904 const struct x509_crt_verify_string *cur;
1908 for (cur = x509_crt_verify_strings; cur->string !=
NULL; cur++) {
1909 if ((
flags & cur->code) == 0) {
1920 "(this should not happen)\n", prefix);
1924 return (
int) (
size -
n);
1931 unsigned int usage_must, usage_may;
1939 usage_must =
usage & ~may_mask;
1941 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1945 usage_may =
usage & may_mask;
1947 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1955 const char *usage_oid,
1971 if (cur_oid->
len == usage_len &&
1972 memcmp(cur_oid->
p, usage_oid, usage_len) == 0) {
1984 #if defined(MBEDTLS_X509_CRL_PARSE_C)
2015 #if defined(MBEDTLS_USE_PSA_CRYPTO)
2026 while (crl_list !=
NULL) {
2029 crl_list = crl_list->
next;
2045 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
2049 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
2053 #if defined(MBEDTLS_USE_PSA_CRYPTO)
2054 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
2078 if (x509_profile_check_key(profile, &ca->
pk) != 0) {
2083 crl_list->sig_md,
hash, hash_length,
2084 crl_list->sig.p, crl_list->sig.len) != 0) {
2089 #if defined(MBEDTLS_HAVE_TIME_DATE)
2112 crl_list = crl_list->
next;
2128 #if !defined(MBEDTLS_USE_PSA_CRYPTO)
2138 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
2157 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2161 child->sig.p, child->sig.len, &rs_ctx->
pk);
2169 child->sig.p, child->sig.len);
2193 if (top && parent->
version < 3) {
2197 if (need_ca_bit && !parent->ca_istrue) {
2252 static int x509_crt_find_parent_in(
2256 int *r_signature_is_good,
2265 int signature_is_good = 0, fallback_signature_is_good;
2267 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2269 if (rs_ctx !=
NULL && rs_ctx->parent !=
NULL) {
2271 parent = rs_ctx->parent;
2272 fallback_parent = rs_ctx->fallback_parent;
2273 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
2276 rs_ctx->parent =
NULL;
2277 rs_ctx->fallback_parent =
NULL;
2278 rs_ctx->fallback_signature_is_good = 0;
2281 goto check_signature;
2285 fallback_parent =
NULL;
2286 fallback_signature_is_good = 0;
2288 for (parent = candidates; parent !=
NULL; parent = parent->
next) {
2290 if (x509_crt_check_parent(child, parent, top) != 0) {
2295 if (parent->max_pathlen > 0 &&
2296 (
size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
2301 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2304 ret = x509_crt_check_signature(child, parent, rs_ctx);
2306 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2309 rs_ctx->parent = parent;
2310 rs_ctx->fallback_parent = fallback_parent;
2311 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
2319 signature_is_good = ret == 0;
2320 if (top && !signature_is_good) {
2324 #if defined(MBEDTLS_HAVE_TIME_DATE)
2328 if (fallback_parent ==
NULL) {
2329 fallback_parent = parent;
2330 fallback_signature_is_good = signature_is_good;
2340 *r_signature_is_good = signature_is_good;
2345 if (parent ==
NULL) {
2346 *r_parent = fallback_parent;
2347 *r_signature_is_good = fallback_signature_is_good;
2375 static int x509_crt_find_parent(
2379 int *parent_is_trusted,
2380 int *signature_is_good,
2389 *parent_is_trusted = 1;
2391 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2393 if (rs_ctx !=
NULL && rs_ctx->parent_is_trusted != -1) {
2394 *parent_is_trusted = rs_ctx->parent_is_trusted;
2395 rs_ctx->parent_is_trusted = -1;
2400 search_list = *parent_is_trusted ? trust_ca : child->
next;
2402 ret = x509_crt_find_parent_in(child, search_list,
2403 parent, signature_is_good,
2405 path_cnt, self_cnt, rs_ctx, now);
2407 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2410 rs_ctx->parent_is_trusted = *parent_is_trusted;
2418 if (*parent !=
NULL || *parent_is_trusted == 0) {
2423 *parent_is_trusted = 0;
2427 if (*parent ==
NULL) {
2428 *parent_is_trusted = 0;
2429 *signature_is_good = 0;
2441 static int x509_crt_check_ee_locally_trusted(
2453 for (cur = trust_ca; cur !=
NULL; cur = cur->
next) {
2504 static int x509_crt_verify_chain(
2508 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2521 int parent_is_trusted;
2522 int child_is_trusted;
2523 int signature_is_good;
2528 #if defined(MBEDTLS_HAVE_TIME_DATE)
2534 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2536 if (rs_ctx !=
NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
2538 *ver_chain = rs_ctx->ver_chain;
2539 self_cnt = rs_ctx->self_cnt;
2542 cur = &ver_chain->items[ver_chain->len - 1];
2544 flags = &cur->flags;
2552 parent_is_trusted = 0;
2553 child_is_trusted = 0;
2557 cur = &ver_chain->items[ver_chain->len];
2561 flags = &cur->flags;
2563 #if defined(MBEDTLS_HAVE_TIME_DATE)
2575 if (child_is_trusted) {
2580 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
2584 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
2589 if (ver_chain->len == 1 &&
2590 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2594 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2600 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2601 if (f_ca_cb !=
NULL) {
2604 ver_chain->trust_ca_cb_result =
NULL;
2606 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2611 cur_trust_ca = ver_chain->trust_ca_cb_result;
2617 cur_trust_ca = trust_ca;
2621 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2622 &parent_is_trusted, &signature_is_good,
2623 ver_chain->len - 1, self_cnt, rs_ctx,
2626 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2629 rs_ctx->in_progress = x509_crt_rs_find_parent;
2630 rs_ctx->self_cnt = self_cnt;
2631 rs_ctx->ver_chain = *ver_chain;
2640 if (parent ==
NULL) {
2648 if (ver_chain->len != 1 &&
2655 if (!parent_is_trusted &&
2662 if (!signature_is_good) {
2667 if (x509_profile_check_key(profile, &parent->
pk) != 0) {
2671 #if defined(MBEDTLS_X509_CRL_PARSE_C)
2673 *
flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
2681 child_is_trusted = parent_is_trusted;
2682 signature_is_good = 0;
2688 #pragma comment(lib, "ws2_32.lib")
2689 #include <winsock2.h>
2690 #include <ws2tcpip.h>
2691 #elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2692 #include <winsock2.h>
2693 #include <ws2tcpip.h>
2696 #define MBEDTLS_TEST_SW_INET_PTON
2698 #elif defined(__sun)
2700 #elif defined(__has_include)
2701 #if __has_include(<sys/socket.h>)
2702 #include <sys/socket.h>
2704 #if __has_include(<arpa/inet.h>)
2705 #include <arpa/inet.h>
2721 #if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON)
2723 static int x509_inet_pton_ipv4(
const char *src,
void *dst);
2725 #define li_cton(c, n) \
2726 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2728 static int x509_inet_pton_ipv6(
const char *src,
void *dst)
2730 const unsigned char *p = (
const unsigned char *) src;
2731 int nonzero_groups = 0, num_digits, zero_group_start = -1;
2736 for (
uint8_t digit; num_digits < 4; num_digits++) {
2737 if (li_cton(*p, digit) == 0) {
2740 group = (group << 4) | digit;
2743 if (num_digits != 0) {
2748 }
else if (*p ==
'.') {
2750 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2751 nonzero_groups >= 7) {
2760 }
while (*p !=
':' && steps > 0);
2767 if (x509_inet_pton_ipv4((
const char *) p,
2768 addr + nonzero_groups) != 0) {
2772 nonzero_groups += 2;
2773 p = (
const unsigned char *)
"";
2775 }
else if (*p !=
':') {
2780 if (zero_group_start != -1 || *p !=
':') {
2783 zero_group_start = nonzero_groups;
2786 if (zero_group_start == 0 && *++p !=
':') {
2796 }
while (nonzero_groups < 8);
2802 if (zero_group_start != -1) {
2803 if (nonzero_groups > 6) {
2806 int zero_groups = 8 - nonzero_groups;
2807 int groups_after_zero = nonzero_groups - zero_group_start;
2810 if (groups_after_zero) {
2812 addr + zero_group_start,
2813 groups_after_zero *
sizeof(*
addr));
2815 memset(
addr + zero_group_start, 0, zero_groups *
sizeof(*
addr));
2817 if (nonzero_groups != 8) {
2825 static int x509_inet_pton_ipv4(
const char *src,
void *dst)
2827 const unsigned char *p = (
const unsigned char *) src;
2829 uint8_t digit, num_digits = 0;
2834 octet = num_digits = 0;
2843 if (octet == 0 && num_digits > 0) {
2847 octet = octet * 10 + digit;
2850 }
while (num_digits < 3);
2852 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
2857 }
while (num_octets < 4 && *p++ ==
'.');
2858 return num_octets == 4 && *p ==
'\0' ? 0 : -1;
2863 static int x509_inet_pton_ipv6(
const char *src,
void *dst)
2865 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2868 static int x509_inet_pton_ipv4(
const char *src,
void *dst)
2870 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2877 return strchr(cn,
':') ==
NULL
2878 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2879 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2886 const char *cn,
size_t cn_len)
2889 if (name->
len == cn_len &&
2890 x509_memcasecmp(cn, name->
p, cn_len) == 0) {
2895 if (x509_check_wildcard(cn, name) == 0) {
2903 const char *cn,
size_t cn_len)
2912 const unsigned char san_type = (
unsigned char) cur->buf.tag &
2915 cur->buf.len == cn_len && memcmp(cur->buf.p,
ip, cn_len) == 0) {
2924 const char *cn,
size_t cn_len)
2927 const unsigned char san_type = (
unsigned char) cur->buf.tag &
2930 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2942 const char *cn,
size_t cn_len)
2950 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2966 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
2971 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
2987 size_t cn_len = strlen(cn);
2996 x509_crt_check_cn(&name->
val, cn, cn_len) == 0) {
3009 static int x509_crt_merge_flags_with_cb(
3020 for (
i = ver_chain->len;
i != 0; --
i) {
3021 cur = &ver_chain->items[
i-1];
3022 cur_flags = cur->flags;
3024 if (
NULL != f_vrfy) {
3025 if ((ret = f_vrfy(p_vrfy, cur->crt, (
int)
i-1, &cur_flags)) != 0) {
3030 *
flags |= cur_flags;
3056 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3060 int (*f_vrfy)(
void *,
3074 x509_crt_verify_chain_reset(&ver_chain);
3076 if (profile ==
NULL) {
3083 x509_crt_verify_name(crt, cn, &ee_flags);
3089 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
3093 if (x509_profile_check_key(profile, &crt->
pk) != 0) {
3098 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3099 f_ca_cb, p_ca_cb, profile,
3100 &ver_chain, rs_ctx);
3107 ver_chain.items[0].flags |= ee_flags;
3110 ret = x509_crt_merge_flags_with_cb(
flags, &ver_chain, f_vrfy, p_vrfy);
3114 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3117 ver_chain.trust_ca_cb_result =
NULL;
3120 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3122 mbedtls_x509_crt_restart_free(rs_ctx);
3156 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3160 f_vrfy, p_vrfy,
NULL);
3174 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3177 f_vrfy, p_vrfy,
NULL);
3180 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3186 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3193 return x509_crt_verify_restartable_ca_cb(crt,
NULL,
NULL,
3196 f_vrfy, p_vrfy,
NULL);
3209 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3212 f_vrfy, p_vrfy, rs_ctx);
3232 while (cert_cur !=
NULL) {
3235 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3246 if (cert_cur->
raw.
p !=
NULL && cert_cur->own_buffer) {
3250 cert_prv = cert_cur;
3251 cert_cur = cert_cur->
next;
3254 if (cert_prv != crt) {
3260 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3266 mbedtls_pk_restart_init(&
ctx->pk);
3270 ctx->fallback_signature_is_good = 0;
3272 ctx->parent_is_trusted = -1;
3274 ctx->in_progress = x509_crt_rs_none;
3276 x509_crt_verify_chain_reset(&
ctx->ver_chain);
3288 mbedtls_pk_restart_free(&
ctx->pk);
3289 mbedtls_x509_crt_restart_init(
ctx);
#define MBEDTLS_PUT_UINT16_BE(n, data, offset)
Put in memory a 16 bits unsigned integer in big-endian order.
Platform Security Architecture cryptography module.
#define MBEDTLS_ERR_ECP_IN_PROGRESS
Operation in progress, call again with the same parameters to continue.
mbedtls_ecp_group_id
Domain-parameter identifiers: curve, subgroup, and generator.
@ MBEDTLS_ECP_DP_SECP384R1
@ MBEDTLS_ECP_DP_SECP256K1
@ MBEDTLS_ECP_DP_SECP521R1
@ MBEDTLS_ECP_DP_SECP256R1
static void cleanup(void)
static DLIST_TYPE *DLIST_NAME() prev(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
size_t len
ASN1 length, in octets.
#define MBEDTLS_ASN1_OCTET_STRING
mbedtls_asn1_buf oid
The object identifier.
unsigned char * p
ASN1 data, e.g.
#define MBEDTLS_OID_CMP(oid_str, oid_buf)
Compares an mbedtls_asn1_buf structure to a reference OID.
#define MBEDTLS_ERR_ASN1_INVALID_LENGTH
Error when trying to determine the length or invalid length.
struct mbedtls_asn1_named_data * next
The next entry in the sequence.
#define MBEDTLS_ASN1_PRINTABLE_STRING
#define MBEDTLS_ASN1_SEQUENCE
#define MBEDTLS_ASN1_INTEGER
mbedtls_asn1_buf buf
Buffer containing the given ASN.1 item.
#define MBEDTLS_ASN1_CONTEXT_SPECIFIC
#define MBEDTLS_ASN1_CONSTRUCTED
#define MBEDTLS_ASN1_TAG_VALUE_MASK
#define MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
ASN1 tag was of an unexpected value.
#define MBEDTLS_ERR_ASN1_ALLOC_FAILED
Memory allocation failed.
struct mbedtls_asn1_sequence * next
The next entry in the sequence.
#define MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
Actual length differs from expected length.
mbedtls_asn1_buf val
The named value.
#define MBEDTLS_ASN1_UTF8_STRING
#define INVALID_HANDLE_VALUE
A value for an invalid file handle.
#define HANDLE
An abstraction for a file handle.
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
int32_t psa_status_t
Function return status.
#define PSA_ERROR_CORRUPTION_DETECTED
A tampering attempt was detected.
#define PSA_SUCCESS
The action was completed successfully.
psa_status_t psa_hash_compute(psa_algorithm_t alg, const uint8_t *input, size_t input_length, uint8_t *hash, size_t hash_size, size_t *hash_length)
Calculate the hash (digest) of a message.
#define MBEDTLS_X509_BADCERT_NOT_TRUSTED
The certificate is not correctly signed by the trusted CA.
#define MBEDTLS_X509_CRT_ERROR_INFO_LIST
#define MBEDTLS_X509_BADCRL_BAD_PK
The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA).
#define MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE
Unavailable feature, e.g.
void mbedtls_x509_crt_restart_ctx
#define MBEDTLS_X509_BADCERT_BAD_MD
The certificate is signed with an unacceptable hash.
#define MBEDTLS_ERR_X509_INVALID_FORMAT
The CRT/CRL/CSR format is invalid, e.g.
#define MBEDTLS_X509_MAX_FILE_PATH_LEN
#define MBEDTLS_ERR_X509_FATAL_ERROR
A fatal error occurred, eg the chain is too long or the vrfy callback failed.
#define MBEDTLS_ERR_X509_INVALID_VERSION
The CRT/CRL/CSR version element is invalid.
#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE
Max size of verification chain: end-entity + intermediates + trusted root.
#define MBEDTLS_X509_BADCERT_EXPIRED
The certificate validity has expired.
#define MBEDTLS_X509_BADCERT_CN_MISMATCH
The certificate Common Name (CN) does not match with the expected CN.
#define MBEDTLS_X509_BADCERT_BAD_PK
The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA).
#define MBEDTLS_ERR_X509_INVALID_EXTENSIONS
The extension tag or value is invalid.
#define MBEDTLS_ERR_X509_UNKNOWN_VERSION
CRT/CRL/CSR has an unsupported version number.
#define MBEDTLS_ERR_X509_BUFFER_TOO_SMALL
Destination buffer is too small.
#define MBEDTLS_X509_MAX_INTERMEDIATE_CA
Maximum number of intermediate CAs in a verification chain.
#define MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
Certificate verification failed, e.g.
#define MBEDTLS_ERR_X509_INVALID_DATE
The date tag or value is invalid.
#define MBEDTLS_ERR_X509_SIG_MISMATCH
Signature algorithms do not match.
#define MBEDTLS_X509_BADCERT_FUTURE
The certificate validity starts in the future.
#define MBEDTLS_ERR_X509_ALLOC_FAILED
Allocation of memory failed.
#define MBEDTLS_X509_BADCRL_NOT_TRUSTED
The CRL is not correctly signed by the trusted CA.
#define MBEDTLS_X509_BADCRL_BAD_MD
The CRL is signed with an unacceptable hash.
#define MBEDTLS_ERR_X509_FILE_IO_ERROR
Read/write of file failed.
#define MBEDTLS_X509_BADCRL_FUTURE
The CRL is from the future.
#define MBEDTLS_X509_BADCERT_REVOKED
The certificate has been revoked (is on a CRL).
#define MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT
Format not recognized as DER or PEM.
#define MBEDTLS_X509_BADCRL_EXPIRED
The CRL is expired.
#define MBEDTLS_X509_ID_FLAG(id)
Build flag from an algorithm/curve identifier (pk, md, ecp) Since 0 is always XXX_NONE,...
#define MBEDTLS_ERR_X509_BAD_INPUT_DATA
Input invalid.
#define MBEDTLS_X509_BADCERT_BAD_KEY
The certificate is signed with an unacceptable key (eg bad curve, RSA too short).
int mbedtls_x509_crt_get_ca_istrue(const mbedtls_x509_crt *crt)
Access the ca_istrue field.
if(yy_accept[yy_current_state])
mbedtls_md_type_t
Supported message digests.
@ MBEDTLS_MD_SHA512
The SHA-512 message digest.
@ MBEDTLS_MD_SHA384
The SHA-384 message digest.
@ MBEDTLS_MD_SHA256
The SHA-256 message digest.
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
This function calculates the message-digest of a buffer, with respect to a configurable message-diges...
const mbedtls_md_info_t * mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
This function returns the message-digest information associated with the given digest type.
#define MBEDTLS_MD_MAX_SIZE
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
This function extracts the message-digest size from the message-digest information structure.
const struct ncbi::grid::netcache::search::fields::SIZE size
#define mbedtls_pem_read_buffer
#define mbedtls_x509_time_gmtime
#define mbedtls_x509_crt_parse_der
#define mbedtls_asn1_sequence_free
#define mbedtls_x509_crt_parse_der_nocopy
#define mbedtls_platform_zeroize
#define mbedtls_x509_crt_profile_default
#define mbedtls_x509_crt_parse_der_with_ext_cb
#define mbedtls_asn1_get_bool
#define mbedtls_x509_crt_parse_file
#define mbedtls_x509_crt_is_revoked
#define mbedtls_asn1_get_int
#define mbedtls_x509_crt_profile_none
#define mbedtls_x509_crt_profile_next
#define mbedtls_pk_load_file
#define mbedtls_x509_crt_init
#define mbedtls_x509_crt_check_extended_key_usage
#define mbedtls_zeroize_and_free
#define mbedtls_x509_crt_verify_restartable
#define mbedtls_asn1_free_named_data_list_shallow
#define mbedtls_x509_crt_free
#define mbedtls_pk_parse_subpubkey
#define mbedtls_x509_crt_profile_suiteb
#define mbedtls_x509_crt_check_key_usage
#define mbedtls_asn1_get_tag
#define mbedtls_asn1_get_sequence_of
#define mbedtls_x509_crt_verify_info
#define mbedtls_x509_crt_parse
#define mbedtls_mutex_lock
#define mbedtls_x509_crt_info
#define mbedtls_x509_crt_verify
#define mbedtls_threading_readdir_mutex
#define mbedtls_mutex_unlock
#define mbedtls_x509_crt_parse_path
#define mbedtls_x509_crt_verify_with_profile
Object Identifier (OID) database.
#define MBEDTLS_OID_ANY_POLICY
anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
int mbedtls_oid_get_extended_key_usage(const mbedtls_asn1_buf *oid, const char **desc)
Translate Extended Key Usage OID into description.
#define MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE
anyExtendedKeyUsage OBJECT IDENTIFIER ::= { id-ce-extKeyUsage 0 }
int mbedtls_oid_get_x509_ext_type(const mbedtls_asn1_buf *oid, int *ext_type)
Translate an X.509 extension OID into local values.
int mbedtls_oid_get_certificate_policies(const mbedtls_asn1_buf *oid, const char **desc)
Translate certificate policies OID into description.
#define MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES
#define MBEDTLS_OID_AT_CN
id-at-commonName AttributeType:= {id-at 3}
Privacy Enhanced Mail (PEM) decoding.
#define MBEDTLS_ERR_PEM_BAD_INPUT_DATA
Bad input parameters to function.
#define MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT
No PEM header or footer found.
const char * mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Access the type name.
int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Tell if a context can do the operation given by type.
mbedtls_pk_type_t
Public key types.
size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Get the size in bits of the underlying key.
int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options, mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature, with options.
int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len, mbedtls_pk_restart_ctx *rs_ctx)
Restartable version of mbedtls_pk_verify()
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Get the key type.
void mbedtls_pk_free(mbedtls_pk_context *ctx)
Free the components of a mbedtls_pk_context.
Public Key abstraction layer: internal (i.e.
Utility functions for the use of the PSA Crypto library.
Internal utility functions for use of PSA Crypto.
Error to string translation.
#define MBEDTLS_ERROR_ADD(high, low)
Combines a high-level and low-level error code together.
#define MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
This is a bug in the library.
#define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
Hardware accelerator failed.
Type-length-value structure that allows for ASN1 using DER.
Container for a sequence or list of 'named' ASN.1 data items.
Container for a sequence of ASN.1 items.
Message digest information.
mbedtls_x509_buf authorityCertSerialNumber
mbedtls_x509_sequence authorityCertIssuer
mbedtls_x509_buf keyIdentifier
Certificate revocation list entry.
mbedtls_x509_buf serial
The serial number of the revoked certificate.
struct mbedtls_x509_crl_entry * next
Next element in the linked list of entries.
Certificate revocation list structure.
mbedtls_x509_time this_update
struct mbedtls_x509_crl * next
Next element in the linked list of CRL.
mbedtls_x509_time next_update
mbedtls_x509_crl_entry entry
The CRL entries containing the certificate revocation times for this CA.
mbedtls_x509_name issuer
The parsed issuer data (named information object).
int version
CRL version (1=v1, 2=v2)
mbedtls_x509_buf tbs
The raw certificate body (DER).
Security profile for certificate verification.
uint32_t rsa_min_bitlen
Minimum size for RSA keys.
uint32_t allowed_mds
MDs for signatures.
uint32_t allowed_pks
PK algs for public keys; this applies to all certificates in the provided chain.
uint32_t allowed_curves
Elliptic curves for ECDSA.
Item in a verification chain: cert and flags for it.
Verification chain as built by mbedtls_crt_verify_chain()
Container for an X.509 certificate.
mbedtls_x509_time valid_to
End time of certificate validity.
mbedtls_x509_buf sig_oid
Signature algorithm, e.g.
int MBEDTLS_PRIVATE(own_buffer)
Indicates if raw is owned by the structure or not.
mbedtls_x509_sequence subject_alt_names
Optional list of raw entries of Subject Alternative Names extension.
mbedtls_x509_buf tbs
The raw certificate body (DER).
mbedtls_x509_authority authority_key_id
Optional X.509 v3 extension authority key identifier.
mbedtls_x509_buf subject_key_id
Optional X.509 v3 extension subject key identifier.
mbedtls_x509_buf raw
The raw certificate data (DER).
mbedtls_x509_buf serial
Unique id for certificate issued by a specific CA.
mbedtls_pk_context pk
Container for the public key context.
mbedtls_x509_buf v3_ext
Optional X.509 v3 extensions.
mbedtls_x509_buf issuer_id
Optional X.509 v2/v3 issuer unique identifier.
mbedtls_x509_name subject
The parsed subject data (named information object).
mbedtls_x509_sequence certificate_policies
Optional list of certificate policies (Only anyPolicy is printed and enforced, however the rest of th...
int version
The X.509 version.
mbedtls_x509_time valid_from
Start time of certificate validity.
mbedtls_x509_buf subject_raw
The raw subject data (DER).
mbedtls_x509_sequence ext_key_usage
Optional list of extended key usage OIDs.
struct mbedtls_x509_crt * next
Next certificate in the linked list that constitutes the CA chain.
mbedtls_x509_buf subject_id
Optional X.509 v2/v3 subject unique identifier.
mbedtls_x509_name issuer
The parsed issuer data (named information object).
mbedtls_x509_buf issuer_raw
The raw issuer data (DER).
Container for date and time (precision in seconds).
Threading abstraction layer.
#define MBEDTLS_ERR_THREADING_MUTEX_ERROR
Locking / unlocking / free failed with error code.
#define MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER
#define MBEDTLS_X509_FORMAT_PEM
#define MBEDTLS_X509_KU_DECIPHER_ONLY
int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Store the certificate DN in printable form into buf; no more than size characters will be written.
#define MBEDTLS_X509_KU_KEY_CERT_SIGN
int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1, const mbedtls_x509_time *t2)
Compare pair of mbedtls_x509_time.
#define MBEDTLS_X509_SAN_IP_ADDRESS
#define MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE
#define MBEDTLS_X509_SAFE_SNPRINTF
#define MBEDTLS_X509_EXT_BASIC_CONSTRAINTS
#define MBEDTLS_X509_KU_CRL_SIGN
#define MBEDTLS_X509_SAN_DNS_NAME
size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
This function parses a CN string as an IP address.
#define MBEDTLS_X509_EXT_KEY_USAGE
#define MBEDTLS_X509_EXT_SUBJECT_ALT_NAME
#define MBEDTLS_X509_KU_ENCIPHER_ONLY
#define MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER
#define MBEDTLS_X509_FORMAT_DER
#define MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER
#define MBEDTLS_X509_EXT_NS_CERT_TYPE
int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Store the certificate serial in printable form into buf; no more than size characters will be written...
X.509 certificate parsing and writing.
int mbedtls_x509_get_subject_alt_name(unsigned char **p, const unsigned char *end, mbedtls_x509_sequence *subject_alt_name)
int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end, mbedtls_x509_name *cur)
int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params, mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg, void **sig_opts)
int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
int mbedtls_x509_get_key_usage(unsigned char **p, const unsigned char *end, unsigned int *key_usage)
int mbedtls_x509_info_cert_type(char **buf, size_t *size, unsigned char ns_cert_type)
int mbedtls_x509_info_key_usage(char **buf, size_t *size, unsigned int key_usage)
int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *ext, int tag)
int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size, const mbedtls_x509_sequence *subject_alt_name, const char *prefix)
int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid, mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg, const void *sig_opts)
int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *serial)
int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end, mbedtls_x509_time *t)
int mbedtls_x509_get_ns_cert_type(unsigned char **p, const unsigned char *end, unsigned char *ns_cert_type)
int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p, const unsigned char *end, mbedtls_x509_sequence *subject_alt_name)