blob: 68930b627c801240c8abf2e284f66a4b6184f60d [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) {
Przemek Stekiel18904ac2023-02-14 11:54:37 +0100131 switch (cur->node.type) {
132 case MBEDTLS_X509_SAN_DNS_NAME:
133 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
134 case MBEDTLS_X509_SAN_IP_ADDRESS:
135 MBEDTLS_ASN1_CHK_ADD(len,
136 mbedtls_asn1_write_raw_buffer(&p, buf,
137 (const unsigned char *) cur->node
138 .name,
139 cur->node.len));
140 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, cur->node.len));
141 MBEDTLS_ASN1_CHK_ADD(len,
142 mbedtls_asn1_write_tag(&p, buf,
143 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
144 cur->node.type));
145 break;
146 default:
147 /* Skip unsupported names. */
148 break;
149 }
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100150 cur = cur->next;
151 }
152
153 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
154 MBEDTLS_ASN1_CHK_ADD(len,
155 mbedtls_asn1_write_tag(&p, buf,
156 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
157
158 ret = mbedtls_x509write_csr_set_extension(
159 ctx,
160 MBEDTLS_OID_SUBJECT_ALT_NAME,
161 MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME),
162 0,
163 buf + buflen - len,
164 len);
165
166 mbedtls_free(buf);
167 return ret;
168}
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200171{
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200173 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200175
176 c = buf + 4;
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8);
179 if (ret < 3 || ret > 4) {
180 return ret;
181 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
184 MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
185 0, c, (size_t) ret);
186 if (ret != 0) {
187 return ret;
188 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200191}
192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
194 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200195{
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200197 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199
200 c = buf + 4;
201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
203 if (ret < 3 || ret > 4) {
204 return ret;
205 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
208 MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
209 0, c, (size_t) ret);
210 if (ret != 0) {
211 return ret;
212 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200215}
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
218 unsigned char *buf,
219 size_t size,
220 unsigned char *sig, size_t sig_size,
221 int (*f_rng)(void *, unsigned char *, size_t),
222 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200223{
Janos Follath865b3eb2019-12-16 11:46:15 +0000224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200225 const char *sig_oid;
226 size_t sig_oid_len = 0;
227 unsigned char *c, *c2;
Przemek Stekiel51669542022-09-13 12:57:05 +0200228 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229 size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
230 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_pk_type_t pk_alg;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400232#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400233 size_t hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(ctx->md_alg);
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400235#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236
Simon Leet40ca54a2020-06-26 21:23:32 +0000237 /* Write the CSR backwards starting from the end of buf */
Doru Gucea2957b352018-12-14 21:08:35 +0200238 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf,
241 ctx->extensions));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (len) {
244 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
245 MBEDTLS_ASN1_CHK_ADD(len,
246 mbedtls_asn1_write_tag(
247 &c, buf,
248 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
251 MBEDTLS_ASN1_CHK_ADD(len,
252 mbedtls_asn1_write_tag(
253 &c, buf,
254 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 MBEDTLS_ASN1_CHK_ADD(len,
257 mbedtls_asn1_write_oid(
258 &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
259 MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS9_CSR_EXT_REQ)));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 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 Bakker7c6b2c32013-09-16 13:49:26 +0200266 }
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
269 MBEDTLS_ASN1_CHK_ADD(len,
270 mbedtls_asn1_write_tag(
271 &c, buf,
272 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_pk_write_pubkey_der(ctx->key,
275 buf, c - buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200276 c -= pub_len;
277 len += pub_len;
278
279 /*
280 * Subject ::= Name
281 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
283 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284
285 /*
286 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
287 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
291 MBEDTLS_ASN1_CHK_ADD(len,
292 mbedtls_asn1_write_tag(
293 &c, buf,
294 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200295
296 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000297 * Sign the written CSR data into the sig buffer
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400298 * Note: hash errors can happen only after an internal error
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200299 */
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400300#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (psa_hash_compute(hash_alg,
302 c,
303 len,
304 hash,
305 sizeof(hash),
306 &hash_len) != PSA_SUCCESS) {
307 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400308 }
309#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash);
311 if (ret != 0) {
312 return ret;
313 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400314#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0,
316 sig, sig_size, &sig_len,
317 f_rng, p_rng)) != 0) {
318 return ret;
Hanno Beckerfc771442017-09-13 08:45:48 +0100319 }
320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100322 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 } else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100324 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 } else {
326 return MBEDTLS_ERR_X509_INVALID_ALG;
327 }
Hanno Beckerfc771442017-09-13 08:45:48 +0100328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
330 &sig_oid, &sig_oid_len)) != 0) {
331 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200332 }
333
334 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000335 * Move the written CSR data to the start of buf to create space for
336 * writing the signature into buf.
337 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 memmove(buf, c, len);
Doru Gucea2957b352018-12-14 21:08:35 +0200339
Simon Leet40ca54a2020-06-26 21:23:32 +0000340 /*
341 * Write sig and its OID into buf backwards from the end of buf.
342 * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
343 * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344 */
345 c2 = buf + size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len,
347 mbedtls_x509_write_sig(&c2, buf + len, sig_oid, sig_oid_len,
348 sig, sig_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349
Simon Leet40ca54a2020-06-26 21:23:32 +0000350 /*
351 * Compact the space between the CSR data and signature by moving the
352 * CSR data to the start of the signature.
353 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354 c2 -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 memmove(c2, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356
Simon Leet40ca54a2020-06-26 21:23:32 +0000357 /* ASN encode the total size and tag the CSR data with it. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358 len += sig_and_oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c2, buf, len));
360 MBEDTLS_ASN1_CHK_ADD(len,
361 mbedtls_asn1_write_tag(
362 &c2, buf,
363 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Simon Leet40ca54a2020-06-26 21:23:32 +0000364
365 /* Zero the unused bytes at the start of buf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 memset(buf, 0, c2 - buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369}
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf,
372 size_t size,
373 int (*f_rng)(void *, unsigned char *, size_t),
374 void *p_rng)
Doru Gucea2957b352018-12-14 21:08:35 +0200375{
376 int ret;
377 unsigned char *sig;
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) {
380 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Doru Gucea2957b352018-12-14 21:08:35 +0200381 }
382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 ret = x509write_csr_der_internal(ctx, buf, size,
384 sig, MBEDTLS_PK_SIGNATURE_MAX_SIZE,
385 f_rng, p_rng);
Doru Gucea2957b352018-12-14 21:08:35 +0200386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 mbedtls_free(sig);
Doru Gucea2957b352018-12-14 21:08:35 +0200388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return ret;
Doru Gucea2957b352018-12-14 21:08:35 +0200390}
391
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392#define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
393#define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100396int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
397 int (*f_rng)(void *, unsigned char *, size_t),
398 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399{
Janos Follath865b3eb2019-12-16 11:46:15 +0000400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401 size_t olen = 0;
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if ((ret = mbedtls_x509write_csr_der(ctx, buf, size,
404 f_rng, p_rng)) < 0) {
405 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 }
407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CSR, PEM_END_CSR,
409 buf + size - ret,
410 ret, buf, size, &olen)) != 0) {
411 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 }
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#endif /* MBEDTLS_X509_CSR_WRITE_C */