mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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