Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 1 | BEGIN_HEADER |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 2 | #include <polarssl/config.h> |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 3 | #include <polarssl/x509.h> |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 4 | |
| 5 | int verify_none( void *data, x509_cert *crt, int certificate_depth, int preverify_ok ) |
| 6 | { |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 7 | ((void) data); |
| 8 | ((void) crt); |
| 9 | ((void) certificate_depth); |
| 10 | ((void) preverify_ok); |
| 11 | |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 12 | return 1; |
| 13 | } |
| 14 | |
| 15 | int verify_all( void *data, x509_cert *crt, int certificate_depth, int preverify_ok ) |
| 16 | { |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 17 | ((void) data); |
| 18 | ((void) crt); |
| 19 | ((void) certificate_depth); |
| 20 | ((void) preverify_ok); |
| 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 | |
| 27 | BEGIN_CASE |
| 28 | x509_cert_info:crt_file:result_str |
| 29 | { |
| 30 | x509_cert crt; |
| 31 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 32 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 33 | |
| 34 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 35 | memset( buf, 0, 2000 ); |
| 36 | |
| 37 | TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 38 | res = x509parse_cert_info( buf, 2000, "", &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 39 | |
| 40 | TEST_ASSERT( res != -1 ); |
| 41 | TEST_ASSERT( res != -2 ); |
| 42 | |
| 43 | TEST_ASSERT( strcmp( buf, {result_str} ) == 0 ); |
| 44 | } |
| 45 | END_CASE |
| 46 | |
| 47 | BEGIN_CASE |
| 48 | x509_crl_info:crl_file:result_str |
| 49 | { |
| 50 | x509_crl crl; |
| 51 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 52 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 53 | |
| 54 | memset( &crl, 0, sizeof( x509_crl ) ); |
| 55 | memset( buf, 0, 2000 ); |
| 56 | |
| 57 | TEST_ASSERT( x509parse_crlfile( &crl, {crl_file} ) == 0 ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 58 | res = x509parse_crl_info( buf, 2000, "", &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 59 | |
| 60 | TEST_ASSERT( res != -1 ); |
| 61 | TEST_ASSERT( res != -2 ); |
| 62 | |
| 63 | TEST_ASSERT( strcmp( buf, {result_str} ) == 0 ); |
| 64 | } |
| 65 | END_CASE |
| 66 | |
| 67 | BEGIN_CASE |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 68 | 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] | 69 | { |
| 70 | x509_cert crt; |
| 71 | x509_cert ca; |
| 72 | x509_crl crl; |
| 73 | int flags = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 74 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 75 | |
| 76 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 77 | memset( &ca, 0, sizeof( x509_cert ) ); |
| 78 | memset( &crl, 0, sizeof( x509_crl ) ); |
| 79 | |
| 80 | TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 ); |
| 81 | TEST_ASSERT( x509parse_crtfile( &ca, {ca_file} ) == 0 ); |
| 82 | TEST_ASSERT( x509parse_crlfile( &crl, {crl_file} ) == 0 ); |
| 83 | |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 84 | res = x509parse_verify( &crt, &ca, &crl, {cn_name}, &flags, {verify_callback}, NULL ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 85 | |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 86 | TEST_ASSERT( res == ( {result} ) ); |
| 87 | TEST_ASSERT( flags == ( {flags} ) ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 88 | } |
| 89 | END_CASE |
| 90 | |
| 91 | BEGIN_CASE |
| 92 | x509_dn_gets:crt_file:entity:result_str |
| 93 | { |
| 94 | x509_cert crt; |
| 95 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 96 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 97 | |
| 98 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 99 | memset( buf, 0, 2000 ); |
| 100 | |
| 101 | TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 102 | res = x509parse_dn_gets( buf, 2000, &crt.{entity} ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 103 | |
| 104 | TEST_ASSERT( res != -1 ); |
| 105 | TEST_ASSERT( res != -2 ); |
| 106 | |
| 107 | TEST_ASSERT( strcmp( buf, {result_str} ) == 0 ); |
| 108 | } |
| 109 | END_CASE |
| 110 | |
| 111 | BEGIN_CASE |
| 112 | x509_time_expired:crt_file:entity:result |
| 113 | { |
| 114 | x509_cert crt; |
| 115 | |
| 116 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 117 | |
| 118 | TEST_ASSERT( x509parse_crtfile( &crt, {crt_file} ) == 0 ); |
| 119 | TEST_ASSERT( x509parse_time_expired( &crt.{entity} ) == {result} ); |
| 120 | } |
| 121 | END_CASE |
| 122 | |
| 123 | BEGIN_CASE |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 124 | x509parse_keyfile:key_file:password:result |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 125 | { |
| 126 | rsa_context rsa; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 127 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 128 | |
| 129 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 130 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 131 | res = x509parse_keyfile( &rsa, {key_file}, {password} ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 132 | |
| 133 | TEST_ASSERT( res == {result} ); |
| 134 | |
| 135 | if( res == 0 ) |
| 136 | { |
| 137 | TEST_ASSERT( rsa_check_privkey( &rsa ) == 0 ); |
| 138 | } |
| 139 | } |
| 140 | END_CASE |
| 141 | |
| 142 | BEGIN_CASE |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 143 | x509parse_crt:crt_data:result_str:result |
| 144 | { |
| 145 | x509_cert crt; |
| 146 | unsigned char buf[2000]; |
| 147 | unsigned char output[2000]; |
| 148 | int data_len, res; |
| 149 | |
| 150 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 151 | memset( buf, 0, 2000 ); |
| 152 | memset( output, 0, 2000 ); |
| 153 | |
| 154 | data_len = unhexify( buf, {crt_data} ); |
| 155 | |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 156 | TEST_ASSERT( x509parse_crt( &crt, buf, data_len ) == ( {result} ) ); |
| 157 | if( ( {result} ) == 0 ) |
| 158 | { |
| 159 | res = x509parse_cert_info( (char *) output, 2000, "", &crt ); |
| 160 | |
| 161 | TEST_ASSERT( res != -1 ); |
| 162 | TEST_ASSERT( res != -2 ); |
| 163 | |
| 164 | TEST_ASSERT( strcmp( (char *) output, {result_str} ) == 0 ); |
| 165 | } |
| 166 | } |
| 167 | END_CASE |
| 168 | |
| 169 | BEGIN_CASE |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 170 | x509parse_crl:crl_data:result_str:result |
| 171 | { |
| 172 | x509_crl crl; |
| 173 | unsigned char buf[2000]; |
| 174 | unsigned char output[2000]; |
| 175 | int data_len, res; |
| 176 | |
| 177 | memset( &crl, 0, sizeof( x509_crl ) ); |
| 178 | memset( buf, 0, 2000 ); |
| 179 | memset( output, 0, 2000 ); |
| 180 | |
| 181 | data_len = unhexify( buf, {crl_data} ); |
| 182 | |
| 183 | TEST_ASSERT( x509parse_crl( &crl, buf, data_len ) == ( {result} ) ); |
| 184 | if( ( {result} ) == 0 ) |
| 185 | { |
| 186 | res = x509parse_crl_info( (char *) output, 2000, "", &crl ); |
| 187 | |
| 188 | TEST_ASSERT( res != -1 ); |
| 189 | TEST_ASSERT( res != -2 ); |
| 190 | |
| 191 | TEST_ASSERT( strcmp( (char *) output, {result_str} ) == 0 ); |
| 192 | } |
| 193 | } |
| 194 | END_CASE |
| 195 | |
| 196 | BEGIN_CASE |
| 197 | x509parse_key:key_data:result_str:result |
| 198 | { |
| 199 | rsa_context rsa; |
| 200 | unsigned char buf[2000]; |
| 201 | unsigned char output[2000]; |
| 202 | int data_len, res; |
| 203 | |
| 204 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 205 | memset( buf, 0, 2000 ); |
| 206 | memset( output, 0, 2000 ); |
| 207 | |
| 208 | data_len = unhexify( buf, {key_data} ); |
| 209 | |
| 210 | res = x509parse_key( &rsa, buf, data_len, NULL, 0 ); |
| 211 | |
| 212 | TEST_ASSERT( x509parse_key( &rsa, buf, data_len, NULL, 0 ) == ( {result} ) ); |
| 213 | if( ( {result} ) == 0 ) |
| 214 | { |
| 215 | TEST_ASSERT( 1 ); |
| 216 | } |
| 217 | } |
| 218 | END_CASE |
| 219 | |
| 220 | BEGIN_CASE |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 221 | x509_selftest: |
| 222 | { |
| 223 | TEST_ASSERT( x509_self_test( 0 ) == 0 ); |
| 224 | } |
| 225 | END_CASE |