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