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 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/x509_csr.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/asn1write.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 31 | #include "mbedtls/error.h" |
| 32 | #include "mbedtls/oid.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 33 | #include "mbedtls/platform_util.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 34 | |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 35 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 36 | #include "psa/crypto.h" |
| 37 | #include "mbedtls/psa_util.h" |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 38 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Przemek Stekiel | 5166954 | 2022-09-13 12:57:05 +0200 | [diff] [blame] | 39 | #include "hash_info.h" |
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; |
Hannes Tschofenig | 6b10860 | 2022-12-28 18:38:53 +0100 | [diff] [blame] | 92 | const mbedtls_x509_san_list *cur = san_list; |
| 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 */ |
| 99 | while (cur != NULL) { |
| 100 | if (cur->node.len <= 0) { |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | /* Calculate size of the required buffer: |
| 105 | * + length of value for each name entry, |
| 106 | * + maximum 4 bytes for the length field, |
| 107 | * + 1 byte for the tag/type. |
| 108 | */ |
| 109 | buflen += cur->node.len + 4 + 1; |
| 110 | |
| 111 | cur = cur->next; |
| 112 | } |
| 113 | |
| 114 | /* Add the extra length field and tag */ |
| 115 | buflen += 4 + 1; |
| 116 | |
| 117 | /* Allocate buffer */ |
| 118 | buf = mbedtls_calloc(1, buflen); |
| 119 | if (buf == NULL) { |
| 120 | return MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
| 121 | } |
| 122 | |
| 123 | mbedtls_platform_zeroize(buf, buflen); |
| 124 | p = buf + buflen; |
| 125 | |
| 126 | /* Write ASN.1-based structure */ |
| 127 | cur = san_list; |
| 128 | len = 0; |
| 129 | while (cur != NULL) { |
Przemek Stekiel | 18904ac | 2023-02-14 11:54:37 +0100 | [diff] [blame] | 130 | switch (cur->node.type) { |
| 131 | case MBEDTLS_X509_SAN_DNS_NAME: |
| 132 | case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER: |
| 133 | case MBEDTLS_X509_SAN_IP_ADDRESS: |
| 134 | MBEDTLS_ASN1_CHK_ADD(len, |
| 135 | mbedtls_asn1_write_raw_buffer(&p, buf, |
| 136 | (const unsigned char *) cur->node |
| 137 | .name, |
| 138 | cur->node.len)); |
| 139 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, cur->node.len)); |
| 140 | MBEDTLS_ASN1_CHK_ADD(len, |
| 141 | mbedtls_asn1_write_tag(&p, buf, |
| 142 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 143 | cur->node.type)); |
| 144 | break; |
| 145 | default: |
| 146 | /* Skip unsupported names. */ |
| 147 | break; |
| 148 | } |
Hannes Tschofenig | 6b10860 | 2022-12-28 18:38:53 +0100 | [diff] [blame] | 149 | cur = cur->next; |
| 150 | } |
| 151 | |
| 152 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, len)); |
| 153 | MBEDTLS_ASN1_CHK_ADD(len, |
| 154 | mbedtls_asn1_write_tag(&p, buf, |
| 155 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)); |
| 156 | |
| 157 | ret = mbedtls_x509write_csr_set_extension( |
| 158 | ctx, |
| 159 | MBEDTLS_OID_SUBJECT_ALT_NAME, |
| 160 | MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME), |
| 161 | 0, |
| 162 | buf + buflen - len, |
| 163 | len); |
| 164 | |
| 165 | mbedtls_free(buf); |
| 166 | return ret; |
| 167 | } |
| 168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | 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] | 170 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | unsigned char buf[4] = { 0 }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 172 | unsigned char *c; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 173 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 174 | |
| 175 | c = buf + 4; |
| 176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 177 | ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8); |
| 178 | if (ret < 3 || ret > 4) { |
| 179 | return ret; |
| 180 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_KEY_USAGE, |
| 183 | MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE), |
| 184 | 0, c, (size_t) ret); |
| 185 | if (ret != 0) { |
| 186 | return ret; |
| 187 | } |
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 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 192 | int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx, |
| 193 | unsigned char ns_cert_type) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 194 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | unsigned char buf[4] = { 0 }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 196 | unsigned char *c; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 197 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 198 | |
| 199 | c = buf + 4; |
| 200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8); |
| 202 | if (ret < 3 || ret > 4) { |
| 203 | return ret; |
| 204 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE, |
| 207 | MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE), |
| 208 | 0, c, (size_t) ret); |
| 209 | if (ret != 0) { |
| 210 | return ret; |
| 211 | } |
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 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 216 | static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx, |
| 217 | unsigned char *buf, |
| 218 | size_t size, |
| 219 | unsigned char *sig, size_t sig_size, |
| 220 | int (*f_rng)(void *, unsigned char *, size_t), |
| 221 | void *p_rng) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 222 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 223 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 224 | const char *sig_oid; |
| 225 | size_t sig_oid_len = 0; |
| 226 | unsigned char *c, *c2; |
Przemek Stekiel | 5166954 | 2022-09-13 12:57:05 +0200 | [diff] [blame] | 227 | unsigned char hash[MBEDTLS_HASH_MAX_SIZE]; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 228 | size_t pub_len = 0, sig_and_oid_len = 0, sig_len; |
| 229 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | mbedtls_pk_type_t pk_alg; |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 231 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 232 | size_t hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | 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] | 234 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 235 | |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 236 | /* Write the CSR backwards starting from the end of buf */ |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 237 | c = buf + size; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf, |
| 240 | ctx->extensions)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | if (len) { |
| 243 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 244 | MBEDTLS_ASN1_CHK_ADD(len, |
| 245 | mbedtls_asn1_write_tag( |
| 246 | &c, buf, |
| 247 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 249 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 250 | MBEDTLS_ASN1_CHK_ADD(len, |
| 251 | mbedtls_asn1_write_tag( |
| 252 | &c, buf, |
| 253 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | MBEDTLS_ASN1_CHK_ADD(len, |
| 256 | mbedtls_asn1_write_oid( |
| 257 | &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ, |
| 258 | MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS9_CSR_EXT_REQ))); |
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 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 261 | MBEDTLS_ASN1_CHK_ADD(len, |
| 262 | mbedtls_asn1_write_tag( |
| 263 | &c, buf, |
| 264 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 265 | } |
| 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_CONTEXT_SPECIFIC)); |
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(pub_len, mbedtls_pk_write_pubkey_der(ctx->key, |
| 274 | buf, c - buf)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 275 | c -= pub_len; |
| 276 | len += pub_len; |
| 277 | |
| 278 | /* |
| 279 | * Subject ::= Name |
| 280 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf, |
| 282 | ctx->subject)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 283 | |
| 284 | /* |
| 285 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 286 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 289 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 290 | MBEDTLS_ASN1_CHK_ADD(len, |
| 291 | mbedtls_asn1_write_tag( |
| 292 | &c, buf, |
| 293 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 294 | |
| 295 | /* |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 296 | * Sign the written CSR data into the sig buffer |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 297 | * Note: hash errors can happen only after an internal error |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 298 | */ |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 299 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | if (psa_hash_compute(hash_alg, |
| 301 | c, |
| 302 | len, |
| 303 | hash, |
| 304 | sizeof(hash), |
| 305 | &hash_len) != PSA_SUCCESS) { |
| 306 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 307 | } |
| 308 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 309 | ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash); |
| 310 | if (ret != 0) { |
| 311 | return ret; |
| 312 | } |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 313 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0, |
| 315 | sig, sig_size, &sig_len, |
| 316 | f_rng, p_rng)) != 0) { |
| 317 | return ret; |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) { |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 321 | pk_alg = MBEDTLS_PK_RSA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | } else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) { |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 323 | pk_alg = MBEDTLS_PK_ECDSA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | } else { |
| 325 | return MBEDTLS_ERR_X509_INVALID_ALG; |
| 326 | } |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg, |
| 329 | &sig_oid, &sig_oid_len)) != 0) { |
| 330 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 334 | * Move the written CSR data to the start of buf to create space for |
| 335 | * writing the signature into buf. |
| 336 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 337 | memmove(buf, c, len); |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 338 | |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 339 | /* |
| 340 | * Write sig and its OID into buf backwards from the end of buf. |
| 341 | * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len |
| 342 | * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 343 | */ |
| 344 | c2 = buf + size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, |
| 346 | mbedtls_x509_write_sig(&c2, buf + len, sig_oid, sig_oid_len, |
| 347 | sig, sig_len)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 348 | |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 349 | /* |
| 350 | * Compact the space between the CSR data and signature by moving the |
| 351 | * CSR data to the start of the signature. |
| 352 | */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 353 | c2 -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 354 | memmove(c2, buf, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 355 | |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 356 | /* ASN encode the total size and tag the CSR data with it. */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 357 | len += sig_and_oid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c2, buf, len)); |
| 359 | MBEDTLS_ASN1_CHK_ADD(len, |
| 360 | mbedtls_asn1_write_tag( |
| 361 | &c2, buf, |
| 362 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)); |
Simon Leet | 40ca54a | 2020-06-26 21:23:32 +0000 | [diff] [blame] | 363 | |
| 364 | /* Zero the unused bytes at the start of buf */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | memset(buf, 0, c2 - buf); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 368 | } |
| 369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf, |
| 371 | size_t size, |
| 372 | int (*f_rng)(void *, unsigned char *, size_t), |
| 373 | void *p_rng) |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 374 | { |
| 375 | int ret; |
| 376 | unsigned char *sig; |
| 377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 378 | if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) { |
| 379 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | ret = x509write_csr_der_internal(ctx, buf, size, |
| 383 | sig, MBEDTLS_PK_SIGNATURE_MAX_SIZE, |
| 384 | f_rng, p_rng); |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | mbedtls_free(sig); |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 388 | return ret; |
Doru Gucea | 2957b35 | 2018-12-14 21:08:35 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 391 | #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n" |
| 392 | #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n" |
| 393 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 394 | #if defined(MBEDTLS_PEM_WRITE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size, |
| 396 | int (*f_rng)(void *, unsigned char *, size_t), |
| 397 | void *p_rng) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 398 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 399 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 400 | size_t olen = 0; |
| 401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | if ((ret = mbedtls_x509write_csr_der(ctx, buf, size, |
| 403 | f_rng, p_rng)) < 0) { |
| 404 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 407 | if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CSR, PEM_END_CSR, |
| 408 | buf + size - ret, |
| 409 | ret, buf, size, &olen)) != 0) { |
| 410 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 411 | } |
| 412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 413 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 414 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | #endif /* MBEDTLS_PEM_WRITE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 417 | #endif /* MBEDTLS_X509_CSR_WRITE_C */ |