Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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