blob: bd45c56657bfb6713229b40d6812399524b302a0 [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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +010063 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064 }
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067 }
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070}
71
72/*
Jens Alfke2d9e3592019-10-29 15:03:37 -070073 * Parse CSR extension requests in DER format
74 */
75static int x509_csr_parse_extensions(mbedtls_x509_csr *csr,
76 unsigned char **p, const unsigned char *end)
77{
78 int ret;
79 size_t len;
80 unsigned char *end_ext_data;
Matthias Schulzadb3cc42023-10-17 11:50:50 +020081 int critical;
Jens Alfke2d9e3592019-10-29 15:03:37 -070082 while (*p < end) {
83 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
84 int ext_type = 0;
85
86 /* Read sequence tag */
87 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
88 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +010089 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -070090 }
91
92 end_ext_data = *p + len;
93
94 /* Get extension ID */
95 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
96 MBEDTLS_ASN1_OID)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +010097 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -070098 }
99
100 extn_oid.tag = MBEDTLS_ASN1_OID;
101 extn_oid.p = *p;
102 *p += extn_oid.len;
103
Matthias Schulzadb3cc42023-10-17 11:50:50 +0200104 /* Get and ignore optional critical flag */
105 (void)mbedtls_asn1_get_bool(p, end_ext_data, &critical);
106
Jens Alfke2d9e3592019-10-29 15:03:37 -0700107 /* Data should be octet string type */
108 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
109 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100110 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700111 }
Przemek Stekiel86d19462023-01-24 09:45:19 +0100112
Jens Alfke2d9e3592019-10-29 15:03:37 -0700113 if (*p + len != end_ext_data) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100114 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
115 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700116 }
117
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100118 /*
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100119 * Detect supported extensions and skip unsupported extensions
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100120 */
121 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
122
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100123 if (ret == 0) {
124 /* Forbid repeated extensions */
125 if ((csr->ext_types & ext_type) != 0) {
Przemek Stekiel36ad5e72023-01-26 22:30:45 +0100126 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
127 MBEDTLS_ERR_ASN1_INVALID_DATA);
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100128 }
129
130 csr->ext_types |= ext_type;
131
132 switch (ext_type) {
133 case MBEDTLS_X509_EXT_KEY_USAGE:
134 /* Parse key usage */
135 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_data,
Przemek Stekiel36ad5e72023-01-26 22:30:45 +0100136 &csr->key_usage)) != 0) {
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100137 return ret;
138 }
139 break;
140
141 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
142 /* Parse subject alt name */
143 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_data,
Przemek Stekiel36ad5e72023-01-26 22:30:45 +0100144 &csr->subject_alt_names)) != 0) {
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100145 return ret;
146 }
147 break;
148
149 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
150 /* Parse netscape certificate type */
151 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_data,
Przemek Stekiel36ad5e72023-01-26 22:30:45 +0100152 &csr->ns_cert_type)) != 0) {
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100153 return ret;
154 }
155 break;
156 default:
157 break;
158 }
Przemek Stekiel86d19462023-01-24 09:45:19 +0100159 }
Jens Alfke2d9e3592019-10-29 15:03:37 -0700160 *p = end_ext_data;
161 }
162
163 if (*p != end) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100164 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
165 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700166 }
167
168 return 0;
169}
170
171/*
172 * Parse CSR attributes in DER format
173 */
174static int x509_csr_parse_attributes(mbedtls_x509_csr *csr,
175 const unsigned char *start, const unsigned char *end)
176{
177 int ret;
178 size_t len;
179 unsigned char *end_attr_data;
180 unsigned char **p = (unsigned char **) &start;
181
182 while (*p < end) {
183 mbedtls_x509_buf attr_oid = { 0, 0, NULL };
184
185 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
186 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100187 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700188 }
189 end_attr_data = *p + len;
190
191 /* Get attribute ID */
192 if ((ret = mbedtls_asn1_get_tag(p, end_attr_data, &attr_oid.len,
193 MBEDTLS_ASN1_OID)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100194 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700195 }
196
197 attr_oid.tag = MBEDTLS_ASN1_OID;
198 attr_oid.p = *p;
199 *p += attr_oid.len;
200
201 /* Check that this is an extension-request attribute */
202 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS9_CSR_EXT_REQ, &attr_oid) == 0) {
203 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
204 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100205 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700206 }
207
208 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
209 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
210 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100211 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700212 }
213
214 if ((ret = x509_csr_parse_extensions(csr, p, *p + len)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100215 return ret;
Jens Alfke2d9e3592019-10-29 15:03:37 -0700216 }
217
218 if (*p != end_attr_data) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100219 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
220 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700221 }
222 }
223
224 *p = end_attr_data;
225 }
226
227 if (*p != end) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100228 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
Przemek Stekiel36ad5e72023-01-26 22:30:45 +0100229 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700230 }
231
232 return 0;
233}
234
235/*
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200236 * Parse a CSR in DER format
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200237 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr,
239 const unsigned char *buf, size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240{
Janos Follath865b3eb2019-12-16 11:46:15 +0000241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242 size_t len;
243 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_x509_buf sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 memset(&sig_params, 0, sizeof(mbedtls_x509_buf));
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200247
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248 /*
249 * Check for valid input
250 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (csr == NULL || buf == NULL || buflen == 0) {
252 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
253 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 mbedtls_x509_csr_init(csr);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200257 /*
258 * first copy the raw DER data
259 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 p = mbedtls_calloc(1, len = buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (p == NULL) {
263 return MBEDTLS_ERR_X509_ALLOC_FAILED;
264 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 memcpy(p, buf, buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200267
268 csr->raw.p = p;
269 csr->raw.len = len;
270 end = p + len;
271
272 /*
273 * CertificationRequest ::= SEQUENCE {
274 * certificationRequestInfo CertificationRequestInfo,
275 * signatureAlgorithm AlgorithmIdentifier,
276 * signature BIT STRING
277 * }
278 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
280 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
281 mbedtls_x509_csr_free(csr);
282 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283 }
284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (len != (size_t) (end - p)) {
286 mbedtls_x509_csr_free(csr);
287 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
288 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289 }
290
291 /*
292 * CertificationRequestInfo ::= SEQUENCE {
293 */
294 csr->cri.p = p;
295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
297 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
298 mbedtls_x509_csr_free(csr);
299 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 }
301
302 end = p + len;
303 csr->cri.len = end - csr->cri.p;
304
305 /*
306 * Version ::= INTEGER { v1(0) }
307 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 if ((ret = x509_csr_get_version(&p, end, &csr->version)) != 0) {
309 mbedtls_x509_csr_free(csr);
310 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311 }
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if (csr->version != 0) {
314 mbedtls_x509_csr_free(csr);
315 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200316 }
317
Andres AG2e3ddfa2017-02-17 13:54:43 +0000318 csr->version++;
319
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320 /*
321 * subject Name
322 */
323 csr->subject_raw.p = p;
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
326 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
327 mbedtls_x509_csr_free(csr);
328 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 }
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if ((ret = mbedtls_x509_get_name(&p, p + len, &csr->subject)) != 0) {
332 mbedtls_x509_csr_free(csr);
333 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200334 }
335
336 csr->subject_raw.len = p - csr->subject_raw.p;
337
338 /*
339 * subjectPKInfo SubjectPublicKeyInfo
340 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &csr->pk)) != 0) {
342 mbedtls_x509_csr_free(csr);
343 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344 }
345
346 /*
347 * attributes [0] Attributes
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000348 *
349 * The list of possible attributes is open-ended, though RFC 2985
350 * (PKCS#9) defines a few in section 5.4. We currently don't support any,
351 * so we just ignore them. This is a safe thing to do as the worst thing
352 * that could happen is that we issue a certificate that does not match
353 * the requester's expectations - this cannot cause a violation of our
354 * signature policies.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
357 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
358 0) {
359 mbedtls_x509_csr_free(csr);
360 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362
Jens Alfke2d9e3592019-10-29 15:03:37 -0700363 if ((ret = x509_csr_parse_attributes(csr, p, p + len)) != 0) {
364 mbedtls_x509_csr_free(csr);
365 return ret;
366 }
367
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368 p += len;
369
370 end = csr->raw.p + csr->raw.len;
371
372 /*
373 * signatureAlgorithm AlgorithmIdentifier,
374 * signature BIT STRING
375 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if ((ret = mbedtls_x509_get_alg(&p, end, &csr->sig_oid, &sig_params)) != 0) {
377 mbedtls_x509_csr_free(csr);
378 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 }
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if ((ret = mbedtls_x509_get_sig_alg(&csr->sig_oid, &sig_params,
382 &csr->sig_md, &csr->sig_pk,
383 &csr->sig_opts)) != 0) {
384 mbedtls_x509_csr_free(csr);
385 return MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386 }
387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if ((ret = mbedtls_x509_get_sig(&p, end, &csr->sig)) != 0) {
389 mbedtls_x509_csr_free(csr);
390 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 }
392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (p != end) {
394 mbedtls_x509_csr_free(csr);
395 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
396 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397 }
398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400}
401
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200402/*
403 * Parse a CSR, allowing for PEM or raw DER encoding
404 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen)
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200406{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_PEM_PARSE_C)
Janos Follath865b3eb2019-12-16 11:46:15 +0000408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200409 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_pem_context pem;
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200411#endif
412
413 /*
414 * Check for valid input
415 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (csr == NULL || buf == NULL || buflen == 0) {
417 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
418 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200421 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if (buf[buflen - 1] == '\0') {
423 mbedtls_pem_init(&pem);
424 ret = mbedtls_pem_read_buffer(&pem,
425 "-----BEGIN CERTIFICATE REQUEST-----",
426 "-----END CERTIFICATE REQUEST-----",
427 buf, NULL, 0, &use_len);
428 if (ret == MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
429 ret = mbedtls_pem_read_buffer(&pem,
430 "-----BEGIN NEW CERTIFICATE REQUEST-----",
431 "-----END NEW CERTIFICATE REQUEST-----",
432 buf, NULL, 0, &use_len);
Simon Butcher0488ce62018-09-30 15:36:50 +0100433 }
Simon Butchere1660af2018-10-07 17:48:37 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (ret == 0) {
Philippe Antoinec03059d2018-06-14 07:35:11 +0200436 /*
437 * Was PEM encoded, parse the result
438 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 ret = mbedtls_x509_csr_parse_der(csr, pem.buf, pem.buflen);
Simon Butcher0488ce62018-09-30 15:36:50 +0100440 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 mbedtls_pem_free(&pem);
443 if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
444 return ret;
445 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200446 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#endif /* MBEDTLS_PEM_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return mbedtls_x509_csr_parse_der(csr, buf, buflen);
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200449}
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452/*
453 * Load a CSR into the structure
454 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100455int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456{
Janos Follath865b3eb2019-12-16 11:46:15 +0000457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458 size_t n;
459 unsigned char *buf;
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
462 return ret;
463 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 ret = mbedtls_x509_csr_parse(csr, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100467 mbedtls_zeroize_and_free(buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
Hanno Becker612a2f12020-10-09 09:19:39 +0100473#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474#define BEFORE_COLON 14
475#define BC "14"
476/*
477 * Return an informational string about the CSR.
478 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix,
480 const mbedtls_x509_csr *csr)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481{
Janos Follath865b3eb2019-12-16 11:46:15 +0000482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483 size_t n;
484 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 char key_size_str[BEFORE_COLON];
486
487 p = buf;
488 n = size;
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 ret = mbedtls_snprintf(p, n, "%sCSR version : %d",
491 prefix, csr->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200492 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200495 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 ret = mbedtls_x509_dn_gets(p, n, &csr->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200497 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200500 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 ret = mbedtls_x509_sig_alg_gets(p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
503 csr->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200504 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
507 mbedtls_pk_get_name(&csr->pk))) != 0) {
508 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509 }
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
512 (int) mbedtls_pk_get_bitlen(&csr->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200513 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100515 /*
516 * Optional extensions
517 */
518
519 if (csr->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
520 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
521 MBEDTLS_X509_SAFE_SNPRINTF;
522
Przemek Stekiel21c37282023-01-16 08:47:49 +0100523 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
524 &csr->subject_alt_names,
525 prefix)) != 0) {
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100526 return ret;
527 }
528 }
529
530 if (csr->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
531 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
532 MBEDTLS_X509_SAFE_SNPRINTF;
533
Przemek Stekiel21c37282023-01-16 08:47:49 +0100534 if ((ret = mbedtls_x509_info_cert_type(&p, &n, csr->ns_cert_type)) != 0) {
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100535 return ret;
536 }
537 }
538
539 if (csr->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
540 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
541 MBEDTLS_X509_SAFE_SNPRINTF;
542
Przemek Stekiel21c37282023-01-16 08:47:49 +0100543 if ((ret = mbedtls_x509_info_key_usage(&p, &n, csr->key_usage)) != 0) {
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100544 return ret;
545 }
546 }
547
548 if (csr->ext_types != 0) {
549 ret = mbedtls_snprintf(p, n, "\n");
550 MBEDTLS_X509_SAFE_SNPRINTF;
551 }
552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554}
Hanno Becker612a2f12020-10-09 09:19:39 +0100555#endif /* MBEDTLS_X509_REMOVE_INFO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556
557/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200558 * Initialize a CSR
559 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100560void mbedtls_x509_csr_init(mbedtls_x509_csr *csr)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200561{
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 memset(csr, 0, sizeof(mbedtls_x509_csr));
Paul Bakker369d2eb2013-09-18 11:58:25 +0200563}
564
565/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566 * Unallocate all CSR data
567 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100568void mbedtls_x509_csr_free(mbedtls_x509_csr *csr)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569{
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (csr == NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571 return;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572 }
573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 mbedtls_pk_free(&csr->pk);
575
576#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
577 mbedtls_free(csr->sig_opts);
578#endif
579
580 mbedtls_asn1_free_named_data_list_shallow(csr->subject.next);
Przemek Stekiela4687682023-01-24 15:19:47 +0100581 mbedtls_asn1_sequence_free(csr->subject_alt_names.next);
Gilles Peskine449bd832023-01-11 14:50:10 +0100582
583 if (csr->raw.p != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100584 mbedtls_zeroize_and_free(csr->raw.p, csr->raw.len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 }
586
587 mbedtls_platform_zeroize(csr, sizeof(mbedtls_x509_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588}
589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#endif /* MBEDTLS_X509_CSR_PARSE_C */