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