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