blob: 6a66b2fceadfbad6556d7a6a21c381b4ff986ed3 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/x509_crt.h"
3#include "mbedtls/x509_csr.h"
4#include "mbedtls/pem.h"
5#include "mbedtls/oid.h"
Hanno Becker2b6c3f62017-09-14 07:51:28 +01006#include "mbedtls/rsa.h"
7
8#if defined(MBEDTLS_RSA_C)
9int mbedtls_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( mbedtls_rsa_pkcs1_decrypt( (mbedtls_rsa_context *) ctx, NULL, NULL, mode, olen,
14 input, output, output_max_len ) );
15}
16int mbedtls_rsa_sign_func( void *ctx,
17 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
18 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
19 const unsigned char *hash, unsigned char *sig )
20{
21 return( mbedtls_rsa_pkcs1_sign( (mbedtls_rsa_context *) ctx, f_rng, p_rng, mode,
22 md_alg, hashlen, hash, sig ) );
23}
24size_t mbedtls_rsa_key_len_func( void *ctx )
25{
26 return( ((const mbedtls_rsa_context *) ctx)->len );
27}
28#endif /* MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 * depends_on:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +020034 * END_DEPENDENCIES
35 */
Paul Bakker6d620502012-02-16 14:09:13 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 mbedtls_pk_context key;
42 mbedtls_x509write_csr req;
Paul Bakker6d620502012-02-16 14:09:13 +000043 unsigned char buf[4000];
44 unsigned char check_buf[4000];
45 int ret;
Paul Bakker77e23fb2013-09-15 20:03:26 +020046 size_t olen = 0, pem_len = 0;
Paul Bakker6d620502012-02-16 14:09:13 +000047 FILE *f;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +010048 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020049 rnd_pseudo_info rnd_info;
Paul Bakker6d620502012-02-16 14:09:13 +000050
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020051 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 mbedtls_pk_init( &key );
54 TEST_ASSERT( mbedtls_pk_parse_keyfile( &key, key_file, NULL ) == 0 );
Paul Bakker6d620502012-02-16 14:09:13 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_x509write_csr_init( &req );
57 mbedtls_x509write_csr_set_md_alg( &req, md_type );
58 mbedtls_x509write_csr_set_key( &req, &key );
59 TEST_ASSERT( mbedtls_x509write_csr_set_subject_name( &req, subject_name ) == 0 );
Manuel Pégourié-Gonnardc5ce83a2014-03-28 12:46:44 +010060 if( key_usage != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 TEST_ASSERT( mbedtls_x509write_csr_set_key_usage( &req, key_usage ) == 0 );
Manuel Pégourié-Gonnardc5ce83a2014-03-28 12:46:44 +010062 if( cert_type != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 TEST_ASSERT( mbedtls_x509write_csr_set_ns_cert_type( &req, cert_type ) == 0 );
Paul Bakker8eabfc12013-08-25 10:18:25 +020064
Hanno Becker7de3ff32017-09-13 15:39:59 +010065 ret = mbedtls_x509write_csr_pem( &req, buf, sizeof( buf ),
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020066 rnd_pseudo_rand, &rnd_info );
Paul Bakker77e23fb2013-09-15 20:03:26 +020067 TEST_ASSERT( ret == 0 );
Paul Bakker6d620502012-02-16 14:09:13 +000068
Paul Bakker77e23fb2013-09-15 20:03:26 +020069 pem_len = strlen( (char *) buf );
Paul Bakker6d620502012-02-16 14:09:13 +000070
Paul Bakker33b43f12013-08-20 11:48:36 +020071 f = fopen( cert_req_check_file, "r" );
Paul Bakker6d620502012-02-16 14:09:13 +000072 TEST_ASSERT( f != NULL );
Paul Bakker77e23fb2013-09-15 20:03:26 +020073 olen = fread( check_buf, 1, sizeof( check_buf ), f );
Paul Bakker6d620502012-02-16 14:09:13 +000074 fclose( f );
75
Paul Bakker77e23fb2013-09-15 20:03:26 +020076 TEST_ASSERT( olen >= pem_len - 1 );
77 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010078
Andres AGeffb5582016-09-07 11:09:44 +010079 ret = mbedtls_x509write_csr_der( &req, buf, pem_len / 2,
80 rnd_pseudo_rand, &rnd_info );
81 TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
82
Paul Bakkerbd51b262014-07-10 15:26:12 +020083exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_x509write_csr_free( &req );
85 mbedtls_pk_free( &key );
Paul Bakker6d620502012-02-16 14:09:13 +000086}
Paul Bakker33b43f12013-08-20 11:48:36 +020087/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +020088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CRT_WRITE_C:MBEDTLS_SHA1_C */
Paul Bakker2397cf32013-09-08 15:58:15 +020090void x509_crt_check( char *subject_key_file, char *subject_pwd,
91 char *subject_name, char *issuer_key_file,
92 char *issuer_pwd, char *issuer_name,
93 char *serial_str, char *not_before, char *not_after,
Hanno Becker2b6c3f62017-09-14 07:51:28 +010094 int md_type, int key_usage, int cert_type, int auth_ident,
95 int ver, char *cert_check_file, int rsa_alt )
Paul Bakker2397cf32013-09-08 15:58:15 +020096{
Hanno Becker2b6c3f62017-09-14 07:51:28 +010097 mbedtls_pk_context subject_key, issuer_key, issuer_key_alt;
98 mbedtls_pk_context *key = &issuer_key;
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_x509write_cert crt;
Paul Bakker2397cf32013-09-08 15:58:15 +0200101 unsigned char buf[4000];
102 unsigned char check_buf[5000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_mpi serial;
Paul Bakker2397cf32013-09-08 15:58:15 +0200104 int ret;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200105 size_t olen = 0, pem_len = 0;
Paul Bakker2397cf32013-09-08 15:58:15 +0200106 FILE *f;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200107 rnd_pseudo_info rnd_info;
Paul Bakker2397cf32013-09-08 15:58:15 +0200108
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200109 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_mpi_init( &serial );
Hanno Becker2b6c3f62017-09-14 07:51:28 +0100111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_pk_init( &subject_key );
Hanno Becker2b6c3f62017-09-14 07:51:28 +0100113 mbedtls_pk_init( &issuer_key );
114 mbedtls_pk_init( &issuer_key_alt );
115
116 mbedtls_x509write_crt_init( &crt );
Paul Bakker2397cf32013-09-08 15:58:15 +0200117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 TEST_ASSERT( mbedtls_pk_parse_keyfile( &subject_key, subject_key_file,
Paul Bakker2397cf32013-09-08 15:58:15 +0200119 subject_pwd ) == 0 );
Hanno Becker2b6c3f62017-09-14 07:51:28 +0100120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 TEST_ASSERT( mbedtls_pk_parse_keyfile( &issuer_key, issuer_key_file,
Paul Bakker2397cf32013-09-08 15:58:15 +0200122 issuer_pwd ) == 0 );
Hanno Becker2b6c3f62017-09-14 07:51:28 +0100123
124 /* For RSA PK contexts, create a copy as an alternative RSA context. */
125 if( rsa_alt == 1 && mbedtls_pk_get_type( &issuer_key ) == MBEDTLS_PK_RSA )
126 {
127 TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &issuer_key_alt,
128 mbedtls_pk_rsa( issuer_key ),
129 mbedtls_rsa_decrypt_func,
130 mbedtls_rsa_sign_func,
131 mbedtls_rsa_key_len_func ) == 0 );
132
133 key = &issuer_key_alt;
134 }
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 TEST_ASSERT( mbedtls_mpi_read_string( &serial, 10, serial_str ) == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +0200137
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100138 if( ver != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_x509write_crt_set_version( &crt, ver );
Hanno Becker2b6c3f62017-09-14 07:51:28 +0100140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 TEST_ASSERT( mbedtls_x509write_crt_set_serial( &crt, &serial ) == 0 );
142 TEST_ASSERT( mbedtls_x509write_crt_set_validity( &crt, not_before,
Hanno Becker7de3ff32017-09-13 15:39:59 +0100143 not_after ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_x509write_crt_set_md_alg( &crt, md_type );
145 TEST_ASSERT( mbedtls_x509write_crt_set_issuer_name( &crt, issuer_name ) == 0 );
146 TEST_ASSERT( mbedtls_x509write_crt_set_subject_name( &crt, subject_name ) == 0 );
147 mbedtls_x509write_crt_set_subject_key( &crt, &subject_key );
Hanno Becker2b6c3f62017-09-14 07:51:28 +0100148
149 mbedtls_x509write_crt_set_issuer_key( &crt, key );
Paul Bakker2397cf32013-09-08 15:58:15 +0200150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 if( crt.version >= MBEDTLS_X509_CRT_VERSION_3 )
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 TEST_ASSERT( mbedtls_x509write_crt_set_basic_constraints( &crt, 0, 0 ) == 0 );
154 TEST_ASSERT( mbedtls_x509write_crt_set_subject_key_identifier( &crt ) == 0 );
Hanno Becker2b6c3f62017-09-14 07:51:28 +0100155 if( auth_ident )
156 TEST_ASSERT( mbedtls_x509write_crt_set_authority_key_identifier( &crt ) == 0 );
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100157 if( key_usage != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 TEST_ASSERT( mbedtls_x509write_crt_set_key_usage( &crt, key_usage ) == 0 );
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100159 if( cert_type != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 TEST_ASSERT( mbedtls_x509write_crt_set_ns_cert_type( &crt, cert_type ) == 0 );
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100161 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200162
Hanno Becker7de3ff32017-09-13 15:39:59 +0100163 ret = mbedtls_x509write_crt_pem( &crt, buf, sizeof( buf ),
164 rnd_pseudo_rand, &rnd_info );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200165 TEST_ASSERT( ret == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +0200166
Paul Bakker77e23fb2013-09-15 20:03:26 +0200167 pem_len = strlen( (char *) buf );
Paul Bakker2397cf32013-09-08 15:58:15 +0200168
169 f = fopen( cert_check_file, "r" );
170 TEST_ASSERT( f != NULL );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100171 olen = fread( check_buf, 1, sizeof( check_buf ), f );
Paul Bakker2397cf32013-09-08 15:58:15 +0200172 fclose( f );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100173 TEST_ASSERT( olen < sizeof( check_buf ) );
Paul Bakker2397cf32013-09-08 15:58:15 +0200174
Paul Bakker77e23fb2013-09-15 20:03:26 +0200175 TEST_ASSERT( olen >= pem_len - 1 );
176 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +0200177
Andres AGeffb5582016-09-07 11:09:44 +0100178 ret = mbedtls_x509write_crt_der( &crt, buf, pem_len / 2,
179 rnd_pseudo_rand, &rnd_info );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100180
Andres AGeffb5582016-09-07 11:09:44 +0100181 TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
182
Paul Bakkerbd51b262014-07-10 15:26:12 +0200183exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_x509write_crt_free( &crt );
Hanno Becker2b6c3f62017-09-14 07:51:28 +0100185 mbedtls_pk_free( &issuer_key_alt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_pk_free( &subject_key );
Hanno Becker2b6c3f62017-09-14 07:51:28 +0100187 mbedtls_pk_free( &issuer_key );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_mpi_free( &serial );
Paul Bakker2397cf32013-09-08 15:58:15 +0200189}
190/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */
193void mbedtls_x509_string_to_names( char *name, char *parsed_name, int result )
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200194{
195 int ret;
196 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_asn1_named_data *names = NULL;
198 mbedtls_x509_name parsed, *parsed_cur, *parsed_prv;
Manuel Pégourié-Gonnard4fd0b252015-06-26 14:15:48 +0200199 unsigned char buf[1024], out[1024], *c;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200200
201 memset( &parsed, 0, sizeof( parsed ) );
Manuel Pégourié-Gonnard4fd0b252015-06-26 14:15:48 +0200202 memset( out, 0, sizeof( out ) );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200203 memset( buf, 0, sizeof( buf ) );
204 c = buf + sizeof( buf );
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 ret = mbedtls_x509_string_to_names( &names, name );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200207 TEST_ASSERT( ret == result );
208
209 if( ret != 0 )
210 goto exit;
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 ret = mbedtls_x509_write_names( &c, buf, names );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200213 TEST_ASSERT( ret > 0 );
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 TEST_ASSERT( mbedtls_asn1_get_tag( &c, buf + sizeof( buf ), &len,
216 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) == 0 );
217 TEST_ASSERT( mbedtls_x509_get_name( &c, buf + sizeof( buf ), &parsed ) == 0 );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200218
Manuel Pégourié-Gonnard4fd0b252015-06-26 14:15:48 +0200219 ret = mbedtls_x509_dn_gets( (char *) out, sizeof( out ), &parsed );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200220 TEST_ASSERT( ret > 0 );
221
Manuel Pégourié-Gonnard4fd0b252015-06-26 14:15:48 +0200222 TEST_ASSERT( strcmp( (char *) out, parsed_name ) == 0 );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200223
224exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_asn1_free_named_data_list( &names );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200226
227 parsed_cur = parsed.next;
228 while( parsed_cur != 0 )
229 {
230 parsed_prv = parsed_cur;
231 parsed_cur = parsed_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_free( parsed_prv );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200233 }
234}
235/* END_CASE */