Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * X.509 Certificate Signing Request writing |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 18 | */ |
| 19 | /* |
| 20 | * References: |
| 21 | * - CSRs: PKCS#10 v1.7 aka RFC 2986 |
| 22 | * - attributes: PKCS#9 v2.0 aka RFC 2985 |
| 23 | */ |
| 24 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 25 | #include "common.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if defined(MBEDTLS_X509_CSR_WRITE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 28 | |
Przemek Stekiel | 5a49d3c | 2023-02-24 13:12:55 +0100 | [diff] [blame] | 29 | #include "mbedtls/x509.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/x509_csr.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/asn1write.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 32 | #include "mbedtls/error.h" |
| 33 | #include "mbedtls/oid.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 34 | #include "mbedtls/platform_util.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 35 | |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 36 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 37 | #include "psa/crypto.h" |
| 38 | #include "mbedtls/psa_util.h" |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 39 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Przemek Stekiel | 5166954 | 2022-09-13 12:57:05 +0200 | [diff] [blame] | 40 | #include "hash_info.h" |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 41 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 42 | #include <string.h> |
| 43 | #include <stdlib.h> |
| 44 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #if defined(MBEDTLS_PEM_WRITE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/pem.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 47 | #endif |
| 48 | |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 49 | #include "mbedtls/platform.h" |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 50 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 52 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 53 | memset(ctx, 0, sizeof(mbedtls_x509write_csr)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 57 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 58 | mbedtls_asn1_free_named_data_list(&ctx->subject); |
| 59 | mbedtls_asn1_free_named_data_list(&ctx->extensions); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 60 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_csr)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 65 | { |
| 66 | ctx->md_alg = md_alg; |
| 67 | } |
| 68 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 70 | { |
| 71 | ctx->key = key; |
| 72 | } |
| 73 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx, |
| 75 | const char *subject_name) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 76 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | return mbedtls_x509_string_to_names(&ctx->subject, subject_name); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | int mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx, |
| 81 | const char *oid, size_t oid_len, |
| 82 | int critical, |
| 83 | const unsigned char *val, size_t val_len) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 84 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len, |
| 86 | critical, val, val_len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Hannes Tschofenig | 6b10860 | 2022-12-28 18:38:53 +0100 | [diff] [blame] | 89 | int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx, |
| 90 | const mbedtls_x509_san_list *san_list) |
| 91 | { |
| 92 | int ret = 0; |
Hannes Tschofenig | 6b10860 | 2022-12-28 18:38:53 +0100 | [diff] [blame] | 93 | const mbedtls_x509_san_list *cur = san_list; |
| 94 | unsigned char *buf; |
| 95 | unsigned char *p; |
| 96 | size_t len; |
| 97 | size_t buflen = 0; |
| 98 | |
| 99 | /* Determine the maximum size of the SubjectAltName list */ |
| 100 | while (cur != NULL) { |
Przemek Stekiel | 5a49d3c | 2023-02-24 13:12:55 +0100 | [diff] [blame] | 101 | /* Calculate size of the required buffer */ |
Przemek Stekiel | 5720771 | 2023-02-24 14:03:30 +0100 | [diff] [blame^] | 102 | switch (cur->node.type) { |
Przemek Stekiel | 5a49d3c | 2023-02-24 13:12:55 +0100 | [diff] [blame] | 103 | case MBEDTLS_X509_SAN_DNS_NAME: |
| 104 | case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER: |
| 105 | case MBEDTLS_X509_SAN_IP_ADDRESS: |
| 106 | /* + length of value for each name entry, |
| 107 | * + maximum 4 bytes for the length field, |
| 108 | * + 1 byte for the tag/type. |
| 109 | */ |
| 110 | buflen += cur->node.san.unstructured_name.len + 4 + 1; |
| 111 | break; |
| 112 | |
| 113 | default: |
| 114 | /* Not supported - skip. */ |
| 115 | break; |
Hannes Tschofenig | 6b10860 | 2022-12-28 18:38:53 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Hannes Tschofenig | 6b10860 | 2022-12-28 18:38:53 +0100 | [diff] [blame] | 118 | |
| 119 | cur = cur->next; |
| 120 | } |
| 121 | |
| 122 | /* Add the extra length field and tag */ |
| 123 | buflen += 4 + 1; |
| 124 | |
| 125 | /* Allocate buffer */ |
| 126 | buf = mbedtls_calloc(1, buflen); |
| 127 | if (buf == NULL) { |
| 128 | return MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
| 129 | } |
| 130 | |
| 131 | mbedtls_platform_zeroize(buf, buflen); |
| 132 | p = buf + buflen; |
| 133 | |
| 134 | /* Write ASN.1-based structure */ |
| 135 | cur = san_list; |
| 136 | len = 0; |
| 137 | while (cur != NULL) { |
Przemek Stekiel | 18904ac | 2023-02-14 11:54:37 +0100 | [diff] [blame] | 138 | switch (cur->node.type) { |
| 139 | case MBEDTLS_X509_SAN_DNS_NAME: |
| 140 | case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER: |
| 141 | case MBEDTLS_X509_SAN_IP_ADDRESS: |
Przemek Stekiel | 5720771 | 2023-02-24 14:03:30 +0100 | [diff] [blame^] | 142 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, |
| 143 | mbedtls_asn1_write_raw_buffer(&p, buf, |
| 144 | (const unsigned char *) |
| 145 | cur->node.san. |
| 146 | unstructured_name.p, |
| 147 | cur->node.san. |
| 148 | unstructured_name.len)); |
| 149 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, |
| 150 | cur->node.san. |
| 151 | unstructured_name.len)); |
| 152 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, |
| 153 | mbedtls_asn1_write_tag(&p, buf, |
| 154 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 155 | cur->node.type)); |
Przemek Stekiel | 18904ac | 2023-02-14 11:54:37 +0100 | [diff] [blame] | 156 | break; |
| 157 | default: |
| 158 | /* Skip unsupported names. */ |
| 159 | break; |
| 160 | } |
Hannes Tschofenig | 6b10860 | 2022-12-28 18:38:53 +0100 | [diff] [blame] | 161 | cur = cur->next; |
| 162 | } |
| 163 | |
Przemek Stekiel | 5720771 | 2023-02-24 14:03:30 +0100 | [diff] [blame^] | 164 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, len)); |
| 165 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, |
| 166 | mbedtls_asn1_write_tag(&p, buf, |
| 167 | MBEDTLS_ASN1_CONSTRUCTED | |
| 168 | MBEDTLS_ASN1_SEQUENCE)); |
Hannes Tschofenig | 6b10860 | 2022-12-28 18:38:53 +0100 | [diff] [blame] | 169 | |
| 170 | ret = mbedtls_x509write_csr_set_extension( |
| 171 | ctx, |
| 172 | MBEDTLS_OID_SUBJECT_ALT_NAME, |
| 173 | MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME), |
| 174 | 0, |
| 175 | buf + buflen - len, |
| 176 | len); |
| 177 | |
Przemek Stekiel | 5720771 | 2023-02-24 14:03:30 +0100 | [diff] [blame^] | 178 | cleanup: |
Hannes Tschofenig | 6b10860 | 2022-12-28 18:38:53 +0100 | [diff] [blame] | 179 | mbedtls_free(buf); |
| 180 | return ret; |
| 181 | } |
| 182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 184 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | unsigned char buf[4] = { 0 }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 186 | unsigned char *c; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 187 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 188 | |
| 189 | c = buf + 4; |
| 190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8); |
| 192 | if (ret < 3 || ret > 4) { |
| 193 | return ret; |
| 194 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_KEY_USAGE, |
| 197 | MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE), |
| 198 | 0, c, (size_t) ret); |
| 199 | if (ret != 0) { |
| 200 | return ret; |
| 201 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx, |
| 207 | unsigned char ns_cert_type) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 208 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 209 | unsigned char buf[4] = { 0 }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 210 | unsigned char *c; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 211 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 212 | |
| 213 | c = buf + 4; |
| 214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8); |
| 216 | if (ret < 3 || ret > 4) { |
| 217 | return ret; |
| 218 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE, |
| 221 | MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE), |
| 222 | 0, c, (size_t) ret); |
| 223 | if (ret != 0) { |
| 224 | return ret; |
| 225 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 227 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx, |
| 231 | unsigned char *buf, |
| 232 | size_t size, |
| 233 | unsigned char *sig, size_t sig_size, |
| 234 | int (*f_rng)(void *, unsigned char *, size_t), |
| 235 | void *p_rng) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 236 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 237 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 238 | const char *sig_oid; |
| 239 | size_t sig_oid_len = 0; |
| 240 | unsigned char *c, *c2; |
Przemek Stekiel | 5166954 | 2022-09-13 12:57:05 +0200 | [diff] [blame] | 241 | unsigned char hash[MBEDTLS_HASH_MAX_SIZE]; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 242 | size_t pub_len = 0, sig_and_oid_len = 0, sig_len; |
| 243 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | mbedtls_pk_type_t pk_alg; |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 245 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 246 | size_t hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(ctx->md_alg); |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 248 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 249 | |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 250 | /* Write the CSR backwards starting from the end of buf */ |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 251 | c = buf + size; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf, |
| 254 | ctx->extensions)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | if (len) { |
| 257 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 258 | MBEDTLS_ASN1_CHK_ADD(len, |
| 259 | mbedtls_asn1_write_tag( |
| 260 | &c, buf, |
| 261 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 264 | MBEDTLS_ASN1_CHK_ADD(len, |
| 265 | mbedtls_asn1_write_tag( |
| 266 | &c, buf, |
| 267 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | MBEDTLS_ASN1_CHK_ADD(len, |
| 270 | mbedtls_asn1_write_oid( |
| 271 | &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ, |
| 272 | MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS9_CSR_EXT_REQ))); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 274 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 275 | MBEDTLS_ASN1_CHK_ADD(len, |
| 276 | mbedtls_asn1_write_tag( |
| 277 | &c, buf, |
| 278 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 282 | MBEDTLS_ASN1_CHK_ADD(len, |
| 283 | mbedtls_asn1_write_tag( |
| 284 | &c, buf, |
| 285 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_pk_write_pubkey_der(ctx->key, |
| 288 | buf, c - buf)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 289 | c -= pub_len; |
| 290 | len += pub_len; |
| 291 | |
| 292 | /* |
| 293 | * Subject ::= Name |
| 294 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf, |
| 296 | ctx->subject)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 300 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 301 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 304 | MBEDTLS_ASN1_CHK_ADD(len, |
| 305 | mbedtls_asn1_write_tag( |
| 306 | &c, buf, |
| 307 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 308 | |
| 309 | /* |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 310 | * Sign the written CSR data into the sig buffer |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 311 | * Note: hash errors can happen only after an internal error |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 312 | */ |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 313 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | if (psa_hash_compute(hash_alg, |
| 315 | c, |
| 316 | len, |
| 317 | hash, |
| 318 | sizeof(hash), |
| 319 | &hash_len) != PSA_SUCCESS) { |
| 320 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 321 | } |
| 322 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash); |
| 324 | if (ret != 0) { |
| 325 | return ret; |
| 326 | } |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 327 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0, |
| 329 | sig, sig_size, &sig_len, |
| 330 | f_rng, p_rng)) != 0) { |
| 331 | return ret; |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 334 | if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) { |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 335 | pk_alg = MBEDTLS_PK_RSA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | } else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) { |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 337 | pk_alg = MBEDTLS_PK_ECDSA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 338 | } else { |
| 339 | return MBEDTLS_ERR_X509_INVALID_ALG; |
| 340 | } |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 342 | if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg, |
| 343 | &sig_oid, &sig_oid_len)) != 0) { |
| 344 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 348 | * Move the written CSR data to the start of buf to create space for |
| 349 | * writing the signature into buf. |
| 350 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 351 | memmove(buf, c, len); |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 352 | |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 353 | /* |
| 354 | * Write sig and its OID into buf backwards from the end of buf. |
| 355 | * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len |
| 356 | * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 357 | */ |
| 358 | c2 = buf + size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 359 | MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, |
| 360 | mbedtls_x509_write_sig(&c2, buf + len, sig_oid, sig_oid_len, |
| 361 | sig, sig_len)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 362 | |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 363 | /* |
| 364 | * Compact the space between the CSR data and signature by moving the |
| 365 | * CSR data to the start of the signature. |
| 366 | */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 367 | c2 -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | memmove(c2, buf, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 369 | |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 370 | /* ASN encode the total size and tag the CSR data with it. */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 371 | len += sig_and_oid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c2, buf, len)); |
| 373 | MBEDTLS_ASN1_CHK_ADD(len, |
| 374 | mbedtls_asn1_write_tag( |
| 375 | &c2, buf, |
| 376 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)); |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 377 | |
| 378 | /* Zero the unused bytes at the start of buf */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | memset(buf, 0, c2 - buf); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 381 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf, |
| 385 | size_t size, |
| 386 | int (*f_rng)(void *, unsigned char *, size_t), |
| 387 | void *p_rng) |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 388 | { |
| 389 | int ret; |
| 390 | unsigned char *sig; |
| 391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) { |
| 393 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | ret = x509write_csr_der_internal(ctx, buf, size, |
| 397 | sig, MBEDTLS_PK_SIGNATURE_MAX_SIZE, |
| 398 | f_rng, p_rng); |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | mbedtls_free(sig); |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | return ret; |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 405 | #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n" |
| 406 | #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n" |
| 407 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 408 | #if defined(MBEDTLS_PEM_WRITE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 409 | int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size, |
| 410 | int (*f_rng)(void *, unsigned char *, size_t), |
| 411 | void *p_rng) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 412 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 413 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 414 | size_t olen = 0; |
| 415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 416 | if ((ret = mbedtls_x509write_csr_der(ctx, buf, size, |
| 417 | f_rng, p_rng)) < 0) { |
| 418 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CSR, PEM_END_CSR, |
| 422 | buf + size - ret, |
| 423 | ret, buf, size, &olen)) != 0) { |
| 424 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 427 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 428 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 429 | #endif /* MBEDTLS_PEM_WRITE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 430 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 431 | #endif /* MBEDTLS_X509_CSR_WRITE_C */ |