| 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" | 
| Manuel Pégourié-Gonnard | feb0396 | 2020-08-20 09:59:33 +0200 | [diff] [blame] | 8 |  | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 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 |  | 
| Gilles Peskine | f4e672e | 2020-01-31 14:22:10 +0100 | [diff] [blame] | 31 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ | 
|  | 32 | defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C) | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 33 | static 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; | 
| Hanno Becker | bf2dacb | 2019-06-03 16:28:24 +0100 | [diff] [blame] | 38 | int ret = 0; | 
|  | 39 |  | 
|  | 40 | mbedtls_x509_csr_init( &csr ); | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 41 |  | 
|  | 42 | if( mbedtls_x509_csr_parse( &csr, buf, buflen ) != 0 ) | 
| Hanno Becker | bf2dacb | 2019-06-03 16:28:24 +0100 | [diff] [blame] | 43 | { | 
|  | 44 | ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; | 
|  | 45 | goto cleanup; | 
|  | 46 | } | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 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 ) | 
| Andrzej Kurek | 4b11407 | 2018-11-19 18:04:01 -0500 | [diff] [blame] | 50 | { | 
|  | 51 | /* Note: this can't happen except after an internal error */ | 
| Hanno Becker | bf2dacb | 2019-06-03 16:28:24 +0100 | [diff] [blame] | 52 | ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; | 
|  | 53 | goto cleanup; | 
| Andrzej Kurek | 4b11407 | 2018-11-19 18:04:01 -0500 | [diff] [blame] | 54 | } | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 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 ) | 
| Andrzej Kurek | 4b11407 | 2018-11-19 18:04:01 -0500 | [diff] [blame] | 59 | { | 
| Hanno Becker | bf2dacb | 2019-06-03 16:28:24 +0100 | [diff] [blame] | 60 | ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED; | 
|  | 61 | goto cleanup; | 
| Andrzej Kurek | 4b11407 | 2018-11-19 18:04:01 -0500 | [diff] [blame] | 62 | } | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 63 |  | 
| Hanno Becker | bf2dacb | 2019-06-03 16:28:24 +0100 | [diff] [blame] | 64 | cleanup: | 
|  | 65 |  | 
| Hanno Becker | 2fcdd74 | 2019-02-26 13:59:48 +0000 | [diff] [blame] | 66 | mbedtls_x509_csr_free( &csr ); | 
| Hanno Becker | bf2dacb | 2019-06-03 16:28:24 +0100 | [diff] [blame] | 67 | return( ret ); | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 68 | } | 
| Gilles Peskine | f4e672e | 2020-01-31 14:22:10 +0100 | [diff] [blame] | 69 | #endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_PEM_WRITE_C && MBEDTLS_X509_CSR_WRITE_C */ | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 70 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 71 | /* END_HEADER */ | 
| 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 | /* BEGIN_DEPENDENCIES | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | * depends_on:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 75 | * END_DEPENDENCIES | 
|  | 76 | */ | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 77 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | /* 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] | 79 | void x509_csr_check( char * key_file, char * cert_req_check_file, int md_type, | 
| Andres Amaya Garcia | 7067f81 | 2018-09-26 10:51:16 +0100 | [diff] [blame] | 80 | int key_usage, int set_key_usage, int cert_type, | 
|  | 81 | int set_cert_type ) | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 82 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | mbedtls_pk_context key; | 
|  | 84 | mbedtls_x509write_csr req; | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 85 | unsigned char buf[4096]; | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 86 | unsigned char check_buf[4000]; | 
|  | 87 | int ret; | 
| Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 88 | size_t olen = 0, pem_len = 0, buf_index; | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 89 | int der_len = -1; | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 90 | FILE *f; | 
| Paul Bakker | 3a8cb6f | 2013-12-30 20:41:54 +0100 | [diff] [blame] | 91 | const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1"; | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 92 | mbedtls_test_rnd_pseudo_info rnd_info; | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 93 |  | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 94 | memset( &rnd_info, 0x2a, sizeof( mbedtls_test_rnd_pseudo_info ) ); | 
| Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 95 |  | 
| Neil Armstrong | 1104866 | 2022-07-20 15:49:49 +0200 | [diff] [blame^] | 96 | mbedtls_x509write_csr_init( &req ); | 
|  | 97 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | mbedtls_pk_init( &key ); | 
|  | 99 | TEST_ASSERT( mbedtls_pk_parse_keyfile( &key, key_file, NULL ) == 0 ); | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 100 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 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 ); | 
| Andres Amaya Garcia | 7067f81 | 2018-09-26 10:51:16 +0100 | [diff] [blame] | 104 | if( set_key_usage != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | TEST_ASSERT( mbedtls_x509write_csr_set_key_usage( &req, key_usage ) == 0 ); | 
| Andres Amaya Garcia | 7067f81 | 2018-09-26 10:51:16 +0100 | [diff] [blame] | 106 | if( set_cert_type != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | 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] | 108 |  | 
| Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 109 | ret = mbedtls_x509write_csr_pem( &req, buf, sizeof( buf ), | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 110 | mbedtls_test_rnd_pseudo_rand, &rnd_info ); | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 111 | TEST_ASSERT( ret == 0 ); | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 112 |  | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 113 | pem_len = strlen( (char *) buf ); | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 114 |  | 
| Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 115 | for( buf_index = pem_len; buf_index < sizeof( buf ); ++buf_index ) | 
|  | 116 | { | 
|  | 117 | TEST_ASSERT( buf[buf_index] == 0 ); | 
|  | 118 | } | 
|  | 119 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 120 | f = fopen( cert_req_check_file, "r" ); | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 121 | TEST_ASSERT( f != NULL ); | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 122 | olen = fread( check_buf, 1, sizeof( check_buf ), f ); | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 123 | fclose( f ); | 
|  | 124 |  | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 125 | TEST_ASSERT( olen >= pem_len - 1 ); | 
|  | 126 | TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 ); | 
| Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 127 |  | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 128 | der_len = mbedtls_x509write_csr_der( &req, buf, sizeof( buf ), | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 129 | mbedtls_test_rnd_pseudo_rand, | 
|  | 130 | &rnd_info ); | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 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 ), | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 137 | mbedtls_test_rnd_pseudo_rand, &rnd_info ); | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 138 | TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); | 
|  | 139 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 140 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | mbedtls_x509write_csr_free( &req ); | 
|  | 142 | mbedtls_pk_free( &key ); | 
| Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 143 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 144 | /* END_CASE */ | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 145 |  | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 146 | /* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C:MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 147 | void x509_csr_check_opaque( char *key_file, int md_type, int key_usage, | 
|  | 148 | int cert_type ) | 
|  | 149 | { | 
|  | 150 | mbedtls_pk_context key; | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 151 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 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"; | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 158 | mbedtls_test_rnd_pseudo_info rnd_info; | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 159 |  | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 160 | memset( &rnd_info, 0x2a, sizeof( mbedtls_test_rnd_pseudo_info ) ); | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 161 |  | 
| Neil Armstrong | 1104866 | 2022-07-20 15:49:49 +0200 | [diff] [blame^] | 162 | mbedtls_x509write_csr_init( &req ); | 
|  | 163 |  | 
|  | 164 | USE_PSA_INIT( ); | 
|  | 165 |  | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 166 | md_alg_psa = mbedtls_psa_translate_md( (mbedtls_md_type_t) md_type ); | 
|  | 167 | TEST_ASSERT( md_alg_psa != MBEDTLS_MD_NONE ); | 
| Andrzej Kurek | 967cfd1 | 2018-11-20 02:53:17 -0500 | [diff] [blame] | 168 |  | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 169 | mbedtls_pk_init( &key ); | 
|  | 170 | TEST_ASSERT( mbedtls_pk_parse_keyfile( &key, key_file, NULL ) == 0 ); | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 171 | TEST_ASSERT( mbedtls_pk_wrap_as_opaque( &key, &key_id, md_alg_psa ) == 0 ); | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 172 |  | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 173 | mbedtls_x509write_csr_set_md_alg( &req, md_type ); | 
|  | 174 | mbedtls_x509write_csr_set_key( &req, &key ); | 
|  | 175 | TEST_ASSERT( mbedtls_x509write_csr_set_subject_name( &req, subject_name ) == 0 ); | 
|  | 176 | if( key_usage != 0 ) | 
|  | 177 | TEST_ASSERT( mbedtls_x509write_csr_set_key_usage( &req, key_usage ) == 0 ); | 
|  | 178 | if( cert_type != 0 ) | 
|  | 179 | TEST_ASSERT( mbedtls_x509write_csr_set_ns_cert_type( &req, cert_type ) == 0 ); | 
|  | 180 |  | 
| Andrzej Kurek | 4b11407 | 2018-11-19 18:04:01 -0500 | [diff] [blame] | 181 | ret = mbedtls_x509write_csr_pem( &req, buf, sizeof( buf ) - 1, | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 182 | mbedtls_test_rnd_pseudo_rand, &rnd_info ); | 
|  | 183 |  | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 184 | TEST_ASSERT( ret == 0 ); | 
|  | 185 |  | 
|  | 186 | pem_len = strlen( (char *) buf ); | 
|  | 187 | buf[pem_len] = '\0'; | 
| Andrzej Kurek | 4b11407 | 2018-11-19 18:04:01 -0500 | [diff] [blame] | 188 | TEST_ASSERT( x509_crt_verifycsr( buf, pem_len + 1 ) == 0 ); | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 189 |  | 
| Manuel Pégourié-Gonnard | feb0396 | 2020-08-20 09:59:33 +0200 | [diff] [blame] | 190 |  | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 191 | exit: | 
|  | 192 | mbedtls_x509write_csr_free( &req ); | 
|  | 193 | mbedtls_pk_free( &key ); | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 194 | psa_destroy_key( key_id ); | 
| Manuel Pégourié-Gonnard | feb0396 | 2020-08-20 09:59:33 +0200 | [diff] [blame] | 195 | PSA_DONE( ); | 
| Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 196 | } | 
|  | 197 | /* END_CASE */ | 
|  | 198 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | /* 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] | 200 | void x509_crt_check( char *subject_key_file, char *subject_pwd, | 
|  | 201 | char *subject_name, char *issuer_key_file, | 
|  | 202 | char *issuer_pwd, char *issuer_name, | 
|  | 203 | char *serial_str, char *not_before, char *not_after, | 
| Andres Amaya Garcia | 7067f81 | 2018-09-26 10:51:16 +0100 | [diff] [blame] | 204 | int md_type, int key_usage, int set_key_usage, | 
|  | 205 | int cert_type, int set_cert_type, int auth_ident, | 
| Darren Krahn | e560be3 | 2020-09-21 17:40:50 -0700 | [diff] [blame] | 206 | int ver, char *cert_check_file, int rsa_alt, int is_ca ) | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 207 | { | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 208 | mbedtls_pk_context subject_key, issuer_key, issuer_key_alt; | 
|  | 209 | mbedtls_pk_context *key = &issuer_key; | 
|  | 210 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | mbedtls_x509write_cert crt; | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 212 | unsigned char buf[4096]; | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 213 | unsigned char check_buf[5000]; | 
| Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 214 | unsigned char *p, *end; | 
|  | 215 | unsigned char tag, sz; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | mbedtls_mpi serial; | 
| Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 217 | int ret, before_tag, after_tag; | 
| Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 218 | size_t olen = 0, pem_len = 0, buf_index = 0; | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 219 | int der_len = -1; | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 220 | FILE *f; | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 221 | mbedtls_test_rnd_pseudo_info rnd_info; | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 222 |  | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 223 | memset( &rnd_info, 0x2a, sizeof( mbedtls_test_rnd_pseudo_info ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | mbedtls_mpi_init( &serial ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 225 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 226 | mbedtls_pk_init( &subject_key ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 227 | mbedtls_pk_init( &issuer_key  ); | 
|  | 228 | mbedtls_pk_init( &issuer_key_alt ); | 
|  | 229 |  | 
|  | 230 | mbedtls_x509write_crt_init( &crt ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 231 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | TEST_ASSERT( mbedtls_pk_parse_keyfile( &subject_key, subject_key_file, | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 233 | subject_pwd ) == 0 ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 234 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 235 | TEST_ASSERT( mbedtls_pk_parse_keyfile( &issuer_key, issuer_key_file, | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 236 | issuer_pwd ) == 0 ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 237 |  | 
| Manuel Pégourié-Gonnard | 147b28e | 2018-03-12 15:26:59 +0100 | [diff] [blame] | 238 | #if defined(MBEDTLS_RSA_C) | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 239 | /* For RSA PK contexts, create a copy as an alternative RSA context. */ | 
|  | 240 | if( rsa_alt == 1 && mbedtls_pk_get_type( &issuer_key ) == MBEDTLS_PK_RSA ) | 
|  | 241 | { | 
|  | 242 | TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &issuer_key_alt, | 
|  | 243 | mbedtls_pk_rsa( issuer_key ), | 
|  | 244 | mbedtls_rsa_decrypt_func, | 
|  | 245 | mbedtls_rsa_sign_func, | 
|  | 246 | mbedtls_rsa_key_len_func ) == 0 ); | 
|  | 247 |  | 
|  | 248 | key = &issuer_key_alt; | 
|  | 249 | } | 
| Manuel Pégourié-Gonnard | 147b28e | 2018-03-12 15:26:59 +0100 | [diff] [blame] | 250 | #else | 
|  | 251 | (void) rsa_alt; | 
|  | 252 | #endif | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 253 |  | 
| Werner Lewis | 24b6078 | 2022-07-07 15:08:17 +0100 | [diff] [blame] | 254 | TEST_ASSERT( mbedtls_test_read_mpi( &serial, serial_str ) == 0 ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 255 |  | 
| Manuel Pégourié-Gonnard | 6c1a73e | 2014-03-28 14:03:22 +0100 | [diff] [blame] | 256 | if( ver != -1 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | mbedtls_x509write_crt_set_version( &crt, ver ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 258 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | TEST_ASSERT( mbedtls_x509write_crt_set_serial( &crt, &serial ) == 0 ); | 
|  | 260 | TEST_ASSERT( mbedtls_x509write_crt_set_validity( &crt, not_before, | 
| Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 261 | not_after ) == 0 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | mbedtls_x509write_crt_set_md_alg( &crt, md_type ); | 
|  | 263 | TEST_ASSERT( mbedtls_x509write_crt_set_issuer_name( &crt, issuer_name ) == 0 ); | 
|  | 264 | TEST_ASSERT( mbedtls_x509write_crt_set_subject_name( &crt, subject_name ) == 0 ); | 
|  | 265 | mbedtls_x509write_crt_set_subject_key( &crt, &subject_key ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 266 |  | 
|  | 267 | mbedtls_x509write_crt_set_issuer_key( &crt, key ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 268 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 269 | if( crt.version >= MBEDTLS_X509_CRT_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | 6c1a73e | 2014-03-28 14:03:22 +0100 | [diff] [blame] | 270 | { | 
| Darren Krahn | 9c134ce | 2021-01-13 22:04:45 -0800 | [diff] [blame] | 271 | /* For the CA case, a path length of -1 means unlimited. */ | 
| Darren Krahn | e560be3 | 2020-09-21 17:40:50 -0700 | [diff] [blame] | 272 | TEST_ASSERT( mbedtls_x509write_crt_set_basic_constraints( &crt, is_ca, | 
|  | 273 | (is_ca ? -1 : 0) ) == 0 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | TEST_ASSERT( mbedtls_x509write_crt_set_subject_key_identifier( &crt ) == 0 ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 275 | if( auth_ident ) | 
|  | 276 | TEST_ASSERT( mbedtls_x509write_crt_set_authority_key_identifier( &crt ) == 0 ); | 
| Andres Amaya Garcia | 7067f81 | 2018-09-26 10:51:16 +0100 | [diff] [blame] | 277 | if( set_key_usage != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 278 | TEST_ASSERT( mbedtls_x509write_crt_set_key_usage( &crt, key_usage ) == 0 ); | 
| Andres Amaya Garcia | 7067f81 | 2018-09-26 10:51:16 +0100 | [diff] [blame] | 279 | if( set_cert_type != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | 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] | 281 | } | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 282 |  | 
| Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 283 | ret = mbedtls_x509write_crt_pem( &crt, buf, sizeof( buf ), | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 284 | mbedtls_test_rnd_pseudo_rand, &rnd_info ); | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 285 | TEST_ASSERT( ret == 0 ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 286 |  | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 287 | pem_len = strlen( (char *) buf ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 288 |  | 
| Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 289 | // check that the rest of the buffer remains clear | 
|  | 290 | for( buf_index = pem_len; buf_index < sizeof( buf ); ++buf_index ) | 
|  | 291 | { | 
|  | 292 | TEST_ASSERT( buf[buf_index] == 0 ); | 
|  | 293 | } | 
|  | 294 |  | 
| Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 295 | if( *cert_check_file != '\0' ) | 
|  | 296 | { | 
|  | 297 | f = fopen( cert_check_file, "r" ); | 
|  | 298 | TEST_ASSERT( f != NULL ); | 
|  | 299 | olen = fread( check_buf, 1, sizeof( check_buf ), f ); | 
|  | 300 | fclose( f ); | 
|  | 301 | TEST_ASSERT( olen < sizeof( check_buf ) ); | 
|  | 302 | TEST_ASSERT( olen >= pem_len - 1 ); | 
|  | 303 | TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 ); | 
|  | 304 | } | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 305 |  | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 306 | der_len = mbedtls_x509write_crt_der( &crt, buf, sizeof( buf ), | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 307 | mbedtls_test_rnd_pseudo_rand, | 
|  | 308 | &rnd_info ); | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 309 | TEST_ASSERT( der_len >= 0 ); | 
|  | 310 |  | 
|  | 311 | if( der_len == 0 ) | 
|  | 312 | goto exit; | 
|  | 313 |  | 
| Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 314 | // Not testing against file, check date format | 
|  | 315 | if( *cert_check_file == '\0' ) | 
|  | 316 | { | 
|  | 317 | // UTC tag if before 2050, 2 digits less for year | 
|  | 318 | if( not_before[0] == '2' && ( not_before[1] > '0' || not_before[2] > '4' ) ) | 
|  | 319 | { | 
|  | 320 | before_tag = MBEDTLS_ASN1_GENERALIZED_TIME; | 
|  | 321 | } | 
|  | 322 | else | 
|  | 323 | { | 
|  | 324 | before_tag = MBEDTLS_ASN1_UTC_TIME; | 
|  | 325 | not_before += 2; | 
|  | 326 | } | 
|  | 327 | if( not_after[0] == '2' && ( not_after[1] > '0' || not_after[2] > '4' ) ) | 
|  | 328 | { | 
|  | 329 | after_tag = MBEDTLS_ASN1_GENERALIZED_TIME; | 
|  | 330 | } | 
|  | 331 | else | 
|  | 332 | { | 
|  | 333 | after_tag = MBEDTLS_ASN1_UTC_TIME; | 
|  | 334 | not_after += 2; | 
|  | 335 | } | 
|  | 336 | end = buf + sizeof( buf ); | 
|  | 337 | for( p = end - der_len ; p < end ; ) | 
|  | 338 | { | 
|  | 339 | tag = *p++; | 
|  | 340 | sz = *p++; | 
|  | 341 | if( tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME ) | 
|  | 342 | { | 
|  | 343 | // Check correct tag and time written | 
|  | 344 | TEST_ASSERT( before_tag == tag ); | 
|  | 345 | TEST_ASSERT( memcmp( p, not_before, sz - 1 ) == 0 ); | 
|  | 346 | p += sz; | 
|  | 347 | tag = *p++; | 
|  | 348 | sz = *p++; | 
|  | 349 | TEST_ASSERT( after_tag == tag ); | 
|  | 350 | TEST_ASSERT( memcmp( p, not_after, sz - 1 ) == 0 ); | 
|  | 351 | break; | 
|  | 352 | } | 
|  | 353 | // Increment if long form ASN1 length | 
|  | 354 | if( sz & 0x80 ) | 
|  | 355 | p += sz & 0x0F; | 
|  | 356 | if( tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) | 
|  | 357 | p += sz; | 
|  | 358 | } | 
|  | 359 | TEST_ASSERT( p < end ); | 
|  | 360 | } | 
|  | 361 |  | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 362 | ret = mbedtls_x509write_crt_der( &crt, buf, (size_t)( der_len - 1 ), | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 363 | mbedtls_test_rnd_pseudo_rand, &rnd_info ); | 
| Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 364 | TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); | 
|  | 365 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 366 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 367 | mbedtls_x509write_crt_free( &crt ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 368 | mbedtls_pk_free( &issuer_key_alt ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 369 | mbedtls_pk_free( &subject_key ); | 
| Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 370 | mbedtls_pk_free( &issuer_key ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 371 | mbedtls_mpi_free( &serial ); | 
| Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 372 | } | 
|  | 373 | /* END_CASE */ | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 374 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 375 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */ | 
| Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 376 | void mbedtls_x509_string_to_names( char * name, char * parsed_name, int result | 
|  | 377 | ) | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 378 | { | 
|  | 379 | int ret; | 
|  | 380 | size_t len = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 381 | mbedtls_asn1_named_data *names = NULL; | 
|  | 382 | mbedtls_x509_name parsed, *parsed_cur, *parsed_prv; | 
| Manuel Pégourié-Gonnard | 4fd0b25 | 2015-06-26 14:15:48 +0200 | [diff] [blame] | 383 | unsigned char buf[1024], out[1024], *c; | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 384 |  | 
|  | 385 | memset( &parsed, 0, sizeof( parsed ) ); | 
| Manuel Pégourié-Gonnard | 4fd0b25 | 2015-06-26 14:15:48 +0200 | [diff] [blame] | 386 | memset( out, 0, sizeof( out ) ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 387 | memset( buf, 0, sizeof( buf ) ); | 
|  | 388 | c = buf + sizeof( buf ); | 
|  | 389 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 390 | ret = mbedtls_x509_string_to_names( &names, name ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 391 | TEST_ASSERT( ret == result ); | 
|  | 392 |  | 
|  | 393 | if( ret != 0 ) | 
|  | 394 | goto exit; | 
|  | 395 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 396 | ret = mbedtls_x509_write_names( &c, buf, names ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 397 | TEST_ASSERT( ret > 0 ); | 
|  | 398 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 399 | TEST_ASSERT( mbedtls_asn1_get_tag( &c, buf + sizeof( buf ), &len, | 
|  | 400 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) == 0 ); | 
|  | 401 | TEST_ASSERT( mbedtls_x509_get_name( &c, buf + sizeof( buf ), &parsed ) == 0 ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 402 |  | 
| Manuel Pégourié-Gonnard | 4fd0b25 | 2015-06-26 14:15:48 +0200 | [diff] [blame] | 403 | ret = mbedtls_x509_dn_gets( (char *) out, sizeof( out ), &parsed ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 404 | TEST_ASSERT( ret > 0 ); | 
|  | 405 |  | 
| Manuel Pégourié-Gonnard | 4fd0b25 | 2015-06-26 14:15:48 +0200 | [diff] [blame] | 406 | TEST_ASSERT( strcmp( (char *) out, parsed_name ) == 0 ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 407 |  | 
|  | 408 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 409 | mbedtls_asn1_free_named_data_list( &names ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 410 |  | 
|  | 411 | parsed_cur = parsed.next; | 
|  | 412 | while( parsed_cur != 0 ) | 
|  | 413 | { | 
|  | 414 | parsed_prv = parsed_cur; | 
|  | 415 | parsed_cur = parsed_cur->next; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | mbedtls_free( parsed_prv ); | 
| Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 417 | } | 
|  | 418 | } | 
|  | 419 | /* END_CASE */ |