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