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