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