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