Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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