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