blob: a830fc2a7d7c13a7ff208a31c2dbfe56892e1637 [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"
pespacek7599a772022-02-07 14:40:55 +010039#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemek Stekiel51669542022-09-13 12:57:05 +020040#include "hash_info.h"
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
Gilles Peskine449bd832023-01-11 14:50:10 +010051void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052{
Gilles Peskine449bd832023-01-11 14:50:10 +010053 memset(ctx, 0, sizeof(mbedtls_x509write_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054}
55
Gilles Peskine449bd832023-01-11 14:50:10 +010056void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057{
Gilles Peskine449bd832023-01-11 14:50:10 +010058 mbedtls_asn1_free_named_data_list(&ctx->subject);
59 mbedtls_asn1_free_named_data_list(&ctx->extensions);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060
Gilles Peskine449bd832023-01-11 14:50:10 +010061 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062}
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065{
66 ctx->md_alg = md_alg;
67}
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070{
71 ctx->key = key;
72}
73
Gilles Peskine449bd832023-01-11 14:50:10 +010074int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
75 const char *subject_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076{
Gilles Peskine449bd832023-01-11 14:50:10 +010077 return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078}
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080int 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 Bakker7c6b2c32013-09-16 13:49:26 +020084{
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
86 critical, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087}
88
Hannes Tschofenig6b108602022-12-28 18:38:53 +010089int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx,
90 const mbedtls_x509_san_list *san_list)
91{
92 int ret = 0;
Przemek Stekiel42510a92023-03-09 08:18:30 +010093 const mbedtls_x509_san_list *cur;
Hannes Tschofenig6b108602022-12-28 18:38:53 +010094 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 */
Przemek Stekiel42510a92023-03-09 08:18:30 +0100100 for(cur = san_list; cur != NULL; cur = cur->next) {
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100101 /* Calculate size of the required buffer */
Przemek Stekiel57207712023-02-24 14:03:30 +0100102 switch (cur->node.type) {
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100103 case MBEDTLS_X509_SAN_DNS_NAME:
104 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
105 case MBEDTLS_X509_SAN_IP_ADDRESS:
Przemek Stekiel42510a92023-03-09 08:18:30 +0100106 /* length of value for each name entry,
107 * maximum 4 bytes for the length field,
108 * 1 byte for the tag/type.
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100109 */
110 buflen += cur->node.san.unstructured_name.len + 4 + 1;
111 break;
112
113 default:
114 /* Not supported - skip. */
115 break;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100116 }
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100117 }
118
119 /* Add the extra length field and tag */
120 buflen += 4 + 1;
121
122 /* Allocate buffer */
123 buf = mbedtls_calloc(1, buflen);
124 if (buf == NULL) {
125 return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
126 }
127
128 mbedtls_platform_zeroize(buf, buflen);
129 p = buf + buflen;
130
131 /* Write ASN.1-based structure */
132 cur = san_list;
133 len = 0;
134 while (cur != NULL) {
Przemek Stekiel18904ac2023-02-14 11:54:37 +0100135 switch (cur->node.type) {
136 case MBEDTLS_X509_SAN_DNS_NAME:
137 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
138 case MBEDTLS_X509_SAN_IP_ADDRESS:
Przemek Stekiel57207712023-02-24 14:03:30 +0100139 MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
140 mbedtls_asn1_write_raw_buffer(&p, buf,
Przemek Stekiel07c5ea32023-03-07 15:43:38 +0100141 (const unsigned char *) cur->node.san.unstructured_name.p,
142 cur->node.san.unstructured_name.len));
Przemek Stekiel57207712023-02-24 14:03:30 +0100143 MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf,
Przemek Stekiel07c5ea32023-03-07 15:43:38 +0100144 cur->node.san.unstructured_name.len));
Przemek Stekiel57207712023-02-24 14:03:30 +0100145 MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
146 mbedtls_asn1_write_tag(&p, buf,
147 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
148 cur->node.type));
Przemek Stekiel18904ac2023-02-14 11:54:37 +0100149 break;
150 default:
151 /* Skip unsupported names. */
152 break;
153 }
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100154 cur = cur->next;
155 }
156
Przemek Stekiel57207712023-02-24 14:03:30 +0100157 MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
158 MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
159 mbedtls_asn1_write_tag(&p, buf,
160 MBEDTLS_ASN1_CONSTRUCTED |
161 MBEDTLS_ASN1_SEQUENCE));
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100162
163 ret = mbedtls_x509write_csr_set_extension(
164 ctx,
165 MBEDTLS_OID_SUBJECT_ALT_NAME,
166 MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME),
167 0,
168 buf + buflen - len,
169 len);
170
Przemek Stekiel07c5ea32023-03-07 15:43:38 +0100171 /* If we exceeded the allocated buffer it means that maximum size of the SubjectAltName list
172 * was incorrectly calculated and memory is corrupted. */
173 if ( p < buf ) {
174 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
175 }
176
Przemek Stekiel57207712023-02-24 14:03:30 +0100177cleanup:
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100178 mbedtls_free(buf);
179 return ret;
180}
181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200183{
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200185 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200187
188 c = buf + 4;
189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8);
191 if (ret < 3 || ret > 4) {
192 return ret;
193 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
196 MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
197 0, c, (size_t) ret);
198 if (ret != 0) {
199 return ret;
200 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203}
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
206 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200207{
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200209 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000210 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200211
212 c = buf + 4;
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
215 if (ret < 3 || ret > 4) {
216 return ret;
217 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
220 MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
221 0, c, (size_t) ret);
222 if (ret != 0) {
223 return ret;
224 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200227}
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
230 unsigned char *buf,
231 size_t size,
232 unsigned char *sig, size_t sig_size,
233 int (*f_rng)(void *, unsigned char *, size_t),
234 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200235{
Janos Follath865b3eb2019-12-16 11:46:15 +0000236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200237 const char *sig_oid;
238 size_t sig_oid_len = 0;
239 unsigned char *c, *c2;
Przemek Stekiel51669542022-09-13 12:57:05 +0200240 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241 size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
242 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_pk_type_t pk_alg;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400244#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400245 size_t hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(ctx->md_alg);
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400247#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248
Simon Leet40ca54a2020-06-26 21:23:32 +0000249 /* Write the CSR backwards starting from the end of buf */
Doru Gucea2957b352018-12-14 21:08:35 +0200250 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf,
253 ctx->extensions));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (len) {
256 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
257 MBEDTLS_ASN1_CHK_ADD(len,
258 mbedtls_asn1_write_tag(
259 &c, buf,
260 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
263 MBEDTLS_ASN1_CHK_ADD(len,
264 mbedtls_asn1_write_tag(
265 &c, buf,
266 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 MBEDTLS_ASN1_CHK_ADD(len,
269 mbedtls_asn1_write_oid(
270 &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
271 MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS9_CSR_EXT_REQ)));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
274 MBEDTLS_ASN1_CHK_ADD(len,
275 mbedtls_asn1_write_tag(
276 &c, buf,
277 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278 }
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
281 MBEDTLS_ASN1_CHK_ADD(len,
282 mbedtls_asn1_write_tag(
283 &c, buf,
284 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_pk_write_pubkey_der(ctx->key,
287 buf, c - buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 c -= pub_len;
289 len += pub_len;
290
291 /*
292 * Subject ::= Name
293 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
295 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296
297 /*
298 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
299 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
303 MBEDTLS_ASN1_CHK_ADD(len,
304 mbedtls_asn1_write_tag(
305 &c, buf,
306 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307
308 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000309 * Sign the written CSR data into the sig buffer
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400310 * Note: hash errors can happen only after an internal error
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311 */
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400312#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if (psa_hash_compute(hash_alg,
314 c,
315 len,
316 hash,
317 sizeof(hash),
318 &hash_len) != PSA_SUCCESS) {
319 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400320 }
321#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash);
323 if (ret != 0) {
324 return ret;
325 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400326#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0,
328 sig, sig_size, &sig_len,
329 f_rng, p_rng)) != 0) {
330 return ret;
Hanno Beckerfc771442017-09-13 08:45:48 +0100331 }
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100334 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 } else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100336 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 } else {
338 return MBEDTLS_ERR_X509_INVALID_ALG;
339 }
Hanno Beckerfc771442017-09-13 08:45:48 +0100340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
342 &sig_oid, &sig_oid_len)) != 0) {
343 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344 }
345
346 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000347 * Move the written CSR data to the start of buf to create space for
348 * writing the signature into buf.
349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 memmove(buf, c, len);
Doru Gucea2957b352018-12-14 21:08:35 +0200351
Simon Leet40ca54a2020-06-26 21:23:32 +0000352 /*
353 * Write sig and its OID into buf backwards from the end of buf.
354 * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
355 * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356 */
357 c2 = buf + size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len,
359 mbedtls_x509_write_sig(&c2, buf + len, sig_oid, sig_oid_len,
360 sig, sig_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361
Simon Leet40ca54a2020-06-26 21:23:32 +0000362 /*
363 * Compact the space between the CSR data and signature by moving the
364 * CSR data to the start of the signature.
365 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366 c2 -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 memmove(c2, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368
Simon Leet40ca54a2020-06-26 21:23:32 +0000369 /* ASN encode the total size and tag the CSR data with it. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370 len += sig_and_oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c2, buf, len));
372 MBEDTLS_ASN1_CHK_ADD(len,
373 mbedtls_asn1_write_tag(
374 &c2, buf,
375 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Simon Leet40ca54a2020-06-26 21:23:32 +0000376
377 /* Zero the unused bytes at the start of buf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 memset(buf, 0, c2 - buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381}
382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf,
384 size_t size,
385 int (*f_rng)(void *, unsigned char *, size_t),
386 void *p_rng)
Doru Gucea2957b352018-12-14 21:08:35 +0200387{
388 int ret;
389 unsigned char *sig;
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) {
392 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Doru Gucea2957b352018-12-14 21:08:35 +0200393 }
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 ret = x509write_csr_der_internal(ctx, buf, size,
396 sig, MBEDTLS_PK_SIGNATURE_MAX_SIZE,
397 f_rng, p_rng);
Doru Gucea2957b352018-12-14 21:08:35 +0200398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 mbedtls_free(sig);
Doru Gucea2957b352018-12-14 21:08:35 +0200400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 return ret;
Doru Gucea2957b352018-12-14 21:08:35 +0200402}
403
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404#define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
405#define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100408int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
409 int (*f_rng)(void *, unsigned char *, size_t),
410 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411{
Janos Follath865b3eb2019-12-16 11:46:15 +0000412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 size_t olen = 0;
414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if ((ret = mbedtls_x509write_csr_der(ctx, buf, size,
416 f_rng, p_rng)) < 0) {
417 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 }
419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CSR, PEM_END_CSR,
421 buf + size - ret,
422 ret, buf, size, &olen)) != 0) {
423 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424 }
425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430#endif /* MBEDTLS_X509_CSR_WRITE_C */