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