blob: 1cfa402953ef575da1f760231bdaba37188c27db [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"
Hanno Becker524f2552017-10-05 08:32:38 +01006#include "polarssl/rsa.h"
7
8#if defined(POLARSSL_RSA_C)
9int rsa_decrypt_func( void *ctx, int mode, size_t *olen,
10 const unsigned char *input, unsigned char *output,
11 size_t output_max_len )
12{
13 return( rsa_pkcs1_decrypt( (rsa_context *) ctx, NULL, NULL, mode, olen,
14 input, output, output_max_len ) );
15}
16int rsa_sign_func( void *ctx,
17 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
18 int mode, md_type_t md_alg, unsigned int hashlen,
19 const unsigned char *hash, unsigned char *sig )
20{
21 return( rsa_pkcs1_sign( (rsa_context *) ctx, f_rng, p_rng, mode,
22 md_alg, hashlen, hash, sig ) );
23}
24size_t rsa_key_len_func( void *ctx )
25{
26 return( ((const rsa_context *) ctx)->len );
27}
28#endif /* POLARSSL_RSA_C */
29
Paul Bakker33b43f12013-08-20 11:48:36 +020030/* END_HEADER */
Paul Bakker6d620502012-02-16 14:09:13 +000031
Paul Bakker33b43f12013-08-20 11:48:36 +020032/* BEGIN_DEPENDENCIES
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033 * depends_on:POLARSSL_BIGNUM_C:POLARSSL_FS_IO:POLARSSL_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +020034 * END_DEPENDENCIES
35 */
Paul Bakker6d620502012-02-16 14:09:13 +000036
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037/* BEGIN_CASE depends_on:POLARSSL_PEM_WRITE_C:POLARSSL_X509_CSR_WRITE_C */
Manuel Pégourié-Gonnardc5ce83a2014-03-28 12:46:44 +010038void x509_csr_check( char *key_file, char *cert_req_check_file,
39 int md_type, int key_usage, int cert_type )
Paul Bakker6d620502012-02-16 14:09:13 +000040{
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020041 pk_context key;
Paul Bakkercd358032013-09-09 12:08:11 +020042 x509write_csr req;
Andres AGf5276092016-09-07 11:09:44 +010043 unsigned char buf[4096];
Paul Bakker6d620502012-02-16 14:09:13 +000044 unsigned char check_buf[4000];
45 int ret;
Paul Bakker77e23fb2013-09-15 20:03:26 +020046 size_t olen = 0, pem_len = 0;
Andres AGf5276092016-09-07 11:09:44 +010047 int der_len = -1;
Paul Bakker6d620502012-02-16 14:09:13 +000048 FILE *f;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +010049 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020050 rnd_pseudo_info rnd_info;
Paul Bakker6d620502012-02-16 14:09:13 +000051
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020052 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
53
54 pk_init( &key );
Paul Bakker1a7550a2013-09-15 13:01:22 +020055 TEST_ASSERT( pk_parse_keyfile( &key, key_file, NULL ) == 0 );
Paul Bakker6d620502012-02-16 14:09:13 +000056
Paul Bakker82e29452013-08-25 11:01:31 +020057 x509write_csr_init( &req );
58 x509write_csr_set_md_alg( &req, md_type );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020059 x509write_csr_set_key( &req, &key );
Paul Bakker82e29452013-08-25 11:01:31 +020060 TEST_ASSERT( x509write_csr_set_subject_name( &req, subject_name ) == 0 );
Manuel Pégourié-Gonnardc5ce83a2014-03-28 12:46:44 +010061 if( key_usage != 0 )
62 TEST_ASSERT( x509write_csr_set_key_usage( &req, key_usage ) == 0 );
63 if( cert_type != 0 )
64 TEST_ASSERT( x509write_csr_set_ns_cert_type( &req, cert_type ) == 0 );
Paul Bakker8eabfc12013-08-25 10:18:25 +020065
Paul Bakker77e23fb2013-09-15 20:03:26 +020066 ret = x509write_csr_pem( &req, buf, sizeof(buf),
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020067 rnd_pseudo_rand, &rnd_info );
Paul Bakker77e23fb2013-09-15 20:03:26 +020068 TEST_ASSERT( ret == 0 );
Paul Bakker6d620502012-02-16 14:09:13 +000069
Paul Bakker77e23fb2013-09-15 20:03:26 +020070 pem_len = strlen( (char *) buf );
Paul Bakker6d620502012-02-16 14:09:13 +000071
Paul Bakker33b43f12013-08-20 11:48:36 +020072 f = fopen( cert_req_check_file, "r" );
Paul Bakker6d620502012-02-16 14:09:13 +000073 TEST_ASSERT( f != NULL );
Paul Bakker77e23fb2013-09-15 20:03:26 +020074 olen = fread( check_buf, 1, sizeof( check_buf ), f );
Paul Bakker6d620502012-02-16 14:09:13 +000075 fclose( f );
76
Paul Bakker77e23fb2013-09-15 20:03:26 +020077 TEST_ASSERT( olen >= pem_len - 1 );
78 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010079
Andres AGf5276092016-09-07 11:09:44 +010080 der_len = x509write_csr_der( &req, buf, sizeof( buf ),
81 rnd_pseudo_rand, &rnd_info );
82 TEST_ASSERT( der_len >= 0 );
83
84 if( der_len == 0 )
85 goto exit;
86
87 ret = x509write_csr_der( &req, buf, (size_t)( der_len - 1 ),
88 rnd_pseudo_rand, &rnd_info );
89 TEST_ASSERT( ret == POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
90
Paul Bakkerbd51b262014-07-10 15:26:12 +020091exit:
Paul Bakker82e29452013-08-25 11:01:31 +020092 x509write_csr_free( &req );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020093 pk_free( &key );
Paul Bakker6d620502012-02-16 14:09:13 +000094}
Paul Bakker33b43f12013-08-20 11:48:36 +020095/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +020096
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +010097/* BEGIN_CASE depends_on:POLARSSL_PEM_WRITE_C:POLARSSL_X509_CRT_WRITE_C:POLARSSL_SHA1_C */
Paul Bakker2397cf32013-09-08 15:58:15 +020098void x509_crt_check( char *subject_key_file, char *subject_pwd,
99 char *subject_name, char *issuer_key_file,
100 char *issuer_pwd, char *issuer_name,
101 char *serial_str, char *not_before, char *not_after,
Hanno Becker524f2552017-10-05 08:32:38 +0100102 int md_type, int key_usage, int cert_type, int auth_ident,
103 int ver, char *cert_check_file, int rsa_alt )
Paul Bakker2397cf32013-09-08 15:58:15 +0200104{
Hanno Becker524f2552017-10-05 08:32:38 +0100105 pk_context subject_key, issuer_key, issuer_key_alt;
106 pk_context *key = &issuer_key;
107
Paul Bakker2397cf32013-09-08 15:58:15 +0200108 x509write_cert crt;
Andres AGf5276092016-09-07 11:09:44 +0100109 unsigned char buf[4096];
Paul Bakker2397cf32013-09-08 15:58:15 +0200110 unsigned char check_buf[5000];
111 mpi serial;
112 int ret;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200113 size_t olen = 0, pem_len = 0;
Andres AGf5276092016-09-07 11:09:44 +0100114 int der_len = -1;
Paul Bakker2397cf32013-09-08 15:58:15 +0200115 FILE *f;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200116 rnd_pseudo_info rnd_info;
Paul Bakker2397cf32013-09-08 15:58:15 +0200117
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200118 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
Paul Bakker2397cf32013-09-08 15:58:15 +0200119 mpi_init( &serial );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200120 pk_init( &subject_key );
121 pk_init( &issuer_key );
Hanno Becker524f2552017-10-05 08:32:38 +0100122 pk_init( &issuer_key_alt );
123
124 x509write_crt_init( &crt );
Paul Bakker2397cf32013-09-08 15:58:15 +0200125
Paul Bakker1a7550a2013-09-15 13:01:22 +0200126 TEST_ASSERT( pk_parse_keyfile( &subject_key, subject_key_file,
Paul Bakker2397cf32013-09-08 15:58:15 +0200127 subject_pwd ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200128 TEST_ASSERT( pk_parse_keyfile( &issuer_key, issuer_key_file,
Paul Bakker2397cf32013-09-08 15:58:15 +0200129 issuer_pwd ) == 0 );
Hanno Becker524f2552017-10-05 08:32:38 +0100130
131 /* For RSA PK contexts, create a copy as an alternative RSA context. */
132 if( rsa_alt == 1 && pk_get_type( &issuer_key ) == POLARSSL_PK_RSA )
133 {
134 TEST_ASSERT( pk_init_ctx_rsa_alt( &issuer_key_alt,
135 pk_rsa( issuer_key ),
136 rsa_decrypt_func,
137 rsa_sign_func,
138 rsa_key_len_func ) == 0 );
139
140 key = &issuer_key_alt;
141 }
142
Paul Bakker2397cf32013-09-08 15:58:15 +0200143 TEST_ASSERT( mpi_read_string( &serial, 10, serial_str ) == 0 );
144
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100145 if( ver != -1 )
146 x509write_crt_set_version( &crt, ver );
147 TEST_ASSERT( x509write_crt_set_serial( &crt, &serial ) == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +0200148 TEST_ASSERT( x509write_crt_set_validity( &crt, not_before,
149 not_after ) == 0 );
150 x509write_crt_set_md_alg( &crt, md_type );
151 TEST_ASSERT( x509write_crt_set_issuer_name( &crt, issuer_name ) == 0 );
152 TEST_ASSERT( x509write_crt_set_subject_name( &crt, subject_name ) == 0 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200153 x509write_crt_set_subject_key( &crt, &subject_key );
Hanno Becker524f2552017-10-05 08:32:38 +0100154 x509write_crt_set_issuer_key( &crt, key );
Paul Bakker2397cf32013-09-08 15:58:15 +0200155
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100156 if( crt.version >= X509_CRT_VERSION_3 )
157 {
158 TEST_ASSERT( x509write_crt_set_basic_constraints( &crt, 0, 0 ) == 0 );
159 TEST_ASSERT( x509write_crt_set_subject_key_identifier( &crt ) == 0 );
Hanno Becker524f2552017-10-05 08:32:38 +0100160 if( auth_ident != 0 )
161 TEST_ASSERT( x509write_crt_set_authority_key_identifier( &crt ) == 0 );
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100162 if( key_usage != 0 )
163 TEST_ASSERT( x509write_crt_set_key_usage( &crt, key_usage ) == 0 );
164 if( cert_type != 0 )
165 TEST_ASSERT( x509write_crt_set_ns_cert_type( &crt, cert_type ) == 0 );
166 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200167
Hanno Beckeref4acc52017-10-05 08:35:48 +0100168 ret = x509write_crt_pem( &crt, buf, sizeof( buf ),
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200169 rnd_pseudo_rand, &rnd_info );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200170 TEST_ASSERT( ret == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +0200171
Paul Bakker77e23fb2013-09-15 20:03:26 +0200172 pem_len = strlen( (char *) buf );
Paul Bakker2397cf32013-09-08 15:58:15 +0200173
174 f = fopen( cert_check_file, "r" );
175 TEST_ASSERT( f != NULL );
Hanno Beckeref4acc52017-10-05 08:35:48 +0100176 olen = fread( check_buf, 1, sizeof( check_buf ), f );
177 TEST_ASSERT( olen < sizeof( check_buf ) );
Paul Bakker2397cf32013-09-08 15:58:15 +0200178 fclose( f );
179
Paul Bakker77e23fb2013-09-15 20:03:26 +0200180 TEST_ASSERT( olen >= pem_len - 1 );
181 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +0200182
Andres AGf5276092016-09-07 11:09:44 +0100183 der_len = x509write_crt_der( &crt, buf, sizeof( buf ),
184 rnd_pseudo_rand, &rnd_info );
185 TEST_ASSERT( der_len >= 0 );
186
187 if( der_len == 0 )
188 goto exit;
189
190 ret = x509write_crt_der( &crt, buf, (size_t)( der_len - 1 ),
191 rnd_pseudo_rand, &rnd_info );
192 TEST_ASSERT( ret == POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
193
Paul Bakkerbd51b262014-07-10 15:26:12 +0200194exit:
Paul Bakker2397cf32013-09-08 15:58:15 +0200195 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200196 pk_free( &issuer_key );
Hanno Becker524f2552017-10-05 08:32:38 +0100197 pk_free( &issuer_key_alt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200198 pk_free( &subject_key );
Paul Bakker2397cf32013-09-08 15:58:15 +0200199 mpi_free( &serial );
200}
201/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200202
203/* BEGIN_CASE depends_on:POLARSSL_X509_CREATE_C:POLARSSL_X509_USE_C */
204void x509_string_to_names( char *name, char *parsed_name, int result )
205{
206 int ret;
207 size_t len = 0;
208 asn1_named_data *names = NULL;
209 x509_name parsed, *parsed_cur, *parsed_prv;
210 unsigned char buf[2048], *c;
211
212 memset( &parsed, 0, sizeof( parsed ) );
213 memset( buf, 0, sizeof( buf ) );
214 c = buf + sizeof( buf );
215
216 ret = x509_string_to_names( &names, name );
217 TEST_ASSERT( ret == result );
218
219 if( ret != 0 )
220 goto exit;
221
222 ret = x509_write_names( &c, buf, names );
223 TEST_ASSERT( ret > 0 );
224
225 TEST_ASSERT( asn1_get_tag( &c, buf + sizeof( buf ), &len,
226 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) == 0 );
227 TEST_ASSERT( x509_get_name( &c, buf + sizeof( buf ), &parsed ) == 0 );
228
229 ret = x509_dn_gets( (char *) buf, sizeof( buf ), &parsed );
230 TEST_ASSERT( ret > 0 );
231
232 TEST_ASSERT( strcmp( (char *) buf, parsed_name ) == 0 );
233
234exit:
235 asn1_free_named_data_list( &names );
236
237 parsed_cur = parsed.next;
238 while( parsed_cur != 0 )
239 {
240 parsed_prv = parsed_cur;
241 parsed_cur = parsed_cur->next;
242 polarssl_free( parsed_prv );
243 }
244}
245/* END_CASE */