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 | |
Andres AG | 67c6df4 | 2016-12-08 17:10:38 +0000 | [diff] [blame] | 170 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRL_PARSE_C */ |
| 171 | void x509_crl_parse( char *crl_file, int result ) |
| 172 | { |
| 173 | x509_crl crl; |
| 174 | char buf[2000]; |
| 175 | |
| 176 | x509_crl_init( &crl ); |
| 177 | memset( buf, 0, 2000 ); |
| 178 | |
| 179 | TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == result ); |
| 180 | |
| 181 | exit: |
| 182 | x509_crl_free( &crl ); |
| 183 | } |
| 184 | /* END_CASE */ |
| 185 | |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 186 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CSR_PARSE_C */ |
| 187 | void x509_csr_info( char *csr_file, char *result_str ) |
| 188 | { |
| 189 | x509_csr csr; |
| 190 | char buf[2000]; |
| 191 | int res; |
| 192 | |
| 193 | x509_csr_init( &csr ); |
| 194 | memset( buf, 0, 2000 ); |
| 195 | |
| 196 | TEST_ASSERT( x509_csr_parse_file( &csr, csr_file ) == 0 ); |
| 197 | res = x509_csr_info( buf, 2000, "", &csr ); |
| 198 | |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 199 | TEST_ASSERT( res != -1 ); |
| 200 | TEST_ASSERT( res != -2 ); |
| 201 | |
| 202 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 203 | |
| 204 | exit: |
| 205 | x509_csr_free( &csr ); |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 206 | } |
| 207 | /* END_CASE */ |
| 208 | |
Manuel Pégourié-Gonnard | 39a183a | 2015-04-17 16:14:32 +0200 | [diff] [blame] | 209 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */ |
| 210 | void x509_verify_info( int flags, char *prefix, char *result_str ) |
| 211 | { |
| 212 | char buf[2000]; |
| 213 | int res; |
| 214 | |
| 215 | memset( buf, 0, sizeof( buf ) ); |
| 216 | |
| 217 | res = x509_crt_verify_info( buf, sizeof( buf ), prefix, flags ); |
| 218 | |
| 219 | TEST_ASSERT( res >= 0 ); |
| 220 | |
| 221 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
| 222 | } |
| 223 | /* END_CASE */ |
| 224 | |
Manuel Pégourié-Gonnard | cbf3ef3 | 2013-09-23 12:20:02 +0200 | [diff] [blame] | 225 | /* 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] | 226 | void x509_verify( char *crt_file, char *ca_file, char *crl_file, |
| 227 | char *cn_name_str, int result, int flags_result, |
| 228 | char *verify_callback ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 229 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 230 | x509_crt crt; |
| 231 | x509_crt ca; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 232 | x509_crl crl; |
| 233 | int flags = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 234 | int res; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 235 | int (*f_vrfy)(void *, x509_crt *, int, int *) = NULL; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 236 | char * cn_name = NULL; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 237 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 238 | x509_crt_init( &crt ); |
| 239 | x509_crt_init( &ca ); |
| 240 | x509_crl_init( &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 | if( strcmp( cn_name_str, "NULL" ) != 0 ) |
| 243 | cn_name = cn_name_str; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 244 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 245 | if( strcmp( verify_callback, "NULL" ) == 0 ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 246 | f_vrfy = NULL; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 247 | else if( strcmp( verify_callback, "verify_none" ) == 0 ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 248 | f_vrfy = verify_none; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 249 | else if( strcmp( verify_callback, "verify_all" ) == 0 ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 250 | f_vrfy = verify_all; |
| 251 | else |
| 252 | TEST_ASSERT( "No known verify callback selected" == 0 ); |
| 253 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 254 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 255 | TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 ); |
| 256 | TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 257 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 258 | 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] | 259 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 260 | TEST_ASSERT( res == ( result ) ); |
| 261 | TEST_ASSERT( flags == ( flags_result ) ); |
| 262 | |
| 263 | exit: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 264 | x509_crt_free( &crt ); |
| 265 | x509_crt_free( &ca ); |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 266 | x509_crl_free( &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 267 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 268 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 269 | |
Manuel Pégourié-Gonnard | 15f1088 | 2015-09-01 11:59:24 +0200 | [diff] [blame] | 270 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
| 271 | void x509_verify_callback( char *crt_file, char *ca_file, |
| 272 | int exp_ret, char *exp_vrfy_out ) |
| 273 | { |
| 274 | int ret; |
| 275 | x509_crt crt; |
| 276 | x509_crt ca; |
| 277 | int flags = 0; |
| 278 | verify_print_context vrfy_ctx; |
| 279 | |
| 280 | x509_crt_init( &crt ); |
| 281 | x509_crt_init( &ca ); |
| 282 | verify_print_init( &vrfy_ctx ); |
| 283 | |
| 284 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 285 | TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 ); |
| 286 | |
| 287 | ret = x509_crt_verify( &crt, &ca, NULL, NULL, &flags, |
| 288 | verify_print, &vrfy_ctx ); |
| 289 | |
| 290 | TEST_ASSERT( ret == exp_ret ); |
| 291 | TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 ); |
| 292 | |
| 293 | exit: |
| 294 | x509_crt_free( &crt ); |
| 295 | x509_crt_free( &ca ); |
| 296 | } |
| 297 | /* END_CASE */ |
| 298 | |
Manuel Pégourié-Gonnard | 8f63e95 | 2015-09-01 18:44:47 +0200 | [diff] [blame] | 299 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 300 | void x509_dn_gets( char *crt_file, char *entity, char *result_str ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 301 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 302 | x509_crt crt; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 303 | char buf[2000]; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 304 | int res = 0; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 305 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 306 | x509_crt_init( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 307 | memset( buf, 0, 2000 ); |
| 308 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 309 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 310 | if( strcmp( entity, "subject" ) == 0 ) |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 311 | res = x509_dn_gets( buf, 2000, &crt.subject ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 312 | else if( strcmp( entity, "issuer" ) == 0 ) |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 313 | res = x509_dn_gets( buf, 2000, &crt.issuer ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 314 | else |
| 315 | TEST_ASSERT( "Unknown entity" == 0 ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 316 | |
| 317 | TEST_ASSERT( res != -1 ); |
| 318 | TEST_ASSERT( res != -2 ); |
| 319 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 320 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 321 | |
| 322 | exit: |
| 323 | x509_crt_free( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 324 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 325 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 326 | |
Manuel Pégourié-Gonnard | 8f63e95 | 2015-09-01 18:44:47 +0200 | [diff] [blame] | 327 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 328 | void x509_time_expired( char *crt_file, char *entity, int result ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 329 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 330 | x509_crt crt; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 331 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 332 | x509_crt_init( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 333 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 334 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 335 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 336 | if( strcmp( entity, "valid_from" ) == 0 ) |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 337 | TEST_ASSERT( x509_time_expired( &crt.valid_from ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 338 | else if( strcmp( entity, "valid_to" ) == 0 ) |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 339 | TEST_ASSERT( x509_time_expired( &crt.valid_to ) == result ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 340 | else |
| 341 | TEST_ASSERT( "Unknown entity" == 0 ); |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 342 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 343 | exit: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 344 | x509_crt_free( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 345 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 346 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 347 | |
Manuel Pégourié-Gonnard | 8f63e95 | 2015-09-01 18:44:47 +0200 | [diff] [blame] | 348 | /* 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] | 349 | void x509_time_future( char *crt_file, char *entity, int result ) |
| 350 | { |
| 351 | x509_crt crt; |
| 352 | |
| 353 | x509_crt_init( &crt ); |
| 354 | |
| 355 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 356 | |
| 357 | if( strcmp( entity, "valid_from" ) == 0 ) |
| 358 | TEST_ASSERT( x509_time_future( &crt.valid_from ) == result ); |
| 359 | else if( strcmp( entity, "valid_to" ) == 0 ) |
| 360 | TEST_ASSERT( x509_time_future( &crt.valid_to ) == result ); |
| 361 | else |
| 362 | TEST_ASSERT( "Unknown entity" == 0 ); |
| 363 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 364 | exit: |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 365 | x509_crt_free( &crt ); |
| 366 | } |
| 367 | /* END_CASE */ |
| 368 | |
Paul Bakker | 5a5fa92 | 2014-09-26 14:53:04 +0200 | [diff] [blame] | 369 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_FS_IO */ |
| 370 | void x509parse_crt_file( char *crt_file, int result ) |
| 371 | { |
| 372 | x509_crt crt; |
| 373 | |
| 374 | x509_crt_init( &crt ); |
| 375 | |
| 376 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == result ); |
| 377 | |
| 378 | exit: |
| 379 | x509_crt_free( &crt ); |
| 380 | } |
| 381 | /* END_CASE */ |
| 382 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 383 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 384 | void x509parse_crt( char *crt_data, char *result_str, int result ) |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 385 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 386 | x509_crt crt; |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 387 | unsigned char buf[2000]; |
| 388 | unsigned char output[2000]; |
| 389 | int data_len, res; |
| 390 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 391 | x509_crt_init( &crt ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 392 | memset( buf, 0, 2000 ); |
| 393 | memset( output, 0, 2000 ); |
| 394 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 395 | data_len = unhexify( buf, crt_data ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 396 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 397 | TEST_ASSERT( x509_crt_parse( &crt, buf, data_len ) == ( result ) ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 398 | if( ( result ) == 0 ) |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 399 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 400 | res = x509_crt_info( (char *) output, 2000, "", &crt ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 401 | |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 402 | TEST_ASSERT( res != -1 ); |
| 403 | TEST_ASSERT( res != -2 ); |
| 404 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 405 | TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 406 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 407 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 408 | exit: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 409 | x509_crt_free( &crt ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 410 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 411 | /* END_CASE */ |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 412 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 413 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRL_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 414 | void x509parse_crl( char *crl_data, char *result_str, int result ) |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 415 | { |
| 416 | x509_crl crl; |
| 417 | unsigned char buf[2000]; |
| 418 | unsigned char output[2000]; |
| 419 | int data_len, res; |
| 420 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 421 | x509_crl_init( &crl ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 422 | memset( buf, 0, 2000 ); |
| 423 | memset( output, 0, 2000 ); |
| 424 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 425 | data_len = unhexify( buf, crl_data ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 426 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 427 | TEST_ASSERT( x509_crl_parse( &crl, buf, data_len ) == ( result ) ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 428 | if( ( result ) == 0 ) |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 429 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 430 | res = x509_crl_info( (char *) output, 2000, "", &crl ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 431 | |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 432 | TEST_ASSERT( res != -1 ); |
| 433 | TEST_ASSERT( res != -2 ); |
| 434 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 435 | TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 436 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 437 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 438 | exit: |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 439 | x509_crl_free( &crl ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 440 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 441 | /* END_CASE */ |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 442 | |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 443 | /* BEGIN_CASE depends_on:POLARSSL_X509_CSR_PARSE_C */ |
| 444 | void x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret ) |
| 445 | { |
| 446 | x509_csr csr; |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 447 | unsigned char *csr_der = NULL; |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 448 | char my_out[1000]; |
| 449 | size_t csr_der_len; |
| 450 | int my_ret; |
| 451 | |
| 452 | x509_csr_init( &csr ); |
| 453 | memset( my_out, 0, sizeof( my_out ) ); |
| 454 | csr_der = unhexify_alloc( csr_der_hex, &csr_der_len ); |
| 455 | |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 456 | 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] | 457 | TEST_ASSERT( my_ret == ref_ret ); |
| 458 | |
| 459 | if( ref_ret == 0 ) |
| 460 | { |
| 461 | size_t my_out_len = x509_csr_info( my_out, sizeof( my_out ), "", &csr ); |
| 462 | TEST_ASSERT( my_out_len == strlen( ref_out ) ); |
| 463 | TEST_ASSERT( strcmp( my_out, ref_out ) == 0 ); |
| 464 | } |
| 465 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 466 | exit: |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 467 | x509_csr_free( &csr ); |
| 468 | polarssl_free( csr_der ); |
| 469 | } |
| 470 | /* END_CASE */ |
| 471 | |
Manuel Pégourié-Gonnard | fbae2a1 | 2013-11-26 16:43:39 +0100 | [diff] [blame] | 472 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
| 473 | void x509_crt_parse_path( char *crt_path, int ret, int nb_crt ) |
| 474 | { |
| 475 | x509_crt chain, *cur; |
| 476 | int i; |
| 477 | |
| 478 | x509_crt_init( &chain ); |
| 479 | |
| 480 | TEST_ASSERT( x509_crt_parse_path( &chain, crt_path ) == ret ); |
| 481 | |
| 482 | /* Check how many certs we got */ |
| 483 | for( i = 0, cur = &chain; cur != NULL; cur = cur->next ) |
| 484 | if( cur->raw.p != NULL ) |
| 485 | i++; |
| 486 | |
| 487 | TEST_ASSERT( i == nb_crt ); |
| 488 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 489 | exit: |
Paul Bakker | a2ffccd | 2013-12-02 21:56:37 +0100 | [diff] [blame] | 490 | x509_crt_free( &chain ); |
Manuel Pégourié-Gonnard | fbae2a1 | 2013-11-26 16:43:39 +0100 | [diff] [blame] | 491 | } |
| 492 | /* END_CASE */ |
| 493 | |
Janos Follath | 189c743 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 494 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 7ac5019 | 2017-07-10 11:09:22 +0200 | [diff] [blame] | 495 | void x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int, |
| 496 | int ret_chk, int flags_chk ) |
| 497 | { |
| 498 | char file_buf[128]; |
| 499 | int ret; |
Manuel Pégourié-Gonnard | 8af7bfa | 2017-07-10 11:20:08 +0200 | [diff] [blame] | 500 | int flags; |
Manuel Pégourié-Gonnard | 7ac5019 | 2017-07-10 11:09:22 +0200 | [diff] [blame] | 501 | x509_crt trusted, chain; |
| 502 | |
| 503 | /* |
| 504 | * We expect chain_dir to contain certificates 00.crt, 01.crt, etc. |
| 505 | * with NN.crt signed by NN-1.crt |
| 506 | */ |
| 507 | |
| 508 | x509_crt_init( &trusted ); |
| 509 | x509_crt_init( &chain ); |
| 510 | |
| 511 | /* Load trusted root */ |
| 512 | TEST_ASSERT( x509_crt_parse_file( &trusted, ca_file ) == 0 ); |
| 513 | |
| 514 | /* Load a chain with nb_int intermediates (from 01 to nb_int), |
| 515 | * plus one "end-entity" cert (nb_int + 1) */ |
| 516 | ret = snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir, |
| 517 | nb_int + 1 ); |
| 518 | TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf ); |
| 519 | TEST_ASSERT( x509_crt_parse_file( &chain, file_buf ) == 0 ); |
| 520 | |
| 521 | /* Try to verify that chain */ |
| 522 | ret = x509_crt_verify( &chain, &trusted, NULL, NULL, &flags, |
| 523 | NULL, NULL ); |
| 524 | TEST_ASSERT( ret == ret_chk ); |
Manuel Pégourié-Gonnard | 8af7bfa | 2017-07-10 11:20:08 +0200 | [diff] [blame] | 525 | TEST_ASSERT( flags == flags_chk ); |
Manuel Pégourié-Gonnard | 7ac5019 | 2017-07-10 11:09:22 +0200 | [diff] [blame] | 526 | |
| 527 | exit: |
| 528 | x509_crt_free( &chain ); |
| 529 | x509_crt_free( &trusted ); |
| 530 | } |
| 531 | /* END_CASE */ |
| 532 | |
| 533 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
Janos Follath | 3d98a7e | 2015-10-11 16:17:27 +0200 | [diff] [blame] | 534 | 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] | 535 | { |
| 536 | char* act; |
| 537 | int flags; |
Janos Follath | 3d98a7e | 2015-10-11 16:17:27 +0200 | [diff] [blame] | 538 | int result, res; |
Janos Follath | 189c743 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 539 | x509_crt trusted, chain; |
| 540 | |
Janos Follath | 3d98a7e | 2015-10-11 16:17:27 +0200 | [diff] [blame] | 541 | result = flags_result ? POLARSSL_ERR_X509_CERT_VERIFY_FAILED : 0; |
| 542 | |
Janos Follath | 189c743 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 543 | x509_crt_init( &chain ); |
| 544 | x509_crt_init( &trusted ); |
| 545 | |
Manuel Pégourié-Gonnard | 28e1ac5 | 2015-11-02 06:34:46 +0900 | [diff] [blame] | 546 | while( ( act = mystrsep( &chain_paths, " " ) ) != NULL ) |
Janos Follath | 189c743 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 547 | TEST_ASSERT( x509_crt_parse_file( &chain, act ) == 0 ); |
| 548 | TEST_ASSERT( x509_crt_parse_file( &trusted, trusted_ca ) == 0 ); |
| 549 | |
| 550 | res = x509_crt_verify( &chain, &trusted, NULL, NULL, &flags, NULL, NULL ); |
| 551 | |
Janos Follath | 3d98a7e | 2015-10-11 16:17:27 +0200 | [diff] [blame] | 552 | TEST_ASSERT( res == result ); |
| 553 | TEST_ASSERT( flags == flags_result ); |
Janos Follath | 189c743 | 2015-10-11 10:25:22 +0200 | [diff] [blame] | 554 | |
| 555 | exit: |
| 556 | x509_crt_free( &trusted ); |
| 557 | x509_crt_free( &chain ); |
| 558 | } |
| 559 | /* END_CASE */ |
| 560 | |
Manuel Pégourié-Gonnard | 0f7b619 | 2014-06-24 11:37:54 +0200 | [diff] [blame] | 561 | /* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */ |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 562 | void x509_oid_desc( char *oid_str, char *ref_desc ) |
| 563 | { |
| 564 | x509_buf oid; |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 565 | const char *desc = NULL; |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 566 | unsigned char buf[20]; |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 567 | int ret; |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 568 | |
| 569 | memset( buf, 0, sizeof buf ); |
| 570 | |
| 571 | oid.tag = ASN1_OID; |
| 572 | oid.len = unhexify( buf, oid_str ); |
| 573 | oid.p = buf; |
| 574 | |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 575 | ret = oid_get_extended_key_usage( &oid, &desc ); |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 576 | |
| 577 | if( strcmp( ref_desc, "notfound" ) == 0 ) |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 578 | { |
| 579 | TEST_ASSERT( ret != 0 ); |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 580 | TEST_ASSERT( desc == NULL ); |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 581 | } |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 582 | else |
| 583 | { |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 584 | TEST_ASSERT( ret == 0 ); |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 585 | TEST_ASSERT( desc != NULL ); |
| 586 | TEST_ASSERT( strcmp( desc, ref_desc ) == 0 ); |
| 587 | } |
| 588 | } |
| 589 | /* END_CASE */ |
| 590 | |
Manuel Pégourié-Gonnard | 0f7b619 | 2014-06-24 11:37:54 +0200 | [diff] [blame] | 591 | /* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */ |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 592 | void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret ) |
| 593 | { |
| 594 | x509_buf oid; |
| 595 | unsigned char oid_buf[20]; |
| 596 | char num_buf[100]; |
| 597 | |
| 598 | memset( oid_buf, 0x00, sizeof oid_buf ); |
| 599 | memset( num_buf, 0x2a, sizeof num_buf ); |
| 600 | |
| 601 | oid.tag = ASN1_OID; |
| 602 | oid.len = unhexify( oid_buf, oid_str ); |
| 603 | oid.p = oid_buf; |
| 604 | |
| 605 | TEST_ASSERT( (size_t) blen <= sizeof num_buf ); |
| 606 | |
Manuel Pégourié-Gonnard | 079333b | 2015-03-20 18:14:26 +0000 | [diff] [blame] | 607 | 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] | 608 | |
| 609 | if( ret >= 0 ) |
| 610 | { |
| 611 | TEST_ASSERT( num_buf[ret] == 0 ); |
| 612 | TEST_ASSERT( strcmp( num_buf, numstr ) == 0 ); |
| 613 | } |
| 614 | } |
| 615 | /* END_CASE */ |
| 616 | |
Paul Bakker | 5b11d02 | 2014-07-10 13:54:38 +0200 | [diff] [blame] | 617 | /* 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] | 618 | void x509_check_key_usage( char *crt_file, int usage, int ret ) |
| 619 | { |
| 620 | x509_crt crt; |
| 621 | |
| 622 | x509_crt_init( &crt ); |
| 623 | |
| 624 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 625 | |
| 626 | TEST_ASSERT( x509_crt_check_key_usage( &crt, usage ) == ret ); |
| 627 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 628 | exit: |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 629 | x509_crt_free( &crt ); |
| 630 | } |
| 631 | /* END_CASE */ |
| 632 | |
Paul Bakker | 5b11d02 | 2014-07-10 13:54:38 +0200 | [diff] [blame] | 633 | /* 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] | 634 | void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret ) |
| 635 | { |
| 636 | x509_crt crt; |
| 637 | char oid[50]; |
| 638 | size_t len; |
| 639 | |
| 640 | x509_crt_init( &crt ); |
| 641 | |
| 642 | len = unhexify( (unsigned char *) oid, usage_hex ); |
| 643 | |
| 644 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 645 | |
| 646 | TEST_ASSERT( x509_crt_check_extended_key_usage( &crt, oid, len ) == ret ); |
| 647 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 648 | exit: |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 649 | x509_crt_free( &crt ); |
| 650 | } |
| 651 | /* END_CASE */ |
| 652 | |
Andres AG | 0da3e44 | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 653 | /* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */ |
| 654 | void x509_get_time( int tag, char *time_str, int ret, |
| 655 | int year, int mon, int day, |
| 656 | int hour, int min, int sec ) |
| 657 | { |
| 658 | x509_time time; |
| 659 | unsigned char buf[17]; |
| 660 | unsigned char* start = buf; |
| 661 | unsigned char* end = buf; |
| 662 | |
| 663 | memset( &time, 0x00, sizeof( time ) ); |
| 664 | *end = (unsigned char)tag; end++; |
| 665 | if( tag == ASN1_UTC_TIME ) |
| 666 | *end = 13; |
| 667 | else |
| 668 | *end = 15; |
| 669 | end++; |
| 670 | memcpy( end, time_str, (size_t)*(end - 1) ); |
| 671 | end += *(end - 1); |
| 672 | |
| 673 | TEST_ASSERT( x509_get_time( &start, end, &time ) == ret ); |
| 674 | if( ret == 0 ) |
| 675 | { |
| 676 | TEST_ASSERT( year == time.year ); |
| 677 | TEST_ASSERT( mon == time.mon ); |
| 678 | TEST_ASSERT( day == time.day ); |
| 679 | TEST_ASSERT( hour == time.hour ); |
| 680 | TEST_ASSERT( min == time.min ); |
| 681 | TEST_ASSERT( sec == time.sec ); |
| 682 | } |
| 683 | } |
| 684 | /* END_CASE */ |
| 685 | |
Manuel Pégourié-Gonnard | d1539b1 | 2014-06-06 16:42:37 +0200 | [diff] [blame] | 686 | /* 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] | 687 | void x509_parse_rsassa_pss_params( char *hex_params, int params_tag, |
| 688 | int ref_msg_md, int ref_mgf_md, |
| 689 | int ref_salt_len, int ref_ret ) |
| 690 | { |
| 691 | int my_ret; |
| 692 | x509_buf params; |
| 693 | md_type_t my_msg_md, my_mgf_md; |
| 694 | int my_salt_len; |
| 695 | |
| 696 | params.p = unhexify_alloc( hex_params, ¶ms.len ); |
| 697 | params.tag = params_tag; |
| 698 | |
| 699 | my_ret = x509_get_rsassa_pss_params( ¶ms, &my_msg_md, &my_mgf_md, |
| 700 | &my_salt_len ); |
| 701 | |
Manuel Pégourié-Gonnard | 8540369 | 2014-06-06 14:48:38 +0200 | [diff] [blame] | 702 | TEST_ASSERT( my_ret == ref_ret ); |
| 703 | |
| 704 | if( ref_ret == 0 ) |
| 705 | { |
| 706 | TEST_ASSERT( my_msg_md == (md_type_t) ref_msg_md ); |
| 707 | TEST_ASSERT( my_mgf_md == (md_type_t) ref_mgf_md ); |
| 708 | TEST_ASSERT( my_salt_len == ref_salt_len ); |
| 709 | } |
| 710 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 711 | exit: |
Manuel Pégourié-Gonnard | 8540369 | 2014-06-06 14:48:38 +0200 | [diff] [blame] | 712 | polarssl_free( params.p ); |
| 713 | } |
| 714 | /* END_CASE */ |
| 715 | |
Manuel Pégourié-Gonnard | 2014016 | 2013-10-10 12:48:03 +0200 | [diff] [blame] | 716 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_SELF_TEST */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 717 | void x509_selftest() |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 718 | { |
| 719 | TEST_ASSERT( x509_self_test( 0 ) == 0 ); |
| 720 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 721 | /* END_CASE */ |