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