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