This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
timbeight
Date:
Thu May 19 16:02:10 2016 +0000
Revision:
1:0ddbe2d3319c
Parent:
0:f7c60d3e7b8a
This is my first commit while in the class.

Who changed what in which revision?

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