blob: a8fef7c54d6b4b8fd9282d0456c1fa3488f27d36 [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,
Matthias Schulzab408222023-10-18 13:20:59 +020076 unsigned char **p, const unsigned char *end,
77 mbedtls_x509_csr_ext_cb_t cb,
78 void *p_ctx)
Jens Alfke2d9e3592019-10-29 15:03:37 -070079{
Matthias Schulz873a2022023-10-17 16:02:20 +020080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Alfke2d9e3592019-10-29 15:03:37 -070081 size_t len;
Matthias Schulzab408222023-10-18 13:20:59 +020082 unsigned char *end_ext_data, *end_ext_octet;
Matthias Schulzcc923f32023-10-17 12:36:23 +020083
Jens Alfke2d9e3592019-10-29 15:03:37 -070084 while (*p < end) {
85 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Matthias Schulz873a2022023-10-17 16:02:20 +020086 int is_critical = 0; /* DEFAULT FALSE */
Jens Alfke2d9e3592019-10-29 15:03:37 -070087 int ext_type = 0;
88
89 /* Read sequence tag */
90 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
91 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +010092 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -070093 }
94
95 end_ext_data = *p + len;
96
97 /* Get extension ID */
98 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
99 MBEDTLS_ASN1_OID)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100100 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700101 }
102
103 extn_oid.tag = MBEDTLS_ASN1_OID;
104 extn_oid.p = *p;
105 *p += extn_oid.len;
106
Matthias Schulz873a2022023-10-17 16:02:20 +0200107 /* Get optional critical */
108 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
109 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
110 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
111 }
Matthias Schulzadb3cc42023-10-17 11:50:50 +0200112
Jens Alfke2d9e3592019-10-29 15:03:37 -0700113 /* Data should be octet string type */
114 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
115 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100116 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700117 }
Przemek Stekiel86d19462023-01-24 09:45:19 +0100118
Matthias Schulzab408222023-10-18 13:20:59 +0200119 end_ext_octet = *p + len;
120
121 if (end_ext_octet != end_ext_data) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100122 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
123 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700124 }
125
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100126 /*
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100127 * Detect supported extensions and skip unsupported extensions
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100128 */
129 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
130
Matthias Schulzab408222023-10-18 13:20:59 +0200131 if (ret != 0) {
132 /* Give the callback (if any) a chance to handle the extension */
133 if (cb != NULL) {
134 ret = cb(p_ctx, csr, &extn_oid, is_critical, *p, end_ext_octet);
135 if (ret != 0 && is_critical) {
136 return ret;
137 }
138 *p = end_ext_octet;
139 continue;
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100140 }
141
Matthias Schulzab408222023-10-18 13:20:59 +0200142 /* No parser found, skip extension */
143 *p = end_ext_octet;
Przemek Stekiel94e21e12023-01-25 11:08:32 +0100144
Matthias Schulz873a2022023-10-17 16:02:20 +0200145 if (is_critical) {
146 /* Data is marked as critical: fail */
147 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
148 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
149 }
Matthias Schulzab408222023-10-18 13:20:59 +0200150 continue;
Przemek Stekiel86d19462023-01-24 09:45:19 +0100151 }
Matthias Schulzab408222023-10-18 13:20:59 +0200152
153 /* Forbid repeated extensions */
154 if ((csr->ext_types & ext_type) != 0) {
155 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200156 MBEDTLS_ERR_ASN1_INVALID_DATA);
Matthias Schulzab408222023-10-18 13:20:59 +0200157 }
158
159 csr->ext_types |= ext_type;
160
161 switch (ext_type) {
162 case MBEDTLS_X509_EXT_KEY_USAGE:
163 /* Parse key usage */
164 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_data,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200165 &csr->key_usage)) != 0) {
Matthias Schulzab408222023-10-18 13:20:59 +0200166 return ret;
167 }
168 break;
169
170 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
171 /* Parse subject alt name */
172 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_data,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200173 &csr->subject_alt_names)) != 0) {
Matthias Schulzab408222023-10-18 13:20:59 +0200174 return ret;
175 }
176 break;
177
178 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
179 /* Parse netscape certificate type */
180 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_data,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200181 &csr->ns_cert_type)) != 0) {
Matthias Schulzab408222023-10-18 13:20:59 +0200182 return ret;
183 }
184 break;
185 default:
186 /*
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200187 * If this is a non-critical extension, which the oid layer
188 * supports, but there isn't an x509 parser for it,
189 * skip the extension.
190 */
Matthias Schulzab408222023-10-18 13:20:59 +0200191 if (is_critical) {
192 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
193 } else {
194 *p = end_ext_octet;
195 }
196 }
Jens Alfke2d9e3592019-10-29 15:03:37 -0700197 }
198
199 if (*p != end) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100200 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
201 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700202 }
203
204 return 0;
205}
206
207/*
208 * Parse CSR attributes in DER format
209 */
210static int x509_csr_parse_attributes(mbedtls_x509_csr *csr,
Matthias Schulzab408222023-10-18 13:20:59 +0200211 const unsigned char *start, const unsigned char *end,
212 mbedtls_x509_csr_ext_cb_t cb,
213 void *p_ctx)
Jens Alfke2d9e3592019-10-29 15:03:37 -0700214{
215 int ret;
216 size_t len;
217 unsigned char *end_attr_data;
218 unsigned char **p = (unsigned char **) &start;
219
220 while (*p < end) {
221 mbedtls_x509_buf attr_oid = { 0, 0, NULL };
222
223 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
224 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100225 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700226 }
227 end_attr_data = *p + len;
228
229 /* Get attribute ID */
230 if ((ret = mbedtls_asn1_get_tag(p, end_attr_data, &attr_oid.len,
231 MBEDTLS_ASN1_OID)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100232 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700233 }
234
235 attr_oid.tag = MBEDTLS_ASN1_OID;
236 attr_oid.p = *p;
237 *p += attr_oid.len;
238
239 /* Check that this is an extension-request attribute */
240 if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS9_CSR_EXT_REQ, &attr_oid) == 0) {
241 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
242 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100243 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700244 }
245
246 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
247 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
248 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100249 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700250 }
251
Matthias Schulzab408222023-10-18 13:20:59 +0200252 if ((ret = x509_csr_parse_extensions(csr, p, *p + len, cb, p_ctx)) != 0) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100253 return ret;
Jens Alfke2d9e3592019-10-29 15:03:37 -0700254 }
255
256 if (*p != end_attr_data) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100257 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
258 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700259 }
260 }
261
262 *p = end_attr_data;
263 }
264
265 if (*p != end) {
Przemek Stekiel86d19462023-01-24 09:45:19 +0100266 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
Przemek Stekiel36ad5e72023-01-26 22:30:45 +0100267 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Jens Alfke2d9e3592019-10-29 15:03:37 -0700268 }
269
270 return 0;
271}
272
273/*
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200274 * Parse a CSR in DER format
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275 */
Matthias Schulzab408222023-10-18 13:20:59 +0200276static int mbedtls_x509_csr_parse_der_internal(mbedtls_x509_csr *csr,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200277 const unsigned char *buf, size_t buflen,
278 mbedtls_x509_csr_ext_cb_t cb,
279 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280{
Janos Follath865b3eb2019-12-16 11:46:15 +0000281 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200282 size_t len;
283 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_x509_buf sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 memset(&sig_params, 0, sizeof(mbedtls_x509_buf));
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200287
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 /*
289 * Check for valid input
290 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (csr == NULL || buf == NULL || buflen == 0) {
292 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
293 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 mbedtls_x509_csr_init(csr);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200297 /*
298 * first copy the raw DER data
299 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 p = mbedtls_calloc(1, len = buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (p == NULL) {
303 return MBEDTLS_ERR_X509_ALLOC_FAILED;
304 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 memcpy(p, buf, buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307
308 csr->raw.p = p;
309 csr->raw.len = len;
310 end = p + len;
311
312 /*
313 * CertificationRequest ::= SEQUENCE {
314 * certificationRequestInfo CertificationRequestInfo,
315 * signatureAlgorithm AlgorithmIdentifier,
316 * signature BIT STRING
317 * }
318 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
320 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
321 mbedtls_x509_csr_free(csr);
322 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323 }
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (len != (size_t) (end - p)) {
326 mbedtls_x509_csr_free(csr);
327 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
328 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 }
330
331 /*
332 * CertificationRequestInfo ::= SEQUENCE {
333 */
334 csr->cri.p = p;
335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
337 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
338 mbedtls_x509_csr_free(csr);
339 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200340 }
341
342 end = p + len;
343 csr->cri.len = end - csr->cri.p;
344
345 /*
346 * Version ::= INTEGER { v1(0) }
347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if ((ret = x509_csr_get_version(&p, end, &csr->version)) != 0) {
349 mbedtls_x509_csr_free(csr);
350 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351 }
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (csr->version != 0) {
354 mbedtls_x509_csr_free(csr);
355 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356 }
357
Andres AG2e3ddfa2017-02-17 13:54:43 +0000358 csr->version++;
359
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360 /*
361 * subject Name
362 */
363 csr->subject_raw.p = p;
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
366 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
367 mbedtls_x509_csr_free(csr);
368 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369 }
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if ((ret = mbedtls_x509_get_name(&p, p + len, &csr->subject)) != 0) {
372 mbedtls_x509_csr_free(csr);
373 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374 }
375
376 csr->subject_raw.len = p - csr->subject_raw.p;
377
378 /*
379 * subjectPKInfo SubjectPublicKeyInfo
380 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &csr->pk)) != 0) {
382 mbedtls_x509_csr_free(csr);
383 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384 }
385
386 /*
387 * attributes [0] Attributes
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000388 *
389 * The list of possible attributes is open-ended, though RFC 2985
390 * (PKCS#9) defines a few in section 5.4. We currently don't support any,
391 * so we just ignore them. This is a safe thing to do as the worst thing
392 * that could happen is that we issue a certificate that does not match
393 * the requester's expectations - this cannot cause a violation of our
394 * signature policies.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
397 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
398 0) {
399 mbedtls_x509_csr_free(csr);
400 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402
Matthias Schulzab408222023-10-18 13:20:59 +0200403 if ((ret = x509_csr_parse_attributes(csr, p, p + len, cb, p_ctx)) != 0) {
Jens Alfke2d9e3592019-10-29 15:03:37 -0700404 mbedtls_x509_csr_free(csr);
405 return ret;
406 }
407
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408 p += len;
409
410 end = csr->raw.p + csr->raw.len;
411
412 /*
413 * signatureAlgorithm AlgorithmIdentifier,
414 * signature BIT STRING
415 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if ((ret = mbedtls_x509_get_alg(&p, end, &csr->sig_oid, &sig_params)) != 0) {
417 mbedtls_x509_csr_free(csr);
418 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419 }
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if ((ret = mbedtls_x509_get_sig_alg(&csr->sig_oid, &sig_params,
422 &csr->sig_md, &csr->sig_pk,
423 &csr->sig_opts)) != 0) {
424 mbedtls_x509_csr_free(csr);
425 return MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426 }
427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if ((ret = mbedtls_x509_get_sig(&p, end, &csr->sig)) != 0) {
429 mbedtls_x509_csr_free(csr);
430 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431 }
432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (p != end) {
434 mbedtls_x509_csr_free(csr);
435 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
436 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437 }
438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440}
441
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200442/*
Matthias Schulzab408222023-10-18 13:20:59 +0200443 * Parse a CSR in DER format
444 */
445int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr,
446 const unsigned char *buf, size_t buflen)
447{
448 return mbedtls_x509_csr_parse_der_internal(csr, buf, buflen, NULL, NULL);
449}
450
451/*
452 * Parse a CSR in DER format with callback for unknown extensions
453 */
454int mbedtls_x509_csr_parse_der_with_ext_cb(mbedtls_x509_csr *csr,
Matthias Schulzedc32ea2023-10-19 16:09:08 +0200455 const unsigned char *buf, size_t buflen,
456 mbedtls_x509_csr_ext_cb_t cb,
457 void *p_ctx)
Matthias Schulzab408222023-10-18 13:20:59 +0200458{
459 return mbedtls_x509_csr_parse_der_internal(csr, buf, buflen, cb, p_ctx);
460}
461
462/*
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200463 * Parse a CSR, allowing for PEM or raw DER encoding
464 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100465int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen)
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200466{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#if defined(MBEDTLS_PEM_PARSE_C)
Janos Follath865b3eb2019-12-16 11:46:15 +0000468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200469 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_pem_context pem;
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200471#endif
472
473 /*
474 * Check for valid input
475 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (csr == NULL || buf == NULL || buflen == 0) {
477 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
478 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200481 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if (buf[buflen - 1] == '\0') {
483 mbedtls_pem_init(&pem);
484 ret = mbedtls_pem_read_buffer(&pem,
485 "-----BEGIN CERTIFICATE REQUEST-----",
486 "-----END CERTIFICATE REQUEST-----",
487 buf, NULL, 0, &use_len);
488 if (ret == MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
489 ret = mbedtls_pem_read_buffer(&pem,
490 "-----BEGIN NEW CERTIFICATE REQUEST-----",
491 "-----END NEW CERTIFICATE REQUEST-----",
492 buf, NULL, 0, &use_len);
Simon Butcher0488ce62018-09-30 15:36:50 +0100493 }
Simon Butchere1660af2018-10-07 17:48:37 +0100494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (ret == 0) {
Philippe Antoinec03059d2018-06-14 07:35:11 +0200496 /*
497 * Was PEM encoded, parse the result
498 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 ret = mbedtls_x509_csr_parse_der(csr, pem.buf, pem.buflen);
Simon Butcher0488ce62018-09-30 15:36:50 +0100500 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 mbedtls_pem_free(&pem);
503 if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
504 return ret;
505 }
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200506 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#endif /* MBEDTLS_PEM_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 return mbedtls_x509_csr_parse_der(csr, buf, buflen);
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200509}
510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512/*
513 * Load a CSR into the structure
514 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100515int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516{
Janos Follath865b3eb2019-12-16 11:46:15 +0000517 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518 size_t n;
519 unsigned char *buf;
520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
522 return ret;
523 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 ret = mbedtls_x509_csr_parse(csr, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100527 mbedtls_zeroize_and_free(buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532
Hanno Becker612a2f12020-10-09 09:19:39 +0100533#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534#define BEFORE_COLON 14
535#define BC "14"
536/*
537 * Return an informational string about the CSR.
538 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100539int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix,
540 const mbedtls_x509_csr *csr)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541{
Janos Follath865b3eb2019-12-16 11:46:15 +0000542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543 size_t n;
544 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545 char key_size_str[BEFORE_COLON];
546
547 p = buf;
548 n = size;
549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 ret = mbedtls_snprintf(p, n, "%sCSR version : %d",
551 prefix, csr->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200552 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200555 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 ret = mbedtls_x509_dn_gets(p, n, &csr->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200557 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200560 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 ret = mbedtls_x509_sig_alg_gets(p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
563 csr->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200564 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
567 mbedtls_pk_get_name(&csr->pk))) != 0) {
568 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569 }
570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
572 (int) mbedtls_pk_get_bitlen(&csr->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200573 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100575 /*
576 * Optional extensions
577 */
578
579 if (csr->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
580 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
581 MBEDTLS_X509_SAFE_SNPRINTF;
582
Przemek Stekiel21c37282023-01-16 08:47:49 +0100583 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
584 &csr->subject_alt_names,
585 prefix)) != 0) {
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100586 return ret;
587 }
588 }
589
590 if (csr->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
591 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
592 MBEDTLS_X509_SAFE_SNPRINTF;
593
Przemek Stekiel21c37282023-01-16 08:47:49 +0100594 if ((ret = mbedtls_x509_info_cert_type(&p, &n, csr->ns_cert_type)) != 0) {
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100595 return ret;
596 }
597 }
598
599 if (csr->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
600 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
601 MBEDTLS_X509_SAFE_SNPRINTF;
602
Przemek Stekiel21c37282023-01-16 08:47:49 +0100603 if ((ret = mbedtls_x509_info_key_usage(&p, &n, csr->key_usage)) != 0) {
Przemek Stekielcbaf3162023-01-12 12:58:02 +0100604 return ret;
605 }
606 }
607
608 if (csr->ext_types != 0) {
609 ret = mbedtls_snprintf(p, n, "\n");
610 MBEDTLS_X509_SAFE_SNPRINTF;
611 }
612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200614}
Hanno Becker612a2f12020-10-09 09:19:39 +0100615#endif /* MBEDTLS_X509_REMOVE_INFO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616
617/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200618 * Initialize a CSR
619 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100620void mbedtls_x509_csr_init(mbedtls_x509_csr *csr)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200621{
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 memset(csr, 0, sizeof(mbedtls_x509_csr));
Paul Bakker369d2eb2013-09-18 11:58:25 +0200623}
624
625/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 * Unallocate all CSR data
627 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628void mbedtls_x509_csr_free(mbedtls_x509_csr *csr)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629{
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (csr == NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631 return;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632 }
633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 mbedtls_pk_free(&csr->pk);
635
636#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
637 mbedtls_free(csr->sig_opts);
638#endif
639
640 mbedtls_asn1_free_named_data_list_shallow(csr->subject.next);
Przemek Stekiela4687682023-01-24 15:19:47 +0100641 mbedtls_asn1_sequence_free(csr->subject_alt_names.next);
Gilles Peskine449bd832023-01-11 14:50:10 +0100642
643 if (csr->raw.p != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100644 mbedtls_zeroize_and_free(csr->raw.p, csr->raw.len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 }
646
647 mbedtls_platform_zeroize(csr, sizeof(mbedtls_x509_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648}
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_X509_CSR_PARSE_C */