blob: 178b166df1b777a5061077c9bd39c0c28a19349d [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"
38#endif
39
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41#include <stdlib.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Doru Gucea2957b352018-12-14 21:08:35 +020047#include "mbedtls/platform.h"
Doru Gucea2957b352018-12-14 21:08:35 +020048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051 memset(ctx, 0, sizeof(mbedtls_x509write_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052}
53
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056 mbedtls_asn1_free_named_data_list(&ctx->subject);
57 mbedtls_asn1_free_named_data_list(&ctx->extensions);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060}
61
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063{
64 ctx->md_alg = md_alg;
65}
66
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068{
69 ctx->key = key;
70}
71
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
73 const char *subject_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076}
77
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078int mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx,
79 const char *oid, size_t oid_len,
80 const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
83 0, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084}
85
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +000090 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091
92 c = buf + 4;
93
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8);
95 if (ret < 3 || ret > 4) {
96 return ret;
97 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
100 MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
101 c, (size_t) ret);
102 if (ret != 0) {
103 return ret;
104 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107}
108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
110 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115
116 c = buf + 4;
117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
119 if (ret < 3 || ret > 4) {
120 return ret;
121 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
124 MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
125 c, (size_t) ret);
126 if (ret != 0) {
127 return ret;
128 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200131}
132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
134 unsigned char *buf,
135 size_t size,
136 unsigned char *sig,
137 int (*f_rng)(void *, unsigned char *, size_t),
138 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200139{
Janos Follath865b3eb2019-12-16 11:46:15 +0000140 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200141 const char *sig_oid;
142 size_t sig_oid_len = 0;
143 unsigned char *c, *c2;
144 unsigned char hash[64];
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
146 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_pk_type_t pk_alg;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400148#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jaeden Amero34973232019-02-20 10:32:28 +0000149 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400150 size_t hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(ctx->md_alg);
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400152#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153
Simon Leet40ca54a2020-06-26 21:23:32 +0000154 /* Write the CSR backwards starting from the end of buf */
Doru Gucea2957b352018-12-14 21:08:35 +0200155 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf,
158 ctx->extensions));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 if (len) {
161 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
162 MBEDTLS_ASN1_CHK_ADD(len,
163 mbedtls_asn1_write_tag(
164 &c, buf,
165 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
168 MBEDTLS_ASN1_CHK_ADD(len,
169 mbedtls_asn1_write_tag(
170 &c, buf,
171 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 MBEDTLS_ASN1_CHK_ADD(len,
174 mbedtls_asn1_write_oid(
175 &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
176 MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS9_CSR_EXT_REQ)));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
179 MBEDTLS_ASN1_CHK_ADD(len,
180 mbedtls_asn1_write_tag(
181 &c, buf,
182 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200183 }
184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
186 MBEDTLS_ASN1_CHK_ADD(len,
187 mbedtls_asn1_write_tag(
188 &c, buf,
189 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_pk_write_pubkey_der(ctx->key,
192 buf, c - buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200193 c -= pub_len;
194 len += pub_len;
195
196 /*
197 * Subject ::= Name
198 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
200 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200201
202 /*
203 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
204 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
208 MBEDTLS_ASN1_CHK_ADD(len,
209 mbedtls_asn1_write_tag(
210 &c, buf,
211 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200212
213 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000214 * Sign the written CSR data into the sig buffer
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400215 * Note: hash errors can happen only after an internal error
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216 */
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400217#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 if (psa_hash_setup(&hash_operation, hash_alg) != PSA_SUCCESS) {
219 return MBEDTLS_ERR_X509_FATAL_ERROR;
220 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 if (psa_hash_update(&hash_operation, c, len) != PSA_SUCCESS) {
223 return MBEDTLS_ERR_X509_FATAL_ERROR;
224 }
Andrzej Kureka6093372018-11-19 13:57:58 -0500225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 if (psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_len)
227 != PSA_SUCCESS) {
228 return MBEDTLS_ERR_X509_FATAL_ERROR;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400229 }
230#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash);
232 if (ret != 0) {
233 return ret;
234 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400235#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
237 f_rng, p_rng)) != 0) {
238 return ret;
Hanno Beckerfc771442017-09-13 08:45:48 +0100239 }
240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100242 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 } else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100244 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 } else {
246 return MBEDTLS_ERR_X509_INVALID_ALG;
247 }
Hanno Beckerfc771442017-09-13 08:45:48 +0100248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
250 &sig_oid, &sig_oid_len)) != 0) {
251 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252 }
253
254 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000255 * Move the written CSR data to the start of buf to create space for
256 * writing the signature into buf.
257 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 memmove(buf, c, len);
Doru Gucea2957b352018-12-14 21:08:35 +0200259
Simon Leet40ca54a2020-06-26 21:23:32 +0000260 /*
261 * Write sig and its OID into buf backwards from the end of buf.
262 * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
263 * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264 */
265 c2 = buf + size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len,
267 mbedtls_x509_write_sig(&c2, buf + len, sig_oid, sig_oid_len,
Marek Jansta0a6743b2022-11-07 12:38:38 +0100268 sig, sig_len, pk_alg));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269
Simon Leet40ca54a2020-06-26 21:23:32 +0000270 /*
271 * Compact the space between the CSR data and signature by moving the
272 * CSR data to the start of the signature.
273 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200274 c2 -= len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 memmove(c2, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200276
Simon Leet40ca54a2020-06-26 21:23:32 +0000277 /* ASN encode the total size and tag the CSR data with it. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278 len += sig_and_oid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c2, buf, len));
280 MBEDTLS_ASN1_CHK_ADD(len,
281 mbedtls_asn1_write_tag(
282 &c2, buf,
283 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Simon Leet40ca54a2020-06-26 21:23:32 +0000284
285 /* Zero the unused bytes at the start of buf */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 memset(buf, 0, c2 - buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289}
290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf,
292 size_t size,
293 int (*f_rng)(void *, unsigned char *, size_t),
294 void *p_rng)
Doru Gucea2957b352018-12-14 21:08:35 +0200295{
296 int ret;
297 unsigned char *sig;
298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) {
300 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Doru Gucea2957b352018-12-14 21:08:35 +0200301 }
302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 ret = x509write_csr_der_internal(ctx, buf, size, sig, f_rng, p_rng);
Doru Gucea2957b352018-12-14 21:08:35 +0200304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 mbedtls_free(sig);
Doru Gucea2957b352018-12-14 21:08:35 +0200306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 return ret;
Doru Gucea2957b352018-12-14 21:08:35 +0200308}
309
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200310#define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
311#define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
315 int (*f_rng)(void *, unsigned char *, size_t),
316 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317{
Janos Follath865b3eb2019-12-16 11:46:15 +0000318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200319 size_t olen = 0;
320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 if ((ret = mbedtls_x509write_csr_der(ctx, buf, size,
322 f_rng, p_rng)) < 0) {
323 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200324 }
325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CSR, PEM_END_CSR,
327 buf + size - ret,
328 ret, buf, size, &olen)) != 0) {
329 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200330 }
331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#endif /* MBEDTLS_X509_CSR_WRITE_C */