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