| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/asn1.h> |
| 5 |
#include <openssl/asn1t.h> |
| 6 |
#include <openssl/digest.h> |
| 7 |
#include <openssl/err.h> |
| 8 |
#include <openssl/mem.h> |
| 9 |
#include <openssl/obj.h> |
| 10 |
#include <openssl/stack.h> |
| 11 |
#include <openssl/thread.h> |
| 12 |
#include <openssl/x509.h> |
| 13 |
#include <openssl/x509v3.h> |
| 14 |
|
| 15 |
#include <assert.h> |
| 16 |
|
| 17 |
#include "../asn1/internal.h" |
| 18 |
#include "../internal.h" |
| 19 |
#include "internal.h" |
| 20 |
|
| 21 |
static int X509_REVOKED_cmp(const X509_REVOKED *const *a, |
| 22 |
const X509_REVOKED *const *b); |
| 23 |
static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp); |
| 24 |
|
| 25 |
ASN1_SEQUENCE(X509_REVOKED) = { |
| 26 |
ASN1_SIMPLE(X509_REVOKED, serialNumber, ASN1_INTEGER), |
| 27 |
ASN1_SIMPLE(X509_REVOKED, revocationDate, ASN1_TIME), |
| 28 |
ASN1_SEQUENCE_OF_OPT(X509_REVOKED, extensions, X509_EXTENSION), |
| 29 |
} ASN1_SEQUENCE_END(X509_REVOKED) |
| 30 |
|
| 31 |
static int crl_lookup(X509_CRL *crl, X509_REVOKED **ret, |
| 32 |
const ASN1_INTEGER *serial, X509_NAME *issuer); |
| 33 |
|
| 34 |
// The X509_CRL_INFO structure needs a bit of customisation. Since we cache |
| 35 |
// the original encoding the signature wont be affected by reordering of the |
| 36 |
// revoked field. |
| 37 |
static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 38 |
void *exarg) { |
| 39 |
X509_CRL_INFO *a = (X509_CRL_INFO *)*pval; |
| 40 |
|
| 41 |
if (!a || !a->revoked) { |
| 42 |
return 1; |
| 43 |
} |
| 44 |
switch (operation) { |
| 45 |
// Just set cmp function here. We don't sort because that would |
| 46 |
// affect the output of X509_CRL_print(). |
| 47 |
case ASN1_OP_D2I_POST: |
| 48 |
(void)sk_X509_REVOKED_set_cmp_func(a->revoked, X509_REVOKED_cmp); |
| 49 |
break; |
| 50 |
} |
| 51 |
return 1; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = { |
| 56 |
ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER), |
| 57 |
ASN1_SIMPLE(X509_CRL_INFO, sig_alg, X509_ALGOR), |
| 58 |
ASN1_SIMPLE(X509_CRL_INFO, issuer, X509_NAME), |
| 59 |
ASN1_SIMPLE(X509_CRL_INFO, lastUpdate, ASN1_TIME), |
| 60 |
ASN1_OPT(X509_CRL_INFO, nextUpdate, ASN1_TIME), |
| 61 |
ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO, revoked, X509_REVOKED), |
| 62 |
ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO, extensions, X509_EXTENSION, 0), |
| 63 |
} ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO) |
| 64 |
|
| 65 |
static int crl_parse_entry_extensions(X509_CRL *crl) { |
| 66 |
STACK_OF(X509_REVOKED) *revoked = X509_CRL_get_REVOKED(crl); |
| 67 |
for (size_t i = 0; i < sk_X509_REVOKED_num(revoked); i++) { |
| 68 |
X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i); |
| 69 |
|
| 70 |
int crit; |
| 71 |
ASN1_ENUMERATED *reason = |
| 72 |
X509_REVOKED_get_ext_d2i(rev, NID_crl_reason, &crit, NULL); |
| 73 |
if (!reason && crit != -1) { |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
if (reason) { |
| 78 |
rev->reason = ASN1_ENUMERATED_get(reason); |
| 79 |
ASN1_ENUMERATED_free(reason); |
| 80 |
} else { |
| 81 |
rev->reason = CRL_REASON_NONE; |
| 82 |
} |
| 83 |
|
| 84 |
// We do not support any critical CRL entry extensions. |
| 85 |
const STACK_OF(X509_EXTENSION) *exts = rev->extensions; |
| 86 |
for (size_t j = 0; j < sk_X509_EXTENSION_num(exts); j++) { |
| 87 |
const X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, j); |
| 88 |
if (X509_EXTENSION_get_critical(ext)) { |
| 89 |
crl->flags |= EXFLAG_CRITICAL; |
| 90 |
break; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return 1; |
| 96 |
} |
| 97 |
|
| 98 |
// The X509_CRL structure needs a bit of customisation. Cache some extensions |
| 99 |
// and hash of the whole CRL. |
| 100 |
static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 101 |
void *exarg) { |
| 102 |
X509_CRL *crl = (X509_CRL *)*pval; |
| 103 |
int i; |
| 104 |
|
| 105 |
switch (operation) { |
| 106 |
case ASN1_OP_NEW_POST: |
| 107 |
crl->idp = NULL; |
| 108 |
crl->akid = NULL; |
| 109 |
crl->flags = 0; |
| 110 |
crl->idp_flags = 0; |
| 111 |
break; |
| 112 |
|
| 113 |
case ASN1_OP_D2I_POST: { |
| 114 |
// The version must be one of v1(0) or v2(1). |
| 115 |
long version = X509_CRL_VERSION_1; |
| 116 |
if (crl->crl->version != NULL) { |
| 117 |
version = ASN1_INTEGER_get(crl->crl->version); |
| 118 |
// TODO(https://crbug.com/boringssl/364): |X509_CRL_VERSION_1| |
| 119 |
// should also be rejected. This means an explicitly-encoded X.509v1 |
| 120 |
// version. v1 is DEFAULT, so DER requires it be omitted. |
| 121 |
if (version < X509_CRL_VERSION_1 || version > X509_CRL_VERSION_2) { |
| 122 |
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_VERSION); |
| 123 |
return 0; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// Per RFC 5280, section 5.1.2.1, extensions require v2. |
| 128 |
if (version != X509_CRL_VERSION_2 && crl->crl->extensions != NULL) { |
| 129 |
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_FIELD_FOR_VERSION); |
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
if (!X509_CRL_digest(crl, EVP_sha256(), crl->crl_hash, NULL)) { |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
crl->idp = |
| 138 |
X509_CRL_get_ext_d2i(crl, NID_issuing_distribution_point, &i, NULL); |
| 139 |
if (crl->idp != NULL) { |
| 140 |
if (!setup_idp(crl, crl->idp)) { |
| 141 |
return 0; |
| 142 |
} |
| 143 |
} else if (i != -1) { |
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
crl->akid = |
| 148 |
X509_CRL_get_ext_d2i(crl, NID_authority_key_identifier, &i, NULL); |
| 149 |
if (crl->akid == NULL && i != -1) { |
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
// See if we have any unhandled critical CRL extensions and indicate |
| 154 |
// this in a flag. We only currently handle IDP so anything else |
| 155 |
// critical sets the flag. This code accesses the X509_CRL structure |
| 156 |
// directly: applications shouldn't do this. |
| 157 |
const STACK_OF(X509_EXTENSION) *exts = crl->crl->extensions; |
| 158 |
for (size_t idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) { |
| 159 |
const X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, idx); |
| 160 |
int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext)); |
| 161 |
if (X509_EXTENSION_get_critical(ext)) { |
| 162 |
if (nid == NID_issuing_distribution_point || |
| 163 |
nid == NID_authority_key_identifier) { |
| 164 |
continue; |
| 165 |
} |
| 166 |
crl->flags |= EXFLAG_CRITICAL; |
| 167 |
break; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
if (!crl_parse_entry_extensions(crl)) { |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
break; |
| 176 |
} |
| 177 |
|
| 178 |
case ASN1_OP_FREE_POST: |
| 179 |
AUTHORITY_KEYID_free(crl->akid); |
| 180 |
ISSUING_DIST_POINT_free(crl->idp); |
| 181 |
break; |
| 182 |
} |
| 183 |
return 1; |
| 184 |
} |
| 185 |
|
| 186 |
// Convert IDP into a more convenient form |
| 187 |
// |
| 188 |
// TODO(davidben): Each of these flags are already booleans, so this is not |
| 189 |
// really more convenient. We can probably remove |idp_flags|. |
| 190 |
static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp) { |
| 191 |
int idp_only = 0; |
| 192 |
// Set various flags according to IDP |
| 193 |
crl->idp_flags |= IDP_PRESENT; |
| 194 |
if (idp->onlyuser > 0) { |
| 195 |
idp_only++; |
| 196 |
crl->idp_flags |= IDP_ONLYUSER; |
| 197 |
} |
| 198 |
if (idp->onlyCA > 0) { |
| 199 |
idp_only++; |
| 200 |
crl->idp_flags |= IDP_ONLYCA; |
| 201 |
} |
| 202 |
if (idp->onlyattr > 0) { |
| 203 |
idp_only++; |
| 204 |
crl->idp_flags |= IDP_ONLYATTR; |
| 205 |
} |
| 206 |
|
| 207 |
// Per RFC 5280, section 5.2.5, at most one of onlyContainsUserCerts, |
| 208 |
// onlyContainsCACerts, and onlyContainsAttributeCerts may be true. |
| 209 |
// |
| 210 |
// TODO(crbug.com/boringssl/443): Move this check to the |ISSUING_DIST_POINT| |
| 211 |
// parser. |
| 212 |
if (idp_only > 1) { |
| 213 |
crl->idp_flags |= IDP_INVALID; |
| 214 |
} |
| 215 |
|
| 216 |
if (idp->indirectCRL > 0) { |
| 217 |
crl->idp_flags |= IDP_INDIRECT; |
| 218 |
} |
| 219 |
|
| 220 |
if (idp->onlysomereasons) { |
| 221 |
crl->idp_flags |= IDP_REASONS; |
| 222 |
} |
| 223 |
|
| 224 |
// TODO(davidben): The new verifier does not support nameRelativeToCRLIssuer. |
| 225 |
// Remove this? |
| 226 |
return DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl)); |
| 227 |
} |
| 228 |
|
| 229 |
ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = { |
| 230 |
ASN1_SIMPLE(X509_CRL, crl, X509_CRL_INFO), |
| 231 |
ASN1_SIMPLE(X509_CRL, sig_alg, X509_ALGOR), |
| 232 |
ASN1_SIMPLE(X509_CRL, signature, ASN1_BIT_STRING), |
| 233 |
} ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL) |
| 234 |
|
| 235 |
// Although |X509_REVOKED| contains an |X509_NAME|, it can be const. It is not |
| 236 |
// affected by https://crbug.com/boringssl/407 because the |X509_NAME| does |
| 237 |
// not participate in serialization. |
| 238 |
IMPLEMENT_ASN1_FUNCTIONS_const(X509_REVOKED) |
| 239 |
IMPLEMENT_ASN1_DUP_FUNCTION_const(X509_REVOKED) |
| 240 |
|
| 241 |
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO) |
| 242 |
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL) |
| 243 |
IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL) |
| 244 |
|
| 245 |
static int X509_REVOKED_cmp(const X509_REVOKED *const *a, |
| 246 |
const X509_REVOKED *const *b) { |
| 247 |
return ASN1_STRING_cmp((*a)->serialNumber, (*b)->serialNumber); |
| 248 |
} |
| 249 |
|
| 250 |
int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev) { |
| 251 |
X509_CRL_INFO *inf; |
| 252 |
inf = crl->crl; |
| 253 |
if (!inf->revoked) { |
| 254 |
inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp); |
| 255 |
} |
| 256 |
if (!inf->revoked || !sk_X509_REVOKED_push(inf->revoked, rev)) { |
| 257 |
return 0; |
| 258 |
} |
| 259 |
asn1_encoding_clear(&inf->enc); |
| 260 |
return 1; |
| 261 |
} |
| 262 |
|
| 263 |
int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *pkey) { |
| 264 |
if (X509_ALGOR_cmp(crl->sig_alg, crl->crl->sig_alg) != 0) { |
| 265 |
OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH); |
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
return ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO), crl->sig_alg, |
| 270 |
crl->signature, crl->crl, pkey); |
| 271 |
} |
| 272 |
|
| 273 |
int X509_CRL_get0_by_serial(X509_CRL *crl, X509_REVOKED **ret, |
| 274 |
const ASN1_INTEGER *serial) { |
| 275 |
return crl_lookup(crl, ret, serial, NULL); |
| 276 |
} |
| 277 |
|
| 278 |
int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x) { |
| 279 |
return crl_lookup(crl, ret, X509_get_serialNumber(x), |
| 280 |
X509_get_issuer_name(x)); |
| 281 |
} |
| 282 |
|
| 283 |
static int crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm, |
| 284 |
X509_REVOKED *rev) { |
| 285 |
return nm == NULL || X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)) == 0; |
| 286 |
} |
| 287 |
|
| 288 |
static struct CRYPTO_STATIC_MUTEX g_crl_sort_lock = CRYPTO_STATIC_MUTEX_INIT; |
| 289 |
|
| 290 |
static int crl_lookup(X509_CRL *crl, X509_REVOKED **ret, |
| 291 |
const ASN1_INTEGER *serial, X509_NAME *issuer) { |
| 292 |
// Use an assert, rather than a runtime error, because returning nothing for a |
| 293 |
// CRL is arguably failing open, rather than closed. |
| 294 |
assert(serial->type == V_ASN1_INTEGER || serial->type == V_ASN1_NEG_INTEGER); |
| 295 |
X509_REVOKED rtmp, *rev; |
| 296 |
size_t idx; |
| 297 |
rtmp.serialNumber = (ASN1_INTEGER *)serial; |
| 298 |
// Sort revoked into serial number order if not already sorted. Do this |
| 299 |
// under a lock to avoid race condition. |
| 300 |
|
| 301 |
CRYPTO_STATIC_MUTEX_lock_read(&g_crl_sort_lock); |
| 302 |
const int is_sorted = sk_X509_REVOKED_is_sorted(crl->crl->revoked); |
| 303 |
CRYPTO_STATIC_MUTEX_unlock_read(&g_crl_sort_lock); |
| 304 |
|
| 305 |
if (!is_sorted) { |
| 306 |
CRYPTO_STATIC_MUTEX_lock_write(&g_crl_sort_lock); |
| 307 |
if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked)) { |
| 308 |
sk_X509_REVOKED_sort(crl->crl->revoked); |
| 309 |
} |
| 310 |
CRYPTO_STATIC_MUTEX_unlock_write(&g_crl_sort_lock); |
| 311 |
} |
| 312 |
|
| 313 |
if (!sk_X509_REVOKED_find_awslc(crl->crl->revoked, &idx, &rtmp)) { |
| 314 |
return 0; |
| 315 |
} |
| 316 |
// Need to look for matching name |
| 317 |
for (; idx < sk_X509_REVOKED_num(crl->crl->revoked); idx++) { |
| 318 |
rev = sk_X509_REVOKED_value(crl->crl->revoked, idx); |
| 319 |
if (ASN1_INTEGER_cmp(rev->serialNumber, serial)) { |
| 320 |
return 0; |
| 321 |
} |
| 322 |
if (crl_revoked_issuer_match(crl, issuer, rev)) { |
| 323 |
if (ret) { |
| 324 |
*ret = rev; |
| 325 |
} |
| 326 |
return 1; |
| 327 |
} |
| 328 |
} |
| 329 |
return 0; |
| 330 |
} |