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 certificate 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 |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 30 | * |
| 31 | * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 32 | */ |
| 33 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 35 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 36 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 38 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 39 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 41 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 42 | #include "mbedtls/x509_crt.h" |
| 43 | #include "mbedtls/oid.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 44 | |
| 45 | #include <stdio.h> |
| 46 | #include <string.h> |
| 47 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 49 | #include "mbedtls/pem.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 50 | #endif |
| 51 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 53 | #include "mbedtls/platform.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 54 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 55 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #define mbedtls_free free |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 57 | #define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | #define mbedtls_snprintf snprintf |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 59 | #endif |
| 60 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 62 | #include "mbedtls/threading.h" |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 63 | #endif |
| 64 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 65 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 66 | #include <windows.h> |
| 67 | #else |
| 68 | #include <time.h> |
| 69 | #endif |
| 70 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | #if defined(MBEDTLS_FS_IO) |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 72 | #include <stdio.h> |
Paul Bakker | 5ff3f91 | 2014-04-04 15:08:20 +0200 | [diff] [blame] | 73 | #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 74 | #include <sys/types.h> |
| 75 | #include <sys/stat.h> |
| 76 | #include <dirent.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 77 | #endif /* !_WIN32 || EFIX64 || EFI32 */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 78 | #endif |
| 79 | |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 80 | /* Implementation that should never be optimized out by the compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | static void mbedtls_zeroize( void *v, size_t n ) { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 82 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 83 | } |
| 84 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 85 | /* |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 86 | * Default profile |
| 87 | */ |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 88 | const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default = |
| 89 | { |
Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 90 | #if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES) |
Gilles Peskine | 5e79cb3 | 2017-05-04 16:17:21 +0200 | [diff] [blame] | 91 | /* Allow SHA-1 (weak, but still safe in controlled environments) */ |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 92 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) | |
Gilles Peskine | 5e79cb3 | 2017-05-04 16:17:21 +0200 | [diff] [blame] | 93 | #endif |
| 94 | /* Only SHA-2 hashes */ |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 95 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) | |
| 96 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) | |
| 97 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) | |
| 98 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ), |
| 99 | 0xFFFFFFF, /* Any PK alg */ |
| 100 | 0xFFFFFFF, /* Any curve */ |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 101 | 2048, |
| 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Next-default profile |
| 106 | */ |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 107 | const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next = |
| 108 | { |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 109 | /* Hashes from SHA-256 and above */ |
| 110 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) | |
| 111 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) | |
| 112 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ), |
| 113 | 0xFFFFFFF, /* Any PK alg */ |
| 114 | #if defined(MBEDTLS_ECP_C) |
| 115 | /* Curves at or above 128-bit security level */ |
| 116 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) | |
| 117 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) | |
| 118 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) | |
| 119 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) | |
| 120 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) | |
| 121 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) | |
| 122 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ), |
| 123 | #else |
| 124 | 0, |
| 125 | #endif |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 126 | 2048, |
| 127 | }; |
| 128 | |
| 129 | /* |
| 130 | * NSA Suite B Profile |
| 131 | */ |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 132 | const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb = |
| 133 | { |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 134 | /* Only SHA-256 and 384 */ |
| 135 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) | |
| 136 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ), |
| 137 | /* Only ECDSA */ |
| 138 | MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ), |
| 139 | #if defined(MBEDTLS_ECP_C) |
| 140 | /* Only NIST P-256 and P-384 */ |
| 141 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) | |
| 142 | MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ), |
| 143 | #else |
| 144 | 0, |
| 145 | #endif |
| 146 | 0, |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /* |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 150 | * Check md_alg against profile |
| 151 | * Return 0 if md_alg acceptable for this profile, -1 otherwise |
| 152 | */ |
| 153 | static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile, |
| 154 | mbedtls_md_type_t md_alg ) |
| 155 | { |
| 156 | if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 ) |
| 157 | return( 0 ); |
| 158 | |
| 159 | return( -1 ); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Check pk_alg against profile |
| 164 | * Return 0 if pk_alg acceptable for this profile, -1 otherwise |
| 165 | */ |
| 166 | static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile, |
| 167 | mbedtls_pk_type_t pk_alg ) |
| 168 | { |
| 169 | if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 ) |
| 170 | return( 0 ); |
| 171 | |
| 172 | return( -1 ); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Check key against profile |
| 177 | * Return 0 if pk_alg acceptable for this profile, -1 otherwise |
| 178 | */ |
| 179 | static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile, |
| 180 | mbedtls_pk_type_t pk_alg, |
| 181 | const mbedtls_pk_context *pk ) |
| 182 | { |
| 183 | #if defined(MBEDTLS_RSA_C) |
| 184 | if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS ) |
| 185 | { |
Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 186 | if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen ) |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 187 | return( 0 ); |
| 188 | |
| 189 | return( -1 ); |
| 190 | } |
| 191 | #endif |
| 192 | |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 193 | #if defined(MBEDTLS_ECP_C) |
| 194 | if( pk_alg == MBEDTLS_PK_ECDSA || |
| 195 | pk_alg == MBEDTLS_PK_ECKEY || |
| 196 | pk_alg == MBEDTLS_PK_ECKEY_DH ) |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 197 | { |
| 198 | mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id; |
| 199 | |
| 200 | if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 ) |
| 201 | return( 0 ); |
| 202 | |
| 203 | return( -1 ); |
| 204 | } |
| 205 | #endif |
| 206 | |
| 207 | return( -1 ); |
| 208 | } |
| 209 | |
| 210 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 211 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 212 | */ |
| 213 | static int x509_get_version( unsigned char **p, |
| 214 | const unsigned char *end, |
| 215 | int *ver ) |
| 216 | { |
| 217 | int ret; |
| 218 | size_t len; |
| 219 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 221 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 222 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 224 | { |
| 225 | *ver = 0; |
| 226 | return( 0 ); |
| 227 | } |
| 228 | |
| 229 | return( ret ); |
| 230 | } |
| 231 | |
| 232 | end = *p + len; |
| 233 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 ) |
| 235 | return( MBEDTLS_ERR_X509_INVALID_VERSION + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 236 | |
| 237 | if( *p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | return( MBEDTLS_ERR_X509_INVALID_VERSION + |
| 239 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 240 | |
| 241 | return( 0 ); |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Validity ::= SEQUENCE { |
| 246 | * notBefore Time, |
| 247 | * notAfter Time } |
| 248 | */ |
| 249 | static int x509_get_dates( unsigned char **p, |
| 250 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | mbedtls_x509_time *from, |
| 252 | mbedtls_x509_time *to ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 253 | { |
| 254 | int ret; |
| 255 | size_t len; |
| 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_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
| 259 | return( MBEDTLS_ERR_X509_INVALID_DATE + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 260 | |
| 261 | end = *p + len; |
| 262 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 264 | return( ret ); |
| 265 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 266 | if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 267 | return( ret ); |
| 268 | |
| 269 | if( *p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | return( MBEDTLS_ERR_X509_INVALID_DATE + |
| 271 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 272 | |
| 273 | return( 0 ); |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * X.509 v2/v3 unique identifier (not parsed) |
| 278 | */ |
| 279 | static int x509_get_uid( unsigned char **p, |
| 280 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | mbedtls_x509_buf *uid, int n ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 282 | { |
| 283 | int ret; |
| 284 | |
| 285 | if( *p == end ) |
| 286 | return( 0 ); |
| 287 | |
| 288 | uid->tag = **p; |
| 289 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 290 | if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len, |
| 291 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 292 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 294 | return( 0 ); |
| 295 | |
| 296 | return( ret ); |
| 297 | } |
| 298 | |
| 299 | uid->p = *p; |
| 300 | *p += uid->len; |
| 301 | |
| 302 | return( 0 ); |
| 303 | } |
| 304 | |
| 305 | static int x509_get_basic_constraints( unsigned char **p, |
| 306 | const unsigned char *end, |
| 307 | int *ca_istrue, |
| 308 | int *max_pathlen ) |
| 309 | { |
| 310 | int ret; |
| 311 | size_t len; |
| 312 | |
| 313 | /* |
| 314 | * BasicConstraints ::= SEQUENCE { |
| 315 | * cA BOOLEAN DEFAULT FALSE, |
| 316 | * pathLenConstraint INTEGER (0..MAX) OPTIONAL } |
| 317 | */ |
| 318 | *ca_istrue = 0; /* DEFAULT FALSE */ |
| 319 | *max_pathlen = 0; /* endless */ |
| 320 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 321 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 322 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
| 323 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 324 | |
| 325 | if( *p == end ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 326 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 327 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 329 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 330 | if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
| 331 | ret = mbedtls_asn1_get_int( p, end, ca_istrue ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 332 | |
| 333 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 334 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 335 | |
| 336 | if( *ca_istrue != 0 ) |
| 337 | *ca_istrue = 1; |
| 338 | } |
| 339 | |
| 340 | if( *p == end ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 341 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 342 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 343 | if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 ) |
| 344 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 345 | |
| 346 | if( *p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 348 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 349 | |
| 350 | (*max_pathlen)++; |
| 351 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 352 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static int x509_get_ns_cert_type( unsigned char **p, |
| 356 | const unsigned char *end, |
| 357 | unsigned char *ns_cert_type) |
| 358 | { |
| 359 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 360 | mbedtls_x509_bitstring bs = { 0, 0, NULL }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 361 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 362 | if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 ) |
| 363 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 364 | |
| 365 | if( bs.len != 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 366 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 367 | MBEDTLS_ERR_ASN1_INVALID_LENGTH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 368 | |
| 369 | /* Get actual bitstring */ |
| 370 | *ns_cert_type = *bs.p; |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 371 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static int x509_get_key_usage( unsigned char **p, |
| 375 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 1d0ca1a | 2015-03-27 16:50:00 +0100 | [diff] [blame] | 376 | unsigned int *key_usage) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 377 | { |
| 378 | int ret; |
Manuel Pégourié-Gonnard | 9a70225 | 2015-06-23 10:14:36 +0200 | [diff] [blame] | 379 | size_t i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 380 | mbedtls_x509_bitstring bs = { 0, 0, NULL }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 381 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 382 | if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 ) |
| 383 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 384 | |
| 385 | if( bs.len < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 386 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 387 | MBEDTLS_ERR_ASN1_INVALID_LENGTH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 388 | |
| 389 | /* Get actual bitstring */ |
Manuel Pégourié-Gonnard | 9a70225 | 2015-06-23 10:14:36 +0200 | [diff] [blame] | 390 | *key_usage = 0; |
| 391 | for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ ) |
| 392 | { |
| 393 | *key_usage |= (unsigned int) bs.p[i] << (8*i); |
| 394 | } |
| 395 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 396 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* |
| 400 | * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId |
| 401 | * |
| 402 | * KeyPurposeId ::= OBJECT IDENTIFIER |
| 403 | */ |
| 404 | static int x509_get_ext_key_usage( unsigned char **p, |
| 405 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 406 | mbedtls_x509_sequence *ext_key_usage) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 407 | { |
| 408 | int ret; |
| 409 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 410 | if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 ) |
| 411 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 412 | |
| 413 | /* Sequence length must be >= 1 */ |
| 414 | if( ext_key_usage->buf.p == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 416 | MBEDTLS_ERR_ASN1_INVALID_LENGTH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 417 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 418 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* |
| 422 | * SubjectAltName ::= GeneralNames |
| 423 | * |
| 424 | * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName |
| 425 | * |
| 426 | * GeneralName ::= CHOICE { |
| 427 | * otherName [0] OtherName, |
| 428 | * rfc822Name [1] IA5String, |
| 429 | * dNSName [2] IA5String, |
| 430 | * x400Address [3] ORAddress, |
| 431 | * directoryName [4] Name, |
| 432 | * ediPartyName [5] EDIPartyName, |
| 433 | * uniformResourceIdentifier [6] IA5String, |
| 434 | * iPAddress [7] OCTET STRING, |
| 435 | * registeredID [8] OBJECT IDENTIFIER } |
| 436 | * |
| 437 | * OtherName ::= SEQUENCE { |
| 438 | * type-id OBJECT IDENTIFIER, |
| 439 | * value [0] EXPLICIT ANY DEFINED BY type-id } |
| 440 | * |
| 441 | * EDIPartyName ::= SEQUENCE { |
| 442 | * nameAssigner [0] DirectoryString OPTIONAL, |
| 443 | * partyName [1] DirectoryString } |
| 444 | * |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 445 | * NOTE: we only parse and use dNSName at this point. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 446 | */ |
| 447 | static int x509_get_subject_alt_name( unsigned char **p, |
| 448 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 449 | mbedtls_x509_sequence *subject_alt_name ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 450 | { |
| 451 | int ret; |
| 452 | size_t len, tag_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 453 | mbedtls_asn1_buf *buf; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 454 | unsigned char tag; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 455 | mbedtls_asn1_sequence *cur = subject_alt_name; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 456 | |
| 457 | /* Get main sequence tag */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 458 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 459 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
| 460 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 461 | |
| 462 | if( *p + len != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 463 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 464 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 465 | |
| 466 | while( *p < end ) |
| 467 | { |
| 468 | if( ( end - *p ) < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 469 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 470 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 471 | |
| 472 | tag = **p; |
| 473 | (*p)++; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 474 | if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 ) |
| 475 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 476 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 477 | if( ( tag & MBEDTLS_ASN1_CONTEXT_SPECIFIC ) != MBEDTLS_ASN1_CONTEXT_SPECIFIC ) |
| 478 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 479 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 480 | |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 481 | /* Skip everything but DNS name */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 482 | if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 483 | { |
| 484 | *p += tag_len; |
| 485 | continue; |
| 486 | } |
| 487 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 488 | /* Allocate and assign next pointer */ |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 489 | if( cur->buf.p != NULL ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 490 | { |
Manuel Pégourié-Gonnard | b134060 | 2014-11-11 23:11:16 +0100 | [diff] [blame] | 491 | if( cur->next != NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 492 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS ); |
Manuel Pégourié-Gonnard | b134060 | 2014-11-11 23:11:16 +0100 | [diff] [blame] | 493 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 494 | cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 495 | |
| 496 | if( cur->next == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 497 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 498 | MBEDTLS_ERR_ASN1_ALLOC_FAILED ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 499 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 500 | cur = cur->next; |
| 501 | } |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 502 | |
| 503 | buf = &(cur->buf); |
| 504 | buf->tag = tag; |
| 505 | buf->p = *p; |
| 506 | buf->len = tag_len; |
| 507 | *p += buf->len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /* Set final sequence entry's next pointer to NULL */ |
| 511 | cur->next = NULL; |
| 512 | |
| 513 | if( *p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 514 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 515 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 516 | |
| 517 | return( 0 ); |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * X.509 v3 extensions |
| 522 | * |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 523 | */ |
| 524 | static int x509_get_crt_ext( unsigned char **p, |
| 525 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 526 | mbedtls_x509_crt *crt ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 527 | { |
| 528 | int ret; |
| 529 | size_t len; |
| 530 | unsigned char *end_ext_data, *end_ext_octet; |
| 531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 532 | if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 533 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 534 | if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 535 | return( 0 ); |
| 536 | |
| 537 | return( ret ); |
| 538 | } |
| 539 | |
| 540 | while( *p < end ) |
| 541 | { |
| 542 | /* |
| 543 | * Extension ::= SEQUENCE { |
| 544 | * extnID OBJECT IDENTIFIER, |
| 545 | * critical BOOLEAN DEFAULT FALSE, |
| 546 | * extnValue OCTET STRING } |
| 547 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 548 | mbedtls_x509_buf extn_oid = {0, 0, NULL}; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 549 | int is_critical = 0; /* DEFAULT FALSE */ |
| 550 | int ext_type = 0; |
| 551 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 552 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 553 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
| 554 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 555 | |
| 556 | end_ext_data = *p + len; |
| 557 | |
| 558 | /* Get extension ID */ |
| 559 | extn_oid.tag = **p; |
| 560 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 561 | if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 ) |
| 562 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 563 | |
| 564 | extn_oid.p = *p; |
| 565 | *p += extn_oid.len; |
| 566 | |
| 567 | if( ( end - *p ) < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 568 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 569 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 570 | |
| 571 | /* Get optional critical */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 572 | if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 && |
| 573 | ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ) |
| 574 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 575 | |
| 576 | /* Data should be octet string type */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 577 | if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len, |
| 578 | MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
| 579 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 580 | |
| 581 | end_ext_octet = *p + len; |
| 582 | |
| 583 | if( end_ext_octet != end_ext_data ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 584 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 585 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 586 | |
| 587 | /* |
| 588 | * Detect supported extensions |
| 589 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 590 | ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 591 | |
| 592 | if( ret != 0 ) |
| 593 | { |
| 594 | /* No parser found, skip extension */ |
| 595 | *p = end_ext_octet; |
| 596 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 597 | #if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 598 | if( is_critical ) |
| 599 | { |
| 600 | /* Data is marked as critical: fail */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 601 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 602 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 603 | } |
| 604 | #endif |
| 605 | continue; |
| 606 | } |
| 607 | |
Manuel Pégourié-Gonnard | 8a5e3d4 | 2014-11-12 17:47:28 +0100 | [diff] [blame] | 608 | /* Forbid repeated extensions */ |
| 609 | if( ( crt->ext_types & ext_type ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 610 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS ); |
Manuel Pégourié-Gonnard | 8a5e3d4 | 2014-11-12 17:47:28 +0100 | [diff] [blame] | 611 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 612 | crt->ext_types |= ext_type; |
| 613 | |
| 614 | switch( ext_type ) |
| 615 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 616 | case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 617 | /* Parse basic constraints */ |
| 618 | if( ( ret = x509_get_basic_constraints( p, end_ext_octet, |
| 619 | &crt->ca_istrue, &crt->max_pathlen ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 620 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 621 | break; |
| 622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | case MBEDTLS_X509_EXT_KEY_USAGE: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 624 | /* Parse key usage */ |
| 625 | if( ( ret = x509_get_key_usage( p, end_ext_octet, |
| 626 | &crt->key_usage ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 627 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 628 | break; |
| 629 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 630 | case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 631 | /* Parse extended key usage */ |
| 632 | if( ( ret = x509_get_ext_key_usage( p, end_ext_octet, |
| 633 | &crt->ext_key_usage ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 634 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 635 | break; |
| 636 | |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 637 | case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 638 | /* Parse subject alt name */ |
| 639 | if( ( ret = x509_get_subject_alt_name( p, end_ext_octet, |
| 640 | &crt->subject_alt_names ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 641 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 642 | break; |
| 643 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 644 | case MBEDTLS_X509_EXT_NS_CERT_TYPE: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 645 | /* Parse netscape certificate type */ |
| 646 | if( ( ret = x509_get_ns_cert_type( p, end_ext_octet, |
| 647 | &crt->ns_cert_type ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 648 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 649 | break; |
| 650 | |
| 651 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 652 | return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
| 656 | if( *p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 657 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 658 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 659 | |
| 660 | return( 0 ); |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Parse and fill a single X.509 certificate in DER format |
| 665 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 666 | static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf, |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 667 | size_t buflen ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 668 | { |
| 669 | int ret; |
| 670 | size_t len; |
| 671 | unsigned char *p, *end, *crt_end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 672 | mbedtls_x509_buf sig_params1, sig_params2, sig_oid2; |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 673 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 674 | memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) ); |
| 675 | memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) ); |
| 676 | memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 677 | |
| 678 | /* |
| 679 | * Check for valid input |
| 680 | */ |
| 681 | if( crt == NULL || buf == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 682 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 683 | |
Janos Follath | cc0e49d | 2016-02-17 14:34:12 +0000 | [diff] [blame] | 684 | // Use the original buffer until we figure out actual length |
| 685 | p = (unsigned char*) buf; |
| 686 | len = buflen; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 687 | end = p + len; |
| 688 | |
| 689 | /* |
| 690 | * Certificate ::= SEQUENCE { |
| 691 | * tbsCertificate TBSCertificate, |
| 692 | * signatureAlgorithm AlgorithmIdentifier, |
| 693 | * signatureValue BIT STRING } |
| 694 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 695 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 696 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 697 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 698 | mbedtls_x509_crt_free( crt ); |
| 699 | return( MBEDTLS_ERR_X509_INVALID_FORMAT ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | if( len > (size_t) ( end - p ) ) |
| 703 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 704 | mbedtls_x509_crt_free( crt ); |
| 705 | return( MBEDTLS_ERR_X509_INVALID_FORMAT + |
| 706 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 707 | } |
| 708 | crt_end = p + len; |
| 709 | |
Janos Follath | cc0e49d | 2016-02-17 14:34:12 +0000 | [diff] [blame] | 710 | // Create and populate a new buffer for the raw field |
| 711 | crt->raw.len = crt_end - buf; |
| 712 | crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len ); |
| 713 | if( p == NULL ) |
| 714 | return( MBEDTLS_ERR_X509_ALLOC_FAILED ); |
| 715 | |
| 716 | memcpy( p, buf, crt->raw.len ); |
| 717 | |
| 718 | // Direct pointers to the new buffer |
| 719 | p += crt->raw.len - len; |
| 720 | end = crt_end = p + len; |
| 721 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 722 | /* |
| 723 | * TBSCertificate ::= SEQUENCE { |
| 724 | */ |
| 725 | crt->tbs.p = p; |
| 726 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 727 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 728 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 729 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 730 | mbedtls_x509_crt_free( crt ); |
| 731 | return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | end = p + len; |
| 735 | crt->tbs.len = end - crt->tbs.p; |
| 736 | |
| 737 | /* |
| 738 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 739 | * |
| 740 | * CertificateSerialNumber ::= INTEGER |
| 741 | * |
| 742 | * signature AlgorithmIdentifier |
| 743 | */ |
| 744 | if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 745 | ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 || |
| 746 | ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid, |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 747 | &sig_params1 ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 748 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 749 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 750 | return( ret ); |
| 751 | } |
| 752 | |
Andres AG | 7ca4a03 | 2017-03-09 16:16:11 +0000 | [diff] [blame] | 753 | if( crt->version < 0 || crt->version > 2 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 754 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 755 | mbedtls_x509_crt_free( crt ); |
| 756 | return( MBEDTLS_ERR_X509_UNKNOWN_VERSION ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 757 | } |
| 758 | |
Andres AG | 7ca4a03 | 2017-03-09 16:16:11 +0000 | [diff] [blame] | 759 | crt->version++; |
| 760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 761 | if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1, |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 762 | &crt->sig_md, &crt->sig_pk, |
| 763 | &crt->sig_opts ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 764 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 765 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 766 | return( ret ); |
| 767 | } |
| 768 | |
| 769 | /* |
| 770 | * issuer Name |
| 771 | */ |
| 772 | crt->issuer_raw.p = p; |
| 773 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 774 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 775 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 776 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 777 | mbedtls_x509_crt_free( crt ); |
| 778 | return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 779 | } |
| 780 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 781 | if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 782 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 783 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 784 | return( ret ); |
| 785 | } |
| 786 | |
| 787 | crt->issuer_raw.len = p - crt->issuer_raw.p; |
| 788 | |
| 789 | /* |
| 790 | * Validity ::= SEQUENCE { |
| 791 | * notBefore Time, |
| 792 | * notAfter Time } |
| 793 | * |
| 794 | */ |
| 795 | if( ( ret = x509_get_dates( &p, end, &crt->valid_from, |
| 796 | &crt->valid_to ) ) != 0 ) |
| 797 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 798 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 799 | return( ret ); |
| 800 | } |
| 801 | |
| 802 | /* |
| 803 | * subject Name |
| 804 | */ |
| 805 | crt->subject_raw.p = p; |
| 806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 807 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 808 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 809 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 810 | mbedtls_x509_crt_free( crt ); |
| 811 | return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 814 | if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 815 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 816 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 817 | return( ret ); |
| 818 | } |
| 819 | |
| 820 | crt->subject_raw.len = p - crt->subject_raw.p; |
| 821 | |
| 822 | /* |
| 823 | * SubjectPublicKeyInfo |
| 824 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 825 | if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 826 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 827 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 828 | return( ret ); |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, |
| 833 | * -- If present, version shall be v2 or v3 |
| 834 | * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, |
| 835 | * -- If present, version shall be v2 or v3 |
| 836 | * extensions [3] EXPLICIT Extensions OPTIONAL |
| 837 | * -- If present, version shall be v3 |
| 838 | */ |
| 839 | if( crt->version == 2 || crt->version == 3 ) |
| 840 | { |
| 841 | ret = x509_get_uid( &p, end, &crt->issuer_id, 1 ); |
| 842 | if( ret != 0 ) |
| 843 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 844 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 845 | return( ret ); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | if( crt->version == 2 || crt->version == 3 ) |
| 850 | { |
| 851 | ret = x509_get_uid( &p, end, &crt->subject_id, 2 ); |
| 852 | if( ret != 0 ) |
| 853 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 854 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 855 | return( ret ); |
| 856 | } |
| 857 | } |
| 858 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 859 | #if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 860 | if( crt->version == 3 ) |
Paul Bakker | c27c4e2 | 2013-09-23 15:01:36 +0200 | [diff] [blame] | 861 | #endif |
Manuel Pégourié-Gonnard | a252af7 | 2015-03-27 16:15:55 +0100 | [diff] [blame] | 862 | { |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 863 | ret = x509_get_crt_ext( &p, end, crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 864 | if( ret != 0 ) |
| 865 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 866 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 867 | return( ret ); |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | if( p != end ) |
| 872 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 873 | mbedtls_x509_crt_free( crt ); |
| 874 | return( MBEDTLS_ERR_X509_INVALID_FORMAT + |
| 875 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | end = crt_end; |
| 879 | |
| 880 | /* |
| 881 | * } |
| 882 | * -- end of TBSCertificate |
| 883 | * |
| 884 | * signatureAlgorithm AlgorithmIdentifier, |
| 885 | * signatureValue BIT STRING |
| 886 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 887 | if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 888 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 889 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 890 | return( ret ); |
| 891 | } |
| 892 | |
Manuel Pégourié-Gonnard | 1022fed | 2015-03-27 16:30:47 +0100 | [diff] [blame] | 893 | if( crt->sig_oid.len != sig_oid2.len || |
| 894 | memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 || |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 895 | sig_params1.len != sig_params2.len || |
Manuel Pégourié-Gonnard | 159c524 | 2015-04-30 11:15:22 +0200 | [diff] [blame] | 896 | ( sig_params1.len != 0 && |
| 897 | memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 898 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 899 | mbedtls_x509_crt_free( crt ); |
| 900 | return( MBEDTLS_ERR_X509_SIG_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 901 | } |
| 902 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 903 | if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 904 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 905 | mbedtls_x509_crt_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 906 | return( ret ); |
| 907 | } |
| 908 | |
| 909 | if( p != end ) |
| 910 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 911 | mbedtls_x509_crt_free( crt ); |
| 912 | return( MBEDTLS_ERR_X509_INVALID_FORMAT + |
| 913 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | return( 0 ); |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * Parse one X.509 certificate in DER format from a buffer and add them to a |
| 921 | * chained list |
| 922 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 923 | int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf, |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 924 | size_t buflen ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 925 | { |
| 926 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 927 | mbedtls_x509_crt *crt = chain, *prev = NULL; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 928 | |
| 929 | /* |
| 930 | * Check for valid input |
| 931 | */ |
| 932 | if( crt == NULL || buf == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 933 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 934 | |
| 935 | while( crt->version != 0 && crt->next != NULL ) |
| 936 | { |
| 937 | prev = crt; |
| 938 | crt = crt->next; |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | * Add new certificate on the end of the chain if needed. |
| 943 | */ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 944 | if( crt->version != 0 && crt->next == NULL ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 945 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 946 | crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 947 | |
| 948 | if( crt->next == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 949 | return( MBEDTLS_ERR_X509_ALLOC_FAILED ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 950 | |
| 951 | prev = crt; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 952 | mbedtls_x509_crt_init( crt->next ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 953 | crt = crt->next; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 954 | } |
| 955 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 956 | if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 957 | { |
| 958 | if( prev ) |
| 959 | prev->next = NULL; |
| 960 | |
| 961 | if( crt != chain ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 962 | mbedtls_free( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 963 | |
| 964 | return( ret ); |
| 965 | } |
| 966 | |
| 967 | return( 0 ); |
| 968 | } |
| 969 | |
| 970 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 971 | * Parse one or more PEM certificates from a buffer and add them to the chained |
| 972 | * list |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 973 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 974 | int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 975 | { |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 976 | #if defined(MBEDTLS_PEM_PARSE_C) |
Andres AG | c0db511 | 2016-12-07 15:05:53 +0000 | [diff] [blame] | 977 | int success = 0, first_error = 0, total_failed = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 978 | int buf_format = MBEDTLS_X509_FORMAT_DER; |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 979 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 980 | |
| 981 | /* |
| 982 | * Check for valid input |
| 983 | */ |
| 984 | if( chain == NULL || buf == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 985 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 986 | |
| 987 | /* |
| 988 | * Determine buffer content. Buffer contains either one DER certificate or |
| 989 | * one or more PEM certificates. |
| 990 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 991 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 0ece0f9 | 2015-05-12 12:43:54 +0200 | [diff] [blame] | 992 | if( buflen != 0 && buf[buflen - 1] == '\0' && |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 993 | strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL ) |
| 994 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 995 | buf_format = MBEDTLS_X509_FORMAT_PEM; |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 996 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 997 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 998 | if( buf_format == MBEDTLS_X509_FORMAT_DER ) |
| 999 | return mbedtls_x509_crt_parse_der( chain, buf, buflen ); |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1000 | #else |
| 1001 | return mbedtls_x509_crt_parse_der( chain, buf, buflen ); |
| 1002 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1003 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1004 | #if defined(MBEDTLS_PEM_PARSE_C) |
| 1005 | if( buf_format == MBEDTLS_X509_FORMAT_PEM ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1006 | { |
| 1007 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1008 | mbedtls_pem_context pem; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1009 | |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1010 | /* 1 rather than 0 since the terminating NULL byte is counted in */ |
| 1011 | while( buflen > 1 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1012 | { |
| 1013 | size_t use_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1014 | mbedtls_pem_init( &pem ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1015 | |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1016 | /* If we get there, we know the string is null-terminated */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1017 | ret = mbedtls_pem_read_buffer( &pem, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1018 | "-----BEGIN CERTIFICATE-----", |
| 1019 | "-----END CERTIFICATE-----", |
| 1020 | buf, NULL, 0, &use_len ); |
| 1021 | |
| 1022 | if( ret == 0 ) |
| 1023 | { |
| 1024 | /* |
| 1025 | * Was PEM encoded |
| 1026 | */ |
| 1027 | buflen -= use_len; |
| 1028 | buf += use_len; |
| 1029 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1030 | else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1031 | { |
| 1032 | return( ret ); |
| 1033 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1034 | else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1035 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1036 | mbedtls_pem_free( &pem ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1037 | |
| 1038 | /* |
| 1039 | * PEM header and footer were found |
| 1040 | */ |
| 1041 | buflen -= use_len; |
| 1042 | buf += use_len; |
| 1043 | |
| 1044 | if( first_error == 0 ) |
| 1045 | first_error = ret; |
| 1046 | |
Paul Bakker | 5a5fa92 | 2014-09-26 14:53:04 +0200 | [diff] [blame] | 1047 | total_failed++; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1048 | continue; |
| 1049 | } |
| 1050 | else |
| 1051 | break; |
| 1052 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1053 | ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1054 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1055 | mbedtls_pem_free( &pem ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1056 | |
| 1057 | if( ret != 0 ) |
| 1058 | { |
| 1059 | /* |
| 1060 | * Quit parsing on a memory error |
| 1061 | */ |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1062 | if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1063 | return( ret ); |
| 1064 | |
| 1065 | if( first_error == 0 ) |
| 1066 | first_error = ret; |
| 1067 | |
| 1068 | total_failed++; |
| 1069 | continue; |
| 1070 | } |
| 1071 | |
| 1072 | success = 1; |
| 1073 | } |
| 1074 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1075 | |
| 1076 | if( success ) |
| 1077 | return( total_failed ); |
| 1078 | else if( first_error ) |
| 1079 | return( first_error ); |
| 1080 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1081 | return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT ); |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1082 | #endif /* MBEDTLS_PEM_PARSE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1085 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1086 | /* |
| 1087 | * Load one or more certificates and add them to the chained list |
| 1088 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1089 | int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1090 | { |
| 1091 | int ret; |
| 1092 | size_t n; |
| 1093 | unsigned char *buf; |
| 1094 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1095 | if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1096 | return( ret ); |
| 1097 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1098 | ret = mbedtls_x509_crt_parse( chain, buf, n ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1099 | |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1100 | mbedtls_zeroize( buf, n ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1101 | mbedtls_free( buf ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1102 | |
| 1103 | return( ret ); |
| 1104 | } |
| 1105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1106 | int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1107 | { |
| 1108 | int ret = 0; |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 1109 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1110 | int w_ret; |
| 1111 | WCHAR szDir[MAX_PATH]; |
| 1112 | char filename[MAX_PATH]; |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1113 | char *p; |
Manuel Pégourié-Gonnard | 261faed | 2015-10-21 10:16:29 +0200 | [diff] [blame] | 1114 | size_t len = strlen( path ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1115 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1116 | WIN32_FIND_DATAW file_data; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1117 | HANDLE hFind; |
| 1118 | |
| 1119 | if( len > MAX_PATH - 3 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1120 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1121 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1122 | memset( szDir, 0, sizeof(szDir) ); |
| 1123 | memset( filename, 0, MAX_PATH ); |
| 1124 | memcpy( filename, path, len ); |
| 1125 | filename[len++] = '\\'; |
| 1126 | p = filename + len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1127 | filename[len++] = '*'; |
| 1128 | |
Simon B | 3c6b18d | 2016-11-03 01:11:37 +0000 | [diff] [blame] | 1129 | w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1130 | MAX_PATH - 3 ); |
Manuel Pégourié-Gonnard | acdb9b9 | 2015-01-23 17:50:34 +0000 | [diff] [blame] | 1131 | if( w_ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1132 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1133 | |
| 1134 | hFind = FindFirstFileW( szDir, &file_data ); |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1135 | if( hFind == INVALID_HANDLE_VALUE ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1136 | return( MBEDTLS_ERR_X509_FILE_IO_ERROR ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1137 | |
| 1138 | len = MAX_PATH - len; |
| 1139 | do |
| 1140 | { |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1141 | memset( p, 0, len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1142 | |
| 1143 | if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) |
| 1144 | continue; |
| 1145 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1146 | w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1147 | lstrlenW( file_data.cFileName ), |
Manuel Pégourié-Gonnard | 261faed | 2015-10-21 10:16:29 +0200 | [diff] [blame] | 1148 | p, (int) len - 1, |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1149 | NULL, NULL ); |
Manuel Pégourié-Gonnard | acdb9b9 | 2015-01-23 17:50:34 +0000 | [diff] [blame] | 1150 | if( w_ret == 0 ) |
Ron Eldor | 36d9042 | 2017-01-09 15:09:16 +0200 | [diff] [blame] | 1151 | { |
| 1152 | ret = MBEDTLS_ERR_X509_FILE_IO_ERROR; |
| 1153 | goto cleanup; |
| 1154 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1156 | w_ret = mbedtls_x509_crt_parse_file( chain, filename ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1157 | if( w_ret < 0 ) |
| 1158 | ret++; |
| 1159 | else |
| 1160 | ret += w_ret; |
| 1161 | } |
| 1162 | while( FindNextFileW( hFind, &file_data ) != 0 ); |
| 1163 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1164 | if( GetLastError() != ERROR_NO_MORE_FILES ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1165 | ret = MBEDTLS_ERR_X509_FILE_IO_ERROR; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1166 | |
Ron Eldor | 36d9042 | 2017-01-09 15:09:16 +0200 | [diff] [blame] | 1167 | cleanup: |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1168 | FindClose( hFind ); |
Paul Bakker | be089b0 | 2013-10-14 15:51:50 +0200 | [diff] [blame] | 1169 | #else /* _WIN32 */ |
Manuel Pégourié-Gonnard | 964bf9b | 2013-11-26 16:47:11 +0100 | [diff] [blame] | 1170 | int t_ret; |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1171 | int snp_ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1172 | struct stat sb; |
Manuel Pégourié-Gonnard | 964bf9b | 2013-11-26 16:47:11 +0100 | [diff] [blame] | 1173 | struct dirent *entry; |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1174 | char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN]; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1175 | DIR *dir = opendir( path ); |
| 1176 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1177 | if( dir == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1178 | return( MBEDTLS_ERR_X509_FILE_IO_ERROR ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1179 | |
Ron Eldor | 6314068 | 2017-01-09 19:27:59 +0200 | [diff] [blame] | 1180 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 1181 | if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 ) |
Manuel Pégourié-Gonnard | f9b85d9 | 2015-06-22 18:39:57 +0200 | [diff] [blame] | 1182 | { |
| 1183 | closedir( dir ); |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1184 | return( ret ); |
Manuel Pégourié-Gonnard | f9b85d9 | 2015-06-22 18:39:57 +0200 | [diff] [blame] | 1185 | } |
Ron Eldor | 6314068 | 2017-01-09 19:27:59 +0200 | [diff] [blame] | 1186 | #endif /* MBEDTLS_THREADING_C */ |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1187 | |
Manuel Pégourié-Gonnard | 964bf9b | 2013-11-26 16:47:11 +0100 | [diff] [blame] | 1188 | while( ( entry = readdir( dir ) ) != NULL ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1189 | { |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1190 | snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name, |
| 1191 | "%s/%s", path, entry->d_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1192 | |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1193 | if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1194 | { |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1195 | ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 1196 | goto cleanup; |
| 1197 | } |
| 1198 | else if( stat( entry_name, &sb ) == -1 ) |
| 1199 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1200 | ret = MBEDTLS_ERR_X509_FILE_IO_ERROR; |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1201 | goto cleanup; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | if( !S_ISREG( sb.st_mode ) ) |
| 1205 | continue; |
| 1206 | |
| 1207 | // Ignore parse errors |
| 1208 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1209 | t_ret = mbedtls_x509_crt_parse_file( chain, entry_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1210 | if( t_ret < 0 ) |
| 1211 | ret++; |
| 1212 | else |
| 1213 | ret += t_ret; |
| 1214 | } |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1215 | |
| 1216 | cleanup: |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1217 | closedir( dir ); |
| 1218 | |
Ron Eldor | 6314068 | 2017-01-09 19:27:59 +0200 | [diff] [blame] | 1219 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 1220 | if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1221 | ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
Ron Eldor | 6314068 | 2017-01-09 19:27:59 +0200 | [diff] [blame] | 1222 | #endif /* MBEDTLS_THREADING_C */ |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1223 | |
Paul Bakker | be089b0 | 2013-10-14 15:51:50 +0200 | [diff] [blame] | 1224 | #endif /* _WIN32 */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1225 | |
| 1226 | return( ret ); |
| 1227 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1228 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1229 | |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1230 | static int x509_info_subject_alt_name( char **buf, size_t *size, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1231 | const mbedtls_x509_sequence *subject_alt_name ) |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1232 | { |
| 1233 | size_t i; |
| 1234 | size_t n = *size; |
| 1235 | char *p = *buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1236 | const mbedtls_x509_sequence *cur = subject_alt_name; |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1237 | const char *sep = ""; |
| 1238 | size_t sep_len = 0; |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1239 | |
| 1240 | while( cur != NULL ) |
| 1241 | { |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1242 | if( cur->buf.len + sep_len >= n ) |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1243 | { |
| 1244 | *p = '\0'; |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1245 | return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1246 | } |
| 1247 | |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1248 | n -= cur->buf.len + sep_len; |
| 1249 | for( i = 0; i < sep_len; i++ ) |
| 1250 | *p++ = sep[i]; |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1251 | for( i = 0; i < cur->buf.len; i++ ) |
| 1252 | *p++ = cur->buf.p[i]; |
| 1253 | |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1254 | sep = ", "; |
| 1255 | sep_len = 2; |
| 1256 | |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1257 | cur = cur->next; |
| 1258 | } |
| 1259 | |
| 1260 | *p = '\0'; |
| 1261 | |
| 1262 | *size = n; |
| 1263 | *buf = p; |
| 1264 | |
| 1265 | return( 0 ); |
| 1266 | } |
| 1267 | |
Manuel Pégourié-Gonnard | 0db29b0 | 2014-04-01 18:12:24 +0200 | [diff] [blame] | 1268 | #define PRINT_ITEM(i) \ |
| 1269 | { \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1270 | ret = mbedtls_snprintf( p, n, "%s" i, sep ); \ |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1271 | MBEDTLS_X509_SAFE_SNPRINTF; \ |
Manuel Pégourié-Gonnard | 0db29b0 | 2014-04-01 18:12:24 +0200 | [diff] [blame] | 1272 | sep = ", "; \ |
| 1273 | } |
| 1274 | |
| 1275 | #define CERT_TYPE(type,name) \ |
| 1276 | if( ns_cert_type & type ) \ |
| 1277 | PRINT_ITEM( name ); |
| 1278 | |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 1279 | static int x509_info_cert_type( char **buf, size_t *size, |
| 1280 | unsigned char ns_cert_type ) |
| 1281 | { |
| 1282 | int ret; |
| 1283 | size_t n = *size; |
| 1284 | char *p = *buf; |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1285 | const char *sep = ""; |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 1286 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1287 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" ); |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 1288 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1289 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" ); |
| 1290 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" ); |
| 1291 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" ); |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 1292 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" ); |
| 1293 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" ); |
| 1294 | CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" ); |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 1295 | |
| 1296 | *size = n; |
| 1297 | *buf = p; |
| 1298 | |
| 1299 | return( 0 ); |
| 1300 | } |
| 1301 | |
Manuel Pégourié-Gonnard | 0db29b0 | 2014-04-01 18:12:24 +0200 | [diff] [blame] | 1302 | #define KEY_USAGE(code,name) \ |
| 1303 | if( key_usage & code ) \ |
| 1304 | PRINT_ITEM( name ); |
| 1305 | |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 1306 | static int x509_info_key_usage( char **buf, size_t *size, |
Manuel Pégourié-Gonnard | 9a70225 | 2015-06-23 10:14:36 +0200 | [diff] [blame] | 1307 | unsigned int key_usage ) |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 1308 | { |
| 1309 | int ret; |
| 1310 | size_t n = *size; |
| 1311 | char *p = *buf; |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1312 | const char *sep = ""; |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 1313 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1314 | KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" ); |
| 1315 | KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" ); |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 1316 | KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" ); |
| 1317 | KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" ); |
| 1318 | KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1319 | KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" ); |
| 1320 | KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" ); |
Manuel Pégourié-Gonnard | 9a70225 | 2015-06-23 10:14:36 +0200 | [diff] [blame] | 1321 | KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" ); |
| 1322 | KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" ); |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 1323 | |
| 1324 | *size = n; |
| 1325 | *buf = p; |
| 1326 | |
| 1327 | return( 0 ); |
| 1328 | } |
| 1329 | |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1330 | static int x509_info_ext_key_usage( char **buf, size_t *size, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1331 | const mbedtls_x509_sequence *extended_key_usage ) |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1332 | { |
| 1333 | int ret; |
| 1334 | const char *desc; |
| 1335 | size_t n = *size; |
| 1336 | char *p = *buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1337 | const mbedtls_x509_sequence *cur = extended_key_usage; |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1338 | const char *sep = ""; |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1339 | |
| 1340 | while( cur != NULL ) |
| 1341 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1342 | if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 ) |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1343 | desc = "???"; |
| 1344 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1345 | ret = mbedtls_snprintf( p, n, "%s%s", sep, desc ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1346 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1347 | |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1348 | sep = ", "; |
| 1349 | |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1350 | cur = cur->next; |
| 1351 | } |
| 1352 | |
| 1353 | *size = n; |
| 1354 | *buf = p; |
| 1355 | |
| 1356 | return( 0 ); |
| 1357 | } |
| 1358 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1359 | /* |
| 1360 | * Return an informational string about the certificate. |
| 1361 | */ |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1362 | #define BEFORE_COLON 18 |
| 1363 | #define BC "18" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1364 | int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix, |
| 1365 | const mbedtls_x509_crt *crt ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1366 | { |
| 1367 | int ret; |
| 1368 | size_t n; |
| 1369 | char *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1370 | char key_size_str[BEFORE_COLON]; |
| 1371 | |
| 1372 | p = buf; |
| 1373 | n = size; |
| 1374 | |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1375 | if( NULL == crt ) |
| 1376 | { |
| 1377 | ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" ); |
| 1378 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1379 | |
| 1380 | return( (int) ( size - n ) ); |
| 1381 | } |
| 1382 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1383 | ret = mbedtls_snprintf( p, n, "%scert. version : %d\n", |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1384 | prefix, crt->version ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1385 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1386 | ret = mbedtls_snprintf( p, n, "%sserial number : ", |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1387 | prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1388 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1389 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1390 | ret = mbedtls_x509_serial_gets( p, n, &crt->serial ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1391 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1392 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1393 | ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1394 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1395 | ret = mbedtls_x509_dn_gets( p, n, &crt->issuer ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1396 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1397 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1398 | ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1399 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1400 | ret = mbedtls_x509_dn_gets( p, n, &crt->subject ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1401 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1402 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1403 | ret = mbedtls_snprintf( p, n, "\n%sissued on : " \ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1404 | "%04d-%02d-%02d %02d:%02d:%02d", prefix, |
| 1405 | crt->valid_from.year, crt->valid_from.mon, |
| 1406 | crt->valid_from.day, crt->valid_from.hour, |
| 1407 | crt->valid_from.min, crt->valid_from.sec ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1408 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1409 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1410 | ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1411 | "%04d-%02d-%02d %02d:%02d:%02d", prefix, |
| 1412 | crt->valid_to.year, crt->valid_to.mon, |
| 1413 | crt->valid_to.day, crt->valid_to.hour, |
| 1414 | crt->valid_to.min, crt->valid_to.sec ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1415 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1417 | ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1418 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1419 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1420 | ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk, |
Manuel Pégourié-Gonnard | bf696d0 | 2014-06-05 17:07:30 +0200 | [diff] [blame] | 1421 | crt->sig_md, crt->sig_opts ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1422 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1423 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1424 | /* Key size */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1425 | if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON, |
| 1426 | mbedtls_pk_get_name( &crt->pk ) ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1427 | { |
| 1428 | return( ret ); |
| 1429 | } |
| 1430 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1431 | ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str, |
Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 1432 | (int) mbedtls_pk_get_bitlen( &crt->pk ) ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1433 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1434 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1435 | /* |
| 1436 | * Optional extensions |
| 1437 | */ |
| 1438 | |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 1439 | if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS ) |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1440 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1441 | ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix, |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1442 | crt->ca_istrue ? "true" : "false" ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1443 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1444 | |
| 1445 | if( crt->max_pathlen > 0 ) |
| 1446 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1447 | ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1448 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 1452 | if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1453 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1454 | ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1455 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1456 | |
| 1457 | if( ( ret = x509_info_subject_alt_name( &p, &n, |
| 1458 | &crt->subject_alt_names ) ) != 0 ) |
| 1459 | return( ret ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1460 | } |
| 1461 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1462 | if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE ) |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1463 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1464 | ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1465 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 1466 | |
| 1467 | if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 ) |
| 1468 | return( ret ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1469 | } |
| 1470 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1471 | if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1472 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1473 | ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1474 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 1475 | |
| 1476 | if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 ) |
| 1477 | return( ret ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1480 | if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1481 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1482 | ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1483 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1484 | |
| 1485 | if( ( ret = x509_info_ext_key_usage( &p, &n, |
| 1486 | &crt->ext_key_usage ) ) != 0 ) |
| 1487 | return( ret ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1488 | } |
| 1489 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1490 | ret = mbedtls_snprintf( p, n, "\n" ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1491 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1492 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1493 | return( (int) ( size - n ) ); |
| 1494 | } |
| 1495 | |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1496 | struct x509_crt_verify_string { |
| 1497 | int code; |
| 1498 | const char *string; |
| 1499 | }; |
| 1500 | |
| 1501 | static const struct x509_crt_verify_string x509_crt_verify_strings[] = { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 1502 | { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" }, |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1503 | { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" }, |
| 1504 | { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" }, |
| 1505 | { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" }, |
| 1506 | { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" }, |
| 1507 | { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" }, |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 1508 | { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" }, |
| 1509 | { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" }, |
| 1510 | { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" }, |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1511 | { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" }, |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 1512 | { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" }, |
| 1513 | { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" }, |
| 1514 | { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" }, |
| 1515 | { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" }, |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 1516 | { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." }, |
| 1517 | { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." }, |
| 1518 | { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." }, |
| 1519 | { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." }, |
| 1520 | { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." }, |
| 1521 | { MBEDTLS_X509_BADCRL_BAD_KEY, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." }, |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1522 | { 0, NULL } |
| 1523 | }; |
| 1524 | |
| 1525 | int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1526 | uint32_t flags ) |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1527 | { |
| 1528 | int ret; |
| 1529 | const struct x509_crt_verify_string *cur; |
| 1530 | char *p = buf; |
| 1531 | size_t n = size; |
| 1532 | |
| 1533 | for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ ) |
| 1534 | { |
| 1535 | if( ( flags & cur->code ) == 0 ) |
| 1536 | continue; |
| 1537 | |
| 1538 | ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1539 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1540 | flags ^= cur->code; |
| 1541 | } |
| 1542 | |
| 1543 | if( flags != 0 ) |
| 1544 | { |
| 1545 | ret = mbedtls_snprintf( p, n, "%sUnknown reason " |
| 1546 | "(this should not happen)\n", prefix ); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1547 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | return( (int) ( size - n ) ); |
| 1551 | } |
| 1552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1553 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | 655a964 | 2015-06-23 10:48:44 +0200 | [diff] [blame] | 1554 | int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt, |
| 1555 | unsigned int usage ) |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 1556 | { |
Manuel Pégourié-Gonnard | 655a964 | 2015-06-23 10:48:44 +0200 | [diff] [blame] | 1557 | unsigned int usage_must, usage_may; |
| 1558 | unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY |
| 1559 | | MBEDTLS_X509_KU_DECIPHER_ONLY; |
| 1560 | |
| 1561 | if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 ) |
| 1562 | return( 0 ); |
| 1563 | |
| 1564 | usage_must = usage & ~may_mask; |
| 1565 | |
| 1566 | if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must ) |
| 1567 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
| 1568 | |
| 1569 | usage_may = usage & may_mask; |
| 1570 | |
| 1571 | if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1572 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 1573 | |
| 1574 | return( 0 ); |
| 1575 | } |
| 1576 | #endif |
| 1577 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1578 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
| 1579 | int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt, |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 1580 | const char *usage_oid, |
| 1581 | size_t usage_len ) |
| 1582 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1583 | const mbedtls_x509_sequence *cur; |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 1584 | |
| 1585 | /* Extension is not mandatory, absent means no restriction */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1586 | if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 ) |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 1587 | return( 0 ); |
| 1588 | |
| 1589 | /* |
| 1590 | * Look for the requested usage (or wildcard ANY) in our list |
| 1591 | */ |
| 1592 | for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next ) |
| 1593 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1594 | const mbedtls_x509_buf *cur_oid = &cur->buf; |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 1595 | |
| 1596 | if( cur_oid->len == usage_len && |
| 1597 | memcmp( cur_oid->p, usage_oid, usage_len ) == 0 ) |
| 1598 | { |
| 1599 | return( 0 ); |
| 1600 | } |
| 1601 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1602 | if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 ) |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 1603 | return( 0 ); |
| 1604 | } |
| 1605 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1606 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 1607 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1608 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 1609 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1610 | #if defined(MBEDTLS_X509_CRL_PARSE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1611 | /* |
| 1612 | * Return 1 if the certificate is revoked, or 0 otherwise. |
| 1613 | */ |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1614 | int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1615 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1616 | const mbedtls_x509_crl_entry *cur = &crl->entry; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1617 | |
| 1618 | while( cur != NULL && cur->serial.len != 0 ) |
| 1619 | { |
| 1620 | if( crt->serial.len == cur->serial.len && |
| 1621 | memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 ) |
| 1622 | { |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1623 | if( mbedtls_x509_time_is_past( &cur->revocation_date ) ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1624 | return( 1 ); |
| 1625 | } |
| 1626 | |
| 1627 | cur = cur->next; |
| 1628 | } |
| 1629 | |
| 1630 | return( 0 ); |
| 1631 | } |
| 1632 | |
| 1633 | /* |
Manuel Pégourié-Gonnard | eeef947 | 2016-02-22 11:36:55 +0100 | [diff] [blame] | 1634 | * Check that the given certificate is not revoked according to the CRL. |
| 1635 | * Skip validation is no CRL for the given CA is present. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1636 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1637 | static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca, |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 1638 | mbedtls_x509_crl *crl_list, |
| 1639 | const mbedtls_x509_crt_profile *profile ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1640 | { |
| 1641 | int flags = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1642 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
| 1643 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1644 | |
| 1645 | if( ca == NULL ) |
| 1646 | return( flags ); |
| 1647 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1648 | while( crl_list != NULL ) |
| 1649 | { |
| 1650 | if( crl_list->version == 0 || |
| 1651 | crl_list->issuer_raw.len != ca->subject_raw.len || |
| 1652 | memcmp( crl_list->issuer_raw.p, ca->subject_raw.p, |
| 1653 | crl_list->issuer_raw.len ) != 0 ) |
| 1654 | { |
| 1655 | crl_list = crl_list->next; |
| 1656 | continue; |
| 1657 | } |
| 1658 | |
| 1659 | /* |
Manuel Pégourié-Gonnard | 99d4f19 | 2014-04-08 15:10:07 +0200 | [diff] [blame] | 1660 | * Check if the CA is configured to sign CRLs |
| 1661 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1662 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
| 1663 | if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 ) |
Manuel Pégourié-Gonnard | 99d4f19 | 2014-04-08 15:10:07 +0200 | [diff] [blame] | 1664 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1665 | flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; |
Manuel Pégourié-Gonnard | 99d4f19 | 2014-04-08 15:10:07 +0200 | [diff] [blame] | 1666 | break; |
| 1667 | } |
| 1668 | #endif |
| 1669 | |
| 1670 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1671 | * Check if CRL is correctly signed by the trusted CA |
| 1672 | */ |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 1673 | if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 ) |
| 1674 | flags |= MBEDTLS_X509_BADCRL_BAD_MD; |
| 1675 | |
| 1676 | if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 ) |
| 1677 | flags |= MBEDTLS_X509_BADCRL_BAD_PK; |
| 1678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1679 | md_info = mbedtls_md_info_from_type( crl_list->sig_md ); |
Manuel Pégourié-Gonnard | 329e78c | 2017-06-26 12:22:17 +0200 | [diff] [blame] | 1680 | if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1681 | { |
Manuel Pégourié-Gonnard | 329e78c | 2017-06-26 12:22:17 +0200 | [diff] [blame] | 1682 | /* Note: this can't happen except after an internal error */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1683 | flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1684 | break; |
| 1685 | } |
| 1686 | |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 1687 | if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 ) |
| 1688 | flags |= MBEDTLS_X509_BADCERT_BAD_KEY; |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 1689 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1690 | if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk, |
| 1691 | crl_list->sig_md, hash, mbedtls_md_get_size( md_info ), |
Manuel Pégourié-Gonnard | 5388202 | 2014-06-05 17:53:52 +0200 | [diff] [blame] | 1692 | crl_list->sig.p, crl_list->sig.len ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1693 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1694 | flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1695 | break; |
| 1696 | } |
| 1697 | |
| 1698 | /* |
| 1699 | * Check for validity of CRL (Do not drop out) |
| 1700 | */ |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1701 | if( mbedtls_x509_time_is_past( &crl_list->next_update ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1702 | flags |= MBEDTLS_X509_BADCRL_EXPIRED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1703 | |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1704 | if( mbedtls_x509_time_is_future( &crl_list->this_update ) ) |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 1705 | flags |= MBEDTLS_X509_BADCRL_FUTURE; |
Manuel Pégourié-Gonnard | 9533765 | 2014-03-10 13:15:18 +0100 | [diff] [blame] | 1706 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1707 | /* |
| 1708 | * Check if certificate is revoked |
| 1709 | */ |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 1710 | if( mbedtls_x509_crt_is_revoked( crt, crl_list ) ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1711 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1712 | flags |= MBEDTLS_X509_BADCERT_REVOKED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1713 | break; |
| 1714 | } |
| 1715 | |
| 1716 | crl_list = crl_list->next; |
| 1717 | } |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 1718 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 1719 | return( flags ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1720 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1721 | #endif /* MBEDTLS_X509_CRL_PARSE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1722 | |
Manuel Pégourié-Gonnard | 8842124 | 2014-10-17 11:36:18 +0200 | [diff] [blame] | 1723 | /* |
| 1724 | * Like memcmp, but case-insensitive and always returns -1 if different |
| 1725 | */ |
| 1726 | static int x509_memcasecmp( const void *s1, const void *s2, size_t len ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1727 | { |
| 1728 | size_t i; |
| 1729 | unsigned char diff; |
| 1730 | const unsigned char *n1 = s1, *n2 = s2; |
| 1731 | |
| 1732 | for( i = 0; i < len; i++ ) |
| 1733 | { |
| 1734 | diff = n1[i] ^ n2[i]; |
| 1735 | |
Paul Bakker | f2b4d86 | 2013-11-20 17:23:53 +0100 | [diff] [blame] | 1736 | if( diff == 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1737 | continue; |
| 1738 | |
Paul Bakker | f2b4d86 | 2013-11-20 17:23:53 +0100 | [diff] [blame] | 1739 | if( diff == 32 && |
| 1740 | ( ( n1[i] >= 'a' && n1[i] <= 'z' ) || |
| 1741 | ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) ) |
| 1742 | { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1743 | continue; |
Paul Bakker | f2b4d86 | 2013-11-20 17:23:53 +0100 | [diff] [blame] | 1744 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1745 | |
Manuel Pégourié-Gonnard | 8842124 | 2014-10-17 11:36:18 +0200 | [diff] [blame] | 1746 | return( -1 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | return( 0 ); |
| 1750 | } |
| 1751 | |
Manuel Pégourié-Gonnard | 8842124 | 2014-10-17 11:36:18 +0200 | [diff] [blame] | 1752 | /* |
Manuel Pégourié-Gonnard | b80d16d | 2015-06-23 09:24:29 +0200 | [diff] [blame] | 1753 | * Return 0 if name matches wildcard, -1 otherwise |
Manuel Pégourié-Gonnard | 8842124 | 2014-10-17 11:36:18 +0200 | [diff] [blame] | 1754 | */ |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 1755 | static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1756 | { |
| 1757 | size_t i; |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 1758 | size_t cn_idx = 0, cn_len = strlen( cn ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1759 | |
| 1760 | if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' ) |
| 1761 | return( 0 ); |
| 1762 | |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 1763 | for( i = 0; i < cn_len; ++i ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1764 | { |
| 1765 | if( cn[i] == '.' ) |
| 1766 | { |
| 1767 | cn_idx = i; |
| 1768 | break; |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | if( cn_idx == 0 ) |
Manuel Pégourié-Gonnard | b80d16d | 2015-06-23 09:24:29 +0200 | [diff] [blame] | 1773 | return( -1 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1774 | |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 1775 | if( cn_len - cn_idx == name->len - 1 && |
Manuel Pégourié-Gonnard | 8842124 | 2014-10-17 11:36:18 +0200 | [diff] [blame] | 1776 | x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1777 | { |
Manuel Pégourié-Gonnard | b80d16d | 2015-06-23 09:24:29 +0200 | [diff] [blame] | 1778 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1779 | } |
| 1780 | |
Manuel Pégourié-Gonnard | b80d16d | 2015-06-23 09:24:29 +0200 | [diff] [blame] | 1781 | return( -1 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1782 | } |
| 1783 | |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 1784 | /* |
Manuel Pégourié-Gonnard | ef9a6ae | 2014-10-17 12:25:12 +0200 | [diff] [blame] | 1785 | * Compare two X.509 strings, case-insensitive, and allowing for some encoding |
| 1786 | * variations (but not all). |
| 1787 | * |
| 1788 | * Return 0 if equal, -1 otherwise. |
| 1789 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1790 | static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b ) |
Manuel Pégourié-Gonnard | ef9a6ae | 2014-10-17 12:25:12 +0200 | [diff] [blame] | 1791 | { |
| 1792 | if( a->tag == b->tag && |
| 1793 | a->len == b->len && |
| 1794 | memcmp( a->p, b->p, b->len ) == 0 ) |
| 1795 | { |
| 1796 | return( 0 ); |
| 1797 | } |
| 1798 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1799 | if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) && |
| 1800 | ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) && |
Manuel Pégourié-Gonnard | ef9a6ae | 2014-10-17 12:25:12 +0200 | [diff] [blame] | 1801 | a->len == b->len && |
| 1802 | x509_memcasecmp( a->p, b->p, b->len ) == 0 ) |
| 1803 | { |
| 1804 | return( 0 ); |
| 1805 | } |
| 1806 | |
| 1807 | return( -1 ); |
| 1808 | } |
| 1809 | |
| 1810 | /* |
| 1811 | * Compare two X.509 Names (aka rdnSequence). |
| 1812 | * |
| 1813 | * See RFC 5280 section 7.1, though we don't implement the whole algorithm: |
| 1814 | * we sometimes return unequal when the full algorithm would return equal, |
| 1815 | * but never the other way. (In particular, we don't do Unicode normalisation |
| 1816 | * or space folding.) |
| 1817 | * |
| 1818 | * Return 0 if equal, -1 otherwise. |
| 1819 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1820 | static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b ) |
Manuel Pégourié-Gonnard | ef9a6ae | 2014-10-17 12:25:12 +0200 | [diff] [blame] | 1821 | { |
Manuel Pégourié-Gonnard | f631bbc | 2014-11-12 18:35:31 +0100 | [diff] [blame] | 1822 | /* Avoid recursion, it might not be optimised by the compiler */ |
| 1823 | while( a != NULL || b != NULL ) |
Manuel Pégourié-Gonnard | ef9a6ae | 2014-10-17 12:25:12 +0200 | [diff] [blame] | 1824 | { |
Manuel Pégourié-Gonnard | f631bbc | 2014-11-12 18:35:31 +0100 | [diff] [blame] | 1825 | if( a == NULL || b == NULL ) |
| 1826 | return( -1 ); |
| 1827 | |
| 1828 | /* type */ |
| 1829 | if( a->oid.tag != b->oid.tag || |
| 1830 | a->oid.len != b->oid.len || |
| 1831 | memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 ) |
| 1832 | { |
| 1833 | return( -1 ); |
| 1834 | } |
| 1835 | |
| 1836 | /* value */ |
| 1837 | if( x509_string_cmp( &a->val, &b->val ) != 0 ) |
| 1838 | return( -1 ); |
| 1839 | |
Manuel Pégourié-Gonnard | 555fbf8 | 2015-02-04 17:11:55 +0000 | [diff] [blame] | 1840 | /* structure of the list of sets */ |
| 1841 | if( a->next_merged != b->next_merged ) |
| 1842 | return( -1 ); |
| 1843 | |
Manuel Pégourié-Gonnard | f631bbc | 2014-11-12 18:35:31 +0100 | [diff] [blame] | 1844 | a = a->next; |
| 1845 | b = b->next; |
Manuel Pégourié-Gonnard | ef9a6ae | 2014-10-17 12:25:12 +0200 | [diff] [blame] | 1846 | } |
| 1847 | |
Manuel Pégourié-Gonnard | f631bbc | 2014-11-12 18:35:31 +0100 | [diff] [blame] | 1848 | /* a == NULL == b */ |
| 1849 | return( 0 ); |
Manuel Pégourié-Gonnard | ef9a6ae | 2014-10-17 12:25:12 +0200 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | /* |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 1853 | * Check the signature of a certificate by its parent |
| 1854 | */ |
| 1855 | static int x509_crt_check_signature( const mbedtls_x509_crt *child, |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 1856 | mbedtls_x509_crt *parent, |
| 1857 | mbedtls_x509_crt_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 1858 | { |
| 1859 | const mbedtls_md_info_t *md_info; |
| 1860 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
| 1861 | |
| 1862 | md_info = mbedtls_md_info_from_type( child->sig_md ); |
| 1863 | if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 ) |
| 1864 | { |
| 1865 | /* Note: this can't happen except after an internal error */ |
| 1866 | return( -1 ); |
| 1867 | } |
| 1868 | |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 1869 | /* Skip expensive computation on obvious mismatch */ |
| 1870 | if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) ) |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 1871 | return( -1 ); |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 1872 | |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 1873 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 1874 | if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA ) |
| 1875 | { |
| 1876 | return( mbedtls_pk_verify_restartable( &parent->pk, |
| 1877 | child->sig_md, hash, mbedtls_md_get_size( md_info ), |
| 1878 | child->sig.p, child->sig.len, &rs_ctx->ecdsa ) ); |
| 1879 | } |
| 1880 | #else |
| 1881 | (void) rs_ctx; |
| 1882 | #endif |
| 1883 | |
| 1884 | return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk, |
| 1885 | child->sig_md, hash, mbedtls_md_get_size( md_info ), |
| 1886 | child->sig.p, child->sig.len ) ); |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | /* |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 1890 | * Check if 'parent' is a suitable parent (signing CA) for 'child'. |
| 1891 | * Return 0 if yes, -1 if not. |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 1892 | * |
| 1893 | * top means parent is a locally-trusted certificate |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 1894 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1895 | static int x509_crt_check_parent( const mbedtls_x509_crt *child, |
| 1896 | const mbedtls_x509_crt *parent, |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 1897 | int top ) |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 1898 | { |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 1899 | int need_ca_bit; |
| 1900 | |
Manuel Pégourié-Gonnard | c4eff16 | 2014-06-19 12:18:08 +0200 | [diff] [blame] | 1901 | /* Parent must be the issuer */ |
Manuel Pégourié-Gonnard | ef9a6ae | 2014-10-17 12:25:12 +0200 | [diff] [blame] | 1902 | if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 ) |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 1903 | return( -1 ); |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 1904 | |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 1905 | /* Parent must have the basicConstraints CA bit set as a general rule */ |
| 1906 | need_ca_bit = 1; |
| 1907 | |
| 1908 | /* Exception: v1/v2 certificates that are locally trusted. */ |
| 1909 | if( top && parent->version < 3 ) |
| 1910 | need_ca_bit = 0; |
| 1911 | |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 1912 | if( need_ca_bit && ! parent->ca_istrue ) |
| 1913 | return( -1 ); |
| 1914 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1915 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 1916 | if( need_ca_bit && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1917 | mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 ) |
Manuel Pégourié-Gonnard | c4eff16 | 2014-06-19 12:18:08 +0200 | [diff] [blame] | 1918 | { |
| 1919 | return( -1 ); |
| 1920 | } |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 1921 | #endif |
| 1922 | |
| 1923 | return( 0 ); |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 1924 | } |
| 1925 | |
Manuel Pégourié-Gonnard | 35407c7 | 2017-06-29 10:45:25 +0200 | [diff] [blame] | 1926 | /* |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 1927 | * Find a suitable parent for child in candidates, or return NULL. |
| 1928 | * |
| 1929 | * Here suitable is defined as: |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 1930 | * 1. subject name matches child's issuer |
| 1931 | * 2. if necessary, the CA bit is set and key usage allows signing certs |
| 1932 | * 3. for trusted roots, the signature is correct |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 1933 | * (for intermediates, the signature is checked and the result reported) |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 1934 | * 4. pathlen constraints are satisfied |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 1935 | * |
Manuel Pégourié-Gonnard | 562df40 | 2017-08-08 18:09:14 +0200 | [diff] [blame] | 1936 | * If there's a suitable candidate which is also time-valid, return the first |
| 1937 | * such. Otherwise, return the first suitable candidate (or NULL if there is |
| 1938 | * none). |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 1939 | * |
| 1940 | * The rationale for this rule is that someone could have a list of trusted |
| 1941 | * roots with two versions on the same root with different validity periods. |
| 1942 | * (At least one user reported having such a list and wanted it to just work.) |
| 1943 | * The reason we don't just require time-validity is that generally there is |
| 1944 | * only one version, and if it's expired we want the flags to state that |
| 1945 | * rather than NOT_TRUSTED, as would be the case if we required it here. |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 1946 | * |
| 1947 | * The rationale for rule 3 (signature for trusted roots) is that users might |
| 1948 | * have two versions of the same CA with different keys in their list, and the |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 1949 | * way we select the correct one is by checking the signature (as we don't |
| 1950 | * rely on key identifier extensions). (This is one way users might choose to |
| 1951 | * handle key rollover, another relies on self-issued certs, see [SIRO].) |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 1952 | */ |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 1953 | static int x509_crt_find_parent_in( |
| 1954 | mbedtls_x509_crt *child, |
| 1955 | mbedtls_x509_crt *candidates, |
| 1956 | mbedtls_x509_crt **r_parent, |
| 1957 | int *r_signature_is_good, |
| 1958 | int top, |
| 1959 | int path_cnt, |
| 1960 | int self_cnt, |
| 1961 | mbedtls_x509_crt_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 1962 | { |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 1963 | int ret; |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 1964 | mbedtls_x509_crt *parent, *fallback_parent = NULL; |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 1965 | int signature_is_good = 0, fallback_sign_good = 0; |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 1966 | |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 1967 | for( parent = candidates; parent != NULL; parent = parent->next ) |
| 1968 | { |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 1969 | /* basic parenting skills (name, CA bit, key usage) */ |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 1970 | if( x509_crt_check_parent( child, parent, top ) != 0 ) |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 1971 | continue; |
| 1972 | |
Manuel Pégourié-Gonnard | 9c6118c | 2017-06-29 12:38:42 +0200 | [diff] [blame] | 1973 | /* +1 because stored max_pathlen is 1 higher that the actual value */ |
| 1974 | if( parent->max_pathlen > 0 && |
| 1975 | parent->max_pathlen < 1 + path_cnt - self_cnt ) |
| 1976 | { |
| 1977 | continue; |
| 1978 | } |
| 1979 | |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 1980 | /* Signature */ |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 1981 | ret = x509_crt_check_signature( child, parent, rs_ctx ); |
| 1982 | |
| 1983 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1984 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) { |
| 1985 | // TODO: stave state |
| 1986 | return( ret ); |
| 1987 | } |
| 1988 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 1989 | |
| 1990 | signature_is_good = ret == 0; |
| 1991 | if( top && ! signature_is_good ) |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 1992 | continue; |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 1993 | |
Manuel Pégourié-Gonnard | 562df40 | 2017-08-08 18:09:14 +0200 | [diff] [blame] | 1994 | /* optional time check */ |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 1995 | if( mbedtls_x509_time_is_past( &parent->valid_to ) || |
| 1996 | mbedtls_x509_time_is_future( &parent->valid_from ) ) |
| 1997 | { |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 1998 | if( fallback_parent == NULL ) |
| 1999 | { |
| 2000 | fallback_parent = parent; |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2001 | fallback_sign_good = signature_is_good; |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2002 | } |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2003 | |
| 2004 | continue; |
| 2005 | } |
| 2006 | |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2007 | break; |
| 2008 | } |
| 2009 | |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2010 | if( parent != NULL ) |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2011 | { |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2012 | *r_parent = parent; |
| 2013 | *r_signature_is_good = signature_is_good; |
| 2014 | } |
| 2015 | else |
| 2016 | { |
| 2017 | *r_parent = fallback_parent; |
| 2018 | *r_signature_is_good = fallback_sign_good; |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2019 | } |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2020 | |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2021 | return( 0 ); |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | /* |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2025 | * Find a parent in trusted CAs or the provided chain, or return NULL. |
| 2026 | * |
| 2027 | * Searches in trusted CAs first, and return the first suitable parent found |
| 2028 | * (see find_parent_in() for definition of suitable). |
| 2029 | */ |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2030 | static int x509_crt_find_parent( |
| 2031 | mbedtls_x509_crt *child, |
| 2032 | mbedtls_x509_crt *trust_ca, |
| 2033 | mbedtls_x509_crt **parent, |
| 2034 | int *parent_is_trusted, |
| 2035 | int *signature_is_good, |
| 2036 | int path_cnt, |
| 2037 | int self_cnt, |
| 2038 | mbedtls_x509_crt_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2039 | { |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2040 | int ret; |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2041 | mbedtls_x509_crt *search_list; |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2042 | |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2043 | *parent_is_trusted = 1; |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2044 | |
| 2045 | while( 1 ) { |
| 2046 | search_list = *parent_is_trusted ? trust_ca : child->next; |
| 2047 | |
| 2048 | ret = x509_crt_find_parent_in( child, search_list, |
| 2049 | parent, signature_is_good, |
| 2050 | *parent_is_trusted, |
| 2051 | path_cnt, self_cnt, rs_ctx ); |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2052 | |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2053 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2054 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) { |
| 2055 | // TODO: stave state |
| 2056 | return( ret ); |
| 2057 | } |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2058 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 2059 | |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2060 | /* stop here if found or already in second iteration */ |
| 2061 | if( *parent != NULL || *parent_is_trusted == 0 ) |
| 2062 | break; |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2063 | |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2064 | /* prepare second iteration */ |
| 2065 | *parent_is_trusted = 0; |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2066 | } |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2067 | |
| 2068 | /* extra precaution against mistakes in the caller */ |
| 2069 | if( parent == NULL ) |
| 2070 | { |
| 2071 | parent_is_trusted = 0; |
| 2072 | signature_is_good = 0; |
| 2073 | } |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2074 | |
| 2075 | return( 0 ); |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | /* |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 2079 | * Check if an end-entity certificate is locally trusted |
| 2080 | * |
| 2081 | * Currently we require such certificates to be self-signed (actually only |
| 2082 | * check for self-issued as self-signatures are not checked) |
| 2083 | */ |
| 2084 | static int x509_crt_check_ee_locally_trusted( |
| 2085 | mbedtls_x509_crt *crt, |
| 2086 | mbedtls_x509_crt *trust_ca ) |
| 2087 | { |
| 2088 | mbedtls_x509_crt *cur; |
| 2089 | |
| 2090 | /* must be self-issued */ |
| 2091 | if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 ) |
| 2092 | return( -1 ); |
| 2093 | |
| 2094 | /* look for an exact match with trusted cert */ |
| 2095 | for( cur = trust_ca; cur != NULL; cur = cur->next ) |
| 2096 | { |
| 2097 | if( crt->raw.len == cur->raw.len && |
| 2098 | memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 ) |
| 2099 | { |
| 2100 | return( 0 ); |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | /* too bad */ |
| 2105 | return( -1 ); |
| 2106 | } |
| 2107 | |
| 2108 | /* |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2109 | * Build and verify a certificate chain |
Manuel Pégourié-Gonnard | 35407c7 | 2017-06-29 10:45:25 +0200 | [diff] [blame] | 2110 | * |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2111 | * Given a peer-provided list of certificates EE, C1, ..., Cn and |
| 2112 | * a list of trusted certs R1, ... Rp, try to build and verify a chain |
Manuel Pégourié-Gonnard | 562df40 | 2017-08-08 18:09:14 +0200 | [diff] [blame] | 2113 | * EE, Ci1, ... Ciq [, Rj] |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2114 | * such that every cert in the chain is a child of the next one, |
| 2115 | * jumping to a trusted root as early as possible. |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2116 | * |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2117 | * Verify that chain and return it with flags for all issues found. |
| 2118 | * |
| 2119 | * Special cases: |
| 2120 | * - EE == Rj -> return a one-element list containing it |
| 2121 | * - EE, Ci1, ..., Ciq cannot be continued with a trusted root |
| 2122 | * -> return that chain with NOT_TRUSTED set on Ciq |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2123 | * |
Manuel Pégourié-Gonnard | d19a41d | 2017-07-14 11:05:59 +0200 | [diff] [blame] | 2124 | * Tests for (aspects of) this function should include at least: |
| 2125 | * - trusted EE |
| 2126 | * - EE -> trusted root |
| 2127 | * - EE -> intermedate CA -> trusted root |
| 2128 | * - if relevant: EE untrusted |
| 2129 | * - if relevant: EE -> intermediate, untrusted |
| 2130 | * with the aspect under test checked at each relevant level (EE, int, root). |
| 2131 | * For some aspects longer chains are required, but usually length 2 is |
| 2132 | * enough (but length 1 is not in general). |
| 2133 | * |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2134 | * Arguments: |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2135 | * - [in] crt: the cert list EE, C1, ..., Cn |
| 2136 | * - [in] trust_ca: the trusted list R1, ..., Rp |
| 2137 | * - [in] ca_crl, profile: as in verify_with_profile() |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2138 | * - [out] ver_chain: the built and verified chain |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2139 | * |
| 2140 | * Return value: |
| 2141 | * - non-zero if the chain could not be fully built and examined |
| 2142 | * - 0 is the chain was successfully built and examined, |
| 2143 | * even if it was found to be invalid |
Manuel Pégourié-Gonnard | 35407c7 | 2017-06-29 10:45:25 +0200 | [diff] [blame] | 2144 | */ |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2145 | static int x509_crt_verify_chain( |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2146 | mbedtls_x509_crt *crt, |
| 2147 | mbedtls_x509_crt *trust_ca, |
| 2148 | mbedtls_x509_crl *ca_crl, |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 2149 | const mbedtls_x509_crt_profile *profile, |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2150 | mbedtls_x509_crt_verify_chain *ver_chain, |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2151 | mbedtls_x509_crt_restart_ctx *rs_ctx ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2152 | { |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2153 | int ret; |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2154 | uint32_t *flags; |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2155 | mbedtls_x509_crt_verify_chain_item *cur; |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2156 | mbedtls_x509_crt *child; |
Manuel Pégourié-Gonnard | 58dcd2d | 2017-07-03 21:35:04 +0200 | [diff] [blame] | 2157 | mbedtls_x509_crt *parent; |
Manuel Pégourié-Gonnard | 6e78674 | 2017-07-03 23:47:44 +0200 | [diff] [blame] | 2158 | int parent_is_trusted = 0; |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2159 | int child_is_trusted = 0; |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2160 | int signature_is_good = 0; |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2161 | int self_cnt = 0; |
Manuel Pégourié-Gonnard | 8f8c282 | 2017-07-03 21:25:10 +0200 | [diff] [blame] | 2162 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2163 | child = crt; |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2164 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2165 | while( 1 ) { |
| 2166 | /* Add certificate to the verification chain */ |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2167 | cur = &ver_chain->items[ver_chain->len]; |
| 2168 | cur->crt = child; |
| 2169 | flags = &cur->flags; |
| 2170 | ver_chain->len++; |
Manuel Pégourié-Gonnard | 66fac75 | 2017-07-03 21:39:21 +0200 | [diff] [blame] | 2171 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2172 | /* Check time-validity (all certificates) */ |
| 2173 | if( mbedtls_x509_time_is_past( &child->valid_to ) ) |
| 2174 | *flags |= MBEDTLS_X509_BADCERT_EXPIRED; |
Manuel Pégourié-Gonnard | 66fac75 | 2017-07-03 21:39:21 +0200 | [diff] [blame] | 2175 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2176 | if( mbedtls_x509_time_is_future( &child->valid_from ) ) |
| 2177 | *flags |= MBEDTLS_X509_BADCERT_FUTURE; |
Manuel Pégourié-Gonnard | cb39610 | 2017-07-04 00:00:24 +0200 | [diff] [blame] | 2178 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2179 | /* Stop here for trusted roots (but not for trusted EE certs) */ |
| 2180 | if( child_is_trusted ) |
| 2181 | return( 0 ); |
Manuel Pégourié-Gonnard | 66fac75 | 2017-07-03 21:39:21 +0200 | [diff] [blame] | 2182 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2183 | /* Check signature algorithm: MD & PK algs */ |
| 2184 | if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 ) |
| 2185 | *flags |= MBEDTLS_X509_BADCERT_BAD_MD; |
Manuel Pégourié-Gonnard | 66fac75 | 2017-07-03 21:39:21 +0200 | [diff] [blame] | 2186 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2187 | if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 ) |
| 2188 | *flags |= MBEDTLS_X509_BADCERT_BAD_PK; |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 2189 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2190 | /* Special case: EE certs that are locally trusted */ |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2191 | if( ver_chain->len == 1 && |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2192 | x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 ) |
| 2193 | { |
| 2194 | return( 0 ); |
| 2195 | } |
Manuel Pégourié-Gonnard | 8f8c282 | 2017-07-03 21:25:10 +0200 | [diff] [blame] | 2196 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2197 | /* Look for a parent in trusted CAs or up the chain */ |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2198 | ret = x509_crt_find_parent( child, trust_ca, &parent, |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2199 | &parent_is_trusted, &signature_is_good, |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2200 | ver_chain->len - 1, self_cnt, rs_ctx ); |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2201 | |
| 2202 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 2203 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) { |
| 2204 | // TODO: stave state |
| 2205 | return( ret ); |
| 2206 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 2207 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2208 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2209 | /* No parent? We're done here */ |
| 2210 | if( parent == NULL ) |
| 2211 | { |
| 2212 | *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED; |
| 2213 | return( 0 ); |
| 2214 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2215 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2216 | /* Count intermediate self-issued (not necessarily self-signed) certs. |
| 2217 | * These can occur with some strategies for key rollover, see [SIRO], |
| 2218 | * and should be excluded from max_pathlen checks. */ |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2219 | if( ver_chain->len != 1 && |
Manuel Pégourié-Gonnard | 24611f9 | 2017-08-09 10:28:07 +0200 | [diff] [blame] | 2220 | x509_name_cmp( &child->issuer, &child->subject ) == 0 ) |
| 2221 | { |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2222 | self_cnt++; |
Manuel Pégourié-Gonnard | 24611f9 | 2017-08-09 10:28:07 +0200 | [diff] [blame] | 2223 | } |
Manuel Pégourié-Gonnard | fd6c85c | 2014-11-20 16:34:20 +0100 | [diff] [blame] | 2224 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2225 | /* path_cnt is 0 for the first intermediate CA, |
| 2226 | * and if parent is trusted it's not an intermediate CA */ |
| 2227 | if( ! parent_is_trusted && |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2228 | ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA ) |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2229 | { |
| 2230 | /* return immediately to avoid overflow the chain array */ |
| 2231 | return( MBEDTLS_ERR_X509_FATAL_ERROR ); |
| 2232 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2233 | |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2234 | /* signature was check while searching parent */ |
| 2235 | if( ! signature_is_good ) |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2236 | *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED; |
| 2237 | |
| 2238 | /* check size of signing key */ |
| 2239 | if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 ) |
| 2240 | *flags |= MBEDTLS_X509_BADCERT_BAD_KEY; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2242 | #if defined(MBEDTLS_X509_CRL_PARSE_C) |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2243 | /* Check trusted CA's CRL for the given crt */ |
Manuel Pégourié-Gonnard | 562df40 | 2017-08-08 18:09:14 +0200 | [diff] [blame] | 2244 | *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile ); |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2245 | #else |
| 2246 | (void) ca_crl; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2247 | #endif |
| 2248 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2249 | /* prepare for next iteration */ |
| 2250 | child = parent; |
| 2251 | parent = NULL; |
| 2252 | child_is_trusted = parent_is_trusted; |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2253 | signature_is_good = 0; |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2254 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | /* |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2258 | * Check for CN match |
| 2259 | */ |
| 2260 | static int x509_crt_check_cn( const mbedtls_x509_buf *name, |
| 2261 | const char *cn, size_t cn_len ) |
| 2262 | { |
| 2263 | /* try exact match */ |
| 2264 | if( name->len == cn_len && |
| 2265 | x509_memcasecmp( cn, name->p, cn_len ) == 0 ) |
| 2266 | { |
| 2267 | return( 0 ); |
| 2268 | } |
| 2269 | |
| 2270 | /* try wildcard match */ |
| 2271 | if( name->len > 2 && |
| 2272 | memcmp( name->p, "*.", 2 ) == 0 && |
| 2273 | x509_check_wildcard( cn, name ) == 0 ) |
| 2274 | { |
| 2275 | return( 0 ); |
| 2276 | } |
| 2277 | |
| 2278 | return( -1 ); |
| 2279 | } |
| 2280 | |
| 2281 | /* |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 2282 | * Verify the requested CN - only call this if cn is not NULL! |
| 2283 | */ |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2284 | static void x509_crt_verify_name( const mbedtls_x509_crt *crt, |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 2285 | const char *cn, |
| 2286 | uint32_t *flags ) |
| 2287 | { |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2288 | const mbedtls_x509_name *name; |
| 2289 | const mbedtls_x509_sequence *cur; |
| 2290 | size_t cn_len = strlen( cn ); |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 2291 | |
| 2292 | if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) |
| 2293 | { |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2294 | for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next ) |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 2295 | { |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2296 | if( x509_crt_check_cn( &cur->buf, cn, cn_len ) == 0 ) |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 2297 | break; |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | if( cur == NULL ) |
| 2301 | *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH; |
| 2302 | } |
| 2303 | else |
| 2304 | { |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2305 | for( name = &crt->subject; name != NULL; name = name->next ) |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 2306 | { |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2307 | if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 && |
| 2308 | x509_crt_check_cn( &name->val, cn, cn_len ) == 0 ) |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 2309 | { |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2310 | break; |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 2311 | } |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | if( name == NULL ) |
| 2315 | *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH; |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | /* |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 2320 | * Merge the flags for all certs in the chain, after calling callback |
| 2321 | */ |
| 2322 | static int x509_crt_merge_flags_with_cb( |
| 2323 | uint32_t *flags, |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2324 | const mbedtls_x509_crt_verify_chain *ver_chain, |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 2325 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 2326 | void *p_vrfy ) |
| 2327 | { |
| 2328 | int ret; |
Manuel Pégourié-Gonnard | 505c395 | 2017-07-05 17:36:47 +0200 | [diff] [blame] | 2329 | size_t i; |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 2330 | uint32_t cur_flags; |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2331 | const mbedtls_x509_crt_verify_chain_item *cur; |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 2332 | |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2333 | for( i = ver_chain->len; i != 0; --i ) |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 2334 | { |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2335 | cur = &ver_chain->items[i-1]; |
| 2336 | cur_flags = cur->flags; |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 2337 | |
| 2338 | if( NULL != f_vrfy ) |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2339 | if( ( ret = f_vrfy( p_vrfy, cur->crt, i-1, &cur_flags ) ) != 0 ) |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 2340 | return( ret ); |
| 2341 | |
| 2342 | *flags |= cur_flags; |
| 2343 | } |
| 2344 | |
| 2345 | return( 0 ); |
| 2346 | } |
| 2347 | |
| 2348 | /* |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 2349 | * Verify the certificate validity (default profile, not restartable) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2350 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2351 | int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt, |
| 2352 | mbedtls_x509_crt *trust_ca, |
| 2353 | mbedtls_x509_crl *ca_crl, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2354 | const char *cn, uint32_t *flags, |
| 2355 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 2356 | void *p_vrfy ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2357 | { |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 2358 | return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl, |
| 2359 | &mbedtls_x509_crt_profile_default, cn, flags, |
| 2360 | f_vrfy, p_vrfy, NULL ) ); |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 2361 | } |
| 2362 | |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 2363 | /* |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 2364 | * Verify the certificate validity (user-chosen profile, not restartable) |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 2365 | */ |
| 2366 | int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt, |
| 2367 | mbedtls_x509_crt *trust_ca, |
| 2368 | mbedtls_x509_crl *ca_crl, |
| 2369 | const mbedtls_x509_crt_profile *profile, |
| 2370 | const char *cn, uint32_t *flags, |
| 2371 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 2372 | void *p_vrfy ) |
| 2373 | { |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 2374 | return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl, |
| 2375 | profile, cn, flags, f_vrfy, p_vrfy, NULL ) ); |
| 2376 | } |
| 2377 | |
| 2378 | /* |
| 2379 | * Verify the certificate validity, with profile, restartable version |
| 2380 | * |
| 2381 | * This function: |
| 2382 | * - checks the requested CN (if any) |
| 2383 | * - checks the type and size of the EE cert's key, |
| 2384 | * as that isn't done as part of chain building/verification currently |
| 2385 | * - builds and verifies the chain |
| 2386 | * - then calls the callback and merges the flags |
| 2387 | */ |
| 2388 | int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt, |
| 2389 | mbedtls_x509_crt *trust_ca, |
| 2390 | mbedtls_x509_crl *ca_crl, |
| 2391 | const mbedtls_x509_crt_profile *profile, |
| 2392 | const char *cn, uint32_t *flags, |
| 2393 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 2394 | void *p_vrfy, |
| 2395 | mbedtls_x509_crt_restart_ctx *rs_ctx ) |
| 2396 | { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2397 | int ret; |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 2398 | mbedtls_pk_type_t pk_type; |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2399 | mbedtls_x509_crt_verify_chain ver_chain; |
| 2400 | uint32_t *ee_flags = &ver_chain.items[0].flags; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2401 | |
| 2402 | *flags = 0; |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2403 | memset( &ver_chain, 0, sizeof( ver_chain ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2404 | |
Manuel Pégourié-Gonnard | d15795a | 2017-06-22 12:19:27 +0200 | [diff] [blame] | 2405 | if( profile == NULL ) |
| 2406 | { |
| 2407 | ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 2408 | goto exit; |
| 2409 | } |
| 2410 | |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 2411 | /* check name if requested */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2412 | if( cn != NULL ) |
Manuel Pégourié-Gonnard | c547d1a | 2017-07-05 13:28:45 +0200 | [diff] [blame] | 2413 | x509_crt_verify_name( crt, cn, ee_flags ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2414 | |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 2415 | /* Check the type and size of the key */ |
| 2416 | pk_type = mbedtls_pk_get_type( &crt->pk ); |
| 2417 | |
| 2418 | if( x509_profile_check_pk_alg( profile, pk_type ) != 0 ) |
Manuel Pégourié-Gonnard | c547d1a | 2017-07-05 13:28:45 +0200 | [diff] [blame] | 2419 | *ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK; |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 2420 | |
| 2421 | if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 ) |
Manuel Pégourié-Gonnard | c547d1a | 2017-07-05 13:28:45 +0200 | [diff] [blame] | 2422 | *ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY; |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 2423 | |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2424 | /* Check the chain */ |
Manuel Pégourié-Gonnard | 505c395 | 2017-07-05 17:36:47 +0200 | [diff] [blame] | 2425 | ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile, |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2426 | &ver_chain, rs_ctx ); |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2427 | |
| 2428 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 2429 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) { |
| 2430 | // TODO: stave state |
| 2431 | return( ret ); |
| 2432 | } |
| 2433 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 2434 | |
Manuel Pégourié-Gonnard | c547d1a | 2017-07-05 13:28:45 +0200 | [diff] [blame] | 2435 | if( ret != 0 ) |
| 2436 | goto exit; |
| 2437 | |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 2438 | /* Build final flags, calling callback on the way if any */ |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame^] | 2439 | ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2440 | |
Manuel Pégourié-Gonnard | d15795a | 2017-06-22 12:19:27 +0200 | [diff] [blame] | 2441 | exit: |
Manuel Pégourié-Gonnard | 9107b5f | 2017-07-06 12:16:25 +0200 | [diff] [blame] | 2442 | /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by |
| 2443 | * the SSL module for authmode optional, but non-zero return from the |
| 2444 | * callback means a fatal error so it shouldn't be ignored */ |
Manuel Pégourié-Gonnard | 31458a1 | 2017-06-26 10:11:49 +0200 | [diff] [blame] | 2445 | if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ) |
| 2446 | ret = MBEDTLS_ERR_X509_FATAL_ERROR; |
| 2447 | |
Manuel Pégourié-Gonnard | d15795a | 2017-06-22 12:19:27 +0200 | [diff] [blame] | 2448 | if( ret != 0 ) |
| 2449 | { |
| 2450 | *flags = (uint32_t) -1; |
| 2451 | return( ret ); |
| 2452 | } |
| 2453 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2454 | if( *flags != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2455 | return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2456 | |
| 2457 | return( 0 ); |
| 2458 | } |
| 2459 | |
| 2460 | /* |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 2461 | * Initialize a certificate chain |
| 2462 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2463 | void mbedtls_x509_crt_init( mbedtls_x509_crt *crt ) |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 2464 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2465 | memset( crt, 0, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 2466 | } |
| 2467 | |
| 2468 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2469 | * Unallocate all certificate data |
| 2470 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2471 | void mbedtls_x509_crt_free( mbedtls_x509_crt *crt ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2472 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2473 | mbedtls_x509_crt *cert_cur = crt; |
| 2474 | mbedtls_x509_crt *cert_prv; |
| 2475 | mbedtls_x509_name *name_cur; |
| 2476 | mbedtls_x509_name *name_prv; |
| 2477 | mbedtls_x509_sequence *seq_cur; |
| 2478 | mbedtls_x509_sequence *seq_prv; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2479 | |
| 2480 | if( crt == NULL ) |
| 2481 | return; |
| 2482 | |
| 2483 | do |
| 2484 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2485 | mbedtls_pk_free( &cert_cur->pk ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2486 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2487 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 2488 | mbedtls_free( cert_cur->sig_opts ); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 2489 | #endif |
| 2490 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2491 | name_cur = cert_cur->issuer.next; |
| 2492 | while( name_cur != NULL ) |
| 2493 | { |
| 2494 | name_prv = name_cur; |
| 2495 | name_cur = name_cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2496 | mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) ); |
| 2497 | mbedtls_free( name_prv ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2498 | } |
| 2499 | |
| 2500 | name_cur = cert_cur->subject.next; |
| 2501 | while( name_cur != NULL ) |
| 2502 | { |
| 2503 | name_prv = name_cur; |
| 2504 | name_cur = name_cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2505 | mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) ); |
| 2506 | mbedtls_free( name_prv ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | seq_cur = cert_cur->ext_key_usage.next; |
| 2510 | while( seq_cur != NULL ) |
| 2511 | { |
| 2512 | seq_prv = seq_cur; |
| 2513 | seq_cur = seq_cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2514 | mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) ); |
| 2515 | mbedtls_free( seq_prv ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | seq_cur = cert_cur->subject_alt_names.next; |
| 2519 | while( seq_cur != NULL ) |
| 2520 | { |
| 2521 | seq_prv = seq_cur; |
| 2522 | seq_cur = seq_cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2523 | mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) ); |
| 2524 | mbedtls_free( seq_prv ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | if( cert_cur->raw.p != NULL ) |
| 2528 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2529 | mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len ); |
| 2530 | mbedtls_free( cert_cur->raw.p ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | cert_cur = cert_cur->next; |
| 2534 | } |
| 2535 | while( cert_cur != NULL ); |
| 2536 | |
| 2537 | cert_cur = crt; |
| 2538 | do |
| 2539 | { |
| 2540 | cert_prv = cert_cur; |
| 2541 | cert_cur = cert_cur->next; |
| 2542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2543 | mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2544 | if( cert_prv != crt ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2545 | mbedtls_free( cert_prv ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2546 | } |
| 2547 | while( cert_cur != NULL ); |
| 2548 | } |
| 2549 | |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 2550 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 2551 | /* |
| 2552 | * Initialize a restart context |
| 2553 | */ |
| 2554 | void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx ) |
| 2555 | { |
| 2556 | mbedtls_ecdsa_restart_init( &ctx->ecdsa ); |
| 2557 | } |
| 2558 | |
| 2559 | /* |
| 2560 | * Free the components of a restart context |
| 2561 | */ |
| 2562 | void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx ) |
| 2563 | { |
| 2564 | if( ctx == NULL ) |
| 2565 | return; |
| 2566 | |
| 2567 | mbedtls_ecdsa_restart_free( &ctx->ecdsa ); |
| 2568 | } |
| 2569 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 2570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2571 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |