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