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 | /* |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 73 | * Parse a CSR in DER format |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 74 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 75 | int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr, |
| 76 | const unsigned char *buf, size_t buflen) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 77 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 78 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 79 | size_t len; |
| 80 | unsigned char *p, *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | mbedtls_x509_buf sig_params; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 82 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 83 | memset(&sig_params, 0, sizeof(mbedtls_x509_buf)); |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 84 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 85 | /* |
| 86 | * Check for valid input |
| 87 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 88 | if (csr == NULL || buf == NULL || buflen == 0) { |
| 89 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 90 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 91 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 92 | mbedtls_x509_csr_init(csr); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 93 | |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 94 | /* |
| 95 | * first copy the raw DER data |
| 96 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 97 | p = mbedtls_calloc(1, len = buflen); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 98 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 99 | if (p == NULL) { |
| 100 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 101 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 103 | memcpy(p, buf, buflen); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 104 | |
| 105 | csr->raw.p = p; |
| 106 | csr->raw.len = len; |
| 107 | end = p + len; |
| 108 | |
| 109 | /* |
| 110 | * CertificationRequest ::= SEQUENCE { |
| 111 | * certificationRequestInfo CertificationRequestInfo, |
| 112 | * signatureAlgorithm AlgorithmIdentifier, |
| 113 | * signature BIT STRING |
| 114 | * } |
| 115 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 116 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 117 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 118 | mbedtls_x509_csr_free(csr); |
| 119 | return MBEDTLS_ERR_X509_INVALID_FORMAT; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 122 | if (len != (size_t) (end - p)) { |
| 123 | mbedtls_x509_csr_free(csr); |
| 124 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, |
| 125 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* |
| 129 | * CertificationRequestInfo ::= SEQUENCE { |
| 130 | */ |
| 131 | csr->cri.p = p; |
| 132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 133 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 134 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 135 | mbedtls_x509_csr_free(csr); |
| 136 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | end = p + len; |
| 140 | csr->cri.len = end - csr->cri.p; |
| 141 | |
| 142 | /* |
| 143 | * Version ::= INTEGER { v1(0) } |
| 144 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 145 | if ((ret = x509_csr_get_version(&p, end, &csr->version)) != 0) { |
| 146 | mbedtls_x509_csr_free(csr); |
| 147 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 150 | if (csr->version != 0) { |
| 151 | mbedtls_x509_csr_free(csr); |
| 152 | return MBEDTLS_ERR_X509_UNKNOWN_VERSION; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Andres AG | 2e3ddfa | 2017-02-17 13:54:43 +0000 | [diff] [blame] | 155 | csr->version++; |
| 156 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 157 | /* |
| 158 | * subject Name |
| 159 | */ |
| 160 | csr->subject_raw.p = p; |
| 161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 162 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 163 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 164 | mbedtls_x509_csr_free(csr); |
| 165 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 168 | if ((ret = mbedtls_x509_get_name(&p, p + len, &csr->subject)) != 0) { |
| 169 | mbedtls_x509_csr_free(csr); |
| 170 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | csr->subject_raw.len = p - csr->subject_raw.p; |
| 174 | |
| 175 | /* |
| 176 | * subjectPKInfo SubjectPublicKeyInfo |
| 177 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 178 | if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &csr->pk)) != 0) { |
| 179 | mbedtls_x509_csr_free(csr); |
| 180 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* |
| 184 | * attributes [0] Attributes |
Manuel Pégourié-Gonnard | 986bbf2 | 2016-02-24 14:36:05 +0000 | [diff] [blame] | 185 | * |
| 186 | * The list of possible attributes is open-ended, though RFC 2985 |
| 187 | * (PKCS#9) defines a few in section 5.4. We currently don't support any, |
| 188 | * so we just ignore them. This is a safe thing to do as the worst thing |
| 189 | * that could happen is that we issue a certificate that does not match |
| 190 | * the requester's expectations - this cannot cause a violation of our |
| 191 | * signature policies. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 192 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 193 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 194 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != |
| 195 | 0) { |
| 196 | mbedtls_x509_csr_free(csr); |
| 197 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 198 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 199 | |
| 200 | p += len; |
| 201 | |
| 202 | end = csr->raw.p + csr->raw.len; |
| 203 | |
| 204 | /* |
| 205 | * signatureAlgorithm AlgorithmIdentifier, |
| 206 | * signature BIT STRING |
| 207 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 208 | if ((ret = mbedtls_x509_get_alg(&p, end, &csr->sig_oid, &sig_params)) != 0) { |
| 209 | mbedtls_x509_csr_free(csr); |
| 210 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 211 | } |
| 212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 213 | if ((ret = mbedtls_x509_get_sig_alg(&csr->sig_oid, &sig_params, |
| 214 | &csr->sig_md, &csr->sig_pk, |
| 215 | &csr->sig_opts)) != 0) { |
| 216 | mbedtls_x509_csr_free(csr); |
| 217 | return MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 220 | if ((ret = mbedtls_x509_get_sig(&p, end, &csr->sig)) != 0) { |
| 221 | mbedtls_x509_csr_free(csr); |
| 222 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 225 | if (p != end) { |
| 226 | mbedtls_x509_csr_free(csr); |
| 227 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, |
| 228 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 231 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 234 | /* |
| 235 | * Parse a CSR, allowing for PEM or raw DER encoding |
| 236 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 237 | 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] | 238 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | #if defined(MBEDTLS_PEM_PARSE_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 240 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 241 | size_t use_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | mbedtls_pem_context pem; |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 243 | #endif |
| 244 | |
| 245 | /* |
| 246 | * Check for valid input |
| 247 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 248 | if (csr == NULL || buf == NULL || buflen == 0) { |
| 249 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 250 | } |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 251 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 252 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 253 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 254 | if (buf[buflen - 1] == '\0') { |
| 255 | mbedtls_pem_init(&pem); |
| 256 | ret = mbedtls_pem_read_buffer(&pem, |
| 257 | "-----BEGIN CERTIFICATE REQUEST-----", |
| 258 | "-----END CERTIFICATE REQUEST-----", |
| 259 | buf, NULL, 0, &use_len); |
| 260 | if (ret == MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 261 | ret = mbedtls_pem_read_buffer(&pem, |
| 262 | "-----BEGIN NEW CERTIFICATE REQUEST-----", |
| 263 | "-----END NEW CERTIFICATE REQUEST-----", |
| 264 | buf, NULL, 0, &use_len); |
Simon Butcher | 0488ce6 | 2018-09-30 15:36:50 +0100 | [diff] [blame] | 265 | } |
Simon Butcher | e1660af | 2018-10-07 17:48:37 +0100 | [diff] [blame] | 266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 267 | if (ret == 0) { |
Philippe Antoine | c03059d | 2018-06-14 07:35:11 +0200 | [diff] [blame] | 268 | /* |
| 269 | * Was PEM encoded, parse the result |
| 270 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 271 | ret = mbedtls_x509_csr_parse_der(csr, pem.buf, pem.buflen); |
Simon Butcher | 0488ce6 | 2018-09-30 15:36:50 +0100 | [diff] [blame] | 272 | } |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 274 | mbedtls_pem_free(&pem); |
| 275 | if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 276 | return ret; |
| 277 | } |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 278 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | #endif /* MBEDTLS_PEM_PARSE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 280 | return mbedtls_x509_csr_parse_der(csr, buf, buflen); |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 283 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 284 | /* |
| 285 | * Load a CSR into the structure |
| 286 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 287 | 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] | 288 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 289 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 290 | size_t n; |
| 291 | unsigned char *buf; |
| 292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 293 | if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) { |
| 294 | return ret; |
| 295 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 297 | ret = mbedtls_x509_csr_parse(csr, buf, n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 299 | mbedtls_platform_zeroize(buf, n); |
| 300 | mbedtls_free(buf); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 302 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 303 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 305 | |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 306 | #if !defined(MBEDTLS_X509_REMOVE_INFO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 307 | #define BEFORE_COLON 14 |
| 308 | #define BC "14" |
| 309 | /* |
| 310 | * Return an informational string about the CSR. |
| 311 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 312 | int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix, |
| 313 | const mbedtls_x509_csr *csr) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 314 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 315 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 316 | size_t n; |
| 317 | char *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 318 | char key_size_str[BEFORE_COLON]; |
| 319 | |
| 320 | p = buf; |
| 321 | n = size; |
| 322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 323 | ret = mbedtls_snprintf(p, n, "%sCSR version : %d", |
| 324 | prefix, csr->version); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 325 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 327 | ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 328 | MBEDTLS_X509_SAFE_SNPRINTF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 329 | ret = mbedtls_x509_dn_gets(p, n, &csr->subject); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 330 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 332 | ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 333 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 335 | ret = mbedtls_x509_sig_alg_gets(p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md, |
| 336 | csr->sig_opts); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 337 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 339 | if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON, |
| 340 | mbedtls_pk_get_name(&csr->pk))) != 0) { |
| 341 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 344 | ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str, |
| 345 | (int) mbedtls_pk_get_bitlen(&csr->pk)); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 346 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 348 | return (int) (size - n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 349 | } |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 350 | #endif /* MBEDTLS_X509_REMOVE_INFO */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 351 | |
| 352 | /* |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 353 | * Initialize a CSR |
| 354 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 355 | void mbedtls_x509_csr_init(mbedtls_x509_csr *csr) |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 356 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 357 | memset(csr, 0, sizeof(mbedtls_x509_csr)); |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 361 | * Unallocate all CSR data |
| 362 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 363 | void mbedtls_x509_csr_free(mbedtls_x509_csr *csr) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 364 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 365 | if (csr == NULL) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 366 | return; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 369 | mbedtls_pk_free(&csr->pk); |
| 370 | |
| 371 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 372 | mbedtls_free(csr->sig_opts); |
| 373 | #endif |
| 374 | |
| 375 | mbedtls_asn1_free_named_data_list_shallow(csr->subject.next); |
| 376 | |
| 377 | if (csr->raw.p != NULL) { |
| 378 | mbedtls_platform_zeroize(csr->raw.p, csr->raw.len); |
| 379 | mbedtls_free(csr->raw.p); |
| 380 | } |
| 381 | |
| 382 | mbedtls_platform_zeroize(csr, sizeof(mbedtls_x509_csr)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 385 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |