Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Andres AG | 0da3e44 | 2016-09-23 13:16:02 +0100 | [diff] [blame^] | 2 | #include "polarssl/x509.h" |
Rich Evans | ce2f237 | 2015-02-06 13:57:42 +0000 | [diff] [blame] | 3 | #include "polarssl/x509_crt.h" |
| 4 | #include "polarssl/x509_crl.h" |
| 5 | #include "polarssl/x509_csr.h" |
| 6 | #include "polarssl/pem.h" |
| 7 | #include "polarssl/oid.h" |
| 8 | #include "polarssl/base64.h" |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 9 | |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 10 | int verify_none( void *data, x509_crt *crt, int certificate_depth, int *flags ) |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 11 | { |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 12 | ((void) data); |
| 13 | ((void) crt); |
| 14 | ((void) certificate_depth); |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 15 | *flags |= BADCERT_OTHER; |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 16 | |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 17 | return 0; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 18 | } |
| 19 | |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 20 | int verify_all( void *data, x509_crt *crt, int certificate_depth, int *flags ) |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 21 | { |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 22 | ((void) data); |
| 23 | ((void) crt); |
| 24 | ((void) certificate_depth); |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 25 | *flags = 0; |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 26 | |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 27 | return 0; |
| 28 | } |
| 29 | |
Manuel Pégourié-Gonnard | 15f1088 | 2015-09-01 11:59:24 +0200 | [diff] [blame] | 30 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
| 31 | typedef struct { |
| 32 | char buf[512]; |
| 33 | char *p; |
| 34 | } verify_print_context; |
| 35 | |
| 36 | void verify_print_init( verify_print_context *ctx ) |
| 37 | { |
| 38 | memset( ctx, 0, sizeof( verify_print_context ) ); |
| 39 | ctx->p = ctx->buf; |
| 40 | } |
| 41 | |
| 42 | #if defined(_MSC_VER) && !defined snprintf |
| 43 | #define snprintf _snprintf |
| 44 | #endif |
| 45 | |
| 46 | #define SAFE_SNPRINTF \ |
| 47 | do \ |
| 48 | { \ |
| 49 | if( ret < 0 || (size_t) ret > n ) \ |
| 50 | { \ |
| 51 | p[n - 1] = '\0'; \ |
| 52 | return( -1 ); \ |
| 53 | } \ |
| 54 | \ |
| 55 | n -= (unsigned int) ret; \ |
| 56 | p += (unsigned int) ret; \ |
| 57 | } while( 0 ) |
| 58 | |
| 59 | int verify_print( void *data, x509_crt *crt, int certificate_depth, int *flags ) |
| 60 | { |
| 61 | int ret; |
| 62 | verify_print_context *ctx = (verify_print_context *) data; |
| 63 | char *p = ctx->p; |
| 64 | size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p; |
| 65 | ((void) flags); |
| 66 | |
| 67 | ret = polarssl_snprintf( p, n, "depth %d - serial ", certificate_depth ); |
| 68 | SAFE_SNPRINTF; |
| 69 | |
| 70 | ret = x509_serial_gets( p, n, &crt->serial ); |
| 71 | SAFE_SNPRINTF; |
| 72 | |
| 73 | ret = polarssl_snprintf( p, n, " - subject " ); |
| 74 | SAFE_SNPRINTF; |
| 75 | |
| 76 | ret = x509_dn_gets( p, n, &crt->subject ); |
| 77 | SAFE_SNPRINTF; |
| 78 | |
| 79 | ret = polarssl_snprintf( p, n, "\n" ); |
| 80 | SAFE_SNPRINTF; |
| 81 | |
| 82 | ctx->p = p; |
| 83 | |
| 84 | return( 0 ); |
| 85 | } |
| 86 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 28e1ac5 | 2015-11-02 06:34:46 +0900 | [diff] [blame] | 87 | |
| 88 | /* strsep() not available on Windows */ |
| 89 | char *mystrsep(char **stringp, const char *delim) |
| 90 | { |
| 91 | const char *p; |
| 92 | char *ret = *stringp; |
| 93 | |
| 94 | if( *stringp == NULL ) |
| 95 | return( NULL ); |
| 96 | |
| 97 | for( ; ; (*stringp)++ ) |
| 98 | { |
| 99 | if( **stringp == '\0' ) |
| 100 | { |
| 101 | *stringp = NULL; |
| 102 | goto done; |
| 103 | } |
| 104 | |
| 105 | for( p = delim; *p != '\0'; p++ ) |
| 106 | if( **stringp == *p ) |
| 107 | { |
| 108 | **stringp = '\0'; |
| 109 | (*stringp)++; |
| 110 | goto done; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | done: |
| 115 | return( ret ); |
| 116 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 117 | /* END_HEADER */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 118 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 119 | /* BEGIN_DEPENDENCIES |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 120 | * depends_on:POLARSSL_BIGNUM_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 121 | * END_DEPENDENCIES |
| 122 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 123 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 124 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 125 | void x509_cert_info( char *crt_file, char *result_str ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 126 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 127 | x509_crt crt; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 128 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 129 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 130 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 131 | x509_crt_init( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 132 | memset( buf, 0, 2000 ); |
| 133 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 134 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 135 | res = x509_crt_info( buf, 2000, "", &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 136 | |
| 137 | TEST_ASSERT( res != -1 ); |
| 138 | TEST_ASSERT( res != -2 ); |
| 139 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 140 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 141 | |
| 142 | exit: |
| 143 | x509_crt_free( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 144 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 145 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 146 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 147 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRL_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 148 | void x509_crl_info( char *crl_file, char *result_str ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 149 | { |
| 150 | x509_crl crl; |
| 151 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 152 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 153 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 154 | x509_crl_init( &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 155 | memset( buf, 0, 2000 ); |
| 156 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 157 | TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 ); |
| 158 | res = x509_crl_info( buf, 2000, "", &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 159 | |
| 160 | TEST_ASSERT( res != -1 ); |
| 161 | TEST_ASSERT( res != -2 ); |
| 162 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 163 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 164 | |
| 165 | exit: |
| 166 | x509_crl_free( &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 167 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 168 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 169 | |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 170 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CSR_PARSE_C */ |
| 171 | void x509_csr_info( char *csr_file, char *result_str ) |
| 172 | { |
| 173 | x509_csr csr; |
| 174 | char buf[2000]; |
| 175 | int res; |
| 176 | |
| 177 | x509_csr_init( &csr ); |
| 178 | memset( buf, 0, 2000 ); |
| 179 | |
| 180 | TEST_ASSERT( x509_csr_parse_file( &csr, csr_file ) == 0 ); |
| 181 | res = x509_csr_info( buf, 2000, "", &csr ); |
| 182 | |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 183 | TEST_ASSERT( res != -1 ); |
| 184 | TEST_ASSERT( res != -2 ); |
| 185 | |
| 186 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 187 | |
| 188 | exit: |
| 189 | x509_csr_free( &csr ); |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 190 | } |
| 191 | /* END_CASE */ |
| 192 | |
Manuel Pégourié-Gonnard | 39a183a | 2015-04-17 16:14:32 +0200 | [diff] [blame] | 193 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */ |
| 194 | void x509_verify_info( int flags, char *prefix, char *result_str ) |
| 195 | { |
| 196 | char buf[2000]; |
| 197 | int res; |
| 198 | |
| 199 | memset( buf, 0, sizeof( buf ) ); |
| 200 | |
| 201 | res = x509_crt_verify_info( buf, sizeof( buf ), prefix, flags ); |
| 202 | |
| 203 | TEST_ASSERT( res >= 0 ); |
| 204 | |
| 205 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
| 206 | } |
| 207 | /* END_CASE */ |
| 208 | |
Manuel Pégourié-Gonnard | cbf3ef3 | 2013-09-23 12:20:02 +0200 | [diff] [blame] | 209 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_CRL_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 210 | void x509_verify( char *crt_file, char *ca_file, char *crl_file, |
| 211 | char *cn_name_str, int result, int flags_result, |
| 212 | char *verify_callback ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 213 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 214 | x509_crt crt; |
| 215 | x509_crt ca; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 216 | x509_crl crl; |
| 217 | int flags = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 218 | int res; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 219 | int (*f_vrfy)(void *, x509_crt *, int, int *) = NULL; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 220 | char * cn_name = NULL; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 221 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 222 | x509_crt_init( &crt ); |
| 223 | x509_crt_init( &ca ); |
| 224 | x509_crl_init( &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 225 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 226 | if( strcmp( cn_name_str, "NULL" ) != 0 ) |
| 227 | cn_name = cn_name_str; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 228 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 229 | if( strcmp( verify_callback, "NULL" ) == 0 ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 230 | f_vrfy = NULL; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 231 | else if( strcmp( verify_callback, "verify_none" ) == 0 ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 232 | f_vrfy = verify_none; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 233 | else if( strcmp( verify_callback, "verify_all" ) == 0 ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 234 | f_vrfy = verify_all; |
| 235 | else |
| 236 | TEST_ASSERT( "No known verify callback selected" == 0 ); |
| 237 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 238 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 239 | TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 ); |
| 240 | TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 241 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 242 | res = x509_crt_verify( &crt, &ca, &crl, cn_name, &flags, f_vrfy, NULL ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 243 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 244 | TEST_ASSERT( res == ( result ) ); |
| 245 | TEST_ASSERT( flags == ( flags_result ) ); |
| 246 | |
| 247 | exit: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 248 | x509_crt_free( &crt ); |
| 249 | x509_crt_free( &ca ); |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 250 | x509_crl_free( &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 251 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 252 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 253 | |
Manuel Pégourié-Gonnard | 15f1088 | 2015-09-01 11:59:24 +0200 | [diff] [blame] | 254 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
| 255 | void x509_verify_callback( char *crt_file, char *ca_file, |
| 256 | int exp_ret, char *exp_vrfy_out ) |
| 257 | { |
| 258 | int ret; |
| 259 | x509_crt crt; |
| 260 | x509_crt ca; |
| 261 | int flags = 0; |
| 262 | verify_print_context vrfy_ctx; |
| 263 | |
| 264 | x509_crt_init( &crt ); |
| 265 | x509_crt_init( &ca ); |
| 266 | verify_print_init( &vrfy_ctx ); |
| 267 | |
| 268 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 269 | TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 ); |
| 270 | |
| 271 | ret = x509_crt_verify( &crt, &ca, NULL, NULL, &flags, |
| 272 | verify_print, &vrfy_ctx ); |
| 273 | |
| 274 | TEST_ASSERT( ret == exp_ret ); |
| 275 | TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 ); |
| 276 | |
| 277 | exit: |
| 278 | x509_crt_free( &crt ); |
| 279 | x509_crt_free( &ca ); |
| 280 | } |
| 281 | /* END_CASE */ |
| 282 | |
Manuel Pégourié-Gonnard | 8f63e95 | 2015-09-01 18:44:47 +0200 | [diff] [blame] | 283 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 284 | void x509_dn_gets( char *crt_file, char *entity, char *result_str ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 285 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 286 | x509_crt crt; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 287 | char buf[2000]; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 288 | int res = 0; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 289 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 290 | x509_crt_init( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 291 | memset( buf, 0, 2000 ); |
| 292 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 293 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 294 | if( strcmp( entity, "subject" ) == 0 ) |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 295 | res = x509_dn_gets( buf, 2000, &crt.subject ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 296 | else if( strcmp( entity, "issuer" ) == 0 ) |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 297 | res = x509_dn_gets( buf, 2000, &crt.issuer ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 298 | else |
| 299 | TEST_ASSERT( "Unknown entity" == 0 ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 300 | |
| 301 | TEST_ASSERT( res != -1 ); |
| 302 | TEST_ASSERT( res != -2 ); |
| 303 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 304 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 305 | |
| 306 | exit: |
| 307 | x509_crt_free( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 308 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 309 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 310 | |
Manuel Pégourié-Gonnard | 8f63e95 | 2015-09-01 18:44:47 +0200 | [diff] [blame] | 311 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 312 | void x509_time_expired( char *crt_file, char *entity, int result ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 313 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 314 | x509_crt crt; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 315 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 316 | x509_crt_init( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 317 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 318 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 319 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 320 | if( strcmp( entity, "valid_from" ) == 0 ) |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 321 | TEST_ASSERT( x509_time_expired( &crt.valid_from ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 322 | else if( strcmp( entity, "valid_to" ) == 0 ) |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 323 | TEST_ASSERT( x509_time_expired( &crt.valid_to ) == result ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 324 | else |
| 325 | TEST_ASSERT( "Unknown entity" == 0 ); |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 326 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 327 | exit: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 328 | x509_crt_free( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 329 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 330 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 331 | |
Manuel Pégourié-Gonnard | 8f63e95 | 2015-09-01 18:44:47 +0200 | [diff] [blame] | 332 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 333 | void x509_time_future( char *crt_file, char *entity, int result ) |
| 334 | { |
| 335 | x509_crt crt; |
| 336 | |
| 337 | x509_crt_init( &crt ); |
| 338 | |
| 339 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 340 | |
| 341 | if( strcmp( entity, "valid_from" ) == 0 ) |
| 342 | TEST_ASSERT( x509_time_future( &crt.valid_from ) == result ); |
| 343 | else if( strcmp( entity, "valid_to" ) == 0 ) |
| 344 | TEST_ASSERT( x509_time_future( &crt.valid_to ) == result ); |
| 345 | else |
| 346 | TEST_ASSERT( "Unknown entity" == 0 ); |
| 347 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 348 | exit: |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 349 | x509_crt_free( &crt ); |
| 350 | } |
| 351 | /* END_CASE */ |
| 352 | |
Paul Bakker | 5a5fa92 | 2014-09-26 14:53:04 +0200 | [diff] [blame] | 353 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_FS_IO */ |
| 354 | void x509parse_crt_file( char *crt_file, int result ) |
| 355 | { |
| 356 | x509_crt crt; |
| 357 | |
| 358 | x509_crt_init( &crt ); |
| 359 | |
| 360 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == result ); |
| 361 | |
| 362 | exit: |
| 363 | x509_crt_free( &crt ); |
| 364 | } |
| 365 | /* END_CASE */ |
| 366 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 367 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 368 | void x509parse_crt( char *crt_data, char *result_str, int result ) |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 369 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 370 | x509_crt crt; |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 371 | unsigned char buf[2000]; |
| 372 | unsigned char output[2000]; |
| 373 | int data_len, res; |
| 374 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 375 | x509_crt_init( &crt ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 376 | memset( buf, 0, 2000 ); |
| 377 | memset( output, 0, 2000 ); |
| 378 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 379 | data_len = unhexify( buf, crt_data ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 380 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 381 | TEST_ASSERT( x509_crt_parse( &crt, buf, data_len ) == ( result ) ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 382 | if( ( result ) == 0 ) |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 383 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 384 | res = x509_crt_info( (char *) output, 2000, "", &crt ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 385 | |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 386 | TEST_ASSERT( res != -1 ); |
| 387 | TEST_ASSERT( res != -2 ); |
| 388 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 389 | TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 390 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 391 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 392 | exit: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 393 | x509_crt_free( &crt ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 394 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 395 | /* END_CASE */ |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 396 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 397 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRL_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 398 | void x509parse_crl( char *crl_data, char *result_str, int result ) |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 399 | { |
| 400 | x509_crl crl; |
| 401 | unsigned char buf[2000]; |
| 402 | unsigned char output[2000]; |
| 403 | int data_len, res; |
| 404 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 405 | x509_crl_init( &crl ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 406 | memset( buf, 0, 2000 ); |
| 407 | memset( output, 0, 2000 ); |
| 408 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 409 | data_len = unhexify( buf, crl_data ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 410 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 411 | TEST_ASSERT( x509_crl_parse( &crl, buf, data_len ) == ( result ) ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 412 | if( ( result ) == 0 ) |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 413 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 414 | res = x509_crl_info( (char *) output, 2000, "", &crl ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 415 | |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 416 | TEST_ASSERT( res != -1 ); |
| 417 | TEST_ASSERT( res != -2 ); |
| 418 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 419 | TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 420 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 421 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 422 | exit: |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 423 | x509_crl_free( &crl ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 424 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 425 | /* END_CASE */ |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 426 | |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 427 | /* BEGIN_CASE depends_on:POLARSSL_X509_CSR_PARSE_C */ |
| 428 | void x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret ) |
| 429 | { |
| 430 | x509_csr csr; |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 431 | unsigned char *csr_der = NULL; |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 432 | char my_out[1000]; |
| 433 | size_t csr_der_len; |
| 434 | int my_ret; |
| 435 | |
| 436 | x509_csr_init( &csr ); |
| 437 | memset( my_out, 0, sizeof( my_out ) ); |
| 438 | csr_der = unhexify_alloc( csr_der_hex, &csr_der_len ); |
| 439 | |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 440 | my_ret = x509_csr_parse_der( &csr, csr_der, csr_der_len ); |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 441 | TEST_ASSERT( my_ret == ref_ret ); |
| 442 | |
| 443 | if( ref_ret == 0 ) |
| 444 | { |
| 445 | size_t my_out_len = x509_csr_info( my_out, sizeof( my_out ), "", &csr ); |
| 446 | TEST_ASSERT( my_out_len == strlen( ref_out ) ); |
| 447 | TEST_ASSERT( strcmp( my_out, ref_out ) == 0 ); |
| 448 | } |
| 449 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 450 | exit: |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 451 | x509_csr_free( &csr ); |
| 452 | polarssl_free( csr_der ); |
| 453 | } |
| 454 | /* END_CASE */ |
| 455 | |
Manuel Pégourié-Gonnard | fbae2a1 | 2013-11-26 16:43:39 +0100 | [diff] [blame] | 456 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
| 457 | void x509_crt_parse_path( char *crt_path, int ret, int nb_crt ) |
| 458 | { |
| 459 | x509_crt chain, *cur; |
| 460 | int i; |
| 461 | |
| 462 | x509_crt_init( &chain ); |
| 463 | |
| 464 | TEST_ASSERT( x509_crt_parse_path( &chain, crt_path ) == ret ); |
| 465 | |
| 466 | /* Check how many certs we got */ |
| 467 | for( i = 0, cur = &chain; cur != NULL; cur = cur->next ) |
| 468 | if( cur->raw.p != NULL ) |
| 469 | i++; |
| 470 | |
| 471 | TEST_ASSERT( i == nb_crt ); |
| 472 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 473 | exit: |
Paul Bakker | a2ffccd | 2013-12-02 21:56:37 +0100 | [diff] [blame] | 474 | x509_crt_free( &chain ); |
Manuel Pégourié-Gonnard | fbae2a1 | 2013-11-26 16:43:39 +0100 | [diff] [blame] | 475 | } |
| 476 | /* END_CASE */ |
| 477 | |
Janos Follath | 189c743 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 478 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
Janos Follath | 3d98a7e | 2015-10-11 16:17:27 +0200 | [diff] [blame] | 479 | void x509_crt_verify_chain( char *chain_paths, char *trusted_ca, int flags_result ) |
Janos Follath | 189c743 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 480 | { |
| 481 | char* act; |
| 482 | int flags; |
Janos Follath | 3d98a7e | 2015-10-11 16:17:27 +0200 | [diff] [blame] | 483 | int result, res; |
Janos Follath | 189c743 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 484 | x509_crt trusted, chain; |
| 485 | |
Janos Follath | 3d98a7e | 2015-10-11 16:17:27 +0200 | [diff] [blame] | 486 | result = flags_result ? POLARSSL_ERR_X509_CERT_VERIFY_FAILED : 0; |
| 487 | |
Janos Follath | 189c743 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 488 | x509_crt_init( &chain ); |
| 489 | x509_crt_init( &trusted ); |
| 490 | |
Manuel Pégourié-Gonnard | 28e1ac5 | 2015-11-02 06:34:46 +0900 | [diff] [blame] | 491 | while( ( act = mystrsep( &chain_paths, " " ) ) != NULL ) |
Janos Follath | 189c743 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 492 | TEST_ASSERT( x509_crt_parse_file( &chain, act ) == 0 ); |
| 493 | TEST_ASSERT( x509_crt_parse_file( &trusted, trusted_ca ) == 0 ); |
| 494 | |
| 495 | res = x509_crt_verify( &chain, &trusted, NULL, NULL, &flags, NULL, NULL ); |
| 496 | |
Janos Follath | 3d98a7e | 2015-10-11 16:17:27 +0200 | [diff] [blame] | 497 | TEST_ASSERT( res == result ); |
| 498 | TEST_ASSERT( flags == flags_result ); |
Janos Follath | 189c743 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 499 | |
| 500 | exit: |
| 501 | x509_crt_free( &trusted ); |
| 502 | x509_crt_free( &chain ); |
| 503 | } |
| 504 | /* END_CASE */ |
| 505 | |
Manuel Pégourié-Gonnard | 0f7b619 | 2014-06-24 11:37:54 +0200 | [diff] [blame] | 506 | /* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */ |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 507 | void x509_oid_desc( char *oid_str, char *ref_desc ) |
| 508 | { |
| 509 | x509_buf oid; |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 510 | const char *desc = NULL; |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 511 | unsigned char buf[20]; |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 512 | int ret; |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 513 | |
| 514 | memset( buf, 0, sizeof buf ); |
| 515 | |
| 516 | oid.tag = ASN1_OID; |
| 517 | oid.len = unhexify( buf, oid_str ); |
| 518 | oid.p = buf; |
| 519 | |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 520 | ret = oid_get_extended_key_usage( &oid, &desc ); |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 521 | |
| 522 | if( strcmp( ref_desc, "notfound" ) == 0 ) |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 523 | { |
| 524 | TEST_ASSERT( ret != 0 ); |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 525 | TEST_ASSERT( desc == NULL ); |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 526 | } |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 527 | else |
| 528 | { |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 529 | TEST_ASSERT( ret == 0 ); |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 530 | TEST_ASSERT( desc != NULL ); |
| 531 | TEST_ASSERT( strcmp( desc, ref_desc ) == 0 ); |
| 532 | } |
| 533 | } |
| 534 | /* END_CASE */ |
| 535 | |
Manuel Pégourié-Gonnard | 0f7b619 | 2014-06-24 11:37:54 +0200 | [diff] [blame] | 536 | /* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */ |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 537 | void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret ) |
| 538 | { |
| 539 | x509_buf oid; |
| 540 | unsigned char oid_buf[20]; |
| 541 | char num_buf[100]; |
| 542 | |
| 543 | memset( oid_buf, 0x00, sizeof oid_buf ); |
| 544 | memset( num_buf, 0x2a, sizeof num_buf ); |
| 545 | |
| 546 | oid.tag = ASN1_OID; |
| 547 | oid.len = unhexify( oid_buf, oid_str ); |
| 548 | oid.p = oid_buf; |
| 549 | |
| 550 | TEST_ASSERT( (size_t) blen <= sizeof num_buf ); |
| 551 | |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 552 | TEST_ASSERT( oid_get_numeric_string( num_buf, blen, &oid ) == ret ); |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 553 | |
| 554 | if( ret >= 0 ) |
| 555 | { |
| 556 | TEST_ASSERT( num_buf[ret] == 0 ); |
| 557 | TEST_ASSERT( strcmp( num_buf, numstr ) == 0 ); |
| 558 | } |
| 559 | } |
| 560 | /* END_CASE */ |
| 561 | |
Paul Bakker | 5b11d02 | 2014-07-10 13:54:38 +0200 | [diff] [blame] | 562 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 563 | void x509_check_key_usage( char *crt_file, int usage, int ret ) |
| 564 | { |
| 565 | x509_crt crt; |
| 566 | |
| 567 | x509_crt_init( &crt ); |
| 568 | |
| 569 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 570 | |
| 571 | TEST_ASSERT( x509_crt_check_key_usage( &crt, usage ) == ret ); |
| 572 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 573 | exit: |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 574 | x509_crt_free( &crt ); |
| 575 | } |
| 576 | /* END_CASE */ |
| 577 | |
Paul Bakker | 5b11d02 | 2014-07-10 13:54:38 +0200 | [diff] [blame] | 578 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 579 | void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret ) |
| 580 | { |
| 581 | x509_crt crt; |
| 582 | char oid[50]; |
| 583 | size_t len; |
| 584 | |
| 585 | x509_crt_init( &crt ); |
| 586 | |
| 587 | len = unhexify( (unsigned char *) oid, usage_hex ); |
| 588 | |
| 589 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 590 | |
| 591 | TEST_ASSERT( x509_crt_check_extended_key_usage( &crt, oid, len ) == ret ); |
| 592 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 593 | exit: |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 594 | x509_crt_free( &crt ); |
| 595 | } |
| 596 | /* END_CASE */ |
| 597 | |
Andres AG | 0da3e44 | 2016-09-23 13:16:02 +0100 | [diff] [blame^] | 598 | /* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */ |
| 599 | void x509_get_time( int tag, char *time_str, int ret, |
| 600 | int year, int mon, int day, |
| 601 | int hour, int min, int sec ) |
| 602 | { |
| 603 | x509_time time; |
| 604 | unsigned char buf[17]; |
| 605 | unsigned char* start = buf; |
| 606 | unsigned char* end = buf; |
| 607 | |
| 608 | memset( &time, 0x00, sizeof( time ) ); |
| 609 | *end = (unsigned char)tag; end++; |
| 610 | if( tag == ASN1_UTC_TIME ) |
| 611 | *end = 13; |
| 612 | else |
| 613 | *end = 15; |
| 614 | end++; |
| 615 | memcpy( end, time_str, (size_t)*(end - 1) ); |
| 616 | end += *(end - 1); |
| 617 | |
| 618 | TEST_ASSERT( x509_get_time( &start, end, &time ) == ret ); |
| 619 | if( ret == 0 ) |
| 620 | { |
| 621 | TEST_ASSERT( year == time.year ); |
| 622 | TEST_ASSERT( mon == time.mon ); |
| 623 | TEST_ASSERT( day == time.day ); |
| 624 | TEST_ASSERT( hour == time.hour ); |
| 625 | TEST_ASSERT( min == time.min ); |
| 626 | TEST_ASSERT( sec == time.sec ); |
| 627 | } |
| 628 | } |
| 629 | /* END_CASE */ |
| 630 | |
Manuel Pégourié-Gonnard | d1539b1 | 2014-06-06 16:42:37 +0200 | [diff] [blame] | 631 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | 8540369 | 2014-06-06 14:48:38 +0200 | [diff] [blame] | 632 | void x509_parse_rsassa_pss_params( char *hex_params, int params_tag, |
| 633 | int ref_msg_md, int ref_mgf_md, |
| 634 | int ref_salt_len, int ref_ret ) |
| 635 | { |
| 636 | int my_ret; |
| 637 | x509_buf params; |
| 638 | md_type_t my_msg_md, my_mgf_md; |
| 639 | int my_salt_len; |
| 640 | |
| 641 | params.p = unhexify_alloc( hex_params, ¶ms.len ); |
| 642 | params.tag = params_tag; |
| 643 | |
| 644 | my_ret = x509_get_rsassa_pss_params( ¶ms, &my_msg_md, &my_mgf_md, |
| 645 | &my_salt_len ); |
| 646 | |
Manuel Pégourié-Gonnard | 8540369 | 2014-06-06 14:48:38 +0200 | [diff] [blame] | 647 | TEST_ASSERT( my_ret == ref_ret ); |
| 648 | |
| 649 | if( ref_ret == 0 ) |
| 650 | { |
| 651 | TEST_ASSERT( my_msg_md == (md_type_t) ref_msg_md ); |
| 652 | TEST_ASSERT( my_mgf_md == (md_type_t) ref_mgf_md ); |
| 653 | TEST_ASSERT( my_salt_len == ref_salt_len ); |
| 654 | } |
| 655 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 656 | exit: |
Manuel Pégourié-Gonnard | 8540369 | 2014-06-06 14:48:38 +0200 | [diff] [blame] | 657 | polarssl_free( params.p ); |
| 658 | } |
| 659 | /* END_CASE */ |
| 660 | |
Manuel Pégourié-Gonnard | 2014016 | 2013-10-10 12:48:03 +0200 | [diff] [blame] | 661 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_SELF_TEST */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 662 | void x509_selftest() |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 663 | { |
| 664 | TEST_ASSERT( x509_self_test( 0 ) == 0 ); |
| 665 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 666 | /* END_CASE */ |