Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * X.509 Certificate Signing Request (CSR) parsing |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 18 | */ |
| 19 | /* |
| 20 | * The ITU-T X.509 standard defines a certificate format for PKI. |
| 21 | * |
Manuel Pégourié-Gonnard | 1c082f3 | 2014-06-12 22:34:55 +0200 | [diff] [blame] | 22 | * 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 25 | * |
| 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 Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 30 | #include "common.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 34 | #include "mbedtls/x509_csr.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 35 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 36 | #include "mbedtls/oid.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 37 | #include "mbedtls/platform_util.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 38 | |
| 39 | #include <string.h> |
| 40 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 42 | #include "mbedtls/pem.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 43 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 45 | #include "mbedtls/platform.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 46 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 48 | #include <stdio.h> |
| 49 | #endif |
| 50 | |
| 51 | /* |
| 52 | * Version ::= INTEGER { v1(0) } |
| 53 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 54 | static int x509_csr_get_version(unsigned char **p, |
| 55 | const unsigned char *end, |
| 56 | int *ver) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 57 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 58 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 59 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 60 | if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) { |
| 61 | if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 62 | *ver = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame^] | 73 | * Parse CSR extension requests in DER format |
| 74 | */ |
| 75 | static 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; |
| 81 | |
| 82 | 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) { |
| 89 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret; |
| 90 | } |
| 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) { |
| 97 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret; |
| 98 | } |
| 99 | |
| 100 | extn_oid.tag = MBEDTLS_ASN1_OID; |
| 101 | extn_oid.p = *p; |
| 102 | *p += extn_oid.len; |
| 103 | |
| 104 | /* Data should be octet string type */ |
| 105 | if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len, |
| 106 | MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 107 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret; |
| 108 | } |
| 109 | if (*p + len != end_ext_data) { |
| 110 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 111 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 112 | } |
| 113 | |
| 114 | if (mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type) == 0) { |
| 115 | switch (ext_type) { |
| 116 | case MBEDTLS_X509_EXT_KEY_USAGE: |
| 117 | /* Parse key usage */ |
| 118 | if ((ret = x509_get_key_usage(p, end_ext_data, |
| 119 | &csr->key_usage)) != 0) { |
| 120 | return ret; |
| 121 | } |
| 122 | break; |
| 123 | |
| 124 | case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME: |
| 125 | /* Parse subject alt name */ |
| 126 | if ((ret = x509_get_subject_alt_name(p, end_ext_data, |
| 127 | &csr->subject_alt_names)) != 0) { |
| 128 | return ret; |
| 129 | } |
| 130 | break; |
| 131 | |
| 132 | case MBEDTLS_X509_EXT_NS_CERT_TYPE: |
| 133 | /* Parse netscape certificate type */ |
| 134 | if ((ret = x509_get_ns_cert_type(p, end_ext_data, |
| 135 | &csr->ns_cert_type)) != 0) { |
| 136 | return ret; |
| 137 | } |
| 138 | break; |
| 139 | default: |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | *p = end_ext_data; |
| 145 | } |
| 146 | |
| 147 | if (*p != end) { |
| 148 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 149 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 150 | } |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Parse CSR attributes in DER format |
| 157 | */ |
| 158 | static int x509_csr_parse_attributes(mbedtls_x509_csr *csr, |
| 159 | const unsigned char *start, const unsigned char *end) |
| 160 | { |
| 161 | int ret; |
| 162 | size_t len; |
| 163 | unsigned char *end_attr_data; |
| 164 | unsigned char **p = (unsigned char **) &start; |
| 165 | |
| 166 | while (*p < end) { |
| 167 | mbedtls_x509_buf attr_oid = { 0, 0, NULL }; |
| 168 | |
| 169 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 170 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 171 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret; |
| 172 | } |
| 173 | end_attr_data = *p + len; |
| 174 | |
| 175 | /* Get attribute ID */ |
| 176 | if ((ret = mbedtls_asn1_get_tag(p, end_attr_data, &attr_oid.len, |
| 177 | MBEDTLS_ASN1_OID)) != 0) { |
| 178 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret; |
| 179 | } |
| 180 | |
| 181 | attr_oid.tag = MBEDTLS_ASN1_OID; |
| 182 | attr_oid.p = *p; |
| 183 | *p += attr_oid.len; |
| 184 | |
| 185 | /* Check that this is an extension-request attribute */ |
| 186 | if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS9_CSR_EXT_REQ, &attr_oid) == 0) { |
| 187 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 188 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) { |
| 189 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret; |
| 190 | } |
| 191 | |
| 192 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 193 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != |
| 194 | 0) { |
| 195 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret; |
| 196 | } |
| 197 | |
| 198 | if ((ret = x509_csr_parse_extensions(csr, p, *p + len)) != 0) { |
| 199 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret; |
| 200 | } |
| 201 | |
| 202 | if (*p != end_attr_data) { |
| 203 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 204 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | *p = end_attr_data; |
| 209 | } |
| 210 | |
| 211 | if (*p != end) { |
| 212 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 213 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /* |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 220 | * Parse a CSR in DER format |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 221 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr, |
| 223 | const unsigned char *buf, size_t buflen) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 224 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 225 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 226 | size_t len; |
| 227 | unsigned char *p, *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | mbedtls_x509_buf sig_params; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | memset(&sig_params, 0, sizeof(mbedtls_x509_buf)); |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 231 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 232 | /* |
| 233 | * Check for valid input |
| 234 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 235 | if (csr == NULL || buf == NULL || buflen == 0) { |
| 236 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 237 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | mbedtls_x509_csr_init(csr); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 240 | |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 241 | /* |
| 242 | * first copy the raw DER data |
| 243 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 244 | p = mbedtls_calloc(1, len = buflen); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 246 | if (p == NULL) { |
| 247 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 248 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 250 | memcpy(p, buf, buflen); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 251 | |
| 252 | csr->raw.p = p; |
| 253 | csr->raw.len = len; |
| 254 | end = p + len; |
| 255 | |
| 256 | /* |
| 257 | * CertificationRequest ::= SEQUENCE { |
| 258 | * certificationRequestInfo CertificationRequestInfo, |
| 259 | * signatureAlgorithm AlgorithmIdentifier, |
| 260 | * signature BIT STRING |
| 261 | * } |
| 262 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 264 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 265 | mbedtls_x509_csr_free(csr); |
| 266 | return MBEDTLS_ERR_X509_INVALID_FORMAT; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | if (len != (size_t) (end - p)) { |
| 270 | mbedtls_x509_csr_free(csr); |
| 271 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, |
| 272 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* |
| 276 | * CertificationRequestInfo ::= SEQUENCE { |
| 277 | */ |
| 278 | csr->cri.p = p; |
| 279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 280 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 281 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 282 | mbedtls_x509_csr_free(csr); |
| 283 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | end = p + len; |
| 287 | csr->cri.len = end - csr->cri.p; |
| 288 | |
| 289 | /* |
| 290 | * Version ::= INTEGER { v1(0) } |
| 291 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | if ((ret = x509_csr_get_version(&p, end, &csr->version)) != 0) { |
| 293 | mbedtls_x509_csr_free(csr); |
| 294 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 297 | if (csr->version != 0) { |
| 298 | mbedtls_x509_csr_free(csr); |
| 299 | return MBEDTLS_ERR_X509_UNKNOWN_VERSION; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Andres AG | 2e3ddfa | 2017-02-17 13:54:43 +0000 | [diff] [blame] | 302 | csr->version++; |
| 303 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 304 | /* |
| 305 | * subject Name |
| 306 | */ |
| 307 | csr->subject_raw.p = p; |
| 308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 309 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 310 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 311 | mbedtls_x509_csr_free(csr); |
| 312 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 315 | if ((ret = mbedtls_x509_get_name(&p, p + len, &csr->subject)) != 0) { |
| 316 | mbedtls_x509_csr_free(csr); |
| 317 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | csr->subject_raw.len = p - csr->subject_raw.p; |
| 321 | |
| 322 | /* |
| 323 | * subjectPKInfo SubjectPublicKeyInfo |
| 324 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &csr->pk)) != 0) { |
| 326 | mbedtls_x509_csr_free(csr); |
| 327 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /* |
| 331 | * attributes [0] Attributes |
Manuel Pégourié-Gonnard | 986bbf2 | 2016-02-24 14:36:05 +0000 | [diff] [blame] | 332 | * |
| 333 | * The list of possible attributes is open-ended, though RFC 2985 |
| 334 | * (PKCS#9) defines a few in section 5.4. We currently don't support any, |
| 335 | * so we just ignore them. This is a safe thing to do as the worst thing |
| 336 | * that could happen is that we issue a certificate that does not match |
| 337 | * the requester's expectations - this cannot cause a violation of our |
| 338 | * signature policies. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 339 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 341 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != |
| 342 | 0) { |
| 343 | mbedtls_x509_csr_free(csr); |
| 344 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 345 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 346 | |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame^] | 347 | if ((ret = x509_csr_parse_attributes(csr, p, p + len)) != 0) { |
| 348 | mbedtls_x509_csr_free(csr); |
| 349 | return ret; |
| 350 | } |
| 351 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 352 | p += len; |
| 353 | |
| 354 | end = csr->raw.p + csr->raw.len; |
| 355 | |
| 356 | /* |
| 357 | * signatureAlgorithm AlgorithmIdentifier, |
| 358 | * signature BIT STRING |
| 359 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 360 | if ((ret = mbedtls_x509_get_alg(&p, end, &csr->sig_oid, &sig_params)) != 0) { |
| 361 | mbedtls_x509_csr_free(csr); |
| 362 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | if ((ret = mbedtls_x509_get_sig_alg(&csr->sig_oid, &sig_params, |
| 366 | &csr->sig_md, &csr->sig_pk, |
| 367 | &csr->sig_opts)) != 0) { |
| 368 | mbedtls_x509_csr_free(csr); |
| 369 | return MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | if ((ret = mbedtls_x509_get_sig(&p, end, &csr->sig)) != 0) { |
| 373 | mbedtls_x509_csr_free(csr); |
| 374 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | if (p != end) { |
| 378 | mbedtls_x509_csr_free(csr); |
| 379 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, |
| 380 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 383 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 384 | } |
| 385 | |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 386 | /* |
| 387 | * Parse a CSR, allowing for PEM or raw DER encoding |
| 388 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen) |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 390 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | #if defined(MBEDTLS_PEM_PARSE_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 392 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 393 | size_t use_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 394 | mbedtls_pem_context pem; |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 395 | #endif |
| 396 | |
| 397 | /* |
| 398 | * Check for valid input |
| 399 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | if (csr == NULL || buf == NULL || buflen == 0) { |
| 401 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 402 | } |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 404 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 405 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | if (buf[buflen - 1] == '\0') { |
| 407 | mbedtls_pem_init(&pem); |
| 408 | ret = mbedtls_pem_read_buffer(&pem, |
| 409 | "-----BEGIN CERTIFICATE REQUEST-----", |
| 410 | "-----END CERTIFICATE REQUEST-----", |
| 411 | buf, NULL, 0, &use_len); |
| 412 | if (ret == MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 413 | ret = mbedtls_pem_read_buffer(&pem, |
| 414 | "-----BEGIN NEW CERTIFICATE REQUEST-----", |
| 415 | "-----END NEW CERTIFICATE REQUEST-----", |
| 416 | buf, NULL, 0, &use_len); |
Simon Butcher | 0488ce6 | 2018-09-30 15:36:50 +0100 | [diff] [blame] | 417 | } |
Simon Butcher | e1660af | 2018-10-07 17:48:37 +0100 | [diff] [blame] | 418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 419 | if (ret == 0) { |
Philippe Antoine | c03059d | 2018-06-14 07:35:11 +0200 | [diff] [blame] | 420 | /* |
| 421 | * Was PEM encoded, parse the result |
| 422 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | ret = mbedtls_x509_csr_parse_der(csr, pem.buf, pem.buflen); |
Simon Butcher | 0488ce6 | 2018-09-30 15:36:50 +0100 | [diff] [blame] | 424 | } |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | mbedtls_pem_free(&pem); |
| 427 | if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 428 | return ret; |
| 429 | } |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 430 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 431 | #endif /* MBEDTLS_PEM_PARSE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | return mbedtls_x509_csr_parse_der(csr, buf, buflen); |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 435 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 436 | /* |
| 437 | * Load a CSR into the structure |
| 438 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 440 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 441 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 442 | size_t n; |
| 443 | unsigned char *buf; |
| 444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 | if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) { |
| 446 | return ret; |
| 447 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | ret = mbedtls_x509_csr_parse(csr, buf, n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 451 | mbedtls_platform_zeroize(buf, n); |
| 452 | mbedtls_free(buf); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 455 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 456 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 457 | |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 458 | #if !defined(MBEDTLS_X509_REMOVE_INFO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 459 | #define BEFORE_COLON 14 |
| 460 | #define BC "14" |
| 461 | /* |
| 462 | * Return an informational string about the CSR. |
| 463 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix, |
| 465 | const mbedtls_x509_csr *csr) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 466 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 467 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 468 | size_t n; |
| 469 | char *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 470 | char key_size_str[BEFORE_COLON]; |
| 471 | |
| 472 | p = buf; |
| 473 | n = size; |
| 474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | ret = mbedtls_snprintf(p, n, "%sCSR version : %d", |
| 476 | prefix, csr->version); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 477 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 480 | MBEDTLS_X509_SAFE_SNPRINTF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | ret = mbedtls_x509_dn_gets(p, n, &csr->subject); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 482 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 485 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | ret = mbedtls_x509_sig_alg_gets(p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md, |
| 488 | csr->sig_opts); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 489 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON, |
| 492 | mbedtls_pk_get_name(&csr->pk))) != 0) { |
| 493 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 494 | } |
| 495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str, |
| 497 | (int) mbedtls_pk_get_bitlen(&csr->pk)); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 498 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 500 | return (int) (size - n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 501 | } |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 502 | #endif /* MBEDTLS_X509_REMOVE_INFO */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 503 | |
| 504 | /* |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 505 | * Initialize a CSR |
| 506 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 507 | void mbedtls_x509_csr_init(mbedtls_x509_csr *csr) |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 508 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 509 | memset(csr, 0, sizeof(mbedtls_x509_csr)); |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 513 | * Unallocate all CSR data |
| 514 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 515 | void mbedtls_x509_csr_free(mbedtls_x509_csr *csr) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 516 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 517 | if (csr == NULL) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 518 | return; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 519 | } |
| 520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 521 | mbedtls_pk_free(&csr->pk); |
| 522 | |
| 523 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 524 | mbedtls_free(csr->sig_opts); |
| 525 | #endif |
| 526 | |
| 527 | mbedtls_asn1_free_named_data_list_shallow(csr->subject.next); |
| 528 | |
| 529 | if (csr->raw.p != NULL) { |
| 530 | mbedtls_platform_zeroize(csr->raw.p, csr->raw.len); |
| 531 | mbedtls_free(csr->raw.p); |
| 532 | } |
| 533 | |
| 534 | mbedtls_platform_zeroize(csr, sizeof(mbedtls_x509_csr)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 537 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |