1/* BEGIN_HEADER */ 2#include "mbedtls/bignum.h" 3#include "mbedtls/x509_crt.h" 4#include "mbedtls/x509_csr.h" 5#include "mbedtls/pem.h" 6#include "mbedtls/oid.h" 7#include "mbedtls/rsa.h" 8 9#if defined(MBEDTLS_RSA_C) 10int mbedtls_rsa_decrypt_func( void *ctx, size_t *olen, 11 const unsigned char *input, unsigned char *output, 12 size_t output_max_len ) 13{ 14 return( mbedtls_rsa_pkcs1_decrypt( (mbedtls_rsa_context *) ctx, NULL, NULL, 15 olen, input, output, output_max_len ) ); 16} 17int mbedtls_rsa_sign_func( void *ctx, 18 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, 19 mbedtls_md_type_t md_alg, unsigned int hashlen, 20 const unsigned char *hash, unsigned char *sig ) 21{ 22 return( mbedtls_rsa_pkcs1_sign( (mbedtls_rsa_context *) ctx, f_rng, p_rng, 23 md_alg, hashlen, hash, sig ) ); 24} 25size_t mbedtls_rsa_key_len_func( void *ctx ) 26{ 27 return( ((const mbedtls_rsa_context *) ctx)->len ); 28} 29#endif /* MBEDTLS_RSA_C */ 30 31#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ 32 defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C) 33static int x509_crt_verifycsr( const unsigned char *buf, size_t buflen ) 34{ 35 unsigned char hash[MBEDTLS_MD_MAX_SIZE]; 36 const mbedtls_md_info_t *md_info; 37 mbedtls_x509_csr csr; 38 int ret = 0; 39 40 mbedtls_x509_csr_init( &csr ); 41 42 if( mbedtls_x509_csr_parse( &csr, buf, buflen ) != 0 ) 43 { 44 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; 45 goto cleanup; 46 } 47 48 md_info = mbedtls_md_info_from_type( csr.sig_md ); 49 if( mbedtls_md( md_info, csr.cri.p, csr.cri.len, hash ) != 0 ) 50 { 51 /* Note: this can't happen except after an internal error */ 52 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; 53 goto cleanup; 54 } 55 56 if( mbedtls_pk_verify_ext( csr.sig_pk, csr.sig_opts, &csr.pk, 57 csr.sig_md, hash, mbedtls_md_get_size( md_info ), 58 csr.sig.p, csr.sig.len ) != 0 ) 59 { 60 ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED; 61 goto cleanup; 62 } 63 64cleanup: 65 66 mbedtls_x509_csr_free( &csr ); 67 return( ret ); 68} 69#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_PEM_WRITE_C && MBEDTLS_X509_CSR_WRITE_C */ 70 71/* END_HEADER */ 72 73/* BEGIN_DEPENDENCIES 74 * depends_on:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C 75 * END_DEPENDENCIES 76 */ 77 78/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */ 79void x509_csr_check( char * key_file, char * cert_req_check_file, int md_type, 80 int key_usage, int set_key_usage, int cert_type, 81 int set_cert_type ) 82{ 83 mbedtls_pk_context key; 84 mbedtls_x509write_csr req; 85 unsigned char buf[4096]; 86 unsigned char check_buf[4000]; 87 int ret; 88 size_t olen = 0, pem_len = 0, buf_index; 89 int der_len = -1; 90 FILE *f; 91 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1"; 92 mbedtls_test_rnd_pseudo_info rnd_info; 93 94 memset( &rnd_info, 0x2a, sizeof( mbedtls_test_rnd_pseudo_info ) ); 95 96 mbedtls_pk_init( &key ); 97 TEST_ASSERT( mbedtls_pk_parse_keyfile( &key, key_file, NULL, 98 mbedtls_test_rnd_std_rand, NULL ) == 0 ); 99 100 mbedtls_x509write_csr_init( &req ); 101 mbedtls_x509write_csr_set_md_alg( &req, md_type ); 102 mbedtls_x509write_csr_set_key( &req, &key ); 103 TEST_ASSERT( mbedtls_x509write_csr_set_subject_name( &req, subject_name ) == 0 ); 104 if( set_key_usage != 0 ) 105 TEST_ASSERT( mbedtls_x509write_csr_set_key_usage( &req, key_usage ) == 0 ); 106 if( set_cert_type != 0 ) 107 TEST_ASSERT( mbedtls_x509write_csr_set_ns_cert_type( &req, cert_type ) == 0 ); 108 109 ret = mbedtls_x509write_csr_pem( &req, buf, sizeof( buf ), 110 mbedtls_test_rnd_pseudo_rand, &rnd_info ); 111 TEST_ASSERT( ret == 0 ); 112 113 pem_len = strlen( (char *) buf ); 114 115 for( buf_index = pem_len; buf_index < sizeof( buf ); ++buf_index ) 116 { 117 TEST_ASSERT( buf[buf_index] == 0 ); 118 } 119 120 f = fopen( cert_req_check_file, "r" ); 121 TEST_ASSERT( f != NULL ); 122 olen = fread( check_buf, 1, sizeof( check_buf ), f ); 123 fclose( f ); 124 125 TEST_ASSERT( olen >= pem_len - 1 ); 126 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 ); 127 128 der_len = mbedtls_x509write_csr_der( &req, buf, sizeof( buf ), 129 mbedtls_test_rnd_pseudo_rand, 130 &rnd_info ); 131 TEST_ASSERT( der_len >= 0 ); 132 133 if( der_len == 0 ) 134 goto exit; 135 136 ret = mbedtls_x509write_csr_der( &req, buf, (size_t)( der_len - 1 ), 137 mbedtls_test_rnd_pseudo_rand, &rnd_info ); 138 TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); 139 140exit: 141 mbedtls_x509write_csr_free( &req ); 142 mbedtls_pk_free( &key ); 143} 144/* END_CASE */ 145 146/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C:MBEDTLS_USE_PSA_CRYPTO */ 147void x509_csr_check_opaque( char *key_file, int md_type, int key_usage, 148 int cert_type ) 149{ 150 mbedtls_pk_context key; 151 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; 152 psa_algorithm_t md_alg_psa; 153 mbedtls_x509write_csr req; 154 unsigned char buf[4096]; 155 int ret; 156 size_t pem_len = 0; 157 const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1"; 158 mbedtls_test_rnd_pseudo_info rnd_info; 159 160 PSA_INIT( ); 161 memset( &rnd_info, 0x2a, sizeof( mbedtls_test_rnd_pseudo_info ) ); 162 163 md_alg_psa = mbedtls_psa_translate_md( (mbedtls_md_type_t) md_type ); 164 TEST_ASSERT( md_alg_psa != MBEDTLS_MD_NONE ); 165 166 mbedtls_pk_init( &key ); 167 TEST_ASSERT( mbedtls_pk_parse_keyfile( &key, key_file, NULL, 168 mbedtls_test_rnd_std_rand, NULL ) == 0 ); 169 TEST_ASSERT( mbedtls_pk_wrap_as_opaque( &key, &key_id, md_alg_psa ) == 0 ); 170 171 mbedtls_x509write_csr_init( &req ); 172 mbedtls_x509write_csr_set_md_alg( &req, md_type ); 173 mbedtls_x509write_csr_set_key( &req, &key ); 174 TEST_ASSERT( mbedtls_x509write_csr_set_subject_name( &req, subject_name ) == 0 ); 175 if( key_usage != 0 ) 176 TEST_ASSERT( mbedtls_x509write_csr_set_key_usage( &req, key_usage ) == 0 ); 177 if( cert_type != 0 ) 178 TEST_ASSERT( mbedtls_x509write_csr_set_ns_cert_type( &req, cert_type ) == 0 ); 179 180 ret = mbedtls_x509write_csr_pem( &req, buf, sizeof( buf ) - 1, 181 mbedtls_test_rnd_pseudo_rand, &rnd_info ); 182 183 TEST_ASSERT( ret == 0 ); 184 185 pem_len = strlen( (char *) buf ); 186 buf[pem_len] = '\0'; 187 TEST_ASSERT( x509_crt_verifycsr( buf, pem_len + 1 ) == 0 ); 188 189 190exit: 191 mbedtls_x509write_csr_free( &req ); 192 mbedtls_pk_free( &key ); 193 psa_destroy_key( key_id ); 194 PSA_DONE( ); 195} 196/* END_CASE */ 197 198/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CRT_WRITE_C:MBEDTLS_SHA1_C */ 199void x509_crt_check( char *subject_key_file, char *subject_pwd, 200 char *subject_name, char *issuer_key_file, 201 char *issuer_pwd, char *issuer_name, 202 char *serial_str, char *not_before, char *not_after, 203 int md_type, int key_usage, int set_key_usage, 204 int cert_type, int set_cert_type, int auth_ident, 205 int ver, char *cert_check_file, int rsa_alt, int is_ca ) 206{ 207 mbedtls_pk_context subject_key, issuer_key, issuer_key_alt; 208 mbedtls_pk_context *key = &issuer_key; 209 210 mbedtls_x509write_cert crt; 211 unsigned char buf[4096]; 212 unsigned char check_buf[5000]; 213 mbedtls_mpi serial; 214 int ret; 215 size_t olen = 0, pem_len = 0, buf_index = 0; 216 int der_len = -1; 217 FILE *f; 218 mbedtls_test_rnd_pseudo_info rnd_info; 219 220 memset( &rnd_info, 0x2a, sizeof( mbedtls_test_rnd_pseudo_info ) ); 221 mbedtls_mpi_init( &serial ); 222 223 mbedtls_pk_init( &subject_key ); 224 mbedtls_pk_init( &issuer_key ); 225 mbedtls_pk_init( &issuer_key_alt ); 226 227 mbedtls_x509write_crt_init( &crt ); 228 229 TEST_ASSERT( mbedtls_pk_parse_keyfile( &subject_key, subject_key_file, 230 subject_pwd, mbedtls_test_rnd_std_rand, NULL ) == 0 ); 231 232 TEST_ASSERT( mbedtls_pk_parse_keyfile( &issuer_key, issuer_key_file, 233 issuer_pwd, mbedtls_test_rnd_std_rand, NULL ) == 0 ); 234 235#if defined(MBEDTLS_RSA_C) 236 /* For RSA PK contexts, create a copy as an alternative RSA context. */ 237 if( rsa_alt == 1 && mbedtls_pk_get_type( &issuer_key ) == MBEDTLS_PK_RSA ) 238 { 239 TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &issuer_key_alt, 240 mbedtls_pk_rsa( issuer_key ), 241 mbedtls_rsa_decrypt_func, 242 mbedtls_rsa_sign_func, 243 mbedtls_rsa_key_len_func ) == 0 ); 244 245 key = &issuer_key_alt; 246 } 247#else 248 (void) rsa_alt; 249#endif 250 251 TEST_ASSERT( mbedtls_test_read_mpi( &serial, 10, serial_str ) == 0 ); 252 253 if( ver != -1 ) 254 mbedtls_x509write_crt_set_version( &crt, ver ); 255 256 TEST_ASSERT( mbedtls_x509write_crt_set_serial( &crt, &serial ) == 0 ); 257 TEST_ASSERT( mbedtls_x509write_crt_set_validity( &crt, not_before, 258 not_after ) == 0 ); 259 mbedtls_x509write_crt_set_md_alg( &crt, md_type ); 260 TEST_ASSERT( mbedtls_x509write_crt_set_issuer_name( &crt, issuer_name ) == 0 ); 261 TEST_ASSERT( mbedtls_x509write_crt_set_subject_name( &crt, subject_name ) == 0 ); 262 mbedtls_x509write_crt_set_subject_key( &crt, &subject_key ); 263 264 mbedtls_x509write_crt_set_issuer_key( &crt, key ); 265 266 if( crt.version >= MBEDTLS_X509_CRT_VERSION_3 ) 267 { 268 /* For the CA case, a path length of -1 means unlimited. */ 269 TEST_ASSERT( mbedtls_x509write_crt_set_basic_constraints( &crt, is_ca, 270 (is_ca ? -1 : 0) ) == 0 ); 271 TEST_ASSERT( mbedtls_x509write_crt_set_subject_key_identifier( &crt ) == 0 ); 272 if( auth_ident ) 273 TEST_ASSERT( mbedtls_x509write_crt_set_authority_key_identifier( &crt ) == 0 ); 274 if( set_key_usage != 0 ) 275 TEST_ASSERT( mbedtls_x509write_crt_set_key_usage( &crt, key_usage ) == 0 ); 276 if( set_cert_type != 0 ) 277 TEST_ASSERT( mbedtls_x509write_crt_set_ns_cert_type( &crt, cert_type ) == 0 ); 278 } 279 280 ret = mbedtls_x509write_crt_pem( &crt, buf, sizeof( buf ), 281 mbedtls_test_rnd_pseudo_rand, &rnd_info ); 282 TEST_ASSERT( ret == 0 ); 283 284 pem_len = strlen( (char *) buf ); 285 286 // check that the rest of the buffer remains clear 287 for( buf_index = pem_len; buf_index < sizeof( buf ); ++buf_index ) 288 { 289 TEST_ASSERT( buf[buf_index] == 0 ); 290 } 291 292 f = fopen( cert_check_file, "r" ); 293 TEST_ASSERT( f != NULL ); 294 olen = fread( check_buf, 1, sizeof( check_buf ), f ); 295 fclose( f ); 296 TEST_ASSERT( olen < sizeof( check_buf ) ); 297 298 TEST_ASSERT( olen >= pem_len - 1 ); 299 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 ); 300 301 der_len = mbedtls_x509write_crt_der( &crt, buf, sizeof( buf ), 302 mbedtls_test_rnd_pseudo_rand, 303 &rnd_info ); 304 TEST_ASSERT( der_len >= 0 ); 305 306 if( der_len == 0 ) 307 goto exit; 308 309 ret = mbedtls_x509write_crt_der( &crt, buf, (size_t)( der_len - 1 ), 310 mbedtls_test_rnd_pseudo_rand, &rnd_info ); 311 TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); 312 313exit: 314 mbedtls_x509write_crt_free( &crt ); 315 mbedtls_pk_free( &issuer_key_alt ); 316 mbedtls_pk_free( &subject_key ); 317 mbedtls_pk_free( &issuer_key ); 318 mbedtls_mpi_free( &serial ); 319} 320/* END_CASE */ 321 322/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */ 323void mbedtls_x509_string_to_names( char * name, char * parsed_name, int result 324 ) 325{ 326 int ret; 327 size_t len = 0; 328 mbedtls_asn1_named_data *names = NULL; 329 mbedtls_x509_name parsed, *parsed_cur, *parsed_prv; 330 unsigned char buf[1024], out[1024], *c; 331 332 memset( &parsed, 0, sizeof( parsed ) ); 333 memset( out, 0, sizeof( out ) ); 334 memset( buf, 0, sizeof( buf ) ); 335 c = buf + sizeof( buf ); 336 337 ret = mbedtls_x509_string_to_names( &names, name ); 338 TEST_ASSERT( ret == result ); 339 340 if( ret != 0 ) 341 goto exit; 342 343 ret = mbedtls_x509_write_names( &c, buf, names ); 344 TEST_ASSERT( ret > 0 ); 345 346 TEST_ASSERT( mbedtls_asn1_get_tag( &c, buf + sizeof( buf ), &len, 347 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) == 0 ); 348 TEST_ASSERT( mbedtls_x509_get_name( &c, buf + sizeof( buf ), &parsed ) == 0 ); 349 350 ret = mbedtls_x509_dn_gets( (char *) out, sizeof( out ), &parsed ); 351 TEST_ASSERT( ret > 0 ); 352 353 TEST_ASSERT( strcmp( (char *) out, parsed_name ) == 0 ); 354 355exit: 356 mbedtls_asn1_free_named_data_list( &names ); 357 358 parsed_cur = parsed.next; 359 while( parsed_cur != 0 ) 360 { 361 parsed_prv = parsed_cur; 362 parsed_cur = parsed_cur->next; 363 mbedtls_free( parsed_prv ); 364 } 365} 366/* END_CASE */ 367