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