mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
Jasper Wallace
Date:
Fri Sep 29 19:50:30 2017 +0100
Revision:
2:bbdeda018a3c
Parent:
1:9ebc941037d5
Update to mbedtls 2.6.0, many changes.

Changes to mbedtls sources made:

in include/mbedtls/config.h comment out:

#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C

uncomment:

#define MBEDTLS_NO_PLATFORM_ENTROPY

remove the following directorys:

programs
yotta
visualc

Who changed what in which revision?

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