blob: baf260664a34739a79c908a4cefc1f6981381b0b [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{
Matthias Schulz873a2022023-10-17 16:02:20 +020078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Alfke2d9e3592019-10-29 15:03:37 -070079 size_t len;
80 unsigned char *end_ext_data;
Matthias Schulzcc923f32023-10-17 12:36:23 +020081
Jens Alfke2d9e3592019-10-29 15:03:37 -070082 while (*p < end) {
83 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Matthias Schulz873a2022023-10-17 16:02:20 +020084 int is_critical = 0; /* DEFAULT FALSE */
Jens Alfke2d9e3592019-10-29 15:03:37 -070085 int ext_type = 0;
86
87 /* Read sequence tag */
88 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
89 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +010090 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -070091 }
92
93 end_ext_data = *p + len;
94
95 /* Get extension ID */
96 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
97 MBEDTLS_ASN1_OID)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +010098 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -070099 }
100
101 extn_oid.tag = MBEDTLS_ASN1_OID;
102 extn_oid.p = *p;
103 *p += extn_oid.len;
104
Matthias Schulz873a2022023-10-17 16:02:20 +0200105 /* Get optional critical */
106 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
107 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
108 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
109 }
Matthias Schulzadb3cc42023-10-17 11:50:50 +0200110
Jens Alfke2d9e3592019-10-29 15:03:37 -0700111 /* Data should be octet string type */
112 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
113 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100114 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700115 }
Przemek Stekiel86d19462023-01-24 09:45:19 +0100116
Jens Alfke2d9e3592019-10-29 15:03:37 -0700117 if (*p + len != end_ext_data) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100118 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
119 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700120 }
121
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100122 /*
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100123 * Detect supported extensions and skip unsupported extensions
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100124 */
125 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
126
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100127 if (ret == 0) {
128 /* Forbid repeated extensions */
129 if ((csr->ext_types & ext_type) != 0) {
Przemek Stekiel36ad5e72023-01-26 22:30:45 +0100130 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
131 MBEDTLS_ERR_ASN1_INVALID_DATA);
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100132 }
133
134 csr->ext_types |= ext_type;
135
136 switch (ext_type) {
137 case MBEDTLS_X509_EXT_KEY_USAGE:
138 /* Parse key usage */
139 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_data,
Przemek Stekiel36ad5e72023-01-26 22:30:45 +0100140 &csr->key_usage)) != 0) {
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100141 return ret;
142 }
143 break;
144
145 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
146 /* Parse subject alt name */
147 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_data,
Przemek Stekiel36ad5e72023-01-26 22:30:45 +0100148 &csr->subject_alt_names)) != 0) {
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100149 return ret;
150 }
151 break;
152
153 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
154 /* Parse netscape certificate type */
155 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_data,
Przemek Stekiel36ad5e72023-01-26 22:30:45 +0100156 &csr->ns_cert_type)) != 0) {
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100157 return ret;
158 }
159 break;
160 default:
161 break;
162 }
Matthias Schulz873a2022023-10-17 16:02:20 +0200163 } else {
164 if (is_critical) {
165 /* Data is marked as critical: fail */
166 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
167 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
168 }
Przemek Stekiel86d19462023-01-24 09:45:19 +0100169 }
Jens Alfke2d9e3592019-10-29 15:03:37 -0700170 *p = end_ext_data;
171 }
172
173 if (*p != end) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100174 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
175 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700176 }
177
178 return 0;
179}
180
181/*
182 * Parse CSR attributes in DER format
183 */
184static int x509_csr_parse_attributes(mbedtls_x509_csr *csr,
185 const unsigned char *start, const unsigned char *end)
186{
187 int ret;
188 size_t len;
189 unsigned char *end_attr_data;
190 unsigned char **p = (unsigned char **) &start;
191
192 while (*p < end) {
193 mbedtls_x509_buf attr_oid = { 0, 0, NULL };
194
195 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
196 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100197 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700198 }
199 end_attr_data = *p + len;
200
201 /* Get attribute ID */
202 if ((ret = mbedtls_asn1_get_tag(p, end_attr_data, &attr_oid.len,
203 MBEDTLS_ASN1_OID)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100204 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700205 }
206
207 attr_oid.tag = MBEDTLS_ASN1_OID;
208 attr_oid.p = *p;
209 *p += attr_oid.len;
210
211 /* Check that this is an extension-request attribute */
212 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS9_CSR_EXT_REQ, &attr_oid) == 0) {
213 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
214 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100215 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700216 }
217
218 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
219 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
220 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100221 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700222 }
223
224 if ((ret = x509_csr_parse_extensions(csr, p, *p + len)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100225 return ret;
Jens Alfke2d9e3592019-10-29 15:03:37 -0700226 }
227
228 if (*p != end_attr_data) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100229 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
230 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700231 }
232 }
233
234 *p = end_attr_data;
235 }
236
237 if (*p != end) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100238 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
Przemek Stekiel36ad5e72023-01-26 22:30:45 +0100239 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700240 }
241
242 return 0;
243}
244
245/*
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200246 * Parse a CSR in DER format
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100248int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr,
249 const unsigned char *buf, size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200250{
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252 size_t len;
253 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_x509_buf sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 memset(&sig_params, 0, sizeof(mbedtls_x509_buf));
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200257
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200258 /*
259 * Check for valid input
260 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (csr == NULL || buf == NULL || buflen == 0) {
262 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
263 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 mbedtls_x509_csr_init(csr);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200266
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200267 /*
268 * first copy the raw DER data
269 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 p = mbedtls_calloc(1, len = buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (p == NULL) {
273 return MBEDTLS_ERR_X509_ALLOC_FAILED;
274 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 memcpy(p, buf, buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200277
278 csr->raw.p = p;
279 csr->raw.len = len;
280 end = p + len;
281
282 /*
283 * CertificationRequest ::= SEQUENCE {
284 * certificationRequestInfo CertificationRequestInfo,
285 * signatureAlgorithm AlgorithmIdentifier,
286 * signature BIT STRING
287 * }
288 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
290 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
291 mbedtls_x509_csr_free(csr);
292 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293 }
294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (len != (size_t) (end - p)) {
296 mbedtls_x509_csr_free(csr);
297 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
298 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200299 }
300
301 /*
302 * CertificationRequestInfo ::= SEQUENCE {
303 */
304 csr->cri.p = p;
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
307 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
308 mbedtls_x509_csr_free(csr);
309 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200310 }
311
312 end = p + len;
313 csr->cri.len = end - csr->cri.p;
314
315 /*
316 * Version ::= INTEGER { v1(0) }
317 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if ((ret = x509_csr_get_version(&p, end, &csr->version)) != 0) {
319 mbedtls_x509_csr_free(csr);
320 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200321 }
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (csr->version != 0) {
324 mbedtls_x509_csr_free(csr);
325 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200326 }
327
Andres AG2e3ddfa2017-02-17 13:54:43 +0000328 csr->version++;
329
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200330 /*
331 * subject Name
332 */
333 csr->subject_raw.p = p;
334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
336 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
337 mbedtls_x509_csr_free(csr);
338 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339 }
340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if ((ret = mbedtls_x509_get_name(&p, p + len, &csr->subject)) != 0) {
342 mbedtls_x509_csr_free(csr);
343 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344 }
345
346 csr->subject_raw.len = p - csr->subject_raw.p;
347
348 /*
349 * subjectPKInfo SubjectPublicKeyInfo
350 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &csr->pk)) != 0) {
352 mbedtls_x509_csr_free(csr);
353 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354 }
355
356 /*
357 * attributes [0] Attributes
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000358 *
359 * The list of possible attributes is open-ended, though RFC 2985
360 * (PKCS#9) defines a few in section 5.4. We currently don't support any,
361 * so we just ignore them. This is a safe thing to do as the worst thing
362 * that could happen is that we issue a certificate that does not match
363 * the requester's expectations - this cannot cause a violation of our
364 * signature policies.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
367 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
368 0) {
369 mbedtls_x509_csr_free(csr);
370 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200371 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372
Jens Alfke2d9e3592019-10-29 15:03:37 -0700373 if ((ret = x509_csr_parse_attributes(csr, p, p + len)) != 0) {
374 mbedtls_x509_csr_free(csr);
375 return ret;
376 }
377
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378 p += len;
379
380 end = csr->raw.p + csr->raw.len;
381
382 /*
383 * signatureAlgorithm AlgorithmIdentifier,
384 * signature BIT STRING
385 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if ((ret = mbedtls_x509_get_alg(&p, end, &csr->sig_oid, &sig_params)) != 0) {
387 mbedtls_x509_csr_free(csr);
388 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389 }
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if ((ret = mbedtls_x509_get_sig_alg(&csr->sig_oid, &sig_params,
392 &csr->sig_md, &csr->sig_pk,
393 &csr->sig_opts)) != 0) {
394 mbedtls_x509_csr_free(csr);
395 return MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396 }
397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if ((ret = mbedtls_x509_get_sig(&p, end, &csr->sig)) != 0) {
399 mbedtls_x509_csr_free(csr);
400 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401 }
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (p != end) {
404 mbedtls_x509_csr_free(csr);
405 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
406 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 }
408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410}
411
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200412/*
413 * Parse a CSR, allowing for PEM or raw DER encoding
414 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen)
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200416{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#if defined(MBEDTLS_PEM_PARSE_C)
Janos Follath865b3eb2019-12-16 11:46:15 +0000418 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200419 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_pem_context pem;
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200421#endif
422
423 /*
424 * Check for valid input
425 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if (csr == NULL || buf == NULL || buflen == 0) {
427 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
428 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200431 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (buf[buflen - 1] == '\0') {
433 mbedtls_pem_init(&pem);
434 ret = mbedtls_pem_read_buffer(&pem,
435 "-----BEGIN CERTIFICATE REQUEST-----",
436 "-----END CERTIFICATE REQUEST-----",
437 buf, NULL, 0, &use_len);
438 if (ret == MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
439 ret = mbedtls_pem_read_buffer(&pem,
440 "-----BEGIN NEW CERTIFICATE REQUEST-----",
441 "-----END NEW CERTIFICATE REQUEST-----",
442 buf, NULL, 0, &use_len);
Simon Butcher0488ce62018-09-30 15:36:50 +0100443 }
Simon Butchere1660af2018-10-07 17:48:37 +0100444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (ret == 0) {
Philippe Antoinec03059d2018-06-14 07:35:11 +0200446 /*
447 * Was PEM encoded, parse the result
448 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 ret = mbedtls_x509_csr_parse_der(csr, pem.buf, pem.buflen);
Simon Butcher0488ce62018-09-30 15:36:50 +0100450 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 mbedtls_pem_free(&pem);
453 if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
454 return ret;
455 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200456 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457#endif /* MBEDTLS_PEM_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 return mbedtls_x509_csr_parse_der(csr, buf, buflen);
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200459}
460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462/*
463 * Load a CSR into the structure
464 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100465int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466{
Janos Follath865b3eb2019-12-16 11:46:15 +0000467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468 size_t n;
469 unsigned char *buf;
470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
472 return ret;
473 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 ret = mbedtls_x509_csr_parse(csr, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100477 mbedtls_zeroize_and_free(buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
Hanno Becker612a2f12020-10-09 09:19:39 +0100483#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484#define BEFORE_COLON 14
485#define BC "14"
486/*
487 * Return an informational string about the CSR.
488 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100489int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix,
490 const mbedtls_x509_csr *csr)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491{
Janos Follath865b3eb2019-12-16 11:46:15 +0000492 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 size_t n;
494 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495 char key_size_str[BEFORE_COLON];
496
497 p = buf;
498 n = size;
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 ret = mbedtls_snprintf(p, n, "%sCSR version : %d",
501 prefix, csr->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200502 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200505 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 ret = mbedtls_x509_dn_gets(p, n, &csr->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200507 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200510 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 ret = mbedtls_x509_sig_alg_gets(p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
513 csr->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200514 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
517 mbedtls_pk_get_name(&csr->pk))) != 0) {
518 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519 }
520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
522 (int) mbedtls_pk_get_bitlen(&csr->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200523 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100525 /*
526 * Optional extensions
527 */
528
529 if (csr->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
530 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
531 MBEDTLS_X509_SAFE_SNPRINTF;
532
Przemek Stekiel21c37282023-01-16 08:47:49 +0100533 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
534 &csr->subject_alt_names,
535 prefix)) != 0) {
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100536 return ret;
537 }
538 }
539
540 if (csr->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
541 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
542 MBEDTLS_X509_SAFE_SNPRINTF;
543
Przemek Stekiel21c37282023-01-16 08:47:49 +0100544 if ((ret = mbedtls_x509_info_cert_type(&p, &n, csr->ns_cert_type)) != 0) {
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100545 return ret;
546 }
547 }
548
549 if (csr->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
550 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
551 MBEDTLS_X509_SAFE_SNPRINTF;
552
Przemek Stekiel21c37282023-01-16 08:47:49 +0100553 if ((ret = mbedtls_x509_info_key_usage(&p, &n, csr->key_usage)) != 0) {
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100554 return ret;
555 }
556 }
557
558 if (csr->ext_types != 0) {
559 ret = mbedtls_snprintf(p, n, "\n");
560 MBEDTLS_X509_SAFE_SNPRINTF;
561 }
562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564}
Hanno Becker612a2f12020-10-09 09:19:39 +0100565#endif /* MBEDTLS_X509_REMOVE_INFO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566
567/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200568 * Initialize a CSR
569 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570void mbedtls_x509_csr_init(mbedtls_x509_csr *csr)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200571{
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 memset(csr, 0, sizeof(mbedtls_x509_csr));
Paul Bakker369d2eb2013-09-18 11:58:25 +0200573}
574
575/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576 * Unallocate all CSR data
577 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100578void mbedtls_x509_csr_free(mbedtls_x509_csr *csr)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579{
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (csr == NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581 return;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582 }
583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 mbedtls_pk_free(&csr->pk);
585
586#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
587 mbedtls_free(csr->sig_opts);
588#endif
589
590 mbedtls_asn1_free_named_data_list_shallow(csr->subject.next);
Przemek Stekiela4687682023-01-24 15:19:47 +0100591 mbedtls_asn1_sequence_free(csr->subject_alt_names.next);
Gilles Peskine449bd832023-01-11 14:50:10 +0100592
593 if (csr->raw.p != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100594 mbedtls_zeroize_and_free(csr->raw.p, csr->raw.len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 }
596
597 mbedtls_platform_zeroize(csr, sizeof(mbedtls_x509_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598}
599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#endif /* MBEDTLS_X509_CSR_PARSE_C */