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