blob: 356af7524ff82a0a3b2705e13bea0fffad644d24 [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"
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009 * depends_on:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C
Paul Bakker33b43f12013-08-20 11:48:36 +020010 * END_DEPENDENCIES
11 */
Paul Bakker6d620502012-02-16 14:09:13 +000012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020017 mbedtls_pk_context key;
18 mbedtls_x509write_csr req;
Paul Bakker6d620502012-02-16 14:09:13 +000019 unsigned char buf[4000];
20 unsigned char check_buf[4000];
21 int ret;
Paul Bakker77e23fb2013-09-15 20:03:26 +020022 size_t olen = 0, pem_len = 0;
Paul Bakker6d620502012-02-16 14:09:13 +000023 FILE *f;
Paul Bakker3a8cb6f2013-12-30 20:41:54 +010024 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020025 rnd_pseudo_info rnd_info;
Paul Bakker6d620502012-02-16 14:09:13 +000026
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020027 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029 mbedtls_pk_init( &key );
30 TEST_ASSERT( mbedtls_pk_parse_keyfile( &key, key_file, NULL ) == 0 );
Paul Bakker6d620502012-02-16 14:09:13 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032 mbedtls_x509write_csr_init( &req );
33 mbedtls_x509write_csr_set_md_alg( &req, md_type );
34 mbedtls_x509write_csr_set_key( &req, &key );
35 TEST_ASSERT( mbedtls_x509write_csr_set_subject_name( &req, subject_name ) == 0 );
Manuel Pégourié-Gonnardc5ce83a2014-03-28 12:46:44 +010036 if( key_usage != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 TEST_ASSERT( mbedtls_x509write_csr_set_key_usage( &req, key_usage ) == 0 );
Manuel Pégourié-Gonnardc5ce83a2014-03-28 12:46:44 +010038 if( cert_type != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 TEST_ASSERT( mbedtls_x509write_csr_set_ns_cert_type( &req, cert_type ) == 0 );
Paul Bakker8eabfc12013-08-25 10:18:25 +020040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 ret = mbedtls_x509write_csr_pem( &req, buf, sizeof(buf),
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020042 rnd_pseudo_rand, &rnd_info );
Paul Bakker77e23fb2013-09-15 20:03:26 +020043 TEST_ASSERT( ret == 0 );
Paul Bakker6d620502012-02-16 14:09:13 +000044
Paul Bakker77e23fb2013-09-15 20:03:26 +020045 pem_len = strlen( (char *) buf );
Paul Bakker6d620502012-02-16 14:09:13 +000046
Paul Bakker33b43f12013-08-20 11:48:36 +020047 f = fopen( cert_req_check_file, "r" );
Paul Bakker6d620502012-02-16 14:09:13 +000048 TEST_ASSERT( f != NULL );
Paul Bakker77e23fb2013-09-15 20:03:26 +020049 olen = fread( check_buf, 1, sizeof( check_buf ), f );
Paul Bakker6d620502012-02-16 14:09:13 +000050 fclose( f );
51
Paul Bakker77e23fb2013-09-15 20:03:26 +020052 TEST_ASSERT( olen >= pem_len - 1 );
53 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010054
Paul Bakkerbd51b262014-07-10 15:26:12 +020055exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_x509write_csr_free( &req );
57 mbedtls_pk_free( &key );
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CRT_WRITE_C:MBEDTLS_SHA1_C */
Paul Bakker2397cf32013-09-08 15:58:15 +020062void 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,
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +010066 int md_type, int key_usage, int cert_type, int ver,
67 char *cert_check_file )
Paul Bakker2397cf32013-09-08 15:58:15 +020068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 mbedtls_pk_context subject_key, issuer_key;
70 mbedtls_x509write_cert crt;
Paul Bakker2397cf32013-09-08 15:58:15 +020071 unsigned char buf[4000];
72 unsigned char check_buf[5000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_mpi serial;
Paul Bakker2397cf32013-09-08 15:58:15 +020074 int ret;
Paul Bakker77e23fb2013-09-15 20:03:26 +020075 size_t olen = 0, pem_len = 0;
Paul Bakker2397cf32013-09-08 15:58:15 +020076 FILE *f;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020077 rnd_pseudo_info rnd_info;
Paul Bakker2397cf32013-09-08 15:58:15 +020078
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020079 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_mpi_init( &serial );
81 mbedtls_pk_init( &subject_key );
82 mbedtls_pk_init( &issuer_key );
Paul Bakker2397cf32013-09-08 15:58:15 +020083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 TEST_ASSERT( mbedtls_pk_parse_keyfile( &subject_key, subject_key_file,
Paul Bakker2397cf32013-09-08 15:58:15 +020085 subject_pwd ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 TEST_ASSERT( mbedtls_pk_parse_keyfile( &issuer_key, issuer_key_file,
Paul Bakker2397cf32013-09-08 15:58:15 +020087 issuer_pwd ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 TEST_ASSERT( mbedtls_mpi_read_string( &serial, 10, serial_str ) == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +020089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +010091 if( ver != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_x509write_crt_set_version( &crt, ver );
93 TEST_ASSERT( mbedtls_x509write_crt_set_serial( &crt, &serial ) == 0 );
94 TEST_ASSERT( mbedtls_x509write_crt_set_validity( &crt, not_before,
Paul Bakker2397cf32013-09-08 15:58:15 +020095 not_after ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_x509write_crt_set_md_alg( &crt, md_type );
97 TEST_ASSERT( mbedtls_x509write_crt_set_issuer_name( &crt, issuer_name ) == 0 );
98 TEST_ASSERT( mbedtls_x509write_crt_set_subject_name( &crt, subject_name ) == 0 );
99 mbedtls_x509write_crt_set_subject_key( &crt, &subject_key );
100 mbedtls_x509write_crt_set_issuer_key( &crt, &issuer_key );
Paul Bakker2397cf32013-09-08 15:58:15 +0200101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 if( crt.version >= MBEDTLS_X509_CRT_VERSION_3 )
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 TEST_ASSERT( mbedtls_x509write_crt_set_basic_constraints( &crt, 0, 0 ) == 0 );
105 TEST_ASSERT( mbedtls_x509write_crt_set_subject_key_identifier( &crt ) == 0 );
106 TEST_ASSERT( mbedtls_x509write_crt_set_authority_key_identifier( &crt ) == 0 );
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100107 if( key_usage != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 TEST_ASSERT( mbedtls_x509write_crt_set_key_usage( &crt, key_usage ) == 0 );
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100109 if( cert_type != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 TEST_ASSERT( mbedtls_x509write_crt_set_ns_cert_type( &crt, cert_type ) == 0 );
Manuel Pégourié-Gonnard6c1a73e2014-03-28 14:03:22 +0100111 }
Paul Bakker2397cf32013-09-08 15:58:15 +0200112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 ret = mbedtls_x509write_crt_pem( &crt, buf, sizeof(buf),
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200114 rnd_pseudo_rand, &rnd_info );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200115 TEST_ASSERT( ret == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +0200116
Paul Bakker77e23fb2013-09-15 20:03:26 +0200117 pem_len = strlen( (char *) buf );
Paul Bakker2397cf32013-09-08 15:58:15 +0200118
119 f = fopen( cert_check_file, "r" );
120 TEST_ASSERT( f != NULL );
Paul Bakker94b916c2014-04-17 16:07:20 +0200121 olen = fread( check_buf, 1, sizeof(check_buf), f );
Paul Bakker2397cf32013-09-08 15:58:15 +0200122 fclose( f );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200123 TEST_ASSERT( olen < sizeof(check_buf) );
Paul Bakker2397cf32013-09-08 15:58:15 +0200124
Paul Bakker77e23fb2013-09-15 20:03:26 +0200125 TEST_ASSERT( olen >= pem_len - 1 );
126 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +0200127
Paul Bakkerbd51b262014-07-10 15:26:12 +0200128exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_x509write_crt_free( &crt );
130 mbedtls_pk_free( &issuer_key );
131 mbedtls_pk_free( &subject_key );
132 mbedtls_mpi_free( &serial );
Paul Bakker2397cf32013-09-08 15:58:15 +0200133}
134/* END_CASE */
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */
137void mbedtls_x509_string_to_names( char *name, char *parsed_name, int result )
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200138{
139 int ret;
140 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_asn1_named_data *names = NULL;
142 mbedtls_x509_name parsed, *parsed_cur, *parsed_prv;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200143 unsigned char buf[2048], *c;
144
145 memset( &parsed, 0, sizeof( parsed ) );
146 memset( buf, 0, sizeof( buf ) );
147 c = buf + sizeof( buf );
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 ret = mbedtls_x509_string_to_names( &names, name );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200150 TEST_ASSERT( ret == result );
151
152 if( ret != 0 )
153 goto exit;
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 ret = mbedtls_x509_write_names( &c, buf, names );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200156 TEST_ASSERT( ret > 0 );
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 TEST_ASSERT( mbedtls_asn1_get_tag( &c, buf + sizeof( buf ), &len,
159 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) == 0 );
160 TEST_ASSERT( mbedtls_x509_get_name( &c, buf + sizeof( buf ), &parsed ) == 0 );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 ret = mbedtls_x509_dn_gets( (char *) buf, sizeof( buf ), &parsed );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200163 TEST_ASSERT( ret > 0 );
164
165 TEST_ASSERT( strcmp( (char *) buf, parsed_name ) == 0 );
166
167exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_asn1_free_named_data_list( &names );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200169
170 parsed_cur = parsed.next;
171 while( parsed_cur != 0 )
172 {
173 parsed_prv = parsed_cur;
174 parsed_cur = parsed_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_free( parsed_prv );
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200176 }
177}
178/* END_CASE */