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