1/* BEGIN_HEADER */ 2#include "mbedtls/x509_crt.h" 3#include "mbedtls/x509_csr.h" 4#include "mbedtls/pem.h" 5#include "mbedtls/oid.h" 6/* END_HEADER */ 7 8/* BEGIN_DEPENDENCIES 9 * depends_on:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C 10 * END_DEPENDENCIES 11 */ 12 13/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */ 14void x509_csr_check( char *key_file, char *cert_req_check_file, 15 int md_type, int key_usage, int cert_type ) 16{ 17 mbedtls_pk_context key; 18 mbedtls_x509write_csr req; 19 unsigned char buf[4096]; 20 unsigned char check_buf[4000]; 21 int ret; 22 size_t olen = 0, pem_len = 0; 23 int der_len = -1; 24 FILE *f; 25 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1"; 26 rnd_pseudo_info rnd_info; 27 28 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) ); 29 30 mbedtls_pk_init( &key ); 31 TEST_ASSERT( mbedtls_pk_parse_keyfile( &key, key_file, NULL ) == 0 ); 32 33 mbedtls_x509write_csr_init( &req ); 34 mbedtls_x509write_csr_set_md_alg( &req, md_type ); 35 mbedtls_x509write_csr_set_key( &req, &key ); 36 TEST_ASSERT( mbedtls_x509write_csr_set_subject_name( &req, subject_name ) == 0 ); 37 if( key_usage != 0 ) 38 TEST_ASSERT( mbedtls_x509write_csr_set_key_usage( &req, key_usage ) == 0 ); 39 if( cert_type != 0 ) 40 TEST_ASSERT( mbedtls_x509write_csr_set_ns_cert_type( &req, cert_type ) == 0 ); 41 42 ret = mbedtls_x509write_csr_pem( &req, buf, sizeof(buf), 43 rnd_pseudo_rand, &rnd_info ); 44 TEST_ASSERT( ret == 0 ); 45 46 pem_len = strlen( (char *) buf ); 47 48 f = fopen( cert_req_check_file, "r" ); 49 TEST_ASSERT( f != NULL ); 50 olen = fread( check_buf, 1, sizeof( check_buf ), f ); 51 fclose( f ); 52 53 TEST_ASSERT( olen >= pem_len - 1 ); 54 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 ); 55 56 der_len = mbedtls_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 = mbedtls_x509write_csr_der( &req, buf, (size_t)( der_len - 1 ), 64 rnd_pseudo_rand, &rnd_info ); 65 TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); 66 67exit: 68 mbedtls_x509write_csr_free( &req ); 69 mbedtls_pk_free( &key ); 70} 71/* END_CASE */ 72 73/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CRT_WRITE_C:MBEDTLS_SHA1_C */ 74void 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, 78 int md_type, int key_usage, int cert_type, int ver, 79 char *cert_check_file ) 80{ 81 mbedtls_pk_context subject_key, issuer_key; 82 mbedtls_x509write_cert crt; 83 unsigned char buf[4096]; 84 unsigned char check_buf[5000]; 85 mbedtls_mpi serial; 86 int ret; 87 size_t olen = 0, pem_len = 0; 88 int der_len = -1; 89 FILE *f; 90 rnd_pseudo_info rnd_info; 91 92 memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) ); 93 mbedtls_mpi_init( &serial ); 94 mbedtls_pk_init( &subject_key ); 95 mbedtls_pk_init( &issuer_key ); 96 97 TEST_ASSERT( mbedtls_pk_parse_keyfile( &subject_key, subject_key_file, 98 subject_pwd ) == 0 ); 99 TEST_ASSERT( mbedtls_pk_parse_keyfile( &issuer_key, issuer_key_file, 100 issuer_pwd ) == 0 ); 101 TEST_ASSERT( mbedtls_mpi_read_string( &serial, 10, serial_str ) == 0 ); 102 103 mbedtls_x509write_crt_init( &crt ); 104 if( ver != -1 ) 105 mbedtls_x509write_crt_set_version( &crt, ver ); 106 TEST_ASSERT( mbedtls_x509write_crt_set_serial( &crt, &serial ) == 0 ); 107 TEST_ASSERT( mbedtls_x509write_crt_set_validity( &crt, not_before, 108 not_after ) == 0 ); 109 mbedtls_x509write_crt_set_md_alg( &crt, md_type ); 110 TEST_ASSERT( mbedtls_x509write_crt_set_issuer_name( &crt, issuer_name ) == 0 ); 111 TEST_ASSERT( mbedtls_x509write_crt_set_subject_name( &crt, subject_name ) == 0 ); 112 mbedtls_x509write_crt_set_subject_key( &crt, &subject_key ); 113 mbedtls_x509write_crt_set_issuer_key( &crt, &issuer_key ); 114 115 if( crt.version >= MBEDTLS_X509_CRT_VERSION_3 ) 116 { 117 TEST_ASSERT( mbedtls_x509write_crt_set_basic_constraints( &crt, 0, 0 ) == 0 ); 118 TEST_ASSERT( mbedtls_x509write_crt_set_subject_key_identifier( &crt ) == 0 ); 119 TEST_ASSERT( mbedtls_x509write_crt_set_authority_key_identifier( &crt ) == 0 ); 120 if( key_usage != 0 ) 121 TEST_ASSERT( mbedtls_x509write_crt_set_key_usage( &crt, key_usage ) == 0 ); 122 if( cert_type != 0 ) 123 TEST_ASSERT( mbedtls_x509write_crt_set_ns_cert_type( &crt, cert_type ) == 0 ); 124 } 125 126 ret = mbedtls_x509write_crt_pem( &crt, buf, sizeof(buf), 127 rnd_pseudo_rand, &rnd_info ); 128 TEST_ASSERT( ret == 0 ); 129 130 pem_len = strlen( (char *) buf ); 131 132 f = fopen( cert_check_file, "r" ); 133 TEST_ASSERT( f != NULL ); 134 olen = fread( check_buf, 1, sizeof(check_buf), f ); 135 fclose( f ); 136 TEST_ASSERT( olen < sizeof(check_buf) ); 137 138 TEST_ASSERT( olen >= pem_len - 1 ); 139 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 ); 140 141 der_len = mbedtls_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 = mbedtls_x509write_crt_der( &crt, buf, (size_t)( der_len - 1 ), 149 rnd_pseudo_rand, &rnd_info ); 150 TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); 151 152exit: 153 mbedtls_x509write_crt_free( &crt ); 154 mbedtls_pk_free( &issuer_key ); 155 mbedtls_pk_free( &subject_key ); 156 mbedtls_mpi_free( &serial ); 157} 158/* END_CASE */ 159 160/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */ 161void mbedtls_x509_string_to_names( char *name, char *parsed_name, int result ) 162{ 163 int ret; 164 size_t len = 0; 165 mbedtls_asn1_named_data *names = NULL; 166 mbedtls_x509_name parsed, *parsed_cur, *parsed_prv; 167 unsigned char buf[1024], out[1024], *c; 168 169 memset( &parsed, 0, sizeof( parsed ) ); 170 memset( out, 0, sizeof( out ) ); 171 memset( buf, 0, sizeof( buf ) ); 172 c = buf + sizeof( buf ); 173 174 ret = mbedtls_x509_string_to_names( &names, name ); 175 TEST_ASSERT( ret == result ); 176 177 if( ret != 0 ) 178 goto exit; 179 180 ret = mbedtls_x509_write_names( &c, buf, names ); 181 TEST_ASSERT( ret > 0 ); 182 183 TEST_ASSERT( mbedtls_asn1_get_tag( &c, buf + sizeof( buf ), &len, 184 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) == 0 ); 185 TEST_ASSERT( mbedtls_x509_get_name( &c, buf + sizeof( buf ), &parsed ) == 0 ); 186 187 ret = mbedtls_x509_dn_gets( (char *) out, sizeof( out ), &parsed ); 188 TEST_ASSERT( ret > 0 ); 189 190 TEST_ASSERT( strcmp( (char *) out, parsed_name ) == 0 ); 191 192exit: 193 mbedtls_asn1_free_named_data_list( &names ); 194 195 parsed_cur = parsed.next; 196 while( parsed_cur != 0 ) 197 { 198 parsed_prv = parsed_cur; 199 parsed_cur = parsed_cur->next; 200 mbedtls_free( parsed_prv ); 201 } 202} 203/* END_CASE */ 204