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