blob: 89344d183bda1cfd14f8b5a8d66f7db08b504621 [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
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 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
28 */
29
Gilles Peskinedb09ef62020-06-03 01:43:33 +020030#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/x509_csr.h"
Janos Follath73c616b2019-12-18 15:07:04 +000035#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050037#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000038
39#include <string.h>
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020043#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020048#include <stdio.h>
49#endif
50
51/*
52 * Version ::= INTEGER { v1(0) }
53 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054static int x509_csr_get_version(unsigned char **p,
55 const unsigned char *end,
56 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057{
Janos Follath865b3eb2019-12-16 11:46:15 +000058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
61 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062 *ver = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064 }
65
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067 }
68
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070}
71
72/*
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020073 * Parse a CSR in DER format
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr,
76 const unsigned char *buf, size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077{
Janos Follath865b3eb2019-12-16 11:46:15 +000078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079 size_t len;
80 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_x509_buf sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 memset(&sig_params, 0, sizeof(mbedtls_x509_buf));
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +020084
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085 /*
86 * Check for valid input
87 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 if (csr == NULL || buf == NULL || buflen == 0) {
89 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
90 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 mbedtls_x509_csr_init(csr);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020094 /*
95 * first copy the raw DER data
96 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 p = mbedtls_calloc(1, len = buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 if (p == NULL) {
100 return MBEDTLS_ERR_X509_ALLOC_FAILED;
101 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 memcpy(p, buf, buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104
105 csr->raw.p = p;
106 csr->raw.len = len;
107 end = p + len;
108
109 /*
110 * CertificationRequest ::= SEQUENCE {
111 * certificationRequestInfo CertificationRequestInfo,
112 * signatureAlgorithm AlgorithmIdentifier,
113 * signature BIT STRING
114 * }
115 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
117 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
118 mbedtls_x509_csr_free(csr);
119 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120 }
121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 if (len != (size_t) (end - p)) {
123 mbedtls_x509_csr_free(csr);
124 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
125 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200126 }
127
128 /*
129 * CertificationRequestInfo ::= SEQUENCE {
130 */
131 csr->cri.p = p;
132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
134 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
135 mbedtls_x509_csr_free(csr);
136 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200137 }
138
139 end = p + len;
140 csr->cri.len = end - csr->cri.p;
141
142 /*
143 * Version ::= INTEGER { v1(0) }
144 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 if ((ret = x509_csr_get_version(&p, end, &csr->version)) != 0) {
146 mbedtls_x509_csr_free(csr);
147 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200148 }
149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 if (csr->version != 0) {
151 mbedtls_x509_csr_free(csr);
152 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153 }
154
Andres AG2e3ddfa2017-02-17 13:54:43 +0000155 csr->version++;
156
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200157 /*
158 * subject Name
159 */
160 csr->subject_raw.p = p;
161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
163 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
164 mbedtls_x509_csr_free(csr);
165 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200166 }
167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 if ((ret = mbedtls_x509_get_name(&p, p + len, &csr->subject)) != 0) {
169 mbedtls_x509_csr_free(csr);
170 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200171 }
172
173 csr->subject_raw.len = p - csr->subject_raw.p;
174
175 /*
176 * subjectPKInfo SubjectPublicKeyInfo
177 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &csr->pk)) != 0) {
179 mbedtls_x509_csr_free(csr);
180 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200181 }
182
183 /*
184 * attributes [0] Attributes
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000185 *
186 * The list of possible attributes is open-ended, though RFC 2985
187 * (PKCS#9) defines a few in section 5.4. We currently don't support any,
188 * so we just ignore them. This is a safe thing to do as the worst thing
189 * that could happen is that we issue a certificate that does not match
190 * the requester's expectations - this cannot cause a violation of our
191 * signature policies.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200192 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
194 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
195 0) {
196 mbedtls_x509_csr_free(csr);
197 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200198 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199
200 p += len;
201
202 end = csr->raw.p + csr->raw.len;
203
204 /*
205 * signatureAlgorithm AlgorithmIdentifier,
206 * signature BIT STRING
207 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 if ((ret = mbedtls_x509_get_alg(&p, end, &csr->sig_oid, &sig_params)) != 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 ((ret = mbedtls_x509_get_sig_alg(&csr->sig_oid, &sig_params,
214 &csr->sig_md, &csr->sig_pk,
215 &csr->sig_opts)) != 0) {
216 mbedtls_x509_csr_free(csr);
217 return MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200218 }
219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 if ((ret = mbedtls_x509_get_sig(&p, end, &csr->sig)) != 0) {
221 mbedtls_x509_csr_free(csr);
222 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200223 }
224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 if (p != end) {
226 mbedtls_x509_csr_free(csr);
227 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
228 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229 }
230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200232}
233
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200234/*
235 * Parse a CSR, allowing for PEM or raw DER encoding
236 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen)
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_PEM_PARSE_C)
Janos Follath865b3eb2019-12-16 11:46:15 +0000240 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200241 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_pem_context pem;
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200243#endif
244
245 /*
246 * Check for valid input
247 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 if (csr == NULL || buf == NULL || buflen == 0) {
249 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
250 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200253 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 if (buf[buflen - 1] == '\0') {
255 mbedtls_pem_init(&pem);
256 ret = mbedtls_pem_read_buffer(&pem,
257 "-----BEGIN CERTIFICATE REQUEST-----",
258 "-----END CERTIFICATE REQUEST-----",
259 buf, NULL, 0, &use_len);
260 if (ret == MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
261 ret = mbedtls_pem_read_buffer(&pem,
262 "-----BEGIN NEW CERTIFICATE REQUEST-----",
263 "-----END NEW CERTIFICATE REQUEST-----",
264 buf, NULL, 0, &use_len);
Simon Butcher0488ce62018-09-30 15:36:50 +0100265 }
Simon Butchere1660af2018-10-07 17:48:37 +0100266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 if (ret == 0) {
Philippe Antoinec03059d2018-06-14 07:35:11 +0200268 /*
269 * Was PEM encoded, parse the result
270 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 ret = mbedtls_x509_csr_parse_der(csr, pem.buf, pem.buflen);
Simon Butcher0488ce62018-09-30 15:36:50 +0100272 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 mbedtls_pem_free(&pem);
275 if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
276 return ret;
277 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200278 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#endif /* MBEDTLS_PEM_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 return mbedtls_x509_csr_parse_der(csr, buf, buflen);
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200281}
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284/*
285 * Load a CSR into the structure
286 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288{
Janos Follath865b3eb2019-12-16 11:46:15 +0000289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290 size_t n;
291 unsigned char *buf;
292
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
294 return ret;
295 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 ret = mbedtls_x509_csr_parse(csr, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 mbedtls_platform_zeroize(buf, n);
300 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200303}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200306#define BEFORE_COLON 14
307#define BC "14"
308/*
309 * Return an informational string about the CSR.
310 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix,
312 const mbedtls_x509_csr *csr)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313{
Janos Follath865b3eb2019-12-16 11:46:15 +0000314 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200315 size_t n;
316 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317 char key_size_str[BEFORE_COLON];
318
319 p = buf;
320 n = size;
321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 ret = mbedtls_snprintf(p, n, "%sCSR version : %d",
323 prefix, csr->version);
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 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200327 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 ret = mbedtls_x509_dn_gets(p, n, &csr->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200329 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200332 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 ret = mbedtls_x509_sig_alg_gets(p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
335 csr->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200336 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
339 mbedtls_pk_get_name(&csr->pk))) != 0) {
340 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341 }
342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
344 (int) mbedtls_pk_get_bitlen(&csr->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200345 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348}
349
350/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200351 * Initialize a CSR
352 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353void mbedtls_x509_csr_init(mbedtls_x509_csr *csr)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200354{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 memset(csr, 0, sizeof(mbedtls_x509_csr));
Paul Bakker369d2eb2013-09-18 11:58:25 +0200356}
357
358/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359 * Unallocate all CSR data
360 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361void mbedtls_x509_csr_free(mbedtls_x509_csr *csr)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_x509_name *name_cur;
364 mbedtls_x509_name *name_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 if (csr == NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 mbedtls_pk_free(&csr->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 mbedtls_free(csr->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200374#endif
375
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376 name_cur = csr->subject.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 while (name_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378 name_prv = name_cur;
379 name_cur = name_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
381 mbedtls_free(name_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382 }
383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 if (csr->raw.p != NULL) {
385 mbedtls_platform_zeroize(csr->raw.p, csr->raw.len);
386 mbedtls_free(csr->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387 }
388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 mbedtls_platform_zeroize(csr, sizeof(mbedtls_x509_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390}
391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#endif /* MBEDTLS_X509_CSR_PARSE_C */