blob: ca097e7ad9107ea48d370741f2d0d1e18850b38b [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002#include <polarssl/x509_crt.h>
3#include <polarssl/x509_csr.h>
Paul Bakker6d620502012-02-16 14:09:13 +00004#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
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 */
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{
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020017 pk_context key;
Paul Bakkercd358032013-09-09 12:08:11 +020018 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
29 pk_init( &key );
Paul Bakker1a7550a2013-09-15 13:01:22 +020030 TEST_ASSERT( pk_parse_keyfile( &key, key_file, NULL ) == 0 );
Paul Bakker6d620502012-02-16 14:09:13 +000031
Paul Bakker82e29452013-08-25 11:01:31 +020032 x509write_csr_init( &req );
33 x509write_csr_set_md_alg( &req, md_type );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020034 x509write_csr_set_key( &req, &key );
Paul Bakker82e29452013-08-25 11:01:31 +020035 TEST_ASSERT( x509write_csr_set_subject_name( &req, subject_name ) == 0 );
Paul Bakker8eabfc12013-08-25 10:18:25 +020036
Paul Bakker77e23fb2013-09-15 20:03:26 +020037 ret = x509write_csr_pem( &req, buf, sizeof(buf),
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020038 rnd_pseudo_rand, &rnd_info );
Paul Bakker77e23fb2013-09-15 20:03:26 +020039 TEST_ASSERT( ret == 0 );
Paul Bakker6d620502012-02-16 14:09:13 +000040
Paul Bakker77e23fb2013-09-15 20:03:26 +020041 pem_len = strlen( (char *) buf );
Paul Bakker6d620502012-02-16 14:09:13 +000042
Paul Bakker33b43f12013-08-20 11:48:36 +020043 f = fopen( cert_req_check_file, "r" );
Paul Bakker6d620502012-02-16 14:09:13 +000044 TEST_ASSERT( f != NULL );
Paul Bakker77e23fb2013-09-15 20:03:26 +020045 olen = fread( check_buf, 1, sizeof( check_buf ), f );
Paul Bakker6d620502012-02-16 14:09:13 +000046 fclose( f );
47
Paul Bakker77e23fb2013-09-15 20:03:26 +020048 TEST_ASSERT( olen >= pem_len - 1 );
49 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010050
Paul Bakker82e29452013-08-25 11:01:31 +020051 x509write_csr_free( &req );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020052 pk_free( &key );
Paul Bakker6d620502012-02-16 14:09:13 +000053}
Paul Bakker33b43f12013-08-20 11:48:36 +020054/* END_CASE */
Paul Bakker2397cf32013-09-08 15:58:15 +020055
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +010056/* BEGIN_CASE depends_on:POLARSSL_PEM_WRITE_C:POLARSSL_X509_CRT_WRITE_C:POLARSSL_SHA1_C */
Paul Bakker2397cf32013-09-08 15:58:15 +020057void x509_crt_check( char *subject_key_file, char *subject_pwd,
58 char *subject_name, char *issuer_key_file,
59 char *issuer_pwd, char *issuer_name,
60 char *serial_str, char *not_before, char *not_after,
61 int md_type, char *cert_check_file )
62{
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +020063 pk_context subject_key, issuer_key;
Paul Bakker2397cf32013-09-08 15:58:15 +020064 x509write_cert crt;
Paul Bakker2397cf32013-09-08 15:58:15 +020065 unsigned char buf[4000];
66 unsigned char check_buf[5000];
67 mpi serial;
68 int ret;
Paul Bakker77e23fb2013-09-15 20:03:26 +020069 size_t olen = 0, pem_len = 0;
Paul Bakker2397cf32013-09-08 15:58:15 +020070 FILE *f;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020071 rnd_pseudo_info rnd_info;
Paul Bakker2397cf32013-09-08 15:58:15 +020072
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020073 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
Paul Bakker2397cf32013-09-08 15:58:15 +020074 mpi_init( &serial );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +020075 pk_init( &subject_key );
76 pk_init( &issuer_key );
Paul Bakker2397cf32013-09-08 15:58:15 +020077
Paul Bakker1a7550a2013-09-15 13:01:22 +020078 TEST_ASSERT( pk_parse_keyfile( &subject_key, subject_key_file,
Paul Bakker2397cf32013-09-08 15:58:15 +020079 subject_pwd ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020080 TEST_ASSERT( pk_parse_keyfile( &issuer_key, issuer_key_file,
Paul Bakker2397cf32013-09-08 15:58:15 +020081 issuer_pwd ) == 0 );
82 TEST_ASSERT( mpi_read_string( &serial, 10, serial_str ) == 0 );
83
84 x509write_crt_init( &crt );
85 x509write_crt_set_serial( &crt, &serial );
86 TEST_ASSERT( x509write_crt_set_validity( &crt, not_before,
87 not_after ) == 0 );
88 x509write_crt_set_md_alg( &crt, md_type );
89 TEST_ASSERT( x509write_crt_set_issuer_name( &crt, issuer_name ) == 0 );
90 TEST_ASSERT( x509write_crt_set_subject_name( &crt, subject_name ) == 0 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +020091 x509write_crt_set_subject_key( &crt, &subject_key );
92 x509write_crt_set_issuer_key( &crt, &issuer_key );
Paul Bakker2397cf32013-09-08 15:58:15 +020093
94 TEST_ASSERT( x509write_crt_set_basic_constraints( &crt, 0, 0 ) == 0 );
95 TEST_ASSERT( x509write_crt_set_subject_key_identifier( &crt ) == 0 );
96 TEST_ASSERT( x509write_crt_set_authority_key_identifier( &crt ) == 0 );
97
Paul Bakker77e23fb2013-09-15 20:03:26 +020098 ret = x509write_crt_pem( &crt, buf, sizeof(buf),
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020099 rnd_pseudo_rand, &rnd_info );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200100 TEST_ASSERT( ret == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +0200101
Paul Bakker77e23fb2013-09-15 20:03:26 +0200102 pem_len = strlen( (char *) buf );
Paul Bakker2397cf32013-09-08 15:58:15 +0200103
104 f = fopen( cert_check_file, "r" );
105 TEST_ASSERT( f != NULL );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200106 TEST_ASSERT( ( olen = fread( check_buf, 1, sizeof(check_buf), f ) ) <
107 sizeof(check_buf) );
Paul Bakker2397cf32013-09-08 15:58:15 +0200108 fclose( f );
109
Paul Bakker77e23fb2013-09-15 20:03:26 +0200110 TEST_ASSERT( olen >= pem_len - 1 );
111 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
Paul Bakker2397cf32013-09-08 15:58:15 +0200112
113 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200114 pk_free( &issuer_key );
115 pk_free( &subject_key );
Paul Bakker2397cf32013-09-08 15:58:15 +0200116 mpi_free( &serial );
117}
118/* END_CASE */