Modified mbed TLS headers for AES functionality only to reduce build size

Dependents:   BLE_Gateway_Linker_fix BLE_Gateway

Fork of mbedtls by sandbox

Committer:
Christopher Haster
Date:
Fri Jan 22 16:44:49 2016 -0600
Revision:
1:24750b9ad5ef
Initial move of mbedtls to mercurial

Who changed what in which revision?

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