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 | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 45 | */ |
| 46 | /* |
| 47 | * The ITU-T X.509 standard defines a certificate format for PKI. |
| 48 | * |
Manuel Pégourié-Gonnard | 1c082f3 | 2014-06-12 22:34:55 +0200 | [diff] [blame] | 49 | * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs) |
| 50 | * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs) |
| 51 | * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 52 | * |
| 53 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf |
| 54 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf |
| 55 | */ |
| 56 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 58 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 59 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 61 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 62 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 63 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 64 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 65 | #include "mbedtls/x509_csr.h" |
| 66 | #include "mbedtls/oid.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 67 | #include "mbedtls/platform_util.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 68 | |
| 69 | #include <string.h> |
| 70 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 72 | #include "mbedtls/pem.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 73 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 74 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 76 | #include "mbedtls/platform.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 77 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 78 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 981732b | 2015-02-17 15:46:45 +0000 | [diff] [blame] | 79 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | #define mbedtls_free free |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 81 | #define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | #define mbedtls_snprintf snprintf |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 83 | #endif |
| 84 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 86 | #include <stdio.h> |
| 87 | #endif |
| 88 | |
| 89 | /* |
| 90 | * Version ::= INTEGER { v1(0) } |
| 91 | */ |
| 92 | static int x509_csr_get_version( unsigned char **p, |
| 93 | const unsigned char *end, |
| 94 | int *ver ) |
| 95 | { |
| 96 | int ret; |
| 97 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 99 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 101 | { |
| 102 | *ver = 0; |
| 103 | return( 0 ); |
| 104 | } |
| 105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | return( MBEDTLS_ERR_X509_INVALID_VERSION + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | return( 0 ); |
| 110 | } |
| 111 | |
| 112 | /* |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 113 | * Parse a CSR in DER format |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 114 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr, |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 116 | const unsigned char *buf, size_t buflen ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 117 | { |
| 118 | int ret; |
| 119 | size_t len; |
| 120 | unsigned char *p, *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | mbedtls_x509_buf sig_params; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 122 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | memset( &sig_params, 0, sizeof( mbedtls_x509_buf ) ); |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 124 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 125 | /* |
| 126 | * Check for valid input |
| 127 | */ |
Nicholas Wilson | 42d47f0 | 2016-04-13 11:53:27 +0100 | [diff] [blame] | 128 | if( csr == NULL || buf == NULL || buflen == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 130 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | mbedtls_x509_csr_init( csr ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 132 | |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 133 | /* |
| 134 | * first copy the raw DER data |
| 135 | */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 136 | p = mbedtls_calloc( 1, len = buflen ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 137 | |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 138 | if( p == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 139 | return( MBEDTLS_ERR_X509_ALLOC_FAILED ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 140 | |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 141 | memcpy( p, buf, buflen ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 142 | |
| 143 | csr->raw.p = p; |
| 144 | csr->raw.len = len; |
| 145 | end = p + len; |
| 146 | |
| 147 | /* |
| 148 | * CertificationRequest ::= SEQUENCE { |
| 149 | * certificationRequestInfo CertificationRequestInfo, |
| 150 | * signatureAlgorithm AlgorithmIdentifier, |
| 151 | * signature BIT STRING |
| 152 | * } |
| 153 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 155 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 156 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 157 | mbedtls_x509_csr_free( csr ); |
| 158 | return( MBEDTLS_ERR_X509_INVALID_FORMAT ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | if( len != (size_t) ( end - p ) ) |
| 162 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | mbedtls_x509_csr_free( csr ); |
| 164 | return( MBEDTLS_ERR_X509_INVALID_FORMAT + |
| 165 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
| 169 | * CertificationRequestInfo ::= SEQUENCE { |
| 170 | */ |
| 171 | csr->cri.p = p; |
| 172 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 174 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 175 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | mbedtls_x509_csr_free( csr ); |
| 177 | return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | end = p + len; |
| 181 | csr->cri.len = end - csr->cri.p; |
| 182 | |
| 183 | /* |
| 184 | * Version ::= INTEGER { v1(0) } |
| 185 | */ |
| 186 | if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 ) |
| 187 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | mbedtls_x509_csr_free( csr ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 189 | return( ret ); |
| 190 | } |
| 191 | |
Andres AG | 2e3ddfa | 2017-02-17 13:54:43 +0000 | [diff] [blame] | 192 | if( csr->version != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 193 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | mbedtls_x509_csr_free( csr ); |
| 195 | return( MBEDTLS_ERR_X509_UNKNOWN_VERSION ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Andres AG | 2e3ddfa | 2017-02-17 13:54:43 +0000 | [diff] [blame] | 198 | csr->version++; |
| 199 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 200 | /* |
| 201 | * subject Name |
| 202 | */ |
| 203 | csr->subject_raw.p = p; |
| 204 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 205 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 206 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 207 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | mbedtls_x509_csr_free( csr ); |
| 209 | return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | if( ( ret = mbedtls_x509_get_name( &p, p + len, &csr->subject ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 213 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 214 | mbedtls_x509_csr_free( csr ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 215 | return( ret ); |
| 216 | } |
| 217 | |
| 218 | csr->subject_raw.len = p - csr->subject_raw.p; |
| 219 | |
| 220 | /* |
| 221 | * subjectPKInfo SubjectPublicKeyInfo |
| 222 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &csr->pk ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 224 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | mbedtls_x509_csr_free( csr ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 226 | return( ret ); |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * attributes [0] Attributes |
Manuel Pégourié-Gonnard | 986bbf2 | 2016-02-24 14:36:05 +0000 | [diff] [blame] | 231 | * |
| 232 | * The list of possible attributes is open-ended, though RFC 2985 |
| 233 | * (PKCS#9) defines a few in section 5.4. We currently don't support any, |
| 234 | * so we just ignore them. This is a safe thing to do as the worst thing |
| 235 | * that could happen is that we issue a certificate that does not match |
| 236 | * the requester's expectations - this cannot cause a violation of our |
| 237 | * signature policies. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 238 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 240 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 241 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | mbedtls_x509_csr_free( csr ); |
| 243 | return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 244 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 245 | |
| 246 | p += len; |
| 247 | |
| 248 | end = csr->raw.p + csr->raw.len; |
| 249 | |
| 250 | /* |
| 251 | * signatureAlgorithm AlgorithmIdentifier, |
| 252 | * signature BIT STRING |
| 253 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | if( ( ret = mbedtls_x509_get_alg( &p, end, &csr->sig_oid, &sig_params ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 255 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 256 | mbedtls_x509_csr_free( csr ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 257 | return( ret ); |
| 258 | } |
| 259 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 260 | if( ( ret = mbedtls_x509_get_sig_alg( &csr->sig_oid, &sig_params, |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 261 | &csr->sig_md, &csr->sig_pk, |
| 262 | &csr->sig_opts ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 263 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 264 | mbedtls_x509_csr_free( csr ); |
| 265 | return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 268 | if( ( ret = mbedtls_x509_get_sig( &p, end, &csr->sig ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 269 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | mbedtls_x509_csr_free( csr ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 271 | return( ret ); |
| 272 | } |
| 273 | |
| 274 | if( p != end ) |
| 275 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | mbedtls_x509_csr_free( csr ); |
| 277 | return( MBEDTLS_ERR_X509_INVALID_FORMAT + |
| 278 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | return( 0 ); |
| 282 | } |
| 283 | |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 284 | /* |
| 285 | * Parse a CSR, allowing for PEM or raw DER encoding |
| 286 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 287 | 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] | 288 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | #if defined(MBEDTLS_PEM_PARSE_C) |
Andres AG | 0368cb7 | 2016-12-07 15:05:53 +0000 | [diff] [blame] | 290 | int ret; |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 291 | size_t use_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 292 | mbedtls_pem_context pem; |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 293 | #endif |
| 294 | |
| 295 | /* |
| 296 | * Check for valid input |
| 297 | */ |
Nicholas Wilson | 42d47f0 | 2016-04-13 11:53:27 +0100 | [diff] [blame] | 298 | if( csr == NULL || buf == NULL || buflen == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 300 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 302 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Philippe Antoine | 21f73b5 | 2018-06-20 08:13:24 +0200 | [diff] [blame] | 303 | if( buf[buflen - 1] == '\0' ) |
| 304 | { |
Philippe Antoine | c03059d | 2018-06-14 07:35:11 +0200 | [diff] [blame] | 305 | mbedtls_pem_init( &pem ); |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 306 | ret = mbedtls_pem_read_buffer( &pem, |
Simon Butcher | 0488ce6 | 2018-09-30 15:36:50 +0100 | [diff] [blame] | 307 | "-----BEGIN CERTIFICATE REQUEST-----", |
| 308 | "-----END CERTIFICATE REQUEST-----", |
| 309 | buf, NULL, 0, &use_len ); |
Simon Butcher | e1660af | 2018-10-07 17:48:37 +0100 | [diff] [blame] | 310 | if( ret == MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
Simon Butcher | 0488ce6 | 2018-09-30 15:36:50 +0100 | [diff] [blame] | 311 | { |
| 312 | ret = mbedtls_pem_read_buffer( &pem, |
| 313 | "-----BEGIN NEW CERTIFICATE REQUEST-----", |
| 314 | "-----END NEW CERTIFICATE REQUEST-----", |
| 315 | buf, NULL, 0, &use_len ); |
| 316 | } |
Simon Butcher | e1660af | 2018-10-07 17:48:37 +0100 | [diff] [blame] | 317 | |
Philippe Antoine | c03059d | 2018-06-14 07:35:11 +0200 | [diff] [blame] | 318 | if( ret == 0 ) |
Simon Butcher | 0488ce6 | 2018-09-30 15:36:50 +0100 | [diff] [blame] | 319 | { |
Philippe Antoine | c03059d | 2018-06-14 07:35:11 +0200 | [diff] [blame] | 320 | /* |
| 321 | * Was PEM encoded, parse the result |
| 322 | */ |
| 323 | ret = mbedtls_x509_csr_parse_der( csr, pem.buf, pem.buflen ); |
Simon Butcher | 0488ce6 | 2018-09-30 15:36:50 +0100 | [diff] [blame] | 324 | } |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 325 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 326 | mbedtls_pem_free( &pem ); |
Philippe Antoine | c03059d | 2018-06-14 07:35:11 +0200 | [diff] [blame] | 327 | if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
| 328 | return( ret ); |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 329 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 330 | #endif /* MBEDTLS_PEM_PARSE_C */ |
| 331 | return( mbedtls_x509_csr_parse_der( csr, buf, buflen ) ); |
Manuel Pégourié-Gonnard | f3b4724 | 2014-06-16 18:06:48 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 334 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 335 | /* |
| 336 | * Load a CSR into the structure |
| 337 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 338 | 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] | 339 | { |
| 340 | int ret; |
| 341 | size_t n; |
| 342 | unsigned char *buf; |
| 343 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 344 | if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 345 | return( ret ); |
| 346 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | ret = mbedtls_x509_csr_parse( csr, buf, n ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 348 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 349 | mbedtls_platform_zeroize( buf, n ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | mbedtls_free( buf ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 351 | |
| 352 | return( ret ); |
| 353 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 354 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 355 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 356 | #define BEFORE_COLON 14 |
| 357 | #define BC "14" |
| 358 | /* |
| 359 | * Return an informational string about the CSR. |
| 360 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 361 | int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix, |
| 362 | const mbedtls_x509_csr *csr ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 363 | { |
| 364 | int ret; |
| 365 | size_t n; |
| 366 | char *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 367 | char key_size_str[BEFORE_COLON]; |
| 368 | |
| 369 | p = buf; |
| 370 | n = size; |
| 371 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 372 | ret = mbedtls_snprintf( p, n, "%sCSR version : %d", |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 373 | prefix, csr->version ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 374 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 375 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 376 | ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 377 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 378 | ret = mbedtls_x509_dn_gets( p, n, &csr->subject ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 379 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 380 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 381 | ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 382 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 383 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 384 | ret = mbedtls_x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md, |
Manuel Pégourié-Gonnard | bf696d0 | 2014-06-05 17:07:30 +0200 | [diff] [blame] | 385 | csr->sig_opts ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 386 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 387 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 388 | if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON, |
| 389 | mbedtls_pk_get_name( &csr->pk ) ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 390 | { |
| 391 | return( ret ); |
| 392 | } |
| 393 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 394 | ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str, |
Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 395 | (int) mbedtls_pk_get_bitlen( &csr->pk ) ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 396 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 397 | |
| 398 | return( (int) ( size - n ) ); |
| 399 | } |
| 400 | |
| 401 | /* |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 402 | * Initialize a CSR |
| 403 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 404 | void mbedtls_x509_csr_init( mbedtls_x509_csr *csr ) |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 405 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 406 | memset( csr, 0, sizeof(mbedtls_x509_csr) ); |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 410 | * Unallocate all CSR data |
| 411 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 412 | void mbedtls_x509_csr_free( mbedtls_x509_csr *csr ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 413 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 414 | mbedtls_x509_name *name_cur; |
| 415 | mbedtls_x509_name *name_prv; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 416 | |
| 417 | if( csr == NULL ) |
| 418 | return; |
| 419 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 420 | mbedtls_pk_free( &csr->pk ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 422 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 423 | mbedtls_free( csr->sig_opts ); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 424 | #endif |
| 425 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 426 | name_cur = csr->subject.next; |
| 427 | while( name_cur != NULL ) |
| 428 | { |
| 429 | name_prv = name_cur; |
| 430 | name_cur = name_cur->next; |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 431 | mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 432 | mbedtls_free( name_prv ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | if( csr->raw.p != NULL ) |
| 436 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 437 | mbedtls_platform_zeroize( csr->raw.p, csr->raw.len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 438 | mbedtls_free( csr->raw.p ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 441 | mbedtls_platform_zeroize( csr, sizeof( mbedtls_x509_csr ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 442 | } |
| 443 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 444 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |