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 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
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 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 20 | */ |
| 21 | /* |
| 22 | * The ITU-T X.509 standard defines a certificate format for PKI. |
| 23 | * |
Manuel Pégourié-Gonnard | 1c082f3 | 2014-06-12 22:34:55 +0200 | [diff] [blame] | 24 | * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs) |
| 25 | * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs) |
| 26 | * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 27 | * |
| 28 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf |
| 29 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf |
| 30 | */ |
| 31 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 33 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 36 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 37 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | #if defined(MBEDTLS_X509_USE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 39 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/x509.h" |
Hanno Becker | f6bc888 | 2019-05-02 13:05:58 +0100 | [diff] [blame] | 41 | #include "mbedtls/x509_internal.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 42 | #include "mbedtls/asn1.h" |
| 43 | #include "mbedtls/oid.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 44 | |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 45 | /* We include x509xxx.c files here so that x509.c is one compilation unit including |
| 46 | * all the x509 files. This is done because some of the internal functions are shared. |
| 47 | * For code size savings internal functions should be static so that compiler can do better job |
| 48 | * when optimizing. We don't wan't x509.c file to get too big so including .c files. |
| 49 | */ |
Teppo Järvelin | ffaba55 | 2019-09-03 12:33:16 +0300 | [diff] [blame] | 50 | #include "x509_crl.c" |
| 51 | #include "x509_crt.c" |
| 52 | #include "x509_csr.c" |
| 53 | #include "x509_create.c" |
| 54 | #include "x509write_crt.c" |
| 55 | #include "x509write_csr.c" |
| 56 | |
Rich Evans | 36796df | 2015-02-12 18:27:14 +0000 | [diff] [blame] | 57 | #include <stdio.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 58 | #include <string.h> |
| 59 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 61 | #include "mbedtls/pem.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 62 | #endif |
| 63 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 65 | #include "mbedtls/platform.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 66 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 67 | #include <stdio.h> |
| 68 | #include <stdlib.h> |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 69 | #define mbedtls_free free |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 70 | #define mbedtls_calloc calloc |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 71 | #define mbedtls_printf printf |
| 72 | #define mbedtls_snprintf snprintf |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 73 | #endif |
| 74 | |
Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 75 | #if defined(MBEDTLS_HAVE_TIME) |
| 76 | #include "mbedtls/platform_time.h" |
| 77 | #endif |
Nicholas Wilson | 512b4ee | 2017-12-05 12:07:33 +0000 | [diff] [blame] | 78 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 79 | #include "mbedtls/platform_util.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 80 | #include <time.h> |
| 81 | #endif |
| 82 | |
Hanno Becker | d6028a1 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 83 | #define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); } |
| 84 | #define CHECK_RANGE(min, max, val) \ |
| 85 | do \ |
| 86 | { \ |
| 87 | if( ( val ) < ( min ) || ( val ) > ( max ) ) \ |
| 88 | { \ |
| 89 | return( ret ); \ |
| 90 | } \ |
| 91 | } while( 0 ) |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 92 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 93 | /* |
| 94 | * CertificateSerialNumber ::= INTEGER |
| 95 | */ |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 96 | static int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | mbedtls_x509_buf *serial ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 98 | { |
| 99 | int ret; |
| 100 | |
| 101 | if( ( end - *p ) < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | return( MBEDTLS_ERR_X509_INVALID_SERIAL + |
| 103 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 104 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) && |
| 106 | **p != MBEDTLS_ASN1_INTEGER ) |
| 107 | return( MBEDTLS_ERR_X509_INVALID_SERIAL + |
| 108 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 109 | |
| 110 | serial->tag = *(*p)++; |
| 111 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 ) |
| 113 | return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 114 | |
| 115 | serial->p = *p; |
| 116 | *p += serial->len; |
| 117 | |
| 118 | return( 0 ); |
| 119 | } |
| 120 | |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 121 | #if defined(MBEDTLS_X509_CRL_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C) || \ |
| 122 | ( !defined(MBEDTLS_X509_ON_DEMAND_PARSING) && defined(MBEDTLS_X509_CRT_PARSE_C) ) |
| 123 | /* |
| 124 | * Parse an algorithm identifier with (optional) parameters |
| 125 | */ |
| 126 | static int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end, |
| 127 | mbedtls_x509_buf *alg, mbedtls_x509_buf *params ) |
| 128 | { |
| 129 | int ret; |
| 130 | |
| 131 | if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 ) |
| 132 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
| 133 | |
| 134 | return( 0 ); |
| 135 | } |
| 136 | #endif /* defined(MBEDTLS_X509_CRL_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C) || |
| 137 | ( !defined(MBEDTLS_X509_ON_DEMAND_PARSING) && defined(MBEDTLS_X509_CRT_PARSE_C) ) */ |
| 138 | |
| 139 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 140 | /* Get an algorithm identifier without parameters (eg for signatures) |
| 141 | * |
| 142 | * AlgorithmIdentifier ::= SEQUENCE { |
| 143 | * algorithm OBJECT IDENTIFIER, |
| 144 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 145 | */ |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 146 | static int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | mbedtls_x509_buf *alg ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 148 | { |
| 149 | int ret; |
| 150 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 ) |
| 152 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 153 | |
| 154 | return( 0 ); |
| 155 | } |
| 156 | |
| 157 | /* |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 158 | * HashAlgorithm ::= AlgorithmIdentifier |
| 159 | * |
| 160 | * AlgorithmIdentifier ::= SEQUENCE { |
| 161 | * algorithm OBJECT IDENTIFIER, |
| 162 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 163 | * |
| 164 | * For HashAlgorithm, parameters MUST be NULL or absent. |
| 165 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | 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] | 167 | { |
| 168 | int ret; |
| 169 | unsigned char *p; |
| 170 | const unsigned char *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | mbedtls_x509_buf md_oid; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 172 | size_t len; |
| 173 | |
| 174 | /* Make sure we got a SEQUENCE and setup bounds */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) |
| 176 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
| 177 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 178 | |
Andrzej Kurek | ddc2db4 | 2020-07-16 04:37:41 -0400 | [diff] [blame] | 179 | p = alg->p; |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 180 | end = p + alg->len; |
| 181 | |
| 182 | if( p >= end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
| 184 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 185 | |
| 186 | /* Parse md_oid */ |
| 187 | md_oid.tag = *p; |
| 188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 ) |
| 190 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 191 | |
| 192 | md_oid.p = p; |
| 193 | p += md_oid.len; |
| 194 | |
| 195 | /* Get md_alg from md_oid */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 ) |
| 197 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 198 | |
| 199 | /* Make sure params is absent of NULL */ |
| 200 | if( p == end ) |
| 201 | return( 0 ); |
| 202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 203 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 ) |
| 204 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 205 | |
| 206 | if( p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 207 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
| 208 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 209 | |
| 210 | return( 0 ); |
| 211 | } |
| 212 | |
| 213 | /* |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 214 | * RSASSA-PSS-params ::= SEQUENCE { |
| 215 | * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier, |
| 216 | * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier, |
| 217 | * saltLength [2] INTEGER DEFAULT 20, |
| 218 | * trailerField [3] INTEGER DEFAULT 1 } |
| 219 | * -- Note that the tags in this Sequence are explicit. |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 220 | * |
| 221 | * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value |
| 222 | * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other |
| 223 | * option. Enfore this at parsing time. |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 224 | */ |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 225 | static int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 226 | 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] | 227 | int *salt_len ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 228 | { |
| 229 | int ret; |
| 230 | unsigned char *p; |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 231 | const unsigned char *end, *end2; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 232 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | mbedtls_x509_buf alg_id, alg_params; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 234 | |
| 235 | /* First set everything to defaults */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | *md_alg = MBEDTLS_MD_SHA1; |
| 237 | *mgf_md = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 238 | *salt_len = 20; |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 239 | |
| 240 | /* Make sure params is a SEQUENCE and setup bounds */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) |
| 242 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
| 243 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 244 | |
| 245 | p = (unsigned char *) params->p; |
| 246 | end = p + params->len; |
| 247 | |
| 248 | if( p == end ) |
| 249 | return( 0 ); |
| 250 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 251 | /* |
| 252 | * HashAlgorithm |
| 253 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 255 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 256 | { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 257 | end2 = p + len; |
| 258 | |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 259 | /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 260 | 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] | 261 | return( ret ); |
| 262 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 ) |
| 264 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 265 | |
| 266 | if( p != end2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 267 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
| 268 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 269 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
| 271 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 272 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 273 | if( p == end ) |
| 274 | return( 0 ); |
| 275 | |
| 276 | /* |
| 277 | * MaskGenAlgorithm |
| 278 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 280 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 281 | { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 282 | end2 = p + len; |
| 283 | |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 284 | /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | 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] | 286 | return( ret ); |
| 287 | |
| 288 | /* Only MFG1 is recognised for now */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 ) |
| 290 | return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE + |
| 291 | MBEDTLS_ERR_OID_NOT_FOUND ); |
Manuel Pégourié-Gonnard | e76b750 | 2014-01-23 19:15:29 +0100 | [diff] [blame] | 292 | |
| 293 | /* Parse HashAlgorithm */ |
| 294 | if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 ) |
| 295 | return( ret ); |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 296 | |
| 297 | if( p != end2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
| 299 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 300 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
| 302 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 303 | |
| 304 | if( p == end ) |
| 305 | return( 0 ); |
| 306 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 307 | /* |
| 308 | * salt_len |
| 309 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 310 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 311 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 312 | { |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 313 | end2 = p + len; |
| 314 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 315 | if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 ) |
| 316 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 317 | |
| 318 | if( p != end2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 319 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
| 320 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 321 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 322 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
| 323 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 324 | |
| 325 | if( p == end ) |
| 326 | return( 0 ); |
| 327 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 328 | /* |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 329 | * trailer_field (if present, must be 1) |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 330 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 331 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 332 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 ) |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 333 | { |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 334 | int trailer_field; |
| 335 | |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 336 | end2 = p + len; |
| 337 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 338 | if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 ) |
| 339 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
Manuel Pégourié-Gonnard | 9c9cf5b | 2014-01-24 14:15:20 +0100 | [diff] [blame] | 340 | |
| 341 | if( p != end2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 342 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
| 343 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Manuel Pégourié-Gonnard | 78117d5 | 2014-05-31 17:08:16 +0200 | [diff] [blame] | 344 | |
| 345 | if( trailer_field != 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 346 | return( MBEDTLS_ERR_X509_INVALID_ALG ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 347 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 348 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
| 349 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 350 | |
| 351 | if( p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 352 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
| 353 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 354 | |
| 355 | return( 0 ); |
| 356 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 357 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | f346bab | 2014-01-23 16:24:44 +0100 | [diff] [blame] | 358 | |
| 359 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 360 | * AttributeTypeAndValue ::= SEQUENCE { |
| 361 | * type AttributeType, |
| 362 | * value AttributeValue } |
| 363 | * |
| 364 | * AttributeType ::= OBJECT IDENTIFIER |
| 365 | * |
| 366 | * AttributeValue ::= ANY DEFINED BY AttributeType |
Hanno Becker | e452add | 2019-05-02 13:19:34 +0100 | [diff] [blame] | 367 | * |
| 368 | * NOTE: This function returns an ASN.1 low-level error code. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 369 | */ |
| 370 | static int x509_get_attr_type_value( unsigned char **p, |
| 371 | const unsigned char *end, |
Hanno Becker | 3470d59 | 2019-02-20 09:45:17 +0000 | [diff] [blame] | 372 | mbedtls_x509_buf *oid, |
| 373 | mbedtls_x509_buf *val ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 374 | { |
| 375 | int ret; |
| 376 | size_t len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 377 | |
Hanno Becker | 30cb1ac | 2019-02-20 11:30:29 +0000 | [diff] [blame] | 378 | ret = mbedtls_asn1_get_tag( p, end, &len, |
| 379 | MBEDTLS_ASN1_CONSTRUCTED | |
| 380 | MBEDTLS_ASN1_SEQUENCE ); |
| 381 | if( ret != 0 ) |
| 382 | goto exit; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 383 | |
Hanno Becker | 4e1bfc1 | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 384 | end = *p + len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 385 | |
Hanno Becker | 3470d59 | 2019-02-20 09:45:17 +0000 | [diff] [blame] | 386 | ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ); |
| 387 | if( ret != 0 ) |
Hanno Becker | 30cb1ac | 2019-02-20 11:30:29 +0000 | [diff] [blame] | 388 | goto exit; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 389 | |
Hanno Becker | ace04a6 | 2019-02-20 09:35:34 +0000 | [diff] [blame] | 390 | oid->tag = MBEDTLS_ASN1_OID; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 391 | oid->p = *p; |
| 392 | *p += oid->len; |
| 393 | |
Hanno Becker | 3470d59 | 2019-02-20 09:45:17 +0000 | [diff] [blame] | 394 | if( *p == end ) |
| 395 | { |
Hanno Becker | 30cb1ac | 2019-02-20 11:30:29 +0000 | [diff] [blame] | 396 | ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA; |
| 397 | goto exit; |
Hanno Becker | 3470d59 | 2019-02-20 09:45:17 +0000 | [diff] [blame] | 398 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 399 | |
Hanno Becker | b40dc58 | 2019-02-20 09:38:45 +0000 | [diff] [blame] | 400 | if( !MBEDTLS_ASN1_IS_STRING_TAG( **p ) ) |
| 401 | { |
Hanno Becker | 30cb1ac | 2019-02-20 11:30:29 +0000 | [diff] [blame] | 402 | ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG; |
| 403 | goto exit; |
Hanno Becker | b40dc58 | 2019-02-20 09:38:45 +0000 | [diff] [blame] | 404 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 405 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 406 | val->tag = *(*p)++; |
| 407 | |
Hanno Becker | 30cb1ac | 2019-02-20 11:30:29 +0000 | [diff] [blame] | 408 | ret = mbedtls_asn1_get_len( p, end, &val->len ); |
| 409 | if( ret != 0 ) |
| 410 | goto exit; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 411 | |
| 412 | val->p = *p; |
| 413 | *p += val->len; |
| 414 | |
Hanno Becker | 4e1bfc1 | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 415 | if( *p != end ) |
Hanno Becker | 30cb1ac | 2019-02-20 11:30:29 +0000 | [diff] [blame] | 416 | ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 417 | |
| 418 | exit: |
| 419 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /* |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 423 | * Name ::= CHOICE { -- only one possibility for now -- |
| 424 | * rdnSequence RDNSequence } |
| 425 | * |
| 426 | * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
| 427 | * |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 428 | * RelativeDistinguishedName ::= |
| 429 | * SET OF AttributeTypeAndValue |
| 430 | * |
| 431 | * AttributeTypeAndValue ::= SEQUENCE { |
| 432 | * type AttributeType, |
| 433 | * value AttributeValue } |
| 434 | * |
| 435 | * AttributeType ::= OBJECT IDENTIFIER |
| 436 | * |
| 437 | * AttributeValue ::= ANY DEFINED BY AttributeType |
Manuel Pégourié-Gonnard | 5d86185 | 2014-10-17 12:41:41 +0200 | [diff] [blame] | 438 | * |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 439 | * The data structure is optimized for the common case where each RDN has only |
| 440 | * one element, which is represented as a list of AttributeTypeAndValue. |
| 441 | * For the general case we still use a flat list, but we mark elements of the |
| 442 | * 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] | 443 | * this list, eg mbedtls_x509_dn_gets(). |
Hanno Becker | e452add | 2019-05-02 13:19:34 +0100 | [diff] [blame] | 444 | * |
| 445 | * NOTE: This function returns an ASN.1 low-level error code. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 446 | */ |
Hanno Becker | 3470d59 | 2019-02-20 09:45:17 +0000 | [diff] [blame] | 447 | static int x509_set_sequence_iterate( unsigned char **p, |
| 448 | unsigned char const **end_set, |
| 449 | unsigned char const *end, |
| 450 | mbedtls_x509_buf *oid, |
| 451 | mbedtls_x509_buf *val ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 452 | { |
| 453 | int ret; |
Manuel Pégourié-Gonnard | 5d86185 | 2014-10-17 12:41:41 +0200 | [diff] [blame] | 454 | size_t set_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 455 | |
Hanno Becker | 3470d59 | 2019-02-20 09:45:17 +0000 | [diff] [blame] | 456 | if( *p == *end_set ) |
Manuel Pégourié-Gonnard | d681443 | 2014-11-12 01:25:31 +0100 | [diff] [blame] | 457 | { |
Hanno Becker | 3470d59 | 2019-02-20 09:45:17 +0000 | [diff] [blame] | 458 | /* Parse next TLV of ASN.1 SET structure. */ |
| 459 | ret = mbedtls_asn1_get_tag( p, end, &set_len, |
| 460 | MBEDTLS_ASN1_CONSTRUCTED | |
| 461 | MBEDTLS_ASN1_SET ); |
| 462 | if( ret != 0 ) |
Hanno Becker | 30cb1ac | 2019-02-20 11:30:29 +0000 | [diff] [blame] | 463 | goto exit; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 464 | |
Hanno Becker | 3470d59 | 2019-02-20 09:45:17 +0000 | [diff] [blame] | 465 | *end_set = *p + set_len; |
| 466 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 467 | |
Hanno Becker | e452add | 2019-05-02 13:19:34 +0100 | [diff] [blame] | 468 | /* x509_get_attr_type_value() returns ASN.1 low-level error codes. */ |
Hanno Becker | 30cb1ac | 2019-02-20 11:30:29 +0000 | [diff] [blame] | 469 | ret = x509_get_attr_type_value( p, *end_set, oid, val ); |
| 470 | |
| 471 | exit: |
| 472 | return( ret ); |
Hanno Becker | 3470d59 | 2019-02-20 09:45:17 +0000 | [diff] [blame] | 473 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 474 | |
Hanno Becker | 88de342 | 2019-02-20 12:41:55 +0000 | [diff] [blame] | 475 | /* |
| 476 | * Like memcmp, but case-insensitive and always returns -1 if different |
| 477 | */ |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 478 | static int mbedtls_x509_memcasecmp( const void *s1, const void *s2, |
Hanno Becker | b3def1d | 2019-02-22 11:46:06 +0000 | [diff] [blame] | 479 | size_t len1, size_t len2 ) |
Hanno Becker | 88de342 | 2019-02-20 12:41:55 +0000 | [diff] [blame] | 480 | { |
| 481 | size_t i; |
| 482 | unsigned char diff; |
| 483 | const unsigned char *n1 = s1, *n2 = s2; |
| 484 | |
Hanno Becker | b3def1d | 2019-02-22 11:46:06 +0000 | [diff] [blame] | 485 | if( len1 != len2 ) |
| 486 | return( -1 ); |
| 487 | |
| 488 | for( i = 0; i < len1; i++ ) |
Hanno Becker | 88de342 | 2019-02-20 12:41:55 +0000 | [diff] [blame] | 489 | { |
| 490 | diff = n1[i] ^ n2[i]; |
| 491 | |
| 492 | if( diff == 0 ) |
| 493 | continue; |
| 494 | |
| 495 | if( diff == 32 && |
| 496 | ( ( n1[i] >= 'a' && n1[i] <= 'z' ) || |
| 497 | ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) ) |
| 498 | { |
| 499 | continue; |
| 500 | } |
| 501 | |
| 502 | return( -1 ); |
| 503 | } |
| 504 | |
| 505 | return( 0 ); |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Compare two X.509 strings, case-insensitive, and allowing for some encoding |
| 510 | * variations (but not all). |
| 511 | * |
| 512 | * Return 0 if equal, -1 otherwise. |
| 513 | */ |
| 514 | static int x509_string_cmp( const mbedtls_x509_buf *a, |
| 515 | const mbedtls_x509_buf *b ) |
| 516 | { |
| 517 | if( a->tag == b->tag && |
| 518 | a->len == b->len && |
Piotr Nowicki | e3c4ee5 | 2020-06-23 12:59:56 +0200 | [diff] [blame] | 519 | mbedtls_platform_memequal( a->p, b->p, b->len ) == 0 ) |
Hanno Becker | 88de342 | 2019-02-20 12:41:55 +0000 | [diff] [blame] | 520 | { |
| 521 | return( 0 ); |
| 522 | } |
| 523 | |
| 524 | if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) && |
| 525 | ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) && |
Hanno Becker | b3def1d | 2019-02-22 11:46:06 +0000 | [diff] [blame] | 526 | mbedtls_x509_memcasecmp( a->p, b->p, |
| 527 | a->len, b->len ) == 0 ) |
Hanno Becker | 88de342 | 2019-02-20 12:41:55 +0000 | [diff] [blame] | 528 | { |
| 529 | return( 0 ); |
| 530 | } |
| 531 | |
| 532 | return( -1 ); |
| 533 | } |
| 534 | |
| 535 | /* |
Hanno Becker | 7dee12a | 2019-02-21 13:58:38 +0000 | [diff] [blame] | 536 | * Compare two X.509 Names (aka rdnSequence) given as raw ASN.1 data. |
Hanno Becker | 88de342 | 2019-02-20 12:41:55 +0000 | [diff] [blame] | 537 | * |
| 538 | * See RFC 5280 section 7.1, though we don't implement the whole algorithm: |
Hanno Becker | 7dee12a | 2019-02-21 13:58:38 +0000 | [diff] [blame] | 539 | * We sometimes return unequal when the full algorithm would return equal, |
Hanno Becker | 88de342 | 2019-02-20 12:41:55 +0000 | [diff] [blame] | 540 | * but never the other way. (In particular, we don't do Unicode normalisation |
| 541 | * or space folding.) |
| 542 | * |
Hanno Becker | 67284cc | 2019-02-21 14:31:51 +0000 | [diff] [blame] | 543 | * Further, this function allows to pass a callback to be triggered for every |
| 544 | * pair of well-formed and equal entries in the two input name lists. |
| 545 | * |
Hanno Becker | 7dee12a | 2019-02-21 13:58:38 +0000 | [diff] [blame] | 546 | * Returns: |
Hanno Becker | 67284cc | 2019-02-21 14:31:51 +0000 | [diff] [blame] | 547 | * - 0 if both sequences are well-formed, present the same X.509 name, |
| 548 | * and the callback (if provided) hasn't returned a non-zero value |
| 549 | * on any of the name components. |
| 550 | * - 1 if a difference was detected in the name components. |
| 551 | * - A non-zero error code if the abort callback returns a non-zero value. |
| 552 | * In this case, the returned error code is the error code from the callback. |
Hanno Becker | 7dee12a | 2019-02-21 13:58:38 +0000 | [diff] [blame] | 553 | * - A negative error code if a parsing error occurred in either |
| 554 | * of the two buffers. |
| 555 | * |
| 556 | * This function can be used to verify that a buffer contains a well-formed |
| 557 | * ASN.1 encoded X.509 name by calling it with equal parameters. |
Hanno Becker | 88de342 | 2019-02-20 12:41:55 +0000 | [diff] [blame] | 558 | */ |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 559 | static int mbedtls_x509_name_cmp_raw( mbedtls_x509_buf_raw const *a, |
Hanno Becker | 67284cc | 2019-02-21 14:31:51 +0000 | [diff] [blame] | 560 | mbedtls_x509_buf_raw const *b, |
| 561 | int (*abort_check)( void *ctx, |
Hanno Becker | be0cf9b | 2019-05-02 13:17:29 +0100 | [diff] [blame] | 562 | mbedtls_x509_buf *oid, |
| 563 | mbedtls_x509_buf *val, |
| 564 | int next_merged ), |
Hanno Becker | 67284cc | 2019-02-21 14:31:51 +0000 | [diff] [blame] | 565 | void *abort_check_ctx ) |
Hanno Becker | a3a2ca1 | 2019-02-20 12:42:07 +0000 | [diff] [blame] | 566 | { |
| 567 | int ret; |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 568 | size_t idx; |
| 569 | unsigned char *p[2], *end[2], *set[2]; |
Hanno Becker | a3a2ca1 | 2019-02-20 12:42:07 +0000 | [diff] [blame] | 570 | |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 571 | p[0] = a->p; |
| 572 | p[1] = b->p; |
| 573 | end[0] = p[0] + a->len; |
| 574 | end[1] = p[1] + b->len; |
Hanno Becker | a3a2ca1 | 2019-02-20 12:42:07 +0000 | [diff] [blame] | 575 | |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 576 | for( idx = 0; idx < 2; idx++ ) |
| 577 | { |
| 578 | size_t len; |
| 579 | ret = mbedtls_asn1_get_tag( &p[idx], end[idx], &len, |
| 580 | MBEDTLS_ASN1_CONSTRUCTED | |
| 581 | MBEDTLS_ASN1_SEQUENCE ); |
Hanno Becker | a3a2ca1 | 2019-02-20 12:42:07 +0000 | [diff] [blame] | 582 | |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 583 | if( end[idx] != p[idx] + len ) |
| 584 | { |
| 585 | return( MBEDTLS_ERR_X509_INVALID_NAME + |
| 586 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
| 587 | } |
| 588 | |
| 589 | set[idx] = p[idx]; |
| 590 | } |
Hanno Becker | a3a2ca1 | 2019-02-20 12:42:07 +0000 | [diff] [blame] | 591 | |
| 592 | while( 1 ) |
| 593 | { |
Hanno Becker | 6b37812 | 2019-02-23 10:20:14 +0000 | [diff] [blame] | 594 | int next_merged; |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 595 | mbedtls_x509_buf oid[2], val[2]; |
Hanno Becker | a3a2ca1 | 2019-02-20 12:42:07 +0000 | [diff] [blame] | 596 | |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 597 | ret = x509_set_sequence_iterate( &p[0], (const unsigned char **) &set[0], |
| 598 | end[0], &oid[0], &val[0] ); |
Hanno Becker | a3a2ca1 | 2019-02-20 12:42:07 +0000 | [diff] [blame] | 599 | if( ret != 0 ) |
| 600 | goto exit; |
| 601 | |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 602 | ret = x509_set_sequence_iterate( &p[1], (const unsigned char **) &set[1], |
| 603 | end[1], &oid[1], &val[1] ); |
Hanno Becker | a3a2ca1 | 2019-02-20 12:42:07 +0000 | [diff] [blame] | 604 | if( ret != 0 ) |
| 605 | goto exit; |
| 606 | |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 607 | if( oid[0].len != oid[1].len || |
Piotr Nowicki | e3c4ee5 | 2020-06-23 12:59:56 +0200 | [diff] [blame] | 608 | mbedtls_platform_memequal( oid[0].p, oid[1].p, oid[1].len ) != 0 ) |
Hanno Becker | a3a2ca1 | 2019-02-20 12:42:07 +0000 | [diff] [blame] | 609 | { |
| 610 | return( 1 ); |
| 611 | } |
| 612 | |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 613 | if( x509_string_cmp( &val[0], &val[1] ) != 0 ) |
Hanno Becker | a3a2ca1 | 2019-02-20 12:42:07 +0000 | [diff] [blame] | 614 | return( 1 ); |
| 615 | |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 616 | next_merged = ( set[0] != p[0] ); |
| 617 | if( next_merged != ( set[1] != p[1] ) ) |
Hanno Becker | a3a2ca1 | 2019-02-20 12:42:07 +0000 | [diff] [blame] | 618 | return( 1 ); |
| 619 | |
Hanno Becker | 67284cc | 2019-02-21 14:31:51 +0000 | [diff] [blame] | 620 | if( abort_check != NULL ) |
| 621 | { |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 622 | ret = abort_check( abort_check_ctx, &oid[0], &val[0], |
Hanno Becker | 6b37812 | 2019-02-23 10:20:14 +0000 | [diff] [blame] | 623 | next_merged ); |
Hanno Becker | 67284cc | 2019-02-21 14:31:51 +0000 | [diff] [blame] | 624 | if( ret != 0 ) |
| 625 | return( ret ); |
| 626 | } |
| 627 | |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 628 | if( p[0] == end[0] && p[1] == end[1] ) |
Hanno Becker | a3a2ca1 | 2019-02-20 12:42:07 +0000 | [diff] [blame] | 629 | break; |
| 630 | } |
| 631 | |
| 632 | exit: |
| 633 | if( ret < 0 ) |
| 634 | ret += MBEDTLS_ERR_X509_INVALID_NAME; |
| 635 | |
| 636 | return( ret ); |
| 637 | } |
| 638 | |
Hanno Becker | c6573a2 | 2019-02-23 09:13:17 +0000 | [diff] [blame] | 639 | static int x509_get_name_cb( void *ctx, |
| 640 | mbedtls_x509_buf *oid, |
| 641 | mbedtls_x509_buf *val, |
| 642 | int next_merged ) |
| 643 | { |
| 644 | mbedtls_x509_name **cur_ptr = (mbedtls_x509_name**) ctx; |
| 645 | mbedtls_x509_name *cur = *cur_ptr; |
| 646 | |
| 647 | if( cur->oid.p != NULL ) |
| 648 | { |
| 649 | cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) ); |
| 650 | if( cur->next == NULL ) |
| 651 | return( MBEDTLS_ERR_ASN1_ALLOC_FAILED ); |
| 652 | |
| 653 | cur = cur->next; |
| 654 | } |
| 655 | |
| 656 | cur->oid = *oid; |
| 657 | cur->val = *val; |
| 658 | cur->next_merged = next_merged; |
| 659 | |
| 660 | *cur_ptr = cur; |
| 661 | return( 0 ); |
| 662 | } |
Hanno Becker | 6b37812 | 2019-02-23 10:20:14 +0000 | [diff] [blame] | 663 | |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 664 | static int mbedtls_x509_get_name( unsigned char *p, |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 665 | size_t len, |
Hanno Becker | 6b37812 | 2019-02-23 10:20:14 +0000 | [diff] [blame] | 666 | mbedtls_x509_name *cur ) |
| 667 | { |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 668 | mbedtls_x509_buf_raw name_buf = { p, len }; |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 669 | mbedtls_platform_memset( cur, 0, sizeof( mbedtls_x509_name ) ); |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 670 | return( mbedtls_x509_name_cmp_raw( &name_buf, &name_buf, |
| 671 | x509_get_name_cb, |
| 672 | &cur ) ); |
Hanno Becker | 6b37812 | 2019-02-23 10:20:14 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 675 | #if ( !defined(MBEDTLS_X509_CRT_REMOVE_TIME) && defined(MBEDTLS_X509_CRT_PARSE_C) ) || \ |
| 676 | defined(MBEDTLS_X509_CRL_PARSE_C) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 677 | static int x509_parse_int( unsigned char **p, size_t n, int *res ) |
| 678 | { |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 679 | *res = 0; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 680 | |
| 681 | for( ; n > 0; --n ) |
| 682 | { |
| 683 | if( ( **p < '0') || ( **p > '9' ) ) |
| 684 | return ( MBEDTLS_ERR_X509_INVALID_DATE ); |
| 685 | |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 686 | *res *= 10; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 687 | *res += ( *(*p)++ - '0' ); |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 688 | } |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 689 | |
| 690 | return( 0 ); |
Rich Evans | 7d5a55a | 2015-02-13 11:48:02 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 693 | static int x509_date_is_valid(const mbedtls_x509_time *t ) |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 694 | { |
| 695 | int ret = MBEDTLS_ERR_X509_INVALID_DATE; |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 696 | int month_len; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 697 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 698 | CHECK_RANGE( 0, 9999, t->year ); |
| 699 | CHECK_RANGE( 0, 23, t->hour ); |
| 700 | CHECK_RANGE( 0, 59, t->min ); |
| 701 | CHECK_RANGE( 0, 59, t->sec ); |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 702 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 703 | switch( t->mon ) |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 704 | { |
| 705 | 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] | 706 | month_len = 31; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 707 | break; |
| 708 | case 4: case 6: case 9: case 11: |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 709 | month_len = 30; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 710 | break; |
| 711 | case 2: |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 712 | if( ( !( t->year % 4 ) && t->year % 100 ) || |
| 713 | !( t->year % 400 ) ) |
| 714 | month_len = 29; |
| 715 | else |
| 716 | month_len = 28; |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 717 | break; |
| 718 | default: |
| 719 | return( ret ); |
| 720 | } |
Andres Amaya Garcia | 735b37e | 2016-11-21 15:38:02 +0000 | [diff] [blame] | 721 | CHECK_RANGE( 1, month_len, t->day ); |
Andres AG | 4b76aec | 2016-09-23 13:16:02 +0100 | [diff] [blame] | 722 | |
| 723 | return( 0 ); |
| 724 | } |
| 725 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 726 | /* |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 727 | * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4) |
| 728 | * field. |
| 729 | */ |
| 730 | 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] | 731 | mbedtls_x509_time *tm ) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 732 | { |
| 733 | int ret; |
| 734 | |
| 735 | /* |
| 736 | * Minimum length is 10 or 12 depending on yearlen |
| 737 | */ |
| 738 | if ( len < yearlen + 8 ) |
| 739 | return ( MBEDTLS_ERR_X509_INVALID_DATE ); |
| 740 | len -= yearlen + 8; |
| 741 | |
| 742 | /* |
| 743 | * Parse year, month, day, hour, minute |
| 744 | */ |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 745 | CHECK( x509_parse_int( p, yearlen, &tm->year ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 746 | if ( 2 == yearlen ) |
| 747 | { |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 748 | if ( tm->year < 50 ) |
| 749 | tm->year += 100; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 750 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 751 | tm->year += 1900; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 754 | CHECK( x509_parse_int( p, 2, &tm->mon ) ); |
| 755 | CHECK( x509_parse_int( p, 2, &tm->day ) ); |
| 756 | CHECK( x509_parse_int( p, 2, &tm->hour ) ); |
| 757 | CHECK( x509_parse_int( p, 2, &tm->min ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 758 | |
| 759 | /* |
| 760 | * Parse seconds if present |
| 761 | */ |
| 762 | if ( len >= 2 ) |
| 763 | { |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 764 | CHECK( x509_parse_int( p, 2, &tm->sec ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 765 | len -= 2; |
| 766 | } |
| 767 | else |
| 768 | return ( MBEDTLS_ERR_X509_INVALID_DATE ); |
| 769 | |
| 770 | /* |
| 771 | * Parse trailing 'Z' if present |
| 772 | */ |
| 773 | if ( 1 == len && 'Z' == **p ) |
| 774 | { |
| 775 | (*p)++; |
| 776 | len--; |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * We should have parsed all characters at this point |
| 781 | */ |
| 782 | if ( 0 != len ) |
| 783 | return ( MBEDTLS_ERR_X509_INVALID_DATE ); |
| 784 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 785 | CHECK( x509_date_is_valid( tm ) ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 786 | |
| 787 | return ( 0 ); |
| 788 | } |
| 789 | |
| 790 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 791 | * Time ::= CHOICE { |
| 792 | * utcTime UTCTime, |
| 793 | * generalTime GeneralizedTime } |
| 794 | */ |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 795 | static int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end, |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 796 | mbedtls_x509_time *tm ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 797 | { |
| 798 | int ret; |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 799 | size_t len, year_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 800 | unsigned char tag; |
| 801 | |
| 802 | if( ( end - *p ) < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 803 | return( MBEDTLS_ERR_X509_INVALID_DATE + |
| 804 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 805 | |
| 806 | tag = **p; |
| 807 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 808 | if( tag == MBEDTLS_ASN1_UTC_TIME ) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 809 | year_len = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 810 | else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME ) |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 811 | year_len = 4; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 812 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 813 | return( MBEDTLS_ERR_X509_INVALID_DATE + |
| 814 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
Janos Follath | 87c9807 | 2017-02-03 12:36:59 +0000 | [diff] [blame] | 815 | |
| 816 | (*p)++; |
| 817 | ret = mbedtls_asn1_get_len( p, end, &len ); |
| 818 | |
| 819 | if( ret != 0 ) |
| 820 | return( MBEDTLS_ERR_X509_INVALID_DATE + ret ); |
| 821 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 822 | return x509_parse_time( p, len, year_len, tm ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 823 | } |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 824 | #endif /* ( !defined(MBEDTLS_X509_CRT_REMOVE_TIME) && defined(MBEDTLS_X509_CRT_PARSE_C) ) || |
| 825 | defined(MBEDTLS_X509_CRL_PARSE_C) */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 826 | |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 827 | static 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] | 828 | { |
| 829 | int ret; |
| 830 | size_t len; |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 831 | int tag_type; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 832 | |
| 833 | if( ( end - *p ) < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 834 | return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + |
| 835 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 836 | |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 837 | tag_type = **p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 838 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 839 | if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 ) |
| 840 | return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 841 | |
Andres AG | 4bdbe09 | 2016-09-19 16:58:45 +0100 | [diff] [blame] | 842 | sig->tag = tag_type; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 843 | sig->len = len; |
| 844 | sig->p = *p; |
| 845 | |
| 846 | *p += len; |
| 847 | |
| 848 | return( 0 ); |
| 849 | } |
| 850 | |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 851 | static int mbedtls_x509_get_sig_alg_raw( unsigned char **p, unsigned char const *end, |
Hanno Becker | b59d3f1 | 2019-02-22 17:49:34 +0000 | [diff] [blame] | 852 | mbedtls_md_type_t *md_alg, |
| 853 | mbedtls_pk_type_t *pk_alg, |
| 854 | void **sig_opts ) |
| 855 | { |
| 856 | int ret; |
| 857 | mbedtls_asn1_buf alg, params; |
| 858 | ret = mbedtls_asn1_get_alg( p, end, &alg, ¶ms ); |
| 859 | if( ret != 0 ) |
| 860 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
| 861 | |
| 862 | return( mbedtls_x509_get_sig_alg( &alg, ¶ms, md_alg, |
| 863 | pk_alg, sig_opts ) ); |
| 864 | } |
| 865 | |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 866 | /* |
| 867 | * Get signature algorithm from alg OID and optional parameters |
| 868 | */ |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 869 | static int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 870 | 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] | 871 | void **sig_opts ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 872 | { |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 873 | int ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 874 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 875 | if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 ) |
| 876 | return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 877 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 878 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 879 | if( *pk_alg == MBEDTLS_PK_RSASSA_PSS ) |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 880 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 881 | mbedtls_pk_rsassa_pss_options *pss_opts; |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 882 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 883 | 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] | 884 | if( pss_opts == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 885 | return( MBEDTLS_ERR_X509_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 886 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 887 | ret = mbedtls_x509_get_rsassa_pss_params( sig_params, |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 888 | md_alg, |
| 889 | &pss_opts->mgf1_hash_id, |
| 890 | &pss_opts->expected_salt_len ); |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 891 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 892 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 893 | mbedtls_free( pss_opts ); |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 894 | return( ret ); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 895 | } |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 896 | |
Hanno Becker | 1898b68 | 2019-02-22 16:03:29 +0000 | [diff] [blame] | 897 | if( sig_opts != NULL ) |
| 898 | *sig_opts = (void *) pss_opts; |
| 899 | else |
| 900 | mbedtls_free( pss_opts ); |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 901 | } |
| 902 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 903 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 904 | { |
| 905 | /* Make sure parameters are absent or NULL */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 906 | 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] | 907 | sig_params->len != 0 ) |
Hanno Becker | 1898b68 | 2019-02-22 16:03:29 +0000 | [diff] [blame] | 908 | return( MBEDTLS_ERR_X509_INVALID_ALG ); |
| 909 | |
| 910 | if( sig_opts != NULL ) |
| 911 | *sig_opts = NULL; |
Manuel Pégourié-Gonnard | cf975a3 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 912 | } |
| 913 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 914 | return( 0 ); |
| 915 | } |
| 916 | |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 917 | #if defined(MBEDTLS_X509_CRL_PARSE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 918 | /* |
| 919 | * X.509 Extensions (No parsing of extensions, pointer should |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 920 | * be either manually updated or extensions should be parsed!) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 921 | */ |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 922 | static int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end, |
Hanno Becker | 2f47214 | 2019-02-12 11:52:10 +0000 | [diff] [blame] | 923 | mbedtls_x509_buf *ext, int tag ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 924 | { |
| 925 | int ret; |
| 926 | size_t len; |
| 927 | |
Hanno Becker | c74ce44 | 2019-02-11 14:33:36 +0000 | [diff] [blame] | 928 | /* Extension structure use EXPLICIT tagging. That is, the actual |
| 929 | * `Extensions` structure is wrapped by a tag-length pair using |
| 930 | * the respective context-specific tag. */ |
Hanno Becker | 2f47214 | 2019-02-12 11:52:10 +0000 | [diff] [blame] | 931 | ret = mbedtls_asn1_get_tag( p, end, &ext->len, |
| 932 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag ); |
| 933 | if( ret != 0 ) |
| 934 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 935 | |
Hanno Becker | 2f47214 | 2019-02-12 11:52:10 +0000 | [diff] [blame] | 936 | ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag; |
| 937 | ext->p = *p; |
| 938 | end = *p + ext->len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 939 | |
| 940 | /* |
| 941 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 942 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 943 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 944 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
| 945 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 946 | |
| 947 | if( end != *p + len ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 948 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 949 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 950 | |
| 951 | return( 0 ); |
| 952 | } |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 953 | #endif /* defined(MBEDTLS_X509_CRL_PARSE_C) */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 954 | /* |
| 955 | * Store the name in printable form into buf; no more |
| 956 | * than size characters will be written |
| 957 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 958 | 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] | 959 | { |
| 960 | int ret; |
| 961 | size_t i, n; |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 962 | unsigned char c, merge = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 963 | const mbedtls_x509_name *name; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 964 | const char *short_name = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 965 | char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 966 | |
| 967 | memset( s, 0, sizeof( s ) ); |
| 968 | |
| 969 | name = dn; |
| 970 | p = buf; |
| 971 | n = size; |
| 972 | |
| 973 | while( name != NULL ) |
| 974 | { |
| 975 | if( !name->oid.p ) |
| 976 | { |
| 977 | name = name->next; |
| 978 | continue; |
| 979 | } |
| 980 | |
| 981 | if( name != dn ) |
| 982 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 983 | ret = mbedtls_snprintf( p, n, merge ? " + " : ", " ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 984 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 985 | } |
| 986 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 987 | ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 988 | |
| 989 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 990 | ret = mbedtls_snprintf( p, n, "%s=", short_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 991 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 992 | ret = mbedtls_snprintf( p, n, "\?\?=" ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 993 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 994 | |
| 995 | for( i = 0; i < name->val.len; i++ ) |
| 996 | { |
| 997 | if( i >= sizeof( s ) - 1 ) |
| 998 | break; |
| 999 | |
| 1000 | c = name->val.p[i]; |
| 1001 | if( c < 32 || c == 127 || ( c > 128 && c < 160 ) ) |
| 1002 | s[i] = '?'; |
| 1003 | else s[i] = c; |
| 1004 | } |
| 1005 | s[i] = '\0'; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1006 | ret = mbedtls_snprintf( p, n, "%s", s ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1007 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 1008 | |
| 1009 | merge = name->next_merged; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1010 | name = name->next; |
| 1011 | } |
| 1012 | |
| 1013 | return( (int) ( size - n ) ); |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * Store the serial in printable form into buf; no more |
| 1018 | * than size characters will be written |
| 1019 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1020 | 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] | 1021 | { |
| 1022 | int ret; |
| 1023 | size_t i, n, nr; |
| 1024 | char *p; |
| 1025 | |
| 1026 | p = buf; |
| 1027 | n = size; |
| 1028 | |
| 1029 | nr = ( serial->len <= 32 ) |
| 1030 | ? serial->len : 28; |
| 1031 | |
| 1032 | for( i = 0; i < nr; i++ ) |
| 1033 | { |
| 1034 | if( i == 0 && nr > 1 && serial->p[i] == 0x0 ) |
| 1035 | continue; |
| 1036 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1037 | ret = mbedtls_snprintf( p, n, "%02X%s", |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1038 | serial->p[i], ( i < nr - 1 ) ? ":" : "" ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1039 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | if( nr != serial->len ) |
| 1043 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1044 | ret = mbedtls_snprintf( p, n, "...." ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1045 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | return( (int) ( size - n ) ); |
| 1049 | } |
| 1050 | |
Hanno Becker | 02a2193 | 2019-06-10 15:08:43 +0100 | [diff] [blame] | 1051 | #if !defined(MBEDTLS_X509_REMOVE_INFO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1052 | /* |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 1053 | * Helper for writing signature algorithms |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1054 | */ |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 1055 | static int mbedtls_x509_sig_alg_gets( char *buf, size_t size, mbedtls_pk_type_t pk_alg, |
Hanno Becker | 83cd867 | 2019-02-21 17:13:46 +0000 | [diff] [blame] | 1056 | mbedtls_md_type_t md_alg, const void *sig_opts ) |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1057 | { |
| 1058 | int ret; |
| 1059 | char *p = buf; |
| 1060 | size_t n = size; |
| 1061 | const char *desc = NULL; |
Hanno Becker | 83cd867 | 2019-02-21 17:13:46 +0000 | [diff] [blame] | 1062 | mbedtls_x509_buf sig_oid; |
| 1063 | mbedtls_md_type_t tmp_md_alg = md_alg; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1064 | |
Hanno Becker | 83cd867 | 2019-02-21 17:13:46 +0000 | [diff] [blame] | 1065 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 1066 | /* The hash for RSASSA is determined by the algorithm parameters; |
| 1067 | * in the OID list, the hash is set to MBEDTLS_MD_NONE. */ |
| 1068 | if( pk_alg == MBEDTLS_PK_RSASSA_PSS ) |
| 1069 | tmp_md_alg = MBEDTLS_MD_NONE; |
| 1070 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
| 1071 | |
| 1072 | sig_oid.tag = MBEDTLS_ASN1_OID; |
| 1073 | ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, tmp_md_alg, |
| 1074 | (const char**) &sig_oid.p, |
| 1075 | &sig_oid.len ); |
| 1076 | if( ret == 0 && |
| 1077 | mbedtls_oid_get_sig_alg_desc( &sig_oid, &desc ) == 0 ) |
| 1078 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1079 | ret = mbedtls_snprintf( p, n, "%s", desc ); |
Hanno Becker | 83cd867 | 2019-02-21 17:13:46 +0000 | [diff] [blame] | 1080 | } |
| 1081 | else |
| 1082 | ret = mbedtls_snprintf( p, n, "???" ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1083 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1084 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1085 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 1086 | if( pk_alg == MBEDTLS_PK_RSASSA_PSS ) |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1087 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1088 | const mbedtls_pk_rsassa_pss_options *pss_opts; |
Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1089 | mbedtls_md_handle_t md_info, mgf_md_info; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1090 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1092 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1093 | md_info = mbedtls_md_info_from_type( md_alg ); |
| 1094 | 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] | 1095 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1096 | ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)", |
| 1097 | md_info ? mbedtls_md_get_name( md_info ) : "???", |
| 1098 | mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???", |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 1099 | pss_opts->expected_salt_len ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1100 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1101 | } |
| 1102 | #else |
| 1103 | ((void) pk_alg); |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 1104 | ((void) md_alg); |
| 1105 | ((void) sig_opts); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1106 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1107 | |
Sander Niemeijer | ef5087d | 2014-08-16 12:45:52 +0200 | [diff] [blame] | 1108 | return( (int)( size - n ) ); |
Manuel Pégourié-Gonnard | cac31ee | 2014-01-25 11:50:59 +0100 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1112 | * Helper for writing "RSA key size", "EC key size", etc |
| 1113 | */ |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 1114 | static 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] | 1115 | { |
| 1116 | char *p = buf; |
Manuel Pégourié-Gonnard | fb317c5 | 2015-06-18 16:25:56 +0200 | [diff] [blame] | 1117 | size_t n = buf_size; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1118 | int ret; |
| 1119 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1120 | ret = mbedtls_snprintf( p, n, "%s key size", name ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1121 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1122 | |
| 1123 | return( 0 ); |
| 1124 | } |
Teppo Järvelin | f69e641 | 2019-09-03 16:50:17 +0300 | [diff] [blame] | 1125 | #endif /* !MBEDTLS_X509_REMOVE_INFO */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1126 | |
Manuel Pégourié-Gonnard | 60c793b | 2015-06-18 20:52:58 +0200 | [diff] [blame] | 1127 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 1128 | /* |
| 1129 | * Set the time structure to the current time. |
| 1130 | * Return 0 on success, non-zero on failure. |
| 1131 | */ |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 1132 | static int x509_get_current_time( mbedtls_x509_time *now ) |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1133 | { |
Nicholas Wilson | 512b4ee | 2017-12-05 12:07:33 +0000 | [diff] [blame] | 1134 | struct tm *lt, tm_buf; |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 1135 | mbedtls_time_t tt; |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 1136 | int ret = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1137 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 1138 | tt = mbedtls_time( NULL ); |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 1139 | lt = mbedtls_platform_gmtime_r( &tt, &tm_buf ); |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 1140 | |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 1141 | if( lt == NULL ) |
| 1142 | ret = -1; |
| 1143 | else |
| 1144 | { |
| 1145 | now->year = lt->tm_year + 1900; |
| 1146 | now->mon = lt->tm_mon + 1; |
| 1147 | now->day = lt->tm_mday; |
| 1148 | now->hour = lt->tm_hour; |
| 1149 | now->min = lt->tm_min; |
| 1150 | now->sec = lt->tm_sec; |
| 1151 | } |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 1152 | |
Manuel Pégourié-Gonnard | 57e10d7 | 2015-06-22 18:59:21 +0200 | [diff] [blame] | 1153 | return( ret ); |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1154 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1155 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1156 | /* |
| 1157 | * Return 0 if before <= after, 1 otherwise |
| 1158 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1159 | 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] | 1160 | { |
| 1161 | if( before->year > after->year ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1162 | return( 1 ); |
| 1163 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1164 | if( before->year == after->year && |
| 1165 | before->mon > after->mon ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1166 | return( 1 ); |
| 1167 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1168 | if( before->year == after->year && |
| 1169 | before->mon == after->mon && |
| 1170 | before->day > after->day ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1171 | return( 1 ); |
| 1172 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1173 | if( before->year == after->year && |
| 1174 | before->mon == after->mon && |
| 1175 | before->day == after->day && |
| 1176 | before->hour > after->hour ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1177 | return( 1 ); |
| 1178 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1179 | if( before->year == after->year && |
| 1180 | before->mon == after->mon && |
| 1181 | before->day == after->day && |
| 1182 | before->hour == after->hour && |
| 1183 | before->min > after->min ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1184 | return( 1 ); |
| 1185 | |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1186 | if( before->year == after->year && |
| 1187 | before->mon == after->mon && |
| 1188 | before->day == after->day && |
| 1189 | before->hour == after->hour && |
| 1190 | before->min == after->min && |
| 1191 | before->sec > after->sec ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1192 | return( 1 ); |
| 1193 | |
| 1194 | return( 0 ); |
| 1195 | } |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1196 | |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1197 | 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] | 1198 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1199 | mbedtls_x509_time now; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1200 | |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 1201 | if( x509_get_current_time( &now ) != 0 ) |
Manuel Pégourié-Gonnard | e7e8984 | 2015-06-22 19:15:32 +0200 | [diff] [blame] | 1202 | return( 1 ); |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1203 | |
| 1204 | return( x509_check_time( &now, to ) ); |
| 1205 | } |
| 1206 | |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1207 | 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] | 1208 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1209 | mbedtls_x509_time now; |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1210 | |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 1211 | if( x509_get_current_time( &now ) != 0 ) |
Manuel Pégourié-Gonnard | e7e8984 | 2015-06-22 19:15:32 +0200 | [diff] [blame] | 1212 | return( 1 ); |
Manuel Pégourié-Gonnard | 6304f78 | 2014-03-10 12:26:11 +0100 | [diff] [blame] | 1213 | |
| 1214 | return( x509_check_time( from, &now ) ); |
| 1215 | } |
Hanno Becker | c00ccea | 2019-06-07 17:03:40 +0100 | [diff] [blame] | 1216 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1217 | |
Hanno Becker | 2bcc764 | 2019-02-26 19:01:00 +0000 | [diff] [blame] | 1218 | void mbedtls_x509_name_free( mbedtls_x509_name *name ) |
| 1219 | { |
| 1220 | while( name != NULL ) |
| 1221 | { |
| 1222 | mbedtls_x509_name *next = name->next; |
| 1223 | mbedtls_platform_zeroize( name, sizeof( *name ) ); |
| 1224 | mbedtls_free( name ); |
| 1225 | name = next; |
| 1226 | } |
| 1227 | } |
| 1228 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1229 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1230 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 1231 | #include "mbedtls/x509_crt.h" |
| 1232 | #include "mbedtls/certs.h" |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1233 | |
| 1234 | /* |
| 1235 | * Checkup routine |
| 1236 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1237 | int mbedtls_x509_self_test( int verbose ) |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1238 | { |
Junhwan Park | 60ee28b | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1239 | int ret = 0; |
Gilles Peskine | 750c353 | 2017-05-05 18:56:30 +0200 | [diff] [blame] | 1240 | #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1241 | uint32_t flags; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1242 | mbedtls_x509_crt cacert; |
| 1243 | mbedtls_x509_crt clicert; |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1244 | |
| 1245 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | mbedtls_printf( " X.509 certificate load: " ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1247 | |
Junhwan Park | 60ee28b | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1248 | mbedtls_x509_crt_init( &cacert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1249 | mbedtls_x509_crt_init( &clicert ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1251 | 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] | 1252 | mbedtls_test_cli_crt_len ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1253 | if( ret != 0 ) |
| 1254 | { |
| 1255 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1256 | mbedtls_printf( "failed\n" ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1257 | |
Junhwan Park | 60ee28b | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1258 | goto cleanup; |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1261 | 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] | 1262 | mbedtls_test_ca_crt_len ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1263 | if( ret != 0 ) |
| 1264 | { |
| 1265 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1266 | mbedtls_printf( "failed\n" ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1267 | |
Junhwan Park | 60ee28b | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1268 | goto cleanup; |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1272 | mbedtls_printf( "passed\n X.509 signature verify: "); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1273 | |
Hanno Becker | 392a8d0 | 2019-09-03 09:09:58 +0100 | [diff] [blame] | 1274 | ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, |
| 1275 | #if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) |
| 1276 | NULL, |
| 1277 | #endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */ |
| 1278 | &flags |
Hanno Becker | 9ec3fe0 | 2019-07-01 17:36:12 +0100 | [diff] [blame] | 1279 | #if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK) |
Hanno Becker | 392a8d0 | 2019-09-03 09:09:58 +0100 | [diff] [blame] | 1280 | , NULL, NULL |
Hanno Becker | 9ec3fe0 | 2019-07-01 17:36:12 +0100 | [diff] [blame] | 1281 | #endif |
Hanno Becker | 392a8d0 | 2019-09-03 09:09:58 +0100 | [diff] [blame] | 1282 | ); |
Hanno Becker | 9ec3fe0 | 2019-07-01 17:36:12 +0100 | [diff] [blame] | 1283 | |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1284 | if( ret != 0 ) |
| 1285 | { |
| 1286 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1287 | mbedtls_printf( "failed\n" ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1288 | |
Junhwan Park | 60ee28b | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1289 | goto cleanup; |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1293 | mbedtls_printf( "passed\n\n"); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1294 | |
Junhwan Park | 60ee28b | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1295 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1296 | mbedtls_x509_crt_free( &cacert ); |
| 1297 | mbedtls_x509_crt_free( &clicert ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1298 | #else |
| 1299 | ((void) verbose); |
Andrzej Kurek | 825ebd4 | 2020-05-18 11:47:25 -0400 | [diff] [blame] | 1300 | #endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */ |
Junhwan Park | 60ee28b | 2018-10-17 21:01:08 +0900 | [diff] [blame] | 1301 | return( ret ); |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1302 | } |
| 1303 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1304 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | e9e6ae3 | 2013-09-16 22:53:25 +0200 | [diff] [blame] | 1305 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1306 | #endif /* MBEDTLS_X509_USE_C */ |