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