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