blob: 68c7b1c9d07e6e90b200bcd1d052782a6d5afd6f [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +00002#include <polarssl/x509write.h>
3#include <polarssl/x509.h>
4#include <polarssl/pem.h>
Paul Bakkerc70b9822013-04-07 22:00:46 +02005#include <polarssl/oid.h>
Paul Bakker33b43f12013-08-20 11:48:36 +02006/* END_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +00007
Paul Bakker33b43f12013-08-20 11:48:36 +02008/* BEGIN_DEPENDENCIES
9 * depends_on:POLARSSL_X509_WRITE_C:POLARSSL_BIGNUM_C
10 * END_DEPENDENCIES
11 */
Paul Bakker6d620502012-02-16 14:09:13 +000012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* BEGIN_CASE */
Paul Bakker82e29452013-08-25 11:01:31 +020014void x509_csr_check( char *key_file, int md_type,
Paul Bakker33b43f12013-08-20 11:48:36 +020015 char *cert_req_check_file )
Paul Bakker6d620502012-02-16 14:09:13 +000016{
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020017 pk_context key;
Paul Bakker6d620502012-02-16 14:09:13 +000018 pem_context pem;
Paul Bakkercd358032013-09-09 12:08:11 +020019 x509write_csr req;
Paul Bakker6d620502012-02-16 14:09:13 +000020 unsigned char *c;
21 unsigned char buf[4000];
22 unsigned char check_buf[4000];
23 int ret;
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +020024 size_t olen = sizeof( check_buf );
Paul Bakker6d620502012-02-16 14:09:13 +000025 FILE *f;
Paul Bakker21307962013-08-25 10:33:27 +020026 char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020027 rnd_pseudo_info rnd_info;
Paul Bakker6d620502012-02-16 14:09:13 +000028
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020029 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
30
31 pk_init( &key );
Paul Bakker1a7550a2013-09-15 13:01:22 +020032 TEST_ASSERT( pk_parse_keyfile( &key, key_file, NULL ) == 0 );
Paul Bakker6d620502012-02-16 14:09:13 +000033
Paul Bakker82e29452013-08-25 11:01:31 +020034 x509write_csr_init( &req );
35 x509write_csr_set_md_alg( &req, md_type );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020036 x509write_csr_set_key( &req, &key );
Paul Bakker82e29452013-08-25 11:01:31 +020037 TEST_ASSERT( x509write_csr_set_subject_name( &req, subject_name ) == 0 );
Paul Bakker8eabfc12013-08-25 10:18:25 +020038
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020039 ret = x509write_csr_der( &req, buf, sizeof( buf ),
40 rnd_pseudo_rand, &rnd_info );
Paul Bakker6d620502012-02-16 14:09:13 +000041 TEST_ASSERT( ret >= 0 );
42
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +020043 c = buf + sizeof( buf ) - ret;
Paul Bakker6d620502012-02-16 14:09:13 +000044
Paul Bakker33b43f12013-08-20 11:48:36 +020045 f = fopen( cert_req_check_file, "r" );
Paul Bakker6d620502012-02-16 14:09:13 +000046 TEST_ASSERT( f != NULL );
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +020047 fread( check_buf, 1, sizeof( check_buf ), f );
Paul Bakker6d620502012-02-16 14:09:13 +000048 fclose( f );
49
50 pem_init( &pem );
51 pem_read_buffer( &pem, "-----BEGIN CERTIFICATE REQUEST-----", "-----END CERTIFICATE REQUEST-----", check_buf, NULL, 0, &olen );
52
Paul Bakker6d620502012-02-16 14:09:13 +000053 TEST_ASSERT( pem.buflen == (size_t) ret );
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +020054 TEST_ASSERT( memcmp( c, pem.buf, pem.buflen ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010055
Paul Bakker82e29452013-08-25 11:01:31 +020056 x509write_csr_free( &req );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010057 pem_free( &pem );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020058 pk_free( &key );
Paul Bakker6d620502012-02-16 14:09:13 +000059}
Paul Bakker33b43f12013-08-20 11:48:36 +020060/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +020061
62/* BEGIN_CASE */
63void x509_crt_check( char *subject_key_file, char *subject_pwd,
64 char *subject_name, char *issuer_key_file,
65 char *issuer_pwd, char *issuer_name,
66 char *serial_str, char *not_before, char *not_after,
67 int md_type, char *cert_check_file )
68{
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +020069 pk_context subject_key, issuer_key;
Paul Bakker2397cf32013-09-08 15:58:15 +020070 pem_context pem;
71 x509write_cert crt;
72 unsigned char *c;
73 unsigned char buf[4000];
74 unsigned char check_buf[5000];
75 mpi serial;
76 int ret;
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +020077 size_t olen = sizeof( check_buf );
Paul Bakker2397cf32013-09-08 15:58:15 +020078 FILE *f;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020079 rnd_pseudo_info rnd_info;
Paul Bakker2397cf32013-09-08 15:58:15 +020080
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020081 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
Paul Bakker2397cf32013-09-08 15:58:15 +020082 mpi_init( &serial );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +020083 pk_init( &subject_key );
84 pk_init( &issuer_key );
Paul Bakker2397cf32013-09-08 15:58:15 +020085
Paul Bakker1a7550a2013-09-15 13:01:22 +020086 TEST_ASSERT( pk_parse_keyfile( &subject_key, subject_key_file,
Paul Bakker2397cf32013-09-08 15:58:15 +020087 subject_pwd ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020088 TEST_ASSERT( pk_parse_keyfile( &issuer_key, issuer_key_file,
Paul Bakker2397cf32013-09-08 15:58:15 +020089 issuer_pwd ) == 0 );
90 TEST_ASSERT( mpi_read_string( &serial, 10, serial_str ) == 0 );
91
92 x509write_crt_init( &crt );
93 x509write_crt_set_serial( &crt, &serial );
94 TEST_ASSERT( x509write_crt_set_validity( &crt, not_before,
95 not_after ) == 0 );
96 x509write_crt_set_md_alg( &crt, md_type );
97 TEST_ASSERT( x509write_crt_set_issuer_name( &crt, issuer_name ) == 0 );
98 TEST_ASSERT( x509write_crt_set_subject_name( &crt, subject_name ) == 0 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +020099 x509write_crt_set_subject_key( &crt, &subject_key );
100 x509write_crt_set_issuer_key( &crt, &issuer_key );
Paul Bakker2397cf32013-09-08 15:58:15 +0200101
102 TEST_ASSERT( x509write_crt_set_basic_constraints( &crt, 0, 0 ) == 0 );
103 TEST_ASSERT( x509write_crt_set_subject_key_identifier( &crt ) == 0 );
104 TEST_ASSERT( x509write_crt_set_authority_key_identifier( &crt ) == 0 );
105
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200106 ret = x509write_crt_der( &crt, buf, sizeof(buf),
107 rnd_pseudo_rand, &rnd_info );
Paul Bakker2397cf32013-09-08 15:58:15 +0200108 TEST_ASSERT( ret >= 0 );
109
Manuel Pégourié-Gonnard27d87fa2013-09-11 17:33:28 +0200110 c = buf + sizeof( buf ) - ret;
Paul Bakker2397cf32013-09-08 15:58:15 +0200111
112 f = fopen( cert_check_file, "r" );
113 TEST_ASSERT( f != NULL );
114 TEST_ASSERT( fread( check_buf, 1, sizeof(check_buf), f ) < sizeof(check_buf) );
115 fclose( f );
116
117 pem_init( &pem );
118 TEST_ASSERT( pem_read_buffer( &pem, "-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----", check_buf, NULL, 0, &olen ) >= 0 );
119
120 TEST_ASSERT( pem.buflen == (size_t) ret );
121 TEST_ASSERT( memcmp( c, pem.buf, pem.buflen ) == 0 );
122
123 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200124 pk_free( &issuer_key );
125 pk_free( &subject_key );
Paul Bakker2397cf32013-09-08 15:58:15 +0200126 pem_free( &pem );
127 mpi_free( &serial );
128}
129/* END_CASE */