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