Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 1 | BEGIN_HEADER |
| 2 | #include <polarssl/x509.h> |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 3 | #include <polarssl/pem.h> |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 4 | |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 5 | int verify_none( void *data, x509_cert *crt, int certificate_depth, int *flags ) |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 6 | { |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 7 | ((void) data); |
| 8 | ((void) crt); |
| 9 | ((void) certificate_depth); |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 10 | *flags |= BADCERT_OTHER; |
| 11 | |
| 12 | return 0; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 13 | } |
| 14 | |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 15 | int verify_all( void *data, x509_cert *crt, int certificate_depth, int *flags ) |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 16 | { |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 17 | ((void) data); |
| 18 | ((void) crt); |
| 19 | ((void) certificate_depth); |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 20 | *flags = 0; |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 21 | |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 22 | return 0; |
| 23 | } |
| 24 | |
Manuel Pégourié-Gonnard | 4dd43ae | 2015-11-02 06:52:52 +0900 | [diff] [blame] | 25 | /* strsep() not available on Windows */ |
| 26 | char *mystrsep(char **stringp, const char *delim) |
| 27 | { |
| 28 | const char *p; |
| 29 | char *ret = *stringp; |
| 30 | |
| 31 | if( *stringp == NULL ) |
| 32 | return( NULL ); |
| 33 | |
| 34 | for( ; ; (*stringp)++ ) |
| 35 | { |
| 36 | if( **stringp == '\0' ) |
| 37 | { |
| 38 | *stringp = NULL; |
| 39 | goto done; |
| 40 | } |
| 41 | |
| 42 | for( p = delim; *p != '\0'; p++ ) |
| 43 | if( **stringp == *p ) |
| 44 | { |
| 45 | **stringp = '\0'; |
| 46 | (*stringp)++; |
| 47 | goto done; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | done: |
| 52 | return( ret ); |
| 53 | } |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 54 | END_HEADER |
| 55 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 56 | BEGIN_DEPENDENCIES |
| 57 | depends_on:POLARSSL_X509_PARSE_C:POLARSSL_BIGNUM_C |
| 58 | END_DEPENDENCIES |
| 59 | |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 60 | BEGIN_CASE |
| 61 | x509_cert_info:crt_file:result_str |
| 62 | { |
| 63 | x509_cert crt; |
| 64 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 65 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 66 | |
| 67 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 68 | memset( buf, 0, 2000 ); |
| 69 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 70 | TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 71 | res = x509parse_cert_info( buf, 2000, "", &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 72 | |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 73 | x509_free( &crt ); |
| 74 | |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 75 | TEST_ASSERT( res != -1 ); |
| 76 | TEST_ASSERT( res != -2 ); |
| 77 | |
| 78 | TEST_ASSERT( strcmp( buf, {result_str} ) == 0 ); |
| 79 | } |
| 80 | END_CASE |
| 81 | |
| 82 | BEGIN_CASE |
| 83 | x509_crl_info:crl_file:result_str |
| 84 | { |
| 85 | x509_crl crl; |
| 86 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 87 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 88 | |
| 89 | memset( &crl, 0, sizeof( x509_crl ) ); |
| 90 | memset( buf, 0, 2000 ); |
| 91 | |
| 92 | TEST_ASSERT( x509parse_crlfile( &crl, {crl_file} ) == 0 ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 93 | res = x509parse_crl_info( buf, 2000, "", &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 94 | |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 95 | x509_crl_free( &crl ); |
| 96 | |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 97 | TEST_ASSERT( res != -1 ); |
| 98 | TEST_ASSERT( res != -2 ); |
| 99 | |
| 100 | TEST_ASSERT( strcmp( buf, {result_str} ) == 0 ); |
| 101 | } |
| 102 | END_CASE |
| 103 | |
| 104 | BEGIN_CASE |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 105 | x509_verify:crt_file:ca_file:crl_file:cn_name:result:flags:verify_callback |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 106 | { |
| 107 | x509_cert crt; |
| 108 | x509_cert ca; |
| 109 | x509_crl crl; |
| 110 | int flags = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 111 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 112 | |
| 113 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 114 | memset( &ca, 0, sizeof( x509_cert ) ); |
| 115 | memset( &crl, 0, sizeof( x509_crl ) ); |
| 116 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 117 | TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 ); |
| 118 | TEST_ASSERT( x509parse_crtfile( &ca, {ca_file} ) == 0 ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 119 | TEST_ASSERT( x509parse_crlfile( &crl, {crl_file} ) == 0 ); |
| 120 | |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 121 | res = x509parse_verify( &crt, &ca, &crl, {cn_name}, &flags, {verify_callback}, NULL ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 122 | |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 123 | x509_free( &crt ); |
| 124 | x509_free( &ca ); |
| 125 | x509_crl_free( &crl ); |
| 126 | |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 127 | TEST_ASSERT( res == ( {result} ) ); |
| 128 | TEST_ASSERT( flags == ( {flags} ) ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 129 | } |
| 130 | END_CASE |
| 131 | |
| 132 | BEGIN_CASE |
| 133 | x509_dn_gets:crt_file:entity:result_str |
| 134 | { |
| 135 | x509_cert crt; |
| 136 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 137 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 138 | |
| 139 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 140 | memset( buf, 0, 2000 ); |
| 141 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 142 | TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 143 | res = x509parse_dn_gets( buf, 2000, &crt.{entity} ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 144 | |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 145 | x509_free( &crt ); |
| 146 | |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 147 | TEST_ASSERT( res != -1 ); |
| 148 | TEST_ASSERT( res != -2 ); |
| 149 | |
| 150 | TEST_ASSERT( strcmp( buf, {result_str} ) == 0 ); |
| 151 | } |
| 152 | END_CASE |
| 153 | |
| 154 | BEGIN_CASE |
| 155 | x509_time_expired:crt_file:entity:result |
| 156 | { |
| 157 | x509_cert crt; |
| 158 | |
| 159 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 160 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 161 | TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 162 | TEST_ASSERT( x509parse_time_expired( &crt.{entity} ) == {result} ); |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 163 | |
| 164 | x509_free( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 165 | } |
| 166 | END_CASE |
| 167 | |
| 168 | BEGIN_CASE |
Paul Bakker | 0d844dd | 2014-07-07 17:44:14 +0200 | [diff] [blame] | 169 | x509_time_future:crt_file:entity:result |
| 170 | { |
| 171 | x509_cert crt; |
| 172 | |
| 173 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 174 | |
| 175 | TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 ); |
| 176 | TEST_ASSERT( x509parse_time_future( &crt.{entity} ) == {result} ); |
| 177 | |
| 178 | x509_free( &crt ); |
| 179 | } |
| 180 | END_CASE |
| 181 | |
| 182 | BEGIN_CASE |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 183 | x509parse_keyfile:key_file:password:result |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 184 | { |
| 185 | rsa_context rsa; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 186 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 187 | |
| 188 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 189 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 190 | res = x509parse_keyfile( &rsa, {key_file}, {password} ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 191 | |
| 192 | TEST_ASSERT( res == {result} ); |
| 193 | |
| 194 | if( res == 0 ) |
| 195 | { |
| 196 | TEST_ASSERT( rsa_check_privkey( &rsa ) == 0 ); |
| 197 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 198 | |
| 199 | rsa_free( &rsa ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 200 | } |
| 201 | END_CASE |
| 202 | |
| 203 | BEGIN_CASE |
Paul Bakker | 36f1b19 | 2011-07-13 11:32:29 +0000 | [diff] [blame] | 204 | x509parse_public_keyfile:key_file:result |
| 205 | { |
| 206 | rsa_context rsa; |
| 207 | int res; |
| 208 | |
| 209 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 210 | |
| 211 | res = x509parse_public_keyfile( &rsa, {key_file} ); |
| 212 | |
| 213 | TEST_ASSERT( res == {result} ); |
| 214 | |
| 215 | if( res == 0 ) |
| 216 | { |
| 217 | TEST_ASSERT( rsa_check_pubkey( &rsa ) == 0 ); |
| 218 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 219 | |
| 220 | rsa_free( &rsa ); |
Paul Bakker | 36f1b19 | 2011-07-13 11:32:29 +0000 | [diff] [blame] | 221 | } |
| 222 | END_CASE |
| 223 | |
| 224 | BEGIN_CASE |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 225 | x509parse_crt:crt_data:result_str:result |
| 226 | { |
| 227 | x509_cert crt; |
| 228 | unsigned char buf[2000]; |
| 229 | unsigned char output[2000]; |
| 230 | int data_len, res; |
| 231 | |
| 232 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 233 | memset( buf, 0, 2000 ); |
| 234 | memset( output, 0, 2000 ); |
| 235 | |
| 236 | data_len = unhexify( buf, {crt_data} ); |
| 237 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 238 | TEST_ASSERT( x509parse_crt( &crt, buf, data_len ) == ( {result} ) ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 239 | if( ( {result} ) == 0 ) |
| 240 | { |
| 241 | res = x509parse_cert_info( (char *) output, 2000, "", &crt ); |
| 242 | |
| 243 | TEST_ASSERT( res != -1 ); |
| 244 | TEST_ASSERT( res != -2 ); |
| 245 | |
| 246 | TEST_ASSERT( strcmp( (char *) output, {result_str} ) == 0 ); |
| 247 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 248 | |
| 249 | x509_free( &crt ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 250 | } |
| 251 | END_CASE |
| 252 | |
| 253 | BEGIN_CASE |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 254 | x509parse_crl:crl_data:result_str:result |
| 255 | { |
| 256 | x509_crl crl; |
| 257 | unsigned char buf[2000]; |
| 258 | unsigned char output[2000]; |
| 259 | int data_len, res; |
| 260 | |
| 261 | memset( &crl, 0, sizeof( x509_crl ) ); |
| 262 | memset( buf, 0, 2000 ); |
| 263 | memset( output, 0, 2000 ); |
| 264 | |
| 265 | data_len = unhexify( buf, {crl_data} ); |
| 266 | |
| 267 | TEST_ASSERT( x509parse_crl( &crl, buf, data_len ) == ( {result} ) ); |
| 268 | if( ( {result} ) == 0 ) |
| 269 | { |
| 270 | res = x509parse_crl_info( (char *) output, 2000, "", &crl ); |
| 271 | |
| 272 | TEST_ASSERT( res != -1 ); |
| 273 | TEST_ASSERT( res != -2 ); |
| 274 | |
| 275 | TEST_ASSERT( strcmp( (char *) output, {result_str} ) == 0 ); |
| 276 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 277 | |
| 278 | x509_crl_free( &crl ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 279 | } |
| 280 | END_CASE |
| 281 | |
| 282 | BEGIN_CASE |
| 283 | x509parse_key:key_data:result_str:result |
| 284 | { |
| 285 | rsa_context rsa; |
| 286 | unsigned char buf[2000]; |
| 287 | unsigned char output[2000]; |
Paul Bakker | eaf90d9 | 2011-07-13 14:21:52 +0000 | [diff] [blame] | 288 | int data_len; |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 289 | |
| 290 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 291 | memset( buf, 0, 2000 ); |
| 292 | memset( output, 0, 2000 ); |
| 293 | |
| 294 | data_len = unhexify( buf, {key_data} ); |
| 295 | |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 296 | TEST_ASSERT( x509parse_key( &rsa, buf, data_len, NULL, 0 ) == ( {result} ) ); |
| 297 | if( ( {result} ) == 0 ) |
| 298 | { |
| 299 | TEST_ASSERT( 1 ); |
| 300 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 301 | |
| 302 | rsa_free( &rsa ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 303 | } |
| 304 | END_CASE |
| 305 | |
| 306 | BEGIN_CASE |
Manuel Pégourié-Gonnard | ab00374 | 2015-10-29 15:27:03 +0100 | [diff] [blame] | 307 | x509_crt_verify_chain:chain_paths_str:trusted_ca:flags_result |
Manuel Pégourié-Gonnard | 03ae1bc | 2015-11-02 06:09:57 +0900 | [diff] [blame] | 308 | { |
| 309 | char *act; |
| 310 | int flags; |
Manuel Pégourié-Gonnard | ab00374 | 2015-10-29 15:27:03 +0100 | [diff] [blame] | 311 | int result, res; |
Manuel Pégourié-Gonnard | 03ae1bc | 2015-11-02 06:09:57 +0900 | [diff] [blame] | 312 | x509_cert trusted, chain; |
| 313 | char *chain_paths; |
| 314 | |
| 315 | memset( &chain, 0, sizeof( x509_cert ) ); |
| 316 | memset( &trusted, 0, sizeof( x509_cert ) ); |
| 317 | chain_paths = strdup( {chain_paths_str} ); |
| 318 | TEST_ASSERT( chain_paths != NULL ); |
| 319 | |
Manuel Pégourié-Gonnard | 4dd43ae | 2015-11-02 06:52:52 +0900 | [diff] [blame] | 320 | while( ( act = mystrsep( &chain_paths, " " ) ) != NULL ) |
Manuel Pégourié-Gonnard | 03ae1bc | 2015-11-02 06:09:57 +0900 | [diff] [blame] | 321 | TEST_ASSERT( x509parse_crtfile( &chain, act ) == 0 ); |
| 322 | TEST_ASSERT( x509parse_crtfile( &trusted, {trusted_ca} ) == 0 ); |
| 323 | |
| 324 | res = x509parse_verify( &chain, &trusted, NULL, NULL, &flags, NULL, NULL ); |
| 325 | |
| 326 | x509_free( &trusted ); |
| 327 | x509_free( &chain ); |
| 328 | |
Manuel Pégourié-Gonnard | ab00374 | 2015-10-29 15:27:03 +0100 | [diff] [blame] | 329 | result = ( {flags_result} ) ? POLARSSL_ERR_X509_CERT_VERIFY_FAILED : 0; |
| 330 | |
| 331 | TEST_ASSERT( res == result ); |
| 332 | TEST_ASSERT( flags == ( {flags_result} ) ); |
Manuel Pégourié-Gonnard | 03ae1bc | 2015-11-02 06:09:57 +0900 | [diff] [blame] | 333 | } |
| 334 | END_CASE |
| 335 | |
| 336 | BEGIN_CASE |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 337 | x509_selftest: |
| 338 | { |
| 339 | TEST_ASSERT( x509_self_test( 0 ) == 0 ); |
| 340 | } |
| 341 | END_CASE |