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