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