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; |
Manuel Pégourié-Gonnard | 5eeb32b | 2014-01-24 15:56:20 +0100 | [diff] [blame] | 253 | x509_buf sig_params; |
| 254 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 255 | #if defined(POLARSSL_PEM_PARSE_C) |
| 256 | size_t use_len; |
| 257 | pem_context pem; |
| 258 | #endif |
| 259 | |
Manuel Pégourié-Gonnard | 5eeb32b | 2014-01-24 15:56:20 +0100 | [diff] [blame] | 260 | memset( &sig_params, 0, sizeof( x509_buf ) ); |
| 261 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 262 | crl = chain; |
| 263 | |
| 264 | /* |
| 265 | * Check for valid input |
| 266 | */ |
| 267 | if( crl == NULL || buf == NULL ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 268 | return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 269 | |
| 270 | while( crl->version != 0 && crl->next != NULL ) |
| 271 | crl = crl->next; |
| 272 | |
| 273 | /* |
| 274 | * Add new CRL on the end of the chain if needed. |
| 275 | */ |
| 276 | if ( crl->version != 0 && crl->next == NULL) |
| 277 | { |
| 278 | crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) ); |
| 279 | |
| 280 | if( crl->next == NULL ) |
| 281 | { |
| 282 | x509_crl_free( crl ); |
| 283 | return( POLARSSL_ERR_X509_MALLOC_FAILED ); |
| 284 | } |
| 285 | |
| 286 | crl = crl->next; |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 287 | x509_crl_init( crl ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | #if defined(POLARSSL_PEM_PARSE_C) |
| 291 | pem_init( &pem ); |
| 292 | ret = pem_read_buffer( &pem, |
| 293 | "-----BEGIN X509 CRL-----", |
| 294 | "-----END X509 CRL-----", |
| 295 | buf, NULL, 0, &use_len ); |
| 296 | |
| 297 | if( ret == 0 ) |
| 298 | { |
| 299 | /* |
| 300 | * Was PEM encoded |
| 301 | */ |
| 302 | buflen -= use_len; |
| 303 | buf += use_len; |
| 304 | |
| 305 | /* |
| 306 | * Steal PEM buffer |
| 307 | */ |
| 308 | p = pem.buf; |
| 309 | pem.buf = NULL; |
| 310 | len = pem.buflen; |
| 311 | pem_free( &pem ); |
| 312 | } |
| 313 | else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
| 314 | { |
| 315 | pem_free( &pem ); |
| 316 | return( ret ); |
| 317 | } |
| 318 | else |
| 319 | #endif |
| 320 | { |
| 321 | /* |
| 322 | * nope, copy the raw DER data |
| 323 | */ |
| 324 | p = (unsigned char *) polarssl_malloc( len = buflen ); |
| 325 | |
| 326 | if( p == NULL ) |
| 327 | return( POLARSSL_ERR_X509_MALLOC_FAILED ); |
| 328 | |
| 329 | memcpy( p, buf, buflen ); |
| 330 | |
| 331 | buflen = 0; |
| 332 | } |
| 333 | |
| 334 | crl->raw.p = p; |
| 335 | crl->raw.len = len; |
| 336 | end = p + len; |
| 337 | |
| 338 | /* |
| 339 | * CertificateList ::= SEQUENCE { |
| 340 | * tbsCertList TBSCertList, |
| 341 | * signatureAlgorithm AlgorithmIdentifier, |
| 342 | * signatureValue BIT STRING } |
| 343 | */ |
| 344 | if( ( ret = asn1_get_tag( &p, end, &len, |
| 345 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
| 346 | { |
| 347 | x509_crl_free( crl ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 348 | return( POLARSSL_ERR_X509_INVALID_FORMAT ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | if( len != (size_t) ( end - p ) ) |
| 352 | { |
| 353 | x509_crl_free( crl ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 354 | return( POLARSSL_ERR_X509_INVALID_FORMAT + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 355 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * TBSCertList ::= SEQUENCE { |
| 360 | */ |
| 361 | crl->tbs.p = p; |
| 362 | |
| 363 | if( ( ret = asn1_get_tag( &p, end, &len, |
| 364 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
| 365 | { |
| 366 | x509_crl_free( crl ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 367 | return( POLARSSL_ERR_X509_INVALID_FORMAT + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | end = p + len; |
| 371 | crl->tbs.len = end - crl->tbs.p; |
| 372 | |
| 373 | /* |
| 374 | * Version ::= INTEGER OPTIONAL { v1(0), v2(1) } |
| 375 | * -- if present, MUST be v2 |
| 376 | * |
| 377 | * signature AlgorithmIdentifier |
| 378 | */ |
| 379 | if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 || |
Manuel Pégourié-Gonnard | 5eeb32b | 2014-01-24 15:56:20 +0100 | [diff] [blame] | 380 | ( ret = x509_get_alg( &p, end, &crl->sig_oid1, &sig_params ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 381 | { |
| 382 | x509_crl_free( crl ); |
| 383 | return( ret ); |
| 384 | } |
| 385 | |
| 386 | crl->version++; |
| 387 | |
| 388 | if( crl->version > 2 ) |
| 389 | { |
| 390 | x509_crl_free( crl ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 391 | return( POLARSSL_ERR_X509_UNKNOWN_VERSION ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Manuel Pégourié-Gonnard | 5cac583 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 394 | if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &sig_params, |
| 395 | &crl->sig_md, &crl->sig_pk ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 396 | { |
| 397 | x509_crl_free( crl ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 398 | return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Manuel Pégourié-Gonnard | 5eeb32b | 2014-01-24 15:56:20 +0100 | [diff] [blame] | 401 | #if defined(POLARSSL_RSASSA_PSS_CERTIFICATES) |
Manuel Pégourié-Gonnard | 5cac583 | 2014-01-24 19:28:43 +0100 | [diff] [blame] | 402 | memcpy( &crl->sig_params, &sig_params, sizeof( x509_buf ) ); |
Manuel Pégourié-Gonnard | 5eeb32b | 2014-01-24 15:56:20 +0100 | [diff] [blame] | 403 | #endif |
Manuel Pégourié-Gonnard | 5eeb32b | 2014-01-24 15:56:20 +0100 | [diff] [blame] | 404 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 405 | /* |
| 406 | * issuer Name |
| 407 | */ |
| 408 | crl->issuer_raw.p = p; |
| 409 | |
| 410 | if( ( ret = asn1_get_tag( &p, end, &len, |
| 411 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
| 412 | { |
| 413 | x509_crl_free( crl ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 414 | return( POLARSSL_ERR_X509_INVALID_FORMAT + ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 ) |
| 418 | { |
| 419 | x509_crl_free( crl ); |
| 420 | return( ret ); |
| 421 | } |
| 422 | |
| 423 | crl->issuer_raw.len = p - crl->issuer_raw.p; |
| 424 | |
| 425 | /* |
| 426 | * thisUpdate Time |
| 427 | * nextUpdate Time OPTIONAL |
| 428 | */ |
| 429 | if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 ) |
| 430 | { |
| 431 | x509_crl_free( crl ); |
| 432 | return( ret ); |
| 433 | } |
| 434 | |
| 435 | if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 ) |
| 436 | { |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 437 | if ( ret != ( POLARSSL_ERR_X509_INVALID_DATE + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 438 | POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) && |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 439 | ret != ( POLARSSL_ERR_X509_INVALID_DATE + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 440 | POLARSSL_ERR_ASN1_OUT_OF_DATA ) ) |
| 441 | { |
| 442 | x509_crl_free( crl ); |
| 443 | return( ret ); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * revokedCertificates SEQUENCE OF SEQUENCE { |
| 449 | * userCertificate CertificateSerialNumber, |
| 450 | * revocationDate Time, |
| 451 | * crlEntryExtensions Extensions OPTIONAL |
| 452 | * -- if present, MUST be v2 |
| 453 | * } OPTIONAL |
| 454 | */ |
| 455 | if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 ) |
| 456 | { |
| 457 | x509_crl_free( crl ); |
| 458 | return( ret ); |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * crlExtensions EXPLICIT Extensions OPTIONAL |
| 463 | * -- if present, MUST be v2 |
| 464 | */ |
| 465 | if( crl->version == 2 ) |
| 466 | { |
| 467 | ret = x509_get_crl_ext( &p, end, &crl->crl_ext ); |
| 468 | |
| 469 | if( ret != 0 ) |
| 470 | { |
| 471 | x509_crl_free( crl ); |
| 472 | return( ret ); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if( p != end ) |
| 477 | { |
| 478 | x509_crl_free( crl ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 479 | return( POLARSSL_ERR_X509_INVALID_FORMAT + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 480 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 481 | } |
| 482 | |
| 483 | end = crl->raw.p + crl->raw.len; |
| 484 | |
| 485 | /* |
| 486 | * signatureAlgorithm AlgorithmIdentifier, |
| 487 | * signatureValue BIT STRING |
| 488 | */ |
Manuel Pégourié-Gonnard | 5eeb32b | 2014-01-24 15:56:20 +0100 | [diff] [blame] | 489 | if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2, &sig_params ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 490 | { |
| 491 | x509_crl_free( crl ); |
| 492 | return( ret ); |
| 493 | } |
| 494 | |
| 495 | if( crl->sig_oid1.len != crl->sig_oid2.len || |
Manuel Pégourié-Gonnard | 5eeb32b | 2014-01-24 15:56:20 +0100 | [diff] [blame] | 496 | memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 |
| 497 | #if defined(POLARSSL_RSASSA_PSS_CERTIFICATES) |
| 498 | || |
| 499 | crl->sig_params.len != sig_params.len || |
| 500 | memcmp( crl->sig_params.p, sig_params.p, sig_params.len ) != 0 |
| 501 | #endif |
| 502 | ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 503 | { |
| 504 | x509_crl_free( crl ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 505 | return( POLARSSL_ERR_X509_SIG_MISMATCH ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 ) |
| 509 | { |
| 510 | x509_crl_free( crl ); |
| 511 | return( ret ); |
| 512 | } |
| 513 | |
| 514 | if( p != end ) |
| 515 | { |
| 516 | x509_crl_free( crl ); |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 517 | return( POLARSSL_ERR_X509_INVALID_FORMAT + |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 518 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 519 | } |
| 520 | |
| 521 | if( buflen > 0 ) |
| 522 | { |
| 523 | crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) ); |
| 524 | |
| 525 | if( crl->next == NULL ) |
| 526 | { |
| 527 | x509_crl_free( crl ); |
| 528 | return( POLARSSL_ERR_X509_MALLOC_FAILED ); |
| 529 | } |
| 530 | |
| 531 | crl = crl->next; |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 532 | x509_crl_init( crl ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 533 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 534 | return( x509_crl_parse( crl, buf, buflen ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | return( 0 ); |
| 538 | } |
| 539 | |
| 540 | #if defined(POLARSSL_FS_IO) |
| 541 | /* |
| 542 | * Load one or more CRLs and add them to the chained list |
| 543 | */ |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 544 | int x509_crl_parse_file( x509_crl *chain, const char *path ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 545 | { |
| 546 | int ret; |
| 547 | size_t n; |
| 548 | unsigned char *buf; |
| 549 | |
| 550 | if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 ) |
| 551 | return( ret ); |
| 552 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 553 | ret = x509_crl_parse( chain, buf, n ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 554 | |
| 555 | memset( buf, 0, n + 1 ); |
| 556 | polarssl_free( buf ); |
| 557 | |
| 558 | return( ret ); |
| 559 | } |
| 560 | #endif /* POLARSSL_FS_IO */ |
| 561 | |
Paul Bakker | 6edcd41 | 2013-10-29 15:22:54 +0100 | [diff] [blame] | 562 | #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \ |
| 563 | !defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 564 | #include <stdarg.h> |
| 565 | |
| 566 | #if !defined vsnprintf |
| 567 | #define vsnprintf _vsnprintf |
| 568 | #endif // vsnprintf |
| 569 | |
| 570 | /* |
| 571 | * Windows _snprintf and _vsnprintf are not compatible to linux versions. |
| 572 | * Result value is not size of buffer needed, but -1 if no fit is possible. |
| 573 | * |
| 574 | * This fuction tries to 'fix' this by at least suggesting enlarging the |
| 575 | * size by 20. |
| 576 | */ |
| 577 | static int compat_snprintf(char *str, size_t size, const char *format, ...) |
| 578 | { |
| 579 | va_list ap; |
| 580 | int res = -1; |
| 581 | |
| 582 | va_start( ap, format ); |
| 583 | |
| 584 | res = vsnprintf( str, size, format, ap ); |
| 585 | |
| 586 | va_end( ap ); |
| 587 | |
| 588 | // No quick fix possible |
| 589 | if ( res < 0 ) |
| 590 | return( (int) size + 20 ); |
| 591 | |
| 592 | return res; |
| 593 | } |
| 594 | |
| 595 | #define snprintf compat_snprintf |
| 596 | #endif |
| 597 | |
| 598 | #define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2 |
| 599 | |
| 600 | #define SAFE_SNPRINTF() \ |
| 601 | { \ |
| 602 | if( ret == -1 ) \ |
| 603 | return( -1 ); \ |
| 604 | \ |
| 605 | if ( (unsigned int) ret > n ) { \ |
| 606 | p[n - 1] = '\0'; \ |
| 607 | return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\ |
| 608 | } \ |
| 609 | \ |
| 610 | n -= (unsigned int) ret; \ |
| 611 | p += (unsigned int) ret; \ |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * Return an informational string about the certificate. |
| 616 | */ |
| 617 | #define BEFORE_COLON 14 |
| 618 | #define BC "14" |
| 619 | /* |
| 620 | * Return an informational string about the CRL. |
| 621 | */ |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 622 | int x509_crl_info( char *buf, size_t size, const char *prefix, |
| 623 | const x509_crl *crl ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 624 | { |
| 625 | int ret; |
| 626 | size_t n; |
| 627 | char *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 628 | const x509_crl_entry *entry; |
Manuel Pégourié-Gonnard | 27b93ad | 2014-01-25 11:50:59 +0100 | [diff] [blame^] | 629 | #if defined(POLARSSL_RSASSA_PSS_CERTIFICATES) |
| 630 | const x509_buf *sig_params = &crl->sig_params; |
| 631 | #else |
| 632 | const x509_buf *sig_params = NULL; |
| 633 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 634 | |
| 635 | p = buf; |
| 636 | n = size; |
| 637 | |
| 638 | ret = snprintf( p, n, "%sCRL version : %d", |
| 639 | prefix, crl->version ); |
| 640 | SAFE_SNPRINTF(); |
| 641 | |
| 642 | ret = snprintf( p, n, "\n%sissuer name : ", prefix ); |
| 643 | SAFE_SNPRINTF(); |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 644 | ret = x509_dn_gets( p, n, &crl->issuer ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 645 | SAFE_SNPRINTF(); |
| 646 | |
| 647 | ret = snprintf( p, n, "\n%sthis update : " \ |
| 648 | "%04d-%02d-%02d %02d:%02d:%02d", prefix, |
| 649 | crl->this_update.year, crl->this_update.mon, |
| 650 | crl->this_update.day, crl->this_update.hour, |
| 651 | crl->this_update.min, crl->this_update.sec ); |
| 652 | SAFE_SNPRINTF(); |
| 653 | |
| 654 | ret = snprintf( p, n, "\n%snext update : " \ |
| 655 | "%04d-%02d-%02d %02d:%02d:%02d", prefix, |
| 656 | crl->next_update.year, crl->next_update.mon, |
| 657 | crl->next_update.day, crl->next_update.hour, |
| 658 | crl->next_update.min, crl->next_update.sec ); |
| 659 | SAFE_SNPRINTF(); |
| 660 | |
| 661 | entry = &crl->entry; |
| 662 | |
| 663 | ret = snprintf( p, n, "\n%sRevoked certificates:", |
| 664 | prefix ); |
| 665 | SAFE_SNPRINTF(); |
| 666 | |
| 667 | while( entry != NULL && entry->raw.len != 0 ) |
| 668 | { |
| 669 | ret = snprintf( p, n, "\n%sserial number: ", |
| 670 | prefix ); |
| 671 | SAFE_SNPRINTF(); |
| 672 | |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 673 | ret = x509_serial_gets( p, n, &entry->serial); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 674 | SAFE_SNPRINTF(); |
| 675 | |
| 676 | ret = snprintf( p, n, " revocation date: " \ |
| 677 | "%04d-%02d-%02d %02d:%02d:%02d", |
| 678 | entry->revocation_date.year, entry->revocation_date.mon, |
| 679 | entry->revocation_date.day, entry->revocation_date.hour, |
| 680 | entry->revocation_date.min, entry->revocation_date.sec ); |
| 681 | SAFE_SNPRINTF(); |
| 682 | |
| 683 | entry = entry->next; |
| 684 | } |
| 685 | |
| 686 | ret = snprintf( p, n, "\n%ssigned using : ", prefix ); |
| 687 | SAFE_SNPRINTF(); |
| 688 | |
Manuel Pégourié-Gonnard | 27b93ad | 2014-01-25 11:50:59 +0100 | [diff] [blame^] | 689 | ret = x509_sig_alg_gets( p, n, &crl->sig_oid1, crl->sig_pk, sig_params ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 690 | SAFE_SNPRINTF(); |
| 691 | |
| 692 | ret = snprintf( p, n, "\n" ); |
| 693 | SAFE_SNPRINTF(); |
| 694 | |
| 695 | return( (int) ( size - n ) ); |
| 696 | } |
| 697 | |
| 698 | /* |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 699 | * Initialize a CRL chain |
| 700 | */ |
| 701 | void x509_crl_init( x509_crl *crl ) |
| 702 | { |
| 703 | memset( crl, 0, sizeof(x509_crl) ); |
| 704 | } |
| 705 | |
| 706 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 707 | * Unallocate all CRL data |
| 708 | */ |
| 709 | void x509_crl_free( x509_crl *crl ) |
| 710 | { |
| 711 | x509_crl *crl_cur = crl; |
| 712 | x509_crl *crl_prv; |
| 713 | x509_name *name_cur; |
| 714 | x509_name *name_prv; |
| 715 | x509_crl_entry *entry_cur; |
| 716 | x509_crl_entry *entry_prv; |
| 717 | |
| 718 | if( crl == NULL ) |
| 719 | return; |
| 720 | |
| 721 | do |
| 722 | { |
| 723 | name_cur = crl_cur->issuer.next; |
| 724 | while( name_cur != NULL ) |
| 725 | { |
| 726 | name_prv = name_cur; |
| 727 | name_cur = name_cur->next; |
| 728 | memset( name_prv, 0, sizeof( x509_name ) ); |
| 729 | polarssl_free( name_prv ); |
| 730 | } |
| 731 | |
| 732 | entry_cur = crl_cur->entry.next; |
| 733 | while( entry_cur != NULL ) |
| 734 | { |
| 735 | entry_prv = entry_cur; |
| 736 | entry_cur = entry_cur->next; |
| 737 | memset( entry_prv, 0, sizeof( x509_crl_entry ) ); |
| 738 | polarssl_free( entry_prv ); |
| 739 | } |
| 740 | |
| 741 | if( crl_cur->raw.p != NULL ) |
| 742 | { |
| 743 | memset( crl_cur->raw.p, 0, crl_cur->raw.len ); |
| 744 | polarssl_free( crl_cur->raw.p ); |
| 745 | } |
| 746 | |
| 747 | crl_cur = crl_cur->next; |
| 748 | } |
| 749 | while( crl_cur != NULL ); |
| 750 | |
| 751 | crl_cur = crl; |
| 752 | do |
| 753 | { |
| 754 | crl_prv = crl_cur; |
| 755 | crl_cur = crl_cur->next; |
| 756 | |
| 757 | memset( crl_prv, 0, sizeof( x509_crl ) ); |
| 758 | if( crl_prv != crl ) |
| 759 | polarssl_free( crl_prv ); |
| 760 | } |
| 761 | while( crl_cur != NULL ); |
| 762 | } |
| 763 | |
| 764 | #endif |