Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /* |
Manuel Pégourié-Gonnard | 1c082f3 | 2014-06-12 22:34:55 +0200 | [diff] [blame] | 2 | * X.509 common functions for parsing and verification |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 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_USE_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.h" |
| 35 | #include "mbedtls/asn1.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 36 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/oid.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 38 | |
Rich Evans | 36796df | 2015-02-12 18:27:14 +0000 | [diff] [blame] | 39 | #include <stdio.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 40 | #include <string.h> |
| 41 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 43 | #include "mbedtls/pem.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 44 | #endif |
| 45 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 47 | #include "mbedtls/platform.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 48 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 49 | #include <stdio.h> |
| 50 | #include <stdlib.h> |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 51 | #define mbedtls_free free |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 52 | #define mbedtls_calloc calloc |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 53 | #define mbedtls_printf printf |
| 54 | #define mbedtls_snprintf snprintf |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 55 | #endif |
| 56 | |
Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 57 | #if defined(MBEDTLS_HAVE_TIME) |
| 58 | #include "mbedtls/platform_time.h" |
| 59 | #endif |
Nicholas Wilson | 512b4ee | 2017-12-05 12:07:33 +0000 | [diff] [blame] | 60 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 61 | #include "mbedtls/platform_util.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 62 | #include <time.h> |
| 63 | #endif |
| 64 | |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 65 | #define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); } |
| 66 | #define CHECK_RANGE(min, max, val) \ |
| 67 | do \ |
| 68 | { \ |
| 69 | if( ( val ) < ( min ) || ( val ) > ( max ) ) \ |
| 70 | { \ |
| 71 | return( ret ); \ |
| 72 | } \ |
| 73 | } while( 0 ) |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 74 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 75 | /* |
| 76 | * CertificateSerialNumber ::= INTEGER |
| 77 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end, |
| 79 | mbedtls_x509_buf *serial ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 80 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 81 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 82 | |
| 83 | if( ( end - *p ) < 1 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 84 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL, |
| 85 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 86 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) && |
| 88 | **p != MBEDTLS_ASN1_INTEGER ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 89 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL, |
| 90 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 91 | |
| 92 | serial->tag = *(*p)++; |
| 93 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 95 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 96 | |
| 97 | serial->p = *p; |
| 98 | *p += serial->len; |
| 99 | |
| 100 | return( 0 ); |
| 101 | } |
| 102 | |
| 103 | /* Get an algorithm identifier without parameters (eg for signatures) |
| 104 | * |
| 105 | * AlgorithmIdentifier ::= SEQUENCE { |
| 106 | * algorithm OBJECT IDENTIFIER, |
| 107 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 108 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end, |
| 110 | mbedtls_x509_buf *alg ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 111 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 112 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 113 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 115 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 116 | |
| 117 | return( 0 ); |
| 118 | } |
| 119 | |
| 120 | /* |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 121 | * Parse an algorithm identifier with (optional) parameters |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 122 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end, |
| 124 | mbedtls_x509_buf *alg, mbedtls_x509_buf *params ) |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 125 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 126 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 127 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 129 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 130 | |
| 131 | return( 0 ); |
| 132 | } |
| 133 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 135 | /* |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 136 | * HashAlgorithm ::= AlgorithmIdentifier |
| 137 | * |
| 138 | * AlgorithmIdentifier ::= SEQUENCE { |
| 139 | * algorithm OBJECT IDENTIFIER, |
| 140 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 141 | * |
| 142 | * For HashAlgorithm, parameters MUST be NULL or absent. |
| 143 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | static int x509_get_hash_alg( const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg ) |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 145 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 146 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 147 | unsigned char *p; |
| 148 | const unsigned char *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | mbedtls_x509_buf md_oid; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 150 | size_t len; |
| 151 | |
| 152 | /* Make sure we got a SEQUENCE and setup bounds */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 154 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 155 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 156 | |
Andrzej Kurek | feaebc5 | 2020-07-16 04:37:41 -0400 | [diff] [blame] | 157 | p = alg->p; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 158 | end = p + alg->len; |
| 159 | |
| 160 | if( p >= end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 161 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 162 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 163 | |
| 164 | /* Parse md_oid */ |
| 165 | md_oid.tag = *p; |
| 166 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 168 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 169 | |
| 170 | md_oid.p = p; |
| 171 | p += md_oid.len; |
| 172 | |
| 173 | /* Get md_alg from md_oid */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 175 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 176 | |
| 177 | /* Make sure params is absent of NULL */ |
| 178 | if( p == end ) |
| 179 | return( 0 ); |
| 180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 182 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 183 | |
| 184 | if( p != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 185 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 186 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 187 | |
| 188 | return( 0 ); |
| 189 | } |
| 190 | |
| 191 | /* |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 192 | * RSASSA-PSS-params ::= SEQUENCE { |
| 193 | * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier, |
| 194 | * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier, |
| 195 | * saltLength [2] INTEGER DEFAULT 20, |
| 196 | * trailerField [3] INTEGER DEFAULT 1 } |
| 197 | * -- Note that the tags in this Sequence are explicit. |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 198 | * |
| 199 | * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value |
| 200 | * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other |
| 201 | * option. Enfore this at parsing time. |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 202 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 203 | int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params, |
| 204 | mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md, |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 205 | int *salt_len ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 206 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 207 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 208 | unsigned char *p; |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 209 | const unsigned char *end, *end2; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 210 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | mbedtls_x509_buf alg_id, alg_params; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 212 | |
| 213 | /* First set everything to defaults */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 214 | *md_alg = MBEDTLS_MD_SHA1; |
| 215 | *mgf_md = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 216 | *salt_len = 20; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 217 | |
| 218 | /* Make sure params is a SEQUENCE and setup bounds */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 219 | if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 220 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 221 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 222 | |
| 223 | p = (unsigned char *) params->p; |
| 224 | end = p + params->len; |
| 225 | |
| 226 | if( p == end ) |
| 227 | return( 0 ); |
| 228 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 229 | /* |
| 230 | * HashAlgorithm |
| 231 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 233 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 234 | { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 235 | end2 = p + len; |
| 236 | |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 237 | /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 ) |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 239 | return( ret ); |
| 240 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 242 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 243 | |
| 244 | if( p != end2 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 245 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 246 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 247 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 249 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 250 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 251 | if( p == end ) |
| 252 | return( 0 ); |
| 253 | |
| 254 | /* |
| 255 | * MaskGenAlgorithm |
| 256 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 258 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 259 | { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 260 | end2 = p + len; |
| 261 | |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 262 | /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 ) |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 264 | return( ret ); |
| 265 | |
| 266 | /* Only MFG1 is recognised for now */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 267 | if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 268 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE, |
| 269 | MBEDTLS_ERR_OID_NOT_FOUND ) ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 270 | |
| 271 | /* Parse HashAlgorithm */ |
| 272 | if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 ) |
| 273 | return( ret ); |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 274 | |
| 275 | if( p != end2 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 276 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 277 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 278 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 280 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 281 | |
| 282 | if( p == end ) |
| 283 | return( 0 ); |
| 284 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 285 | /* |
| 286 | * salt_len |
| 287 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 288 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 289 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 290 | { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 291 | end2 = p + len; |
| 292 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 294 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 295 | |
| 296 | if( p != end2 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 297 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 298 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 299 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 300 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 301 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 302 | |
| 303 | if( p == end ) |
| 304 | return( 0 ); |
| 305 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 306 | /* |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 307 | * trailer_field (if present, must be 1) |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 308 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 309 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 310 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 311 | { |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 312 | int trailer_field; |
| 313 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 314 | end2 = p + len; |
| 315 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 316 | if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 317 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 318 | |
| 319 | if( p != end2 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 320 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 321 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 322 | |
| 323 | if( trailer_field != 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 324 | return( MBEDTLS_ERR_X509_INVALID_ALG ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 325 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 326 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 327 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 328 | |
| 329 | if( p != end ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 330 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, |
| 331 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 332 | |
| 333 | return( 0 ); |
| 334 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 336 | |
| 337 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 338 | * AttributeTypeAndValue ::= SEQUENCE { |
| 339 | * type AttributeType, |
| 340 | * value AttributeValue } |
| 341 | * |
| 342 | * AttributeType ::= OBJECT IDENTIFIER |
| 343 | * |
| 344 | * AttributeValue ::= ANY DEFINED BY AttributeType |
| 345 | */ |
| 346 | static int x509_get_attr_type_value( unsigned char **p, |
| 347 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 348 | mbedtls_x509_name *cur ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 349 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 350 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 351 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 352 | mbedtls_x509_buf *oid; |
| 353 | mbedtls_x509_buf *val; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 354 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 355 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 356 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 357 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 358 | |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 359 | end = *p + len; |
| 360 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 361 | if( ( end - *p ) < 1 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 362 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, |
| 363 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 364 | |
| 365 | oid = &cur->oid; |
| 366 | oid->tag = **p; |
| 367 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 368 | if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 369 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 370 | |
| 371 | oid->p = *p; |
| 372 | *p += oid->len; |
| 373 | |
| 374 | if( ( end - *p ) < 1 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 375 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, |
| 376 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 377 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 378 | if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING && |
| 379 | **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING && |
| 380 | **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING && |
| 381 | **p != MBEDTLS_ASN1_BIT_STRING ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 382 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, |
| 383 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 384 | |
| 385 | val = &cur->val; |
| 386 | val->tag = *(*p)++; |
| 387 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 388 | if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 389 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 390 | |
| 391 | val->p = *p; |
| 392 | *p += val->len; |
| 393 | |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 394 | if( *p != end ) |
| 395 | { |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 396 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, |
| 397 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 400 | cur->next = NULL; |
| 401 | |
| 402 | return( 0 ); |
| 403 | } |
| 404 | |
| 405 | /* |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 406 | * Name ::= CHOICE { -- only one possibility for now -- |
| 407 | * rdnSequence RDNSequence } |
| 408 | * |
| 409 | * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
| 410 | * |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 411 | * RelativeDistinguishedName ::= |
| 412 | * SET OF AttributeTypeAndValue |
| 413 | * |
| 414 | * AttributeTypeAndValue ::= SEQUENCE { |
| 415 | * type AttributeType, |
| 416 | * value AttributeValue } |
| 417 | * |
| 418 | * AttributeType ::= OBJECT IDENTIFIER |
| 419 | * |
| 420 | * AttributeValue ::= ANY DEFINED BY AttributeType |
Manuel Pégourié-Gonnard | 5d86185 | 2014-10-17 12:41:41 +0200 | [diff] [blame] | 421 | * |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 422 | * The data structure is optimized for the common case where each RDN has only |
| 423 | * one element, which is represented as a list of AttributeTypeAndValue. |
| 424 | * For the general case we still use a flat list, but we mark elements of the |
| 425 | * same set so that they are "merged" together in the functions that consume |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | * this list, eg mbedtls_x509_dn_gets(). |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 427 | * |
David Horstmann | 5ad5e16 | 2022-10-17 18:10:23 +0100 | [diff] [blame] | 428 | * On success, this function may allocate a linked list starting at cur->next |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 429 | * that must later be free'd by the caller using mbedtls_free(). In error |
| 430 | * cases, this function frees all allocated memory internally and the caller |
| 431 | * has no freeing responsibilities. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 432 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 433 | int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end, |
| 434 | mbedtls_x509_name *cur ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 435 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 436 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 5d86185 | 2014-10-17 12:41:41 +0200 | [diff] [blame] | 437 | size_t set_len; |
| 438 | const unsigned char *end_set; |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 439 | mbedtls_x509_name *head = cur; |
| 440 | mbedtls_x509_name *prev, *allocated; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 441 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 442 | /* don't use recursion, we'd risk stack overflow if not optimized */ |
| 443 | while( 1 ) |
| 444 | { |
| 445 | /* |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 446 | * parse SET |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 447 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 448 | if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len, |
| 449 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 ) |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 450 | { |
| 451 | ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ); |
| 452 | goto error; |
| 453 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 454 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 455 | end_set = *p + set_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 456 | |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 457 | while( 1 ) |
| 458 | { |
| 459 | if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 ) |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 460 | goto error; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 461 | |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 462 | if( *p == end_set ) |
| 463 | break; |
| 464 | |
Manuel Pégourié-Gonnard | eecb43c | 2015-05-12 12:56:41 +0200 | [diff] [blame] | 465 | /* Mark this item as being no the only one in a set */ |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 466 | cur->next_merged = 1; |
| 467 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 468 | cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) ); |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 469 | |
| 470 | if( cur->next == NULL ) |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 471 | { |
| 472 | ret = MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 473 | goto error; |
| 474 | } |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 475 | |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 476 | cur = cur->next; |
| 477 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 478 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 479 | /* |
| 480 | * continue until end of SEQUENCE is reached |
| 481 | */ |
| 482 | if( *p == end ) |
| 483 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 484 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 485 | cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 486 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 487 | if( cur->next == NULL ) |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 488 | { |
| 489 | ret = MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 490 | goto error; |
| 491 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 492 | |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 493 | cur = cur->next; |
| 494 | } |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 495 | |
| 496 | error: |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 497 | /* Skip the first element as we did not allocate it */ |
| 498 | allocated = head->next; |
| 499 | |
David Horstmann | 8c176b4 | 2022-10-04 18:12:06 +0100 | [diff] [blame] | 500 | while( allocated != NULL ) |
| 501 | { |
| 502 | prev = allocated; |
| 503 | allocated = allocated->next; |
| 504 | |
| 505 | mbedtls_platform_zeroize( prev, sizeof( *prev ) ); |
| 506 | mbedtls_free( prev ); |
| 507 | } |
| 508 | |
| 509 | mbedtls_platform_zeroize( head, sizeof( *head ) ); |
| 510 | |
David Horstmann | 670a993 | 2022-10-18 18:11:13 +0100 | [diff] [blame^] | 511 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 514 | static int x509_parse_int( unsigned char **p, size_t n, int *res ) |
| 515 | { |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 516 | *res = 0; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 517 | |
| 518 | for( ; n > 0; --n ) |
| 519 | { |
| 520 | if( ( **p < '0') || ( **p > '9' ) ) |
| 521 | return ( MBEDTLS_ERR_X509_INVALID_DATE ); |
| 522 | |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 523 | *res *= 10; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 524 | *res += ( *(*p)++ - '0' ); |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 525 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 526 | |
| 527 | return( 0 ); |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 530 | static int x509_date_is_valid(const mbedtls_x509_time *t ) |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 531 | { |
| 532 | int ret = MBEDTLS_ERR_X509_INVALID_DATE; |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 533 | int month_len; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 534 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 535 | CHECK_RANGE( 0, 9999, t->year ); |
| 536 | CHECK_RANGE( 0, 23, t->hour ); |
| 537 | CHECK_RANGE( 0, 59, t->min ); |
| 538 | CHECK_RANGE( 0, 59, t->sec ); |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 539 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 540 | switch( t->mon ) |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 541 | { |
| 542 | case 1: case 3: case 5: case 7: case 8: case 10: case 12: |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 543 | month_len = 31; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 544 | break; |
| 545 | case 4: case 6: case 9: case 11: |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 546 | month_len = 30; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 547 | break; |
| 548 | case 2: |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 549 | if( ( !( t->year % 4 ) && t->year % 100 ) || |
| 550 | !( t->year % 400 ) ) |
| 551 | month_len = 29; |
| 552 | else |
| 553 | month_len = 28; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 554 | break; |
| 555 | default: |
| 556 | return( ret ); |
| 557 | } |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 558 | CHECK_RANGE( 1, month_len, t->day ); |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 559 | |
| 560 | return( 0 ); |
| 561 | } |
| 562 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 563 | /* |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 564 | * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4) |
| 565 | * field. |
| 566 | */ |
| 567 | static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen, |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 568 | mbedtls_x509_time *tm ) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 569 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 570 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 571 | |
| 572 | /* |
| 573 | * Minimum length is 10 or 12 depending on yearlen |
| 574 | */ |
| 575 | if ( len < yearlen + 8 ) |
| 576 | return ( MBEDTLS_ERR_X509_INVALID_DATE ); |
| 577 | len -= yearlen + 8; |
| 578 | |
| 579 | /* |
| 580 | * Parse year, month, day, hour, minute |
| 581 | */ |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 582 | CHECK( x509_parse_int( p, yearlen, &tm->year ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 583 | if ( 2 == yearlen ) |
| 584 | { |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 585 | if ( tm->year < 50 ) |
| 586 | tm->year += 100; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 587 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 588 | tm->year += 1900; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 591 | CHECK( x509_parse_int( p, 2, &tm->mon ) ); |
| 592 | CHECK( x509_parse_int( p, 2, &tm->day ) ); |
| 593 | CHECK( x509_parse_int( p, 2, &tm->hour ) ); |
| 594 | CHECK( x509_parse_int( p, 2, &tm->min ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 595 | |
| 596 | /* |
| 597 | * Parse seconds if present |
| 598 | */ |
| 599 | if ( len >= 2 ) |
| 600 | { |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 601 | CHECK( x509_parse_int( p, 2, &tm->sec ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 602 | len -= 2; |
| 603 | } |
| 604 | else |
| 605 | return ( MBEDTLS_ERR_X509_INVALID_DATE ); |
| 606 | |
| 607 | /* |
| 608 | * Parse trailing 'Z' if present |
| 609 | */ |
| 610 | if ( 1 == len && 'Z' == **p ) |
| 611 | { |
| 612 | (*p)++; |
| 613 | len--; |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * We should have parsed all characters at this point |
| 618 | */ |
| 619 | if ( 0 != len ) |
| 620 | return ( MBEDTLS_ERR_X509_INVALID_DATE ); |
| 621 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 622 | CHECK( x509_date_is_valid( tm ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 623 | |
| 624 | return ( 0 ); |
| 625 | } |
| 626 | |
| 627 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 628 | * Time ::= CHOICE { |
| 629 | * utcTime UTCTime, |
| 630 | * generalTime GeneralizedTime } |
| 631 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 632 | int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end, |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 633 | mbedtls_x509_time *tm ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 634 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 635 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 636 | size_t len, year_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 637 | unsigned char tag; |
| 638 | |
| 639 | if( ( end - *p ) < 1 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 640 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, |
| 641 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 642 | |
| 643 | tag = **p; |
| 644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 645 | if( tag == MBEDTLS_ASN1_UTC_TIME ) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 646 | year_len = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 647 | else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME ) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 648 | year_len = 4; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 649 | else |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 650 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, |
| 651 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 652 | |
| 653 | (*p)++; |
| 654 | ret = mbedtls_asn1_get_len( p, end, &len ); |
| 655 | |
| 656 | if( ret != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 657 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 658 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 659 | return x509_parse_time( p, len, year_len, tm ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 660 | } |
| 661 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 662 | int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 663 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 664 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 665 | size_t len; |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 666 | int tag_type; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 667 | |
| 668 | if( ( end - *p ) < 1 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 669 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE, |
| 670 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 671 | |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 672 | tag_type = **p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 673 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 674 | if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 675 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 676 | |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 677 | sig->tag = tag_type; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 678 | sig->len = len; |
| 679 | sig->p = *p; |
| 680 | |
| 681 | *p += len; |
| 682 | |
| 683 | return( 0 ); |
| 684 | } |
| 685 | |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 686 | /* |
| 687 | * Get signature algorithm from alg OID and optional parameters |
| 688 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 689 | int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params, |
| 690 | mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg, |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 691 | void **sig_opts ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 692 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 693 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 694 | |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 695 | if( *sig_opts != NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 696 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 697 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 698 | if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 699 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 700 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 701 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 702 | if( *pk_alg == MBEDTLS_PK_RSASSA_PSS ) |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 703 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 704 | mbedtls_pk_rsassa_pss_options *pss_opts; |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 705 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 706 | pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) ); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 707 | if( pss_opts == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 708 | return( MBEDTLS_ERR_X509_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 709 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 710 | ret = mbedtls_x509_get_rsassa_pss_params( sig_params, |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 711 | md_alg, |
| 712 | &pss_opts->mgf1_hash_id, |
| 713 | &pss_opts->expected_salt_len ); |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 714 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 715 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 716 | mbedtls_free( pss_opts ); |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 717 | return( ret ); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 718 | } |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 719 | |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 720 | *sig_opts = (void *) pss_opts; |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 721 | } |
| 722 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 723 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 724 | { |
| 725 | /* Make sure parameters are absent or NULL */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 726 | if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) || |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 727 | sig_params->len != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 728 | return( MBEDTLS_ERR_X509_INVALID_ALG ); |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 729 | } |
| 730 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 731 | return( 0 ); |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * X.509 Extensions (No parsing of extensions, pointer should |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 736 | * be either manually updated or extensions should be parsed!) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 737 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 738 | int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end, |
Hanno Becker | 6ccfb18 | 2019-02-12 11:52:10 +0000 | [diff] [blame] | 739 | mbedtls_x509_buf *ext, int tag ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 740 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 741 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 742 | size_t len; |
| 743 | |
Hanno Becker | 3cddba8 | 2019-02-11 14:33:36 +0000 | [diff] [blame] | 744 | /* Extension structure use EXPLICIT tagging. That is, the actual |
| 745 | * `Extensions` structure is wrapped by a tag-length pair using |
| 746 | * the respective context-specific tag. */ |
Hanno Becker | 6ccfb18 | 2019-02-12 11:52:10 +0000 | [diff] [blame] | 747 | ret = mbedtls_asn1_get_tag( p, end, &ext->len, |
| 748 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag ); |
| 749 | if( ret != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 750 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 751 | |
Hanno Becker | 6ccfb18 | 2019-02-12 11:52:10 +0000 | [diff] [blame] | 752 | ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag; |
| 753 | ext->p = *p; |
| 754 | end = *p + ext->len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 755 | |
| 756 | /* |
| 757 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 758 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 759 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 760 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 761 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 762 | |
| 763 | if( end != *p + len ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 764 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 765 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 766 | |
| 767 | return( 0 ); |
| 768 | } |
| 769 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 770 | /* |
| 771 | * Store the name in printable form into buf; no more |
| 772 | * than size characters will be written |
| 773 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 774 | int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 775 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 776 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Werner Lewis | 02c9d3b | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 777 | size_t i, j, n; |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 778 | unsigned char c, merge = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 779 | const mbedtls_x509_name *name; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 780 | const char *short_name = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 781 | char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 782 | |
| 783 | memset( s, 0, sizeof( s ) ); |
| 784 | |
| 785 | name = dn; |
| 786 | p = buf; |
| 787 | n = size; |
| 788 | |
| 789 | while( name != NULL ) |
| 790 | { |
| 791 | if( !name->oid.p ) |
| 792 | { |
| 793 | name = name->next; |
| 794 | continue; |
| 795 | } |
| 796 | |
| 797 | if( name != dn ) |
| 798 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 799 | ret = mbedtls_snprintf( p, n, merge ? " + " : ", " ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 800 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 803 | ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 804 | |
| 805 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 806 | ret = mbedtls_snprintf( p, n, "%s=", short_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 807 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 808 | ret = mbedtls_snprintf( p, n, "\?\?=" ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 809 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 810 | |
Werner Lewis | 02c9d3b | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 811 | for( i = 0, j = 0; i < name->val.len; i++, j++ ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 812 | { |
Werner Lewis | 02c9d3b | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 813 | if( j >= sizeof( s ) - 1 ) |
| 814 | return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 815 | |
| 816 | c = name->val.p[i]; |
Werner Lewis | 02c9d3b | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 817 | // Special characters requiring escaping, RFC 1779 |
| 818 | if( c && strchr( ",=+<>#;\"\\", c ) ) |
| 819 | { |
| 820 | if( j + 1 >= sizeof( s ) - 1 ) |
Werner Lewis | 1421efa | 2022-06-27 12:01:22 +0100 | [diff] [blame] | 821 | return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL ); |
Werner Lewis | 02c9d3b | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 822 | s[j++] = '\\'; |
| 823 | } |
Koh M. Nakagawa | 46b8782 | 2020-05-16 10:08:09 +0900 | [diff] [blame] | 824 | if( c < 32 || c >= 127 ) |
Werner Lewis | 02c9d3b | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 825 | s[j] = '?'; |
| 826 | else s[j] = c; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 827 | } |
Werner Lewis | 02c9d3b | 2022-05-20 12:48:46 +0100 | [diff] [blame] | 828 | s[j] = '\0'; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 829 | ret = mbedtls_snprintf( p, n, "%s", s ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 830 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 831 | |
| 832 | merge = name->next_merged; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 833 | name = name->next; |
| 834 | } |
| 835 | |
| 836 | return( (int) ( size - n ) ); |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * Store the serial in printable form into buf; no more |
| 841 | * than size characters will be written |
| 842 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 843 | int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 844 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 845 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 846 | size_t i, n, nr; |
| 847 | char *p; |
| 848 | |
| 849 | p = buf; |
| 850 | n = size; |
| 851 | |
| 852 | nr = ( serial->len <= 32 ) |
| 853 | ? serial->len : 28; |
| 854 | |
| 855 | for( i = 0; i < nr; i++ ) |
| 856 | { |
| 857 | if( i == 0 && nr > 1 && serial->p[i] == 0x0 ) |
| 858 | continue; |
| 859 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 860 | ret = mbedtls_snprintf( p, n, "%02X%s", |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 861 | serial->p[i], ( i < nr - 1 ) ? ":" : "" ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 862 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | if( nr != serial->len ) |
| 866 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 867 | ret = mbedtls_snprintf( p, n, "...." ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 868 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | return( (int) ( size - n ) ); |
| 872 | } |
| 873 | |
| 874 | /* |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 875 | * Helper for writing signature algorithms |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 876 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 877 | int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid, |
| 878 | mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 879 | const void *sig_opts ) |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 880 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 881 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 882 | char *p = buf; |
| 883 | size_t n = size; |
| 884 | const char *desc = NULL; |
| 885 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 886 | ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc ); |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 887 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 888 | ret = mbedtls_snprintf( p, n, "???" ); |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 889 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 890 | ret = mbedtls_snprintf( p, n, "%s", desc ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 891 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 892 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 893 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 894 | if( pk_alg == MBEDTLS_PK_RSASSA_PSS ) |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 895 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 896 | const mbedtls_pk_rsassa_pss_options *pss_opts; |
| 897 | const mbedtls_md_info_t *md_info, *mgf_md_info; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 898 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 899 | pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 900 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 901 | md_info = mbedtls_md_info_from_type( md_alg ); |
| 902 | mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id ); |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 903 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 904 | ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)", |
| 905 | md_info ? mbedtls_md_get_name( md_info ) : "???", |
| 906 | mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???", |
Kenneth Soerensen | 518d435 | 2020-04-01 17:22:45 +0200 | [diff] [blame] | 907 | (unsigned int) pss_opts->expected_salt_len ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 908 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 909 | } |
| 910 | #else |
| 911 | ((void) pk_alg); |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 912 | ((void) md_alg); |
| 913 | ((void) sig_opts); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 914 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 915 | |
Sander Niemeijer | ef5087d | 2014-08-16 12:45:52 +0200 | [diff] [blame] | 916 | return( (int)( size - n ) ); |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 920 | * Helper for writing "RSA key size", "EC key size", etc |
| 921 | */ |
Manuel Pégourié-Gonnard | fb317c5 | 2015-06-18 16:25:56 +0200 | [diff] [blame] | 922 | int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 923 | { |
| 924 | char *p = buf; |
Manuel Pégourié-Gonnard | fb317c5 | 2015-06-18 16:25:56 +0200 | [diff] [blame] | 925 | size_t n = buf_size; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 926 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 927 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 928 | ret = mbedtls_snprintf( p, n, "%s key size", name ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 929 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 930 | |
| 931 | return( 0 ); |
| 932 | } |
| 933 | |
Manuel Pégourié-Gonnard | 60c793b | 2015-06-18 20:52:58 +0200 | [diff] [blame] | 934 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 935 | /* |
| 936 | * Set the time structure to the current time. |
| 937 | * Return 0 on success, non-zero on failure. |
| 938 | */ |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 939 | static int x509_get_current_time( mbedtls_x509_time *now ) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 940 | { |
Nicholas Wilson | 512b4ee | 2017-12-05 12:07:33 +0000 | [diff] [blame] | 941 | struct tm *lt, tm_buf; |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 942 | mbedtls_time_t tt; |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 943 | int ret = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 944 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 945 | tt = mbedtls_time( NULL ); |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 946 | lt = mbedtls_platform_gmtime_r( &tt, &tm_buf ); |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 947 | |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 948 | if( lt == NULL ) |
| 949 | ret = -1; |
| 950 | else |
| 951 | { |
| 952 | now->year = lt->tm_year + 1900; |
| 953 | now->mon = lt->tm_mon + 1; |
| 954 | now->day = lt->tm_mday; |
| 955 | now->hour = lt->tm_hour; |
| 956 | now->min = lt->tm_min; |
| 957 | now->sec = lt->tm_sec; |
| 958 | } |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 959 | |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 960 | return( ret ); |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 961 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 962 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 963 | /* |
| 964 | * Return 0 if before <= after, 1 otherwise |
| 965 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 966 | static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after ) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 967 | { |
| 968 | if( before->year > after->year ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 969 | return( 1 ); |
| 970 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 971 | if( before->year == after->year && |
| 972 | before->mon > after->mon ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 973 | return( 1 ); |
| 974 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 975 | if( before->year == after->year && |
| 976 | before->mon == after->mon && |
| 977 | before->day > after->day ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 978 | return( 1 ); |
| 979 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 980 | if( before->year == after->year && |
| 981 | before->mon == after->mon && |
| 982 | before->day == after->day && |
| 983 | before->hour > after->hour ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 984 | return( 1 ); |
| 985 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 986 | if( before->year == after->year && |
| 987 | before->mon == after->mon && |
| 988 | before->day == after->day && |
| 989 | before->hour == after->hour && |
| 990 | before->min > after->min ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 991 | return( 1 ); |
| 992 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 993 | if( before->year == after->year && |
| 994 | before->mon == after->mon && |
| 995 | before->day == after->day && |
| 996 | before->hour == after->hour && |
| 997 | before->min == after->min && |
| 998 | before->sec > after->sec ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 999 | return( 1 ); |
| 1000 | |
| 1001 | return( 0 ); |
| 1002 | } |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1003 | |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1004 | int mbedtls_x509_time_is_past( const mbedtls_x509_time *to ) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1005 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1006 | mbedtls_x509_time now; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1007 | |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 1008 | if( x509_get_current_time( &now ) != 0 ) |
Manuel Pégourié-Gonnard | e7e8984 | 2015-06-22 19:15:32 +0200 | [diff] [blame] | 1009 | return( 1 ); |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1010 | |
| 1011 | return( x509_check_time( &now, to ) ); |
| 1012 | } |
| 1013 | |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1014 | int mbedtls_x509_time_is_future( const mbedtls_x509_time *from ) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1015 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1016 | mbedtls_x509_time now; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1017 | |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 1018 | if( x509_get_current_time( &now ) != 0 ) |
Manuel Pégourié-Gonnard | e7e8984 | 2015-06-22 19:15:32 +0200 | [diff] [blame] | 1019 | return( 1 ); |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1020 | |
| 1021 | return( x509_check_time( from, &now ) ); |
| 1022 | } |
| 1023 | |
Manuel Pégourié-Gonnard | 60c793b | 2015-06-18 20:52:58 +0200 | [diff] [blame] | 1024 | #else /* MBEDTLS_HAVE_TIME_DATE */ |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1025 | |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1026 | int mbedtls_x509_time_is_past( const mbedtls_x509_time *to ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1027 | { |
| 1028 | ((void) to); |
| 1029 | return( 0 ); |
| 1030 | } |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1031 | |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1032 | int mbedtls_x509_time_is_future( const mbedtls_x509_time *from ) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1033 | { |
| 1034 | ((void) from); |
| 1035 | return( 0 ); |
| 1036 | } |
Manuel Pégourié-Gonnard | 60c793b | 2015-06-18 20:52:58 +0200 | [diff] [blame] | 1037 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1038 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1039 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1040 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 1041 | #include "mbedtls/x509_crt.h" |
| 1042 | #include "mbedtls/certs.h" |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1043 | |
| 1044 | /* |
| 1045 | * Checkup routine |
| 1046 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1047 | int mbedtls_x509_self_test( int verbose ) |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1048 | { |
Junhwan Park | 39bdab7 | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1049 | int ret = 0; |
Gilles Peskine | 750c353 | 2017-05-05 18:56:30 +0200 | [diff] [blame] | 1050 | #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1051 | uint32_t flags; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1052 | mbedtls_x509_crt cacert; |
| 1053 | mbedtls_x509_crt clicert; |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1054 | |
| 1055 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1056 | mbedtls_printf( " X.509 certificate load: " ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1057 | |
Junhwan Park | 39bdab7 | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1058 | mbedtls_x509_crt_init( &cacert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1059 | mbedtls_x509_crt_init( &clicert ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1060 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1061 | ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt, |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1062 | mbedtls_test_cli_crt_len ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1063 | if( ret != 0 ) |
| 1064 | { |
| 1065 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1066 | mbedtls_printf( "failed\n" ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1067 | |
Junhwan Park | 39bdab7 | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1068 | goto cleanup; |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1069 | } |
| 1070 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1071 | ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt, |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1072 | mbedtls_test_ca_crt_len ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1073 | if( ret != 0 ) |
| 1074 | { |
| 1075 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1076 | mbedtls_printf( "failed\n" ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1077 | |
Junhwan Park | 39bdab7 | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1078 | goto cleanup; |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1082 | mbedtls_printf( "passed\n X.509 signature verify: "); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1083 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1084 | ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1085 | if( ret != 0 ) |
| 1086 | { |
| 1087 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1088 | mbedtls_printf( "failed\n" ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1089 | |
Junhwan Park | 39bdab7 | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1090 | goto cleanup; |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1094 | mbedtls_printf( "passed\n\n"); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1095 | |
Junhwan Park | 39bdab7 | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1096 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1097 | mbedtls_x509_crt_free( &cacert ); |
| 1098 | mbedtls_x509_crt_free( &clicert ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1099 | #else |
| 1100 | ((void) verbose); |
Simon Butcher | 3aa6405 | 2020-03-27 16:55:35 +0000 | [diff] [blame] | 1101 | #endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */ |
Junhwan Park | 39bdab7 | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1102 | return( ret ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1103 | } |
| 1104 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1105 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1106 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1107 | #endif /* MBEDTLS_X509_USE_C */ |