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