mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /*
ansond 0:137634ff4186 2 * X.509 Certidicate Revocation List (CRL) parsing
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 9 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 10 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 11 * (at your option) any later version.
ansond 0:137634ff4186 12 *
ansond 0:137634ff4186 13 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 16 * GNU General Public License for more details.
ansond 0:137634ff4186 17 *
ansond 0:137634ff4186 18 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 19 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 21 */
ansond 0:137634ff4186 22 /*
ansond 0:137634ff4186 23 * The ITU-T X.509 standard defines a certificate format for PKI.
ansond 0:137634ff4186 24 *
ansond 0:137634ff4186 25 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
ansond 0:137634ff4186 26 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
ansond 0:137634ff4186 27 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
ansond 0:137634ff4186 28 *
ansond 0:137634ff4186 29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
ansond 0:137634ff4186 30 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
ansond 0:137634ff4186 31 */
ansond 0:137634ff4186 32
ansond 0:137634ff4186 33 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 34 #include "polarssl/config.h"
ansond 0:137634ff4186 35 #else
ansond 0:137634ff4186 36 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 37 #endif
ansond 0:137634ff4186 38
ansond 0:137634ff4186 39 #if defined(POLARSSL_X509_CRL_PARSE_C)
ansond 0:137634ff4186 40
ansond 0:137634ff4186 41 #include "polarssl/x509_crl.h"
ansond 0:137634ff4186 42 #include "polarssl/oid.h"
ansond 0:137634ff4186 43
ansond 0:137634ff4186 44 #include <string.h>
ansond 0:137634ff4186 45
ansond 0:137634ff4186 46 #if defined(POLARSSL_PEM_PARSE_C)
ansond 0:137634ff4186 47 #include "polarssl/pem.h"
ansond 0:137634ff4186 48 #endif
ansond 0:137634ff4186 49
ansond 0:137634ff4186 50 #if defined(POLARSSL_PLATFORM_C)
ansond 0:137634ff4186 51 #include "polarssl/platform.h"
ansond 0:137634ff4186 52 #else
ansond 0:137634ff4186 53 #include <stdlib.h>
ansond 0:137634ff4186 54 #include <stdio.h>
ansond 0:137634ff4186 55 #define polarssl_free free
ansond 0:137634ff4186 56 #define polarssl_malloc malloc
ansond 0:137634ff4186 57 #define polarssl_snprintf snprintf
ansond 0:137634ff4186 58 #endif
ansond 0:137634ff4186 59
ansond 0:137634ff4186 60 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
ansond 0:137634ff4186 61 #include <windows.h>
ansond 0:137634ff4186 62 #else
ansond 0:137634ff4186 63 #include <time.h>
ansond 0:137634ff4186 64 #endif
ansond 0:137634ff4186 65
ansond 0:137634ff4186 66 #if defined(POLARSSL_FS_IO) || defined(EFIX64) || defined(EFI32)
ansond 0:137634ff4186 67 #include <stdio.h>
ansond 0:137634ff4186 68 #endif
ansond 0:137634ff4186 69
ansond 0:137634ff4186 70 /* Implementation that should never be optimized out by the compiler */
ansond 0:137634ff4186 71 static void polarssl_zeroize( void *v, size_t n ) {
ansond 0:137634ff4186 72 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
ansond 0:137634ff4186 73 }
ansond 0:137634ff4186 74
ansond 0:137634ff4186 75 /*
ansond 0:137634ff4186 76 * Version ::= INTEGER { v1(0), v2(1) }
ansond 0:137634ff4186 77 */
ansond 0:137634ff4186 78 static int x509_crl_get_version( unsigned char **p,
ansond 0:137634ff4186 79 const unsigned char *end,
ansond 0:137634ff4186 80 int *ver )
ansond 0:137634ff4186 81 {
ansond 0:137634ff4186 82 int ret;
ansond 0:137634ff4186 83
ansond 0:137634ff4186 84 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
ansond 0:137634ff4186 85 {
ansond 0:137634ff4186 86 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
ansond 0:137634ff4186 87 {
ansond 0:137634ff4186 88 *ver = 0;
ansond 0:137634ff4186 89 return( 0 );
ansond 0:137634ff4186 90 }
ansond 0:137634ff4186 91
ansond 0:137634ff4186 92 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
ansond 0:137634ff4186 93 }
ansond 0:137634ff4186 94
ansond 0:137634ff4186 95 return( 0 );
ansond 0:137634ff4186 96 }
ansond 0:137634ff4186 97
ansond 0:137634ff4186 98 /*
ansond 0:137634ff4186 99 * X.509 CRL v2 extensions (no extensions parsed yet.)
ansond 0:137634ff4186 100 */
ansond 0:137634ff4186 101 static int x509_get_crl_ext( unsigned char **p,
ansond 0:137634ff4186 102 const unsigned char *end,
ansond 0:137634ff4186 103 x509_buf *ext )
ansond 0:137634ff4186 104 {
ansond 0:137634ff4186 105 int ret;
ansond 0:137634ff4186 106 size_t len = 0;
ansond 0:137634ff4186 107
ansond 0:137634ff4186 108 /* Get explicit tag */
ansond 0:137634ff4186 109 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
ansond 0:137634ff4186 110 {
ansond 0:137634ff4186 111 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
ansond 0:137634ff4186 112 return( 0 );
ansond 0:137634ff4186 113
ansond 0:137634ff4186 114 return( ret );
ansond 0:137634ff4186 115 }
ansond 0:137634ff4186 116
ansond 0:137634ff4186 117 while( *p < end )
ansond 0:137634ff4186 118 {
ansond 0:137634ff4186 119 if( ( ret = asn1_get_tag( p, end, &len,
ansond 0:137634ff4186 120 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
ansond 0:137634ff4186 121 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
ansond 0:137634ff4186 122
ansond 0:137634ff4186 123 *p += len;
ansond 0:137634ff4186 124 }
ansond 0:137634ff4186 125
ansond 0:137634ff4186 126 if( *p != end )
ansond 0:137634ff4186 127 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
ansond 0:137634ff4186 128 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
ansond 0:137634ff4186 129
ansond 0:137634ff4186 130 return( 0 );
ansond 0:137634ff4186 131 }
ansond 0:137634ff4186 132
ansond 0:137634ff4186 133 /*
ansond 0:137634ff4186 134 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
ansond 0:137634ff4186 135 */
ansond 0:137634ff4186 136 static int x509_get_crl_entry_ext( unsigned char **p,
ansond 0:137634ff4186 137 const unsigned char *end,
ansond 0:137634ff4186 138 x509_buf *ext )
ansond 0:137634ff4186 139 {
ansond 0:137634ff4186 140 int ret;
ansond 0:137634ff4186 141 size_t len = 0;
ansond 0:137634ff4186 142
ansond 0:137634ff4186 143 /* OPTIONAL */
ansond 0:137634ff4186 144 if( end <= *p )
ansond 0:137634ff4186 145 return( 0 );
ansond 0:137634ff4186 146
ansond 0:137634ff4186 147 ext->tag = **p;
ansond 0:137634ff4186 148 ext->p = *p;
ansond 0:137634ff4186 149
ansond 0:137634ff4186 150 /*
ansond 0:137634ff4186 151 * Get CRL-entry extension sequence header
ansond 0:137634ff4186 152 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
ansond 0:137634ff4186 153 */
ansond 0:137634ff4186 154 if( ( ret = asn1_get_tag( p, end, &ext->len,
ansond 0:137634ff4186 155 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
ansond 0:137634ff4186 156 {
ansond 0:137634ff4186 157 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
ansond 0:137634ff4186 158 {
ansond 0:137634ff4186 159 ext->p = NULL;
ansond 0:137634ff4186 160 return( 0 );
ansond 0:137634ff4186 161 }
ansond 0:137634ff4186 162 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
ansond 0:137634ff4186 163 }
ansond 0:137634ff4186 164
ansond 0:137634ff4186 165 end = *p + ext->len;
ansond 0:137634ff4186 166
ansond 0:137634ff4186 167 if( end != *p + ext->len )
ansond 0:137634ff4186 168 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
ansond 0:137634ff4186 169 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
ansond 0:137634ff4186 170
ansond 0:137634ff4186 171 while( *p < end )
ansond 0:137634ff4186 172 {
ansond 0:137634ff4186 173 if( ( ret = asn1_get_tag( p, end, &len,
ansond 0:137634ff4186 174 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
ansond 0:137634ff4186 175 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
ansond 0:137634ff4186 176
ansond 0:137634ff4186 177 *p += len;
ansond 0:137634ff4186 178 }
ansond 0:137634ff4186 179
ansond 0:137634ff4186 180 if( *p != end )
ansond 0:137634ff4186 181 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
ansond 0:137634ff4186 182 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
ansond 0:137634ff4186 183
ansond 0:137634ff4186 184 return( 0 );
ansond 0:137634ff4186 185 }
ansond 0:137634ff4186 186
ansond 0:137634ff4186 187 /*
ansond 0:137634ff4186 188 * X.509 CRL Entries
ansond 0:137634ff4186 189 */
ansond 0:137634ff4186 190 static int x509_get_entries( unsigned char **p,
ansond 0:137634ff4186 191 const unsigned char *end,
ansond 0:137634ff4186 192 x509_crl_entry *entry )
ansond 0:137634ff4186 193 {
ansond 0:137634ff4186 194 int ret;
ansond 0:137634ff4186 195 size_t entry_len;
ansond 0:137634ff4186 196 x509_crl_entry *cur_entry = entry;
ansond 0:137634ff4186 197
ansond 0:137634ff4186 198 if( *p == end )
ansond 0:137634ff4186 199 return( 0 );
ansond 0:137634ff4186 200
ansond 0:137634ff4186 201 if( ( ret = asn1_get_tag( p, end, &entry_len,
ansond 0:137634ff4186 202 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
ansond 0:137634ff4186 203 {
ansond 0:137634ff4186 204 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
ansond 0:137634ff4186 205 return( 0 );
ansond 0:137634ff4186 206
ansond 0:137634ff4186 207 return( ret );
ansond 0:137634ff4186 208 }
ansond 0:137634ff4186 209
ansond 0:137634ff4186 210 end = *p + entry_len;
ansond 0:137634ff4186 211
ansond 0:137634ff4186 212 while( *p < end )
ansond 0:137634ff4186 213 {
ansond 0:137634ff4186 214 size_t len2;
ansond 0:137634ff4186 215 const unsigned char *end2;
ansond 0:137634ff4186 216
ansond 0:137634ff4186 217 if( ( ret = asn1_get_tag( p, end, &len2,
ansond 0:137634ff4186 218 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
ansond 0:137634ff4186 219 {
ansond 0:137634ff4186 220 return( ret );
ansond 0:137634ff4186 221 }
ansond 0:137634ff4186 222
ansond 0:137634ff4186 223 cur_entry->raw.tag = **p;
ansond 0:137634ff4186 224 cur_entry->raw.p = *p;
ansond 0:137634ff4186 225 cur_entry->raw.len = len2;
ansond 0:137634ff4186 226 end2 = *p + len2;
ansond 0:137634ff4186 227
ansond 0:137634ff4186 228 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
ansond 0:137634ff4186 229 return( ret );
ansond 0:137634ff4186 230
ansond 0:137634ff4186 231 if( ( ret = x509_get_time( p, end2,
ansond 0:137634ff4186 232 &cur_entry->revocation_date ) ) != 0 )
ansond 0:137634ff4186 233 return( ret );
ansond 0:137634ff4186 234
ansond 0:137634ff4186 235 if( ( ret = x509_get_crl_entry_ext( p, end2,
ansond 0:137634ff4186 236 &cur_entry->entry_ext ) ) != 0 )
ansond 0:137634ff4186 237 return( ret );
ansond 0:137634ff4186 238
ansond 0:137634ff4186 239 if( *p < end )
ansond 0:137634ff4186 240 {
ansond 0:137634ff4186 241 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
ansond 0:137634ff4186 242
ansond 0:137634ff4186 243 if( cur_entry->next == NULL )
ansond 0:137634ff4186 244 return( POLARSSL_ERR_X509_MALLOC_FAILED );
ansond 0:137634ff4186 245
ansond 0:137634ff4186 246 memset( cur_entry->next, 0, sizeof( x509_crl_entry ) );
ansond 0:137634ff4186 247 cur_entry = cur_entry->next;
ansond 0:137634ff4186 248 }
ansond 0:137634ff4186 249 }
ansond 0:137634ff4186 250
ansond 0:137634ff4186 251 return( 0 );
ansond 0:137634ff4186 252 }
ansond 0:137634ff4186 253
ansond 0:137634ff4186 254 /*
ansond 0:137634ff4186 255 * Parse one CRLs in DER format and append it to the chained list
ansond 0:137634ff4186 256 */
ansond 0:137634ff4186 257 int x509_crl_parse_der( x509_crl *chain,
ansond 0:137634ff4186 258 const unsigned char *buf, size_t buflen )
ansond 0:137634ff4186 259 {
ansond 0:137634ff4186 260 int ret;
ansond 0:137634ff4186 261 size_t len;
ansond 0:137634ff4186 262 unsigned char *p, *end;
ansond 0:137634ff4186 263 x509_buf sig_params1, sig_params2;
ansond 0:137634ff4186 264 x509_crl *crl = chain;
ansond 0:137634ff4186 265
ansond 0:137634ff4186 266 /*
ansond 0:137634ff4186 267 * Check for valid input
ansond 0:137634ff4186 268 */
ansond 0:137634ff4186 269 if( crl == NULL || buf == NULL )
ansond 0:137634ff4186 270 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
ansond 0:137634ff4186 271
ansond 0:137634ff4186 272 memset( &sig_params1, 0, sizeof( x509_buf ) );
ansond 0:137634ff4186 273 memset( &sig_params2, 0, sizeof( x509_buf ) );
ansond 0:137634ff4186 274
ansond 0:137634ff4186 275 /*
ansond 0:137634ff4186 276 * Add new CRL on the end of the chain if needed.
ansond 0:137634ff4186 277 */
ansond 0:137634ff4186 278 while( crl->version != 0 && crl->next != NULL )
ansond 0:137634ff4186 279 crl = crl->next;
ansond 0:137634ff4186 280
ansond 0:137634ff4186 281 if( crl->version != 0 && crl->next == NULL )
ansond 0:137634ff4186 282 {
ansond 0:137634ff4186 283 crl->next = polarssl_malloc( sizeof( x509_crl ) );
ansond 0:137634ff4186 284
ansond 0:137634ff4186 285 if( crl->next == NULL )
ansond 0:137634ff4186 286 {
ansond 0:137634ff4186 287 x509_crl_free( crl );
ansond 0:137634ff4186 288 return( POLARSSL_ERR_X509_MALLOC_FAILED );
ansond 0:137634ff4186 289 }
ansond 0:137634ff4186 290
ansond 0:137634ff4186 291 x509_crl_init( crl->next );
ansond 0:137634ff4186 292 crl = crl->next;
ansond 0:137634ff4186 293 }
ansond 0:137634ff4186 294
ansond 0:137634ff4186 295 /*
ansond 0:137634ff4186 296 * Copy raw DER-encoded CRL
ansond 0:137634ff4186 297 */
ansond 0:137634ff4186 298 if( ( p = polarssl_malloc( buflen ) ) == NULL )
ansond 0:137634ff4186 299 return( POLARSSL_ERR_X509_MALLOC_FAILED );
ansond 0:137634ff4186 300
ansond 0:137634ff4186 301 memcpy( p, buf, buflen );
ansond 0:137634ff4186 302
ansond 0:137634ff4186 303 crl->raw.p = p;
ansond 0:137634ff4186 304 crl->raw.len = buflen;
ansond 0:137634ff4186 305
ansond 0:137634ff4186 306 end = p + buflen;
ansond 0:137634ff4186 307
ansond 0:137634ff4186 308 /*
ansond 0:137634ff4186 309 * CertificateList ::= SEQUENCE {
ansond 0:137634ff4186 310 * tbsCertList TBSCertList,
ansond 0:137634ff4186 311 * signatureAlgorithm AlgorithmIdentifier,
ansond 0:137634ff4186 312 * signatureValue BIT STRING }
ansond 0:137634ff4186 313 */
ansond 0:137634ff4186 314 if( ( ret = asn1_get_tag( &p, end, &len,
ansond 0:137634ff4186 315 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
ansond 0:137634ff4186 316 {
ansond 0:137634ff4186 317 x509_crl_free( crl );
ansond 0:137634ff4186 318 return( POLARSSL_ERR_X509_INVALID_FORMAT );
ansond 0:137634ff4186 319 }
ansond 0:137634ff4186 320
ansond 0:137634ff4186 321 if( len != (size_t) ( end - p ) )
ansond 0:137634ff4186 322 {
ansond 0:137634ff4186 323 x509_crl_free( crl );
ansond 0:137634ff4186 324 return( POLARSSL_ERR_X509_INVALID_FORMAT +
ansond 0:137634ff4186 325 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
ansond 0:137634ff4186 326 }
ansond 0:137634ff4186 327
ansond 0:137634ff4186 328 /*
ansond 0:137634ff4186 329 * TBSCertList ::= SEQUENCE {
ansond 0:137634ff4186 330 */
ansond 0:137634ff4186 331 crl->tbs.p = p;
ansond 0:137634ff4186 332
ansond 0:137634ff4186 333 if( ( ret = asn1_get_tag( &p, end, &len,
ansond 0:137634ff4186 334 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
ansond 0:137634ff4186 335 {
ansond 0:137634ff4186 336 x509_crl_free( crl );
ansond 0:137634ff4186 337 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
ansond 0:137634ff4186 338 }
ansond 0:137634ff4186 339
ansond 0:137634ff4186 340 end = p + len;
ansond 0:137634ff4186 341 crl->tbs.len = end - crl->tbs.p;
ansond 0:137634ff4186 342
ansond 0:137634ff4186 343 /*
ansond 0:137634ff4186 344 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
ansond 0:137634ff4186 345 * -- if present, MUST be v2
ansond 0:137634ff4186 346 *
ansond 0:137634ff4186 347 * signature AlgorithmIdentifier
ansond 0:137634ff4186 348 */
ansond 0:137634ff4186 349 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
ansond 0:137634ff4186 350 ( ret = x509_get_alg( &p, end, &crl->sig_oid1, &sig_params1 ) ) != 0 )
ansond 0:137634ff4186 351 {
ansond 0:137634ff4186 352 x509_crl_free( crl );
ansond 0:137634ff4186 353 return( ret );
ansond 0:137634ff4186 354 }
ansond 0:137634ff4186 355
ansond 0:137634ff4186 356 crl->version++;
ansond 0:137634ff4186 357
ansond 0:137634ff4186 358 if( crl->version > 2 )
ansond 0:137634ff4186 359 {
ansond 0:137634ff4186 360 x509_crl_free( crl );
ansond 0:137634ff4186 361 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
ansond 0:137634ff4186 362 }
ansond 0:137634ff4186 363
ansond 0:137634ff4186 364 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &sig_params1,
ansond 0:137634ff4186 365 &crl->sig_md, &crl->sig_pk,
ansond 0:137634ff4186 366 &crl->sig_opts ) ) != 0 )
ansond 0:137634ff4186 367 {
ansond 0:137634ff4186 368 x509_crl_free( crl );
ansond 0:137634ff4186 369 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG );
ansond 0:137634ff4186 370 }
ansond 0:137634ff4186 371
ansond 0:137634ff4186 372 /*
ansond 0:137634ff4186 373 * issuer Name
ansond 0:137634ff4186 374 */
ansond 0:137634ff4186 375 crl->issuer_raw.p = p;
ansond 0:137634ff4186 376
ansond 0:137634ff4186 377 if( ( ret = asn1_get_tag( &p, end, &len,
ansond 0:137634ff4186 378 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
ansond 0:137634ff4186 379 {
ansond 0:137634ff4186 380 x509_crl_free( crl );
ansond 0:137634ff4186 381 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
ansond 0:137634ff4186 382 }
ansond 0:137634ff4186 383
ansond 0:137634ff4186 384 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
ansond 0:137634ff4186 385 {
ansond 0:137634ff4186 386 x509_crl_free( crl );
ansond 0:137634ff4186 387 return( ret );
ansond 0:137634ff4186 388 }
ansond 0:137634ff4186 389
ansond 0:137634ff4186 390 crl->issuer_raw.len = p - crl->issuer_raw.p;
ansond 0:137634ff4186 391
ansond 0:137634ff4186 392 /*
ansond 0:137634ff4186 393 * thisUpdate Time
ansond 0:137634ff4186 394 * nextUpdate Time OPTIONAL
ansond 0:137634ff4186 395 */
ansond 0:137634ff4186 396 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
ansond 0:137634ff4186 397 {
ansond 0:137634ff4186 398 x509_crl_free( crl );
ansond 0:137634ff4186 399 return( ret );
ansond 0:137634ff4186 400 }
ansond 0:137634ff4186 401
ansond 0:137634ff4186 402 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
ansond 0:137634ff4186 403 {
ansond 0:137634ff4186 404 if( ret != ( POLARSSL_ERR_X509_INVALID_DATE +
ansond 0:137634ff4186 405 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
ansond 0:137634ff4186 406 ret != ( POLARSSL_ERR_X509_INVALID_DATE +
ansond 0:137634ff4186 407 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
ansond 0:137634ff4186 408 {
ansond 0:137634ff4186 409 x509_crl_free( crl );
ansond 0:137634ff4186 410 return( ret );
ansond 0:137634ff4186 411 }
ansond 0:137634ff4186 412 }
ansond 0:137634ff4186 413
ansond 0:137634ff4186 414 /*
ansond 0:137634ff4186 415 * revokedCertificates SEQUENCE OF SEQUENCE {
ansond 0:137634ff4186 416 * userCertificate CertificateSerialNumber,
ansond 0:137634ff4186 417 * revocationDate Time,
ansond 0:137634ff4186 418 * crlEntryExtensions Extensions OPTIONAL
ansond 0:137634ff4186 419 * -- if present, MUST be v2
ansond 0:137634ff4186 420 * } OPTIONAL
ansond 0:137634ff4186 421 */
ansond 0:137634ff4186 422 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
ansond 0:137634ff4186 423 {
ansond 0:137634ff4186 424 x509_crl_free( crl );
ansond 0:137634ff4186 425 return( ret );
ansond 0:137634ff4186 426 }
ansond 0:137634ff4186 427
ansond 0:137634ff4186 428 /*
ansond 0:137634ff4186 429 * crlExtensions EXPLICIT Extensions OPTIONAL
ansond 0:137634ff4186 430 * -- if present, MUST be v2
ansond 0:137634ff4186 431 */
ansond 0:137634ff4186 432 if( crl->version == 2 )
ansond 0:137634ff4186 433 {
ansond 0:137634ff4186 434 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
ansond 0:137634ff4186 435
ansond 0:137634ff4186 436 if( ret != 0 )
ansond 0:137634ff4186 437 {
ansond 0:137634ff4186 438 x509_crl_free( crl );
ansond 0:137634ff4186 439 return( ret );
ansond 0:137634ff4186 440 }
ansond 0:137634ff4186 441 }
ansond 0:137634ff4186 442
ansond 0:137634ff4186 443 if( p != end )
ansond 0:137634ff4186 444 {
ansond 0:137634ff4186 445 x509_crl_free( crl );
ansond 0:137634ff4186 446 return( POLARSSL_ERR_X509_INVALID_FORMAT +
ansond 0:137634ff4186 447 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
ansond 0:137634ff4186 448 }
ansond 0:137634ff4186 449
ansond 0:137634ff4186 450 end = crl->raw.p + crl->raw.len;
ansond 0:137634ff4186 451
ansond 0:137634ff4186 452 /*
ansond 0:137634ff4186 453 * signatureAlgorithm AlgorithmIdentifier,
ansond 0:137634ff4186 454 * signatureValue BIT STRING
ansond 0:137634ff4186 455 */
ansond 0:137634ff4186 456 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2, &sig_params2 ) ) != 0 )
ansond 0:137634ff4186 457 {
ansond 0:137634ff4186 458 x509_crl_free( crl );
ansond 0:137634ff4186 459 return( ret );
ansond 0:137634ff4186 460 }
ansond 0:137634ff4186 461
ansond 0:137634ff4186 462 if( crl->sig_oid1.len != crl->sig_oid2.len ||
ansond 0:137634ff4186 463 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 ||
ansond 0:137634ff4186 464 sig_params1.len != sig_params2.len ||
ansond 0:137634ff4186 465 ( sig_params1.len != 0 &&
ansond 0:137634ff4186 466 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
ansond 0:137634ff4186 467 {
ansond 0:137634ff4186 468 x509_crl_free( crl );
ansond 0:137634ff4186 469 return( POLARSSL_ERR_X509_SIG_MISMATCH );
ansond 0:137634ff4186 470 }
ansond 0:137634ff4186 471
ansond 0:137634ff4186 472 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
ansond 0:137634ff4186 473 {
ansond 0:137634ff4186 474 x509_crl_free( crl );
ansond 0:137634ff4186 475 return( ret );
ansond 0:137634ff4186 476 }
ansond 0:137634ff4186 477
ansond 0:137634ff4186 478 if( p != end )
ansond 0:137634ff4186 479 {
ansond 0:137634ff4186 480 x509_crl_free( crl );
ansond 0:137634ff4186 481 return( POLARSSL_ERR_X509_INVALID_FORMAT +
ansond 0:137634ff4186 482 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
ansond 0:137634ff4186 483 }
ansond 0:137634ff4186 484
ansond 0:137634ff4186 485 return( 0 );
ansond 0:137634ff4186 486 }
ansond 0:137634ff4186 487
ansond 0:137634ff4186 488 /*
ansond 0:137634ff4186 489 * Parse one or more CRLs and add them to the chained list
ansond 0:137634ff4186 490 */
ansond 0:137634ff4186 491 int x509_crl_parse( x509_crl *chain, const unsigned char *buf, size_t buflen )
ansond 0:137634ff4186 492 {
ansond 0:137634ff4186 493 #if defined(POLARSSL_PEM_PARSE_C)
ansond 0:137634ff4186 494 int ret;
ansond 0:137634ff4186 495 size_t use_len;
ansond 0:137634ff4186 496 pem_context pem;
ansond 0:137634ff4186 497 int is_pem = 0;
ansond 0:137634ff4186 498
ansond 0:137634ff4186 499 if( chain == NULL || buf == NULL )
ansond 0:137634ff4186 500 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
ansond 0:137634ff4186 501
ansond 0:137634ff4186 502 do
ansond 0:137634ff4186 503 {
ansond 0:137634ff4186 504 pem_init( &pem );
ansond 0:137634ff4186 505 ret = pem_read_buffer( &pem,
ansond 0:137634ff4186 506 "-----BEGIN X509 CRL-----",
ansond 0:137634ff4186 507 "-----END X509 CRL-----",
ansond 0:137634ff4186 508 buf, NULL, 0, &use_len );
ansond 0:137634ff4186 509
ansond 0:137634ff4186 510 if( ret == 0 )
ansond 0:137634ff4186 511 {
ansond 0:137634ff4186 512 /*
ansond 0:137634ff4186 513 * Was PEM encoded
ansond 0:137634ff4186 514 */
ansond 0:137634ff4186 515 is_pem = 1;
ansond 0:137634ff4186 516
ansond 0:137634ff4186 517 buflen -= use_len;
ansond 0:137634ff4186 518 buf += use_len;
ansond 0:137634ff4186 519
ansond 0:137634ff4186 520 if( ( ret = x509_crl_parse_der( chain,
ansond 0:137634ff4186 521 pem.buf, pem.buflen ) ) != 0 )
ansond 0:137634ff4186 522 {
ansond 0:137634ff4186 523 return( ret );
ansond 0:137634ff4186 524 }
ansond 0:137634ff4186 525
ansond 0:137634ff4186 526 pem_free( &pem );
ansond 0:137634ff4186 527 }
ansond 0:137634ff4186 528 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
ansond 0:137634ff4186 529 {
ansond 0:137634ff4186 530 pem_free( &pem );
ansond 0:137634ff4186 531 return( ret );
ansond 0:137634ff4186 532 }
ansond 0:137634ff4186 533 }
ansond 0:137634ff4186 534 while( is_pem && buflen > 0 );
ansond 0:137634ff4186 535
ansond 0:137634ff4186 536 if( is_pem )
ansond 0:137634ff4186 537 return( 0 );
ansond 0:137634ff4186 538 else
ansond 0:137634ff4186 539 #endif /* POLARSSL_PEM_PARSE_C */
ansond 0:137634ff4186 540 return( x509_crl_parse_der( chain, buf, buflen ) );
ansond 0:137634ff4186 541 }
ansond 0:137634ff4186 542
ansond 0:137634ff4186 543 #if defined(POLARSSL_FS_IO)
ansond 0:137634ff4186 544 /*
ansond 0:137634ff4186 545 * Load one or more CRLs and add them to the chained list
ansond 0:137634ff4186 546 */
ansond 0:137634ff4186 547 int x509_crl_parse_file( x509_crl *chain, const char *path )
ansond 0:137634ff4186 548 {
ansond 0:137634ff4186 549 int ret;
ansond 0:137634ff4186 550 size_t n;
ansond 0:137634ff4186 551 unsigned char *buf;
ansond 0:137634ff4186 552
ansond 0:137634ff4186 553 if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 )
ansond 0:137634ff4186 554 return( ret );
ansond 0:137634ff4186 555
ansond 0:137634ff4186 556 ret = x509_crl_parse( chain, buf, n );
ansond 0:137634ff4186 557
ansond 0:137634ff4186 558 polarssl_zeroize( buf, n + 1 );
ansond 0:137634ff4186 559 polarssl_free( buf );
ansond 0:137634ff4186 560
ansond 0:137634ff4186 561 return( ret );
ansond 0:137634ff4186 562 }
ansond 0:137634ff4186 563 #endif /* POLARSSL_FS_IO */
ansond 0:137634ff4186 564
ansond 0:137634ff4186 565 #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
ansond 0:137634ff4186 566 !defined(EFI32)
ansond 0:137634ff4186 567 #include <stdarg.h>
ansond 0:137634ff4186 568
ansond 0:137634ff4186 569 #if !defined vsnprintf
ansond 0:137634ff4186 570 #define vsnprintf _vsnprintf
ansond 0:137634ff4186 571 #endif // vsnprintf
ansond 0:137634ff4186 572
ansond 0:137634ff4186 573 /*
ansond 0:137634ff4186 574 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
ansond 0:137634ff4186 575 * Result value is not size of buffer needed, but -1 if no fit is possible.
ansond 0:137634ff4186 576 *
ansond 0:137634ff4186 577 * This fuction tries to 'fix' this by at least suggesting enlarging the
ansond 0:137634ff4186 578 * size by 20.
ansond 0:137634ff4186 579 */
ansond 0:137634ff4186 580 static int compat_snprintf( char *str, size_t size, const char *format, ... )
ansond 0:137634ff4186 581 {
ansond 0:137634ff4186 582 va_list ap;
ansond 0:137634ff4186 583 int res = -1;
ansond 0:137634ff4186 584
ansond 0:137634ff4186 585 va_start( ap, format );
ansond 0:137634ff4186 586
ansond 0:137634ff4186 587 res = vsnprintf( str, size, format, ap );
ansond 0:137634ff4186 588
ansond 0:137634ff4186 589 va_end( ap );
ansond 0:137634ff4186 590
ansond 0:137634ff4186 591 // No quick fix possible
ansond 0:137634ff4186 592 if( res < 0 )
ansond 0:137634ff4186 593 return( (int) size + 20 );
ansond 0:137634ff4186 594
ansond 0:137634ff4186 595 return( res );
ansond 0:137634ff4186 596 }
ansond 0:137634ff4186 597
ansond 0:137634ff4186 598 #define snprintf compat_snprintf
ansond 0:137634ff4186 599 #endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
ansond 0:137634ff4186 600
ansond 0:137634ff4186 601 #define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
ansond 0:137634ff4186 602
ansond 0:137634ff4186 603 #define SAFE_SNPRINTF() \
ansond 0:137634ff4186 604 { \
ansond 0:137634ff4186 605 if( ret == -1 ) \
ansond 0:137634ff4186 606 return( -1 ); \
ansond 0:137634ff4186 607 \
ansond 0:137634ff4186 608 if( (unsigned int) ret > n ) { \
ansond 0:137634ff4186 609 p[n - 1] = '\0'; \
ansond 0:137634ff4186 610 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
ansond 0:137634ff4186 611 } \
ansond 0:137634ff4186 612 \
ansond 0:137634ff4186 613 n -= (unsigned int) ret; \
ansond 0:137634ff4186 614 p += (unsigned int) ret; \
ansond 0:137634ff4186 615 }
ansond 0:137634ff4186 616
ansond 0:137634ff4186 617 /*
ansond 0:137634ff4186 618 * Return an informational string about the certificate.
ansond 0:137634ff4186 619 */
ansond 0:137634ff4186 620 #define BEFORE_COLON 14
ansond 0:137634ff4186 621 #define BC "14"
ansond 0:137634ff4186 622 /*
ansond 0:137634ff4186 623 * Return an informational string about the CRL.
ansond 0:137634ff4186 624 */
ansond 0:137634ff4186 625 int x509_crl_info( char *buf, size_t size, const char *prefix,
ansond 0:137634ff4186 626 const x509_crl *crl )
ansond 0:137634ff4186 627 {
ansond 0:137634ff4186 628 int ret;
ansond 0:137634ff4186 629 size_t n;
ansond 0:137634ff4186 630 char *p;
ansond 0:137634ff4186 631 const x509_crl_entry *entry;
ansond 0:137634ff4186 632
ansond 0:137634ff4186 633 p = buf;
ansond 0:137634ff4186 634 n = size;
ansond 0:137634ff4186 635
ansond 0:137634ff4186 636 ret = polarssl_snprintf( p, n, "%sCRL version : %d",
ansond 0:137634ff4186 637 prefix, crl->version );
ansond 0:137634ff4186 638 SAFE_SNPRINTF();
ansond 0:137634ff4186 639
ansond 0:137634ff4186 640 ret = polarssl_snprintf( p, n, "\n%sissuer name : ", prefix );
ansond 0:137634ff4186 641 SAFE_SNPRINTF();
ansond 0:137634ff4186 642 ret = x509_dn_gets( p, n, &crl->issuer );
ansond 0:137634ff4186 643 SAFE_SNPRINTF();
ansond 0:137634ff4186 644
ansond 0:137634ff4186 645 ret = polarssl_snprintf( p, n, "\n%sthis update : " \
ansond 0:137634ff4186 646 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
ansond 0:137634ff4186 647 crl->this_update.year, crl->this_update.mon,
ansond 0:137634ff4186 648 crl->this_update.day, crl->this_update.hour,
ansond 0:137634ff4186 649 crl->this_update.min, crl->this_update.sec );
ansond 0:137634ff4186 650 SAFE_SNPRINTF();
ansond 0:137634ff4186 651
ansond 0:137634ff4186 652 ret = polarssl_snprintf( p, n, "\n%snext update : " \
ansond 0:137634ff4186 653 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
ansond 0:137634ff4186 654 crl->next_update.year, crl->next_update.mon,
ansond 0:137634ff4186 655 crl->next_update.day, crl->next_update.hour,
ansond 0:137634ff4186 656 crl->next_update.min, crl->next_update.sec );
ansond 0:137634ff4186 657 SAFE_SNPRINTF();
ansond 0:137634ff4186 658
ansond 0:137634ff4186 659 entry = &crl->entry;
ansond 0:137634ff4186 660
ansond 0:137634ff4186 661 ret = polarssl_snprintf( p, n, "\n%sRevoked certificates:",
ansond 0:137634ff4186 662 prefix );
ansond 0:137634ff4186 663 SAFE_SNPRINTF();
ansond 0:137634ff4186 664
ansond 0:137634ff4186 665 while( entry != NULL && entry->raw.len != 0 )
ansond 0:137634ff4186 666 {
ansond 0:137634ff4186 667 ret = polarssl_snprintf( p, n, "\n%sserial number: ",
ansond 0:137634ff4186 668 prefix );
ansond 0:137634ff4186 669 SAFE_SNPRINTF();
ansond 0:137634ff4186 670
ansond 0:137634ff4186 671 ret = x509_serial_gets( p, n, &entry->serial );
ansond 0:137634ff4186 672 SAFE_SNPRINTF();
ansond 0:137634ff4186 673
ansond 0:137634ff4186 674 ret = polarssl_snprintf( p, n, " revocation date: " \
ansond 0:137634ff4186 675 "%04d-%02d-%02d %02d:%02d:%02d",
ansond 0:137634ff4186 676 entry->revocation_date.year, entry->revocation_date.mon,
ansond 0:137634ff4186 677 entry->revocation_date.day, entry->revocation_date.hour,
ansond 0:137634ff4186 678 entry->revocation_date.min, entry->revocation_date.sec );
ansond 0:137634ff4186 679 SAFE_SNPRINTF();
ansond 0:137634ff4186 680
ansond 0:137634ff4186 681 entry = entry->next;
ansond 0:137634ff4186 682 }
ansond 0:137634ff4186 683
ansond 0:137634ff4186 684 ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix );
ansond 0:137634ff4186 685 SAFE_SNPRINTF();
ansond 0:137634ff4186 686
ansond 0:137634ff4186 687 ret = x509_sig_alg_gets( p, n, &crl->sig_oid1, crl->sig_pk, crl->sig_md,
ansond 0:137634ff4186 688 crl->sig_opts );
ansond 0:137634ff4186 689 SAFE_SNPRINTF();
ansond 0:137634ff4186 690
ansond 0:137634ff4186 691 ret = polarssl_snprintf( p, n, "\n" );
ansond 0:137634ff4186 692 SAFE_SNPRINTF();
ansond 0:137634ff4186 693
ansond 0:137634ff4186 694 return( (int) ( size - n ) );
ansond 0:137634ff4186 695 }
ansond 0:137634ff4186 696
ansond 0:137634ff4186 697 /*
ansond 0:137634ff4186 698 * Initialize a CRL chain
ansond 0:137634ff4186 699 */
ansond 0:137634ff4186 700 void x509_crl_init( x509_crl *crl )
ansond 0:137634ff4186 701 {
ansond 0:137634ff4186 702 memset( crl, 0, sizeof(x509_crl) );
ansond 0:137634ff4186 703 }
ansond 0:137634ff4186 704
ansond 0:137634ff4186 705 /*
ansond 0:137634ff4186 706 * Unallocate all CRL data
ansond 0:137634ff4186 707 */
ansond 0:137634ff4186 708 void x509_crl_free( x509_crl *crl )
ansond 0:137634ff4186 709 {
ansond 0:137634ff4186 710 x509_crl *crl_cur = crl;
ansond 0:137634ff4186 711 x509_crl *crl_prv;
ansond 0:137634ff4186 712 x509_name *name_cur;
ansond 0:137634ff4186 713 x509_name *name_prv;
ansond 0:137634ff4186 714 x509_crl_entry *entry_cur;
ansond 0:137634ff4186 715 x509_crl_entry *entry_prv;
ansond 0:137634ff4186 716
ansond 0:137634ff4186 717 if( crl == NULL )
ansond 0:137634ff4186 718 return;
ansond 0:137634ff4186 719
ansond 0:137634ff4186 720 do
ansond 0:137634ff4186 721 {
ansond 0:137634ff4186 722 #if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
ansond 0:137634ff4186 723 polarssl_free( crl_cur->sig_opts );
ansond 0:137634ff4186 724 #endif
ansond 0:137634ff4186 725
ansond 0:137634ff4186 726 name_cur = crl_cur->issuer.next;
ansond 0:137634ff4186 727 while( name_cur != NULL )
ansond 0:137634ff4186 728 {
ansond 0:137634ff4186 729 name_prv = name_cur;
ansond 0:137634ff4186 730 name_cur = name_cur->next;
ansond 0:137634ff4186 731 polarssl_zeroize( name_prv, sizeof( x509_name ) );
ansond 0:137634ff4186 732 polarssl_free( name_prv );
ansond 0:137634ff4186 733 }
ansond 0:137634ff4186 734
ansond 0:137634ff4186 735 entry_cur = crl_cur->entry.next;
ansond 0:137634ff4186 736 while( entry_cur != NULL )
ansond 0:137634ff4186 737 {
ansond 0:137634ff4186 738 entry_prv = entry_cur;
ansond 0:137634ff4186 739 entry_cur = entry_cur->next;
ansond 0:137634ff4186 740 polarssl_zeroize( entry_prv, sizeof( x509_crl_entry ) );
ansond 0:137634ff4186 741 polarssl_free( entry_prv );
ansond 0:137634ff4186 742 }
ansond 0:137634ff4186 743
ansond 0:137634ff4186 744 if( crl_cur->raw.p != NULL )
ansond 0:137634ff4186 745 {
ansond 0:137634ff4186 746 polarssl_zeroize( crl_cur->raw.p, crl_cur->raw.len );
ansond 0:137634ff4186 747 polarssl_free( crl_cur->raw.p );
ansond 0:137634ff4186 748 }
ansond 0:137634ff4186 749
ansond 0:137634ff4186 750 crl_cur = crl_cur->next;
ansond 0:137634ff4186 751 }
ansond 0:137634ff4186 752 while( crl_cur != NULL );
ansond 0:137634ff4186 753
ansond 0:137634ff4186 754 crl_cur = crl;
ansond 0:137634ff4186 755 do
ansond 0:137634ff4186 756 {
ansond 0:137634ff4186 757 crl_prv = crl_cur;
ansond 0:137634ff4186 758 crl_cur = crl_cur->next;
ansond 0:137634ff4186 759
ansond 0:137634ff4186 760 polarssl_zeroize( crl_prv, sizeof( x509_crl ) );
ansond 0:137634ff4186 761 if( crl_prv != crl )
ansond 0:137634ff4186 762 polarssl_free( crl_prv );
ansond 0:137634ff4186 763 }
ansond 0:137634ff4186 764 while( crl_cur != NULL );
ansond 0:137634ff4186 765 }
ansond 0:137634ff4186 766
ansond 0:137634ff4186 767 #endif /* POLARSSL_X509_CRL_PARSE_C */
ansond 0:137634ff4186 768