Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * X.509 certificate and private key decoding |
| 3 | * |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, Brainspark B.V. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | /* |
| 26 | * The ITU-T X.509 standard defines a certificate format for PKI. |
| 27 | * |
| 28 | * http://www.ietf.org/rfc/rfc3279.txt |
| 29 | * http://www.ietf.org/rfc/rfc3280.txt |
| 30 | * |
| 31 | * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc |
| 32 | * |
| 33 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf |
| 34 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf |
| 35 | */ |
| 36 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 37 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 38 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 39 | #else |
| 40 | #include POLARSSL_CONFIG_FILE |
| 41 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 42 | |
| 43 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
| 44 | |
| 45 | #include "polarssl/x509_crt.h" |
| 46 | #include "polarssl/oid.h" |
| 47 | #if defined(POLARSSL_PEM_PARSE_C) |
| 48 | #include "polarssl/pem.h" |
| 49 | #endif |
| 50 | |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 51 | #if defined(POLARSSL_PLATFORM_C) |
| 52 | #include "polarssl/platform.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 53 | #else |
| 54 | #define polarssl_malloc malloc |
| 55 | #define polarssl_free free |
| 56 | #endif |
| 57 | |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 58 | #if defined(POLARSSL_THREADING_C) |
| 59 | #include "polarssl/threading.h" |
| 60 | #endif |
| 61 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 62 | #include <string.h> |
| 63 | #include <stdlib.h> |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 64 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 65 | #include <windows.h> |
| 66 | #else |
| 67 | #include <time.h> |
| 68 | #endif |
| 69 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 70 | #if defined(EFIX64) || defined(EFI32) |
| 71 | #include <stdio.h> |
| 72 | #endif |
| 73 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 74 | #if defined(POLARSSL_FS_IO) |
| 75 | #include <stdio.h> |
Paul Bakker | 5ff3f91 | 2014-04-04 15:08:20 +0200 | [diff] [blame] | 76 | #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 77 | #include <sys/types.h> |
| 78 | #include <sys/stat.h> |
| 79 | #include <dirent.h> |
| 80 | #endif |
| 81 | #endif |
| 82 | |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 83 | /* Implementation that should never be optimized out by the compiler */ |
| 84 | static void polarssl_zeroize( void *v, size_t n ) { |
| 85 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 86 | } |
| 87 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 88 | /* |
| 89 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 90 | */ |
| 91 | static int x509_get_version( unsigned char **p, |
| 92 | const unsigned char *end, |
| 93 | int *ver ) |
| 94 | { |
| 95 | int ret; |
| 96 | size_t len; |
| 97 | |
| 98 | if( ( ret = asn1_get_tag( p, end, &len, |
| 99 | ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 ) |
| 100 | { |
| 101 | if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) |
| 102 | { |
| 103 | *ver = 0; |
| 104 | return( 0 ); |
| 105 | } |
| 106 | |
| 107 | return( ret ); |
| 108 | } |
| 109 | |
| 110 | end = *p + len; |
| 111 | |
| 112 | if( ( ret = asn1_get_int( p, end, ver ) ) != 0 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 113 | return( POLARSSL_ERR_X509_INVALID_VERSION + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 114 | |
| 115 | if( *p != end ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 116 | return( POLARSSL_ERR_X509_INVALID_VERSION + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 117 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 118 | |
| 119 | return( 0 ); |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Validity ::= SEQUENCE { |
| 124 | * notBefore Time, |
| 125 | * notAfter Time } |
| 126 | */ |
| 127 | static int x509_get_dates( unsigned char **p, |
| 128 | const unsigned char *end, |
| 129 | x509_time *from, |
| 130 | x509_time *to ) |
| 131 | { |
| 132 | int ret; |
| 133 | size_t len; |
| 134 | |
| 135 | if( ( ret = asn1_get_tag( p, end, &len, |
| 136 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 137 | return( POLARSSL_ERR_X509_INVALID_DATE + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 138 | |
| 139 | end = *p + len; |
| 140 | |
| 141 | if( ( ret = x509_get_time( p, end, from ) ) != 0 ) |
| 142 | return( ret ); |
| 143 | |
| 144 | if( ( ret = x509_get_time( p, end, to ) ) != 0 ) |
| 145 | return( ret ); |
| 146 | |
| 147 | if( *p != end ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 148 | return( POLARSSL_ERR_X509_INVALID_DATE + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 149 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 150 | |
| 151 | return( 0 ); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * X.509 v2/v3 unique identifier (not parsed) |
| 156 | */ |
| 157 | static int x509_get_uid( unsigned char **p, |
| 158 | const unsigned char *end, |
| 159 | x509_buf *uid, int n ) |
| 160 | { |
| 161 | int ret; |
| 162 | |
| 163 | if( *p == end ) |
| 164 | return( 0 ); |
| 165 | |
| 166 | uid->tag = **p; |
| 167 | |
| 168 | if( ( ret = asn1_get_tag( p, end, &uid->len, |
| 169 | ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 ) |
| 170 | { |
| 171 | if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) |
| 172 | return( 0 ); |
| 173 | |
| 174 | return( ret ); |
| 175 | } |
| 176 | |
| 177 | uid->p = *p; |
| 178 | *p += uid->len; |
| 179 | |
| 180 | return( 0 ); |
| 181 | } |
| 182 | |
| 183 | static int x509_get_basic_constraints( unsigned char **p, |
| 184 | const unsigned char *end, |
| 185 | int *ca_istrue, |
| 186 | int *max_pathlen ) |
| 187 | { |
| 188 | int ret; |
| 189 | size_t len; |
| 190 | |
| 191 | /* |
| 192 | * BasicConstraints ::= SEQUENCE { |
| 193 | * cA BOOLEAN DEFAULT FALSE, |
| 194 | * pathLenConstraint INTEGER (0..MAX) OPTIONAL } |
| 195 | */ |
| 196 | *ca_istrue = 0; /* DEFAULT FALSE */ |
| 197 | *max_pathlen = 0; /* endless */ |
| 198 | |
| 199 | if( ( ret = asn1_get_tag( p, end, &len, |
| 200 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 201 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 202 | |
| 203 | if( *p == end ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 204 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 205 | |
| 206 | if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 ) |
| 207 | { |
| 208 | if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) |
| 209 | ret = asn1_get_int( p, end, ca_istrue ); |
| 210 | |
| 211 | if( ret != 0 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 212 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 213 | |
| 214 | if( *ca_istrue != 0 ) |
| 215 | *ca_istrue = 1; |
| 216 | } |
| 217 | |
| 218 | if( *p == end ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 219 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 220 | |
| 221 | if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 222 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 223 | |
| 224 | if( *p != end ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 225 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 226 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 227 | |
| 228 | (*max_pathlen)++; |
| 229 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 230 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static int x509_get_ns_cert_type( unsigned char **p, |
| 234 | const unsigned char *end, |
| 235 | unsigned char *ns_cert_type) |
| 236 | { |
| 237 | int ret; |
| 238 | x509_bitstring bs = { 0, 0, NULL }; |
| 239 | |
| 240 | if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 241 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 242 | |
| 243 | if( bs.len != 1 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 244 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 245 | POLARSSL_ERR_ASN1_INVALID_LENGTH ); |
| 246 | |
| 247 | /* Get actual bitstring */ |
| 248 | *ns_cert_type = *bs.p; |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 249 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static int x509_get_key_usage( unsigned char **p, |
| 253 | const unsigned char *end, |
| 254 | unsigned char *key_usage) |
| 255 | { |
| 256 | int ret; |
| 257 | x509_bitstring bs = { 0, 0, NULL }; |
| 258 | |
| 259 | if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 260 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 261 | |
| 262 | if( bs.len < 1 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 263 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 264 | POLARSSL_ERR_ASN1_INVALID_LENGTH ); |
| 265 | |
| 266 | /* Get actual bitstring */ |
| 267 | *key_usage = *bs.p; |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 268 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
| 272 | * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId |
| 273 | * |
| 274 | * KeyPurposeId ::= OBJECT IDENTIFIER |
| 275 | */ |
| 276 | static int x509_get_ext_key_usage( unsigned char **p, |
| 277 | const unsigned char *end, |
| 278 | x509_sequence *ext_key_usage) |
| 279 | { |
| 280 | int ret; |
| 281 | |
| 282 | if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 283 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 284 | |
| 285 | /* Sequence length must be >= 1 */ |
| 286 | if( ext_key_usage->buf.p == NULL ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 287 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 288 | POLARSSL_ERR_ASN1_INVALID_LENGTH ); |
| 289 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 290 | return( 0 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /* |
| 294 | * SubjectAltName ::= GeneralNames |
| 295 | * |
| 296 | * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName |
| 297 | * |
| 298 | * GeneralName ::= CHOICE { |
| 299 | * otherName [0] OtherName, |
| 300 | * rfc822Name [1] IA5String, |
| 301 | * dNSName [2] IA5String, |
| 302 | * x400Address [3] ORAddress, |
| 303 | * directoryName [4] Name, |
| 304 | * ediPartyName [5] EDIPartyName, |
| 305 | * uniformResourceIdentifier [6] IA5String, |
| 306 | * iPAddress [7] OCTET STRING, |
| 307 | * registeredID [8] OBJECT IDENTIFIER } |
| 308 | * |
| 309 | * OtherName ::= SEQUENCE { |
| 310 | * type-id OBJECT IDENTIFIER, |
| 311 | * value [0] EXPLICIT ANY DEFINED BY type-id } |
| 312 | * |
| 313 | * EDIPartyName ::= SEQUENCE { |
| 314 | * nameAssigner [0] DirectoryString OPTIONAL, |
| 315 | * partyName [1] DirectoryString } |
| 316 | * |
| 317 | * NOTE: PolarSSL only parses and uses dNSName at this point. |
| 318 | */ |
| 319 | static int x509_get_subject_alt_name( unsigned char **p, |
| 320 | const unsigned char *end, |
| 321 | x509_sequence *subject_alt_name ) |
| 322 | { |
| 323 | int ret; |
| 324 | size_t len, tag_len; |
| 325 | asn1_buf *buf; |
| 326 | unsigned char tag; |
| 327 | asn1_sequence *cur = subject_alt_name; |
| 328 | |
| 329 | /* Get main sequence tag */ |
| 330 | if( ( ret = asn1_get_tag( p, end, &len, |
| 331 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 332 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 333 | |
| 334 | if( *p + len != end ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 335 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 336 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 337 | |
| 338 | while( *p < end ) |
| 339 | { |
| 340 | if( ( end - *p ) < 1 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 341 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 342 | POLARSSL_ERR_ASN1_OUT_OF_DATA ); |
| 343 | |
| 344 | tag = **p; |
| 345 | (*p)++; |
| 346 | if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 347 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 348 | |
| 349 | if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 350 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 351 | POLARSSL_ERR_ASN1_UNEXPECTED_TAG ); |
| 352 | |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 353 | /* Skip everything but DNS name */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 354 | if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) ) |
| 355 | { |
| 356 | *p += tag_len; |
| 357 | continue; |
| 358 | } |
| 359 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 360 | /* Allocate and assign next pointer */ |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 361 | if( cur->buf.p != NULL ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 362 | { |
| 363 | cur->next = (asn1_sequence *) polarssl_malloc( |
| 364 | sizeof( asn1_sequence ) ); |
| 365 | |
| 366 | if( cur->next == NULL ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 367 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 368 | POLARSSL_ERR_ASN1_MALLOC_FAILED ); |
| 369 | |
| 370 | memset( cur->next, 0, sizeof( asn1_sequence ) ); |
| 371 | cur = cur->next; |
| 372 | } |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 373 | |
| 374 | buf = &(cur->buf); |
| 375 | buf->tag = tag; |
| 376 | buf->p = *p; |
| 377 | buf->len = tag_len; |
| 378 | *p += buf->len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* Set final sequence entry's next pointer to NULL */ |
| 382 | cur->next = NULL; |
| 383 | |
| 384 | if( *p != end ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 385 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 386 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 387 | |
| 388 | return( 0 ); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * X.509 v3 extensions |
| 393 | * |
| 394 | * TODO: Perform all of the basic constraints tests required by the RFC |
| 395 | * TODO: Set values for undetected extensions to a sane default? |
| 396 | * |
| 397 | */ |
| 398 | static int x509_get_crt_ext( unsigned char **p, |
| 399 | const unsigned char *end, |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 400 | x509_crt *crt ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 401 | { |
| 402 | int ret; |
| 403 | size_t len; |
| 404 | unsigned char *end_ext_data, *end_ext_octet; |
| 405 | |
| 406 | if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 ) |
| 407 | { |
| 408 | if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) |
| 409 | return( 0 ); |
| 410 | |
| 411 | return( ret ); |
| 412 | } |
| 413 | |
| 414 | while( *p < end ) |
| 415 | { |
| 416 | /* |
| 417 | * Extension ::= SEQUENCE { |
| 418 | * extnID OBJECT IDENTIFIER, |
| 419 | * critical BOOLEAN DEFAULT FALSE, |
| 420 | * extnValue OCTET STRING } |
| 421 | */ |
| 422 | x509_buf extn_oid = {0, 0, NULL}; |
| 423 | int is_critical = 0; /* DEFAULT FALSE */ |
| 424 | int ext_type = 0; |
| 425 | |
| 426 | if( ( ret = asn1_get_tag( p, end, &len, |
| 427 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 428 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 429 | |
| 430 | end_ext_data = *p + len; |
| 431 | |
| 432 | /* Get extension ID */ |
| 433 | extn_oid.tag = **p; |
| 434 | |
| 435 | if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 436 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 437 | |
| 438 | extn_oid.p = *p; |
| 439 | *p += extn_oid.len; |
| 440 | |
| 441 | if( ( end - *p ) < 1 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 442 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 443 | POLARSSL_ERR_ASN1_OUT_OF_DATA ); |
| 444 | |
| 445 | /* Get optional critical */ |
| 446 | if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 && |
| 447 | ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 448 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 449 | |
| 450 | /* Data should be octet string type */ |
| 451 | if( ( ret = asn1_get_tag( p, end_ext_data, &len, |
| 452 | ASN1_OCTET_STRING ) ) != 0 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 453 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 454 | |
| 455 | end_ext_octet = *p + len; |
| 456 | |
| 457 | if( end_ext_octet != end_ext_data ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 458 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 459 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 460 | |
| 461 | /* |
| 462 | * Detect supported extensions |
| 463 | */ |
| 464 | ret = oid_get_x509_ext_type( &extn_oid, &ext_type ); |
| 465 | |
| 466 | if( ret != 0 ) |
| 467 | { |
| 468 | /* No parser found, skip extension */ |
| 469 | *p = end_ext_octet; |
| 470 | |
| 471 | #if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION) |
| 472 | if( is_critical ) |
| 473 | { |
| 474 | /* Data is marked as critical: fail */ |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 475 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 476 | POLARSSL_ERR_ASN1_UNEXPECTED_TAG ); |
| 477 | } |
| 478 | #endif |
| 479 | continue; |
| 480 | } |
| 481 | |
| 482 | crt->ext_types |= ext_type; |
| 483 | |
| 484 | switch( ext_type ) |
| 485 | { |
| 486 | case EXT_BASIC_CONSTRAINTS: |
| 487 | /* Parse basic constraints */ |
| 488 | if( ( ret = x509_get_basic_constraints( p, end_ext_octet, |
| 489 | &crt->ca_istrue, &crt->max_pathlen ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 490 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 491 | break; |
| 492 | |
| 493 | case EXT_KEY_USAGE: |
| 494 | /* Parse key usage */ |
| 495 | if( ( ret = x509_get_key_usage( p, end_ext_octet, |
| 496 | &crt->key_usage ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 497 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 498 | break; |
| 499 | |
| 500 | case EXT_EXTENDED_KEY_USAGE: |
| 501 | /* Parse extended key usage */ |
| 502 | if( ( ret = x509_get_ext_key_usage( p, end_ext_octet, |
| 503 | &crt->ext_key_usage ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 504 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 505 | break; |
| 506 | |
| 507 | case EXT_SUBJECT_ALT_NAME: |
| 508 | /* Parse subject alt name */ |
| 509 | if( ( ret = x509_get_subject_alt_name( p, end_ext_octet, |
| 510 | &crt->subject_alt_names ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 511 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 512 | break; |
| 513 | |
| 514 | case EXT_NS_CERT_TYPE: |
| 515 | /* Parse netscape certificate type */ |
| 516 | if( ( ret = x509_get_ns_cert_type( p, end_ext_octet, |
| 517 | &crt->ns_cert_type ) ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 518 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 519 | break; |
| 520 | |
| 521 | default: |
| 522 | return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE ); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if( *p != end ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 527 | return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 528 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 529 | |
| 530 | return( 0 ); |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Parse and fill a single X.509 certificate in DER format |
| 535 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 536 | static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf, |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 537 | size_t buflen ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 538 | { |
| 539 | int ret; |
| 540 | size_t len; |
| 541 | unsigned char *p, *end, *crt_end; |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 542 | x509_buf sig_params1, sig_params2; |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 543 | |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 544 | memset( &sig_params1, 0, sizeof( x509_buf ) ); |
| 545 | memset( &sig_params2, 0, sizeof( x509_buf ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 546 | |
| 547 | /* |
| 548 | * Check for valid input |
| 549 | */ |
| 550 | if( crt == NULL || buf == NULL ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 551 | return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 552 | |
| 553 | p = (unsigned char *) polarssl_malloc( len = buflen ); |
| 554 | |
| 555 | if( p == NULL ) |
| 556 | return( POLARSSL_ERR_X509_MALLOC_FAILED ); |
| 557 | |
| 558 | memcpy( p, buf, buflen ); |
| 559 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 560 | crt->raw.p = p; |
| 561 | crt->raw.len = len; |
| 562 | end = p + len; |
| 563 | |
| 564 | /* |
| 565 | * Certificate ::= SEQUENCE { |
| 566 | * tbsCertificate TBSCertificate, |
| 567 | * signatureAlgorithm AlgorithmIdentifier, |
| 568 | * signatureValue BIT STRING } |
| 569 | */ |
| 570 | if( ( ret = asn1_get_tag( &p, end, &len, |
| 571 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
| 572 | { |
| 573 | x509_crt_free( crt ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 574 | return( POLARSSL_ERR_X509_INVALID_FORMAT ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | if( len > (size_t) ( end - p ) ) |
| 578 | { |
| 579 | x509_crt_free( crt ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 580 | return( POLARSSL_ERR_X509_INVALID_FORMAT + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 581 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 582 | } |
| 583 | crt_end = p + len; |
| 584 | |
| 585 | /* |
| 586 | * TBSCertificate ::= SEQUENCE { |
| 587 | */ |
| 588 | crt->tbs.p = p; |
| 589 | |
| 590 | if( ( ret = asn1_get_tag( &p, end, &len, |
| 591 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
| 592 | { |
| 593 | x509_crt_free( crt ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 594 | return( POLARSSL_ERR_X509_INVALID_FORMAT + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | end = p + len; |
| 598 | crt->tbs.len = end - crt->tbs.p; |
| 599 | |
| 600 | /* |
| 601 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 602 | * |
| 603 | * CertificateSerialNumber ::= INTEGER |
| 604 | * |
| 605 | * signature AlgorithmIdentifier |
| 606 | */ |
| 607 | if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 || |
| 608 | ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 || |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 609 | ( ret = x509_get_alg( &p, end, &crt->sig_oid1, |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 610 | &sig_params1 ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 611 | { |
| 612 | x509_crt_free( crt ); |
| 613 | return( ret ); |
| 614 | } |
| 615 | |
| 616 | crt->version++; |
| 617 | |
| 618 | if( crt->version > 3 ) |
| 619 | { |
| 620 | x509_crt_free( crt ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 621 | return( POLARSSL_ERR_X509_UNKNOWN_VERSION ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 622 | } |
| 623 | |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 624 | if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &sig_params1, |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 625 | &crt->sig_md, &crt->sig_pk, |
| 626 | &crt->sig_opts ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 627 | { |
| 628 | x509_crt_free( crt ); |
| 629 | return( ret ); |
| 630 | } |
| 631 | |
| 632 | /* |
| 633 | * issuer Name |
| 634 | */ |
| 635 | crt->issuer_raw.p = p; |
| 636 | |
| 637 | if( ( ret = asn1_get_tag( &p, end, &len, |
| 638 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
| 639 | { |
| 640 | x509_crt_free( crt ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 641 | return( POLARSSL_ERR_X509_INVALID_FORMAT + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 ) |
| 645 | { |
| 646 | x509_crt_free( crt ); |
| 647 | return( ret ); |
| 648 | } |
| 649 | |
| 650 | crt->issuer_raw.len = p - crt->issuer_raw.p; |
| 651 | |
| 652 | /* |
| 653 | * Validity ::= SEQUENCE { |
| 654 | * notBefore Time, |
| 655 | * notAfter Time } |
| 656 | * |
| 657 | */ |
| 658 | if( ( ret = x509_get_dates( &p, end, &crt->valid_from, |
| 659 | &crt->valid_to ) ) != 0 ) |
| 660 | { |
| 661 | x509_crt_free( crt ); |
| 662 | return( ret ); |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * subject Name |
| 667 | */ |
| 668 | crt->subject_raw.p = p; |
| 669 | |
| 670 | if( ( ret = asn1_get_tag( &p, end, &len, |
| 671 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
| 672 | { |
| 673 | x509_crt_free( crt ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 674 | return( POLARSSL_ERR_X509_INVALID_FORMAT + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 ) |
| 678 | { |
| 679 | x509_crt_free( crt ); |
| 680 | return( ret ); |
| 681 | } |
| 682 | |
| 683 | crt->subject_raw.len = p - crt->subject_raw.p; |
| 684 | |
| 685 | /* |
| 686 | * SubjectPublicKeyInfo |
| 687 | */ |
Paul Bakker | da77115 | 2013-09-16 22:45:03 +0200 | [diff] [blame] | 688 | if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 689 | { |
| 690 | x509_crt_free( crt ); |
| 691 | return( ret ); |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, |
| 696 | * -- If present, version shall be v2 or v3 |
| 697 | * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, |
| 698 | * -- If present, version shall be v2 or v3 |
| 699 | * extensions [3] EXPLICIT Extensions OPTIONAL |
| 700 | * -- If present, version shall be v3 |
| 701 | */ |
| 702 | if( crt->version == 2 || crt->version == 3 ) |
| 703 | { |
| 704 | ret = x509_get_uid( &p, end, &crt->issuer_id, 1 ); |
| 705 | if( ret != 0 ) |
| 706 | { |
| 707 | x509_crt_free( crt ); |
| 708 | return( ret ); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | if( crt->version == 2 || crt->version == 3 ) |
| 713 | { |
| 714 | ret = x509_get_uid( &p, end, &crt->subject_id, 2 ); |
| 715 | if( ret != 0 ) |
| 716 | { |
| 717 | x509_crt_free( crt ); |
| 718 | return( ret ); |
| 719 | } |
| 720 | } |
| 721 | |
Paul Bakker | c27c4e2 | 2013-09-23 15:01:36 +0200 | [diff] [blame] | 722 | #if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 723 | if( crt->version == 3 ) |
| 724 | { |
Paul Bakker | c27c4e2 | 2013-09-23 15:01:36 +0200 | [diff] [blame] | 725 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 726 | ret = x509_get_crt_ext( &p, end, crt); |
| 727 | if( ret != 0 ) |
| 728 | { |
| 729 | x509_crt_free( crt ); |
| 730 | return( ret ); |
| 731 | } |
Paul Bakker | c27c4e2 | 2013-09-23 15:01:36 +0200 | [diff] [blame] | 732 | #if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 733 | } |
Paul Bakker | c27c4e2 | 2013-09-23 15:01:36 +0200 | [diff] [blame] | 734 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 735 | |
| 736 | if( p != end ) |
| 737 | { |
| 738 | x509_crt_free( crt ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 739 | return( POLARSSL_ERR_X509_INVALID_FORMAT + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 740 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 741 | } |
| 742 | |
| 743 | end = crt_end; |
| 744 | |
| 745 | /* |
| 746 | * } |
| 747 | * -- end of TBSCertificate |
| 748 | * |
| 749 | * signatureAlgorithm AlgorithmIdentifier, |
| 750 | * signatureValue BIT STRING |
| 751 | */ |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 752 | if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params2 ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 753 | { |
| 754 | x509_crt_free( crt ); |
| 755 | return( ret ); |
| 756 | } |
| 757 | |
| 758 | if( crt->sig_oid1.len != crt->sig_oid2.len || |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 759 | memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 || |
| 760 | sig_params1.len != sig_params2.len || |
| 761 | memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 762 | { |
| 763 | x509_crt_free( crt ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 764 | return( POLARSSL_ERR_X509_SIG_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 ) |
| 768 | { |
| 769 | x509_crt_free( crt ); |
| 770 | return( ret ); |
| 771 | } |
| 772 | |
| 773 | if( p != end ) |
| 774 | { |
| 775 | x509_crt_free( crt ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 776 | return( POLARSSL_ERR_X509_INVALID_FORMAT + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 777 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 778 | } |
| 779 | |
| 780 | return( 0 ); |
| 781 | } |
| 782 | |
| 783 | /* |
| 784 | * Parse one X.509 certificate in DER format from a buffer and add them to a |
| 785 | * chained list |
| 786 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 787 | int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf, |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 788 | size_t buflen ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 789 | { |
| 790 | int ret; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 791 | x509_crt *crt = chain, *prev = NULL; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 792 | |
| 793 | /* |
| 794 | * Check for valid input |
| 795 | */ |
| 796 | if( crt == NULL || buf == NULL ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 797 | return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 798 | |
| 799 | while( crt->version != 0 && crt->next != NULL ) |
| 800 | { |
| 801 | prev = crt; |
| 802 | crt = crt->next; |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | * Add new certificate on the end of the chain if needed. |
| 807 | */ |
| 808 | if ( crt->version != 0 && crt->next == NULL) |
| 809 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 810 | crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 811 | |
| 812 | if( crt->next == NULL ) |
| 813 | return( POLARSSL_ERR_X509_MALLOC_FAILED ); |
| 814 | |
| 815 | prev = crt; |
| 816 | crt = crt->next; |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 817 | x509_crt_init( crt ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 818 | } |
| 819 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 820 | if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 821 | { |
| 822 | if( prev ) |
| 823 | prev->next = NULL; |
| 824 | |
| 825 | if( crt != chain ) |
| 826 | polarssl_free( crt ); |
| 827 | |
| 828 | return( ret ); |
| 829 | } |
| 830 | |
| 831 | return( 0 ); |
| 832 | } |
| 833 | |
| 834 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 835 | * Parse one or more PEM certificates from a buffer and add them to the chained |
| 836 | * list |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 837 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 838 | int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 839 | { |
| 840 | int success = 0, first_error = 0, total_failed = 0; |
| 841 | int buf_format = X509_FORMAT_DER; |
| 842 | |
| 843 | /* |
| 844 | * Check for valid input |
| 845 | */ |
| 846 | if( chain == NULL || buf == NULL ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 847 | return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 848 | |
| 849 | /* |
| 850 | * Determine buffer content. Buffer contains either one DER certificate or |
| 851 | * one or more PEM certificates. |
| 852 | */ |
| 853 | #if defined(POLARSSL_PEM_PARSE_C) |
| 854 | if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL ) |
| 855 | buf_format = X509_FORMAT_PEM; |
| 856 | #endif |
| 857 | |
| 858 | if( buf_format == X509_FORMAT_DER ) |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 859 | return x509_crt_parse_der( chain, buf, buflen ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 860 | |
| 861 | #if defined(POLARSSL_PEM_PARSE_C) |
| 862 | if( buf_format == X509_FORMAT_PEM ) |
| 863 | { |
| 864 | int ret; |
| 865 | pem_context pem; |
| 866 | |
| 867 | while( buflen > 0 ) |
| 868 | { |
| 869 | size_t use_len; |
| 870 | pem_init( &pem ); |
| 871 | |
| 872 | ret = pem_read_buffer( &pem, |
| 873 | "-----BEGIN CERTIFICATE-----", |
| 874 | "-----END CERTIFICATE-----", |
| 875 | buf, NULL, 0, &use_len ); |
| 876 | |
| 877 | if( ret == 0 ) |
| 878 | { |
| 879 | /* |
| 880 | * Was PEM encoded |
| 881 | */ |
| 882 | buflen -= use_len; |
| 883 | buf += use_len; |
| 884 | } |
| 885 | else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA ) |
| 886 | { |
| 887 | return( ret ); |
| 888 | } |
| 889 | else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
| 890 | { |
| 891 | pem_free( &pem ); |
| 892 | |
| 893 | /* |
| 894 | * PEM header and footer were found |
| 895 | */ |
| 896 | buflen -= use_len; |
| 897 | buf += use_len; |
| 898 | |
| 899 | if( first_error == 0 ) |
| 900 | first_error = ret; |
| 901 | |
| 902 | continue; |
| 903 | } |
| 904 | else |
| 905 | break; |
| 906 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 907 | ret = x509_crt_parse_der( chain, pem.buf, pem.buflen ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 908 | |
| 909 | pem_free( &pem ); |
| 910 | |
| 911 | if( ret != 0 ) |
| 912 | { |
| 913 | /* |
| 914 | * Quit parsing on a memory error |
| 915 | */ |
| 916 | if( ret == POLARSSL_ERR_X509_MALLOC_FAILED ) |
| 917 | return( ret ); |
| 918 | |
| 919 | if( first_error == 0 ) |
| 920 | first_error = ret; |
| 921 | |
| 922 | total_failed++; |
| 923 | continue; |
| 924 | } |
| 925 | |
| 926 | success = 1; |
| 927 | } |
| 928 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 929 | #endif /* POLARSSL_PEM_PARSE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 930 | |
| 931 | if( success ) |
| 932 | return( total_failed ); |
| 933 | else if( first_error ) |
| 934 | return( first_error ); |
| 935 | else |
| 936 | return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT ); |
| 937 | } |
| 938 | |
| 939 | #if defined(POLARSSL_FS_IO) |
| 940 | /* |
| 941 | * Load one or more certificates and add them to the chained list |
| 942 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 943 | int x509_crt_parse_file( x509_crt *chain, const char *path ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 944 | { |
| 945 | int ret; |
| 946 | size_t n; |
| 947 | unsigned char *buf; |
| 948 | |
| 949 | if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 ) |
| 950 | return( ret ); |
| 951 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 952 | ret = x509_crt_parse( chain, buf, n ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 953 | |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 954 | polarssl_zeroize( buf, n + 1 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 955 | polarssl_free( buf ); |
| 956 | |
| 957 | return( ret ); |
| 958 | } |
| 959 | |
Manuel Pégourié-Gonnard | 6df0957 | 2014-02-12 09:29:05 +0100 | [diff] [blame] | 960 | #if defined(POLARSSL_THREADING_PTHREAD) |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 961 | static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 962 | #endif |
| 963 | |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 964 | int x509_crt_parse_path( x509_crt *chain, const char *path ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 965 | { |
| 966 | int ret = 0; |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 967 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 968 | int w_ret; |
| 969 | WCHAR szDir[MAX_PATH]; |
| 970 | char filename[MAX_PATH]; |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 971 | char *p; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 972 | int len = (int) strlen( path ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 973 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 974 | WIN32_FIND_DATAW file_data; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 975 | HANDLE hFind; |
| 976 | |
| 977 | if( len > MAX_PATH - 3 ) |
Paul Bakker | 3cf63ed | 2013-09-23 15:10:16 +0200 | [diff] [blame] | 978 | return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 979 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 980 | memset( szDir, 0, sizeof(szDir) ); |
| 981 | memset( filename, 0, MAX_PATH ); |
| 982 | memcpy( filename, path, len ); |
| 983 | filename[len++] = '\\'; |
| 984 | p = filename + len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 985 | filename[len++] = '*'; |
| 986 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 987 | w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir, |
| 988 | MAX_PATH - 3 ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 989 | |
| 990 | hFind = FindFirstFileW( szDir, &file_data ); |
Paul Bakker | 4aa40d4 | 2013-10-11 10:49:24 +0200 | [diff] [blame] | 991 | if (hFind == INVALID_HANDLE_VALUE) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 992 | return( POLARSSL_ERR_X509_FILE_IO_ERROR ); |
| 993 | |
| 994 | len = MAX_PATH - len; |
| 995 | do |
| 996 | { |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 997 | memset( p, 0, len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 998 | |
| 999 | if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) |
| 1000 | continue; |
| 1001 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1002 | w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName, |
| 1003 | lstrlenW(file_data.cFileName), |
| 1004 | p, len - 1, |
| 1005 | NULL, NULL ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1006 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1007 | w_ret = x509_crt_parse_file( chain, filename ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1008 | if( w_ret < 0 ) |
| 1009 | ret++; |
| 1010 | else |
| 1011 | ret += w_ret; |
| 1012 | } |
| 1013 | while( FindNextFileW( hFind, &file_data ) != 0 ); |
| 1014 | |
Paul Bakker | 4aa40d4 | 2013-10-11 10:49:24 +0200 | [diff] [blame] | 1015 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1016 | ret = POLARSSL_ERR_X509_FILE_IO_ERROR; |
| 1017 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1018 | FindClose( hFind ); |
Paul Bakker | be089b0 | 2013-10-14 15:51:50 +0200 | [diff] [blame] | 1019 | #else /* _WIN32 */ |
Manuel Pégourié-Gonnard | 964bf9b | 2013-11-26 16:47:11 +0100 | [diff] [blame] | 1020 | int t_ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1021 | struct stat sb; |
Manuel Pégourié-Gonnard | 964bf9b | 2013-11-26 16:47:11 +0100 | [diff] [blame] | 1022 | struct dirent *entry; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1023 | char entry_name[255]; |
| 1024 | DIR *dir = opendir( path ); |
| 1025 | |
| 1026 | if( dir == NULL) |
| 1027 | return( POLARSSL_ERR_X509_FILE_IO_ERROR ); |
| 1028 | |
Manuel Pégourié-Gonnard | 6df0957 | 2014-02-12 09:29:05 +0100 | [diff] [blame] | 1029 | #if defined(POLARSSL_THREADING_PTHREAD) |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1030 | if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 ) |
| 1031 | return( ret ); |
| 1032 | #endif |
| 1033 | |
Manuel Pégourié-Gonnard | 964bf9b | 2013-11-26 16:47:11 +0100 | [diff] [blame] | 1034 | while( ( entry = readdir( dir ) ) != NULL ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1035 | { |
Manuel Pégourié-Gonnard | 964bf9b | 2013-11-26 16:47:11 +0100 | [diff] [blame] | 1036 | snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1037 | |
Manuel Pégourié-Gonnard | 964bf9b | 2013-11-26 16:47:11 +0100 | [diff] [blame] | 1038 | if( stat( entry_name, &sb ) == -1 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1039 | { |
| 1040 | closedir( dir ); |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1041 | ret = POLARSSL_ERR_X509_FILE_IO_ERROR; |
| 1042 | goto cleanup; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | if( !S_ISREG( sb.st_mode ) ) |
| 1046 | continue; |
| 1047 | |
| 1048 | // Ignore parse errors |
| 1049 | // |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1050 | t_ret = x509_crt_parse_file( chain, entry_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1051 | if( t_ret < 0 ) |
| 1052 | ret++; |
| 1053 | else |
| 1054 | ret += t_ret; |
| 1055 | } |
| 1056 | closedir( dir ); |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1057 | |
| 1058 | cleanup: |
Manuel Pégourié-Gonnard | 6df0957 | 2014-02-12 09:29:05 +0100 | [diff] [blame] | 1059 | #if defined(POLARSSL_THREADING_PTHREAD) |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1060 | if( polarssl_mutex_unlock( &readdir_mutex ) != 0 ) |
| 1061 | ret = POLARSSL_ERR_THREADING_MUTEX_ERROR; |
| 1062 | #endif |
| 1063 | |
Paul Bakker | be089b0 | 2013-10-14 15:51:50 +0200 | [diff] [blame] | 1064 | #endif /* _WIN32 */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1065 | |
| 1066 | return( ret ); |
| 1067 | } |
| 1068 | #endif /* POLARSSL_FS_IO */ |
| 1069 | |
Paul Bakker | 6edcd41 | 2013-10-29 15:22:54 +0100 | [diff] [blame] | 1070 | #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \ |
| 1071 | !defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1072 | #include <stdarg.h> |
| 1073 | |
| 1074 | #if !defined vsnprintf |
| 1075 | #define vsnprintf _vsnprintf |
| 1076 | #endif // vsnprintf |
| 1077 | |
| 1078 | /* |
| 1079 | * Windows _snprintf and _vsnprintf are not compatible to linux versions. |
| 1080 | * Result value is not size of buffer needed, but -1 if no fit is possible. |
| 1081 | * |
| 1082 | * This fuction tries to 'fix' this by at least suggesting enlarging the |
| 1083 | * size by 20. |
| 1084 | */ |
| 1085 | static int compat_snprintf(char *str, size_t size, const char *format, ...) |
| 1086 | { |
| 1087 | va_list ap; |
| 1088 | int res = -1; |
| 1089 | |
| 1090 | va_start( ap, format ); |
| 1091 | |
| 1092 | res = vsnprintf( str, size, format, ap ); |
| 1093 | |
| 1094 | va_end( ap ); |
| 1095 | |
| 1096 | // No quick fix possible |
| 1097 | if ( res < 0 ) |
| 1098 | return( (int) size + 20 ); |
| 1099 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 1100 | return( res ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | #define snprintf compat_snprintf |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1104 | #endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1105 | |
| 1106 | #define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2 |
| 1107 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 1108 | #define SAFE_SNPRINTF() \ |
| 1109 | { \ |
| 1110 | if( ret == -1 ) \ |
| 1111 | return( -1 ); \ |
| 1112 | \ |
| 1113 | if ( (unsigned int) ret > n ) { \ |
| 1114 | p[n - 1] = '\0'; \ |
| 1115 | return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \ |
| 1116 | } \ |
| 1117 | \ |
| 1118 | n -= (unsigned int) ret; \ |
| 1119 | p += (unsigned int) ret; \ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1120 | } |
| 1121 | |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1122 | static int x509_info_subject_alt_name( char **buf, size_t *size, |
| 1123 | const x509_sequence *subject_alt_name ) |
| 1124 | { |
| 1125 | size_t i; |
| 1126 | size_t n = *size; |
| 1127 | char *p = *buf; |
| 1128 | const x509_sequence *cur = subject_alt_name; |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1129 | const char *sep = ""; |
| 1130 | size_t sep_len = 0; |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1131 | |
| 1132 | while( cur != NULL ) |
| 1133 | { |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1134 | if( cur->buf.len + sep_len >= n ) |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1135 | { |
| 1136 | *p = '\0'; |
| 1137 | return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); |
| 1138 | } |
| 1139 | |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1140 | n -= cur->buf.len + sep_len; |
| 1141 | for( i = 0; i < sep_len; i++ ) |
| 1142 | *p++ = sep[i]; |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1143 | for( i = 0; i < cur->buf.len; i++ ) |
| 1144 | *p++ = cur->buf.p[i]; |
| 1145 | |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1146 | sep = ", "; |
| 1147 | sep_len = 2; |
| 1148 | |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1149 | cur = cur->next; |
| 1150 | } |
| 1151 | |
| 1152 | *p = '\0'; |
| 1153 | |
| 1154 | *size = n; |
| 1155 | *buf = p; |
| 1156 | |
| 1157 | return( 0 ); |
| 1158 | } |
| 1159 | |
Manuel Pégourié-Gonnard | 0db29b0 | 2014-04-01 18:12:24 +0200 | [diff] [blame] | 1160 | #define PRINT_ITEM(i) \ |
| 1161 | { \ |
| 1162 | ret = snprintf( p, n, "%s" i, sep ); \ |
| 1163 | SAFE_SNPRINTF(); \ |
| 1164 | sep = ", "; \ |
| 1165 | } |
| 1166 | |
| 1167 | #define CERT_TYPE(type,name) \ |
| 1168 | if( ns_cert_type & type ) \ |
| 1169 | PRINT_ITEM( name ); |
| 1170 | |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 1171 | static int x509_info_cert_type( char **buf, size_t *size, |
| 1172 | unsigned char ns_cert_type ) |
| 1173 | { |
| 1174 | int ret; |
| 1175 | size_t n = *size; |
| 1176 | char *p = *buf; |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1177 | const char *sep = ""; |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 1178 | |
Manuel Pégourié-Gonnard | 0db29b0 | 2014-04-01 18:12:24 +0200 | [diff] [blame] | 1179 | CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" ); |
| 1180 | CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" ); |
| 1181 | CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" ); |
| 1182 | CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" ); |
| 1183 | CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" ); |
| 1184 | CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" ); |
| 1185 | CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" ); |
| 1186 | CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" ); |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 1187 | |
| 1188 | *size = n; |
| 1189 | *buf = p; |
| 1190 | |
| 1191 | return( 0 ); |
| 1192 | } |
| 1193 | |
Manuel Pégourié-Gonnard | 0db29b0 | 2014-04-01 18:12:24 +0200 | [diff] [blame] | 1194 | #define KEY_USAGE(code,name) \ |
| 1195 | if( key_usage & code ) \ |
| 1196 | PRINT_ITEM( name ); |
| 1197 | |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 1198 | static int x509_info_key_usage( char **buf, size_t *size, |
| 1199 | unsigned char key_usage ) |
| 1200 | { |
| 1201 | int ret; |
| 1202 | size_t n = *size; |
| 1203 | char *p = *buf; |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1204 | const char *sep = ""; |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 1205 | |
Manuel Pégourié-Gonnard | 0db29b0 | 2014-04-01 18:12:24 +0200 | [diff] [blame] | 1206 | KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" ); |
| 1207 | KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" ); |
| 1208 | KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" ); |
| 1209 | KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" ); |
| 1210 | KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" ); |
| 1211 | KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" ); |
| 1212 | KEY_USAGE( KU_CRL_SIGN, "CRL Sign" ); |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 1213 | |
| 1214 | *size = n; |
| 1215 | *buf = p; |
| 1216 | |
| 1217 | return( 0 ); |
| 1218 | } |
| 1219 | |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1220 | static int x509_info_ext_key_usage( char **buf, size_t *size, |
| 1221 | const x509_sequence *extended_key_usage ) |
| 1222 | { |
| 1223 | int ret; |
| 1224 | const char *desc; |
| 1225 | size_t n = *size; |
| 1226 | char *p = *buf; |
| 1227 | const x509_sequence *cur = extended_key_usage; |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1228 | const char *sep = ""; |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1229 | |
| 1230 | while( cur != NULL ) |
| 1231 | { |
| 1232 | if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 ) |
| 1233 | desc = "???"; |
| 1234 | |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1235 | ret = snprintf( p, n, "%s%s", sep, desc ); |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1236 | SAFE_SNPRINTF(); |
| 1237 | |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1238 | sep = ", "; |
| 1239 | |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1240 | cur = cur->next; |
| 1241 | } |
| 1242 | |
| 1243 | *size = n; |
| 1244 | *buf = p; |
| 1245 | |
| 1246 | return( 0 ); |
| 1247 | } |
| 1248 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1249 | /* |
| 1250 | * Return an informational string about the certificate. |
| 1251 | */ |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1252 | #define BEFORE_COLON 18 |
| 1253 | #define BC "18" |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1254 | int x509_crt_info( char *buf, size_t size, const char *prefix, |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1255 | const x509_crt *crt ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1256 | { |
| 1257 | int ret; |
| 1258 | size_t n; |
| 1259 | char *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1260 | char key_size_str[BEFORE_COLON]; |
| 1261 | |
| 1262 | p = buf; |
| 1263 | n = size; |
| 1264 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1265 | ret = snprintf( p, n, "%scert. version : %d\n", |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1266 | prefix, crt->version ); |
| 1267 | SAFE_SNPRINTF(); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1268 | ret = snprintf( p, n, "%sserial number : ", |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1269 | prefix ); |
| 1270 | SAFE_SNPRINTF(); |
| 1271 | |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 1272 | ret = x509_serial_gets( p, n, &crt->serial); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1273 | SAFE_SNPRINTF(); |
| 1274 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1275 | ret = snprintf( p, n, "\n%sissuer name : ", prefix ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1276 | SAFE_SNPRINTF(); |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 1277 | ret = x509_dn_gets( p, n, &crt->issuer ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1278 | SAFE_SNPRINTF(); |
| 1279 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1280 | ret = snprintf( p, n, "\n%ssubject name : ", prefix ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1281 | SAFE_SNPRINTF(); |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 1282 | ret = x509_dn_gets( p, n, &crt->subject ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1283 | SAFE_SNPRINTF(); |
| 1284 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1285 | ret = snprintf( p, n, "\n%sissued on : " \ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1286 | "%04d-%02d-%02d %02d:%02d:%02d", prefix, |
| 1287 | crt->valid_from.year, crt->valid_from.mon, |
| 1288 | crt->valid_from.day, crt->valid_from.hour, |
| 1289 | crt->valid_from.min, crt->valid_from.sec ); |
| 1290 | SAFE_SNPRINTF(); |
| 1291 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1292 | ret = snprintf( p, n, "\n%sexpires on : " \ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1293 | "%04d-%02d-%02d %02d:%02d:%02d", prefix, |
| 1294 | crt->valid_to.year, crt->valid_to.mon, |
| 1295 | crt->valid_to.day, crt->valid_to.hour, |
| 1296 | crt->valid_to.min, crt->valid_to.sec ); |
| 1297 | SAFE_SNPRINTF(); |
| 1298 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1299 | ret = snprintf( p, n, "\n%ssigned using : ", prefix ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1300 | SAFE_SNPRINTF(); |
| 1301 | |
Manuel Pégourié-Gonnard | 9113603 | 2014-06-05 15:41:39 +0200 | [diff] [blame] | 1302 | ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk, |
Manuel Pégourié-Gonnard | bf696d0 | 2014-06-05 17:07:30 +0200 | [diff] [blame] | 1303 | crt->sig_md, crt->sig_opts ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1304 | SAFE_SNPRINTF(); |
| 1305 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1306 | /* Key size */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1307 | if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON, |
| 1308 | pk_get_name( &crt->pk ) ) ) != 0 ) |
| 1309 | { |
| 1310 | return( ret ); |
| 1311 | } |
| 1312 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1313 | ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1314 | (int) pk_get_size( &crt->pk ) ); |
| 1315 | SAFE_SNPRINTF(); |
| 1316 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1317 | /* |
| 1318 | * Optional extensions |
| 1319 | */ |
| 1320 | |
| 1321 | if( crt->ext_types & EXT_BASIC_CONSTRAINTS ) |
| 1322 | { |
| 1323 | ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix, |
| 1324 | crt->ca_istrue ? "true" : "false" ); |
| 1325 | SAFE_SNPRINTF(); |
| 1326 | |
| 1327 | if( crt->max_pathlen > 0 ) |
| 1328 | { |
| 1329 | ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 ); |
| 1330 | SAFE_SNPRINTF(); |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | if( crt->ext_types & EXT_SUBJECT_ALT_NAME ) |
| 1335 | { |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1336 | ret = snprintf( p, n, "\n%ssubject alt name : ", prefix ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1337 | SAFE_SNPRINTF(); |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1338 | |
| 1339 | if( ( ret = x509_info_subject_alt_name( &p, &n, |
| 1340 | &crt->subject_alt_names ) ) != 0 ) |
| 1341 | return( ret ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | if( crt->ext_types & EXT_NS_CERT_TYPE ) |
| 1345 | { |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1346 | ret = snprintf( p, n, "\n%scert. type : ", prefix ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1347 | SAFE_SNPRINTF(); |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 1348 | |
| 1349 | if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 ) |
| 1350 | return( ret ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | if( crt->ext_types & EXT_KEY_USAGE ) |
| 1354 | { |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1355 | ret = snprintf( p, n, "\n%skey usage : ", prefix ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1356 | SAFE_SNPRINTF(); |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 1357 | |
| 1358 | if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 ) |
| 1359 | return( ret ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | if( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) |
| 1363 | { |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1364 | ret = snprintf( p, n, "\n%sext key usage : ", prefix ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1365 | SAFE_SNPRINTF(); |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1366 | |
| 1367 | if( ( ret = x509_info_ext_key_usage( &p, &n, |
| 1368 | &crt->ext_key_usage ) ) != 0 ) |
| 1369 | return( ret ); |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | ret = snprintf( p, n, "\n" ); |
| 1373 | SAFE_SNPRINTF(); |
| 1374 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1375 | return( (int) ( size - n ) ); |
| 1376 | } |
| 1377 | |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 1378 | #if defined(POLARSSL_X509_CHECK_KEY_USAGE) |
| 1379 | int x509_crt_check_key_usage( const x509_crt *crt, int usage ) |
| 1380 | { |
| 1381 | if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 && |
| 1382 | ( crt->key_usage & usage ) != usage ) |
| 1383 | return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); |
| 1384 | |
| 1385 | return( 0 ); |
| 1386 | } |
| 1387 | #endif |
| 1388 | |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 1389 | #if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) |
| 1390 | int x509_crt_check_extended_key_usage( const x509_crt *crt, |
| 1391 | const char *usage_oid, |
| 1392 | size_t usage_len ) |
| 1393 | { |
| 1394 | const x509_sequence *cur; |
| 1395 | |
| 1396 | /* Extension is not mandatory, absent means no restriction */ |
| 1397 | if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 ) |
| 1398 | return( 0 ); |
| 1399 | |
| 1400 | /* |
| 1401 | * Look for the requested usage (or wildcard ANY) in our list |
| 1402 | */ |
| 1403 | for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next ) |
| 1404 | { |
| 1405 | const x509_buf *cur_oid = &cur->buf; |
| 1406 | |
| 1407 | if( cur_oid->len == usage_len && |
| 1408 | memcmp( cur_oid->p, usage_oid, usage_len ) == 0 ) |
| 1409 | { |
| 1410 | return( 0 ); |
| 1411 | } |
| 1412 | |
| 1413 | if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) ) |
| 1414 | return( 0 ); |
| 1415 | } |
| 1416 | |
| 1417 | return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); |
| 1418 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1419 | #endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 1420 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1421 | #if defined(POLARSSL_X509_CRL_PARSE_C) |
| 1422 | /* |
| 1423 | * Return 1 if the certificate is revoked, or 0 otherwise. |
| 1424 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1425 | int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1426 | { |
| 1427 | const x509_crl_entry *cur = &crl->entry; |
| 1428 | |
| 1429 | while( cur != NULL && cur->serial.len != 0 ) |
| 1430 | { |
| 1431 | if( crt->serial.len == cur->serial.len && |
| 1432 | memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 ) |
| 1433 | { |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 1434 | if( x509_time_expired( &cur->revocation_date ) ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1435 | return( 1 ); |
| 1436 | } |
| 1437 | |
| 1438 | cur = cur->next; |
| 1439 | } |
| 1440 | |
| 1441 | return( 0 ); |
| 1442 | } |
| 1443 | |
| 1444 | /* |
Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 1445 | * Check that the given certificate is valid according to the CRL. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1446 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1447 | static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca, |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1448 | x509_crl *crl_list) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1449 | { |
| 1450 | int flags = 0; |
| 1451 | unsigned char hash[POLARSSL_MD_MAX_SIZE]; |
| 1452 | const md_info_t *md_info; |
| 1453 | |
| 1454 | if( ca == NULL ) |
| 1455 | return( flags ); |
| 1456 | |
| 1457 | /* |
| 1458 | * TODO: What happens if no CRL is present? |
| 1459 | * Suggestion: Revocation state should be unknown if no CRL is present. |
| 1460 | * For backwards compatibility this is not yet implemented. |
| 1461 | */ |
| 1462 | |
| 1463 | while( crl_list != NULL ) |
| 1464 | { |
| 1465 | if( crl_list->version == 0 || |
| 1466 | crl_list->issuer_raw.len != ca->subject_raw.len || |
| 1467 | memcmp( crl_list->issuer_raw.p, ca->subject_raw.p, |
| 1468 | crl_list->issuer_raw.len ) != 0 ) |
| 1469 | { |
| 1470 | crl_list = crl_list->next; |
| 1471 | continue; |
| 1472 | } |
| 1473 | |
| 1474 | /* |
Manuel Pégourié-Gonnard | 99d4f19 | 2014-04-08 15:10:07 +0200 | [diff] [blame] | 1475 | * Check if the CA is configured to sign CRLs |
| 1476 | */ |
| 1477 | #if defined(POLARSSL_X509_CHECK_KEY_USAGE) |
| 1478 | if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 ) |
| 1479 | { |
| 1480 | flags |= BADCRL_NOT_TRUSTED; |
| 1481 | break; |
| 1482 | } |
| 1483 | #endif |
| 1484 | |
| 1485 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1486 | * Check if CRL is correctly signed by the trusted CA |
| 1487 | */ |
| 1488 | md_info = md_info_from_type( crl_list->sig_md ); |
| 1489 | if( md_info == NULL ) |
| 1490 | { |
| 1491 | /* |
| 1492 | * Cannot check 'unknown' hash |
| 1493 | */ |
| 1494 | flags |= BADCRL_NOT_TRUSTED; |
| 1495 | break; |
| 1496 | } |
| 1497 | |
| 1498 | md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ); |
| 1499 | |
Manuel Pégourié-Gonnard | 5388202 | 2014-06-05 17:53:52 +0200 | [diff] [blame] | 1500 | if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk, |
| 1501 | crl_list->sig_md, hash, md_info->size, |
| 1502 | crl_list->sig.p, crl_list->sig.len ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1503 | { |
| 1504 | flags |= BADCRL_NOT_TRUSTED; |
| 1505 | break; |
| 1506 | } |
| 1507 | |
| 1508 | /* |
| 1509 | * Check for validity of CRL (Do not drop out) |
| 1510 | */ |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 1511 | if( x509_time_expired( &crl_list->next_update ) ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1512 | flags |= BADCRL_EXPIRED; |
| 1513 | |
Manuel Pégourié-Gonnard | 9533765 | 2014-03-10 13:15:18 +0100 | [diff] [blame] | 1514 | if( x509_time_future( &crl_list->this_update ) ) |
| 1515 | flags |= BADCRL_FUTURE; |
| 1516 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1517 | /* |
| 1518 | * Check if certificate is revoked |
| 1519 | */ |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1520 | if( x509_crt_revoked(crt, crl_list) ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1521 | { |
| 1522 | flags |= BADCERT_REVOKED; |
| 1523 | break; |
| 1524 | } |
| 1525 | |
| 1526 | crl_list = crl_list->next; |
| 1527 | } |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame^] | 1528 | return( flags ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1529 | } |
| 1530 | #endif /* POLARSSL_X509_CRL_PARSE_C */ |
| 1531 | |
| 1532 | // Equal == 0, inequal == 1 |
| 1533 | static int x509_name_cmp( const void *s1, const void *s2, size_t len ) |
| 1534 | { |
| 1535 | size_t i; |
| 1536 | unsigned char diff; |
| 1537 | const unsigned char *n1 = s1, *n2 = s2; |
| 1538 | |
| 1539 | for( i = 0; i < len; i++ ) |
| 1540 | { |
| 1541 | diff = n1[i] ^ n2[i]; |
| 1542 | |
Paul Bakker | f2b4d86 | 2013-11-20 17:23:53 +0100 | [diff] [blame] | 1543 | if( diff == 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1544 | continue; |
| 1545 | |
Paul Bakker | f2b4d86 | 2013-11-20 17:23:53 +0100 | [diff] [blame] | 1546 | if( diff == 32 && |
| 1547 | ( ( n1[i] >= 'a' && n1[i] <= 'z' ) || |
| 1548 | ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) ) |
| 1549 | { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1550 | continue; |
Paul Bakker | f2b4d86 | 2013-11-20 17:23:53 +0100 | [diff] [blame] | 1551 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1552 | |
| 1553 | return( 1 ); |
| 1554 | } |
| 1555 | |
| 1556 | return( 0 ); |
| 1557 | } |
| 1558 | |
| 1559 | static int x509_wildcard_verify( const char *cn, x509_buf *name ) |
| 1560 | { |
| 1561 | size_t i; |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 1562 | size_t cn_idx = 0, cn_len = strlen( cn ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1563 | |
| 1564 | if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' ) |
| 1565 | return( 0 ); |
| 1566 | |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 1567 | for( i = 0; i < cn_len; ++i ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1568 | { |
| 1569 | if( cn[i] == '.' ) |
| 1570 | { |
| 1571 | cn_idx = i; |
| 1572 | break; |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | if( cn_idx == 0 ) |
| 1577 | return( 0 ); |
| 1578 | |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 1579 | if( cn_len - cn_idx == name->len - 1 && |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1580 | x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 ) |
| 1581 | { |
| 1582 | return( 1 ); |
| 1583 | } |
| 1584 | |
| 1585 | return( 0 ); |
| 1586 | } |
| 1587 | |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 1588 | /* |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 1589 | * Check if 'parent' is a suitable parent (signing CA) for 'child'. |
| 1590 | * Return 0 if yes, -1 if not. |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 1591 | */ |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 1592 | static int x509_crt_check_parent( const x509_crt *child, |
| 1593 | const x509_crt *parent ) |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 1594 | { |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 1595 | if( parent->version == 0 || |
| 1596 | parent->ca_istrue == 0 || |
| 1597 | child->issuer_raw.len != parent->subject_raw.len || |
| 1598 | memcmp( child->issuer_raw.p, parent->subject_raw.p, |
| 1599 | child->issuer_raw.len ) != 0 ) |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 1600 | { |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 1601 | return( -1 ); |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 1602 | } |
| 1603 | |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 1604 | #if defined(POLARSSL_X509_CHECK_KEY_USAGE) |
| 1605 | if( x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 ) |
| 1606 | return( -1 ); |
| 1607 | #endif |
| 1608 | |
| 1609 | return( 0 ); |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 1610 | } |
| 1611 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1612 | static int x509_crt_verify_top( |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1613 | x509_crt *child, x509_crt *trust_ca, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1614 | x509_crl *ca_crl, int path_cnt, int *flags, |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1615 | int (*f_vrfy)(void *, x509_crt *, int, int *), |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1616 | void *p_vrfy ) |
| 1617 | { |
| 1618 | int ret; |
| 1619 | int ca_flags = 0, check_path_cnt = path_cnt + 1; |
| 1620 | unsigned char hash[POLARSSL_MD_MAX_SIZE]; |
| 1621 | const md_info_t *md_info; |
| 1622 | |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 1623 | if( x509_time_expired( &child->valid_to ) ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1624 | *flags |= BADCERT_EXPIRED; |
| 1625 | |
Manuel Pégourié-Gonnard | 9533765 | 2014-03-10 13:15:18 +0100 | [diff] [blame] | 1626 | if( x509_time_future( &child->valid_from ) ) |
| 1627 | *flags |= BADCERT_FUTURE; |
| 1628 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1629 | /* |
| 1630 | * Child is the top of the chain. Check against the trust_ca list. |
| 1631 | */ |
| 1632 | *flags |= BADCERT_NOT_TRUSTED; |
| 1633 | |
| 1634 | md_info = md_info_from_type( child->sig_md ); |
| 1635 | if( md_info == NULL ) |
| 1636 | { |
| 1637 | /* |
| 1638 | * Cannot check 'unknown', no need to try any CA |
| 1639 | */ |
| 1640 | trust_ca = NULL; |
| 1641 | } |
| 1642 | else |
| 1643 | md( md_info, child->tbs.p, child->tbs.len, hash ); |
| 1644 | |
Manuel Pégourié-Gonnard | 490047c | 2014-04-09 14:30:45 +0200 | [diff] [blame] | 1645 | for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1646 | { |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 1647 | if( x509_crt_check_parent( child, trust_ca ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1648 | continue; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1649 | |
| 1650 | /* |
| 1651 | * Reduce path_len to check against if top of the chain is |
| 1652 | * the same as the trusted CA |
| 1653 | */ |
| 1654 | if( child->subject_raw.len == trust_ca->subject_raw.len && |
| 1655 | memcmp( child->subject_raw.p, trust_ca->subject_raw.p, |
| 1656 | child->issuer_raw.len ) == 0 ) |
| 1657 | { |
| 1658 | check_path_cnt--; |
| 1659 | } |
| 1660 | |
| 1661 | if( trust_ca->max_pathlen > 0 && |
| 1662 | trust_ca->max_pathlen < check_path_cnt ) |
| 1663 | { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1664 | continue; |
| 1665 | } |
| 1666 | |
Manuel Pégourié-Gonnard | 46db4b0 | 2014-06-05 16:34:18 +0200 | [diff] [blame] | 1667 | if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk, |
| 1668 | child->sig_md, hash, md_info->size, |
| 1669 | child->sig.p, child->sig.len ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1670 | { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1671 | continue; |
| 1672 | } |
| 1673 | |
| 1674 | /* |
| 1675 | * Top of chain is signed by a trusted CA |
| 1676 | */ |
| 1677 | *flags &= ~BADCERT_NOT_TRUSTED; |
| 1678 | break; |
| 1679 | } |
| 1680 | |
| 1681 | /* |
| 1682 | * If top of chain is not the same as the trusted CA send a verify request |
| 1683 | * to the callback for any issues with validity and CRL presence for the |
| 1684 | * trusted CA certificate. |
| 1685 | */ |
| 1686 | if( trust_ca != NULL && |
| 1687 | ( child->subject_raw.len != trust_ca->subject_raw.len || |
| 1688 | memcmp( child->subject_raw.p, trust_ca->subject_raw.p, |
| 1689 | child->issuer_raw.len ) != 0 ) ) |
| 1690 | { |
| 1691 | #if defined(POLARSSL_X509_CRL_PARSE_C) |
| 1692 | /* Check trusted CA's CRL for the chain's top crt */ |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1693 | *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl ); |
Manuel Pégourié-Gonnard | cbf3ef3 | 2013-09-23 12:20:02 +0200 | [diff] [blame] | 1694 | #else |
| 1695 | ((void) ca_crl); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1696 | #endif |
| 1697 | |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 1698 | if( x509_time_expired( &trust_ca->valid_to ) ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1699 | ca_flags |= BADCERT_EXPIRED; |
| 1700 | |
Manuel Pégourié-Gonnard | 9533765 | 2014-03-10 13:15:18 +0100 | [diff] [blame] | 1701 | if( x509_time_future( &trust_ca->valid_from ) ) |
| 1702 | ca_flags |= BADCERT_FUTURE; |
| 1703 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1704 | if( NULL != f_vrfy ) |
| 1705 | { |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1706 | if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, |
| 1707 | &ca_flags ) ) != 0 ) |
| 1708 | { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1709 | return( ret ); |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1710 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | /* Call callback on top cert */ |
| 1715 | if( NULL != f_vrfy ) |
| 1716 | { |
| 1717 | if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 ) |
| 1718 | return( ret ); |
| 1719 | } |
| 1720 | |
| 1721 | *flags |= ca_flags; |
| 1722 | |
| 1723 | return( 0 ); |
| 1724 | } |
| 1725 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1726 | static int x509_crt_verify_child( |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1727 | x509_crt *child, x509_crt *parent, x509_crt *trust_ca, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1728 | x509_crl *ca_crl, int path_cnt, int *flags, |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1729 | int (*f_vrfy)(void *, x509_crt *, int, int *), |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1730 | void *p_vrfy ) |
| 1731 | { |
| 1732 | int ret; |
| 1733 | int parent_flags = 0; |
| 1734 | unsigned char hash[POLARSSL_MD_MAX_SIZE]; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1735 | x509_crt *grandparent; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1736 | const md_info_t *md_info; |
| 1737 | |
Manuel Pégourié-Gonnard | 8c045ef | 2014-04-08 11:55:03 +0200 | [diff] [blame] | 1738 | if( x509_time_expired( &child->valid_to ) ) |
| 1739 | *flags |= BADCERT_EXPIRED; |
| 1740 | |
Manuel Pégourié-Gonnard | 9533765 | 2014-03-10 13:15:18 +0100 | [diff] [blame] | 1741 | if( x509_time_future( &child->valid_from ) ) |
| 1742 | *flags |= BADCERT_FUTURE; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1743 | |
| 1744 | md_info = md_info_from_type( child->sig_md ); |
| 1745 | if( md_info == NULL ) |
| 1746 | { |
| 1747 | /* |
| 1748 | * Cannot check 'unknown' hash |
| 1749 | */ |
| 1750 | *flags |= BADCERT_NOT_TRUSTED; |
| 1751 | } |
| 1752 | else |
| 1753 | { |
| 1754 | md( md_info, child->tbs.p, child->tbs.len, hash ); |
| 1755 | |
Manuel Pégourié-Gonnard | 46db4b0 | 2014-06-05 16:34:18 +0200 | [diff] [blame] | 1756 | if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk, |
| 1757 | child->sig_md, hash, md_info->size, |
| 1758 | child->sig.p, child->sig.len ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1759 | { |
| 1760 | *flags |= BADCERT_NOT_TRUSTED; |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | #if defined(POLARSSL_X509_CRL_PARSE_C) |
| 1765 | /* Check trusted CA's CRL for the given crt */ |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1766 | *flags |= x509_crt_verifycrl(child, parent, ca_crl); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1767 | #endif |
| 1768 | |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 1769 | /* Look for a grandparent upwards the chain */ |
| 1770 | for( grandparent = parent->next; |
| 1771 | grandparent != NULL; |
| 1772 | grandparent = grandparent->next ) |
| 1773 | { |
| 1774 | if( x509_crt_check_parent( parent, grandparent ) == 0 ) |
| 1775 | break; |
| 1776 | } |
| 1777 | |
| 1778 | /* Is our parent part of the chain or at the top? */ |
| 1779 | if( grandparent != NULL ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1780 | { |
Manuel Pégourié-Gonnard | 490047c | 2014-04-09 14:30:45 +0200 | [diff] [blame] | 1781 | ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl, |
| 1782 | path_cnt + 1, &parent_flags, f_vrfy, p_vrfy ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1783 | if( ret != 0 ) |
| 1784 | return( ret ); |
| 1785 | } |
| 1786 | else |
| 1787 | { |
Manuel Pégourié-Gonnard | 490047c | 2014-04-09 14:30:45 +0200 | [diff] [blame] | 1788 | ret = x509_crt_verify_top( parent, trust_ca, ca_crl, |
| 1789 | path_cnt + 1, &parent_flags, f_vrfy, p_vrfy ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1790 | if( ret != 0 ) |
| 1791 | return( ret ); |
| 1792 | } |
| 1793 | |
| 1794 | /* child is verified to be a child of the parent, call verify callback */ |
| 1795 | if( NULL != f_vrfy ) |
| 1796 | if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 ) |
| 1797 | return( ret ); |
| 1798 | |
| 1799 | *flags |= parent_flags; |
| 1800 | |
| 1801 | return( 0 ); |
| 1802 | } |
| 1803 | |
| 1804 | /* |
| 1805 | * Verify the certificate validity |
| 1806 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1807 | int x509_crt_verify( x509_crt *crt, |
| 1808 | x509_crt *trust_ca, |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1809 | x509_crl *ca_crl, |
| 1810 | const char *cn, int *flags, |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1811 | int (*f_vrfy)(void *, x509_crt *, int, int *), |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 1812 | void *p_vrfy ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1813 | { |
| 1814 | size_t cn_len; |
| 1815 | int ret; |
| 1816 | int pathlen = 0; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1817 | x509_crt *parent; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1818 | x509_name *name; |
| 1819 | x509_sequence *cur = NULL; |
| 1820 | |
| 1821 | *flags = 0; |
| 1822 | |
| 1823 | if( cn != NULL ) |
| 1824 | { |
| 1825 | name = &crt->subject; |
| 1826 | cn_len = strlen( cn ); |
| 1827 | |
| 1828 | if( crt->ext_types & EXT_SUBJECT_ALT_NAME ) |
| 1829 | { |
| 1830 | cur = &crt->subject_alt_names; |
| 1831 | |
| 1832 | while( cur != NULL ) |
| 1833 | { |
| 1834 | if( cur->buf.len == cn_len && |
| 1835 | x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 ) |
| 1836 | break; |
| 1837 | |
| 1838 | if( cur->buf.len > 2 && |
| 1839 | memcmp( cur->buf.p, "*.", 2 ) == 0 && |
| 1840 | x509_wildcard_verify( cn, &cur->buf ) ) |
| 1841 | break; |
| 1842 | |
| 1843 | cur = cur->next; |
| 1844 | } |
| 1845 | |
| 1846 | if( cur == NULL ) |
| 1847 | *flags |= BADCERT_CN_MISMATCH; |
| 1848 | } |
| 1849 | else |
| 1850 | { |
| 1851 | while( name != NULL ) |
| 1852 | { |
| 1853 | if( OID_CMP( OID_AT_CN, &name->oid ) ) |
| 1854 | { |
| 1855 | if( name->val.len == cn_len && |
| 1856 | x509_name_cmp( name->val.p, cn, cn_len ) == 0 ) |
| 1857 | break; |
| 1858 | |
| 1859 | if( name->val.len > 2 && |
| 1860 | memcmp( name->val.p, "*.", 2 ) == 0 && |
| 1861 | x509_wildcard_verify( cn, &name->val ) ) |
| 1862 | break; |
| 1863 | } |
| 1864 | |
| 1865 | name = name->next; |
| 1866 | } |
| 1867 | |
| 1868 | if( name == NULL ) |
| 1869 | *flags |= BADCERT_CN_MISMATCH; |
| 1870 | } |
| 1871 | } |
| 1872 | |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 1873 | /* Look for a parent upwards the chain */ |
| 1874 | for( parent = crt->next; parent != NULL; parent = parent->next ) |
| 1875 | { |
| 1876 | if( x509_crt_check_parent( crt, parent ) == 0 ) |
| 1877 | break; |
| 1878 | } |
| 1879 | |
| 1880 | /* Are we part of the chain or at the top? */ |
| 1881 | if( parent != NULL ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1882 | { |
Manuel Pégourié-Gonnard | 490047c | 2014-04-09 14:30:45 +0200 | [diff] [blame] | 1883 | ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, |
| 1884 | pathlen, flags, f_vrfy, p_vrfy ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1885 | if( ret != 0 ) |
| 1886 | return( ret ); |
| 1887 | } |
| 1888 | else |
| 1889 | { |
Manuel Pégourié-Gonnard | 490047c | 2014-04-09 14:30:45 +0200 | [diff] [blame] | 1890 | ret = x509_crt_verify_top( crt, trust_ca, ca_crl, |
| 1891 | pathlen, flags, f_vrfy, p_vrfy ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1892 | if( ret != 0 ) |
| 1893 | return( ret ); |
| 1894 | } |
| 1895 | |
| 1896 | if( *flags != 0 ) |
| 1897 | return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED ); |
| 1898 | |
| 1899 | return( 0 ); |
| 1900 | } |
| 1901 | |
| 1902 | /* |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 1903 | * Initialize a certificate chain |
| 1904 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1905 | void x509_crt_init( x509_crt *crt ) |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 1906 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1907 | memset( crt, 0, sizeof(x509_crt) ); |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1911 | * Unallocate all certificate data |
| 1912 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1913 | void x509_crt_free( x509_crt *crt ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1914 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 1915 | x509_crt *cert_cur = crt; |
| 1916 | x509_crt *cert_prv; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1917 | x509_name *name_cur; |
| 1918 | x509_name *name_prv; |
| 1919 | x509_sequence *seq_cur; |
| 1920 | x509_sequence *seq_prv; |
| 1921 | |
| 1922 | if( crt == NULL ) |
| 1923 | return; |
| 1924 | |
| 1925 | do |
| 1926 | { |
| 1927 | pk_free( &cert_cur->pk ); |
| 1928 | |
Manuel Pégourié-Gonnard | d1539b1 | 2014-06-06 16:42:37 +0200 | [diff] [blame] | 1929 | #if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT) |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 1930 | polarssl_free( cert_cur->sig_opts ); |
| 1931 | #endif |
| 1932 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1933 | name_cur = cert_cur->issuer.next; |
| 1934 | while( name_cur != NULL ) |
| 1935 | { |
| 1936 | name_prv = name_cur; |
| 1937 | name_cur = name_cur->next; |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 1938 | polarssl_zeroize( name_prv, sizeof( x509_name ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1939 | polarssl_free( name_prv ); |
| 1940 | } |
| 1941 | |
| 1942 | name_cur = cert_cur->subject.next; |
| 1943 | while( name_cur != NULL ) |
| 1944 | { |
| 1945 | name_prv = name_cur; |
| 1946 | name_cur = name_cur->next; |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 1947 | polarssl_zeroize( name_prv, sizeof( x509_name ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1948 | polarssl_free( name_prv ); |
| 1949 | } |
| 1950 | |
| 1951 | seq_cur = cert_cur->ext_key_usage.next; |
| 1952 | while( seq_cur != NULL ) |
| 1953 | { |
| 1954 | seq_prv = seq_cur; |
| 1955 | seq_cur = seq_cur->next; |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 1956 | polarssl_zeroize( seq_prv, sizeof( x509_sequence ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1957 | polarssl_free( seq_prv ); |
| 1958 | } |
| 1959 | |
| 1960 | seq_cur = cert_cur->subject_alt_names.next; |
| 1961 | while( seq_cur != NULL ) |
| 1962 | { |
| 1963 | seq_prv = seq_cur; |
| 1964 | seq_cur = seq_cur->next; |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 1965 | polarssl_zeroize( seq_prv, sizeof( x509_sequence ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1966 | polarssl_free( seq_prv ); |
| 1967 | } |
| 1968 | |
| 1969 | if( cert_cur->raw.p != NULL ) |
| 1970 | { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 1971 | polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1972 | polarssl_free( cert_cur->raw.p ); |
| 1973 | } |
| 1974 | |
| 1975 | cert_cur = cert_cur->next; |
| 1976 | } |
| 1977 | while( cert_cur != NULL ); |
| 1978 | |
| 1979 | cert_cur = crt; |
| 1980 | do |
| 1981 | { |
| 1982 | cert_prv = cert_cur; |
| 1983 | cert_cur = cert_cur->next; |
| 1984 | |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 1985 | polarssl_zeroize( cert_prv, sizeof( x509_crt ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1986 | if( cert_prv != crt ) |
| 1987 | polarssl_free( cert_prv ); |
| 1988 | } |
| 1989 | while( cert_cur != NULL ); |
| 1990 | } |
| 1991 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1992 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |