blob: 10338aa715e8af7cde7ff3a4d89758e4216e97a9 [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{
17 rsa_context rsa;
18 pem_context pem;
Paul Bakker82e29452013-08-25 11:01:31 +020019 x509_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;
24 size_t olen = 2000;
25 FILE *f;
Paul Bakker21307962013-08-25 10:33:27 +020026 char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Paul Bakker6d620502012-02-16 14:09:13 +000027
28 memset( &rsa, 0, sizeof(rsa_context) );
Paul Bakker33b43f12013-08-20 11:48:36 +020029 ret = x509parse_keyfile_rsa( &rsa, key_file, NULL );
Paul Bakker6d620502012-02-16 14:09:13 +000030 TEST_ASSERT( ret == 0 );
31 if( ret != 0 )
Paul Bakker33b43f12013-08-20 11:48:36 +020032 return;
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 );
36 x509write_csr_set_rsa_key( &req, &rsa );
37 TEST_ASSERT( x509write_csr_set_subject_name( &req, subject_name ) == 0 );
Paul Bakker8eabfc12013-08-25 10:18:25 +020038
Paul Bakker82e29452013-08-25 11:01:31 +020039 ret = x509write_csr_der( &req, buf, 4000 );
Paul Bakker6d620502012-02-16 14:09:13 +000040 TEST_ASSERT( ret >= 0 );
41
42 c = buf + 3999 - ret;
43
Paul Bakker33b43f12013-08-20 11:48:36 +020044 f = fopen( cert_req_check_file, "r" );
Paul Bakker6d620502012-02-16 14:09:13 +000045 TEST_ASSERT( f != NULL );
46 fread( check_buf, 1, 4000, f );
47 fclose( f );
48
49 pem_init( &pem );
50 pem_read_buffer( &pem, "-----BEGIN CERTIFICATE REQUEST-----", "-----END CERTIFICATE REQUEST-----", check_buf, NULL, 0, &olen );
51
52 TEST_ASSERT( memcmp( c, pem.buf, pem.buflen ) == 0 );
53 TEST_ASSERT( pem.buflen == (size_t) ret );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010054
Paul Bakker82e29452013-08-25 11:01:31 +020055 x509write_csr_free( &req );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010056 rsa_free( &rsa );
57 pem_free( &pem );
Paul Bakker6d620502012-02-16 14:09:13 +000058}
Paul Bakker33b43f12013-08-20 11:48:36 +020059/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +020060
61/* BEGIN_CASE */
62void x509_crt_check( char *subject_key_file, char *subject_pwd,
63 char *subject_name, char *issuer_key_file,
64 char *issuer_pwd, char *issuer_name,
65 char *serial_str, char *not_before, char *not_after,
66 int md_type, char *cert_check_file )
67{
68 rsa_context subject_rsa, issuer_rsa;
69 pem_context pem;
70 x509write_cert crt;
71 unsigned char *c;
72 unsigned char buf[4000];
73 unsigned char check_buf[5000];
74 mpi serial;
75 int ret;
76 size_t olen = 2000;
77 FILE *f;
78
79 mpi_init( &serial );
80 rsa_init( &subject_rsa, RSA_PKCS_V15, 0 );
81 rsa_init( &issuer_rsa, RSA_PKCS_V15, 0 );
82
83 TEST_ASSERT( x509parse_keyfile_rsa( &subject_rsa, subject_key_file,
84 subject_pwd ) == 0 );
85 TEST_ASSERT( x509parse_keyfile_rsa( &issuer_rsa, issuer_key_file,
86 issuer_pwd ) == 0 );
87 TEST_ASSERT( mpi_read_string( &serial, 10, serial_str ) == 0 );
88
89 x509write_crt_init( &crt );
90 x509write_crt_set_serial( &crt, &serial );
91 TEST_ASSERT( x509write_crt_set_validity( &crt, not_before,
92 not_after ) == 0 );
93 x509write_crt_set_md_alg( &crt, md_type );
94 TEST_ASSERT( x509write_crt_set_issuer_name( &crt, issuer_name ) == 0 );
95 TEST_ASSERT( x509write_crt_set_subject_name( &crt, subject_name ) == 0 );
96 x509write_crt_set_subject_key( &crt, &subject_rsa );
97 x509write_crt_set_issuer_key( &crt, &issuer_rsa );
98
99 TEST_ASSERT( x509write_crt_set_basic_constraints( &crt, 0, 0 ) == 0 );
100 TEST_ASSERT( x509write_crt_set_subject_key_identifier( &crt ) == 0 );
101 TEST_ASSERT( x509write_crt_set_authority_key_identifier( &crt ) == 0 );
102
103 ret = x509write_crt_der( &crt, buf, sizeof(buf) );
104 TEST_ASSERT( ret >= 0 );
105
106 c = buf + 3999 - ret;
107
108 f = fopen( cert_check_file, "r" );
109 TEST_ASSERT( f != NULL );
110 TEST_ASSERT( fread( check_buf, 1, sizeof(check_buf), f ) < sizeof(check_buf) );
111 fclose( f );
112
113 pem_init( &pem );
114 TEST_ASSERT( pem_read_buffer( &pem, "-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----", check_buf, NULL, 0, &olen ) >= 0 );
115
116 TEST_ASSERT( pem.buflen == (size_t) ret );
117 TEST_ASSERT( memcmp( c, pem.buf, pem.buflen ) == 0 );
118
119 x509write_crt_free( &crt );
120 rsa_free( &issuer_rsa );
121 rsa_free( &subject_rsa );
122 pem_free( &pem );
123 mpi_free( &serial );
124}
125/* END_CASE */