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, |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 76 | unsigned char **p, const unsigned char *end, |
| 77 | mbedtls_x509_csr_ext_cb_t cb, |
| 78 | void *p_ctx) |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 79 | { |
Matthias Schulz | 873a202 | 2023-10-17 16:02:20 +0200 | [diff] [blame] | 80 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 81 | size_t len; |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 82 | unsigned char *end_ext_data, *end_ext_octet; |
Matthias Schulz | cc923f3 | 2023-10-17 12:36:23 +0200 | [diff] [blame] | 83 | |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 84 | while (*p < end) { |
| 85 | mbedtls_x509_buf extn_oid = { 0, 0, NULL }; |
Matthias Schulz | 873a202 | 2023-10-17 16:02:20 +0200 | [diff] [blame] | 86 | int is_critical = 0; /* DEFAULT FALSE */ |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 87 | 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 Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 92 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 93 | } |
| 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 Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 100 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | extn_oid.tag = MBEDTLS_ASN1_OID; |
| 104 | extn_oid.p = *p; |
| 105 | *p += extn_oid.len; |
| 106 | |
Matthias Schulz | 873a202 | 2023-10-17 16:02:20 +0200 | [diff] [blame] | 107 | /* 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 Schulz | adb3cc4 | 2023-10-17 11:50:50 +0200 | [diff] [blame] | 112 | |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 113 | /* 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 Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 116 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 117 | } |
Przemek Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 118 | |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 119 | end_ext_octet = *p + len; |
| 120 | |
| 121 | if (end_ext_octet != end_ext_data) { |
Przemek Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 122 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 123 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Przemek Stekiel | cbaf316 | 2023-01-12 12:58:02 +0100 | [diff] [blame] | 126 | /* |
Przemek Stekiel | 94e21e1 | 2023-01-25 11:08:32 +0100 | [diff] [blame] | 127 | * Detect supported extensions and skip unsupported extensions |
Przemek Stekiel | cbaf316 | 2023-01-12 12:58:02 +0100 | [diff] [blame] | 128 | */ |
| 129 | ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type); |
| 130 | |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 131 | 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 Stekiel | 94e21e1 | 2023-01-25 11:08:32 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 142 | /* No parser found, skip extension */ |
| 143 | *p = end_ext_octet; |
Przemek Stekiel | 94e21e1 | 2023-01-25 11:08:32 +0100 | [diff] [blame] | 144 | |
Matthias Schulz | 873a202 | 2023-10-17 16:02:20 +0200 | [diff] [blame] | 145 | 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 Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 150 | continue; |
Przemek Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 151 | } |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 152 | |
| 153 | /* Forbid repeated extensions */ |
| 154 | if ((csr->ext_types & ext_type) != 0) { |
| 155 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
Matthias Schulz | edc32ea | 2023-10-19 16:09:08 +0200 | [diff] [blame^] | 156 | MBEDTLS_ERR_ASN1_INVALID_DATA); |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 157 | } |
| 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 Schulz | edc32ea | 2023-10-19 16:09:08 +0200 | [diff] [blame^] | 165 | &csr->key_usage)) != 0) { |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 166 | 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 Schulz | edc32ea | 2023-10-19 16:09:08 +0200 | [diff] [blame^] | 173 | &csr->subject_alt_names)) != 0) { |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 174 | 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 Schulz | edc32ea | 2023-10-19 16:09:08 +0200 | [diff] [blame^] | 181 | &csr->ns_cert_type)) != 0) { |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 182 | return ret; |
| 183 | } |
| 184 | break; |
| 185 | default: |
| 186 | /* |
Matthias Schulz | edc32ea | 2023-10-19 16:09:08 +0200 | [diff] [blame^] | 187 | * 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 Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 191 | if (is_critical) { |
| 192 | return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; |
| 193 | } else { |
| 194 | *p = end_ext_octet; |
| 195 | } |
| 196 | } |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | if (*p != end) { |
Przemek Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 200 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 201 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Parse CSR attributes in DER format |
| 209 | */ |
| 210 | static int x509_csr_parse_attributes(mbedtls_x509_csr *csr, |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 211 | const unsigned char *start, const unsigned char *end, |
| 212 | mbedtls_x509_csr_ext_cb_t cb, |
| 213 | void *p_ctx) |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 214 | { |
| 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 Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 225 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 226 | } |
| 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 Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 232 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 233 | } |
| 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 Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 243 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 247 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != |
| 248 | 0) { |
Przemek Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 249 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 252 | if ((ret = x509_csr_parse_extensions(csr, p, *p + len, cb, p_ctx)) != 0) { |
Przemek Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 253 | return ret; |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | if (*p != end_attr_data) { |
Przemek Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 257 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 258 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | *p = end_attr_data; |
| 263 | } |
| 264 | |
| 265 | if (*p != end) { |
Przemek Stekiel | 86d1946 | 2023-01-24 09:45:19 +0100 | [diff] [blame] | 266 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
Przemek Stekiel | 36ad5e7 | 2023-01-26 22:30:45 +0100 | [diff] [blame] | 267 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | /* |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 274 | * Parse a CSR in DER format |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 275 | */ |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 276 | static int mbedtls_x509_csr_parse_der_internal(mbedtls_x509_csr *csr, |
Matthias Schulz | edc32ea | 2023-10-19 16:09:08 +0200 | [diff] [blame^] | 277 | const unsigned char *buf, size_t buflen, |
| 278 | mbedtls_x509_csr_ext_cb_t cb, |
| 279 | void *p_ctx) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 280 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 281 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 282 | size_t len; |
| 283 | unsigned char *p, *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 284 | mbedtls_x509_buf sig_params; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | memset(&sig_params, 0, sizeof(mbedtls_x509_buf)); |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 287 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 288 | /* |
| 289 | * Check for valid input |
| 290 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | if (csr == NULL || buf == NULL || buflen == 0) { |
| 292 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 293 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | mbedtls_x509_csr_init(csr); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 296 | |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 297 | /* |
| 298 | * first copy the raw DER data |
| 299 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | p = mbedtls_calloc(1, len = buflen); |
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 | if (p == NULL) { |
| 303 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 304 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | memcpy(p, buf, buflen); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 307 | |
| 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 Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
| 332 | * CertificationRequestInfo ::= SEQUENCE { |
| 333 | */ |
| 334 | csr->cri.p = p; |
| 335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | end = p + len; |
| 343 | csr->cri.len = end - csr->cri.p; |
| 344 | |
| 345 | /* |
| 346 | * Version ::= INTEGER { v1(0) } |
| 347 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 348 | if ((ret = x509_csr_get_version(&p, end, &csr->version)) != 0) { |
| 349 | mbedtls_x509_csr_free(csr); |
| 350 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 353 | if (csr->version != 0) { |
| 354 | mbedtls_x509_csr_free(csr); |
| 355 | return MBEDTLS_ERR_X509_UNKNOWN_VERSION; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Andres AG | 2e3ddfa | 2017-02-17 13:54:43 +0000 | [diff] [blame] | 358 | csr->version++; |
| 359 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 360 | /* |
| 361 | * subject Name |
| 362 | */ |
| 363 | csr->subject_raw.p = p; |
| 364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 371 | if ((ret = mbedtls_x509_get_name(&p, p + len, &csr->subject)) != 0) { |
| 372 | mbedtls_x509_csr_free(csr); |
| 373 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | csr->subject_raw.len = p - csr->subject_raw.p; |
| 377 | |
| 378 | /* |
| 379 | * subjectPKInfo SubjectPublicKeyInfo |
| 380 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 381 | if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &csr->pk)) != 0) { |
| 382 | mbedtls_x509_csr_free(csr); |
| 383 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* |
| 387 | * attributes [0] Attributes |
Manuel Pégourié-Gonnard | 986bbf2 | 2016-02-24 14:36:05 +0000 | [diff] [blame] | 388 | * |
| 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 395 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 401 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 402 | |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 403 | if ((ret = x509_csr_parse_attributes(csr, p, p + len, cb, p_ctx)) != 0) { |
Jens Alfke | 2d9e359 | 2019-10-29 15:03:37 -0700 | [diff] [blame] | 404 | mbedtls_x509_csr_free(csr); |
| 405 | return ret; |
| 406 | } |
| 407 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 408 | p += len; |
| 409 | |
| 410 | end = csr->raw.p + csr->raw.len; |
| 411 | |
| 412 | /* |
| 413 | * signatureAlgorithm AlgorithmIdentifier, |
| 414 | * signature BIT STRING |
| 415 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 416 | if ((ret = mbedtls_x509_get_alg(&p, end, &csr->sig_oid, &sig_params)) != 0) { |
| 417 | mbedtls_x509_csr_free(csr); |
| 418 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 426 | } |
| 427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | if ((ret = mbedtls_x509_get_sig(&p, end, &csr->sig)) != 0) { |
| 429 | mbedtls_x509_csr_free(csr); |
| 430 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 440 | } |
| 441 | |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 442 | /* |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 443 | * Parse a CSR in DER format |
| 444 | */ |
| 445 | int 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 | */ |
| 454 | int mbedtls_x509_csr_parse_der_with_ext_cb(mbedtls_x509_csr *csr, |
Matthias Schulz | edc32ea | 2023-10-19 16:09:08 +0200 | [diff] [blame^] | 455 | const unsigned char *buf, size_t buflen, |
| 456 | mbedtls_x509_csr_ext_cb_t cb, |
| 457 | void *p_ctx) |
Matthias Schulz | ab40822 | 2023-10-18 13:20:59 +0200 | [diff] [blame] | 458 | { |
| 459 | return mbedtls_x509_csr_parse_der_internal(csr, buf, buflen, cb, p_ctx); |
| 460 | } |
| 461 | |
| 462 | /* |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 463 | * Parse a CSR, allowing for PEM or raw DER encoding |
| 464 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | 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] | 466 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 467 | #if defined(MBEDTLS_PEM_PARSE_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 468 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 469 | size_t use_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 470 | mbedtls_pem_context pem; |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 471 | #endif |
| 472 | |
| 473 | /* |
| 474 | * Check for valid input |
| 475 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | if (csr == NULL || buf == NULL || buflen == 0) { |
| 477 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 478 | } |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 479 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 480 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 481 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | 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 Butcher | 0488ce6 | 2018-09-30 15:36:50 +0100 | [diff] [blame] | 493 | } |
Simon Butcher | e1660af | 2018-10-07 17:48:37 +0100 | [diff] [blame] | 494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | if (ret == 0) { |
Philippe Antoine | c03059d | 2018-06-14 07:35:11 +0200 | [diff] [blame] | 496 | /* |
| 497 | * Was PEM encoded, parse the result |
| 498 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 499 | ret = mbedtls_x509_csr_parse_der(csr, pem.buf, pem.buflen); |
Simon Butcher | 0488ce6 | 2018-09-30 15:36:50 +0100 | [diff] [blame] | 500 | } |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 502 | mbedtls_pem_free(&pem); |
| 503 | if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 504 | return ret; |
| 505 | } |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 506 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | #endif /* MBEDTLS_PEM_PARSE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 508 | return mbedtls_x509_csr_parse_der(csr, buf, buflen); |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 509 | } |
| 510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 511 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 512 | /* |
| 513 | * Load a CSR into the structure |
| 514 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 515 | 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] | 516 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 517 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 518 | size_t n; |
| 519 | unsigned char *buf; |
| 520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 521 | if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) { |
| 522 | return ret; |
| 523 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | ret = mbedtls_x509_csr_parse(csr, buf, n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 526 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 527 | mbedtls_zeroize_and_free(buf, n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 530 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 531 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 532 | |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 533 | #if !defined(MBEDTLS_X509_REMOVE_INFO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 534 | #define BEFORE_COLON 14 |
| 535 | #define BC "14" |
| 536 | /* |
| 537 | * Return an informational string about the CSR. |
| 538 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 539 | int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix, |
| 540 | const mbedtls_x509_csr *csr) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 541 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 542 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 543 | size_t n; |
| 544 | char *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 545 | char key_size_str[BEFORE_COLON]; |
| 546 | |
| 547 | p = buf; |
| 548 | n = size; |
| 549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 550 | ret = mbedtls_snprintf(p, n, "%sCSR version : %d", |
| 551 | prefix, csr->version); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 552 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 554 | ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 555 | MBEDTLS_X509_SAFE_SNPRINTF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 556 | ret = mbedtls_x509_dn_gets(p, n, &csr->subject); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 557 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 559 | ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 560 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | ret = mbedtls_x509_sig_alg_gets(p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md, |
| 563 | csr->sig_opts); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 564 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 566 | if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON, |
| 567 | mbedtls_pk_get_name(&csr->pk))) != 0) { |
| 568 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 571 | 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é-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 573 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 574 | |
Przemek Stekiel | cbaf316 | 2023-01-12 12:58:02 +0100 | [diff] [blame] | 575 | /* |
| 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 Stekiel | 21c3728 | 2023-01-16 08:47:49 +0100 | [diff] [blame] | 583 | if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n, |
| 584 | &csr->subject_alt_names, |
| 585 | prefix)) != 0) { |
Przemek Stekiel | cbaf316 | 2023-01-12 12:58:02 +0100 | [diff] [blame] | 586 | 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 Stekiel | 21c3728 | 2023-01-16 08:47:49 +0100 | [diff] [blame] | 594 | if ((ret = mbedtls_x509_info_cert_type(&p, &n, csr->ns_cert_type)) != 0) { |
Przemek Stekiel | cbaf316 | 2023-01-12 12:58:02 +0100 | [diff] [blame] | 595 | 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 Stekiel | 21c3728 | 2023-01-16 08:47:49 +0100 | [diff] [blame] | 603 | if ((ret = mbedtls_x509_info_key_usage(&p, &n, csr->key_usage)) != 0) { |
Przemek Stekiel | cbaf316 | 2023-01-12 12:58:02 +0100 | [diff] [blame] | 604 | 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 Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 613 | return (int) (size - n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 614 | } |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 615 | #endif /* MBEDTLS_X509_REMOVE_INFO */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 616 | |
| 617 | /* |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 618 | * Initialize a CSR |
| 619 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 620 | void mbedtls_x509_csr_init(mbedtls_x509_csr *csr) |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 621 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 622 | memset(csr, 0, sizeof(mbedtls_x509_csr)); |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 626 | * Unallocate all CSR data |
| 627 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 628 | void mbedtls_x509_csr_free(mbedtls_x509_csr *csr) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 629 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 630 | if (csr == NULL) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 631 | return; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 632 | } |
| 633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 634 | 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 Stekiel | a468768 | 2023-01-24 15:19:47 +0100 | [diff] [blame] | 641 | mbedtls_asn1_sequence_free(csr->subject_alt_names.next); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 642 | |
| 643 | if (csr->raw.p != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 644 | mbedtls_zeroize_and_free(csr->raw.p, csr->raw.len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | mbedtls_platform_zeroize(csr, sizeof(mbedtls_x509_csr)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 648 | } |
| 649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 650 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |