Coverage Report

Created: 2025-11-03 06:30

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