blob: 825a59313c3a3ea74dccd9914e609ef92ef3e767 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Rich Evansce2f2372015-02-06 13:57:42 +00002#include "polarssl/x509_crt.h"
3#include "polarssl/x509_csr.h"
4#include "polarssl/pem.h"
5#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
Paul Bakker7c6b2c32013-09-16 13:49:26 +02009 * depends_on:POLARSSL_BIGNUM_C:POLARSSL_FS_IO:POLARSSL_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +020010 * END_DEPENDENCIES
11 */
Paul Bakker6d620502012-02-16 14:09:13 +000012
Paul Bakker7c6b2c32013-09-16 13:49:26 +020013/* BEGIN_CASE depends_on:POLARSSL_PEM_WRITE_C:POLARSSL_X509_CSR_WRITE_C */
Manuel Pégourié-Gonnardc5ce83a2014-03-28 12:46:44 +010014void x509_csr_check( char *key_file, char *cert_req_check_file,
15 int md_type, int key_usage, int cert_type )
Paul Bakker6d620502012-02-16 14:09:13 +000016{
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020017 pk_context key;
Paul Bakkercd358032013-09-09 12:08:11 +020018 x509write_csr req;
Andres AGf5276092016-09-07 11:09:44 +010019 unsigned char buf[4096];
Paul Bakker6d620502012-02-16 14:09:13 +000020 unsigned char check_buf[4000];
21 int ret;
Paul Bakker77e23fb2013-09-15 20:03:26 +020022 size_t olen = 0, pem_len = 0;
Andres AGf5276092016-09-07 11:09:44 +010023 int der_len = -1;
Paul Bakker6d620502012-02-16 14:09:13 +000024 FILE *f;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +010025 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020026 rnd_pseudo_info rnd_info;
Paul Bakker6d620502012-02-16 14:09:13 +000027
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020028 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
29
30 pk_init( &key );
Paul Bakker1a7550a2013-09-15 13:01:22 +020031 TEST_ASSERT( pk_parse_keyfile( &key, key_file, NULL ) == 0 );
Paul Bakker6d620502012-02-16 14:09:13 +000032
Paul Bakker82e29452013-08-25 11:01:31 +020033 x509write_csr_init( &req );
34 x509write_csr_set_md_alg( &req, md_type );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020035 x509write_csr_set_key( &req, &key );
Paul Bakker82e29452013-08-25 11:01:31 +020036 TEST_ASSERT( x509write_csr_set_subject_name( &req, subject_name ) == 0 );
Manuel Pégourié-Gonnardc5ce83a2014-03-28 12:46:44 +010037 if( key_usage != 0 )
38 TEST_ASSERT( x509write_csr_set_key_usage( &req, key_usage ) == 0 );
39 if( cert_type != 0 )
40 TEST_ASSERT( x509write_csr_set_ns_cert_type( &req, cert_type ) == 0 );
Paul Bakker8eabfc12013-08-25 10:18:25 +020041
Paul Bakker77e23fb2013-09-15 20:03:26 +020042 ret = x509write_csr_pem( &req, buf, sizeof(buf),
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020043 rnd_pseudo_rand, &rnd_info );
Paul Bakker77e23fb2013-09-15 20:03:26 +020044 TEST_ASSERT( ret == 0 );
Paul Bakker6d620502012-02-16 14:09:13 +000045
Paul Bakker77e23fb2013-09-15 20:03:26 +020046 pem_len = strlen( (char *) buf );
Paul Bakker6d620502012-02-16 14:09:13 +000047
Paul Bakker33b43f12013-08-20 11:48:36 +020048 f = fopen( cert_req_check_file, "r" );
Paul Bakker6d620502012-02-16 14:09:13 +000049 TEST_ASSERT( f != NULL );
Paul Bakker77e23fb2013-09-15 20:03:26 +020050 olen = fread( check_buf, 1, sizeof( check_buf ), f );
Paul Bakker6d620502012-02-16 14:09:13 +000051 fclose( f );
52
Paul Bakker77e23fb2013-09-15 20:03:26 +020053 TEST_ASSERT( olen >= pem_len - 1 );
54 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010055
Andres AGf5276092016-09-07 11:09:44 +010056 der_len = x509write_csr_der( &req, buf, sizeof( buf ),
57 rnd_pseudo_rand, &rnd_info );
58 TEST_ASSERT( der_len >= 0 );
59
60 if( der_len == 0 )
61 goto exit;
62
63 ret = x509write_csr_der( &req, buf, (size_t)( der_len - 1 ),
64 rnd_pseudo_rand, &rnd_info );
65 TEST_ASSERT( ret == POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
66
Paul Bakkerbd51b262014-07-10 15:26:12 +020067exit:
Paul Bakker82e29452013-08-25 11:01:31 +020068 x509write_csr_free( &req );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020069 pk_free( &key );
Paul Bakker6d620502012-02-16 14:09:13 +000070}
Paul Bakker33b43f12013-08-20 11:48:36 +020071/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +020072
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +010073/* BEGIN_CASE depends_on:POLARSSL_PEM_WRITE_C:POLARSSL_X509_CRT_WRITE_C:POLARSSL_SHA1_C */
Paul Bakker2397cf32013-09-08 15:58:15 +020074void x509_crt_check( char *subject_key_file, char *subject_pwd,
75 char *subject_name, char *issuer_key_file,
76 char *issuer_pwd, char *issuer_name,
77 char *serial_str, char *not_before, char *not_after,
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +010078 int md_type, int key_usage, int cert_type, int ver,
79 char *cert_check_file )
Paul Bakker2397cf32013-09-08 15:58:15 +020080{
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +020081 pk_context subject_key, issuer_key;
Paul Bakker2397cf32013-09-08 15:58:15 +020082 x509write_cert crt;
Andres AGf5276092016-09-07 11:09:44 +010083 unsigned char buf[4096];
Paul Bakker2397cf32013-09-08 15:58:15 +020084 unsigned char check_buf[5000];
85 mpi serial;
86 int ret;
Paul Bakker77e23fb2013-09-15 20:03:26 +020087 size_t olen = 0, pem_len = 0;
Andres AGf5276092016-09-07 11:09:44 +010088 int der_len = -1;
Paul Bakker2397cf32013-09-08 15:58:15 +020089 FILE *f;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020090 rnd_pseudo_info rnd_info;
Paul Bakker2397cf32013-09-08 15:58:15 +020091
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020092 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
Paul Bakker2397cf32013-09-08 15:58:15 +020093 mpi_init( &serial );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +020094 pk_init( &subject_key );
95 pk_init( &issuer_key );
Paul Bakker2397cf32013-09-08 15:58:15 +020096
Paul Bakker1a7550a2013-09-15 13:01:22 +020097 TEST_ASSERT( pk_parse_keyfile( &subject_key, subject_key_file,
Paul Bakker2397cf32013-09-08 15:58:15 +020098 subject_pwd ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020099 TEST_ASSERT( pk_parse_keyfile( &issuer_key, issuer_key_file,
Paul Bakker2397cf32013-09-08 15:58:15 +0200100 issuer_pwd ) == 0 );
101 TEST_ASSERT( mpi_read_string( &serial, 10, serial_str ) == 0 );
102
103 x509write_crt_init( &crt );
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100104 if( ver != -1 )
105 x509write_crt_set_version( &crt, ver );
106 TEST_ASSERT( x509write_crt_set_serial( &crt, &serial ) == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +0200107 TEST_ASSERT( x509write_crt_set_validity( &crt, not_before,
108 not_after ) == 0 );
109 x509write_crt_set_md_alg( &crt, md_type );
110 TEST_ASSERT( x509write_crt_set_issuer_name( &crt, issuer_name ) == 0 );
111 TEST_ASSERT( x509write_crt_set_subject_name( &crt, subject_name ) == 0 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200112 x509write_crt_set_subject_key( &crt, &subject_key );
113 x509write_crt_set_issuer_key( &crt, &issuer_key );
Paul Bakker2397cf32013-09-08 15:58:15 +0200114
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100115 if( crt.version >= X509_CRT_VERSION_3 )
116 {
117 TEST_ASSERT( x509write_crt_set_basic_constraints( &crt, 0, 0 ) == 0 );
118 TEST_ASSERT( x509write_crt_set_subject_key_identifier( &crt ) == 0 );
119 TEST_ASSERT( x509write_crt_set_authority_key_identifier( &crt ) == 0 );
120 if( key_usage != 0 )
121 TEST_ASSERT( x509write_crt_set_key_usage( &crt, key_usage ) == 0 );
122 if( cert_type != 0 )
123 TEST_ASSERT( x509write_crt_set_ns_cert_type( &crt, cert_type ) == 0 );
124 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200125
Paul Bakker77e23fb2013-09-15 20:03:26 +0200126 ret = x509write_crt_pem( &crt, buf, sizeof(buf),
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200127 rnd_pseudo_rand, &rnd_info );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200128 TEST_ASSERT( ret == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +0200129
Paul Bakker77e23fb2013-09-15 20:03:26 +0200130 pem_len = strlen( (char *) buf );
Paul Bakker2397cf32013-09-08 15:58:15 +0200131
132 f = fopen( cert_check_file, "r" );
133 TEST_ASSERT( f != NULL );
Paul Bakker94b916c2014-04-17 16:07:20 +0200134 olen = fread( check_buf, 1, sizeof(check_buf), f );
135 TEST_ASSERT( olen < sizeof(check_buf) );
Paul Bakker2397cf32013-09-08 15:58:15 +0200136 fclose( f );
137
Paul Bakker77e23fb2013-09-15 20:03:26 +0200138 TEST_ASSERT( olen >= pem_len - 1 );
139 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +0200140
Andres AGf5276092016-09-07 11:09:44 +0100141 der_len = x509write_crt_der( &crt, buf, sizeof( buf ),
142 rnd_pseudo_rand, &rnd_info );
143 TEST_ASSERT( der_len >= 0 );
144
145 if( der_len == 0 )
146 goto exit;
147
148 ret = x509write_crt_der( &crt, buf, (size_t)( der_len - 1 ),
149 rnd_pseudo_rand, &rnd_info );
150 TEST_ASSERT( ret == POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
151
Paul Bakkerbd51b262014-07-10 15:26:12 +0200152exit:
Paul Bakker2397cf32013-09-08 15:58:15 +0200153 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200154 pk_free( &issuer_key );
155 pk_free( &subject_key );
Paul Bakker2397cf32013-09-08 15:58:15 +0200156 mpi_free( &serial );
157}
158/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200159
160/* BEGIN_CASE depends_on:POLARSSL_X509_CREATE_C:POLARSSL_X509_USE_C */
161void x509_string_to_names( char *name, char *parsed_name, int result )
162{
163 int ret;
164 size_t len = 0;
165 asn1_named_data *names = NULL;
166 x509_name parsed, *parsed_cur, *parsed_prv;
167 unsigned char buf[2048], *c;
168
169 memset( &parsed, 0, sizeof( parsed ) );
170 memset( buf, 0, sizeof( buf ) );
171 c = buf + sizeof( buf );
172
173 ret = x509_string_to_names( &names, name );
174 TEST_ASSERT( ret == result );
175
176 if( ret != 0 )
177 goto exit;
178
179 ret = x509_write_names( &c, buf, names );
180 TEST_ASSERT( ret > 0 );
181
182 TEST_ASSERT( asn1_get_tag( &c, buf + sizeof( buf ), &len,
183 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) == 0 );
184 TEST_ASSERT( x509_get_name( &c, buf + sizeof( buf ), &parsed ) == 0 );
185
186 ret = x509_dn_gets( (char *) buf, sizeof( buf ), &parsed );
187 TEST_ASSERT( ret > 0 );
188
189 TEST_ASSERT( strcmp( (char *) buf, parsed_name ) == 0 );
190
191exit:
192 asn1_free_named_data_list( &names );
193
194 parsed_cur = parsed.next;
195 while( parsed_cur != 0 )
196 {
197 parsed_prv = parsed_cur;
198 parsed_cur = parsed_cur->next;
199 polarssl_free( parsed_prv );
200 }
201}
202/* END_CASE */