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