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 | |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 25 | END_HEADER |
| 26 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 27 | BEGIN_DEPENDENCIES |
| 28 | depends_on:POLARSSL_X509_PARSE_C:POLARSSL_BIGNUM_C |
| 29 | END_DEPENDENCIES |
| 30 | |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 31 | BEGIN_CASE |
| 32 | x509_cert_info:crt_file:result_str |
| 33 | { |
| 34 | x509_cert crt; |
| 35 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 36 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 37 | |
| 38 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 39 | memset( buf, 0, 2000 ); |
| 40 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 41 | TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 42 | res = x509parse_cert_info( buf, 2000, "", &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 43 | |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 44 | x509_free( &crt ); |
| 45 | |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 46 | TEST_ASSERT( res != -1 ); |
| 47 | TEST_ASSERT( res != -2 ); |
| 48 | |
| 49 | TEST_ASSERT( strcmp( buf, {result_str} ) == 0 ); |
| 50 | } |
| 51 | END_CASE |
| 52 | |
| 53 | BEGIN_CASE |
| 54 | x509_crl_info:crl_file:result_str |
| 55 | { |
| 56 | x509_crl crl; |
| 57 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 58 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 59 | |
| 60 | memset( &crl, 0, sizeof( x509_crl ) ); |
| 61 | memset( buf, 0, 2000 ); |
| 62 | |
| 63 | TEST_ASSERT( x509parse_crlfile( &crl, {crl_file} ) == 0 ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 64 | res = x509parse_crl_info( buf, 2000, "", &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 65 | |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 66 | x509_crl_free( &crl ); |
| 67 | |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 68 | TEST_ASSERT( res != -1 ); |
| 69 | TEST_ASSERT( res != -2 ); |
| 70 | |
| 71 | TEST_ASSERT( strcmp( buf, {result_str} ) == 0 ); |
| 72 | } |
| 73 | END_CASE |
| 74 | |
| 75 | BEGIN_CASE |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 76 | 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] | 77 | { |
| 78 | x509_cert crt; |
| 79 | x509_cert ca; |
| 80 | x509_crl crl; |
| 81 | int flags = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 82 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 83 | |
| 84 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 85 | memset( &ca, 0, sizeof( x509_cert ) ); |
| 86 | memset( &crl, 0, sizeof( x509_crl ) ); |
| 87 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 88 | TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 ); |
| 89 | TEST_ASSERT( x509parse_crtfile( &ca, {ca_file} ) == 0 ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 90 | TEST_ASSERT( x509parse_crlfile( &crl, {crl_file} ) == 0 ); |
| 91 | |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 92 | res = x509parse_verify( &crt, &ca, &crl, {cn_name}, &flags, {verify_callback}, NULL ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 93 | |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 94 | x509_free( &crt ); |
| 95 | x509_free( &ca ); |
| 96 | x509_crl_free( &crl ); |
| 97 | |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 98 | TEST_ASSERT( res == ( {result} ) ); |
| 99 | TEST_ASSERT( flags == ( {flags} ) ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 100 | } |
| 101 | END_CASE |
| 102 | |
| 103 | BEGIN_CASE |
| 104 | x509_dn_gets:crt_file:entity:result_str |
| 105 | { |
| 106 | x509_cert crt; |
| 107 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 108 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 109 | |
| 110 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 111 | memset( buf, 0, 2000 ); |
| 112 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 113 | TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 114 | res = x509parse_dn_gets( buf, 2000, &crt.{entity} ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 115 | |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 116 | x509_free( &crt ); |
| 117 | |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 118 | TEST_ASSERT( res != -1 ); |
| 119 | TEST_ASSERT( res != -2 ); |
| 120 | |
| 121 | TEST_ASSERT( strcmp( buf, {result_str} ) == 0 ); |
| 122 | } |
| 123 | END_CASE |
| 124 | |
| 125 | BEGIN_CASE |
| 126 | x509_time_expired:crt_file:entity:result |
| 127 | { |
| 128 | x509_cert crt; |
| 129 | |
| 130 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 131 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 132 | TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 133 | TEST_ASSERT( x509parse_time_expired( &crt.{entity} ) == {result} ); |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 134 | |
| 135 | x509_free( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 136 | } |
| 137 | END_CASE |
| 138 | |
| 139 | BEGIN_CASE |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 140 | x509parse_keyfile:key_file:password:result |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 141 | { |
| 142 | rsa_context rsa; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 143 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 144 | |
| 145 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 146 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 147 | res = x509parse_keyfile( &rsa, {key_file}, {password} ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 148 | |
| 149 | TEST_ASSERT( res == {result} ); |
| 150 | |
| 151 | if( res == 0 ) |
| 152 | { |
| 153 | TEST_ASSERT( rsa_check_privkey( &rsa ) == 0 ); |
| 154 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 155 | |
| 156 | rsa_free( &rsa ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 157 | } |
| 158 | END_CASE |
| 159 | |
| 160 | BEGIN_CASE |
Paul Bakker | 36f1b19 | 2011-07-13 11:32:29 +0000 | [diff] [blame] | 161 | x509parse_public_keyfile:key_file:result |
| 162 | { |
| 163 | rsa_context rsa; |
| 164 | int res; |
| 165 | |
| 166 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 167 | |
| 168 | res = x509parse_public_keyfile( &rsa, {key_file} ); |
| 169 | |
| 170 | TEST_ASSERT( res == {result} ); |
| 171 | |
| 172 | if( res == 0 ) |
| 173 | { |
| 174 | TEST_ASSERT( rsa_check_pubkey( &rsa ) == 0 ); |
| 175 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 176 | |
| 177 | rsa_free( &rsa ); |
Paul Bakker | 36f1b19 | 2011-07-13 11:32:29 +0000 | [diff] [blame] | 178 | } |
| 179 | END_CASE |
| 180 | |
| 181 | BEGIN_CASE |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 182 | x509parse_crt:crt_data:result_str:result |
| 183 | { |
| 184 | x509_cert crt; |
| 185 | unsigned char buf[2000]; |
| 186 | unsigned char output[2000]; |
| 187 | int data_len, res; |
| 188 | |
| 189 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 190 | memset( buf, 0, 2000 ); |
| 191 | memset( output, 0, 2000 ); |
| 192 | |
| 193 | data_len = unhexify( buf, {crt_data} ); |
| 194 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 195 | TEST_ASSERT( x509parse_crt( &crt, buf, data_len ) == ( {result} ) ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 196 | if( ( {result} ) == 0 ) |
| 197 | { |
| 198 | res = x509parse_cert_info( (char *) output, 2000, "", &crt ); |
| 199 | |
| 200 | TEST_ASSERT( res != -1 ); |
| 201 | TEST_ASSERT( res != -2 ); |
| 202 | |
| 203 | TEST_ASSERT( strcmp( (char *) output, {result_str} ) == 0 ); |
| 204 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 205 | |
| 206 | x509_free( &crt ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 207 | } |
| 208 | END_CASE |
| 209 | |
| 210 | BEGIN_CASE |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 211 | x509parse_crl:crl_data:result_str:result |
| 212 | { |
| 213 | x509_crl crl; |
| 214 | unsigned char buf[2000]; |
| 215 | unsigned char output[2000]; |
| 216 | int data_len, res; |
| 217 | |
| 218 | memset( &crl, 0, sizeof( x509_crl ) ); |
| 219 | memset( buf, 0, 2000 ); |
| 220 | memset( output, 0, 2000 ); |
| 221 | |
| 222 | data_len = unhexify( buf, {crl_data} ); |
| 223 | |
| 224 | TEST_ASSERT( x509parse_crl( &crl, buf, data_len ) == ( {result} ) ); |
| 225 | if( ( {result} ) == 0 ) |
| 226 | { |
| 227 | res = x509parse_crl_info( (char *) output, 2000, "", &crl ); |
| 228 | |
| 229 | TEST_ASSERT( res != -1 ); |
| 230 | TEST_ASSERT( res != -2 ); |
| 231 | |
| 232 | TEST_ASSERT( strcmp( (char *) output, {result_str} ) == 0 ); |
| 233 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 234 | |
| 235 | x509_crl_free( &crl ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 236 | } |
| 237 | END_CASE |
| 238 | |
| 239 | BEGIN_CASE |
| 240 | x509parse_key:key_data:result_str:result |
| 241 | { |
| 242 | rsa_context rsa; |
| 243 | unsigned char buf[2000]; |
| 244 | unsigned char output[2000]; |
Paul Bakker | eaf90d9 | 2011-07-13 14:21:52 +0000 | [diff] [blame] | 245 | int data_len; |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 246 | |
| 247 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 248 | memset( buf, 0, 2000 ); |
| 249 | memset( output, 0, 2000 ); |
| 250 | |
| 251 | data_len = unhexify( buf, {key_data} ); |
| 252 | |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 253 | TEST_ASSERT( x509parse_key( &rsa, buf, data_len, NULL, 0 ) == ( {result} ) ); |
| 254 | if( ( {result} ) == 0 ) |
| 255 | { |
| 256 | TEST_ASSERT( 1 ); |
| 257 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 258 | |
| 259 | rsa_free( &rsa ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 260 | } |
| 261 | END_CASE |
| 262 | |
| 263 | BEGIN_CASE |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 264 | x509_selftest: |
| 265 | { |
| 266 | TEST_ASSERT( x509_self_test( 0 ) == 0 ); |
| 267 | } |
| 268 | END_CASE |