blob: 095364e5e8417cf0f3fdc1abcda1793e689a016f [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 Certificate Signing Request (CSR) parsing
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006 */
7/*
8 * The ITU-T X.509 standard defines a certificate format for PKI.
9 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020010 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
11 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
12 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020013 *
14 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
15 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
16 */
17
Gilles Peskinedb09ef62020-06-03 01:43:33 +020018#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/x509_csr.h"
Janos Follath73c616b2019-12-18 15:07:04 +000023#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050025#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000026
27#include <string.h>
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036#include <stdio.h>
37#endif
38
39/*
40 * Version ::= INTEGER { v1(0) }
41 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010042static int x509_csr_get_version(unsigned char **p,
43 const unsigned char *end,
44 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045{
Janos Follath865b3eb2019-12-16 11:46:15 +000046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
49 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050 *ver = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052 }
53
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055 }
56
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058}
59
60/*
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020061 * Parse a CSR in DER format
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr,
64 const unsigned char *buf, size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065{
Janos Follath865b3eb2019-12-16 11:46:15 +000066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067 size_t len;
68 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 mbedtls_x509_buf sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 memset(&sig_params, 0, sizeof(mbedtls_x509_buf));
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +020072
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073 /*
74 * Check for valid input
75 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 if (csr == NULL || buf == NULL || buflen == 0) {
77 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
78 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 mbedtls_x509_csr_init(csr);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020082 /*
83 * first copy the raw DER data
84 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 p = mbedtls_calloc(1, len = buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 if (p == NULL) {
88 return MBEDTLS_ERR_X509_ALLOC_FAILED;
89 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 memcpy(p, buf, buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092
93 csr->raw.p = p;
94 csr->raw.len = len;
95 end = p + len;
96
97 /*
98 * CertificationRequest ::= SEQUENCE {
99 * certificationRequestInfo CertificationRequestInfo,
100 * signatureAlgorithm AlgorithmIdentifier,
101 * signature BIT STRING
102 * }
103 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
105 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
106 mbedtls_x509_csr_free(csr);
107 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108 }
109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 if (len != (size_t) (end - p)) {
111 mbedtls_x509_csr_free(csr);
112 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
113 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200114 }
115
116 /*
117 * CertificationRequestInfo ::= SEQUENCE {
118 */
119 csr->cri.p = p;
120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
123 mbedtls_x509_csr_free(csr);
124 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125 }
126
127 end = p + len;
128 csr->cri.len = end - csr->cri.p;
129
130 /*
131 * Version ::= INTEGER { v1(0) }
132 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 if ((ret = x509_csr_get_version(&p, end, &csr->version)) != 0) {
134 mbedtls_x509_csr_free(csr);
135 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200136 }
137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 if (csr->version != 0) {
139 mbedtls_x509_csr_free(csr);
140 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200141 }
142
Andres AG2e3ddfa2017-02-17 13:54:43 +0000143 csr->version++;
144
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 /*
146 * subject Name
147 */
148 csr->subject_raw.p = p;
149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
151 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
152 mbedtls_x509_csr_free(csr);
153 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200154 }
155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 if ((ret = mbedtls_x509_get_name(&p, p + len, &csr->subject)) != 0) {
157 mbedtls_x509_csr_free(csr);
158 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200159 }
160
161 csr->subject_raw.len = p - csr->subject_raw.p;
162
163 /*
164 * subjectPKInfo SubjectPublicKeyInfo
165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &csr->pk)) != 0) {
167 mbedtls_x509_csr_free(csr);
168 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200169 }
170
171 /*
172 * attributes [0] Attributes
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000173 *
174 * The list of possible attributes is open-ended, though RFC 2985
175 * (PKCS#9) defines a few in section 5.4. We currently don't support any,
176 * so we just ignore them. This is a safe thing to do as the worst thing
177 * that could happen is that we issue a certificate that does not match
178 * the requester's expectations - this cannot cause a violation of our
179 * signature policies.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
182 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
183 0) {
184 mbedtls_x509_csr_free(csr);
185 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200187
188 p += len;
189
190 end = csr->raw.p + csr->raw.len;
191
192 /*
193 * signatureAlgorithm AlgorithmIdentifier,
194 * signature BIT STRING
195 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 if ((ret = mbedtls_x509_get_alg(&p, end, &csr->sig_oid, &sig_params)) != 0) {
197 mbedtls_x509_csr_free(csr);
198 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199 }
200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 if ((ret = mbedtls_x509_get_sig_alg(&csr->sig_oid, &sig_params,
202 &csr->sig_md, &csr->sig_pk,
203 &csr->sig_opts)) != 0) {
204 mbedtls_x509_csr_free(csr);
205 return MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206 }
207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 if ((ret = mbedtls_x509_get_sig(&p, end, &csr->sig)) != 0) {
209 mbedtls_x509_csr_free(csr);
210 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200211 }
212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 if (p != end) {
214 mbedtls_x509_csr_free(csr);
215 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
216 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200217 }
218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200220}
221
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200222/*
223 * Parse a CSR, allowing for PEM or raw DER encoding
224 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen)
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200226{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#if defined(MBEDTLS_PEM_PARSE_C)
Janos Follath865b3eb2019-12-16 11:46:15 +0000228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200229 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_pem_context pem;
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200231#endif
232
233 /*
234 * Check for valid input
235 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 if (csr == NULL || buf == NULL || buflen == 0) {
237 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
238 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200241 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 if (buf[buflen - 1] == '\0') {
243 mbedtls_pem_init(&pem);
244 ret = mbedtls_pem_read_buffer(&pem,
245 "-----BEGIN CERTIFICATE REQUEST-----",
246 "-----END CERTIFICATE REQUEST-----",
247 buf, NULL, 0, &use_len);
248 if (ret == MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
249 ret = mbedtls_pem_read_buffer(&pem,
250 "-----BEGIN NEW CERTIFICATE REQUEST-----",
251 "-----END NEW CERTIFICATE REQUEST-----",
252 buf, NULL, 0, &use_len);
Simon Butcher0488ce62018-09-30 15:36:50 +0100253 }
Simon Butchere1660af2018-10-07 17:48:37 +0100254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 if (ret == 0) {
Philippe Antoinec03059d2018-06-14 07:35:11 +0200256 /*
257 * Was PEM encoded, parse the result
258 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 ret = mbedtls_x509_csr_parse_der(csr, pem.buf, pem.buflen);
Simon Butcher0488ce62018-09-30 15:36:50 +0100260 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 mbedtls_pem_free(&pem);
263 if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
264 return ret;
265 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200266 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#endif /* MBEDTLS_PEM_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 return mbedtls_x509_csr_parse_der(csr, buf, buflen);
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200269}
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200272/*
273 * Load a CSR into the structure
274 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200276{
Janos Follath865b3eb2019-12-16 11:46:15 +0000277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278 size_t n;
279 unsigned char *buf;
280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
282 return ret;
283 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 ret = mbedtls_x509_csr_parse(csr, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 mbedtls_platform_zeroize(buf, n);
288 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200294#define BEFORE_COLON 14
295#define BC "14"
296/*
297 * Return an informational string about the CSR.
298 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix,
300 const mbedtls_x509_csr *csr)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301{
Janos Follath865b3eb2019-12-16 11:46:15 +0000302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200303 size_t n;
304 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305 char key_size_str[BEFORE_COLON];
306
307 p = buf;
308 n = size;
309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 ret = mbedtls_snprintf(p, n, "%sCSR version : %d",
311 prefix, csr->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200312 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200315 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 ret = mbedtls_x509_dn_gets(p, n, &csr->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200317 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200320 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 ret = mbedtls_x509_sig_alg_gets(p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
323 csr->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200324 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
327 mbedtls_pk_get_name(&csr->pk))) != 0) {
328 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 }
330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
332 (int) mbedtls_pk_get_bitlen(&csr->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200333 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336}
337
338/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200339 * Initialize a CSR
340 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341void mbedtls_x509_csr_init(mbedtls_x509_csr *csr)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200342{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 memset(csr, 0, sizeof(mbedtls_x509_csr));
Paul Bakker369d2eb2013-09-18 11:58:25 +0200344}
345
346/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347 * Unallocate all CSR data
348 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349void mbedtls_x509_csr_free(mbedtls_x509_csr *csr)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200350{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_x509_name *name_cur;
352 mbedtls_x509_name *name_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 if (csr == NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 mbedtls_pk_free(&csr->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 mbedtls_free(csr->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200362#endif
363
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 name_cur = csr->subject.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 while (name_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366 name_prv = name_cur;
367 name_cur = name_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
369 mbedtls_free(name_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370 }
371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 if (csr->raw.p != NULL) {
373 mbedtls_platform_zeroize(csr->raw.p, csr->raw.len);
374 mbedtls_free(csr->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375 }
376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 mbedtls_platform_zeroize(csr, sizeof(mbedtls_x509_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378}
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#endif /* MBEDTLS_X509_CSR_PARSE_C */