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