Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 2 | #include "mbedtls/x509.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 3 | #include "mbedtls/x509_crt.h" |
| 4 | #include "mbedtls/x509_crl.h" |
| 5 | #include "mbedtls/x509_csr.h" |
| 6 | #include "mbedtls/pem.h" |
| 7 | #include "mbedtls/oid.h" |
| 8 | #include "mbedtls/base64.h" |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 9 | |
Simon Butcher | efdfeeb | 2017-07-28 12:15:13 +0100 | [diff] [blame] | 10 | #if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19 |
Hanno Becker | 3b1422e | 2017-07-26 13:38:02 +0100 | [diff] [blame] | 11 | #error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \ |
| 12 | than the current threshold 19. To test larger values, please \ |
| 13 | adapt the script tests/data_files/dir-max/long.sh." |
| 14 | #endif |
| 15 | |
Hanno Becker | 958c411 | 2019-06-03 14:27:03 +0100 | [diff] [blame] | 16 | /* Test-only profile allowing all digests, PK algorithms, and curves. */ |
| 17 | const mbedtls_x509_crt_profile profile_all = |
| 18 | { |
| 19 | 0xFFFFFFFF, /* Any MD */ |
| 20 | 0xFFFFFFFF, /* Any PK alg */ |
| 21 | 0xFFFFFFFF, /* Any curve */ |
| 22 | 1024, |
| 23 | }; |
| 24 | |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 25 | /* Profile for backward compatibility. Allows SHA-1, unlike the default |
| 26 | profile. */ |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 27 | const mbedtls_x509_crt_profile compat_profile = |
| 28 | { |
| 29 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) | |
| 30 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) | |
| 31 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) | |
| 32 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) | |
| 33 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) | |
| 34 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ), |
| 35 | 0xFFFFFFF, /* Any PK alg */ |
| 36 | 0xFFFFFFF, /* Any curve */ |
| 37 | 1024, |
| 38 | }; |
| 39 | |
Manuel Pégourié-Gonnard | 189bb40 | 2017-05-23 11:29:29 +0200 | [diff] [blame] | 40 | const mbedtls_x509_crt_profile profile_rsa3072 = |
| 41 | { |
| 42 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) | |
| 43 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) | |
| 44 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ), |
| 45 | MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ), |
| 46 | 0, |
| 47 | 3072, |
| 48 | }; |
| 49 | |
| 50 | const mbedtls_x509_crt_profile profile_sha512 = |
| 51 | { |
| 52 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ), |
| 53 | 0xFFFFFFF, /* Any PK alg */ |
| 54 | 0xFFFFFFF, /* Any curve */ |
| 55 | 1024, |
| 56 | }; |
| 57 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 58 | 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] | 59 | { |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 60 | ((void) data); |
| 61 | ((void) crt); |
| 62 | ((void) certificate_depth); |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 63 | *flags |= MBEDTLS_X509_BADCERT_OTHER; |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 64 | |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 65 | return 0; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 68 | 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] | 69 | { |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 70 | ((void) data); |
| 71 | ((void) crt); |
| 72 | ((void) certificate_depth); |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 73 | *flags = 0; |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 74 | |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
Manuel Pégourié-Gonnard | 9cca267 | 2017-05-23 12:26:58 +0200 | [diff] [blame] | 78 | int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags ) |
| 79 | { |
| 80 | int *levels = (int *) data; |
| 81 | |
| 82 | ((void) crt); |
| 83 | ((void) certificate_depth); |
| 84 | |
| 85 | /* Simulate a fatal error in the callback */ |
| 86 | if( *levels & ( 1 << certificate_depth ) ) |
| 87 | { |
| 88 | *flags |= ( 1 << certificate_depth ); |
Manuel Pégourié-Gonnard | b0ef3e2 | 2017-05-23 12:58:53 +0200 | [diff] [blame] | 89 | return( -1 - certificate_depth ); |
Manuel Pégourié-Gonnard | 9cca267 | 2017-05-23 12:26:58 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | return( 0 ); |
| 93 | } |
| 94 | |
Manuel Pégourié-Gonnard | a8838af | 2015-11-02 06:34:46 +0900 | [diff] [blame] | 95 | /* strsep() not available on Windows */ |
| 96 | char *mystrsep(char **stringp, const char *delim) |
| 97 | { |
| 98 | const char *p; |
| 99 | char *ret = *stringp; |
| 100 | |
| 101 | if( *stringp == NULL ) |
| 102 | return( NULL ); |
| 103 | |
| 104 | for( ; ; (*stringp)++ ) |
| 105 | { |
| 106 | if( **stringp == '\0' ) |
| 107 | { |
| 108 | *stringp = NULL; |
| 109 | goto done; |
| 110 | } |
| 111 | |
| 112 | for( p = delim; *p != '\0'; p++ ) |
| 113 | if( **stringp == *p ) |
| 114 | { |
| 115 | **stringp = '\0'; |
| 116 | (*stringp)++; |
| 117 | goto done; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | done: |
| 122 | return( ret ); |
| 123 | } |
| 124 | |
Manuel Pégourié-Gonnard | 560fea3 | 2015-09-01 11:59:24 +0200 | [diff] [blame] | 125 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 126 | typedef struct { |
| 127 | char buf[512]; |
| 128 | char *p; |
| 129 | } verify_print_context; |
| 130 | |
| 131 | void verify_print_init( verify_print_context *ctx ) |
| 132 | { |
| 133 | memset( ctx, 0, sizeof( verify_print_context ) ); |
| 134 | ctx->p = ctx->buf; |
| 135 | } |
| 136 | |
| 137 | int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags ) |
| 138 | { |
| 139 | int ret; |
| 140 | verify_print_context *ctx = (verify_print_context *) data; |
| 141 | char *p = ctx->p; |
| 142 | size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p; |
| 143 | ((void) flags); |
| 144 | |
| 145 | ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth ); |
| 146 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 147 | |
| 148 | ret = mbedtls_x509_serial_gets( p, n, &crt->serial ); |
| 149 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 150 | |
| 151 | ret = mbedtls_snprintf( p, n, " - subject " ); |
| 152 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 153 | |
| 154 | ret = mbedtls_x509_dn_gets( p, n, &crt->subject ); |
| 155 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 156 | |
Manuel Pégourié-Gonnard | 7c28b56 | 2017-08-21 11:00:22 +0200 | [diff] [blame] | 157 | ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags ); |
Manuel Pégourié-Gonnard | 560fea3 | 2015-09-01 11:59:24 +0200 | [diff] [blame] | 158 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 159 | |
| 160 | ctx->p = p; |
| 161 | |
| 162 | return( 0 ); |
| 163 | } |
| 164 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 165 | /* END_HEADER */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 166 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 167 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | * depends_on:MBEDTLS_BIGNUM_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 169 | * END_DEPENDENCIES |
| 170 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 171 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 173 | void x509_cert_info( char *crt_file, char *result_str ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 174 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | mbedtls_x509_crt crt; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 176 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 177 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 178 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 179 | mbedtls_x509_crt_init( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 180 | memset( buf, 0, 2000 ); |
| 181 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 183 | res = mbedtls_x509_crt_info( buf, 2000, "", &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 184 | |
| 185 | TEST_ASSERT( res != -1 ); |
| 186 | TEST_ASSERT( res != -2 ); |
| 187 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 188 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 189 | |
| 190 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 191 | mbedtls_x509_crt_free( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 192 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 193 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */ |
| 196 | void mbedtls_x509_crl_info( char *crl_file, char *result_str ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 197 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 198 | mbedtls_x509_crl crl; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 199 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 200 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | mbedtls_x509_crl_init( &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 203 | memset( buf, 0, 2000 ); |
| 204 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 205 | TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 ); |
| 206 | res = mbedtls_x509_crl_info( buf, 2000, "", &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 207 | |
| 208 | TEST_ASSERT( res != -1 ); |
| 209 | TEST_ASSERT( res != -2 ); |
| 210 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 211 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 212 | |
| 213 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 214 | mbedtls_x509_crl_free( &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 215 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 216 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 217 | |
Andres AG | a39db39 | 2016-12-08 17:10:38 +0000 | [diff] [blame] | 218 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */ |
| 219 | void mbedtls_x509_crl_parse( char *crl_file, int result ) |
| 220 | { |
| 221 | mbedtls_x509_crl crl; |
| 222 | char buf[2000]; |
| 223 | |
| 224 | mbedtls_x509_crl_init( &crl ); |
| 225 | memset( buf, 0, 2000 ); |
| 226 | |
| 227 | TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result ); |
| 228 | |
| 229 | exit: |
| 230 | mbedtls_x509_crl_free( &crl ); |
| 231 | } |
| 232 | /* END_CASE */ |
| 233 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */ |
| 235 | 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] | 236 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | mbedtls_x509_csr csr; |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 238 | char buf[2000]; |
| 239 | int res; |
| 240 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | mbedtls_x509_csr_init( &csr ); |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 242 | memset( buf, 0, 2000 ); |
| 243 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 ); |
| 245 | res = mbedtls_x509_csr_info( buf, 2000, "", &csr ); |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 246 | |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 247 | TEST_ASSERT( res != -1 ); |
| 248 | TEST_ASSERT( res != -2 ); |
| 249 | |
| 250 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 251 | |
| 252 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | mbedtls_x509_csr_free( &csr ); |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 254 | } |
| 255 | /* END_CASE */ |
| 256 | |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 257 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
| 258 | void x509_verify_info( int flags, char *prefix, char *result_str ) |
| 259 | { |
| 260 | char buf[2000]; |
| 261 | int res; |
| 262 | |
| 263 | memset( buf, 0, sizeof( buf ) ); |
| 264 | |
| 265 | res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags ); |
| 266 | |
| 267 | TEST_ASSERT( res >= 0 ); |
| 268 | |
| 269 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
| 270 | } |
| 271 | /* END_CASE */ |
| 272 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 273 | /* 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] | 274 | void x509_verify( char *crt_file, char *ca_file, char *crl_file, |
| 275 | char *cn_name_str, int result, int flags_result, |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 276 | char *profile_str, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 277 | char *verify_callback ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 278 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | mbedtls_x509_crt crt; |
| 280 | mbedtls_x509_crt ca; |
| 281 | mbedtls_x509_crl crl; |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 282 | uint32_t flags = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 283 | int res; |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 284 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 285 | char * cn_name = NULL; |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 286 | const mbedtls_x509_crt_profile *profile; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 287 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 288 | mbedtls_x509_crt_init( &crt ); |
| 289 | mbedtls_x509_crt_init( &ca ); |
| 290 | mbedtls_x509_crl_init( &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 291 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 292 | if( strcmp( cn_name_str, "NULL" ) != 0 ) |
| 293 | cn_name = cn_name_str; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 294 | |
Manuel Pégourié-Gonnard | aefd2dc | 2017-08-09 10:41:42 +0200 | [diff] [blame] | 295 | if( strcmp( profile_str, "" ) == 0 ) |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 296 | profile = &mbedtls_x509_crt_profile_default; |
Ron Eldor | c153998 | 2018-02-06 18:47:17 +0200 | [diff] [blame] | 297 | else if( strcmp( profile_str, "next" ) == 0 ) |
| 298 | profile = &mbedtls_x509_crt_profile_next; |
| 299 | else if( strcmp( profile_str, "suite_b" ) == 0 ) |
| 300 | profile = &mbedtls_x509_crt_profile_suiteb; |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 301 | else if( strcmp( profile_str, "compat" ) == 0 ) |
| 302 | profile = &compat_profile; |
Hanno Becker | 958c411 | 2019-06-03 14:27:03 +0100 | [diff] [blame] | 303 | else if( strcmp( profile_str, "all" ) == 0 ) |
| 304 | profile = &profile_all; |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 305 | else |
| 306 | TEST_ASSERT( "Unknown algorithm profile" == 0 ); |
| 307 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 308 | if( strcmp( verify_callback, "NULL" ) == 0 ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 309 | f_vrfy = NULL; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 310 | else if( strcmp( verify_callback, "verify_none" ) == 0 ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 311 | f_vrfy = verify_none; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 312 | else if( strcmp( verify_callback, "verify_all" ) == 0 ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 313 | f_vrfy = verify_all; |
| 314 | else |
| 315 | TEST_ASSERT( "No known verify callback selected" == 0 ); |
| 316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 318 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 ); |
| 319 | TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 320 | |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 321 | 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] | 322 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 323 | TEST_ASSERT( res == ( result ) ); |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 324 | TEST_ASSERT( flags == (uint32_t)( flags_result ) ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 325 | |
| 326 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 327 | mbedtls_x509_crt_free( &crt ); |
| 328 | mbedtls_x509_crt_free( &ca ); |
| 329 | mbedtls_x509_crl_free( &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 330 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 331 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 332 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 333 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 50fc51a | 2017-07-05 18:14:38 +0200 | [diff] [blame] | 334 | 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] | 335 | int exp_ret, char *exp_vrfy_out ) |
| 336 | { |
| 337 | int ret; |
| 338 | mbedtls_x509_crt crt; |
| 339 | mbedtls_x509_crt ca; |
| 340 | uint32_t flags = 0; |
| 341 | verify_print_context vrfy_ctx; |
| 342 | |
| 343 | mbedtls_x509_crt_init( &crt ); |
| 344 | mbedtls_x509_crt_init( &ca ); |
| 345 | verify_print_init( &vrfy_ctx ); |
| 346 | |
| 347 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 348 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 ); |
| 349 | |
Manuel Pégourié-Gonnard | 50fc51a | 2017-07-05 18:14:38 +0200 | [diff] [blame] | 350 | if( strcmp( name, "NULL" ) == 0 ) |
| 351 | name = NULL; |
| 352 | |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 353 | ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL, |
| 354 | &compat_profile, |
Manuel Pégourié-Gonnard | 50fc51a | 2017-07-05 18:14:38 +0200 | [diff] [blame] | 355 | name, &flags, |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 356 | verify_print, &vrfy_ctx ); |
Manuel Pégourié-Gonnard | 560fea3 | 2015-09-01 11:59:24 +0200 | [diff] [blame] | 357 | |
| 358 | TEST_ASSERT( ret == exp_ret ); |
| 359 | TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 ); |
| 360 | |
| 361 | exit: |
| 362 | mbedtls_x509_crt_free( &crt ); |
| 363 | mbedtls_x509_crt_free( &ca ); |
| 364 | } |
| 365 | /* END_CASE */ |
| 366 | |
| 367 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 368 | 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] | 369 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 370 | mbedtls_x509_crt crt; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 371 | char buf[2000]; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 372 | int res = 0; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 373 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 374 | mbedtls_x509_crt_init( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 375 | memset( buf, 0, 2000 ); |
| 376 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 377 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 378 | if( strcmp( entity, "subject" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 379 | res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 380 | else if( strcmp( entity, "issuer" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 381 | res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 382 | else |
| 383 | TEST_ASSERT( "Unknown entity" == 0 ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 384 | |
| 385 | TEST_ASSERT( res != -1 ); |
| 386 | TEST_ASSERT( res != -2 ); |
| 387 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 388 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 389 | |
| 390 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | mbedtls_x509_crt_free( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 392 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 393 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 394 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 395 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 396 | 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] | 397 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 398 | mbedtls_x509_crt crt; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 399 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 400 | mbedtls_x509_crt_init( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 401 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 402 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 403 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 404 | if( strcmp( entity, "valid_from" ) == 0 ) |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 405 | TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 406 | else if( strcmp( entity, "valid_to" ) == 0 ) |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 407 | TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 408 | else |
| 409 | TEST_ASSERT( "Unknown entity" == 0 ); |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 410 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 411 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 412 | mbedtls_x509_crt_free( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 413 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 414 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 415 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 417 | 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] | 418 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 419 | mbedtls_x509_crt crt; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 420 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | mbedtls_x509_crt_init( &crt ); |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 422 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 423 | 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] | 424 | |
| 425 | if( strcmp( entity, "valid_from" ) == 0 ) |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 426 | 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] | 427 | else if( strcmp( entity, "valid_to" ) == 0 ) |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 428 | 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] | 429 | else |
| 430 | TEST_ASSERT( "Unknown entity" == 0 ); |
| 431 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 432 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 433 | mbedtls_x509_crt_free( &crt ); |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 434 | } |
| 435 | /* END_CASE */ |
| 436 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 437 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */ |
Paul Bakker | 5a5fa92 | 2014-09-26 14:53:04 +0200 | [diff] [blame] | 438 | void x509parse_crt_file( char *crt_file, int result ) |
| 439 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 440 | mbedtls_x509_crt crt; |
Paul Bakker | 5a5fa92 | 2014-09-26 14:53:04 +0200 | [diff] [blame] | 441 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 442 | mbedtls_x509_crt_init( &crt ); |
Paul Bakker | 5a5fa92 | 2014-09-26 14:53:04 +0200 | [diff] [blame] | 443 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 444 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result ); |
Paul Bakker | 5a5fa92 | 2014-09-26 14:53:04 +0200 | [diff] [blame] | 445 | |
| 446 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 447 | mbedtls_x509_crt_free( &crt ); |
Paul Bakker | 5a5fa92 | 2014-09-26 14:53:04 +0200 | [diff] [blame] | 448 | } |
| 449 | /* END_CASE */ |
| 450 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 451 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 452 | void x509parse_crt( char *crt_data, char *result_str, int result ) |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 453 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 454 | mbedtls_x509_crt crt; |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 455 | unsigned char buf[2000]; |
| 456 | unsigned char output[2000]; |
| 457 | int data_len, res; |
| 458 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | mbedtls_x509_crt_init( &crt ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 460 | memset( buf, 0, 2000 ); |
| 461 | memset( output, 0, 2000 ); |
| 462 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 463 | data_len = unhexify( buf, crt_data ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 464 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 465 | TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf, data_len ) == ( result ) ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 466 | if( ( result ) == 0 ) |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 467 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 468 | res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 469 | |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 470 | TEST_ASSERT( res != -1 ); |
| 471 | TEST_ASSERT( res != -2 ); |
| 472 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 473 | TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 474 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 475 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 476 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 477 | mbedtls_x509_crt_free( &crt ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 478 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 479 | /* END_CASE */ |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 481 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 482 | void x509parse_crl( char *crl_data, char *result_str, int result ) |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 483 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 484 | mbedtls_x509_crl crl; |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 485 | unsigned char buf[2000]; |
| 486 | unsigned char output[2000]; |
| 487 | int data_len, res; |
| 488 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 489 | mbedtls_x509_crl_init( &crl ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 490 | memset( buf, 0, 2000 ); |
| 491 | memset( output, 0, 2000 ); |
| 492 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 493 | data_len = unhexify( buf, crl_data ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 494 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 495 | TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf, data_len ) == ( result ) ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 496 | if( ( result ) == 0 ) |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 497 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 498 | res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 499 | |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 500 | TEST_ASSERT( res != -1 ); |
| 501 | TEST_ASSERT( res != -2 ); |
| 502 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 503 | TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 504 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 505 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 506 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | mbedtls_x509_crl_free( &crl ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 508 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 509 | /* END_CASE */ |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 511 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */ |
| 512 | void mbedtls_x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret ) |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 513 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 514 | mbedtls_x509_csr csr; |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 515 | unsigned char *csr_der = NULL; |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 516 | char my_out[1000]; |
| 517 | size_t csr_der_len; |
| 518 | int my_ret; |
| 519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 520 | mbedtls_x509_csr_init( &csr ); |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 521 | memset( my_out, 0, sizeof( my_out ) ); |
| 522 | csr_der = unhexify_alloc( csr_der_hex, &csr_der_len ); |
| 523 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 524 | my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der, csr_der_len ); |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 525 | TEST_ASSERT( my_ret == ref_ret ); |
| 526 | |
| 527 | if( ref_ret == 0 ) |
| 528 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 529 | 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] | 530 | TEST_ASSERT( my_out_len == strlen( ref_out ) ); |
| 531 | TEST_ASSERT( strcmp( my_out, ref_out ) == 0 ); |
| 532 | } |
| 533 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 534 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 535 | mbedtls_x509_csr_free( &csr ); |
| 536 | mbedtls_free( csr_der ); |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 537 | } |
| 538 | /* END_CASE */ |
| 539 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 540 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| 541 | 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] | 542 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 543 | mbedtls_x509_crt chain, *cur; |
Manuel Pégourié-Gonnard | fbae2a1 | 2013-11-26 16:43:39 +0100 | [diff] [blame] | 544 | int i; |
| 545 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 546 | mbedtls_x509_crt_init( &chain ); |
Manuel Pégourié-Gonnard | fbae2a1 | 2013-11-26 16:43:39 +0100 | [diff] [blame] | 547 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 548 | 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] | 549 | |
| 550 | /* Check how many certs we got */ |
| 551 | for( i = 0, cur = &chain; cur != NULL; cur = cur->next ) |
| 552 | if( cur->raw.p != NULL ) |
| 553 | i++; |
| 554 | |
| 555 | TEST_ASSERT( i == nb_crt ); |
| 556 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 557 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 558 | mbedtls_x509_crt_free( &chain ); |
Manuel Pégourié-Gonnard | fbae2a1 | 2013-11-26 16:43:39 +0100 | [diff] [blame] | 559 | } |
| 560 | /* END_CASE */ |
| 561 | |
Janos Follath | 822b2c3 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 562 | /* 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] | 563 | void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int, |
| 564 | int ret_chk, int flags_chk ) |
| 565 | { |
| 566 | char file_buf[128]; |
| 567 | int ret; |
| 568 | uint32_t flags; |
| 569 | mbedtls_x509_crt trusted, chain; |
| 570 | |
| 571 | /* |
| 572 | * We expect chain_dir to contain certificates 00.crt, 01.crt, etc. |
| 573 | * with NN.crt signed by NN-1.crt |
| 574 | */ |
| 575 | |
| 576 | mbedtls_x509_crt_init( &trusted ); |
| 577 | mbedtls_x509_crt_init( &chain ); |
| 578 | |
| 579 | /* Load trusted root */ |
| 580 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 ); |
| 581 | |
| 582 | /* Load a chain with nb_int intermediates (from 01 to nb_int), |
| 583 | * plus one "end-entity" cert (nb_int + 1) */ |
| 584 | ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir, |
| 585 | nb_int + 1 ); |
| 586 | TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf ); |
| 587 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 ); |
| 588 | |
| 589 | /* Try to verify that chain */ |
| 590 | ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags, |
| 591 | NULL, NULL ); |
| 592 | TEST_ASSERT( ret == ret_chk ); |
| 593 | TEST_ASSERT( flags == (uint32_t) flags_chk ); |
| 594 | |
| 595 | exit: |
| 596 | mbedtls_x509_crt_free( &chain ); |
| 597 | mbedtls_x509_crt_free( &trusted ); |
| 598 | } |
| 599 | /* END_CASE */ |
| 600 | |
| 601 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 7e9709a | 2017-05-22 12:04:25 +0200 | [diff] [blame] | 602 | void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca, |
| 603 | int flags_result, int result, |
Manuel Pégourié-Gonnard | 9cca267 | 2017-05-23 12:26:58 +0200 | [diff] [blame] | 604 | char *profile_name, int vrfy_fatal_lvls ) |
Janos Follath | 822b2c3 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 605 | { |
| 606 | char* act; |
| 607 | uint32_t flags; |
Manuel Pégourié-Gonnard | 7e9709a | 2017-05-22 12:04:25 +0200 | [diff] [blame] | 608 | int res; |
Manuel Pégourié-Gonnard | e670f90 | 2015-10-30 09:23:19 +0100 | [diff] [blame] | 609 | mbedtls_x509_crt trusted, chain; |
Manuel Pégourié-Gonnard | 7e9709a | 2017-05-22 12:04:25 +0200 | [diff] [blame] | 610 | const mbedtls_x509_crt_profile *profile = NULL; |
Janos Follath | ef4f258 | 2015-10-11 16:17:27 +0200 | [diff] [blame] | 611 | |
Janos Follath | 822b2c3 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 612 | mbedtls_x509_crt_init( &chain ); |
| 613 | mbedtls_x509_crt_init( &trusted ); |
| 614 | |
Manuel Pégourié-Gonnard | a8838af | 2015-11-02 06:34:46 +0900 | [diff] [blame] | 615 | while( ( act = mystrsep( &chain_paths, " " ) ) != NULL ) |
Manuel Pégourié-Gonnard | e670f90 | 2015-10-30 09:23:19 +0100 | [diff] [blame] | 616 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 ); |
| 617 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 ); |
Janos Follath | 822b2c3 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 618 | |
Manuel Pégourié-Gonnard | 650780c | 2017-08-08 11:10:37 +0200 | [diff] [blame] | 619 | if( strcmp( profile_name, "" ) == 0 ) |
Manuel Pégourié-Gonnard | 7e9709a | 2017-05-22 12:04:25 +0200 | [diff] [blame] | 620 | profile = &mbedtls_x509_crt_profile_default; |
Manuel Pégourié-Gonnard | 650780c | 2017-08-08 11:10:37 +0200 | [diff] [blame] | 621 | else if( strcmp( profile_name, "next" ) == 0 ) |
Manuel Pégourié-Gonnard | 7e9709a | 2017-05-22 12:04:25 +0200 | [diff] [blame] | 622 | profile = &mbedtls_x509_crt_profile_next; |
Manuel Pégourié-Gonnard | 650780c | 2017-08-08 11:10:37 +0200 | [diff] [blame] | 623 | else if( strcmp( profile_name, "suiteb" ) == 0 ) |
Manuel Pégourié-Gonnard | 7e9709a | 2017-05-22 12:04:25 +0200 | [diff] [blame] | 624 | profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | 650780c | 2017-08-08 11:10:37 +0200 | [diff] [blame] | 625 | else if( strcmp( profile_name, "rsa3072" ) == 0 ) |
Manuel Pégourié-Gonnard | 189bb40 | 2017-05-23 11:29:29 +0200 | [diff] [blame] | 626 | profile = &profile_rsa3072; |
Manuel Pégourié-Gonnard | 650780c | 2017-08-08 11:10:37 +0200 | [diff] [blame] | 627 | else if( strcmp( profile_name, "sha512" ) == 0 ) |
Manuel Pégourié-Gonnard | 189bb40 | 2017-05-23 11:29:29 +0200 | [diff] [blame] | 628 | profile = &profile_sha512; |
Manuel Pégourié-Gonnard | 7e9709a | 2017-05-22 12:04:25 +0200 | [diff] [blame] | 629 | |
| 630 | res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile, |
Manuel Pégourié-Gonnard | 9cca267 | 2017-05-23 12:26:58 +0200 | [diff] [blame] | 631 | NULL, &flags, verify_fatal, &vrfy_fatal_lvls ); |
Janos Follath | ef4f258 | 2015-10-11 16:17:27 +0200 | [diff] [blame] | 632 | |
| 633 | TEST_ASSERT( res == ( result ) ); |
| 634 | TEST_ASSERT( flags == (uint32_t)( flags_result ) ); |
Janos Follath | 822b2c3 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 635 | |
| 636 | exit: |
| 637 | mbedtls_x509_crt_free( &trusted ); |
| 638 | mbedtls_x509_crt_free( &chain ); |
| 639 | } |
| 640 | /* END_CASE */ |
| 641 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 642 | /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */ |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 643 | void x509_oid_desc( char *oid_str, char *ref_desc ) |
| 644 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 645 | mbedtls_x509_buf oid; |
Manuel Pégourié-Gonnard | 48d3cef | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 646 | const char *desc = NULL; |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 647 | unsigned char buf[20]; |
Manuel Pégourié-Gonnard | 48d3cef | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 648 | int ret; |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 649 | |
| 650 | memset( buf, 0, sizeof buf ); |
| 651 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 652 | oid.tag = MBEDTLS_ASN1_OID; |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 653 | oid.len = unhexify( buf, oid_str ); |
| 654 | oid.p = buf; |
| 655 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 656 | ret = mbedtls_oid_get_extended_key_usage( &oid, &desc ); |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 657 | |
| 658 | if( strcmp( ref_desc, "notfound" ) == 0 ) |
Manuel Pégourié-Gonnard | 48d3cef | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 659 | { |
| 660 | TEST_ASSERT( ret != 0 ); |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 661 | TEST_ASSERT( desc == NULL ); |
Manuel Pégourié-Gonnard | 48d3cef | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 662 | } |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 663 | else |
| 664 | { |
Manuel Pégourié-Gonnard | 48d3cef | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 665 | TEST_ASSERT( ret == 0 ); |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 666 | TEST_ASSERT( desc != NULL ); |
| 667 | TEST_ASSERT( strcmp( desc, ref_desc ) == 0 ); |
| 668 | } |
| 669 | } |
| 670 | /* END_CASE */ |
| 671 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 672 | /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */ |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 673 | void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret ) |
| 674 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 675 | mbedtls_x509_buf oid; |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 676 | unsigned char oid_buf[20]; |
| 677 | char num_buf[100]; |
| 678 | |
| 679 | memset( oid_buf, 0x00, sizeof oid_buf ); |
| 680 | memset( num_buf, 0x2a, sizeof num_buf ); |
| 681 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 682 | oid.tag = MBEDTLS_ASN1_OID; |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 683 | oid.len = unhexify( oid_buf, oid_str ); |
| 684 | oid.p = oid_buf; |
| 685 | |
| 686 | TEST_ASSERT( (size_t) blen <= sizeof num_buf ); |
| 687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | 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] | 689 | |
| 690 | if( ret >= 0 ) |
| 691 | { |
| 692 | TEST_ASSERT( num_buf[ret] == 0 ); |
| 693 | TEST_ASSERT( strcmp( num_buf, numstr ) == 0 ); |
| 694 | } |
| 695 | } |
| 696 | /* END_CASE */ |
| 697 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 698 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 699 | void x509_check_key_usage( char *crt_file, int usage, int ret ) |
| 700 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 701 | mbedtls_x509_crt crt; |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 702 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 703 | mbedtls_x509_crt_init( &crt ); |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 704 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 705 | 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] | 706 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 707 | 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] | 708 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 709 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 710 | mbedtls_x509_crt_free( &crt ); |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 711 | } |
| 712 | /* END_CASE */ |
| 713 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 714 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 715 | void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret ) |
| 716 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 717 | mbedtls_x509_crt crt; |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 718 | char oid[50]; |
| 719 | size_t len; |
| 720 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 721 | mbedtls_x509_crt_init( &crt ); |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 722 | |
| 723 | len = unhexify( (unsigned char *) oid, usage_hex ); |
| 724 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 725 | 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] | 726 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 727 | TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, oid, len ) == ret ); |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 728 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 729 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 730 | mbedtls_x509_crt_free( &crt ); |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 731 | } |
| 732 | /* END_CASE */ |
| 733 | |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 734 | /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */ |
| 735 | void x509_get_time( int tag, char *time_str, int ret, |
| 736 | int year, int mon, int day, |
| 737 | int hour, int min, int sec ) |
| 738 | { |
| 739 | mbedtls_x509_time time; |
Janos Follath | ea7054a | 2017-02-08 14:13:02 +0000 | [diff] [blame] | 740 | unsigned char buf[21]; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 741 | unsigned char* start = buf; |
| 742 | unsigned char* end = buf; |
| 743 | |
| 744 | memset( &time, 0x00, sizeof( time ) ); |
| 745 | *end = (unsigned char)tag; end++; |
Janos Follath | ea7054a | 2017-02-08 14:13:02 +0000 | [diff] [blame] | 746 | *end = strlen( time_str ); |
| 747 | TEST_ASSERT( *end < 20 ); |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 748 | end++; |
| 749 | memcpy( end, time_str, (size_t)*(end - 1) ); |
| 750 | end += *(end - 1); |
| 751 | |
| 752 | TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret ); |
| 753 | if( ret == 0 ) |
| 754 | { |
| 755 | TEST_ASSERT( year == time.year ); |
| 756 | TEST_ASSERT( mon == time.mon ); |
| 757 | TEST_ASSERT( day == time.day ); |
| 758 | TEST_ASSERT( hour == time.hour ); |
| 759 | TEST_ASSERT( min == time.min ); |
| 760 | TEST_ASSERT( sec == time.sec ); |
| 761 | } |
| 762 | } |
| 763 | /* END_CASE */ |
| 764 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 765 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | 8540369 | 2014-06-06 14:48:38 +0200 | [diff] [blame] | 766 | void x509_parse_rsassa_pss_params( char *hex_params, int params_tag, |
| 767 | int ref_msg_md, int ref_mgf_md, |
| 768 | int ref_salt_len, int ref_ret ) |
| 769 | { |
| 770 | int my_ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 771 | mbedtls_x509_buf params; |
| 772 | mbedtls_md_type_t my_msg_md, my_mgf_md; |
Manuel Pégourié-Gonnard | 8540369 | 2014-06-06 14:48:38 +0200 | [diff] [blame] | 773 | int my_salt_len; |
| 774 | |
| 775 | params.p = unhexify_alloc( hex_params, ¶ms.len ); |
| 776 | params.tag = params_tag; |
| 777 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 778 | 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] | 779 | &my_salt_len ); |
| 780 | |
Manuel Pégourié-Gonnard | 8540369 | 2014-06-06 14:48:38 +0200 | [diff] [blame] | 781 | TEST_ASSERT( my_ret == ref_ret ); |
| 782 | |
| 783 | if( ref_ret == 0 ) |
| 784 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 785 | TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md ); |
| 786 | 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] | 787 | TEST_ASSERT( my_salt_len == ref_salt_len ); |
| 788 | } |
| 789 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 790 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 791 | mbedtls_free( params.p ); |
Manuel Pégourié-Gonnard | 8540369 | 2014-06-06 14:48:38 +0200 | [diff] [blame] | 792 | } |
| 793 | /* END_CASE */ |
| 794 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 795 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 796 | void x509_selftest() |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 797 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 798 | TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 799 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 800 | /* END_CASE */ |