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