Coverage Report

Created: 2025-06-11 06:41

/src/boringssl/crypto/x509/x_crl.cc
Line
Count
Source (jump to first uncovered line)
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 wont 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
  STACK_OF(X509_REVOKED) *revoked = X509_CRL_get_REVOKED(crl);
77
0
  for (size_t i = 0; i < sk_X509_REVOKED_num(revoked); i++) {
78
0
    X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
79
80
0
    int crit;
81
0
    ASN1_ENUMERATED *reason = reinterpret_cast<ASN1_ENUMERATED *>(
82
0
        X509_REVOKED_get_ext_d2i(rev, NID_crl_reason, &crit, NULL));
83
0
    if (!reason && crit != -1) {
84
0
      crl->flags |= EXFLAG_INVALID;
85
0
      return 1;
86
0
    }
87
88
0
    if (reason) {
89
0
      rev->reason = ASN1_ENUMERATED_get(reason);
90
0
      ASN1_ENUMERATED_free(reason);
91
0
    } else {
92
0
      rev->reason = CRL_REASON_NONE;
93
0
    }
94
95
    // We do not support any critical CRL entry extensions.
96
0
    const STACK_OF(X509_EXTENSION) *exts = rev->extensions;
97
0
    for (size_t j = 0; j < sk_X509_EXTENSION_num(exts); j++) {
98
0
      const X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, j);
99
0
      if (X509_EXTENSION_get_critical(ext)) {
100
0
        crl->flags |= EXFLAG_CRITICAL;
101
0
        break;
102
0
      }
103
0
    }
104
0
  }
105
106
0
  return 1;
107
0
}
108
109
// The X509_CRL structure needs a bit of customisation. Cache some extensions
110
// and hash of the whole CRL.
111
static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
112
0
                  void *exarg) {
113
0
  X509_CRL *crl = (X509_CRL *)*pval;
114
0
  int i;
115
116
0
  switch (operation) {
117
0
    case ASN1_OP_NEW_POST:
118
0
      crl->idp = NULL;
119
0
      crl->akid = NULL;
120
0
      crl->flags = 0;
121
0
      crl->idp_flags = 0;
122
0
      break;
123
124
0
    case ASN1_OP_D2I_POST: {
125
      // The version must be one of v1(0) or v2(1).
126
0
      long version = X509_CRL_VERSION_1;
127
0
      if (crl->crl->version != NULL) {
128
0
        version = ASN1_INTEGER_get(crl->crl->version);
129
        // TODO(https://crbug.com/boringssl/364): |X509_CRL_VERSION_1|
130
        // should also be rejected. This means an explicitly-encoded X.509v1
131
        // version. v1 is DEFAULT, so DER requires it be omitted.
132
0
        if (version < X509_CRL_VERSION_1 || version > X509_CRL_VERSION_2) {
133
0
          OPENSSL_PUT_ERROR(X509, X509_R_INVALID_VERSION);
134
0
          return 0;
135
0
        }
136
0
      }
137
138
      // Per RFC 5280, section 5.1.2.1, extensions require v2.
139
0
      if (version != X509_CRL_VERSION_2 && crl->crl->extensions != NULL) {
140
0
        OPENSSL_PUT_ERROR(X509, X509_R_INVALID_FIELD_FOR_VERSION);
141
0
        return 0;
142
0
      }
143
144
0
      if (!X509_CRL_digest(crl, EVP_sha256(), crl->crl_hash, NULL)) {
145
0
        return 0;
146
0
      }
147
148
0
      crl->idp = reinterpret_cast<ISSUING_DIST_POINT *>(
149
0
          X509_CRL_get_ext_d2i(crl, NID_issuing_distribution_point, &i, NULL));
150
0
      if (crl->idp != NULL) {
151
0
        if (!setup_idp(crl, crl->idp)) {
152
0
          return 0;
153
0
        }
154
0
      } else if (i != -1) {
155
0
        return 0;
156
0
      }
157
158
0
      crl->akid = reinterpret_cast<AUTHORITY_KEYID *>(
159
0
          X509_CRL_get_ext_d2i(crl, NID_authority_key_identifier, &i, NULL));
160
0
      if (crl->akid == NULL && i != -1) {
161
0
        return 0;
162
0
      }
163
164
      // See if we have any unhandled critical CRL extensions and indicate
165
      // this in a flag. We only currently handle IDP so anything else
166
      // critical sets the flag. This code accesses the X509_CRL structure
167
      // directly: applications shouldn't do this.
168
0
      const STACK_OF(X509_EXTENSION) *exts = crl->crl->extensions;
169
0
      for (size_t idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
170
0
        const X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, idx);
171
0
        int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext));
172
0
        if (X509_EXTENSION_get_critical(ext)) {
173
0
          if (nid == NID_issuing_distribution_point ||
174
0
              nid == NID_authority_key_identifier) {
175
0
            continue;
176
0
          }
177
0
          crl->flags |= EXFLAG_CRITICAL;
178
0
          break;
179
0
        }
180
0
      }
181
182
0
      if (!crl_parse_entry_extensions(crl)) {
183
0
        return 0;
184
0
      }
185
186
0
      break;
187
0
    }
188
189
0
    case ASN1_OP_FREE_POST:
190
0
      AUTHORITY_KEYID_free(crl->akid);
191
0
      ISSUING_DIST_POINT_free(crl->idp);
192
0
      break;
193
0
  }
194
0
  return 1;
195
0
}
196
197
// Convert IDP into a more convenient form
198
//
199
// TODO(davidben): Each of these flags are already booleans, so this is not
200
// really more convenient. We can probably remove |idp_flags|.
201
0
static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp) {
202
0
  int idp_only = 0;
203
  // Set various flags according to IDP
204
0
  crl->idp_flags |= IDP_PRESENT;
205
0
  if (idp->onlyuser > 0) {
206
0
    idp_only++;
207
0
    crl->idp_flags |= IDP_ONLYUSER;
208
0
  }
209
0
  if (idp->onlyCA > 0) {
210
0
    idp_only++;
211
0
    crl->idp_flags |= IDP_ONLYCA;
212
0
  }
213
0
  if (idp->onlyattr > 0) {
214
0
    idp_only++;
215
0
    crl->idp_flags |= IDP_ONLYATTR;
216
0
  }
217
218
  // Per RFC 5280, section 5.2.5, at most one of onlyContainsUserCerts,
219
  // onlyContainsCACerts, and onlyContainsAttributeCerts may be true.
220
  //
221
  // TODO(crbug.com/boringssl/443): Move this check to the |ISSUING_DIST_POINT|
222
  // parser.
223
0
  if (idp_only > 1) {
224
0
    crl->idp_flags |= IDP_INVALID;
225
0
  }
226
227
0
  if (idp->indirectCRL > 0) {
228
0
    crl->idp_flags |= IDP_INDIRECT;
229
0
  }
230
231
0
  if (idp->onlysomereasons) {
232
0
    crl->idp_flags |= IDP_REASONS;
233
0
  }
234
235
  // TODO(davidben): The new verifier does not support nameRelativeToCRLIssuer.
236
  // Remove this?
237
0
  return DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
238
0
}
239
240
ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
241
    ASN1_SIMPLE(X509_CRL, crl, X509_CRL_INFO),
242
    ASN1_SIMPLE(X509_CRL, sig_alg, X509_ALGOR),
243
    ASN1_SIMPLE(X509_CRL, signature, ASN1_BIT_STRING),
244
} ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
245
246
// Although |X509_REVOKED| contains an |X509_NAME|, it can be const. It is not
247
// affected by https://crbug.com/boringssl/407 because the  |X509_NAME| does
248
// not participate in serialization.
249
IMPLEMENT_ASN1_FUNCTIONS_const(X509_REVOKED)
250
IMPLEMENT_ASN1_DUP_FUNCTION_const(X509_REVOKED)
251
252
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
253
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
254
IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
255
256
static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
257
0
                            const X509_REVOKED *const *b) {
258
0
  return ASN1_STRING_cmp((*a)->serialNumber, (*b)->serialNumber);
259
0
}
260
261
0
int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev) {
262
0
  X509_CRL_INFO *inf;
263
0
  inf = crl->crl;
264
0
  if (!inf->revoked) {
265
0
    inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
266
0
  }
267
0
  if (!inf->revoked || !sk_X509_REVOKED_push(inf->revoked, rev)) {
268
0
    return 0;
269
0
  }
270
0
  asn1_encoding_clear(&inf->enc);
271
0
  return 1;
272
0
}
273
274
0
int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *pkey) {
275
0
  if (X509_ALGOR_cmp(crl->sig_alg, crl->crl->sig_alg) != 0) {
276
0
    OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH);
277
0
    return 0;
278
0
  }
279
280
0
  return ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO), crl->sig_alg,
281
0
                          crl->signature, crl->crl, pkey);
282
0
}
283
284
int X509_CRL_get0_by_serial(X509_CRL *crl, X509_REVOKED **ret,
285
0
                            const ASN1_INTEGER *serial) {
286
0
  return crl_lookup(crl, ret, serial, NULL);
287
0
}
288
289
0
int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x) {
290
0
  return crl_lookup(crl, ret, X509_get_serialNumber(x),
291
0
                    X509_get_issuer_name(x));
292
0
}
293
294
static int crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm,
295
0
                                    X509_REVOKED *rev) {
296
0
  return nm == NULL || X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)) == 0;
297
0
}
298
299
static CRYPTO_MUTEX g_crl_sort_lock = CRYPTO_MUTEX_INIT;
300
301
static int crl_lookup(X509_CRL *crl, X509_REVOKED **ret,
302
0
                      const ASN1_INTEGER *serial, X509_NAME *issuer) {
303
  // Use an assert, rather than a runtime error, because returning nothing for a
304
  // CRL is arguably failing open, rather than closed.
305
0
  assert(serial->type == V_ASN1_INTEGER || serial->type == V_ASN1_NEG_INTEGER);
306
0
  X509_REVOKED rtmp, *rev;
307
0
  size_t idx;
308
0
  rtmp.serialNumber = (ASN1_INTEGER *)serial;
309
  // Sort revoked into serial number order if not already sorted. Do this
310
  // under a lock to avoid race condition.
311
312
0
  CRYPTO_MUTEX_lock_read(&g_crl_sort_lock);
313
0
  const int is_sorted = sk_X509_REVOKED_is_sorted(crl->crl->revoked);
314
0
  CRYPTO_MUTEX_unlock_read(&g_crl_sort_lock);
315
316
0
  if (!is_sorted) {
317
0
    CRYPTO_MUTEX_lock_write(&g_crl_sort_lock);
318
0
    if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked)) {
319
0
      sk_X509_REVOKED_sort(crl->crl->revoked);
320
0
    }
321
0
    CRYPTO_MUTEX_unlock_write(&g_crl_sort_lock);
322
0
  }
323
324
0
  if (!sk_X509_REVOKED_find(crl->crl->revoked, &idx, &rtmp)) {
325
0
    return 0;
326
0
  }
327
  // Need to look for matching name
328
0
  for (; idx < sk_X509_REVOKED_num(crl->crl->revoked); idx++) {
329
0
    rev = sk_X509_REVOKED_value(crl->crl->revoked, idx);
330
0
    if (ASN1_INTEGER_cmp(rev->serialNumber, serial)) {
331
0
      return 0;
332
0
    }
333
0
    if (crl_revoked_issuer_match(crl, issuer, rev)) {
334
0
      if (ret) {
335
0
        *ret = rev;
336
0
      }
337
0
      return 1;
338
0
    }
339
0
  }
340
0
  return 0;
341
0
}