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