| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ | 
| Mohammad Azim Khan | cf32c45 | 2017-06-13 14:55:58 +0100 | [diff] [blame] | 2 | #include "mbedtls/bignum.h" | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 3 | #include "mbedtls/x509_crt.h" | 
|  | 4 | #include "mbedtls/x509_csr.h" | 
|  | 5 | #include "mbedtls/pem.h" | 
|  | 6 | #include "mbedtls/oid.h" | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 7 | #include "mbedtls/rsa.h" | 
|  | 8 |  | 
|  | 9 | #if defined(MBEDTLS_RSA_C) | 
|  | 10 | int mbedtls_rsa_decrypt_func( void *ctx, int mode, 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, mode, olen, | 
|  | 15 | input, output, output_max_len ) ); | 
|  | 16 | } | 
|  | 17 | int mbedtls_rsa_sign_func( void *ctx, | 
|  | 18 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, | 
|  | 19 | int mode, 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, mode, | 
|  | 23 | md_alg, hashlen, hash, sig ) ); | 
|  | 24 | } | 
|  | 25 | size_t mbedtls_rsa_key_len_func( void *ctx ) | 
|  | 26 | { | 
|  | 27 | return( ((const mbedtls_rsa_context *) ctx)->len ); | 
|  | 28 | } | 
|  | 29 | #endif /* MBEDTLS_RSA_C */ | 
|  | 30 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 31 | /* END_HEADER */ | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 32 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 33 | /* BEGIN_DEPENDENCIES | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | * depends_on:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 35 | * END_DEPENDENCIES | 
|  | 36 | */ | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 37 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | /* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */ | 
| Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 39 | void x509_csr_check( char * key_file, char * cert_req_check_file, int md_type, | 
|  | 40 | int key_usage, int cert_type ) | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 41 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | mbedtls_pk_context key; | 
|  | 43 | mbedtls_x509write_csr req; | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 44 | unsigned char buf[4096]; | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 45 | unsigned char check_buf[4000]; | 
|  | 46 | int ret; | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 47 | size_t olen = 0, pem_len = 0; | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 48 | int der_len = -1; | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 49 | FILE *f; | 
| Paul Bakker | 3a8cb6f | 2013-12-30 20:41:54 +0100 | [diff] [blame] | 50 | const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1"; | 
| Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 51 | rnd_pseudo_info rnd_info; | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 52 |  | 
| Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 53 | memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) ); | 
|  | 54 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | mbedtls_pk_init( &key ); | 
|  | 56 | TEST_ASSERT( mbedtls_pk_parse_keyfile( &key, key_file, NULL ) == 0 ); | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 57 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | mbedtls_x509write_csr_init( &req ); | 
|  | 59 | mbedtls_x509write_csr_set_md_alg( &req, md_type ); | 
|  | 60 | mbedtls_x509write_csr_set_key( &req, &key ); | 
|  | 61 | TEST_ASSERT( mbedtls_x509write_csr_set_subject_name( &req, subject_name ) == 0 ); | 
| Manuel Pégourié-Gonnard | c5ce83a | 2014-03-28 12:46:44 +0100 | [diff] [blame] | 62 | if( key_usage != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 63 | TEST_ASSERT( mbedtls_x509write_csr_set_key_usage( &req, key_usage ) == 0 ); | 
| Manuel Pégourié-Gonnard | c5ce83a | 2014-03-28 12:46:44 +0100 | [diff] [blame] | 64 | if( cert_type != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | TEST_ASSERT( mbedtls_x509write_csr_set_ns_cert_type( &req, cert_type ) == 0 ); | 
| Paul Bakker | 8eabfc1 | 2013-08-25 10:18:25 +0200 | [diff] [blame] | 66 |  | 
| Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 67 | ret = mbedtls_x509write_csr_pem( &req, buf, sizeof( buf ), | 
| Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 68 | rnd_pseudo_rand, &rnd_info ); | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 69 | TEST_ASSERT( ret == 0 ); | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 70 |  | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 71 | pem_len = strlen( (char *) buf ); | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 72 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 73 | f = fopen( cert_req_check_file, "r" ); | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 74 | TEST_ASSERT( f != NULL ); | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 75 | olen = fread( check_buf, 1, sizeof( check_buf ), f ); | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 76 | fclose( f ); | 
|  | 77 |  | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 78 | TEST_ASSERT( olen >= pem_len - 1 ); | 
|  | 79 | TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 ); | 
| Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 80 |  | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 81 | der_len = mbedtls_x509write_csr_der( &req, buf, sizeof( buf ), | 
|  | 82 | rnd_pseudo_rand, &rnd_info ); | 
|  | 83 | TEST_ASSERT( der_len >= 0 ); | 
|  | 84 |  | 
|  | 85 | if( der_len == 0 ) | 
|  | 86 | goto exit; | 
|  | 87 |  | 
|  | 88 | ret = mbedtls_x509write_csr_der( &req, buf, (size_t)( der_len - 1 ), | 
|  | 89 | rnd_pseudo_rand, &rnd_info ); | 
|  | 90 | TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); | 
|  | 91 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 92 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 93 | mbedtls_x509write_csr_free( &req ); | 
|  | 94 | mbedtls_pk_free( &key ); | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 95 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 96 | /* END_CASE */ | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 97 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | /* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CRT_WRITE_C:MBEDTLS_SHA1_C */ | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 99 | void x509_crt_check( char *subject_key_file, char *subject_pwd, | 
|  | 100 | char *subject_name, char *issuer_key_file, | 
|  | 101 | char *issuer_pwd, char *issuer_name, | 
|  | 102 | char *serial_str, char *not_before, char *not_after, | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 103 | int md_type, int key_usage, int cert_type, int auth_ident, | 
|  | 104 | int ver, char *cert_check_file, int rsa_alt ) | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 105 | { | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 106 | mbedtls_pk_context subject_key, issuer_key, issuer_key_alt; | 
|  | 107 | mbedtls_pk_context *key = &issuer_key; | 
|  | 108 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | mbedtls_x509write_cert crt; | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 110 | unsigned char buf[4096]; | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 111 | unsigned char check_buf[5000]; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | mbedtls_mpi serial; | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 113 | int ret; | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 114 | size_t olen = 0, pem_len = 0; | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 115 | int der_len = -1; | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 116 | FILE *f; | 
| Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 117 | rnd_pseudo_info rnd_info; | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 118 |  | 
| Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 119 | memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | mbedtls_mpi_init( &serial ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 121 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | mbedtls_pk_init( &subject_key ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 123 | mbedtls_pk_init( &issuer_key  ); | 
|  | 124 | mbedtls_pk_init( &issuer_key_alt ); | 
|  | 125 |  | 
|  | 126 | mbedtls_x509write_crt_init( &crt ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 127 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | TEST_ASSERT( mbedtls_pk_parse_keyfile( &subject_key, subject_key_file, | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 129 | subject_pwd ) == 0 ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 130 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | TEST_ASSERT( mbedtls_pk_parse_keyfile( &issuer_key, issuer_key_file, | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 132 | issuer_pwd ) == 0 ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 133 |  | 
| Manuel Pégourié-Gonnard | 147b28e | 2018-03-12 15:26:59 +0100 | [diff] [blame] | 134 | #if defined(MBEDTLS_RSA_C) | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 135 | /* For RSA PK contexts, create a copy as an alternative RSA context. */ | 
|  | 136 | if( rsa_alt == 1 && mbedtls_pk_get_type( &issuer_key ) == MBEDTLS_PK_RSA ) | 
|  | 137 | { | 
|  | 138 | TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &issuer_key_alt, | 
|  | 139 | mbedtls_pk_rsa( issuer_key ), | 
|  | 140 | mbedtls_rsa_decrypt_func, | 
|  | 141 | mbedtls_rsa_sign_func, | 
|  | 142 | mbedtls_rsa_key_len_func ) == 0 ); | 
|  | 143 |  | 
|  | 144 | key = &issuer_key_alt; | 
|  | 145 | } | 
| Manuel Pégourié-Gonnard | 147b28e | 2018-03-12 15:26:59 +0100 | [diff] [blame] | 146 | #else | 
|  | 147 | (void) rsa_alt; | 
|  | 148 | #endif | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 149 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | TEST_ASSERT( mbedtls_mpi_read_string( &serial, 10, serial_str ) == 0 ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 151 |  | 
| Manuel Pégourié-Gonnard | 6c1a73e | 2014-03-28 14:03:22 +0100 | [diff] [blame] | 152 | if( ver != -1 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | mbedtls_x509write_crt_set_version( &crt, ver ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 154 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | TEST_ASSERT( mbedtls_x509write_crt_set_serial( &crt, &serial ) == 0 ); | 
|  | 156 | TEST_ASSERT( mbedtls_x509write_crt_set_validity( &crt, not_before, | 
| Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 157 | not_after ) == 0 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | mbedtls_x509write_crt_set_md_alg( &crt, md_type ); | 
|  | 159 | TEST_ASSERT( mbedtls_x509write_crt_set_issuer_name( &crt, issuer_name ) == 0 ); | 
|  | 160 | TEST_ASSERT( mbedtls_x509write_crt_set_subject_name( &crt, subject_name ) == 0 ); | 
|  | 161 | mbedtls_x509write_crt_set_subject_key( &crt, &subject_key ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 162 |  | 
|  | 163 | mbedtls_x509write_crt_set_issuer_key( &crt, key ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 164 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | if( crt.version >= MBEDTLS_X509_CRT_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | 6c1a73e | 2014-03-28 14:03:22 +0100 | [diff] [blame] | 166 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | TEST_ASSERT( mbedtls_x509write_crt_set_basic_constraints( &crt, 0, 0 ) == 0 ); | 
|  | 168 | TEST_ASSERT( mbedtls_x509write_crt_set_subject_key_identifier( &crt ) == 0 ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 169 | if( auth_ident ) | 
|  | 170 | TEST_ASSERT( mbedtls_x509write_crt_set_authority_key_identifier( &crt ) == 0 ); | 
| Manuel Pégourié-Gonnard | 6c1a73e | 2014-03-28 14:03:22 +0100 | [diff] [blame] | 171 | if( key_usage != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | TEST_ASSERT( mbedtls_x509write_crt_set_key_usage( &crt, key_usage ) == 0 ); | 
| Manuel Pégourié-Gonnard | 6c1a73e | 2014-03-28 14:03:22 +0100 | [diff] [blame] | 173 | if( cert_type != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | TEST_ASSERT( mbedtls_x509write_crt_set_ns_cert_type( &crt, cert_type ) == 0 ); | 
| Manuel Pégourié-Gonnard | 6c1a73e | 2014-03-28 14:03:22 +0100 | [diff] [blame] | 175 | } | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 176 |  | 
| Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 177 | ret = mbedtls_x509write_crt_pem( &crt, buf, sizeof( buf ), | 
|  | 178 | rnd_pseudo_rand, &rnd_info ); | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 179 | TEST_ASSERT( ret == 0 ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 180 |  | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 181 | pem_len = strlen( (char *) buf ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 182 |  | 
|  | 183 | f = fopen( cert_check_file, "r" ); | 
|  | 184 | TEST_ASSERT( f != NULL ); | 
| Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 185 | olen = fread( check_buf, 1, sizeof( check_buf ), f ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 186 | fclose( f ); | 
| Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 187 | TEST_ASSERT( olen < sizeof( check_buf ) ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 188 |  | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 189 | TEST_ASSERT( olen >= pem_len - 1 ); | 
|  | 190 | TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 191 |  | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 192 | der_len = mbedtls_x509write_crt_der( &crt, buf, sizeof( buf ), | 
| Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 193 | rnd_pseudo_rand, &rnd_info ); | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 194 | TEST_ASSERT( der_len >= 0 ); | 
|  | 195 |  | 
|  | 196 | if( der_len == 0 ) | 
|  | 197 | goto exit; | 
|  | 198 |  | 
|  | 199 | ret = mbedtls_x509write_crt_der( &crt, buf, (size_t)( der_len - 1 ), | 
| Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 200 | rnd_pseudo_rand, &rnd_info ); | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 201 | TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); | 
|  | 202 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 203 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | mbedtls_x509write_crt_free( &crt ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 205 | mbedtls_pk_free( &issuer_key_alt ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | mbedtls_pk_free( &subject_key ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 207 | mbedtls_pk_free( &issuer_key ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | mbedtls_mpi_free( &serial ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 209 | } | 
|  | 210 | /* END_CASE */ | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 211 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */ | 
| Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 213 | void mbedtls_x509_string_to_names( char * name, char * parsed_name, int result | 
|  | 214 | ) | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 215 | { | 
|  | 216 | int ret; | 
|  | 217 | size_t len = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 218 | mbedtls_asn1_named_data *names = NULL; | 
|  | 219 | mbedtls_x509_name parsed, *parsed_cur, *parsed_prv; | 
| Manuel Pégourié-Gonnard | 4fd0b25 | 2015-06-26 14:15:48 +0200 | [diff] [blame] | 220 | unsigned char buf[1024], out[1024], *c; | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 221 |  | 
|  | 222 | memset( &parsed, 0, sizeof( parsed ) ); | 
| Manuel Pégourié-Gonnard | 4fd0b25 | 2015-06-26 14:15:48 +0200 | [diff] [blame] | 223 | memset( out, 0, sizeof( out ) ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 224 | memset( buf, 0, sizeof( buf ) ); | 
|  | 225 | c = buf + sizeof( buf ); | 
|  | 226 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | ret = mbedtls_x509_string_to_names( &names, name ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 228 | TEST_ASSERT( ret == result ); | 
|  | 229 |  | 
|  | 230 | if( ret != 0 ) | 
|  | 231 | goto exit; | 
|  | 232 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | ret = mbedtls_x509_write_names( &c, buf, names ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 234 | TEST_ASSERT( ret > 0 ); | 
|  | 235 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | TEST_ASSERT( mbedtls_asn1_get_tag( &c, buf + sizeof( buf ), &len, | 
|  | 237 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) == 0 ); | 
|  | 238 | TEST_ASSERT( mbedtls_x509_get_name( &c, buf + sizeof( buf ), &parsed ) == 0 ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 239 |  | 
| Manuel Pégourié-Gonnard | 4fd0b25 | 2015-06-26 14:15:48 +0200 | [diff] [blame] | 240 | ret = mbedtls_x509_dn_gets( (char *) out, sizeof( out ), &parsed ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 241 | TEST_ASSERT( ret > 0 ); | 
|  | 242 |  | 
| Manuel Pégourié-Gonnard | 4fd0b25 | 2015-06-26 14:15:48 +0200 | [diff] [blame] | 243 | TEST_ASSERT( strcmp( (char *) out, parsed_name ) == 0 ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 244 |  | 
|  | 245 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 246 | mbedtls_asn1_free_named_data_list( &names ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 247 |  | 
|  | 248 | parsed_cur = parsed.next; | 
|  | 249 | while( parsed_cur != 0 ) | 
|  | 250 | { | 
|  | 251 | parsed_prv = parsed_cur; | 
|  | 252 | parsed_cur = parsed_cur->next; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | mbedtls_free( parsed_prv ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 254 | } | 
|  | 255 | } | 
|  | 256 | /* END_CASE */ |