blob: 76473c4862845abce46976d11ebb714a62df39f0 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 Certificate Signing Request writing
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * References:
21 * - CSRs: PKCS#10 v1.7 aka RFC 2986
22 * - attributes: PKCS#9 v2.0 aka RFC 2985
23 */
24
Gilles Peskinedb09ef62020-06-03 01:43:33 +020025#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_X509_CSR_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +010029#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/x509_csr.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/asn1write.h"
Janos Follath73c616b2019-12-18 15:07:04 +000032#include "mbedtls/error.h"
33#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050034#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Andrzej Kurekd4a65532018-10-31 06:18:39 -040036#if defined(MBEDTLS_USE_PSA_CRYPTO)
37#include "psa/crypto.h"
38#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020039#include "md_psa.h"
pespacek7599a772022-02-07 14:40:55 +010040#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurekd4a65532018-10-31 06:18:39 -040041
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43#include <stdlib.h>
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047#endif
48
Doru Gucea2957b352018-12-14 21:08:35 +020049#include "mbedtls/platform.h"
Doru Gucea2957b352018-12-14 21:08:35 +020050
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -040051#define CHECK_OVERFLOW_ADD(a, b) \
52 do \
53 { \
54 if (a > SIZE_MAX - (b)) \
55 { \
56 return MBEDTLS_ERR_X509_BAD_INPUT_DATA; \
57 } \
58 a += b; \
59 } while (0)
60
Gilles Peskine449bd832023-01-11 14:50:10 +010061void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062{
Gilles Peskine449bd832023-01-11 14:50:10 +010063 memset(ctx, 0, sizeof(mbedtls_x509write_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064}
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 mbedtls_asn1_free_named_data_list(&ctx->subject);
69 mbedtls_asn1_free_named_data_list(&ctx->extensions);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072}
73
Gilles Peskine449bd832023-01-11 14:50:10 +010074void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075{
76 ctx->md_alg = md_alg;
77}
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080{
81 ctx->key = key;
82}
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
85 const char *subject_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088}
89
Gilles Peskine449bd832023-01-11 14:50:10 +010090int mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx,
91 const char *oid, size_t oid_len,
92 int critical,
93 const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094{
Gilles Peskine449bd832023-01-11 14:50:10 +010095 return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
96 critical, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097}
98
Hannes Tschofenig6b108602022-12-28 18:38:53 +010099int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx,
100 const mbedtls_x509_san_list *san_list)
101{
102 int ret = 0;
Przemek Stekiel42510a92023-03-09 08:18:30 +0100103 const mbedtls_x509_san_list *cur;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100104 unsigned char *buf;
105 unsigned char *p;
106 size_t len;
107 size_t buflen = 0;
108
109 /* Determine the maximum size of the SubjectAltName list */
Przemek Stekiel55ceff62023-03-10 12:40:41 +0100110 for (cur = san_list; cur != NULL; cur = cur->next) {
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100111 /* Calculate size of the required buffer */
Przemek Stekiel57207712023-02-24 14:03:30 +0100112 switch (cur->node.type) {
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100113 case MBEDTLS_X509_SAN_DNS_NAME:
114 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
115 case MBEDTLS_X509_SAN_IP_ADDRESS:
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -0400116 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekiel42510a92023-03-09 08:18:30 +0100117 /* length of value for each name entry,
118 * maximum 4 bytes for the length field,
119 * 1 byte for the tag/type.
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100120 */
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -0400121 CHECK_OVERFLOW_ADD(buflen, cur->node.san.unstructured_name.len);
122 CHECK_OVERFLOW_ADD(buflen, 4 + 1);
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100123 break;
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -0400124 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
125 {
126 const mbedtls_asn1_named_data *chunk = &cur->node.san.directory_name;
127 while (chunk != NULL) {
128 // Max 4 bytes for length, +1 for tag,
129 // additional 4 max for length, +1 for tag.
130 // See x509_write_name for more information.
131 CHECK_OVERFLOW_ADD(buflen, 4 + 1 + 4 + 1);
132 CHECK_OVERFLOW_ADD(buflen, chunk->oid.len);
133 CHECK_OVERFLOW_ADD(buflen, chunk->val.len);
134 chunk = chunk->next;
135 }
136 CHECK_OVERFLOW_ADD(buflen, 4 + 1);
137 break;
138 }
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100139 default:
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -0400140 /* Not supported - return. */
141 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100142 }
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100143 }
144
145 /* Add the extra length field and tag */
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -0400146 CHECK_OVERFLOW_ADD(buflen, 4 + 1);
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100147
148 /* Allocate buffer */
149 buf = mbedtls_calloc(1, buflen);
150 if (buf == NULL) {
151 return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
152 }
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100153 p = buf + buflen;
154
155 /* Write ASN.1-based structure */
156 cur = san_list;
157 len = 0;
158 while (cur != NULL) {
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -0400159 size_t single_san_len = 0;
Przemek Stekiel18904ac2023-02-14 11:54:37 +0100160 switch (cur->node.type) {
161 case MBEDTLS_X509_SAN_DNS_NAME:
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -0400162 case MBEDTLS_X509_SAN_RFC822_NAME:
Przemek Stekiel18904ac2023-02-14 11:54:37 +0100163 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
164 case MBEDTLS_X509_SAN_IP_ADDRESS:
Przemek Stekiel55ceff62023-03-10 12:40:41 +0100165 {
166 const unsigned char *unstructured_name =
167 (const unsigned char *) cur->node.san.unstructured_name.p;
168 size_t unstructured_name_len = cur->node.san.unstructured_name.len;
169
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -0400170 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
Przemek Stekiel55ceff62023-03-10 12:40:41 +0100171 mbedtls_asn1_write_raw_buffer(
172 &p, buf,
173 unstructured_name, unstructured_name_len));
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -0400174 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, mbedtls_asn1_write_len(
Przemek Stekiel55ceff62023-03-10 12:40:41 +0100175 &p, buf, unstructured_name_len));
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -0400176 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
Przemek Stekiel55ceff62023-03-10 12:40:41 +0100177 mbedtls_asn1_write_tag(
178 &p, buf,
179 MBEDTLS_ASN1_CONTEXT_SPECIFIC | cur->node.type));
180 }
181 break;
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -0400182 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
183 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
184 mbedtls_x509_write_names(&p, buf,
185 (mbedtls_asn1_named_data *) &
186 cur->node
187 .san.directory_name));
188 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
189 mbedtls_asn1_write_len(&p, buf, single_san_len));
190 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
191 mbedtls_asn1_write_tag(&p, buf,
192 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
193 MBEDTLS_ASN1_CONSTRUCTED |
194 MBEDTLS_X509_SAN_DIRECTORY_NAME));
Przemek Stekiel18904ac2023-02-14 11:54:37 +0100195 break;
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -0400196 default:
197 /* Error out on an unsupported SAN */
198 ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
199 goto cleanup;
Przemek Stekiel18904ac2023-02-14 11:54:37 +0100200 }
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100201 cur = cur->next;
Andrzej Kurek1c8ecbe2023-07-07 05:12:52 -0400202 /* check for overflow */
203 if (len > SIZE_MAX - single_san_len) {
204 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
205 goto cleanup;
206 }
207 len += single_san_len;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100208 }
209
Przemek Stekiel57207712023-02-24 14:03:30 +0100210 MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
211 MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
212 mbedtls_asn1_write_tag(&p, buf,
213 MBEDTLS_ASN1_CONSTRUCTED |
214 MBEDTLS_ASN1_SEQUENCE));
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100215
216 ret = mbedtls_x509write_csr_set_extension(
217 ctx,
218 MBEDTLS_OID_SUBJECT_ALT_NAME,
219 MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME),
220 0,
221 buf + buflen - len,
222 len);
223
Przemek Stekiel07c5ea32023-03-07 15:43:38 +0100224 /* If we exceeded the allocated buffer it means that maximum size of the SubjectAltName list
225 * was incorrectly calculated and memory is corrupted. */
Przemek Stekiel55ceff62023-03-10 12:40:41 +0100226 if (p < buf) {
Przemek Stekiel07c5ea32023-03-07 15:43:38 +0100227 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
228 }
229
Przemek Stekiel57207712023-02-24 14:03:30 +0100230cleanup:
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100231 mbedtls_free(buf);
232 return ret;
233}
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236{
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240
241 c = buf + 4;
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8);
244 if (ret < 3 || ret > 4) {
245 return ret;
246 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
249 MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
250 0, c, (size_t) ret);
251 if (ret != 0) {
252 return ret;
253 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256}
257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
259 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260{
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200262 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264
265 c = buf + 4;
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
268 if (ret < 3 || ret > 4) {
269 return ret;
270 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
273 MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
274 0, c, (size_t) ret);
275 if (ret != 0) {
276 return ret;
277 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280}
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
283 unsigned char *buf,
284 size_t size,
285 unsigned char *sig, size_t sig_size,
286 int (*f_rng)(void *, unsigned char *, size_t),
287 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288{
Janos Follath865b3eb2019-12-16 11:46:15 +0000289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290 const char *sig_oid;
291 size_t sig_oid_len = 0;
292 unsigned char *c, *c2;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +0200293 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200294 size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
295 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_pk_type_t pk_alg;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400297#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400298 size_t hash_len;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200299 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(ctx->md_alg);
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400300#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301
Simon Leet40ca54a2020-06-26 21:23:32 +0000302 /* Write the CSR backwards starting from the end of buf */
Doru Gucea2957b352018-12-14 21:08:35 +0200303 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf,
306 ctx->extensions));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 if (len) {
309 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
310 MBEDTLS_ASN1_CHK_ADD(len,
311 mbedtls_asn1_write_tag(
312 &c, buf,
313 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
316 MBEDTLS_ASN1_CHK_ADD(len,
317 mbedtls_asn1_write_tag(
318 &c, buf,
319 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 MBEDTLS_ASN1_CHK_ADD(len,
322 mbedtls_asn1_write_oid(
323 &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
324 MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS9_CSR_EXT_REQ)));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
327 MBEDTLS_ASN1_CHK_ADD(len,
328 mbedtls_asn1_write_tag(
329 &c, buf,
330 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200331 }
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
334 MBEDTLS_ASN1_CHK_ADD(len,
335 mbedtls_asn1_write_tag(
336 &c, buf,
337 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_pk_write_pubkey_der(ctx->key,
340 buf, c - buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341 c -= pub_len;
342 len += pub_len;
343
344 /*
345 * Subject ::= Name
346 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
348 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349
350 /*
351 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
352 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
356 MBEDTLS_ASN1_CHK_ADD(len,
357 mbedtls_asn1_write_tag(
358 &c, buf,
359 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
361 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000362 * Sign the written CSR data into the sig buffer
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400363 * Note: hash errors can happen only after an internal error
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 */
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400365#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (psa_hash_compute(hash_alg,
367 c,
368 len,
369 hash,
370 sizeof(hash),
371 &hash_len) != PSA_SUCCESS) {
372 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400373 }
374#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash);
376 if (ret != 0) {
377 return ret;
378 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400379#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0,
381 sig, sig_size, &sig_len,
382 f_rng, p_rng)) != 0) {
383 return ret;
Hanno Beckerfc771442017-09-13 08:45:48 +0100384 }
385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100387 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 } else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100389 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 } else {
391 return MBEDTLS_ERR_X509_INVALID_ALG;
392 }
Hanno Beckerfc771442017-09-13 08:45:48 +0100393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
395 &sig_oid, &sig_oid_len)) != 0) {
396 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397 }
398
399 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000400 * Move the written CSR data to the start of buf to create space for
401 * writing the signature into buf.
402 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 memmove(buf, c, len);
Doru Gucea2957b352018-12-14 21:08:35 +0200404
Simon Leet40ca54a2020-06-26 21:23:32 +0000405 /*
406 * Write sig and its OID into buf backwards from the end of buf.
407 * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
408 * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409 */
410 c2 = buf + size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len,
412 mbedtls_x509_write_sig(&c2, buf + len, sig_oid, sig_oid_len,
413 sig, sig_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414
Simon Leet40ca54a2020-06-26 21:23:32 +0000415 /*
416 * Compact the space between the CSR data and signature by moving the
417 * CSR data to the start of the signature.
418 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419 c2 -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 memmove(c2, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421
Simon Leet40ca54a2020-06-26 21:23:32 +0000422 /* ASN encode the total size and tag the CSR data with it. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423 len += sig_and_oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c2, buf, len));
425 MBEDTLS_ASN1_CHK_ADD(len,
426 mbedtls_asn1_write_tag(
427 &c2, buf,
428 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Simon Leet40ca54a2020-06-26 21:23:32 +0000429
430 /* Zero the unused bytes at the start of buf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 memset(buf, 0, c2 - buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434}
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf,
437 size_t size,
438 int (*f_rng)(void *, unsigned char *, size_t),
439 void *p_rng)
Doru Gucea2957b352018-12-14 21:08:35 +0200440{
441 int ret;
442 unsigned char *sig;
443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) {
445 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Doru Gucea2957b352018-12-14 21:08:35 +0200446 }
447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 ret = x509write_csr_der_internal(ctx, buf, size,
449 sig, MBEDTLS_PK_SIGNATURE_MAX_SIZE,
450 f_rng, p_rng);
Doru Gucea2957b352018-12-14 21:08:35 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 mbedtls_free(sig);
Doru Gucea2957b352018-12-14 21:08:35 +0200453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return ret;
Doru Gucea2957b352018-12-14 21:08:35 +0200455}
456
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457#define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
458#define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100461int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
462 int (*f_rng)(void *, unsigned char *, size_t),
463 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464{
Janos Follath865b3eb2019-12-16 11:46:15 +0000465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466 size_t olen = 0;
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if ((ret = mbedtls_x509write_csr_der(ctx, buf, size,
469 f_rng, p_rng)) < 0) {
470 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471 }
472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CSR, PEM_END_CSR,
474 buf + size - ret,
475 ret, buf, size, &olen)) != 0) {
476 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477 }
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483#endif /* MBEDTLS_X509_CSR_WRITE_C */