Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2 | #include <polarssl/x509_crt.h> |
| 3 | #include <polarssl/x509_crl.h> |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 4 | #include <polarssl/x509_csr.h> |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 5 | #include <polarssl/pem.h> |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 6 | #include <polarssl/oid.h> |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 7 | |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 8 | 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] | 9 | { |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 10 | ((void) data); |
| 11 | ((void) crt); |
| 12 | ((void) certificate_depth); |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 13 | *flags |= BADCERT_OTHER; |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 14 | |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 15 | return 0; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 16 | } |
| 17 | |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 18 | 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] | 19 | { |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 20 | ((void) data); |
| 21 | ((void) crt); |
| 22 | ((void) certificate_depth); |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 23 | *flags = 0; |
Paul Bakker | 5a62408 | 2011-01-18 16:31:52 +0000 | [diff] [blame] | 24 | |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 25 | return 0; |
| 26 | } |
| 27 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 28 | /* END_HEADER */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 29 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 30 | /* BEGIN_DEPENDENCIES |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 31 | * depends_on:POLARSSL_BIGNUM_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 32 | * END_DEPENDENCIES |
| 33 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 34 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 35 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 36 | void x509_cert_info( char *crt_file, char *result_str ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 37 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 38 | x509_crt crt; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 39 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 40 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 41 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 42 | x509_crt_init( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 43 | memset( buf, 0, 2000 ); |
| 44 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 45 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 46 | res = x509_crt_info( buf, 2000, "", &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 47 | |
| 48 | TEST_ASSERT( res != -1 ); |
| 49 | TEST_ASSERT( res != -2 ); |
| 50 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 51 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 52 | |
| 53 | exit: |
| 54 | x509_crt_free( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 55 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 56 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 57 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 58 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRL_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 59 | void x509_crl_info( char *crl_file, char *result_str ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 60 | { |
| 61 | x509_crl crl; |
| 62 | char buf[2000]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 63 | int res; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 64 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 65 | x509_crl_init( &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 66 | memset( buf, 0, 2000 ); |
| 67 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 68 | TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 ); |
| 69 | res = x509_crl_info( buf, 2000, "", &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 70 | |
| 71 | TEST_ASSERT( res != -1 ); |
| 72 | TEST_ASSERT( res != -2 ); |
| 73 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 74 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 75 | |
| 76 | exit: |
| 77 | x509_crl_free( &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 78 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 79 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 80 | |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 81 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CSR_PARSE_C */ |
| 82 | void x509_csr_info( char *csr_file, char *result_str ) |
| 83 | { |
| 84 | x509_csr csr; |
| 85 | char buf[2000]; |
| 86 | int res; |
| 87 | |
| 88 | x509_csr_init( &csr ); |
| 89 | memset( buf, 0, 2000 ); |
| 90 | |
| 91 | TEST_ASSERT( x509_csr_parse_file( &csr, csr_file ) == 0 ); |
| 92 | res = x509_csr_info( buf, 2000, "", &csr ); |
| 93 | |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 94 | TEST_ASSERT( res != -1 ); |
| 95 | TEST_ASSERT( res != -2 ); |
| 96 | |
| 97 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 98 | |
| 99 | exit: |
| 100 | x509_csr_free( &csr ); |
Manuel Pégourié-Gonnard | 2a8d7fd | 2014-01-24 17:34:26 +0100 | [diff] [blame] | 101 | } |
| 102 | /* END_CASE */ |
| 103 | |
Manuel Pégourié-Gonnard | cbf3ef3 | 2013-09-23 12:20:02 +0200 | [diff] [blame] | 104 | /* 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] | 105 | void x509_verify( char *crt_file, char *ca_file, char *crl_file, |
| 106 | char *cn_name_str, int result, int flags_result, |
| 107 | char *verify_callback ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 108 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 109 | x509_crt crt; |
| 110 | x509_crt ca; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 111 | x509_crl crl; |
| 112 | int flags = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 113 | int res; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 114 | int (*f_vrfy)(void *, x509_crt *, int, int *) = NULL; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 115 | char * cn_name = NULL; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 116 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 117 | x509_crt_init( &crt ); |
| 118 | x509_crt_init( &ca ); |
| 119 | x509_crl_init( &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 120 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 121 | if( strcmp( cn_name_str, "NULL" ) != 0 ) |
| 122 | cn_name = cn_name_str; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 123 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 124 | if( strcmp( verify_callback, "NULL" ) == 0 ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 125 | f_vrfy = NULL; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 126 | else if( strcmp( verify_callback, "verify_none" ) == 0 ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 127 | f_vrfy = verify_none; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 128 | else if( strcmp( verify_callback, "verify_all" ) == 0 ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 129 | f_vrfy = verify_all; |
| 130 | else |
| 131 | TEST_ASSERT( "No known verify callback selected" == 0 ); |
| 132 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 133 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 134 | TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 ); |
| 135 | TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 136 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 137 | 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] | 138 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 139 | TEST_ASSERT( res == ( result ) ); |
| 140 | TEST_ASSERT( flags == ( flags_result ) ); |
| 141 | |
| 142 | exit: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 143 | x509_crt_free( &crt ); |
| 144 | x509_crt_free( &ca ); |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 145 | x509_crl_free( &crl ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 146 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 147 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 148 | |
Manuel Pégourié-Gonnard | fea3102 | 2014-06-24 11:32:05 +0200 | [diff] [blame] | 149 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 150 | void x509_dn_gets( char *crt_file, char *entity, char *result_str ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 151 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 152 | x509_crt crt; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 153 | char buf[2000]; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 154 | int res = 0; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 155 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 156 | x509_crt_init( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 157 | memset( buf, 0, 2000 ); |
| 158 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 159 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 160 | if( strcmp( entity, "subject" ) == 0 ) |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 161 | res = x509_dn_gets( buf, 2000, &crt.subject ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 162 | else if( strcmp( entity, "issuer" ) == 0 ) |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 163 | res = x509_dn_gets( buf, 2000, &crt.issuer ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 164 | else |
| 165 | TEST_ASSERT( "Unknown entity" == 0 ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 166 | |
| 167 | TEST_ASSERT( res != -1 ); |
| 168 | TEST_ASSERT( res != -2 ); |
| 169 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 170 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 171 | |
| 172 | exit: |
| 173 | x509_crt_free( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 174 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 175 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 176 | |
Manuel Pégourié-Gonnard | fea3102 | 2014-06-24 11:32:05 +0200 | [diff] [blame] | 177 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 178 | void x509_time_expired( char *crt_file, char *entity, int result ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 179 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 180 | x509_crt crt; |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 181 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 182 | x509_crt_init( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 183 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 184 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 185 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 186 | if( strcmp( entity, "valid_from" ) == 0 ) |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 187 | TEST_ASSERT( x509_time_expired( &crt.valid_from ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 188 | else if( strcmp( entity, "valid_to" ) == 0 ) |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 189 | TEST_ASSERT( x509_time_expired( &crt.valid_to ) == result ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 190 | else |
| 191 | TEST_ASSERT( "Unknown entity" == 0 ); |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 192 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 193 | exit: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 194 | x509_crt_free( &crt ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 195 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 196 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 197 | |
Manuel Pégourié-Gonnard | fea3102 | 2014-06-24 11:32:05 +0200 | [diff] [blame] | 198 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_C */ |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 199 | void x509_time_future( char *crt_file, char *entity, int result ) |
| 200 | { |
| 201 | x509_crt crt; |
| 202 | |
| 203 | x509_crt_init( &crt ); |
| 204 | |
| 205 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 206 | |
| 207 | if( strcmp( entity, "valid_from" ) == 0 ) |
| 208 | TEST_ASSERT( x509_time_future( &crt.valid_from ) == result ); |
| 209 | else if( strcmp( entity, "valid_to" ) == 0 ) |
| 210 | TEST_ASSERT( x509_time_future( &crt.valid_to ) == result ); |
| 211 | else |
| 212 | TEST_ASSERT( "Unknown entity" == 0 ); |
| 213 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 214 | exit: |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 215 | x509_crt_free( &crt ); |
| 216 | } |
| 217 | /* END_CASE */ |
| 218 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 219 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 220 | void x509parse_crt( char *crt_data, char *result_str, int result ) |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 221 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 222 | x509_crt crt; |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 223 | unsigned char buf[2000]; |
| 224 | unsigned char output[2000]; |
| 225 | int data_len, res; |
| 226 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 227 | x509_crt_init( &crt ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 228 | memset( buf, 0, 2000 ); |
| 229 | memset( output, 0, 2000 ); |
| 230 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 231 | data_len = unhexify( buf, crt_data ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 232 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 233 | TEST_ASSERT( x509_crt_parse( &crt, buf, data_len ) == ( result ) ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 234 | if( ( result ) == 0 ) |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 235 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 236 | res = x509_crt_info( (char *) output, 2000, "", &crt ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 237 | |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 238 | TEST_ASSERT( res != -1 ); |
| 239 | TEST_ASSERT( res != -2 ); |
| 240 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 241 | TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 242 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 243 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 244 | exit: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 245 | x509_crt_free( &crt ); |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 246 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 247 | /* END_CASE */ |
Paul Bakker | b2c38f5 | 2009-07-19 19:36:15 +0000 | [diff] [blame] | 248 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 249 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRL_PARSE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 250 | void x509parse_crl( char *crl_data, char *result_str, int result ) |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 251 | { |
| 252 | x509_crl crl; |
| 253 | unsigned char buf[2000]; |
| 254 | unsigned char output[2000]; |
| 255 | int data_len, res; |
| 256 | |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 257 | x509_crl_init( &crl ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 258 | memset( buf, 0, 2000 ); |
| 259 | memset( output, 0, 2000 ); |
| 260 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 261 | data_len = unhexify( buf, crl_data ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 262 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 263 | TEST_ASSERT( x509_crl_parse( &crl, buf, data_len ) == ( result ) ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 264 | if( ( result ) == 0 ) |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 265 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 266 | res = x509_crl_info( (char *) output, 2000, "", &crl ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 267 | |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 268 | TEST_ASSERT( res != -1 ); |
| 269 | TEST_ASSERT( res != -2 ); |
| 270 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 271 | TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 272 | } |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 273 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 274 | exit: |
Paul Bakker | b08e684 | 2012-02-11 18:43:20 +0000 | [diff] [blame] | 275 | x509_crl_free( &crl ); |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 276 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 277 | /* END_CASE */ |
Paul Bakker | 6b0fa4f | 2009-07-20 20:35:41 +0000 | [diff] [blame] | 278 | |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 279 | /* BEGIN_CASE depends_on:POLARSSL_X509_CSR_PARSE_C */ |
| 280 | void x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret ) |
| 281 | { |
| 282 | x509_csr csr; |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 283 | unsigned char *csr_der = NULL; |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 284 | char my_out[1000]; |
| 285 | size_t csr_der_len; |
| 286 | int my_ret; |
| 287 | |
| 288 | x509_csr_init( &csr ); |
| 289 | memset( my_out, 0, sizeof( my_out ) ); |
| 290 | csr_der = unhexify_alloc( csr_der_hex, &csr_der_len ); |
| 291 | |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 292 | 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] | 293 | TEST_ASSERT( my_ret == ref_ret ); |
| 294 | |
| 295 | if( ref_ret == 0 ) |
| 296 | { |
| 297 | size_t my_out_len = x509_csr_info( my_out, sizeof( my_out ), "", &csr ); |
| 298 | TEST_ASSERT( my_out_len == strlen( ref_out ) ); |
| 299 | TEST_ASSERT( strcmp( my_out, ref_out ) == 0 ); |
| 300 | } |
| 301 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 302 | exit: |
Manuel Pégourié-Gonnard | d77cd5d | 2014-06-13 11:13:15 +0200 | [diff] [blame] | 303 | x509_csr_free( &csr ); |
| 304 | polarssl_free( csr_der ); |
| 305 | } |
| 306 | /* END_CASE */ |
| 307 | |
Manuel Pégourié-Gonnard | fbae2a1 | 2013-11-26 16:43:39 +0100 | [diff] [blame] | 308 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO:POLARSSL_X509_CRT_PARSE_C */ |
| 309 | void x509_crt_parse_path( char *crt_path, int ret, int nb_crt ) |
| 310 | { |
| 311 | x509_crt chain, *cur; |
| 312 | int i; |
| 313 | |
| 314 | x509_crt_init( &chain ); |
| 315 | |
| 316 | TEST_ASSERT( x509_crt_parse_path( &chain, crt_path ) == ret ); |
| 317 | |
| 318 | /* Check how many certs we got */ |
| 319 | for( i = 0, cur = &chain; cur != NULL; cur = cur->next ) |
| 320 | if( cur->raw.p != NULL ) |
| 321 | i++; |
| 322 | |
| 323 | TEST_ASSERT( i == nb_crt ); |
| 324 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 325 | exit: |
Paul Bakker | a2ffccd | 2013-12-02 21:56:37 +0100 | [diff] [blame] | 326 | x509_crt_free( &chain ); |
Manuel Pégourié-Gonnard | fbae2a1 | 2013-11-26 16:43:39 +0100 | [diff] [blame] | 327 | } |
| 328 | /* END_CASE */ |
| 329 | |
Manuel Pégourié-Gonnard | 0f7b619 | 2014-06-24 11:37:54 +0200 | [diff] [blame] | 330 | /* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */ |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 331 | void x509_oid_desc( char *oid_str, char *ref_desc ) |
| 332 | { |
| 333 | x509_buf oid; |
| 334 | const char *desc; |
| 335 | unsigned char buf[20]; |
| 336 | |
| 337 | memset( buf, 0, sizeof buf ); |
| 338 | |
| 339 | oid.tag = ASN1_OID; |
| 340 | oid.len = unhexify( buf, oid_str ); |
| 341 | oid.p = buf; |
| 342 | |
| 343 | desc = x509_oid_get_description( &oid ); |
| 344 | |
| 345 | if( strcmp( ref_desc, "notfound" ) == 0 ) |
| 346 | TEST_ASSERT( desc == NULL ); |
| 347 | else |
| 348 | { |
| 349 | TEST_ASSERT( desc != NULL ); |
| 350 | TEST_ASSERT( strcmp( desc, ref_desc ) == 0 ); |
| 351 | } |
| 352 | } |
| 353 | /* END_CASE */ |
| 354 | |
Manuel Pégourié-Gonnard | 0f7b619 | 2014-06-24 11:37:54 +0200 | [diff] [blame] | 355 | /* BEGIN_CASE depends_on:POLARSSL_X509_USE_C */ |
Manuel Pégourié-Gonnard | 7afdb88 | 2014-03-28 16:06:35 +0100 | [diff] [blame] | 356 | void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret ) |
| 357 | { |
| 358 | x509_buf oid; |
| 359 | unsigned char oid_buf[20]; |
| 360 | char num_buf[100]; |
| 361 | |
| 362 | memset( oid_buf, 0x00, sizeof oid_buf ); |
| 363 | memset( num_buf, 0x2a, sizeof num_buf ); |
| 364 | |
| 365 | oid.tag = ASN1_OID; |
| 366 | oid.len = unhexify( oid_buf, oid_str ); |
| 367 | oid.p = oid_buf; |
| 368 | |
| 369 | TEST_ASSERT( (size_t) blen <= sizeof num_buf ); |
| 370 | |
| 371 | TEST_ASSERT( x509_oid_get_numeric_string( num_buf, blen, &oid ) == ret ); |
| 372 | |
| 373 | if( ret >= 0 ) |
| 374 | { |
| 375 | TEST_ASSERT( num_buf[ret] == 0 ); |
| 376 | TEST_ASSERT( strcmp( num_buf, numstr ) == 0 ); |
| 377 | } |
| 378 | } |
| 379 | /* END_CASE */ |
| 380 | |
Paul Bakker | 5b11d02 | 2014-07-10 13:54:38 +0200 | [diff] [blame] | 381 | /* 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] | 382 | void x509_check_key_usage( char *crt_file, int usage, int ret ) |
| 383 | { |
| 384 | x509_crt crt; |
| 385 | |
| 386 | x509_crt_init( &crt ); |
| 387 | |
| 388 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 389 | |
| 390 | TEST_ASSERT( x509_crt_check_key_usage( &crt, usage ) == ret ); |
| 391 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 392 | exit: |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 393 | x509_crt_free( &crt ); |
| 394 | } |
| 395 | /* END_CASE */ |
| 396 | |
Paul Bakker | 5b11d02 | 2014-07-10 13:54:38 +0200 | [diff] [blame] | 397 | /* 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] | 398 | void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret ) |
| 399 | { |
| 400 | x509_crt crt; |
| 401 | char oid[50]; |
| 402 | size_t len; |
| 403 | |
| 404 | x509_crt_init( &crt ); |
| 405 | |
| 406 | len = unhexify( (unsigned char *) oid, usage_hex ); |
| 407 | |
| 408 | TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| 409 | |
| 410 | TEST_ASSERT( x509_crt_check_extended_key_usage( &crt, oid, len ) == ret ); |
| 411 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 412 | exit: |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 413 | x509_crt_free( &crt ); |
| 414 | } |
| 415 | /* END_CASE */ |
| 416 | |
Manuel Pégourié-Gonnard | d1539b1 | 2014-06-06 16:42:37 +0200 | [diff] [blame] | 417 | /* 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] | 418 | void x509_parse_rsassa_pss_params( char *hex_params, int params_tag, |
| 419 | int ref_msg_md, int ref_mgf_md, |
| 420 | int ref_salt_len, int ref_ret ) |
| 421 | { |
| 422 | int my_ret; |
| 423 | x509_buf params; |
| 424 | md_type_t my_msg_md, my_mgf_md; |
| 425 | int my_salt_len; |
| 426 | |
| 427 | params.p = unhexify_alloc( hex_params, ¶ms.len ); |
| 428 | params.tag = params_tag; |
| 429 | |
| 430 | my_ret = x509_get_rsassa_pss_params( ¶ms, &my_msg_md, &my_mgf_md, |
| 431 | &my_salt_len ); |
| 432 | |
| 433 | if( my_ret != ref_ret ) printf( "\n%04X\n", - my_ret ); |
| 434 | |
| 435 | TEST_ASSERT( my_ret == ref_ret ); |
| 436 | |
| 437 | if( ref_ret == 0 ) |
| 438 | { |
| 439 | TEST_ASSERT( my_msg_md == (md_type_t) ref_msg_md ); |
| 440 | TEST_ASSERT( my_mgf_md == (md_type_t) ref_mgf_md ); |
| 441 | TEST_ASSERT( my_salt_len == ref_salt_len ); |
| 442 | } |
| 443 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 444 | exit: |
Manuel Pégourié-Gonnard | 8540369 | 2014-06-06 14:48:38 +0200 | [diff] [blame] | 445 | polarssl_free( params.p ); |
| 446 | } |
| 447 | /* END_CASE */ |
| 448 | |
Manuel Pégourié-Gonnard | 2014016 | 2013-10-10 12:48:03 +0200 | [diff] [blame] | 449 | /* BEGIN_CASE depends_on:POLARSSL_X509_CRT_PARSE_C:POLARSSL_SELF_TEST */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 450 | void x509_selftest() |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 451 | { |
| 452 | TEST_ASSERT( x509_self_test( 0 ) == 0 ); |
| 453 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 454 | /* END_CASE */ |