blob: 2e7d97251d3421fd6605451bfef5de434caf91d0 [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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020034# include "mbedtls/x509_csr.h"
35# include "mbedtls/error.h"
36# include "mbedtls/oid.h"
37# include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000038
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020039# include <string.h>
Rich Evans00ab4702015-02-06 13:43:58 +000040
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020041# if defined(MBEDTLS_PEM_PARSE_C)
42# include "mbedtls/pem.h"
43# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020045# if defined(MBEDTLS_PLATFORM_C)
46# include "mbedtls/platform.h"
47# else
48# include <stdlib.h>
49# include <stdio.h>
50# define mbedtls_free free
51# define mbedtls_calloc calloc
52# define mbedtls_snprintf snprintf
53# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020055# if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
56# include <stdio.h>
57# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058
59/*
60 * Version ::= INTEGER { v1(0) }
61 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020062static int
63x509_csr_get_version(unsigned char **p, const unsigned char *end, int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064{
Janos Follath865b3eb2019-12-16 11:46:15 +000065 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020067 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
68 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069 *ver = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071 }
72
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020073 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074 }
75
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020076 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077}
78
79/*
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020080 * Parse a CSR in DER format
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020082int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr,
83 const unsigned char *buf,
84 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085{
Janos Follath865b3eb2019-12-16 11:46:15 +000086 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087 size_t len;
88 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_x509_buf sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020091 memset(&sig_params, 0, sizeof(mbedtls_x509_buf));
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +020092
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093 /*
94 * Check for valid input
95 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020096 if (csr == NULL || buf == NULL || buflen == 0)
97 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020099 mbedtls_x509_csr_init(csr);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200101 /*
102 * first copy the raw DER data
103 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200104 p = mbedtls_calloc(1, len = buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200106 if (p == NULL)
107 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200109 memcpy(p, buf, buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110
111 csr->raw.p = p;
112 csr->raw.len = len;
113 end = p + len;
114
115 /*
116 * CertificationRequest ::= SEQUENCE {
117 * certificationRequestInfo CertificationRequestInfo,
118 * signatureAlgorithm AlgorithmIdentifier,
119 * signature BIT STRING
120 * }
121 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200122 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
123 MBEDTLS_ASN1_CONSTRUCTED |
124 MBEDTLS_ASN1_SEQUENCE)) != 0) {
125 mbedtls_x509_csr_free(csr);
126 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127 }
128
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200129 if (len != (size_t)(end - p)) {
130 mbedtls_x509_csr_free(csr);
131 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
132 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200133 }
134
135 /*
136 * CertificationRequestInfo ::= SEQUENCE {
137 */
138 csr->cri.p = p;
139
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200140 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
141 MBEDTLS_ASN1_CONSTRUCTED |
142 MBEDTLS_ASN1_SEQUENCE)) != 0) {
143 mbedtls_x509_csr_free(csr);
144 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 }
146
147 end = p + len;
148 csr->cri.len = end - csr->cri.p;
149
150 /*
151 * Version ::= INTEGER { v1(0) }
152 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200153 if ((ret = x509_csr_get_version(&p, end, &csr->version)) != 0) {
154 mbedtls_x509_csr_free(csr);
155 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200156 }
157
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200158 if (csr->version != 0) {
159 mbedtls_x509_csr_free(csr);
160 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200161 }
162
Andres AG2e3ddfa2017-02-17 13:54:43 +0000163 csr->version++;
164
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165 /*
166 * subject Name
167 */
168 csr->subject_raw.p = p;
169
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200170 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
171 MBEDTLS_ASN1_CONSTRUCTED |
172 MBEDTLS_ASN1_SEQUENCE)) != 0) {
173 mbedtls_x509_csr_free(csr);
174 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200175 }
176
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200177 if ((ret = mbedtls_x509_get_name(&p, p + len, &csr->subject)) != 0) {
178 mbedtls_x509_csr_free(csr);
179 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180 }
181
182 csr->subject_raw.len = p - csr->subject_raw.p;
183
184 /*
185 * subjectPKInfo SubjectPublicKeyInfo
186 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200187 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &csr->pk)) != 0) {
188 mbedtls_x509_csr_free(csr);
189 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200190 }
191
192 /*
193 * attributes [0] Attributes
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000194 *
195 * The list of possible attributes is open-ended, though RFC 2985
196 * (PKCS#9) defines a few in section 5.4. We currently don't support any,
197 * so we just ignore them. This is a safe thing to do as the worst thing
198 * that could happen is that we issue a certificate that does not match
199 * the requester's expectations - this cannot cause a violation of our
200 * signature policies.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200201 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200202 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
203 MBEDTLS_ASN1_CONSTRUCTED |
204 MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != 0) {
205 mbedtls_x509_csr_free(csr);
206 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200207 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200208
209 p += len;
210
211 end = csr->raw.p + csr->raw.len;
212
213 /*
214 * signatureAlgorithm AlgorithmIdentifier,
215 * signature BIT STRING
216 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200217 if ((ret = mbedtls_x509_get_alg(&p, end, &csr->sig_oid, &sig_params)) !=
218 0) {
219 mbedtls_x509_csr_free(csr);
220 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221 }
222
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200223 if ((ret = mbedtls_x509_get_sig_alg(&csr->sig_oid, &sig_params,
224 &csr->sig_md, &csr->sig_pk,
225 &csr->sig_opts)) != 0) {
226 mbedtls_x509_csr_free(csr);
227 return MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200228 }
229
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200230 if ((ret = mbedtls_x509_get_sig(&p, end, &csr->sig)) != 0) {
231 mbedtls_x509_csr_free(csr);
232 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200233 }
234
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200235 if (p != end) {
236 mbedtls_x509_csr_free(csr);
237 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
238 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200239 }
240
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200241 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242}
243
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200244/*
245 * Parse a CSR, allowing for PEM or raw DER encoding
246 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200247int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr,
248 const unsigned char *buf,
249 size_t buflen)
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200250{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200251# if defined(MBEDTLS_PEM_PARSE_C)
Janos Follath865b3eb2019-12-16 11:46:15 +0000252 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200253 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_pem_context pem;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200255# endif
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200256
257 /*
258 * Check for valid input
259 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200260 if (csr == NULL || buf == NULL || buflen == 0)
261 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200262
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200263# if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200264 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200265 if (buf[buflen - 1] == '\0') {
266 mbedtls_pem_init(&pem);
267 ret = mbedtls_pem_read_buffer(&pem,
268 "-----BEGIN CERTIFICATE REQUEST-----",
269 "-----END CERTIFICATE REQUEST-----", buf,
270 NULL, 0, &use_len);
271 if (ret == MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
272 ret = mbedtls_pem_read_buffer(
273 &pem, "-----BEGIN NEW CERTIFICATE REQUEST-----",
274 "-----END NEW CERTIFICATE REQUEST-----", buf, NULL, 0,
275 &use_len);
Simon Butcher0488ce62018-09-30 15:36:50 +0100276 }
Simon Butchere1660af2018-10-07 17:48:37 +0100277
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200278 if (ret == 0) {
Philippe Antoinec03059d2018-06-14 07:35:11 +0200279 /*
280 * Was PEM encoded, parse the result
281 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200282 ret = mbedtls_x509_csr_parse_der(csr, pem.buf, pem.buflen);
Simon Butcher0488ce62018-09-30 15:36:50 +0100283 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200284
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200285 mbedtls_pem_free(&pem);
286 if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT)
287 return ret;
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200288 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200289# endif /* MBEDTLS_PEM_PARSE_C */
290 return mbedtls_x509_csr_parse_der(csr, buf, buflen);
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200291}
292
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200293# if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200294/*
295 * Load a CSR into the structure
296 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200297int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298{
Janos Follath865b3eb2019-12-16 11:46:15 +0000299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 size_t n;
301 unsigned char *buf;
302
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200303 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0)
304 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200306 ret = mbedtls_x509_csr_parse(csr, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200308 mbedtls_platform_zeroize(buf, n);
309 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200310
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200311 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200312}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200313# endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200314
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200315# if !defined(MBEDTLS_X509_REMOVE_INFO)
316# define BEFORE_COLON 14
317# define BC "14"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318/*
319 * Return an informational string about the CSR.
320 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200321int mbedtls_x509_csr_info(char *buf,
322 size_t size,
323 const char *prefix,
324 const mbedtls_x509_csr *csr)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325{
Janos Follath865b3eb2019-12-16 11:46:15 +0000326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327 size_t n;
328 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 char key_size_str[BEFORE_COLON];
330
331 p = buf;
332 n = size;
333
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200334 ret = mbedtls_snprintf(p, n, "%sCSR version : %d", prefix, csr->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200335 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200337 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200338 MBEDTLS_X509_SAFE_SNPRINTF;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200339 ret = mbedtls_x509_dn_gets(p, n, &csr->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200340 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200342 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200343 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200345 ret = mbedtls_x509_sig_alg_gets(p, n, &csr->sig_oid, csr->sig_pk,
346 csr->sig_md, csr->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200347 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200349 if ((ret = mbedtls_x509_key_size_helper(
350 key_size_str, BEFORE_COLON, mbedtls_pk_get_name(&csr->pk))) != 0) {
351 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352 }
353
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200354 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits\n", prefix,
355 key_size_str, (int)mbedtls_pk_get_bitlen(&csr->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200356 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200358 return ((int)(size - n));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200360# endif /* MBEDTLS_X509_REMOVE_INFO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361
362/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200363 * Initialize a CSR
364 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200365void mbedtls_x509_csr_init(mbedtls_x509_csr *csr)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200366{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200367 memset(csr, 0, sizeof(mbedtls_x509_csr));
Paul Bakker369d2eb2013-09-18 11:58:25 +0200368}
369
370/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200371 * Unallocate all CSR data
372 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200373void mbedtls_x509_csr_free(mbedtls_x509_csr *csr)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_x509_name *name_cur;
376 mbedtls_x509_name *name_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200378 if (csr == NULL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 return;
380
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200381 mbedtls_pk_free(&csr->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200383# if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
384 mbedtls_free(csr->sig_opts);
385# endif
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200386
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387 name_cur = csr->subject.next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200388 while (name_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389 name_prv = name_cur;
390 name_cur = name_cur->next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200391 mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
392 mbedtls_free(name_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393 }
394
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200395 if (csr->raw.p != NULL) {
396 mbedtls_platform_zeroize(csr->raw.p, csr->raw.len);
397 mbedtls_free(csr->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398 }
399
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200400 mbedtls_platform_zeroize(csr, sizeof(mbedtls_x509_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401}
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#endif /* MBEDTLS_X509_CSR_PARSE_C */