1a8e1175bSopenharmony_ci/*
2a8e1175bSopenharmony_ci *  X.509 Certificate Signing Request writing
3a8e1175bSopenharmony_ci *
4a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
5a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6a8e1175bSopenharmony_ci */
7a8e1175bSopenharmony_ci/*
8a8e1175bSopenharmony_ci * References:
9a8e1175bSopenharmony_ci * - CSRs: PKCS#10 v1.7 aka RFC 2986
10a8e1175bSopenharmony_ci * - attributes: PKCS#9 v2.0 aka RFC 2985
11a8e1175bSopenharmony_ci */
12a8e1175bSopenharmony_ci
13a8e1175bSopenharmony_ci#include "common.h"
14a8e1175bSopenharmony_ci
15a8e1175bSopenharmony_ci#if defined(MBEDTLS_X509_CSR_WRITE_C)
16a8e1175bSopenharmony_ci
17a8e1175bSopenharmony_ci#include "x509_internal.h"
18a8e1175bSopenharmony_ci#include "mbedtls/x509_csr.h"
19a8e1175bSopenharmony_ci#include "mbedtls/asn1write.h"
20a8e1175bSopenharmony_ci#include "mbedtls/error.h"
21a8e1175bSopenharmony_ci#include "mbedtls/oid.h"
22a8e1175bSopenharmony_ci#include "mbedtls/platform_util.h"
23a8e1175bSopenharmony_ci
24a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
25a8e1175bSopenharmony_ci#include "psa/crypto.h"
26a8e1175bSopenharmony_ci#include "psa_util_internal.h"
27a8e1175bSopenharmony_ci#include "mbedtls/psa_util.h"
28a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */
29a8e1175bSopenharmony_ci
30a8e1175bSopenharmony_ci#include <string.h>
31a8e1175bSopenharmony_ci#include <stdlib.h>
32a8e1175bSopenharmony_ci
33a8e1175bSopenharmony_ci#if defined(MBEDTLS_PEM_WRITE_C)
34a8e1175bSopenharmony_ci#include "mbedtls/pem.h"
35a8e1175bSopenharmony_ci#endif
36a8e1175bSopenharmony_ci
37a8e1175bSopenharmony_ci#include "mbedtls/platform.h"
38a8e1175bSopenharmony_ci
39a8e1175bSopenharmony_civoid mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx)
40a8e1175bSopenharmony_ci{
41a8e1175bSopenharmony_ci    memset(ctx, 0, sizeof(mbedtls_x509write_csr));
42a8e1175bSopenharmony_ci}
43a8e1175bSopenharmony_ci
44a8e1175bSopenharmony_civoid mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx)
45a8e1175bSopenharmony_ci{
46a8e1175bSopenharmony_ci    mbedtls_asn1_free_named_data_list(&ctx->subject);
47a8e1175bSopenharmony_ci    mbedtls_asn1_free_named_data_list(&ctx->extensions);
48a8e1175bSopenharmony_ci
49a8e1175bSopenharmony_ci    mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_csr));
50a8e1175bSopenharmony_ci}
51a8e1175bSopenharmony_ci
52a8e1175bSopenharmony_civoid mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg)
53a8e1175bSopenharmony_ci{
54a8e1175bSopenharmony_ci    ctx->md_alg = md_alg;
55a8e1175bSopenharmony_ci}
56a8e1175bSopenharmony_ci
57a8e1175bSopenharmony_civoid mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key)
58a8e1175bSopenharmony_ci{
59a8e1175bSopenharmony_ci    ctx->key = key;
60a8e1175bSopenharmony_ci}
61a8e1175bSopenharmony_ci
62a8e1175bSopenharmony_ciint mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
63a8e1175bSopenharmony_ci                                           const char *subject_name)
64a8e1175bSopenharmony_ci{
65a8e1175bSopenharmony_ci    return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
66a8e1175bSopenharmony_ci}
67a8e1175bSopenharmony_ci
68a8e1175bSopenharmony_ciint mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx,
69a8e1175bSopenharmony_ci                                        const char *oid, size_t oid_len,
70a8e1175bSopenharmony_ci                                        int critical,
71a8e1175bSopenharmony_ci                                        const unsigned char *val, size_t val_len)
72a8e1175bSopenharmony_ci{
73a8e1175bSopenharmony_ci    return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
74a8e1175bSopenharmony_ci                                      critical, val, val_len);
75a8e1175bSopenharmony_ci}
76a8e1175bSopenharmony_ci
77a8e1175bSopenharmony_ciint mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx,
78a8e1175bSopenharmony_ci                                                       const mbedtls_x509_san_list *san_list)
79a8e1175bSopenharmony_ci{
80a8e1175bSopenharmony_ci    return mbedtls_x509_write_set_san_common(&ctx->extensions, san_list);
81a8e1175bSopenharmony_ci}
82a8e1175bSopenharmony_ci
83a8e1175bSopenharmony_ciint mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage)
84a8e1175bSopenharmony_ci{
85a8e1175bSopenharmony_ci    unsigned char buf[4] = { 0 };
86a8e1175bSopenharmony_ci    unsigned char *c;
87a8e1175bSopenharmony_ci    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
88a8e1175bSopenharmony_ci
89a8e1175bSopenharmony_ci    c = buf + 4;
90a8e1175bSopenharmony_ci
91a8e1175bSopenharmony_ci    ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8);
92a8e1175bSopenharmony_ci    if (ret < 3 || ret > 4) {
93a8e1175bSopenharmony_ci        return ret;
94a8e1175bSopenharmony_ci    }
95a8e1175bSopenharmony_ci
96a8e1175bSopenharmony_ci    ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
97a8e1175bSopenharmony_ci                                              MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
98a8e1175bSopenharmony_ci                                              0, c, (size_t) ret);
99a8e1175bSopenharmony_ci    if (ret != 0) {
100a8e1175bSopenharmony_ci        return ret;
101a8e1175bSopenharmony_ci    }
102a8e1175bSopenharmony_ci
103a8e1175bSopenharmony_ci    return 0;
104a8e1175bSopenharmony_ci}
105a8e1175bSopenharmony_ci
106a8e1175bSopenharmony_ciint mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
107a8e1175bSopenharmony_ci                                           unsigned char ns_cert_type)
108a8e1175bSopenharmony_ci{
109a8e1175bSopenharmony_ci    unsigned char buf[4] = { 0 };
110a8e1175bSopenharmony_ci    unsigned char *c;
111a8e1175bSopenharmony_ci    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
112a8e1175bSopenharmony_ci
113a8e1175bSopenharmony_ci    c = buf + 4;
114a8e1175bSopenharmony_ci
115a8e1175bSopenharmony_ci    ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
116a8e1175bSopenharmony_ci    if (ret < 3 || ret > 4) {
117a8e1175bSopenharmony_ci        return ret;
118a8e1175bSopenharmony_ci    }
119a8e1175bSopenharmony_ci
120a8e1175bSopenharmony_ci    ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
121a8e1175bSopenharmony_ci                                              MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
122a8e1175bSopenharmony_ci                                              0, c, (size_t) ret);
123a8e1175bSopenharmony_ci    if (ret != 0) {
124a8e1175bSopenharmony_ci        return ret;
125a8e1175bSopenharmony_ci    }
126a8e1175bSopenharmony_ci
127a8e1175bSopenharmony_ci    return 0;
128a8e1175bSopenharmony_ci}
129a8e1175bSopenharmony_ci
130a8e1175bSopenharmony_cistatic int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
131a8e1175bSopenharmony_ci                                      unsigned char *buf,
132a8e1175bSopenharmony_ci                                      size_t size,
133a8e1175bSopenharmony_ci                                      unsigned char *sig, size_t sig_size,
134a8e1175bSopenharmony_ci                                      int (*f_rng)(void *, unsigned char *, size_t),
135a8e1175bSopenharmony_ci                                      void *p_rng)
136a8e1175bSopenharmony_ci{
137a8e1175bSopenharmony_ci    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
138a8e1175bSopenharmony_ci    const char *sig_oid;
139a8e1175bSopenharmony_ci    size_t sig_oid_len = 0;
140a8e1175bSopenharmony_ci    unsigned char *c, *c2;
141a8e1175bSopenharmony_ci    unsigned char hash[MBEDTLS_MD_MAX_SIZE];
142a8e1175bSopenharmony_ci    size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
143a8e1175bSopenharmony_ci    size_t len = 0;
144a8e1175bSopenharmony_ci    mbedtls_pk_type_t pk_alg;
145a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
146a8e1175bSopenharmony_ci    size_t hash_len;
147a8e1175bSopenharmony_ci    psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(ctx->md_alg);
148a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */
149a8e1175bSopenharmony_ci
150a8e1175bSopenharmony_ci    /* Write the CSR backwards starting from the end of buf */
151a8e1175bSopenharmony_ci    c = buf + size;
152a8e1175bSopenharmony_ci
153a8e1175bSopenharmony_ci    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf,
154a8e1175bSopenharmony_ci                                                            ctx->extensions));
155a8e1175bSopenharmony_ci
156a8e1175bSopenharmony_ci    if (len) {
157a8e1175bSopenharmony_ci        MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
158a8e1175bSopenharmony_ci        MBEDTLS_ASN1_CHK_ADD(len,
159a8e1175bSopenharmony_ci                             mbedtls_asn1_write_tag(
160a8e1175bSopenharmony_ci                                 &c, buf,
161a8e1175bSopenharmony_ci                                 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
162a8e1175bSopenharmony_ci
163a8e1175bSopenharmony_ci        MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
164a8e1175bSopenharmony_ci        MBEDTLS_ASN1_CHK_ADD(len,
165a8e1175bSopenharmony_ci                             mbedtls_asn1_write_tag(
166a8e1175bSopenharmony_ci                                 &c, buf,
167a8e1175bSopenharmony_ci                                 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
168a8e1175bSopenharmony_ci
169a8e1175bSopenharmony_ci        MBEDTLS_ASN1_CHK_ADD(len,
170a8e1175bSopenharmony_ci                             mbedtls_asn1_write_oid(
171a8e1175bSopenharmony_ci                                 &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
172a8e1175bSopenharmony_ci                                 MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS9_CSR_EXT_REQ)));
173a8e1175bSopenharmony_ci
174a8e1175bSopenharmony_ci        MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
175a8e1175bSopenharmony_ci        MBEDTLS_ASN1_CHK_ADD(len,
176a8e1175bSopenharmony_ci                             mbedtls_asn1_write_tag(
177a8e1175bSopenharmony_ci                                 &c, buf,
178a8e1175bSopenharmony_ci                                 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
179a8e1175bSopenharmony_ci    }
180a8e1175bSopenharmony_ci
181a8e1175bSopenharmony_ci    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
182a8e1175bSopenharmony_ci    MBEDTLS_ASN1_CHK_ADD(len,
183a8e1175bSopenharmony_ci                         mbedtls_asn1_write_tag(
184a8e1175bSopenharmony_ci                             &c, buf,
185a8e1175bSopenharmony_ci                             MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC));
186a8e1175bSopenharmony_ci
187a8e1175bSopenharmony_ci    MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_pk_write_pubkey_der(ctx->key,
188a8e1175bSopenharmony_ci                                                              buf, (size_t) (c - buf)));
189a8e1175bSopenharmony_ci    c -= pub_len;
190a8e1175bSopenharmony_ci    len += pub_len;
191a8e1175bSopenharmony_ci
192a8e1175bSopenharmony_ci    /*
193a8e1175bSopenharmony_ci     *  Subject  ::=  Name
194a8e1175bSopenharmony_ci     */
195a8e1175bSopenharmony_ci    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
196a8e1175bSopenharmony_ci                                                       ctx->subject));
197a8e1175bSopenharmony_ci
198a8e1175bSopenharmony_ci    /*
199a8e1175bSopenharmony_ci     *  Version  ::=  INTEGER  {  v1(0), v2(1), v3(2)  }
200a8e1175bSopenharmony_ci     */
201a8e1175bSopenharmony_ci    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
202a8e1175bSopenharmony_ci
203a8e1175bSopenharmony_ci    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
204a8e1175bSopenharmony_ci    MBEDTLS_ASN1_CHK_ADD(len,
205a8e1175bSopenharmony_ci                         mbedtls_asn1_write_tag(
206a8e1175bSopenharmony_ci                             &c, buf,
207a8e1175bSopenharmony_ci                             MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
208a8e1175bSopenharmony_ci
209a8e1175bSopenharmony_ci    /*
210a8e1175bSopenharmony_ci     * Sign the written CSR data into the sig buffer
211a8e1175bSopenharmony_ci     * Note: hash errors can happen only after an internal error
212a8e1175bSopenharmony_ci     */
213a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO)
214a8e1175bSopenharmony_ci    if (psa_hash_compute(hash_alg,
215a8e1175bSopenharmony_ci                         c,
216a8e1175bSopenharmony_ci                         len,
217a8e1175bSopenharmony_ci                         hash,
218a8e1175bSopenharmony_ci                         sizeof(hash),
219a8e1175bSopenharmony_ci                         &hash_len) != PSA_SUCCESS) {
220a8e1175bSopenharmony_ci        return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
221a8e1175bSopenharmony_ci    }
222a8e1175bSopenharmony_ci#else /* MBEDTLS_USE_PSA_CRYPTO */
223a8e1175bSopenharmony_ci    ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash);
224a8e1175bSopenharmony_ci    if (ret != 0) {
225a8e1175bSopenharmony_ci        return ret;
226a8e1175bSopenharmony_ci    }
227a8e1175bSopenharmony_ci#endif
228a8e1175bSopenharmony_ci    if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0,
229a8e1175bSopenharmony_ci                               sig, sig_size, &sig_len,
230a8e1175bSopenharmony_ci                               f_rng, p_rng)) != 0) {
231a8e1175bSopenharmony_ci        return ret;
232a8e1175bSopenharmony_ci    }
233a8e1175bSopenharmony_ci
234a8e1175bSopenharmony_ci    if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) {
235a8e1175bSopenharmony_ci        pk_alg = MBEDTLS_PK_RSA;
236a8e1175bSopenharmony_ci    } else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) {
237a8e1175bSopenharmony_ci        pk_alg = MBEDTLS_PK_ECDSA;
238a8e1175bSopenharmony_ci    } else {
239a8e1175bSopenharmony_ci        return MBEDTLS_ERR_X509_INVALID_ALG;
240a8e1175bSopenharmony_ci    }
241a8e1175bSopenharmony_ci
242a8e1175bSopenharmony_ci    if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
243a8e1175bSopenharmony_ci                                              &sig_oid, &sig_oid_len)) != 0) {
244a8e1175bSopenharmony_ci        return ret;
245a8e1175bSopenharmony_ci    }
246a8e1175bSopenharmony_ci
247a8e1175bSopenharmony_ci    /*
248a8e1175bSopenharmony_ci     * Move the written CSR data to the start of buf to create space for
249a8e1175bSopenharmony_ci     * writing the signature into buf.
250a8e1175bSopenharmony_ci     */
251a8e1175bSopenharmony_ci    memmove(buf, c, len);
252a8e1175bSopenharmony_ci
253a8e1175bSopenharmony_ci    /*
254a8e1175bSopenharmony_ci     * Write sig and its OID into buf backwards from the end of buf.
255a8e1175bSopenharmony_ci     * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
256a8e1175bSopenharmony_ci     * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
257a8e1175bSopenharmony_ci     */
258a8e1175bSopenharmony_ci    c2 = buf + size;
259a8e1175bSopenharmony_ci    MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len,
260a8e1175bSopenharmony_ci                         mbedtls_x509_write_sig(&c2, buf + len, sig_oid, sig_oid_len,
261a8e1175bSopenharmony_ci                                                sig, sig_len, pk_alg));
262a8e1175bSopenharmony_ci
263a8e1175bSopenharmony_ci    /*
264a8e1175bSopenharmony_ci     * Compact the space between the CSR data and signature by moving the
265a8e1175bSopenharmony_ci     * CSR data to the start of the signature.
266a8e1175bSopenharmony_ci     */
267a8e1175bSopenharmony_ci    c2 -= len;
268a8e1175bSopenharmony_ci    memmove(c2, buf, len);
269a8e1175bSopenharmony_ci
270a8e1175bSopenharmony_ci    /* ASN encode the total size and tag the CSR data with it. */
271a8e1175bSopenharmony_ci    len += sig_and_oid_len;
272a8e1175bSopenharmony_ci    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c2, buf, len));
273a8e1175bSopenharmony_ci    MBEDTLS_ASN1_CHK_ADD(len,
274a8e1175bSopenharmony_ci                         mbedtls_asn1_write_tag(
275a8e1175bSopenharmony_ci                             &c2, buf,
276a8e1175bSopenharmony_ci                             MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
277a8e1175bSopenharmony_ci
278a8e1175bSopenharmony_ci    /* Zero the unused bytes at the start of buf */
279a8e1175bSopenharmony_ci    memset(buf, 0, (size_t) (c2 - buf));
280a8e1175bSopenharmony_ci
281a8e1175bSopenharmony_ci    return (int) len;
282a8e1175bSopenharmony_ci}
283a8e1175bSopenharmony_ci
284a8e1175bSopenharmony_ciint mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf,
285a8e1175bSopenharmony_ci                              size_t size,
286a8e1175bSopenharmony_ci                              int (*f_rng)(void *, unsigned char *, size_t),
287a8e1175bSopenharmony_ci                              void *p_rng)
288a8e1175bSopenharmony_ci{
289a8e1175bSopenharmony_ci    int ret;
290a8e1175bSopenharmony_ci    unsigned char *sig;
291a8e1175bSopenharmony_ci
292a8e1175bSopenharmony_ci    if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) {
293a8e1175bSopenharmony_ci        return MBEDTLS_ERR_X509_ALLOC_FAILED;
294a8e1175bSopenharmony_ci    }
295a8e1175bSopenharmony_ci
296a8e1175bSopenharmony_ci    ret = x509write_csr_der_internal(ctx, buf, size,
297a8e1175bSopenharmony_ci                                     sig, MBEDTLS_PK_SIGNATURE_MAX_SIZE,
298a8e1175bSopenharmony_ci                                     f_rng, p_rng);
299a8e1175bSopenharmony_ci
300a8e1175bSopenharmony_ci    mbedtls_free(sig);
301a8e1175bSopenharmony_ci
302a8e1175bSopenharmony_ci    return ret;
303a8e1175bSopenharmony_ci}
304a8e1175bSopenharmony_ci
305a8e1175bSopenharmony_ci#define PEM_BEGIN_CSR           "-----BEGIN CERTIFICATE REQUEST-----\n"
306a8e1175bSopenharmony_ci#define PEM_END_CSR             "-----END CERTIFICATE REQUEST-----\n"
307a8e1175bSopenharmony_ci
308a8e1175bSopenharmony_ci#if defined(MBEDTLS_PEM_WRITE_C)
309a8e1175bSopenharmony_ciint mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
310a8e1175bSopenharmony_ci                              int (*f_rng)(void *, unsigned char *, size_t),
311a8e1175bSopenharmony_ci                              void *p_rng)
312a8e1175bSopenharmony_ci{
313a8e1175bSopenharmony_ci    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
314a8e1175bSopenharmony_ci    size_t olen = 0;
315a8e1175bSopenharmony_ci
316a8e1175bSopenharmony_ci    if ((ret = mbedtls_x509write_csr_der(ctx, buf, size,
317a8e1175bSopenharmony_ci                                         f_rng, p_rng)) < 0) {
318a8e1175bSopenharmony_ci        return ret;
319a8e1175bSopenharmony_ci    }
320a8e1175bSopenharmony_ci
321a8e1175bSopenharmony_ci    if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CSR, PEM_END_CSR,
322a8e1175bSopenharmony_ci                                        buf + size - ret,
323a8e1175bSopenharmony_ci                                        ret, buf, size, &olen)) != 0) {
324a8e1175bSopenharmony_ci        return ret;
325a8e1175bSopenharmony_ci    }
326a8e1175bSopenharmony_ci
327a8e1175bSopenharmony_ci    return 0;
328a8e1175bSopenharmony_ci}
329a8e1175bSopenharmony_ci#endif /* MBEDTLS_PEM_WRITE_C */
330a8e1175bSopenharmony_ci
331a8e1175bSopenharmony_ci#endif /* MBEDTLS_X509_CSR_WRITE_C */
332