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