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" |
Valerio Setti | d3f7df4 | 2022-10-19 15:14:29 +0200 | [diff] [blame] | 8 | #include "mbedtls/asn1write.h" |
Manuel Pégourié-Gonnard | feb0396 | 2020-08-20 09:59:33 +0200 | [diff] [blame] | 9 | |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 10 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 11 | int mbedtls_rsa_decrypt_func(void *ctx, int mode, size_t *olen, |
| 12 | const unsigned char *input, unsigned char *output, |
| 13 | size_t output_max_len) |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 14 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 15 | return mbedtls_rsa_pkcs1_decrypt((mbedtls_rsa_context *) ctx, NULL, NULL, mode, olen, |
| 16 | input, output, output_max_len); |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 17 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 18 | int mbedtls_rsa_sign_func(void *ctx, |
| 19 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 20 | int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, |
| 21 | const unsigned char *hash, unsigned char *sig) |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 22 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 23 | return mbedtls_rsa_pkcs1_sign((mbedtls_rsa_context *) ctx, f_rng, p_rng, mode, |
| 24 | md_alg, hashlen, hash, sig); |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 25 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 26 | size_t mbedtls_rsa_key_len_func(void *ctx) |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 27 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 28 | return ((const mbedtls_rsa_context *) ctx)->len; |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 29 | } |
| 30 | #endif /* MBEDTLS_RSA_C */ |
| 31 | |
Gilles Peskine | f4e672e | 2020-01-31 14:22:10 +0100 | [diff] [blame] | 32 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 33 | defined(MBEDTLS_PEM_WRITE_C) && defined(MBEDTLS_X509_CSR_WRITE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 34 | static int x509_crt_verifycsr(const unsigned char *buf, size_t buflen) |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 35 | { |
| 36 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
| 37 | const mbedtls_md_info_t *md_info; |
| 38 | mbedtls_x509_csr csr; |
Hanno Becker | bf2dacb | 2019-06-03 16:28:24 +0100 | [diff] [blame] | 39 | int ret = 0; |
| 40 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 41 | mbedtls_x509_csr_init(&csr); |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 42 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 43 | if (mbedtls_x509_csr_parse(&csr, buf, buflen) != 0) { |
Hanno Becker | bf2dacb | 2019-06-03 16:28:24 +0100 | [diff] [blame] | 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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 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 | /* Note: this can't happen except after an internal error */ |
Hanno Becker | bf2dacb | 2019-06-03 16:28:24 +0100 | [diff] [blame] | 51 | ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 52 | goto cleanup; |
Andrzej Kurek | 4b11407 | 2018-11-19 18:04:01 -0500 | [diff] [blame] | 53 | } |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 54 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 55 | if (mbedtls_pk_verify_ext(csr.sig_pk, csr.sig_opts, &csr.pk, |
| 56 | csr.sig_md, hash, mbedtls_md_get_size(md_info), |
| 57 | csr.sig.p, csr.sig.len) != 0) { |
Hanno Becker | bf2dacb | 2019-06-03 16:28:24 +0100 | [diff] [blame] | 58 | ret = MBEDTLS_ERR_X509_CERT_VERIFY_FAILED; |
| 59 | goto cleanup; |
Andrzej Kurek | 4b11407 | 2018-11-19 18:04:01 -0500 | [diff] [blame] | 60 | } |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 61 | |
Hanno Becker | bf2dacb | 2019-06-03 16:28:24 +0100 | [diff] [blame] | 62 | cleanup: |
| 63 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 64 | mbedtls_x509_csr_free(&csr); |
| 65 | return ret; |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 66 | } |
Gilles Peskine | f4e672e | 2020-01-31 14:22:10 +0100 | [diff] [blame] | 67 | #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] | 68 | |
Valerio Setti | d3f7df4 | 2022-10-19 15:14:29 +0200 | [diff] [blame] | 69 | #if defined(MBEDTLS_X509_CSR_WRITE_C) |
| 70 | |
| 71 | /* |
| 72 | * The size of this temporary buffer is given by the sequence of functions |
| 73 | * called hereinafter: |
| 74 | * - mbedtls_asn1_write_oid() |
| 75 | * - 8 bytes for MBEDTLS_OID_EXTENDED_KEY_USAGE raw value |
| 76 | * - 1 byte for MBEDTLS_OID_EXTENDED_KEY_USAGE length |
| 77 | * - 1 byte for MBEDTLS_ASN1_OID tag |
| 78 | * - mbedtls_asn1_write_len() |
| 79 | * - 1 byte since we're dealing with sizes which are less than 0x80 |
| 80 | * - mbedtls_asn1_write_tag() |
| 81 | * - 1 byte |
| 82 | * |
| 83 | * This length is fine as long as this function is called using the |
| 84 | * MBEDTLS_OID_SERVER_AUTH OID. If this is changed in the future, then this |
| 85 | * buffer's length should be adjusted accordingly. |
| 86 | * Unfortunately there's no predefined max size for OIDs which can be used |
| 87 | * to set an overall upper boundary which is always guaranteed. |
| 88 | */ |
| 89 | #define EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH 12 |
| 90 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 91 | static int csr_set_extended_key_usage(mbedtls_x509write_csr *ctx, |
| 92 | const char *oid, size_t oid_len) |
Valerio Setti | d3f7df4 | 2022-10-19 15:14:29 +0200 | [diff] [blame] | 93 | { |
| 94 | unsigned char buf[EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH] = { 0 }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 95 | unsigned char *p = buf + sizeof(buf); |
Valerio Setti | d3f7df4 | 2022-10-19 15:14:29 +0200 | [diff] [blame] | 96 | int ret; |
| 97 | size_t len = 0; |
| 98 | |
| 99 | /* |
| 100 | * Following functions fail anyway if the temporary buffer is not large, |
| 101 | * but we set an extra check here to emphasize a possible source of errors |
| 102 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 103 | if (oid_len > EXT_KEY_USAGE_TMP_BUF_MAX_LENGTH) { |
Valerio Setti | d3f7df4 | 2022-10-19 15:14:29 +0200 | [diff] [blame] | 104 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 105 | } |
| 106 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 107 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(&p, buf, oid, oid_len)); |
| 108 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, ret)); |
| 109 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf, |
| 110 | MBEDTLS_ASN1_CONSTRUCTED | |
| 111 | MBEDTLS_ASN1_SEQUENCE)); |
Valerio Setti | d3f7df4 | 2022-10-19 15:14:29 +0200 | [diff] [blame] | 112 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 113 | ret = mbedtls_x509write_csr_set_extension(ctx, |
| 114 | MBEDTLS_OID_EXTENDED_KEY_USAGE, |
| 115 | MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE), |
| 116 | p, |
| 117 | len); |
Valerio Setti | d3f7df4 | 2022-10-19 15:14:29 +0200 | [diff] [blame] | 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | #endif /* MBEDTLS_X509_CSR_WRITE_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 122 | /* END_HEADER */ |
Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 123 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 124 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 125 | * depends_on:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO:MBEDTLS_PK_PARSE_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 126 | * END_DEPENDENCIES |
| 127 | */ |
Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 128 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | /* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 130 | void x509_csr_check(char *key_file, char *cert_req_check_file, int md_type, |
| 131 | int key_usage, int set_key_usage, int cert_type, |
| 132 | int set_cert_type, int set_extension) |
Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 133 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | mbedtls_pk_context key; |
| 135 | mbedtls_x509write_csr req; |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 136 | unsigned char buf[4096]; |
Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 137 | unsigned char check_buf[4000]; |
| 138 | int ret; |
Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 139 | size_t olen = 0, pem_len = 0, buf_index; |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 140 | int der_len = -1; |
Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 141 | FILE *f; |
Paul Bakker | 3a8cb6f | 2013-12-30 20:41:54 +0100 | [diff] [blame] | 142 | const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1"; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 143 | mbedtls_test_rnd_pseudo_info rnd_info; |
Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 144 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 145 | memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info)); |
Manuel Pégourié-Gonnard | ee73179 | 2013-09-11 22:48:40 +0200 | [diff] [blame] | 146 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 147 | mbedtls_x509write_csr_init(&req); |
Neil Armstrong | 1104866 | 2022-07-20 15:49:49 +0200 | [diff] [blame] | 148 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 149 | mbedtls_pk_init(&key); |
| 150 | TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0); |
Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 151 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 152 | mbedtls_x509write_csr_set_md_alg(&req, md_type); |
| 153 | mbedtls_x509write_csr_set_key(&req, &key); |
| 154 | TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0); |
| 155 | if (set_key_usage != 0) { |
| 156 | TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0); |
| 157 | } |
| 158 | if (set_cert_type != 0) { |
| 159 | TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0); |
| 160 | } |
| 161 | if (set_extension != 0) { |
| 162 | TEST_ASSERT(csr_set_extended_key_usage(&req, MBEDTLS_OID_SERVER_AUTH, |
| 163 | MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) == 0); |
Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 166 | ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf), |
| 167 | mbedtls_test_rnd_pseudo_rand, &rnd_info); |
| 168 | TEST_ASSERT(ret == 0); |
Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 169 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 170 | pem_len = strlen((char *) buf); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 171 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 172 | for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) { |
| 173 | TEST_ASSERT(buf[buf_index] == 0); |
| 174 | } |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 175 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 176 | f = fopen(cert_req_check_file, "r"); |
| 177 | TEST_ASSERT(f != NULL); |
| 178 | olen = fread(check_buf, 1, sizeof(check_buf), f); |
| 179 | fclose(f); |
| 180 | |
| 181 | TEST_ASSERT(olen >= pem_len - 1); |
| 182 | TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0); |
| 183 | |
| 184 | der_len = mbedtls_x509write_csr_der(&req, buf, sizeof(buf), |
| 185 | mbedtls_test_rnd_pseudo_rand, |
| 186 | &rnd_info); |
| 187 | TEST_ASSERT(der_len >= 0); |
| 188 | |
| 189 | if (der_len == 0) { |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 190 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 191 | } |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 192 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 193 | ret = mbedtls_x509write_csr_der(&req, buf, (size_t) (der_len - 1), |
| 194 | mbedtls_test_rnd_pseudo_rand, &rnd_info); |
| 195 | TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL); |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 196 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 197 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 198 | mbedtls_x509write_csr_free(&req); |
| 199 | mbedtls_pk_free(&key); |
Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 200 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 201 | /* END_CASE */ |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 202 | |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 203 | /* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CSR_WRITE_C:MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 204 | void x509_csr_check_opaque(char *key_file, int md_type, int key_usage, |
| 205 | int cert_type) |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 206 | { |
| 207 | mbedtls_pk_context key; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 208 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 209 | psa_algorithm_t md_alg_psa; |
| 210 | mbedtls_x509write_csr req; |
| 211 | unsigned char buf[4096]; |
| 212 | int ret; |
| 213 | size_t pem_len = 0; |
| 214 | const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1"; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 215 | mbedtls_test_rnd_pseudo_info rnd_info; |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 216 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 217 | memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info)); |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 218 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 219 | mbedtls_x509write_csr_init(&req); |
Neil Armstrong | 1104866 | 2022-07-20 15:49:49 +0200 | [diff] [blame] | 220 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 221 | USE_PSA_INIT(); |
Neil Armstrong | 1104866 | 2022-07-20 15:49:49 +0200 | [diff] [blame] | 222 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 223 | md_alg_psa = mbedtls_psa_translate_md((mbedtls_md_type_t) md_type); |
| 224 | TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE); |
Andrzej Kurek | 967cfd1 | 2018-11-20 02:53:17 -0500 | [diff] [blame] | 225 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 226 | mbedtls_pk_init(&key); |
| 227 | TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0); |
| 228 | 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] | 229 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 230 | mbedtls_x509write_csr_set_md_alg(&req, md_type); |
| 231 | mbedtls_x509write_csr_set_key(&req, &key); |
| 232 | TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0); |
| 233 | if (key_usage != 0) { |
| 234 | TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0); |
| 235 | } |
| 236 | if (cert_type != 0) { |
| 237 | TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0); |
| 238 | } |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 239 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 240 | ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf) - 1, |
| 241 | mbedtls_test_rnd_pseudo_rand, &rnd_info); |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 242 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 243 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 244 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 245 | pem_len = strlen((char *) buf); |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 246 | buf[pem_len] = '\0'; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 247 | TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0); |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 248 | |
Manuel Pégourié-Gonnard | feb0396 | 2020-08-20 09:59:33 +0200 | [diff] [blame] | 249 | |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 250 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 251 | mbedtls_x509write_csr_free(&req); |
| 252 | mbedtls_pk_free(&key); |
| 253 | psa_destroy_key(key_id); |
| 254 | PSA_DONE(); |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 255 | } |
| 256 | /* END_CASE */ |
| 257 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 258 | /* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C:MBEDTLS_X509_CRT_WRITE_C:MBEDTLS_SHA1_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 259 | void x509_crt_check(char *subject_key_file, char *subject_pwd, |
| 260 | char *subject_name, char *issuer_key_file, |
| 261 | char *issuer_pwd, char *issuer_name, |
| 262 | char *serial_str, char *not_before, char *not_after, |
| 263 | int md_type, int key_usage, int set_key_usage, |
| 264 | int cert_type, int set_cert_type, int auth_ident, |
| 265 | int ver, char *cert_check_file, int rsa_alt, int is_ca) |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 266 | { |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 267 | mbedtls_pk_context subject_key, issuer_key, issuer_key_alt; |
| 268 | mbedtls_pk_context *key = &issuer_key; |
| 269 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | mbedtls_x509write_cert crt; |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 271 | unsigned char buf[4096]; |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 272 | unsigned char check_buf[5000]; |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 273 | unsigned char *p, *end; |
| 274 | unsigned char tag, sz; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 275 | mbedtls_mpi serial; |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 276 | int ret, before_tag, after_tag; |
Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 277 | size_t olen = 0, pem_len = 0, buf_index = 0; |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 278 | int der_len = -1; |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 279 | FILE *f; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 280 | mbedtls_test_rnd_pseudo_info rnd_info; |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 281 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 282 | memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info)); |
| 283 | mbedtls_mpi_init(&serial); |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 284 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 285 | mbedtls_pk_init(&subject_key); |
| 286 | mbedtls_pk_init(&issuer_key); |
| 287 | mbedtls_pk_init(&issuer_key_alt); |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 288 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 289 | mbedtls_x509write_crt_init(&crt); |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 290 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 291 | TEST_ASSERT(mbedtls_pk_parse_keyfile(&subject_key, subject_key_file, |
| 292 | subject_pwd) == 0); |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 293 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 294 | TEST_ASSERT(mbedtls_pk_parse_keyfile(&issuer_key, issuer_key_file, |
| 295 | issuer_pwd) == 0); |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 296 | |
Manuel Pégourié-Gonnard | 147b28e | 2018-03-12 15:26:59 +0100 | [diff] [blame] | 297 | #if defined(MBEDTLS_RSA_C) |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 298 | /* For RSA PK contexts, create a copy as an alternative RSA context. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 299 | if (rsa_alt == 1 && mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_RSA) { |
| 300 | TEST_ASSERT(mbedtls_pk_setup_rsa_alt(&issuer_key_alt, |
| 301 | mbedtls_pk_rsa(issuer_key), |
| 302 | mbedtls_rsa_decrypt_func, |
| 303 | mbedtls_rsa_sign_func, |
| 304 | mbedtls_rsa_key_len_func) == 0); |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 305 | |
| 306 | key = &issuer_key_alt; |
| 307 | } |
Manuel Pégourié-Gonnard | 147b28e | 2018-03-12 15:26:59 +0100 | [diff] [blame] | 308 | #else |
| 309 | (void) rsa_alt; |
| 310 | #endif |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 311 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 312 | TEST_ASSERT(mbedtls_test_read_mpi(&serial, serial_str) == 0); |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 314 | if (ver != -1) { |
| 315 | mbedtls_x509write_crt_set_version(&crt, ver); |
Manuel Pégourié-Gonnard | 6c1a73e | 2014-03-28 14:03:22 +0100 | [diff] [blame] | 316 | } |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 317 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 318 | TEST_ASSERT(mbedtls_x509write_crt_set_serial(&crt, &serial) == 0); |
| 319 | TEST_ASSERT(mbedtls_x509write_crt_set_validity(&crt, not_before, |
| 320 | not_after) == 0); |
| 321 | mbedtls_x509write_crt_set_md_alg(&crt, md_type); |
| 322 | TEST_ASSERT(mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0); |
| 323 | TEST_ASSERT(mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0); |
| 324 | mbedtls_x509write_crt_set_subject_key(&crt, &subject_key); |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 325 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 326 | mbedtls_x509write_crt_set_issuer_key(&crt, key); |
| 327 | |
| 328 | if (crt.version >= MBEDTLS_X509_CRT_VERSION_3) { |
| 329 | /* For the CA case, a path length of -1 means unlimited. */ |
| 330 | TEST_ASSERT(mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca, |
| 331 | (is_ca ? -1 : 0)) == 0); |
| 332 | TEST_ASSERT(mbedtls_x509write_crt_set_subject_key_identifier(&crt) == 0); |
| 333 | if (auth_ident) { |
| 334 | TEST_ASSERT(mbedtls_x509write_crt_set_authority_key_identifier(&crt) == 0); |
| 335 | } |
| 336 | if (set_key_usage != 0) { |
| 337 | TEST_ASSERT(mbedtls_x509write_crt_set_key_usage(&crt, key_usage) == 0); |
| 338 | } |
| 339 | if (set_cert_type != 0) { |
| 340 | TEST_ASSERT(mbedtls_x509write_crt_set_ns_cert_type(&crt, cert_type) == 0); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | ret = mbedtls_x509write_crt_pem(&crt, buf, sizeof(buf), |
| 345 | mbedtls_test_rnd_pseudo_rand, &rnd_info); |
| 346 | TEST_ASSERT(ret == 0); |
| 347 | |
| 348 | pem_len = strlen((char *) buf); |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 349 | |
Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 350 | // check that the rest of the buffer remains clear |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 351 | for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) { |
| 352 | TEST_ASSERT(buf[buf_index] == 0); |
Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 355 | if (*cert_check_file != '\0') { |
| 356 | f = fopen(cert_check_file, "r"); |
| 357 | TEST_ASSERT(f != NULL); |
| 358 | olen = fread(check_buf, 1, sizeof(check_buf), f); |
| 359 | fclose(f); |
| 360 | TEST_ASSERT(olen < sizeof(check_buf)); |
| 361 | TEST_ASSERT(olen >= pem_len - 1); |
| 362 | TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0); |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 363 | } |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 364 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 365 | der_len = mbedtls_x509write_crt_der(&crt, buf, sizeof(buf), |
| 366 | mbedtls_test_rnd_pseudo_rand, |
| 367 | &rnd_info); |
| 368 | TEST_ASSERT(der_len >= 0); |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 369 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 370 | if (der_len == 0) { |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 371 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 372 | } |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 373 | |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 374 | // Not testing against file, check date format |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 375 | if (*cert_check_file == '\0') { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 376 | // UTC tag if before 2050, 2 digits less for year |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 377 | if (not_before[0] == '2' && (not_before[1] > '0' || not_before[2] > '4')) { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 378 | before_tag = MBEDTLS_ASN1_GENERALIZED_TIME; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 379 | } else { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 380 | before_tag = MBEDTLS_ASN1_UTC_TIME; |
| 381 | not_before += 2; |
| 382 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 383 | if (not_after[0] == '2' && (not_after[1] > '0' || not_after[2] > '4')) { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 384 | after_tag = MBEDTLS_ASN1_GENERALIZED_TIME; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 385 | } else { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 386 | after_tag = MBEDTLS_ASN1_UTC_TIME; |
| 387 | not_after += 2; |
| 388 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 389 | end = buf + sizeof(buf); |
| 390 | for (p = end - der_len; p < end;) { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 391 | tag = *p++; |
| 392 | sz = *p++; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 393 | if (tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME) { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 394 | // Check correct tag and time written |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 395 | TEST_ASSERT(before_tag == tag); |
| 396 | TEST_ASSERT(memcmp(p, not_before, sz - 1) == 0); |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 397 | p += sz; |
| 398 | tag = *p++; |
| 399 | sz = *p++; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 400 | TEST_ASSERT(after_tag == tag); |
| 401 | TEST_ASSERT(memcmp(p, not_after, sz - 1) == 0); |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 402 | break; |
| 403 | } |
| 404 | // Increment if long form ASN1 length |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 405 | if (sz & 0x80) { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 406 | p += sz & 0x0F; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 407 | } |
| 408 | if (tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 409 | p += sz; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 410 | } |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 411 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 412 | TEST_ASSERT(p < end); |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 415 | ret = mbedtls_x509write_crt_der(&crt, buf, (size_t) (der_len - 1), |
| 416 | mbedtls_test_rnd_pseudo_rand, &rnd_info); |
| 417 | TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL); |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 418 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 419 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 420 | mbedtls_x509write_crt_free(&crt); |
| 421 | mbedtls_pk_free(&issuer_key_alt); |
| 422 | mbedtls_pk_free(&subject_key); |
| 423 | mbedtls_pk_free(&issuer_key); |
| 424 | mbedtls_mpi_free(&serial); |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 425 | } |
| 426 | /* END_CASE */ |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 427 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 428 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 429 | void mbedtls_x509_string_to_names(char *name, char *parsed_name, int result |
| 430 | ) |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 431 | { |
| 432 | int ret; |
| 433 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 434 | mbedtls_asn1_named_data *names = NULL; |
| 435 | mbedtls_x509_name parsed, *parsed_cur, *parsed_prv; |
Manuel Pégourié-Gonnard | 4fd0b25 | 2015-06-26 14:15:48 +0200 | [diff] [blame] | 436 | unsigned char buf[1024], out[1024], *c; |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 437 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 438 | memset(&parsed, 0, sizeof(parsed)); |
| 439 | memset(out, 0, sizeof(out)); |
| 440 | memset(buf, 0, sizeof(buf)); |
| 441 | c = buf + sizeof(buf); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 442 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 443 | ret = mbedtls_x509_string_to_names(&names, name); |
| 444 | TEST_ASSERT(ret == result); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 445 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 446 | if (ret != 0) { |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 447 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 448 | } |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 449 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 450 | ret = mbedtls_x509_write_names(&c, buf, names); |
| 451 | TEST_ASSERT(ret > 0); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 452 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 453 | TEST_ASSERT(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len, |
| 454 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) == 0); |
| 455 | TEST_ASSERT(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed) == 0); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 456 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 457 | ret = mbedtls_x509_dn_gets((char *) out, sizeof(out), &parsed); |
| 458 | TEST_ASSERT(ret > 0); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 459 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 460 | TEST_ASSERT(strcmp((char *) out, parsed_name) == 0); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 461 | |
| 462 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 463 | mbedtls_asn1_free_named_data_list(&names); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 464 | |
| 465 | parsed_cur = parsed.next; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 466 | while (parsed_cur != 0) { |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 467 | parsed_prv = parsed_cur; |
| 468 | parsed_cur = parsed_cur->next; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 469 | mbedtls_free(parsed_prv); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | /* END_CASE */ |