blob: 1d46fdbdc3fed68a4174a940223493d204c963a2 [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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/x509_csr.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/asn1write.h"
Janos Follath73c616b2019-12-18 15:07:04 +000031#include "mbedtls/error.h"
32#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050033#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034
Andrzej Kurekd4a65532018-10-31 06:18:39 -040035#if defined(MBEDTLS_USE_PSA_CRYPTO)
36#include "psa/crypto.h"
37#include "mbedtls/psa_util.h"
pespacek7599a772022-02-07 14:40:55 +010038#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemek Stekiel51669542022-09-13 12:57:05 +020039#include "hash_info.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42#include <stdlib.h>
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020046#endif
47
Doru Gucea2957b352018-12-14 21:08:35 +020048#include "mbedtls/platform.h"
Doru Gucea2957b352018-12-14 21:08:35 +020049
Gilles Peskine449bd832023-01-11 14:50:10 +010050void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051{
Gilles Peskine449bd832023-01-11 14:50:10 +010052 memset(ctx, 0, sizeof(mbedtls_x509write_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053}
54
Gilles Peskine449bd832023-01-11 14:50:10 +010055void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056{
Gilles Peskine449bd832023-01-11 14:50:10 +010057 mbedtls_asn1_free_named_data_list(&ctx->subject);
58 mbedtls_asn1_free_named_data_list(&ctx->extensions);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059
Gilles Peskine449bd832023-01-11 14:50:10 +010060 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061}
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064{
65 ctx->md_alg = md_alg;
66}
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069{
70 ctx->key = key;
71}
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
74 const char *subject_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075{
Gilles Peskine449bd832023-01-11 14:50:10 +010076 return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077}
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079int 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 Bakker7c6b2c32013-09-16 13:49:26 +020083{
Gilles Peskine449bd832023-01-11 14:50:10 +010084 return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
85 critical, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086}
87
Hannes Tschofenig6b108602022-12-28 18:38:53 +010088int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx,
89 const mbedtls_x509_san_list *san_list)
90{
91 int ret = 0;
92 size_t sandeep = 0;
93 const mbedtls_x509_san_list *cur = san_list;
94 unsigned char *buf;
95 unsigned char *p;
96 size_t len;
97 size_t buflen = 0;
98
99 /* Determine the maximum size of the SubjectAltName list */
100 while (cur != NULL) {
101 if (cur->node.len <= 0) {
102 return 0;
103 }
104
105 /* Calculate size of the required buffer:
106 * + length of value for each name entry,
107 * + maximum 4 bytes for the length field,
108 * + 1 byte for the tag/type.
109 */
110 buflen += cur->node.len + 4 + 1;
111
112 cur = cur->next;
113 }
114
115 /* Add the extra length field and tag */
116 buflen += 4 + 1;
117
118 /* Allocate buffer */
119 buf = mbedtls_calloc(1, buflen);
120 if (buf == NULL) {
121 return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
122 }
123
124 mbedtls_platform_zeroize(buf, buflen);
125 p = buf + buflen;
126
127 /* Write ASN.1-based structure */
128 cur = san_list;
129 len = 0;
130 while (cur != NULL) {
131 MBEDTLS_ASN1_CHK_ADD(len,
132 mbedtls_asn1_write_raw_buffer(&p, buf,
133 (const unsigned char *) cur->node.name,
134 cur->node.len));
135 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, cur->node.len));
136 MBEDTLS_ASN1_CHK_ADD(len,
137 mbedtls_asn1_write_tag(&p, buf,
138 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
139 cur->node.type));
140
141 cur = cur->next;
142 }
143
144 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
145 MBEDTLS_ASN1_CHK_ADD(len,
146 mbedtls_asn1_write_tag(&p, buf,
147 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
148
149 ret = mbedtls_x509write_csr_set_extension(
150 ctx,
151 MBEDTLS_OID_SUBJECT_ALT_NAME,
152 MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME),
153 0,
154 buf + buflen - len,
155 len);
156
157 mbedtls_free(buf);
158 return ret;
159}
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200162{
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200164 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000165 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200166
167 c = buf + 4;
168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8);
170 if (ret < 3 || ret > 4) {
171 return ret;
172 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
175 MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
176 0, c, (size_t) ret);
177 if (ret != 0) {
178 return ret;
179 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200182}
183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
185 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186{
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200188 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200190
191 c = buf + 4;
192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
194 if (ret < 3 || ret > 4) {
195 return ret;
196 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
199 MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
200 0, c, (size_t) ret);
201 if (ret != 0) {
202 return ret;
203 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206}
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
209 unsigned char *buf,
210 size_t size,
211 unsigned char *sig, size_t sig_size,
212 int (*f_rng)(void *, unsigned char *, size_t),
213 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200214{
Janos Follath865b3eb2019-12-16 11:46:15 +0000215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216 const char *sig_oid;
217 size_t sig_oid_len = 0;
218 unsigned char *c, *c2;
Przemek Stekiel51669542022-09-13 12:57:05 +0200219 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200220 size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
221 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 mbedtls_pk_type_t pk_alg;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400224 size_t hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(ctx->md_alg);
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400226#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200227
Simon Leet40ca54a2020-06-26 21:23:32 +0000228 /* Write the CSR backwards starting from the end of buf */
Doru Gucea2957b352018-12-14 21:08:35 +0200229 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf,
232 ctx->extensions));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if (len) {
235 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
236 MBEDTLS_ASN1_CHK_ADD(len,
237 mbedtls_asn1_write_tag(
238 &c, buf,
239 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
242 MBEDTLS_ASN1_CHK_ADD(len,
243 mbedtls_asn1_write_tag(
244 &c, buf,
245 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 MBEDTLS_ASN1_CHK_ADD(len,
248 mbedtls_asn1_write_oid(
249 &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
250 MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS9_CSR_EXT_REQ)));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
253 MBEDTLS_ASN1_CHK_ADD(len,
254 mbedtls_asn1_write_tag(
255 &c, buf,
256 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200257 }
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
260 MBEDTLS_ASN1_CHK_ADD(len,
261 mbedtls_asn1_write_tag(
262 &c, buf,
263 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_pk_write_pubkey_der(ctx->key,
266 buf, c - buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200267 c -= pub_len;
268 len += pub_len;
269
270 /*
271 * Subject ::= Name
272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
274 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275
276 /*
277 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
278 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
282 MBEDTLS_ASN1_CHK_ADD(len,
283 mbedtls_asn1_write_tag(
284 &c, buf,
285 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286
287 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000288 * Sign the written CSR data into the sig buffer
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400289 * Note: hash errors can happen only after an internal error
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290 */
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400291#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (psa_hash_compute(hash_alg,
293 c,
294 len,
295 hash,
296 sizeof(hash),
297 &hash_len) != PSA_SUCCESS) {
298 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400299 }
300#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash);
302 if (ret != 0) {
303 return ret;
304 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400305#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0,
307 sig, sig_size, &sig_len,
308 f_rng, p_rng)) != 0) {
309 return ret;
Hanno Beckerfc771442017-09-13 08:45:48 +0100310 }
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100313 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 } else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100315 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 } else {
317 return MBEDTLS_ERR_X509_INVALID_ALG;
318 }
Hanno Beckerfc771442017-09-13 08:45:48 +0100319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
321 &sig_oid, &sig_oid_len)) != 0) {
322 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323 }
324
325 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000326 * Move the written CSR data to the start of buf to create space for
327 * writing the signature into buf.
328 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 memmove(buf, c, len);
Doru Gucea2957b352018-12-14 21:08:35 +0200330
Simon Leet40ca54a2020-06-26 21:23:32 +0000331 /*
332 * Write sig and its OID into buf backwards from the end of buf.
333 * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
334 * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335 */
336 c2 = buf + size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len,
338 mbedtls_x509_write_sig(&c2, buf + len, sig_oid, sig_oid_len,
339 sig, sig_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200340
Simon Leet40ca54a2020-06-26 21:23:32 +0000341 /*
342 * Compact the space between the CSR data and signature by moving the
343 * CSR data to the start of the signature.
344 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345 c2 -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 memmove(c2, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347
Simon Leet40ca54a2020-06-26 21:23:32 +0000348 /* ASN encode the total size and tag the CSR data with it. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349 len += sig_and_oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c2, buf, len));
351 MBEDTLS_ASN1_CHK_ADD(len,
352 mbedtls_asn1_write_tag(
353 &c2, buf,
354 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Simon Leet40ca54a2020-06-26 21:23:32 +0000355
356 /* Zero the unused bytes at the start of buf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 memset(buf, 0, c2 - buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360}
361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf,
363 size_t size,
364 int (*f_rng)(void *, unsigned char *, size_t),
365 void *p_rng)
Doru Gucea2957b352018-12-14 21:08:35 +0200366{
367 int ret;
368 unsigned char *sig;
369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) {
371 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Doru Gucea2957b352018-12-14 21:08:35 +0200372 }
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 ret = x509write_csr_der_internal(ctx, buf, size,
375 sig, MBEDTLS_PK_SIGNATURE_MAX_SIZE,
376 f_rng, p_rng);
Doru Gucea2957b352018-12-14 21:08:35 +0200377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 mbedtls_free(sig);
Doru Gucea2957b352018-12-14 21:08:35 +0200379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return ret;
Doru Gucea2957b352018-12-14 21:08:35 +0200381}
382
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383#define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
384#define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100387int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
388 int (*f_rng)(void *, unsigned char *, size_t),
389 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390{
Janos Follath865b3eb2019-12-16 11:46:15 +0000391 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392 size_t olen = 0;
393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 if ((ret = mbedtls_x509write_csr_der(ctx, buf, size,
395 f_rng, p_rng)) < 0) {
396 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397 }
398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CSR, PEM_END_CSR,
400 buf + size - ret,
401 ret, buf, size, &olen)) != 0) {
402 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403 }
404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_X509_CSR_WRITE_C */