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