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