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); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 148 | mbedtls_pk_init(&key); |
Valerio Setti | e7373a8 | 2023-04-19 14:53:36 +0200 | [diff] [blame] | 149 | USE_PSA_INIT(); |
| 150 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 151 | TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0); |
Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 152 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 153 | mbedtls_x509write_csr_set_md_alg(&req, md_type); |
| 154 | mbedtls_x509write_csr_set_key(&req, &key); |
| 155 | TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0); |
| 156 | if (set_key_usage != 0) { |
| 157 | TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0); |
| 158 | } |
| 159 | if (set_cert_type != 0) { |
| 160 | TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0); |
| 161 | } |
| 162 | if (set_extension != 0) { |
| 163 | TEST_ASSERT(csr_set_extended_key_usage(&req, MBEDTLS_OID_SERVER_AUTH, |
| 164 | MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) == 0); |
Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 167 | ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf), |
| 168 | mbedtls_test_rnd_pseudo_rand, &rnd_info); |
| 169 | TEST_ASSERT(ret == 0); |
Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 170 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 171 | pem_len = strlen((char *) buf); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 172 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 173 | for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) { |
| 174 | TEST_ASSERT(buf[buf_index] == 0); |
| 175 | } |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 176 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 177 | f = fopen(cert_req_check_file, "r"); |
| 178 | TEST_ASSERT(f != NULL); |
| 179 | olen = fread(check_buf, 1, sizeof(check_buf), f); |
| 180 | fclose(f); |
| 181 | |
| 182 | TEST_ASSERT(olen >= pem_len - 1); |
| 183 | TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0); |
| 184 | |
| 185 | der_len = mbedtls_x509write_csr_der(&req, buf, sizeof(buf), |
| 186 | mbedtls_test_rnd_pseudo_rand, |
| 187 | &rnd_info); |
| 188 | TEST_ASSERT(der_len >= 0); |
| 189 | |
| 190 | if (der_len == 0) { |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 191 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 192 | } |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 193 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 194 | ret = mbedtls_x509write_csr_der(&req, buf, (size_t) (der_len - 1), |
| 195 | mbedtls_test_rnd_pseudo_rand, &rnd_info); |
| 196 | TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL); |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 197 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 198 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 199 | mbedtls_x509write_csr_free(&req); |
| 200 | mbedtls_pk_free(&key); |
Valerio Setti | e7373a8 | 2023-04-19 14:53:36 +0200 | [diff] [blame] | 201 | USE_PSA_DONE(); |
Paul Bakker | 6d62050 | 2012-02-16 14:09:13 +0000 | [diff] [blame] | 202 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 203 | /* END_CASE */ |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 204 | |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 205 | /* 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] | 206 | void x509_csr_check_opaque(char *key_file, int md_type, int key_usage, |
| 207 | int cert_type) |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 208 | { |
| 209 | mbedtls_pk_context key; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 210 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 211 | psa_algorithm_t md_alg_psa; |
| 212 | mbedtls_x509write_csr req; |
| 213 | unsigned char buf[4096]; |
| 214 | int ret; |
| 215 | size_t pem_len = 0; |
| 216 | const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1"; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 217 | mbedtls_test_rnd_pseudo_info rnd_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 | memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info)); |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 220 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 221 | mbedtls_x509write_csr_init(&req); |
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 | USE_PSA_INIT(); |
Neil Armstrong | 1104866 | 2022-07-20 15:49:49 +0200 | [diff] [blame] | 224 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 225 | md_alg_psa = mbedtls_psa_translate_md((mbedtls_md_type_t) md_type); |
| 226 | TEST_ASSERT(md_alg_psa != MBEDTLS_MD_NONE); |
Andrzej Kurek | 967cfd1 | 2018-11-20 02:53:17 -0500 | [diff] [blame] | 227 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 228 | mbedtls_pk_init(&key); |
| 229 | TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL) == 0); |
| 230 | 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] | 231 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 232 | mbedtls_x509write_csr_set_md_alg(&req, md_type); |
| 233 | mbedtls_x509write_csr_set_key(&req, &key); |
| 234 | TEST_ASSERT(mbedtls_x509write_csr_set_subject_name(&req, subject_name) == 0); |
| 235 | if (key_usage != 0) { |
| 236 | TEST_ASSERT(mbedtls_x509write_csr_set_key_usage(&req, key_usage) == 0); |
| 237 | } |
| 238 | if (cert_type != 0) { |
| 239 | TEST_ASSERT(mbedtls_x509write_csr_set_ns_cert_type(&req, cert_type) == 0); |
| 240 | } |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 241 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 242 | ret = mbedtls_x509write_csr_pem(&req, buf, sizeof(buf) - 1, |
| 243 | mbedtls_test_rnd_pseudo_rand, &rnd_info); |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 244 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 245 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 246 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 247 | pem_len = strlen((char *) buf); |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 248 | buf[pem_len] = '\0'; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 249 | TEST_ASSERT(x509_crt_verifycsr(buf, pem_len + 1) == 0); |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 250 | |
| 251 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 252 | mbedtls_x509write_csr_free(&req); |
| 253 | mbedtls_pk_free(&key); |
| 254 | psa_destroy_key(key_id); |
Valerio Setti | e7373a8 | 2023-04-19 14:53:36 +0200 | [diff] [blame] | 255 | USE_PSA_DONE(); |
Andrzej Kurek | 5f7bad3 | 2018-11-19 10:12:37 -0500 | [diff] [blame] | 256 | } |
| 257 | /* END_CASE */ |
| 258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | /* 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] | 260 | void x509_crt_check(char *subject_key_file, char *subject_pwd, |
| 261 | char *subject_name, char *issuer_key_file, |
| 262 | char *issuer_pwd, char *issuer_name, |
| 263 | char *serial_str, char *not_before, char *not_after, |
| 264 | int md_type, int key_usage, int set_key_usage, |
| 265 | int cert_type, int set_cert_type, int auth_ident, |
| 266 | int ver, char *cert_check_file, int rsa_alt, int is_ca) |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 267 | { |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 268 | mbedtls_pk_context subject_key, issuer_key, issuer_key_alt; |
| 269 | mbedtls_pk_context *key = &issuer_key; |
| 270 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | mbedtls_x509write_cert crt; |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 272 | unsigned char buf[4096]; |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 273 | unsigned char check_buf[5000]; |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 274 | unsigned char *p, *end; |
| 275 | unsigned char tag, sz; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | mbedtls_mpi serial; |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 277 | int ret, before_tag, after_tag; |
Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 278 | size_t olen = 0, pem_len = 0, buf_index = 0; |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 279 | int der_len = -1; |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 280 | FILE *f; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 281 | mbedtls_test_rnd_pseudo_info rnd_info; |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 282 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 283 | memset(&rnd_info, 0x2a, sizeof(mbedtls_test_rnd_pseudo_info)); |
| 284 | mbedtls_mpi_init(&serial); |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 285 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 286 | mbedtls_pk_init(&subject_key); |
| 287 | mbedtls_pk_init(&issuer_key); |
| 288 | mbedtls_pk_init(&issuer_key_alt); |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 289 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 290 | mbedtls_x509write_crt_init(&crt); |
Valerio Setti | e7373a8 | 2023-04-19 14:53:36 +0200 | [diff] [blame] | 291 | USE_PSA_INIT(); |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 292 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 293 | TEST_ASSERT(mbedtls_pk_parse_keyfile(&subject_key, subject_key_file, |
| 294 | subject_pwd) == 0); |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 295 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 296 | TEST_ASSERT(mbedtls_pk_parse_keyfile(&issuer_key, issuer_key_file, |
| 297 | issuer_pwd) == 0); |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 298 | |
Dave Rodgman | bd2b8e4 | 2023-01-20 11:39:00 +0000 | [diff] [blame] | 299 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 300 | /* For RSA PK contexts, create a copy as an alternative RSA context. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 301 | if (rsa_alt == 1 && mbedtls_pk_get_type(&issuer_key) == MBEDTLS_PK_RSA) { |
| 302 | TEST_ASSERT(mbedtls_pk_setup_rsa_alt(&issuer_key_alt, |
| 303 | mbedtls_pk_rsa(issuer_key), |
| 304 | mbedtls_rsa_decrypt_func, |
| 305 | mbedtls_rsa_sign_func, |
| 306 | mbedtls_rsa_key_len_func) == 0); |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 307 | |
| 308 | key = &issuer_key_alt; |
| 309 | } |
Manuel Pégourié-Gonnard | 147b28e | 2018-03-12 15:26:59 +0100 | [diff] [blame] | 310 | #else |
| 311 | (void) rsa_alt; |
| 312 | #endif |
Hanno Becker | 418a622 | 2017-09-14 07:51:28 +0100 | [diff] [blame] | 313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 314 | TEST_ASSERT(mbedtls_test_read_mpi(&serial, serial_str) == 0); |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 315 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 316 | if (ver != -1) { |
| 317 | mbedtls_x509write_crt_set_version(&crt, ver); |
Manuel Pégourié-Gonnard | 6c1a73e | 2014-03-28 14:03:22 +0100 | [diff] [blame] | 318 | } |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 319 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 320 | TEST_ASSERT(mbedtls_x509write_crt_set_serial(&crt, &serial) == 0); |
| 321 | TEST_ASSERT(mbedtls_x509write_crt_set_validity(&crt, not_before, |
| 322 | not_after) == 0); |
| 323 | mbedtls_x509write_crt_set_md_alg(&crt, md_type); |
| 324 | TEST_ASSERT(mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name) == 0); |
| 325 | TEST_ASSERT(mbedtls_x509write_crt_set_subject_name(&crt, subject_name) == 0); |
| 326 | mbedtls_x509write_crt_set_subject_key(&crt, &subject_key); |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 327 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 328 | mbedtls_x509write_crt_set_issuer_key(&crt, key); |
| 329 | |
| 330 | if (crt.version >= MBEDTLS_X509_CRT_VERSION_3) { |
| 331 | /* For the CA case, a path length of -1 means unlimited. */ |
| 332 | TEST_ASSERT(mbedtls_x509write_crt_set_basic_constraints(&crt, is_ca, |
| 333 | (is_ca ? -1 : 0)) == 0); |
| 334 | TEST_ASSERT(mbedtls_x509write_crt_set_subject_key_identifier(&crt) == 0); |
| 335 | if (auth_ident) { |
| 336 | TEST_ASSERT(mbedtls_x509write_crt_set_authority_key_identifier(&crt) == 0); |
| 337 | } |
| 338 | if (set_key_usage != 0) { |
| 339 | TEST_ASSERT(mbedtls_x509write_crt_set_key_usage(&crt, key_usage) == 0); |
| 340 | } |
| 341 | if (set_cert_type != 0) { |
| 342 | TEST_ASSERT(mbedtls_x509write_crt_set_ns_cert_type(&crt, cert_type) == 0); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | ret = mbedtls_x509write_crt_pem(&crt, buf, sizeof(buf), |
| 347 | mbedtls_test_rnd_pseudo_rand, &rnd_info); |
| 348 | TEST_ASSERT(ret == 0); |
| 349 | |
| 350 | pem_len = strlen((char *) buf); |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 351 | |
Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 352 | // check that the rest of the buffer remains clear |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 353 | for (buf_index = pem_len; buf_index < sizeof(buf); ++buf_index) { |
| 354 | TEST_ASSERT(buf[buf_index] == 0); |
Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 357 | if (*cert_check_file != '\0') { |
| 358 | f = fopen(cert_check_file, "r"); |
| 359 | TEST_ASSERT(f != NULL); |
| 360 | olen = fread(check_buf, 1, sizeof(check_buf), f); |
| 361 | fclose(f); |
| 362 | TEST_ASSERT(olen < sizeof(check_buf)); |
| 363 | TEST_ASSERT(olen >= pem_len - 1); |
| 364 | TEST_ASSERT(memcmp(buf, check_buf, pem_len - 1) == 0); |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 365 | } |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 366 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 367 | der_len = mbedtls_x509write_crt_der(&crt, buf, sizeof(buf), |
| 368 | mbedtls_test_rnd_pseudo_rand, |
| 369 | &rnd_info); |
| 370 | TEST_ASSERT(der_len >= 0); |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 371 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 372 | if (der_len == 0) { |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 373 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 374 | } |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 375 | |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 376 | // Not testing against file, check date format |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 377 | if (*cert_check_file == '\0') { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 378 | // UTC tag if before 2050, 2 digits less for year |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 379 | 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] | 380 | before_tag = MBEDTLS_ASN1_GENERALIZED_TIME; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 381 | } else { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 382 | before_tag = MBEDTLS_ASN1_UTC_TIME; |
| 383 | not_before += 2; |
| 384 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 385 | 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] | 386 | after_tag = MBEDTLS_ASN1_GENERALIZED_TIME; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 387 | } else { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 388 | after_tag = MBEDTLS_ASN1_UTC_TIME; |
| 389 | not_after += 2; |
| 390 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 391 | end = buf + sizeof(buf); |
| 392 | for (p = end - der_len; p < end;) { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 393 | tag = *p++; |
| 394 | sz = *p++; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 395 | if (tag == MBEDTLS_ASN1_UTC_TIME || tag == MBEDTLS_ASN1_GENERALIZED_TIME) { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 396 | // Check correct tag and time written |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 397 | TEST_ASSERT(before_tag == tag); |
| 398 | TEST_ASSERT(memcmp(p, not_before, sz - 1) == 0); |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 399 | p += sz; |
| 400 | tag = *p++; |
| 401 | sz = *p++; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 402 | TEST_ASSERT(after_tag == tag); |
| 403 | TEST_ASSERT(memcmp(p, not_after, sz - 1) == 0); |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 404 | break; |
| 405 | } |
| 406 | // Increment if long form ASN1 length |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 407 | if (sz & 0x80) { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 408 | p += sz & 0x0F; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 409 | } |
| 410 | if (tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 411 | p += sz; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 412 | } |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 413 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 414 | TEST_ASSERT(p < end); |
Werner Lewis | 1b54a05 | 2022-05-10 12:23:13 +0100 | [diff] [blame] | 415 | } |
| 416 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 417 | ret = mbedtls_x509write_crt_der(&crt, buf, (size_t) (der_len - 1), |
| 418 | mbedtls_test_rnd_pseudo_rand, &rnd_info); |
| 419 | TEST_ASSERT(ret == MBEDTLS_ERR_ASN1_BUF_TOO_SMALL); |
Andres AG | e0af995 | 2016-09-07 11:09:44 +0100 | [diff] [blame] | 420 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 421 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 422 | mbedtls_x509write_crt_free(&crt); |
| 423 | mbedtls_pk_free(&issuer_key_alt); |
| 424 | mbedtls_pk_free(&subject_key); |
| 425 | mbedtls_pk_free(&issuer_key); |
| 426 | mbedtls_mpi_free(&serial); |
Valerio Setti | e7373a8 | 2023-04-19 14:53:36 +0200 | [diff] [blame] | 427 | USE_PSA_DONE(); |
Paul Bakker | 2397cf3 | 2013-09-08 15:58:15 +0200 | [diff] [blame] | 428 | } |
| 429 | /* END_CASE */ |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 430 | |
Valerio Setti | 5b78714 | 2023-01-13 08:40:26 +0100 | [diff] [blame] | 431 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_WRITE_C */ |
| 432 | void x509_set_serial_check() |
| 433 | { |
| 434 | mbedtls_x509write_cert ctx; |
| 435 | mbedtls_mpi serial_mpi; |
| 436 | uint8_t invalid_serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN + 1]; |
| 437 | |
Valerio Setti | e7373a8 | 2023-04-19 14:53:36 +0200 | [diff] [blame] | 438 | USE_PSA_INIT(); |
Valerio Setti | 5b78714 | 2023-01-13 08:40:26 +0100 | [diff] [blame] | 439 | memset(invalid_serial, 0x01, sizeof(invalid_serial)); |
| 440 | |
| 441 | mbedtls_mpi_init(&serial_mpi); |
| 442 | TEST_EQUAL(mbedtls_mpi_read_binary(&serial_mpi, invalid_serial, |
| 443 | sizeof(invalid_serial)), 0); |
| 444 | TEST_EQUAL(mbedtls_x509write_crt_set_serial(&ctx, &serial_mpi), |
| 445 | MBEDTLS_ERR_X509_BAD_INPUT_DATA); |
Valerio Setti | 7ba0037 | 2023-01-26 18:03:27 +0100 | [diff] [blame] | 446 | |
| 447 | exit: |
Valerio Setti | 5b78714 | 2023-01-13 08:40:26 +0100 | [diff] [blame] | 448 | mbedtls_mpi_free(&serial_mpi); |
Valerio Setti | e7373a8 | 2023-04-19 14:53:36 +0200 | [diff] [blame] | 449 | USE_PSA_DONE(); |
Valerio Setti | 5b78714 | 2023-01-13 08:40:26 +0100 | [diff] [blame] | 450 | } |
| 451 | /* END_CASE */ |
| 452 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 453 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 454 | void mbedtls_x509_string_to_names(char *name, char *parsed_name, int result |
| 455 | ) |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 456 | { |
| 457 | int ret; |
| 458 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | mbedtls_asn1_named_data *names = NULL; |
| 460 | mbedtls_x509_name parsed, *parsed_cur, *parsed_prv; |
Manuel Pégourié-Gonnard | 4fd0b25 | 2015-06-26 14:15:48 +0200 | [diff] [blame] | 461 | unsigned char buf[1024], out[1024], *c; |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 462 | |
Valerio Setti | e7373a8 | 2023-04-19 14:53:36 +0200 | [diff] [blame] | 463 | USE_PSA_INIT(); |
| 464 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 465 | memset(&parsed, 0, sizeof(parsed)); |
| 466 | memset(out, 0, sizeof(out)); |
| 467 | memset(buf, 0, sizeof(buf)); |
| 468 | c = buf + sizeof(buf); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 469 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 470 | ret = mbedtls_x509_string_to_names(&names, name); |
| 471 | TEST_ASSERT(ret == result); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 472 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 473 | if (ret != 0) { |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 474 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 475 | } |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 476 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 477 | ret = mbedtls_x509_write_names(&c, buf, names); |
| 478 | TEST_ASSERT(ret > 0); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 479 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 480 | TEST_ASSERT(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len, |
| 481 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) == 0); |
| 482 | TEST_ASSERT(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed) == 0); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 483 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 484 | ret = mbedtls_x509_dn_gets((char *) out, sizeof(out), &parsed); |
| 485 | TEST_ASSERT(ret > 0); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 486 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 487 | TEST_ASSERT(strcmp((char *) out, parsed_name) == 0); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 488 | |
| 489 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 490 | mbedtls_asn1_free_named_data_list(&names); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 491 | |
| 492 | parsed_cur = parsed.next; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 493 | while (parsed_cur != 0) { |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 494 | parsed_prv = parsed_cur; |
| 495 | parsed_cur = parsed_cur->next; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 496 | mbedtls_free(parsed_prv); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 497 | } |
Valerio Setti | e7373a8 | 2023-04-19 14:53:36 +0200 | [diff] [blame] | 498 | USE_PSA_DONE(); |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 499 | } |
| 500 | /* END_CASE */ |