mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
features/mbedtls/src/x509.c@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* |
elessair | 0:f269e3021894 | 2 | * X.509 common functions for parsing and verification |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
elessair | 0:f269e3021894 | 5 | * SPDX-License-Identifier: Apache-2.0 |
elessair | 0:f269e3021894 | 6 | * |
elessair | 0:f269e3021894 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
elessair | 0:f269e3021894 | 8 | * not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 9 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 10 | * |
elessair | 0:f269e3021894 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 12 | * |
elessair | 0:f269e3021894 | 13 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
elessair | 0:f269e3021894 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 16 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 17 | * limitations under the License. |
elessair | 0:f269e3021894 | 18 | * |
elessair | 0:f269e3021894 | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
elessair | 0:f269e3021894 | 20 | */ |
elessair | 0:f269e3021894 | 21 | /* |
elessair | 0:f269e3021894 | 22 | * The ITU-T X.509 standard defines a certificate format for PKI. |
elessair | 0:f269e3021894 | 23 | * |
elessair | 0:f269e3021894 | 24 | * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs) |
elessair | 0:f269e3021894 | 25 | * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs) |
elessair | 0:f269e3021894 | 26 | * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10) |
elessair | 0:f269e3021894 | 27 | * |
elessair | 0:f269e3021894 | 28 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf |
elessair | 0:f269e3021894 | 29 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf |
elessair | 0:f269e3021894 | 30 | */ |
elessair | 0:f269e3021894 | 31 | |
elessair | 0:f269e3021894 | 32 | #if !defined(MBEDTLS_CONFIG_FILE) |
elessair | 0:f269e3021894 | 33 | #include "mbedtls/config.h" |
elessair | 0:f269e3021894 | 34 | #else |
elessair | 0:f269e3021894 | 35 | #include MBEDTLS_CONFIG_FILE |
elessair | 0:f269e3021894 | 36 | #endif |
elessair | 0:f269e3021894 | 37 | |
elessair | 0:f269e3021894 | 38 | #if defined(MBEDTLS_X509_USE_C) |
elessair | 0:f269e3021894 | 39 | |
elessair | 0:f269e3021894 | 40 | #include "mbedtls/x509.h" |
elessair | 0:f269e3021894 | 41 | #include "mbedtls/asn1.h" |
elessair | 0:f269e3021894 | 42 | #include "mbedtls/oid.h" |
elessair | 0:f269e3021894 | 43 | |
elessair | 0:f269e3021894 | 44 | #include <stdio.h> |
elessair | 0:f269e3021894 | 45 | #include <string.h> |
elessair | 0:f269e3021894 | 46 | |
elessair | 0:f269e3021894 | 47 | #if defined(MBEDTLS_PEM_PARSE_C) |
elessair | 0:f269e3021894 | 48 | #include "mbedtls/pem.h" |
elessair | 0:f269e3021894 | 49 | #endif |
elessair | 0:f269e3021894 | 50 | |
elessair | 0:f269e3021894 | 51 | #if defined(MBEDTLS_PLATFORM_C) |
elessair | 0:f269e3021894 | 52 | #include "mbedtls/platform.h" |
elessair | 0:f269e3021894 | 53 | #else |
elessair | 0:f269e3021894 | 54 | #include <stdio.h> |
elessair | 0:f269e3021894 | 55 | #include <stdlib.h> |
elessair | 0:f269e3021894 | 56 | #define mbedtls_free free |
elessair | 0:f269e3021894 | 57 | #define mbedtls_calloc calloc |
elessair | 0:f269e3021894 | 58 | #define mbedtls_printf printf |
elessair | 0:f269e3021894 | 59 | #define mbedtls_snprintf snprintf |
elessair | 0:f269e3021894 | 60 | #endif |
elessair | 0:f269e3021894 | 61 | |
elessair | 0:f269e3021894 | 62 | |
elessair | 0:f269e3021894 | 63 | #if defined(MBEDTLS_HAVE_TIME) |
elessair | 0:f269e3021894 | 64 | #include "mbedtls/platform_time.h" |
elessair | 0:f269e3021894 | 65 | #endif |
elessair | 0:f269e3021894 | 66 | |
elessair | 0:f269e3021894 | 67 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
elessair | 0:f269e3021894 | 68 | #include <windows.h> |
elessair | 0:f269e3021894 | 69 | #else |
elessair | 0:f269e3021894 | 70 | #include <time.h> |
elessair | 0:f269e3021894 | 71 | #endif |
elessair | 0:f269e3021894 | 72 | |
elessair | 0:f269e3021894 | 73 | #if defined(MBEDTLS_FS_IO) |
elessair | 0:f269e3021894 | 74 | #include <stdio.h> |
elessair | 0:f269e3021894 | 75 | #if !defined(_WIN32) |
elessair | 0:f269e3021894 | 76 | #include <sys/types.h> |
elessair | 0:f269e3021894 | 77 | #include <sys/stat.h> |
elessair | 0:f269e3021894 | 78 | #include <dirent.h> |
elessair | 0:f269e3021894 | 79 | #endif |
elessair | 0:f269e3021894 | 80 | #endif |
elessair | 0:f269e3021894 | 81 | |
elessair | 0:f269e3021894 | 82 | #define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); } |
elessair | 0:f269e3021894 | 83 | #define CHECK_RANGE(min, max, val) if( val < min || val > max ){ return( ret ); } |
elessair | 0:f269e3021894 | 84 | |
elessair | 0:f269e3021894 | 85 | /* |
elessair | 0:f269e3021894 | 86 | * CertificateSerialNumber ::= INTEGER |
elessair | 0:f269e3021894 | 87 | */ |
elessair | 0:f269e3021894 | 88 | int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end, |
elessair | 0:f269e3021894 | 89 | mbedtls_x509_buf *serial ) |
elessair | 0:f269e3021894 | 90 | { |
elessair | 0:f269e3021894 | 91 | int ret; |
elessair | 0:f269e3021894 | 92 | |
elessair | 0:f269e3021894 | 93 | if( ( end - *p ) < 1 ) |
elessair | 0:f269e3021894 | 94 | return( MBEDTLS_ERR_X509_INVALID_SERIAL + |
elessair | 0:f269e3021894 | 95 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
elessair | 0:f269e3021894 | 96 | |
elessair | 0:f269e3021894 | 97 | if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) && |
elessair | 0:f269e3021894 | 98 | **p != MBEDTLS_ASN1_INTEGER ) |
elessair | 0:f269e3021894 | 99 | return( MBEDTLS_ERR_X509_INVALID_SERIAL + |
elessair | 0:f269e3021894 | 100 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
elessair | 0:f269e3021894 | 101 | |
elessair | 0:f269e3021894 | 102 | serial->tag = *(*p)++; |
elessair | 0:f269e3021894 | 103 | |
elessair | 0:f269e3021894 | 104 | if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 ) |
elessair | 0:f269e3021894 | 105 | return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret ); |
elessair | 0:f269e3021894 | 106 | |
elessair | 0:f269e3021894 | 107 | serial->p = *p; |
elessair | 0:f269e3021894 | 108 | *p += serial->len; |
elessair | 0:f269e3021894 | 109 | |
elessair | 0:f269e3021894 | 110 | return( 0 ); |
elessair | 0:f269e3021894 | 111 | } |
elessair | 0:f269e3021894 | 112 | |
elessair | 0:f269e3021894 | 113 | /* Get an algorithm identifier without parameters (eg for signatures) |
elessair | 0:f269e3021894 | 114 | * |
elessair | 0:f269e3021894 | 115 | * AlgorithmIdentifier ::= SEQUENCE { |
elessair | 0:f269e3021894 | 116 | * algorithm OBJECT IDENTIFIER, |
elessair | 0:f269e3021894 | 117 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
elessair | 0:f269e3021894 | 118 | */ |
elessair | 0:f269e3021894 | 119 | int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end, |
elessair | 0:f269e3021894 | 120 | mbedtls_x509_buf *alg ) |
elessair | 0:f269e3021894 | 121 | { |
elessair | 0:f269e3021894 | 122 | int ret; |
elessair | 0:f269e3021894 | 123 | |
elessair | 0:f269e3021894 | 124 | if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 ) |
elessair | 0:f269e3021894 | 125 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
elessair | 0:f269e3021894 | 126 | |
elessair | 0:f269e3021894 | 127 | return( 0 ); |
elessair | 0:f269e3021894 | 128 | } |
elessair | 0:f269e3021894 | 129 | |
elessair | 0:f269e3021894 | 130 | /* |
elessair | 0:f269e3021894 | 131 | * Parse an algorithm identifier with (optional) paramaters |
elessair | 0:f269e3021894 | 132 | */ |
elessair | 0:f269e3021894 | 133 | int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end, |
elessair | 0:f269e3021894 | 134 | mbedtls_x509_buf *alg, mbedtls_x509_buf *params ) |
elessair | 0:f269e3021894 | 135 | { |
elessair | 0:f269e3021894 | 136 | int ret; |
elessair | 0:f269e3021894 | 137 | |
elessair | 0:f269e3021894 | 138 | if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 ) |
elessair | 0:f269e3021894 | 139 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
elessair | 0:f269e3021894 | 140 | |
elessair | 0:f269e3021894 | 141 | return( 0 ); |
elessair | 0:f269e3021894 | 142 | } |
elessair | 0:f269e3021894 | 143 | |
elessair | 0:f269e3021894 | 144 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
elessair | 0:f269e3021894 | 145 | /* |
elessair | 0:f269e3021894 | 146 | * HashAlgorithm ::= AlgorithmIdentifier |
elessair | 0:f269e3021894 | 147 | * |
elessair | 0:f269e3021894 | 148 | * AlgorithmIdentifier ::= SEQUENCE { |
elessair | 0:f269e3021894 | 149 | * algorithm OBJECT IDENTIFIER, |
elessair | 0:f269e3021894 | 150 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
elessair | 0:f269e3021894 | 151 | * |
elessair | 0:f269e3021894 | 152 | * For HashAlgorithm, parameters MUST be NULL or absent. |
elessair | 0:f269e3021894 | 153 | */ |
elessair | 0:f269e3021894 | 154 | static int x509_get_hash_alg( const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg ) |
elessair | 0:f269e3021894 | 155 | { |
elessair | 0:f269e3021894 | 156 | int ret; |
elessair | 0:f269e3021894 | 157 | unsigned char *p; |
elessair | 0:f269e3021894 | 158 | const unsigned char *end; |
elessair | 0:f269e3021894 | 159 | mbedtls_x509_buf md_oid; |
elessair | 0:f269e3021894 | 160 | size_t len; |
elessair | 0:f269e3021894 | 161 | |
elessair | 0:f269e3021894 | 162 | /* Make sure we got a SEQUENCE and setup bounds */ |
elessair | 0:f269e3021894 | 163 | if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) |
elessair | 0:f269e3021894 | 164 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
elessair | 0:f269e3021894 | 165 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
elessair | 0:f269e3021894 | 166 | |
elessair | 0:f269e3021894 | 167 | p = (unsigned char *) alg->p; |
elessair | 0:f269e3021894 | 168 | end = p + alg->len; |
elessair | 0:f269e3021894 | 169 | |
elessair | 0:f269e3021894 | 170 | if( p >= end ) |
elessair | 0:f269e3021894 | 171 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
elessair | 0:f269e3021894 | 172 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
elessair | 0:f269e3021894 | 173 | |
elessair | 0:f269e3021894 | 174 | /* Parse md_oid */ |
elessair | 0:f269e3021894 | 175 | md_oid.tag = *p; |
elessair | 0:f269e3021894 | 176 | |
elessair | 0:f269e3021894 | 177 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 ) |
elessair | 0:f269e3021894 | 178 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
elessair | 0:f269e3021894 | 179 | |
elessair | 0:f269e3021894 | 180 | md_oid.p = p; |
elessair | 0:f269e3021894 | 181 | p += md_oid.len; |
elessair | 0:f269e3021894 | 182 | |
elessair | 0:f269e3021894 | 183 | /* Get md_alg from md_oid */ |
elessair | 0:f269e3021894 | 184 | if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 ) |
elessair | 0:f269e3021894 | 185 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
elessair | 0:f269e3021894 | 186 | |
elessair | 0:f269e3021894 | 187 | /* Make sure params is absent of NULL */ |
elessair | 0:f269e3021894 | 188 | if( p == end ) |
elessair | 0:f269e3021894 | 189 | return( 0 ); |
elessair | 0:f269e3021894 | 190 | |
elessair | 0:f269e3021894 | 191 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 ) |
elessair | 0:f269e3021894 | 192 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
elessair | 0:f269e3021894 | 193 | |
elessair | 0:f269e3021894 | 194 | if( p != end ) |
elessair | 0:f269e3021894 | 195 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
elessair | 0:f269e3021894 | 196 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
elessair | 0:f269e3021894 | 197 | |
elessair | 0:f269e3021894 | 198 | return( 0 ); |
elessair | 0:f269e3021894 | 199 | } |
elessair | 0:f269e3021894 | 200 | |
elessair | 0:f269e3021894 | 201 | /* |
elessair | 0:f269e3021894 | 202 | * RSASSA-PSS-params ::= SEQUENCE { |
elessair | 0:f269e3021894 | 203 | * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier, |
elessair | 0:f269e3021894 | 204 | * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier, |
elessair | 0:f269e3021894 | 205 | * saltLength [2] INTEGER DEFAULT 20, |
elessair | 0:f269e3021894 | 206 | * trailerField [3] INTEGER DEFAULT 1 } |
elessair | 0:f269e3021894 | 207 | * -- Note that the tags in this Sequence are explicit. |
elessair | 0:f269e3021894 | 208 | * |
elessair | 0:f269e3021894 | 209 | * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value |
elessair | 0:f269e3021894 | 210 | * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other |
elessair | 0:f269e3021894 | 211 | * option. Enfore this at parsing time. |
elessair | 0:f269e3021894 | 212 | */ |
elessair | 0:f269e3021894 | 213 | int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params, |
elessair | 0:f269e3021894 | 214 | mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md, |
elessair | 0:f269e3021894 | 215 | int *salt_len ) |
elessair | 0:f269e3021894 | 216 | { |
elessair | 0:f269e3021894 | 217 | int ret; |
elessair | 0:f269e3021894 | 218 | unsigned char *p; |
elessair | 0:f269e3021894 | 219 | const unsigned char *end, *end2; |
elessair | 0:f269e3021894 | 220 | size_t len; |
elessair | 0:f269e3021894 | 221 | mbedtls_x509_buf alg_id, alg_params; |
elessair | 0:f269e3021894 | 222 | |
elessair | 0:f269e3021894 | 223 | /* First set everything to defaults */ |
elessair | 0:f269e3021894 | 224 | *md_alg = MBEDTLS_MD_SHA1; |
elessair | 0:f269e3021894 | 225 | *mgf_md = MBEDTLS_MD_SHA1; |
elessair | 0:f269e3021894 | 226 | *salt_len = 20; |
elessair | 0:f269e3021894 | 227 | |
elessair | 0:f269e3021894 | 228 | /* Make sure params is a SEQUENCE and setup bounds */ |
elessair | 0:f269e3021894 | 229 | if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) |
elessair | 0:f269e3021894 | 230 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
elessair | 0:f269e3021894 | 231 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
elessair | 0:f269e3021894 | 232 | |
elessair | 0:f269e3021894 | 233 | p = (unsigned char *) params->p; |
elessair | 0:f269e3021894 | 234 | end = p + params->len; |
elessair | 0:f269e3021894 | 235 | |
elessair | 0:f269e3021894 | 236 | if( p == end ) |
elessair | 0:f269e3021894 | 237 | return( 0 ); |
elessair | 0:f269e3021894 | 238 | |
elessair | 0:f269e3021894 | 239 | /* |
elessair | 0:f269e3021894 | 240 | * HashAlgorithm |
elessair | 0:f269e3021894 | 241 | */ |
elessair | 0:f269e3021894 | 242 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
elessair | 0:f269e3021894 | 243 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 ) |
elessair | 0:f269e3021894 | 244 | { |
elessair | 0:f269e3021894 | 245 | end2 = p + len; |
elessair | 0:f269e3021894 | 246 | |
elessair | 0:f269e3021894 | 247 | /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */ |
elessair | 0:f269e3021894 | 248 | if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 ) |
elessair | 0:f269e3021894 | 249 | return( ret ); |
elessair | 0:f269e3021894 | 250 | |
elessair | 0:f269e3021894 | 251 | if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 ) |
elessair | 0:f269e3021894 | 252 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
elessair | 0:f269e3021894 | 253 | |
elessair | 0:f269e3021894 | 254 | if( p != end2 ) |
elessair | 0:f269e3021894 | 255 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
elessair | 0:f269e3021894 | 256 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
elessair | 0:f269e3021894 | 257 | } |
elessair | 0:f269e3021894 | 258 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
elessair | 0:f269e3021894 | 259 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
elessair | 0:f269e3021894 | 260 | |
elessair | 0:f269e3021894 | 261 | if( p == end ) |
elessair | 0:f269e3021894 | 262 | return( 0 ); |
elessair | 0:f269e3021894 | 263 | |
elessair | 0:f269e3021894 | 264 | /* |
elessair | 0:f269e3021894 | 265 | * MaskGenAlgorithm |
elessair | 0:f269e3021894 | 266 | */ |
elessair | 0:f269e3021894 | 267 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
elessair | 0:f269e3021894 | 268 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 ) |
elessair | 0:f269e3021894 | 269 | { |
elessair | 0:f269e3021894 | 270 | end2 = p + len; |
elessair | 0:f269e3021894 | 271 | |
elessair | 0:f269e3021894 | 272 | /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */ |
elessair | 0:f269e3021894 | 273 | if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 ) |
elessair | 0:f269e3021894 | 274 | return( ret ); |
elessair | 0:f269e3021894 | 275 | |
elessair | 0:f269e3021894 | 276 | /* Only MFG1 is recognised for now */ |
elessair | 0:f269e3021894 | 277 | if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 ) |
elessair | 0:f269e3021894 | 278 | return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE + |
elessair | 0:f269e3021894 | 279 | MBEDTLS_ERR_OID_NOT_FOUND ); |
elessair | 0:f269e3021894 | 280 | |
elessair | 0:f269e3021894 | 281 | /* Parse HashAlgorithm */ |
elessair | 0:f269e3021894 | 282 | if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 ) |
elessair | 0:f269e3021894 | 283 | return( ret ); |
elessair | 0:f269e3021894 | 284 | |
elessair | 0:f269e3021894 | 285 | if( p != end2 ) |
elessair | 0:f269e3021894 | 286 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
elessair | 0:f269e3021894 | 287 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
elessair | 0:f269e3021894 | 288 | } |
elessair | 0:f269e3021894 | 289 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
elessair | 0:f269e3021894 | 290 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
elessair | 0:f269e3021894 | 291 | |
elessair | 0:f269e3021894 | 292 | if( p == end ) |
elessair | 0:f269e3021894 | 293 | return( 0 ); |
elessair | 0:f269e3021894 | 294 | |
elessair | 0:f269e3021894 | 295 | /* |
elessair | 0:f269e3021894 | 296 | * salt_len |
elessair | 0:f269e3021894 | 297 | */ |
elessair | 0:f269e3021894 | 298 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
elessair | 0:f269e3021894 | 299 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 ) |
elessair | 0:f269e3021894 | 300 | { |
elessair | 0:f269e3021894 | 301 | end2 = p + len; |
elessair | 0:f269e3021894 | 302 | |
elessair | 0:f269e3021894 | 303 | if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 ) |
elessair | 0:f269e3021894 | 304 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
elessair | 0:f269e3021894 | 305 | |
elessair | 0:f269e3021894 | 306 | if( p != end2 ) |
elessair | 0:f269e3021894 | 307 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
elessair | 0:f269e3021894 | 308 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
elessair | 0:f269e3021894 | 309 | } |
elessair | 0:f269e3021894 | 310 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
elessair | 0:f269e3021894 | 311 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
elessair | 0:f269e3021894 | 312 | |
elessair | 0:f269e3021894 | 313 | if( p == end ) |
elessair | 0:f269e3021894 | 314 | return( 0 ); |
elessair | 0:f269e3021894 | 315 | |
elessair | 0:f269e3021894 | 316 | /* |
elessair | 0:f269e3021894 | 317 | * trailer_field (if present, must be 1) |
elessair | 0:f269e3021894 | 318 | */ |
elessair | 0:f269e3021894 | 319 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
elessair | 0:f269e3021894 | 320 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 ) |
elessair | 0:f269e3021894 | 321 | { |
elessair | 0:f269e3021894 | 322 | int trailer_field; |
elessair | 0:f269e3021894 | 323 | |
elessair | 0:f269e3021894 | 324 | end2 = p + len; |
elessair | 0:f269e3021894 | 325 | |
elessair | 0:f269e3021894 | 326 | if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 ) |
elessair | 0:f269e3021894 | 327 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
elessair | 0:f269e3021894 | 328 | |
elessair | 0:f269e3021894 | 329 | if( p != end2 ) |
elessair | 0:f269e3021894 | 330 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
elessair | 0:f269e3021894 | 331 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
elessair | 0:f269e3021894 | 332 | |
elessair | 0:f269e3021894 | 333 | if( trailer_field != 1 ) |
elessair | 0:f269e3021894 | 334 | return( MBEDTLS_ERR_X509_INVALID_ALG ); |
elessair | 0:f269e3021894 | 335 | } |
elessair | 0:f269e3021894 | 336 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
elessair | 0:f269e3021894 | 337 | return( MBEDTLS_ERR_X509_INVALID_ALG + ret ); |
elessair | 0:f269e3021894 | 338 | |
elessair | 0:f269e3021894 | 339 | if( p != end ) |
elessair | 0:f269e3021894 | 340 | return( MBEDTLS_ERR_X509_INVALID_ALG + |
elessair | 0:f269e3021894 | 341 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
elessair | 0:f269e3021894 | 342 | |
elessair | 0:f269e3021894 | 343 | return( 0 ); |
elessair | 0:f269e3021894 | 344 | } |
elessair | 0:f269e3021894 | 345 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
elessair | 0:f269e3021894 | 346 | |
elessair | 0:f269e3021894 | 347 | /* |
elessair | 0:f269e3021894 | 348 | * AttributeTypeAndValue ::= SEQUENCE { |
elessair | 0:f269e3021894 | 349 | * type AttributeType, |
elessair | 0:f269e3021894 | 350 | * value AttributeValue } |
elessair | 0:f269e3021894 | 351 | * |
elessair | 0:f269e3021894 | 352 | * AttributeType ::= OBJECT IDENTIFIER |
elessair | 0:f269e3021894 | 353 | * |
elessair | 0:f269e3021894 | 354 | * AttributeValue ::= ANY DEFINED BY AttributeType |
elessair | 0:f269e3021894 | 355 | */ |
elessair | 0:f269e3021894 | 356 | static int x509_get_attr_type_value( unsigned char **p, |
elessair | 0:f269e3021894 | 357 | const unsigned char *end, |
elessair | 0:f269e3021894 | 358 | mbedtls_x509_name *cur ) |
elessair | 0:f269e3021894 | 359 | { |
elessair | 0:f269e3021894 | 360 | int ret; |
elessair | 0:f269e3021894 | 361 | size_t len; |
elessair | 0:f269e3021894 | 362 | mbedtls_x509_buf *oid; |
elessair | 0:f269e3021894 | 363 | mbedtls_x509_buf *val; |
elessair | 0:f269e3021894 | 364 | |
elessair | 0:f269e3021894 | 365 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
elessair | 0:f269e3021894 | 366 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
elessair | 0:f269e3021894 | 367 | return( MBEDTLS_ERR_X509_INVALID_NAME + ret ); |
elessair | 0:f269e3021894 | 368 | |
elessair | 0:f269e3021894 | 369 | if( ( end - *p ) < 1 ) |
elessair | 0:f269e3021894 | 370 | return( MBEDTLS_ERR_X509_INVALID_NAME + |
elessair | 0:f269e3021894 | 371 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
elessair | 0:f269e3021894 | 372 | |
elessair | 0:f269e3021894 | 373 | oid = &cur->oid; |
elessair | 0:f269e3021894 | 374 | oid->tag = **p; |
elessair | 0:f269e3021894 | 375 | |
elessair | 0:f269e3021894 | 376 | if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 ) |
elessair | 0:f269e3021894 | 377 | return( MBEDTLS_ERR_X509_INVALID_NAME + ret ); |
elessair | 0:f269e3021894 | 378 | |
elessair | 0:f269e3021894 | 379 | oid->p = *p; |
elessair | 0:f269e3021894 | 380 | *p += oid->len; |
elessair | 0:f269e3021894 | 381 | |
elessair | 0:f269e3021894 | 382 | if( ( end - *p ) < 1 ) |
elessair | 0:f269e3021894 | 383 | return( MBEDTLS_ERR_X509_INVALID_NAME + |
elessair | 0:f269e3021894 | 384 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
elessair | 0:f269e3021894 | 385 | |
elessair | 0:f269e3021894 | 386 | if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING && |
elessair | 0:f269e3021894 | 387 | **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING && |
elessair | 0:f269e3021894 | 388 | **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING && |
elessair | 0:f269e3021894 | 389 | **p != MBEDTLS_ASN1_BIT_STRING ) |
elessair | 0:f269e3021894 | 390 | return( MBEDTLS_ERR_X509_INVALID_NAME + |
elessair | 0:f269e3021894 | 391 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
elessair | 0:f269e3021894 | 392 | |
elessair | 0:f269e3021894 | 393 | val = &cur->val; |
elessair | 0:f269e3021894 | 394 | val->tag = *(*p)++; |
elessair | 0:f269e3021894 | 395 | |
elessair | 0:f269e3021894 | 396 | if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 ) |
elessair | 0:f269e3021894 | 397 | return( MBEDTLS_ERR_X509_INVALID_NAME + ret ); |
elessair | 0:f269e3021894 | 398 | |
elessair | 0:f269e3021894 | 399 | val->p = *p; |
elessair | 0:f269e3021894 | 400 | *p += val->len; |
elessair | 0:f269e3021894 | 401 | |
elessair | 0:f269e3021894 | 402 | cur->next = NULL; |
elessair | 0:f269e3021894 | 403 | |
elessair | 0:f269e3021894 | 404 | return( 0 ); |
elessair | 0:f269e3021894 | 405 | } |
elessair | 0:f269e3021894 | 406 | |
elessair | 0:f269e3021894 | 407 | /* |
elessair | 0:f269e3021894 | 408 | * Name ::= CHOICE { -- only one possibility for now -- |
elessair | 0:f269e3021894 | 409 | * rdnSequence RDNSequence } |
elessair | 0:f269e3021894 | 410 | * |
elessair | 0:f269e3021894 | 411 | * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
elessair | 0:f269e3021894 | 412 | * |
elessair | 0:f269e3021894 | 413 | * RelativeDistinguishedName ::= |
elessair | 0:f269e3021894 | 414 | * SET OF AttributeTypeAndValue |
elessair | 0:f269e3021894 | 415 | * |
elessair | 0:f269e3021894 | 416 | * AttributeTypeAndValue ::= SEQUENCE { |
elessair | 0:f269e3021894 | 417 | * type AttributeType, |
elessair | 0:f269e3021894 | 418 | * value AttributeValue } |
elessair | 0:f269e3021894 | 419 | * |
elessair | 0:f269e3021894 | 420 | * AttributeType ::= OBJECT IDENTIFIER |
elessair | 0:f269e3021894 | 421 | * |
elessair | 0:f269e3021894 | 422 | * AttributeValue ::= ANY DEFINED BY AttributeType |
elessair | 0:f269e3021894 | 423 | * |
elessair | 0:f269e3021894 | 424 | * The data structure is optimized for the common case where each RDN has only |
elessair | 0:f269e3021894 | 425 | * one element, which is represented as a list of AttributeTypeAndValue. |
elessair | 0:f269e3021894 | 426 | * For the general case we still use a flat list, but we mark elements of the |
elessair | 0:f269e3021894 | 427 | * same set so that they are "merged" together in the functions that consume |
elessair | 0:f269e3021894 | 428 | * this list, eg mbedtls_x509_dn_gets(). |
elessair | 0:f269e3021894 | 429 | */ |
elessair | 0:f269e3021894 | 430 | int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end, |
elessair | 0:f269e3021894 | 431 | mbedtls_x509_name *cur ) |
elessair | 0:f269e3021894 | 432 | { |
elessair | 0:f269e3021894 | 433 | int ret; |
elessair | 0:f269e3021894 | 434 | size_t set_len; |
elessair | 0:f269e3021894 | 435 | const unsigned char *end_set; |
elessair | 0:f269e3021894 | 436 | |
elessair | 0:f269e3021894 | 437 | /* don't use recursion, we'd risk stack overflow if not optimized */ |
elessair | 0:f269e3021894 | 438 | while( 1 ) |
elessair | 0:f269e3021894 | 439 | { |
elessair | 0:f269e3021894 | 440 | /* |
elessair | 0:f269e3021894 | 441 | * parse SET |
elessair | 0:f269e3021894 | 442 | */ |
elessair | 0:f269e3021894 | 443 | if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len, |
elessair | 0:f269e3021894 | 444 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 ) |
elessair | 0:f269e3021894 | 445 | return( MBEDTLS_ERR_X509_INVALID_NAME + ret ); |
elessair | 0:f269e3021894 | 446 | |
elessair | 0:f269e3021894 | 447 | end_set = *p + set_len; |
elessair | 0:f269e3021894 | 448 | |
elessair | 0:f269e3021894 | 449 | while( 1 ) |
elessair | 0:f269e3021894 | 450 | { |
elessair | 0:f269e3021894 | 451 | if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 ) |
elessair | 0:f269e3021894 | 452 | return( ret ); |
elessair | 0:f269e3021894 | 453 | |
elessair | 0:f269e3021894 | 454 | if( *p == end_set ) |
elessair | 0:f269e3021894 | 455 | break; |
elessair | 0:f269e3021894 | 456 | |
elessair | 0:f269e3021894 | 457 | /* Mark this item as being no the only one in a set */ |
elessair | 0:f269e3021894 | 458 | cur->next_merged = 1; |
elessair | 0:f269e3021894 | 459 | |
elessair | 0:f269e3021894 | 460 | cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) ); |
elessair | 0:f269e3021894 | 461 | |
elessair | 0:f269e3021894 | 462 | if( cur->next == NULL ) |
elessair | 0:f269e3021894 | 463 | return( MBEDTLS_ERR_X509_ALLOC_FAILED ); |
elessair | 0:f269e3021894 | 464 | |
elessair | 0:f269e3021894 | 465 | cur = cur->next; |
elessair | 0:f269e3021894 | 466 | } |
elessair | 0:f269e3021894 | 467 | |
elessair | 0:f269e3021894 | 468 | /* |
elessair | 0:f269e3021894 | 469 | * continue until end of SEQUENCE is reached |
elessair | 0:f269e3021894 | 470 | */ |
elessair | 0:f269e3021894 | 471 | if( *p == end ) |
elessair | 0:f269e3021894 | 472 | return( 0 ); |
elessair | 0:f269e3021894 | 473 | |
elessair | 0:f269e3021894 | 474 | cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) ); |
elessair | 0:f269e3021894 | 475 | |
elessair | 0:f269e3021894 | 476 | if( cur->next == NULL ) |
elessair | 0:f269e3021894 | 477 | return( MBEDTLS_ERR_X509_ALLOC_FAILED ); |
elessair | 0:f269e3021894 | 478 | |
elessair | 0:f269e3021894 | 479 | cur = cur->next; |
elessair | 0:f269e3021894 | 480 | } |
elessair | 0:f269e3021894 | 481 | } |
elessair | 0:f269e3021894 | 482 | |
elessair | 0:f269e3021894 | 483 | static int x509_parse_int(unsigned char **p, unsigned n, int *res){ |
elessair | 0:f269e3021894 | 484 | *res = 0; |
elessair | 0:f269e3021894 | 485 | for( ; n > 0; --n ){ |
elessair | 0:f269e3021894 | 486 | if( ( **p < '0') || ( **p > '9' ) ) return MBEDTLS_ERR_X509_INVALID_DATE; |
elessair | 0:f269e3021894 | 487 | *res *= 10; |
elessair | 0:f269e3021894 | 488 | *res += (*(*p)++ - '0'); |
elessair | 0:f269e3021894 | 489 | } |
elessair | 0:f269e3021894 | 490 | return 0; |
elessair | 0:f269e3021894 | 491 | } |
elessair | 0:f269e3021894 | 492 | |
elessair | 0:f269e3021894 | 493 | static int x509_date_is_valid(const mbedtls_x509_time *time) |
elessair | 0:f269e3021894 | 494 | { |
elessair | 0:f269e3021894 | 495 | int ret = MBEDTLS_ERR_X509_INVALID_DATE; |
elessair | 0:f269e3021894 | 496 | |
elessair | 0:f269e3021894 | 497 | CHECK_RANGE( 0, 9999, time->year ); |
elessair | 0:f269e3021894 | 498 | CHECK_RANGE( 0, 23, time->hour ); |
elessair | 0:f269e3021894 | 499 | CHECK_RANGE( 0, 59, time->min ); |
elessair | 0:f269e3021894 | 500 | CHECK_RANGE( 0, 59, time->sec ); |
elessair | 0:f269e3021894 | 501 | |
elessair | 0:f269e3021894 | 502 | switch( time->mon ) |
elessair | 0:f269e3021894 | 503 | { |
elessair | 0:f269e3021894 | 504 | case 1: case 3: case 5: case 7: case 8: case 10: case 12: |
elessair | 0:f269e3021894 | 505 | CHECK_RANGE( 1, 31, time->day ); |
elessair | 0:f269e3021894 | 506 | break; |
elessair | 0:f269e3021894 | 507 | case 4: case 6: case 9: case 11: |
elessair | 0:f269e3021894 | 508 | CHECK_RANGE( 1, 30, time->day ); |
elessair | 0:f269e3021894 | 509 | break; |
elessair | 0:f269e3021894 | 510 | case 2: |
elessair | 0:f269e3021894 | 511 | CHECK_RANGE( 1, 28 + (time->year % 4 == 0), time->day ); |
elessair | 0:f269e3021894 | 512 | break; |
elessair | 0:f269e3021894 | 513 | default: |
elessair | 0:f269e3021894 | 514 | return( ret ); |
elessair | 0:f269e3021894 | 515 | } |
elessair | 0:f269e3021894 | 516 | |
elessair | 0:f269e3021894 | 517 | return( 0 ); |
elessair | 0:f269e3021894 | 518 | } |
elessair | 0:f269e3021894 | 519 | |
elessair | 0:f269e3021894 | 520 | /* |
elessair | 0:f269e3021894 | 521 | * Time ::= CHOICE { |
elessair | 0:f269e3021894 | 522 | * utcTime UTCTime, |
elessair | 0:f269e3021894 | 523 | * generalTime GeneralizedTime } |
elessair | 0:f269e3021894 | 524 | */ |
elessair | 0:f269e3021894 | 525 | int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end, |
elessair | 0:f269e3021894 | 526 | mbedtls_x509_time *time ) |
elessair | 0:f269e3021894 | 527 | { |
elessair | 0:f269e3021894 | 528 | int ret; |
elessair | 0:f269e3021894 | 529 | size_t len; |
elessair | 0:f269e3021894 | 530 | unsigned char tag; |
elessair | 0:f269e3021894 | 531 | |
elessair | 0:f269e3021894 | 532 | if( ( end - *p ) < 1 ) |
elessair | 0:f269e3021894 | 533 | return( MBEDTLS_ERR_X509_INVALID_DATE + |
elessair | 0:f269e3021894 | 534 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
elessair | 0:f269e3021894 | 535 | |
elessair | 0:f269e3021894 | 536 | tag = **p; |
elessair | 0:f269e3021894 | 537 | |
elessair | 0:f269e3021894 | 538 | if( tag == MBEDTLS_ASN1_UTC_TIME ) |
elessair | 0:f269e3021894 | 539 | { |
elessair | 0:f269e3021894 | 540 | (*p)++; |
elessair | 0:f269e3021894 | 541 | ret = mbedtls_asn1_get_len( p, end, &len ); |
elessair | 0:f269e3021894 | 542 | |
elessair | 0:f269e3021894 | 543 | if( ret != 0 ) |
elessair | 0:f269e3021894 | 544 | return( MBEDTLS_ERR_X509_INVALID_DATE + ret ); |
elessair | 0:f269e3021894 | 545 | |
elessair | 0:f269e3021894 | 546 | CHECK( x509_parse_int( p, 2, &time->year ) ); |
elessair | 0:f269e3021894 | 547 | CHECK( x509_parse_int( p, 2, &time->mon ) ); |
elessair | 0:f269e3021894 | 548 | CHECK( x509_parse_int( p, 2, &time->day ) ); |
elessair | 0:f269e3021894 | 549 | CHECK( x509_parse_int( p, 2, &time->hour ) ); |
elessair | 0:f269e3021894 | 550 | CHECK( x509_parse_int( p, 2, &time->min ) ); |
elessair | 0:f269e3021894 | 551 | if( len > 10 ) |
elessair | 0:f269e3021894 | 552 | CHECK( x509_parse_int( p, 2, &time->sec ) ); |
elessair | 0:f269e3021894 | 553 | if( len > 12 && *(*p)++ != 'Z' ) |
elessair | 0:f269e3021894 | 554 | return( MBEDTLS_ERR_X509_INVALID_DATE ); |
elessair | 0:f269e3021894 | 555 | |
elessair | 0:f269e3021894 | 556 | time->year += 100 * ( time->year < 50 ); |
elessair | 0:f269e3021894 | 557 | time->year += 1900; |
elessair | 0:f269e3021894 | 558 | |
elessair | 0:f269e3021894 | 559 | CHECK( x509_date_is_valid( time ) ); |
elessair | 0:f269e3021894 | 560 | |
elessair | 0:f269e3021894 | 561 | return( 0 ); |
elessair | 0:f269e3021894 | 562 | } |
elessair | 0:f269e3021894 | 563 | else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME ) |
elessair | 0:f269e3021894 | 564 | { |
elessair | 0:f269e3021894 | 565 | (*p)++; |
elessair | 0:f269e3021894 | 566 | ret = mbedtls_asn1_get_len( p, end, &len ); |
elessair | 0:f269e3021894 | 567 | |
elessair | 0:f269e3021894 | 568 | if( ret != 0 ) |
elessair | 0:f269e3021894 | 569 | return( MBEDTLS_ERR_X509_INVALID_DATE + ret ); |
elessair | 0:f269e3021894 | 570 | |
elessair | 0:f269e3021894 | 571 | CHECK( x509_parse_int( p, 4, &time->year ) ); |
elessair | 0:f269e3021894 | 572 | CHECK( x509_parse_int( p, 2, &time->mon ) ); |
elessair | 0:f269e3021894 | 573 | CHECK( x509_parse_int( p, 2, &time->day ) ); |
elessair | 0:f269e3021894 | 574 | CHECK( x509_parse_int( p, 2, &time->hour ) ); |
elessair | 0:f269e3021894 | 575 | CHECK( x509_parse_int( p, 2, &time->min ) ); |
elessair | 0:f269e3021894 | 576 | if( len > 12 ) |
elessair | 0:f269e3021894 | 577 | CHECK( x509_parse_int( p, 2, &time->sec ) ); |
elessair | 0:f269e3021894 | 578 | if( len > 14 && *(*p)++ != 'Z' ) |
elessair | 0:f269e3021894 | 579 | return( MBEDTLS_ERR_X509_INVALID_DATE ); |
elessair | 0:f269e3021894 | 580 | |
elessair | 0:f269e3021894 | 581 | CHECK( x509_date_is_valid( time ) ); |
elessair | 0:f269e3021894 | 582 | |
elessair | 0:f269e3021894 | 583 | return( 0 ); |
elessair | 0:f269e3021894 | 584 | } |
elessair | 0:f269e3021894 | 585 | else |
elessair | 0:f269e3021894 | 586 | return( MBEDTLS_ERR_X509_INVALID_DATE + |
elessair | 0:f269e3021894 | 587 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
elessair | 0:f269e3021894 | 588 | } |
elessair | 0:f269e3021894 | 589 | |
elessair | 0:f269e3021894 | 590 | int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig ) |
elessair | 0:f269e3021894 | 591 | { |
elessair | 0:f269e3021894 | 592 | int ret; |
elessair | 0:f269e3021894 | 593 | size_t len; |
elessair | 0:f269e3021894 | 594 | int tag_type; |
elessair | 0:f269e3021894 | 595 | |
elessair | 0:f269e3021894 | 596 | if( ( end - *p ) < 1 ) |
elessair | 0:f269e3021894 | 597 | return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + |
elessair | 0:f269e3021894 | 598 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
elessair | 0:f269e3021894 | 599 | |
elessair | 0:f269e3021894 | 600 | tag_type = **p; |
elessair | 0:f269e3021894 | 601 | |
elessair | 0:f269e3021894 | 602 | if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 ) |
elessair | 0:f269e3021894 | 603 | return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret ); |
elessair | 0:f269e3021894 | 604 | |
elessair | 0:f269e3021894 | 605 | sig->tag = tag_type; |
elessair | 0:f269e3021894 | 606 | sig->len = len; |
elessair | 0:f269e3021894 | 607 | sig->p = *p; |
elessair | 0:f269e3021894 | 608 | |
elessair | 0:f269e3021894 | 609 | *p += len; |
elessair | 0:f269e3021894 | 610 | |
elessair | 0:f269e3021894 | 611 | return( 0 ); |
elessair | 0:f269e3021894 | 612 | } |
elessair | 0:f269e3021894 | 613 | |
elessair | 0:f269e3021894 | 614 | /* |
elessair | 0:f269e3021894 | 615 | * Get signature algorithm from alg OID and optional parameters |
elessair | 0:f269e3021894 | 616 | */ |
elessair | 0:f269e3021894 | 617 | int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params, |
elessair | 0:f269e3021894 | 618 | mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg, |
elessair | 0:f269e3021894 | 619 | void **sig_opts ) |
elessair | 0:f269e3021894 | 620 | { |
elessair | 0:f269e3021894 | 621 | int ret; |
elessair | 0:f269e3021894 | 622 | |
elessair | 0:f269e3021894 | 623 | if( *sig_opts != NULL ) |
elessair | 0:f269e3021894 | 624 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
elessair | 0:f269e3021894 | 625 | |
elessair | 0:f269e3021894 | 626 | if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 ) |
elessair | 0:f269e3021894 | 627 | return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + ret ); |
elessair | 0:f269e3021894 | 628 | |
elessair | 0:f269e3021894 | 629 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
elessair | 0:f269e3021894 | 630 | if( *pk_alg == MBEDTLS_PK_RSASSA_PSS ) |
elessair | 0:f269e3021894 | 631 | { |
elessair | 0:f269e3021894 | 632 | mbedtls_pk_rsassa_pss_options *pss_opts; |
elessair | 0:f269e3021894 | 633 | |
elessair | 0:f269e3021894 | 634 | pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) ); |
elessair | 0:f269e3021894 | 635 | if( pss_opts == NULL ) |
elessair | 0:f269e3021894 | 636 | return( MBEDTLS_ERR_X509_ALLOC_FAILED ); |
elessair | 0:f269e3021894 | 637 | |
elessair | 0:f269e3021894 | 638 | ret = mbedtls_x509_get_rsassa_pss_params( sig_params, |
elessair | 0:f269e3021894 | 639 | md_alg, |
elessair | 0:f269e3021894 | 640 | &pss_opts->mgf1_hash_id, |
elessair | 0:f269e3021894 | 641 | &pss_opts->expected_salt_len ); |
elessair | 0:f269e3021894 | 642 | if( ret != 0 ) |
elessair | 0:f269e3021894 | 643 | { |
elessair | 0:f269e3021894 | 644 | mbedtls_free( pss_opts ); |
elessair | 0:f269e3021894 | 645 | return( ret ); |
elessair | 0:f269e3021894 | 646 | } |
elessair | 0:f269e3021894 | 647 | |
elessair | 0:f269e3021894 | 648 | *sig_opts = (void *) pss_opts; |
elessair | 0:f269e3021894 | 649 | } |
elessair | 0:f269e3021894 | 650 | else |
elessair | 0:f269e3021894 | 651 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
elessair | 0:f269e3021894 | 652 | { |
elessair | 0:f269e3021894 | 653 | /* Make sure parameters are absent or NULL */ |
elessair | 0:f269e3021894 | 654 | if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) || |
elessair | 0:f269e3021894 | 655 | sig_params->len != 0 ) |
elessair | 0:f269e3021894 | 656 | return( MBEDTLS_ERR_X509_INVALID_ALG ); |
elessair | 0:f269e3021894 | 657 | } |
elessair | 0:f269e3021894 | 658 | |
elessair | 0:f269e3021894 | 659 | return( 0 ); |
elessair | 0:f269e3021894 | 660 | } |
elessair | 0:f269e3021894 | 661 | |
elessair | 0:f269e3021894 | 662 | /* |
elessair | 0:f269e3021894 | 663 | * X.509 Extensions (No parsing of extensions, pointer should |
elessair | 0:f269e3021894 | 664 | * be either manually updated or extensions should be parsed! |
elessair | 0:f269e3021894 | 665 | */ |
elessair | 0:f269e3021894 | 666 | int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end, |
elessair | 0:f269e3021894 | 667 | mbedtls_x509_buf *ext, int tag ) |
elessair | 0:f269e3021894 | 668 | { |
elessair | 0:f269e3021894 | 669 | int ret; |
elessair | 0:f269e3021894 | 670 | size_t len; |
elessair | 0:f269e3021894 | 671 | |
elessair | 0:f269e3021894 | 672 | if( *p == end ) |
elessair | 0:f269e3021894 | 673 | return( 0 ); |
elessair | 0:f269e3021894 | 674 | |
elessair | 0:f269e3021894 | 675 | ext->tag = **p; |
elessair | 0:f269e3021894 | 676 | |
elessair | 0:f269e3021894 | 677 | if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len, |
elessair | 0:f269e3021894 | 678 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag ) ) != 0 ) |
elessair | 0:f269e3021894 | 679 | return( ret ); |
elessair | 0:f269e3021894 | 680 | |
elessair | 0:f269e3021894 | 681 | ext->p = *p; |
elessair | 0:f269e3021894 | 682 | end = *p + ext->len; |
elessair | 0:f269e3021894 | 683 | |
elessair | 0:f269e3021894 | 684 | /* |
elessair | 0:f269e3021894 | 685 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
elessair | 0:f269e3021894 | 686 | * |
elessair | 0:f269e3021894 | 687 | * Extension ::= SEQUENCE { |
elessair | 0:f269e3021894 | 688 | * extnID OBJECT IDENTIFIER, |
elessair | 0:f269e3021894 | 689 | * critical BOOLEAN DEFAULT FALSE, |
elessair | 0:f269e3021894 | 690 | * extnValue OCTET STRING } |
elessair | 0:f269e3021894 | 691 | */ |
elessair | 0:f269e3021894 | 692 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
elessair | 0:f269e3021894 | 693 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
elessair | 0:f269e3021894 | 694 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret ); |
elessair | 0:f269e3021894 | 695 | |
elessair | 0:f269e3021894 | 696 | if( end != *p + len ) |
elessair | 0:f269e3021894 | 697 | return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
elessair | 0:f269e3021894 | 698 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
elessair | 0:f269e3021894 | 699 | |
elessair | 0:f269e3021894 | 700 | return( 0 ); |
elessair | 0:f269e3021894 | 701 | } |
elessair | 0:f269e3021894 | 702 | |
elessair | 0:f269e3021894 | 703 | /* |
elessair | 0:f269e3021894 | 704 | * Store the name in printable form into buf; no more |
elessair | 0:f269e3021894 | 705 | * than size characters will be written |
elessair | 0:f269e3021894 | 706 | */ |
elessair | 0:f269e3021894 | 707 | int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn ) |
elessair | 0:f269e3021894 | 708 | { |
elessair | 0:f269e3021894 | 709 | int ret; |
elessair | 0:f269e3021894 | 710 | size_t i, n; |
elessair | 0:f269e3021894 | 711 | unsigned char c, merge = 0; |
elessair | 0:f269e3021894 | 712 | const mbedtls_x509_name *name; |
elessair | 0:f269e3021894 | 713 | const char *short_name = NULL; |
elessair | 0:f269e3021894 | 714 | char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p; |
elessair | 0:f269e3021894 | 715 | |
elessair | 0:f269e3021894 | 716 | memset( s, 0, sizeof( s ) ); |
elessair | 0:f269e3021894 | 717 | |
elessair | 0:f269e3021894 | 718 | name = dn; |
elessair | 0:f269e3021894 | 719 | p = buf; |
elessair | 0:f269e3021894 | 720 | n = size; |
elessair | 0:f269e3021894 | 721 | |
elessair | 0:f269e3021894 | 722 | while( name != NULL ) |
elessair | 0:f269e3021894 | 723 | { |
elessair | 0:f269e3021894 | 724 | if( !name->oid.p ) |
elessair | 0:f269e3021894 | 725 | { |
elessair | 0:f269e3021894 | 726 | name = name->next; |
elessair | 0:f269e3021894 | 727 | continue; |
elessair | 0:f269e3021894 | 728 | } |
elessair | 0:f269e3021894 | 729 | |
elessair | 0:f269e3021894 | 730 | if( name != dn ) |
elessair | 0:f269e3021894 | 731 | { |
elessair | 0:f269e3021894 | 732 | ret = mbedtls_snprintf( p, n, merge ? " + " : ", " ); |
elessair | 0:f269e3021894 | 733 | MBEDTLS_X509_SAFE_SNPRINTF; |
elessair | 0:f269e3021894 | 734 | } |
elessair | 0:f269e3021894 | 735 | |
elessair | 0:f269e3021894 | 736 | ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name ); |
elessair | 0:f269e3021894 | 737 | |
elessair | 0:f269e3021894 | 738 | if( ret == 0 ) |
elessair | 0:f269e3021894 | 739 | ret = mbedtls_snprintf( p, n, "%s=", short_name ); |
elessair | 0:f269e3021894 | 740 | else |
elessair | 0:f269e3021894 | 741 | ret = mbedtls_snprintf( p, n, "\?\?=" ); |
elessair | 0:f269e3021894 | 742 | MBEDTLS_X509_SAFE_SNPRINTF; |
elessair | 0:f269e3021894 | 743 | |
elessair | 0:f269e3021894 | 744 | for( i = 0; i < name->val.len; i++ ) |
elessair | 0:f269e3021894 | 745 | { |
elessair | 0:f269e3021894 | 746 | if( i >= sizeof( s ) - 1 ) |
elessair | 0:f269e3021894 | 747 | break; |
elessair | 0:f269e3021894 | 748 | |
elessair | 0:f269e3021894 | 749 | c = name->val.p[i]; |
elessair | 0:f269e3021894 | 750 | if( c < 32 || c == 127 || ( c > 128 && c < 160 ) ) |
elessair | 0:f269e3021894 | 751 | s[i] = '?'; |
elessair | 0:f269e3021894 | 752 | else s[i] = c; |
elessair | 0:f269e3021894 | 753 | } |
elessair | 0:f269e3021894 | 754 | s[i] = '\0'; |
elessair | 0:f269e3021894 | 755 | ret = mbedtls_snprintf( p, n, "%s", s ); |
elessair | 0:f269e3021894 | 756 | MBEDTLS_X509_SAFE_SNPRINTF; |
elessair | 0:f269e3021894 | 757 | |
elessair | 0:f269e3021894 | 758 | merge = name->next_merged; |
elessair | 0:f269e3021894 | 759 | name = name->next; |
elessair | 0:f269e3021894 | 760 | } |
elessair | 0:f269e3021894 | 761 | |
elessair | 0:f269e3021894 | 762 | return( (int) ( size - n ) ); |
elessair | 0:f269e3021894 | 763 | } |
elessair | 0:f269e3021894 | 764 | |
elessair | 0:f269e3021894 | 765 | /* |
elessair | 0:f269e3021894 | 766 | * Store the serial in printable form into buf; no more |
elessair | 0:f269e3021894 | 767 | * than size characters will be written |
elessair | 0:f269e3021894 | 768 | */ |
elessair | 0:f269e3021894 | 769 | int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial ) |
elessair | 0:f269e3021894 | 770 | { |
elessair | 0:f269e3021894 | 771 | int ret; |
elessair | 0:f269e3021894 | 772 | size_t i, n, nr; |
elessair | 0:f269e3021894 | 773 | char *p; |
elessair | 0:f269e3021894 | 774 | |
elessair | 0:f269e3021894 | 775 | p = buf; |
elessair | 0:f269e3021894 | 776 | n = size; |
elessair | 0:f269e3021894 | 777 | |
elessair | 0:f269e3021894 | 778 | nr = ( serial->len <= 32 ) |
elessair | 0:f269e3021894 | 779 | ? serial->len : 28; |
elessair | 0:f269e3021894 | 780 | |
elessair | 0:f269e3021894 | 781 | for( i = 0; i < nr; i++ ) |
elessair | 0:f269e3021894 | 782 | { |
elessair | 0:f269e3021894 | 783 | if( i == 0 && nr > 1 && serial->p[i] == 0x0 ) |
elessair | 0:f269e3021894 | 784 | continue; |
elessair | 0:f269e3021894 | 785 | |
elessair | 0:f269e3021894 | 786 | ret = mbedtls_snprintf( p, n, "%02X%s", |
elessair | 0:f269e3021894 | 787 | serial->p[i], ( i < nr - 1 ) ? ":" : "" ); |
elessair | 0:f269e3021894 | 788 | MBEDTLS_X509_SAFE_SNPRINTF; |
elessair | 0:f269e3021894 | 789 | } |
elessair | 0:f269e3021894 | 790 | |
elessair | 0:f269e3021894 | 791 | if( nr != serial->len ) |
elessair | 0:f269e3021894 | 792 | { |
elessair | 0:f269e3021894 | 793 | ret = mbedtls_snprintf( p, n, "...." ); |
elessair | 0:f269e3021894 | 794 | MBEDTLS_X509_SAFE_SNPRINTF; |
elessair | 0:f269e3021894 | 795 | } |
elessair | 0:f269e3021894 | 796 | |
elessair | 0:f269e3021894 | 797 | return( (int) ( size - n ) ); |
elessair | 0:f269e3021894 | 798 | } |
elessair | 0:f269e3021894 | 799 | |
elessair | 0:f269e3021894 | 800 | /* |
elessair | 0:f269e3021894 | 801 | * Helper for writing signature algorithms |
elessair | 0:f269e3021894 | 802 | */ |
elessair | 0:f269e3021894 | 803 | int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid, |
elessair | 0:f269e3021894 | 804 | mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg, |
elessair | 0:f269e3021894 | 805 | const void *sig_opts ) |
elessair | 0:f269e3021894 | 806 | { |
elessair | 0:f269e3021894 | 807 | int ret; |
elessair | 0:f269e3021894 | 808 | char *p = buf; |
elessair | 0:f269e3021894 | 809 | size_t n = size; |
elessair | 0:f269e3021894 | 810 | const char *desc = NULL; |
elessair | 0:f269e3021894 | 811 | |
elessair | 0:f269e3021894 | 812 | ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc ); |
elessair | 0:f269e3021894 | 813 | if( ret != 0 ) |
elessair | 0:f269e3021894 | 814 | ret = mbedtls_snprintf( p, n, "???" ); |
elessair | 0:f269e3021894 | 815 | else |
elessair | 0:f269e3021894 | 816 | ret = mbedtls_snprintf( p, n, "%s", desc ); |
elessair | 0:f269e3021894 | 817 | MBEDTLS_X509_SAFE_SNPRINTF; |
elessair | 0:f269e3021894 | 818 | |
elessair | 0:f269e3021894 | 819 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
elessair | 0:f269e3021894 | 820 | if( pk_alg == MBEDTLS_PK_RSASSA_PSS ) |
elessair | 0:f269e3021894 | 821 | { |
elessair | 0:f269e3021894 | 822 | const mbedtls_pk_rsassa_pss_options *pss_opts; |
elessair | 0:f269e3021894 | 823 | const mbedtls_md_info_t *md_info, *mgf_md_info; |
elessair | 0:f269e3021894 | 824 | |
elessair | 0:f269e3021894 | 825 | pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts; |
elessair | 0:f269e3021894 | 826 | |
elessair | 0:f269e3021894 | 827 | md_info = mbedtls_md_info_from_type( md_alg ); |
elessair | 0:f269e3021894 | 828 | mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id ); |
elessair | 0:f269e3021894 | 829 | |
elessair | 0:f269e3021894 | 830 | ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)", |
elessair | 0:f269e3021894 | 831 | md_info ? mbedtls_md_get_name( md_info ) : "???", |
elessair | 0:f269e3021894 | 832 | mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???", |
elessair | 0:f269e3021894 | 833 | pss_opts->expected_salt_len ); |
elessair | 0:f269e3021894 | 834 | MBEDTLS_X509_SAFE_SNPRINTF; |
elessair | 0:f269e3021894 | 835 | } |
elessair | 0:f269e3021894 | 836 | #else |
elessair | 0:f269e3021894 | 837 | ((void) pk_alg); |
elessair | 0:f269e3021894 | 838 | ((void) md_alg); |
elessair | 0:f269e3021894 | 839 | ((void) sig_opts); |
elessair | 0:f269e3021894 | 840 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
elessair | 0:f269e3021894 | 841 | |
elessair | 0:f269e3021894 | 842 | return( (int)( size - n ) ); |
elessair | 0:f269e3021894 | 843 | } |
elessair | 0:f269e3021894 | 844 | |
elessair | 0:f269e3021894 | 845 | /* |
elessair | 0:f269e3021894 | 846 | * Helper for writing "RSA key size", "EC key size", etc |
elessair | 0:f269e3021894 | 847 | */ |
elessair | 0:f269e3021894 | 848 | int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name ) |
elessair | 0:f269e3021894 | 849 | { |
elessair | 0:f269e3021894 | 850 | char *p = buf; |
elessair | 0:f269e3021894 | 851 | size_t n = buf_size; |
elessair | 0:f269e3021894 | 852 | int ret; |
elessair | 0:f269e3021894 | 853 | |
elessair | 0:f269e3021894 | 854 | ret = mbedtls_snprintf( p, n, "%s key size", name ); |
elessair | 0:f269e3021894 | 855 | MBEDTLS_X509_SAFE_SNPRINTF; |
elessair | 0:f269e3021894 | 856 | |
elessair | 0:f269e3021894 | 857 | return( 0 ); |
elessair | 0:f269e3021894 | 858 | } |
elessair | 0:f269e3021894 | 859 | |
elessair | 0:f269e3021894 | 860 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
elessair | 0:f269e3021894 | 861 | /* |
elessair | 0:f269e3021894 | 862 | * Set the time structure to the current time. |
elessair | 0:f269e3021894 | 863 | * Return 0 on success, non-zero on failure. |
elessair | 0:f269e3021894 | 864 | */ |
elessair | 0:f269e3021894 | 865 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
elessair | 0:f269e3021894 | 866 | static int x509_get_current_time( mbedtls_x509_time *now ) |
elessair | 0:f269e3021894 | 867 | { |
elessair | 0:f269e3021894 | 868 | SYSTEMTIME st; |
elessair | 0:f269e3021894 | 869 | |
elessair | 0:f269e3021894 | 870 | GetSystemTime( &st ); |
elessair | 0:f269e3021894 | 871 | |
elessair | 0:f269e3021894 | 872 | now->year = st.wYear; |
elessair | 0:f269e3021894 | 873 | now->mon = st.wMonth; |
elessair | 0:f269e3021894 | 874 | now->day = st.wDay; |
elessair | 0:f269e3021894 | 875 | now->hour = st.wHour; |
elessair | 0:f269e3021894 | 876 | now->min = st.wMinute; |
elessair | 0:f269e3021894 | 877 | now->sec = st.wSecond; |
elessair | 0:f269e3021894 | 878 | |
elessair | 0:f269e3021894 | 879 | return( 0 ); |
elessair | 0:f269e3021894 | 880 | } |
elessair | 0:f269e3021894 | 881 | #else |
elessair | 0:f269e3021894 | 882 | static int x509_get_current_time( mbedtls_x509_time *now ) |
elessair | 0:f269e3021894 | 883 | { |
elessair | 0:f269e3021894 | 884 | struct tm *lt; |
elessair | 0:f269e3021894 | 885 | mbedtls_time_t tt; |
elessair | 0:f269e3021894 | 886 | int ret = 0; |
elessair | 0:f269e3021894 | 887 | |
elessair | 0:f269e3021894 | 888 | #if defined(MBEDTLS_THREADING_C) |
elessair | 0:f269e3021894 | 889 | if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 ) |
elessair | 0:f269e3021894 | 890 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
elessair | 0:f269e3021894 | 891 | #endif |
elessair | 0:f269e3021894 | 892 | |
elessair | 0:f269e3021894 | 893 | tt = mbedtls_time( NULL ); |
elessair | 0:f269e3021894 | 894 | lt = gmtime( &tt ); |
elessair | 0:f269e3021894 | 895 | |
elessair | 0:f269e3021894 | 896 | if( lt == NULL ) |
elessair | 0:f269e3021894 | 897 | ret = -1; |
elessair | 0:f269e3021894 | 898 | else |
elessair | 0:f269e3021894 | 899 | { |
elessair | 0:f269e3021894 | 900 | now->year = lt->tm_year + 1900; |
elessair | 0:f269e3021894 | 901 | now->mon = lt->tm_mon + 1; |
elessair | 0:f269e3021894 | 902 | now->day = lt->tm_mday; |
elessair | 0:f269e3021894 | 903 | now->hour = lt->tm_hour; |
elessair | 0:f269e3021894 | 904 | now->min = lt->tm_min; |
elessair | 0:f269e3021894 | 905 | now->sec = lt->tm_sec; |
elessair | 0:f269e3021894 | 906 | } |
elessair | 0:f269e3021894 | 907 | |
elessair | 0:f269e3021894 | 908 | #if defined(MBEDTLS_THREADING_C) |
elessair | 0:f269e3021894 | 909 | if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 ) |
elessair | 0:f269e3021894 | 910 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
elessair | 0:f269e3021894 | 911 | #endif |
elessair | 0:f269e3021894 | 912 | |
elessair | 0:f269e3021894 | 913 | return( ret ); |
elessair | 0:f269e3021894 | 914 | } |
elessair | 0:f269e3021894 | 915 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
elessair | 0:f269e3021894 | 916 | |
elessair | 0:f269e3021894 | 917 | /* |
elessair | 0:f269e3021894 | 918 | * Return 0 if before <= after, 1 otherwise |
elessair | 0:f269e3021894 | 919 | */ |
elessair | 0:f269e3021894 | 920 | static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after ) |
elessair | 0:f269e3021894 | 921 | { |
elessair | 0:f269e3021894 | 922 | if( before->year > after->year ) |
elessair | 0:f269e3021894 | 923 | return( 1 ); |
elessair | 0:f269e3021894 | 924 | |
elessair | 0:f269e3021894 | 925 | if( before->year == after->year && |
elessair | 0:f269e3021894 | 926 | before->mon > after->mon ) |
elessair | 0:f269e3021894 | 927 | return( 1 ); |
elessair | 0:f269e3021894 | 928 | |
elessair | 0:f269e3021894 | 929 | if( before->year == after->year && |
elessair | 0:f269e3021894 | 930 | before->mon == after->mon && |
elessair | 0:f269e3021894 | 931 | before->day > after->day ) |
elessair | 0:f269e3021894 | 932 | return( 1 ); |
elessair | 0:f269e3021894 | 933 | |
elessair | 0:f269e3021894 | 934 | if( before->year == after->year && |
elessair | 0:f269e3021894 | 935 | before->mon == after->mon && |
elessair | 0:f269e3021894 | 936 | before->day == after->day && |
elessair | 0:f269e3021894 | 937 | before->hour > after->hour ) |
elessair | 0:f269e3021894 | 938 | return( 1 ); |
elessair | 0:f269e3021894 | 939 | |
elessair | 0:f269e3021894 | 940 | if( before->year == after->year && |
elessair | 0:f269e3021894 | 941 | before->mon == after->mon && |
elessair | 0:f269e3021894 | 942 | before->day == after->day && |
elessair | 0:f269e3021894 | 943 | before->hour == after->hour && |
elessair | 0:f269e3021894 | 944 | before->min > after->min ) |
elessair | 0:f269e3021894 | 945 | return( 1 ); |
elessair | 0:f269e3021894 | 946 | |
elessair | 0:f269e3021894 | 947 | if( before->year == after->year && |
elessair | 0:f269e3021894 | 948 | before->mon == after->mon && |
elessair | 0:f269e3021894 | 949 | before->day == after->day && |
elessair | 0:f269e3021894 | 950 | before->hour == after->hour && |
elessair | 0:f269e3021894 | 951 | before->min == after->min && |
elessair | 0:f269e3021894 | 952 | before->sec > after->sec ) |
elessair | 0:f269e3021894 | 953 | return( 1 ); |
elessair | 0:f269e3021894 | 954 | |
elessair | 0:f269e3021894 | 955 | return( 0 ); |
elessair | 0:f269e3021894 | 956 | } |
elessair | 0:f269e3021894 | 957 | |
elessair | 0:f269e3021894 | 958 | int mbedtls_x509_time_is_past( const mbedtls_x509_time *to ) |
elessair | 0:f269e3021894 | 959 | { |
elessair | 0:f269e3021894 | 960 | mbedtls_x509_time now; |
elessair | 0:f269e3021894 | 961 | |
elessair | 0:f269e3021894 | 962 | if( x509_get_current_time( &now ) != 0 ) |
elessair | 0:f269e3021894 | 963 | return( 1 ); |
elessair | 0:f269e3021894 | 964 | |
elessair | 0:f269e3021894 | 965 | return( x509_check_time( &now, to ) ); |
elessair | 0:f269e3021894 | 966 | } |
elessair | 0:f269e3021894 | 967 | |
elessair | 0:f269e3021894 | 968 | int mbedtls_x509_time_is_future( const mbedtls_x509_time *from ) |
elessair | 0:f269e3021894 | 969 | { |
elessair | 0:f269e3021894 | 970 | mbedtls_x509_time now; |
elessair | 0:f269e3021894 | 971 | |
elessair | 0:f269e3021894 | 972 | if( x509_get_current_time( &now ) != 0 ) |
elessair | 0:f269e3021894 | 973 | return( 1 ); |
elessair | 0:f269e3021894 | 974 | |
elessair | 0:f269e3021894 | 975 | return( x509_check_time( from, &now ) ); |
elessair | 0:f269e3021894 | 976 | } |
elessair | 0:f269e3021894 | 977 | |
elessair | 0:f269e3021894 | 978 | #else /* MBEDTLS_HAVE_TIME_DATE */ |
elessair | 0:f269e3021894 | 979 | |
elessair | 0:f269e3021894 | 980 | int mbedtls_x509_time_is_past( const mbedtls_x509_time *to ) |
elessair | 0:f269e3021894 | 981 | { |
elessair | 0:f269e3021894 | 982 | ((void) to); |
elessair | 0:f269e3021894 | 983 | return( 0 ); |
elessair | 0:f269e3021894 | 984 | } |
elessair | 0:f269e3021894 | 985 | |
elessair | 0:f269e3021894 | 986 | int mbedtls_x509_time_is_future( const mbedtls_x509_time *from ) |
elessair | 0:f269e3021894 | 987 | { |
elessair | 0:f269e3021894 | 988 | ((void) from); |
elessair | 0:f269e3021894 | 989 | return( 0 ); |
elessair | 0:f269e3021894 | 990 | } |
elessair | 0:f269e3021894 | 991 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
elessair | 0:f269e3021894 | 992 | |
elessair | 0:f269e3021894 | 993 | #if defined(MBEDTLS_SELF_TEST) |
elessair | 0:f269e3021894 | 994 | |
elessair | 0:f269e3021894 | 995 | #include "mbedtls/x509_crt.h" |
elessair | 0:f269e3021894 | 996 | #include "mbedtls/certs.h" |
elessair | 0:f269e3021894 | 997 | |
elessair | 0:f269e3021894 | 998 | /* |
elessair | 0:f269e3021894 | 999 | * Checkup routine |
elessair | 0:f269e3021894 | 1000 | */ |
elessair | 0:f269e3021894 | 1001 | int mbedtls_x509_self_test( int verbose ) |
elessair | 0:f269e3021894 | 1002 | { |
elessair | 0:f269e3021894 | 1003 | #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA1_C) |
elessair | 0:f269e3021894 | 1004 | int ret; |
elessair | 0:f269e3021894 | 1005 | uint32_t flags; |
elessair | 0:f269e3021894 | 1006 | mbedtls_x509_crt cacert; |
elessair | 0:f269e3021894 | 1007 | mbedtls_x509_crt clicert; |
elessair | 0:f269e3021894 | 1008 | |
elessair | 0:f269e3021894 | 1009 | if( verbose != 0 ) |
elessair | 0:f269e3021894 | 1010 | mbedtls_printf( " X.509 certificate load: " ); |
elessair | 0:f269e3021894 | 1011 | |
elessair | 0:f269e3021894 | 1012 | mbedtls_x509_crt_init( &clicert ); |
elessair | 0:f269e3021894 | 1013 | |
elessair | 0:f269e3021894 | 1014 | ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt, |
elessair | 0:f269e3021894 | 1015 | mbedtls_test_cli_crt_len ); |
elessair | 0:f269e3021894 | 1016 | if( ret != 0 ) |
elessair | 0:f269e3021894 | 1017 | { |
elessair | 0:f269e3021894 | 1018 | if( verbose != 0 ) |
elessair | 0:f269e3021894 | 1019 | mbedtls_printf( "failed\n" ); |
elessair | 0:f269e3021894 | 1020 | |
elessair | 0:f269e3021894 | 1021 | return( ret ); |
elessair | 0:f269e3021894 | 1022 | } |
elessair | 0:f269e3021894 | 1023 | |
elessair | 0:f269e3021894 | 1024 | mbedtls_x509_crt_init( &cacert ); |
elessair | 0:f269e3021894 | 1025 | |
elessair | 0:f269e3021894 | 1026 | ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt, |
elessair | 0:f269e3021894 | 1027 | mbedtls_test_ca_crt_len ); |
elessair | 0:f269e3021894 | 1028 | if( ret != 0 ) |
elessair | 0:f269e3021894 | 1029 | { |
elessair | 0:f269e3021894 | 1030 | if( verbose != 0 ) |
elessair | 0:f269e3021894 | 1031 | mbedtls_printf( "failed\n" ); |
elessair | 0:f269e3021894 | 1032 | |
elessair | 0:f269e3021894 | 1033 | return( ret ); |
elessair | 0:f269e3021894 | 1034 | } |
elessair | 0:f269e3021894 | 1035 | |
elessair | 0:f269e3021894 | 1036 | if( verbose != 0 ) |
elessair | 0:f269e3021894 | 1037 | mbedtls_printf( "passed\n X.509 signature verify: "); |
elessair | 0:f269e3021894 | 1038 | |
elessair | 0:f269e3021894 | 1039 | ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL ); |
elessair | 0:f269e3021894 | 1040 | if( ret != 0 ) |
elessair | 0:f269e3021894 | 1041 | { |
elessair | 0:f269e3021894 | 1042 | if( verbose != 0 ) |
elessair | 0:f269e3021894 | 1043 | mbedtls_printf( "failed\n" ); |
elessair | 0:f269e3021894 | 1044 | |
elessair | 0:f269e3021894 | 1045 | return( ret ); |
elessair | 0:f269e3021894 | 1046 | } |
elessair | 0:f269e3021894 | 1047 | |
elessair | 0:f269e3021894 | 1048 | if( verbose != 0 ) |
elessair | 0:f269e3021894 | 1049 | mbedtls_printf( "passed\n\n"); |
elessair | 0:f269e3021894 | 1050 | |
elessair | 0:f269e3021894 | 1051 | mbedtls_x509_crt_free( &cacert ); |
elessair | 0:f269e3021894 | 1052 | mbedtls_x509_crt_free( &clicert ); |
elessair | 0:f269e3021894 | 1053 | |
elessair | 0:f269e3021894 | 1054 | return( 0 ); |
elessair | 0:f269e3021894 | 1055 | #else |
elessair | 0:f269e3021894 | 1056 | ((void) verbose); |
elessair | 0:f269e3021894 | 1057 | return( 0 ); |
elessair | 0:f269e3021894 | 1058 | #endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA1_C */ |
elessair | 0:f269e3021894 | 1059 | } |
elessair | 0:f269e3021894 | 1060 | |
elessair | 0:f269e3021894 | 1061 | #endif /* MBEDTLS_SELF_TEST */ |
elessair | 0:f269e3021894 | 1062 | |
elessair | 0:f269e3021894 | 1063 | #endif /* MBEDTLS_X509_USE_C */ |