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