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