blob: c0fe0a873262dc3f841f4ce70e455bdc7167e454 [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;
Hannes Tschofenig6b108602022-12-28 18:38:53 +010092 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 Stekiel18904ac2023-02-14 11:54:37 +0100130 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 Tschofenig6b108602022-12-28 18:38:53 +0100149 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 Peskine449bd832023-01-11 14:50:10 +0100169int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200170{
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200174
175 c = buf + 4;
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8);
178 if (ret < 3 || ret > 4) {
179 return ret;
180 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 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 Bakker7c6b2c32013-09-16 13:49:26 +0200188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200190}
191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
193 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200194{
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200196 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000197 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200198
199 c = buf + 4;
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
202 if (ret < 3 || ret > 4) {
203 return ret;
204 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 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 Bakker7c6b2c32013-09-16 13:49:26 +0200212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200214}
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216static 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 Bakker7c6b2c32013-09-16 13:49:26 +0200222{
Janos Follath865b3eb2019-12-16 11:46:15 +0000223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200224 const char *sig_oid;
225 size_t sig_oid_len = 0;
226 unsigned char *c, *c2;
Przemek Stekiel51669542022-09-13 12:57:05 +0200227 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200228 size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
229 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_pk_type_t pk_alg;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400231#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400232 size_t hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(ctx->md_alg);
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400234#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200235
Simon Leet40ca54a2020-06-26 21:23:32 +0000236 /* Write the CSR backwards starting from the end of buf */
Doru Gucea2957b352018-12-14 21:08:35 +0200237 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf,
240 ctx->extensions));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 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 Bakker7c6b2c32013-09-16 13:49:26 +0200248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 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 Bakker7c6b2c32013-09-16 13:49:26 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 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 Bakker7c6b2c32013-09-16 13:49:26 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 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 Bakker7c6b2c32013-09-16 13:49:26 +0200265 }
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 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 Bakker7c6b2c32013-09-16 13:49:26 +0200272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_pk_write_pubkey_der(ctx->key,
274 buf, c - buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275 c -= pub_len;
276 len += pub_len;
277
278 /*
279 * Subject ::= Name
280 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
282 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283
284 /*
285 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
286 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 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 Bakker7c6b2c32013-09-16 13:49:26 +0200294
295 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000296 * Sign the written CSR data into the sig buffer
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400297 * Note: hash errors can happen only after an internal error
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298 */
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400299#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 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 Kurekd4a65532018-10-31 06:18:39 -0400307 }
308#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash);
310 if (ret != 0) {
311 return ret;
312 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400313#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 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 Beckerfc771442017-09-13 08:45:48 +0100318 }
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100321 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 } else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100323 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 } else {
325 return MBEDTLS_ERR_X509_INVALID_ALG;
326 }
Hanno Beckerfc771442017-09-13 08:45:48 +0100327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 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 Bakker7c6b2c32013-09-16 13:49:26 +0200331 }
332
333 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000334 * Move the written CSR data to the start of buf to create space for
335 * writing the signature into buf.
336 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 memmove(buf, c, len);
Doru Gucea2957b352018-12-14 21:08:35 +0200338
Simon Leet40ca54a2020-06-26 21:23:32 +0000339 /*
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 Bakker7c6b2c32013-09-16 13:49:26 +0200343 */
344 c2 = buf + size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 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 Bakker7c6b2c32013-09-16 13:49:26 +0200348
Simon Leet40ca54a2020-06-26 21:23:32 +0000349 /*
350 * Compact the space between the CSR data and signature by moving the
351 * CSR data to the start of the signature.
352 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353 c2 -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 memmove(c2, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355
Simon Leet40ca54a2020-06-26 21:23:32 +0000356 /* ASN encode the total size and tag the CSR data with it. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357 len += sig_and_oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 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 Leet40ca54a2020-06-26 21:23:32 +0000363
364 /* Zero the unused bytes at the start of buf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 memset(buf, 0, c2 - buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368}
369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370int 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 Gucea2957b352018-12-14 21:08:35 +0200374{
375 int ret;
376 unsigned char *sig;
377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) {
379 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Doru Gucea2957b352018-12-14 21:08:35 +0200380 }
381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 ret = x509write_csr_der_internal(ctx, buf, size,
383 sig, MBEDTLS_PK_SIGNATURE_MAX_SIZE,
384 f_rng, p_rng);
Doru Gucea2957b352018-12-14 21:08:35 +0200385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 mbedtls_free(sig);
Doru Gucea2957b352018-12-14 21:08:35 +0200387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return ret;
Doru Gucea2957b352018-12-14 21:08:35 +0200389}
390
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391#define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
392#define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100395int 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 Bakker7c6b2c32013-09-16 13:49:26 +0200398{
Janos Follath865b3eb2019-12-16 11:46:15 +0000399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 size_t olen = 0;
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if ((ret = mbedtls_x509write_csr_der(ctx, buf, size,
403 f_rng, p_rng)) < 0) {
404 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 }
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 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 Bakker7c6b2c32013-09-16 13:49:26 +0200411 }
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_X509_CSR_WRITE_C */