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