Coverage Report

Created: 2026-06-28 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/x509/x509_vfy.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 <ctype.h>
16
#include <limits.h>
17
#include <string.h>
18
#include <time.h>
19
20
#include <openssl/asn1.h>
21
#include <openssl/err.h>
22
#include <openssl/evp.h>
23
#include <openssl/mem.h>
24
#include <openssl/obj.h>
25
#include <openssl/x509.h>
26
27
#include "../internal.h"
28
#include "../mem_internal.h"
29
#include "internal.h"
30
31
32
using namespace bssl;
33
34
static ExDataClass g_ex_data_class(/*with_app_data=*/true);
35
36
// CRL score values
37
38
// No unhandled critical extensions
39
0
#define CRL_SCORE_NOCRITICAL 0x100
40
41
// certificate is within CRL scope
42
0
#define CRL_SCORE_SCOPE 0x080
43
44
// CRL times valid
45
0
#define CRL_SCORE_TIME 0x040
46
47
// Issuer name matches certificate
48
0
#define CRL_SCORE_ISSUER_NAME 0x020
49
50
// If this score or above CRL is probably valid
51
#define CRL_SCORE_VALID \
52
0
  (CRL_SCORE_NOCRITICAL | CRL_SCORE_TIME | CRL_SCORE_SCOPE)
53
54
// CRL issuer is certificate issuer
55
0
#define CRL_SCORE_ISSUER_CERT 0x018
56
57
// CRL issuer is on certificate path
58
0
#define CRL_SCORE_SAME_PATH 0x008
59
60
// CRL issuer matches CRL AKID
61
0
#define CRL_SCORE_AKID 0x004
62
63
static int null_callback(int ok, X509_STORE_CTX *e);
64
static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
65
static int check_chain_extensions(X509_STORE_CTX *ctx);
66
static int check_name_constraints(X509_STORE_CTX *ctx);
67
static int check_id(X509_STORE_CTX *ctx);
68
static int check_trust(X509_STORE_CTX *ctx);
69
static int check_revocation(X509_STORE_CTX *ctx);
70
static int check_cert(X509_STORE_CTX *ctx);
71
static int check_policy(X509_STORE_CTX *ctx);
72
73
static X509 *get_trusted_issuer(X509_STORE_CTX *ctx, X509 *x);
74
static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, X509_CRL *crl,
75
                         X509 *x);
76
static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x);
77
static int crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
78
                          int *pcrl_score);
79
static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score);
80
static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl);
81
static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x);
82
83
static int internal_verify(X509_STORE_CTX *ctx);
84
85
0
static int null_callback(int ok, X509_STORE_CTX *e) { return ok; }
86
87
// cert_self_signed checks if `x` is self-signed. If `x` is valid, it returns
88
// one and sets `*out_is_self_signed` to the result. If `x` is invalid, it
89
// returns zero.
90
0
static int cert_self_signed(X509 *x, int *out_is_self_signed) {
91
0
  if (!x509v3_cache_extensions(x)) {
92
0
    return 0;
93
0
  }
94
0
  auto *impl = FromOpaque(x);
95
0
  *out_is_self_signed = (impl->ex_flags & EXFLAG_SS) != 0;
96
0
  return 1;
97
0
}
98
99
0
static int call_verify_cb(int ok, X509_STORE_CTX *ctx) {
100
0
  ok = ctx->verify_cb(ok, ctx);
101
  // Historically, callbacks returning values like -1 would be treated as a mix
102
  // of success or failure. Insert that callers check correctly.
103
  //
104
  // TODO(davidben): Also use this wrapper to constrain which errors may be
105
  // suppressed, and ensure all `verify_cb` calls remember to fill in an error.
106
0
  BSSL_CHECK(ok == 0 || ok == 1);
107
0
  return ok;
108
0
}
109
110
// Given a certificate try and find an exact match in the store
111
0
static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x) {
112
0
  STACK_OF(X509) *certs;
113
0
  X509 *xtmp = nullptr;
114
0
  size_t i;
115
  // Lookup all certs with matching subject name
116
0
  certs = X509_STORE_CTX_get1_certs(ctx, X509_get_subject_name(x));
117
0
  if (certs == nullptr) {
118
0
    return nullptr;
119
0
  }
120
  // Look for exact match
121
0
  for (i = 0; i < sk_X509_num(certs); i++) {
122
0
    xtmp = sk_X509_value(certs, i);
123
0
    if (!X509_cmp(xtmp, x)) {
124
0
      break;
125
0
    }
126
0
  }
127
0
  if (i < sk_X509_num(certs)) {
128
0
    X509_up_ref(xtmp);
129
0
  } else {
130
0
    xtmp = nullptr;
131
0
  }
132
0
  sk_X509_pop_free(certs, X509_free);
133
0
  return xtmp;
134
0
}
135
136
0
int X509_verify_cert(X509_STORE_CTX *ctx) {
137
0
  X509 *chain_ss = nullptr;
138
0
  int bad_chain = 0;
139
0
  X509_VERIFY_PARAM *param = ctx->param;
140
0
  int i, ok = 0;
141
0
  int j, retry, trust;
142
0
  STACK_OF(X509) *sktmp = nullptr;
143
144
0
  {
145
0
    if (ctx->cert == nullptr) {
146
0
      OPENSSL_PUT_ERROR(X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
147
0
      ctx->error = X509_V_ERR_INVALID_CALL;
148
0
      return 0;
149
0
    }
150
151
0
    if (ctx->chain != nullptr) {
152
      // This X509_STORE_CTX has already been used to verify a cert. We
153
      // cannot do another one.
154
0
      OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
155
0
      ctx->error = X509_V_ERR_INVALID_CALL;
156
0
      return 0;
157
0
    }
158
159
0
    if (ctx->param->flags &
160
0
        (X509_V_FLAG_EXTENDED_CRL_SUPPORT | X509_V_FLAG_USE_DELTAS)) {
161
      // We do not support indirect or delta CRLs. The flags still exist for
162
      // compatibility with bindings libraries, but to ensure we do not
163
      // inadvertently skip a CRL check that the caller expects, fail closed.
164
0
      OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
165
0
      ctx->error = X509_V_ERR_INVALID_CALL;
166
0
      return 0;
167
0
    }
168
169
    // first we make sure the chain we are going to build is present and that
170
    // the first entry is in place
171
0
    ctx->chain = sk_X509_new_null();
172
0
    if (ctx->chain == nullptr || !sk_X509_push(ctx->chain, ctx->cert)) {
173
0
      ctx->error = X509_V_ERR_OUT_OF_MEM;
174
0
      goto end;
175
0
    }
176
0
    X509_up_ref(ctx->cert);
177
0
    ctx->last_untrusted = 1;
178
179
    // We use a temporary STACK so we can chop and hack at it.
180
0
    if (ctx->untrusted != nullptr &&
181
0
        (sktmp = sk_X509_dup(ctx->untrusted)) == nullptr) {
182
0
      ctx->error = X509_V_ERR_OUT_OF_MEM;
183
0
      goto end;
184
0
    }
185
186
0
    int num = (int)sk_X509_num(ctx->chain);
187
0
    X509 *x = sk_X509_value(ctx->chain, num - 1);
188
    // `param->depth` does not include the leaf certificate or the trust anchor,
189
    // so the maximum size is 2 more.
190
0
    int max_chain = param->depth >= INT_MAX - 2 ? INT_MAX : param->depth + 2;
191
192
0
    for (;;) {
193
0
      if (num >= max_chain) {
194
        // FIXME: If this happens, we should take note of it and, if
195
        // appropriate, use the X509_V_ERR_CERT_CHAIN_TOO_LONG error code later.
196
0
        break;
197
0
      }
198
199
0
      int is_self_signed;
200
0
      if (!cert_self_signed(x, &is_self_signed)) {
201
0
        ctx->error = X509_V_ERR_INVALID_EXTENSION;
202
0
        goto end;
203
0
      }
204
205
      // If we are self signed, we break
206
0
      if (is_self_signed) {
207
0
        break;
208
0
      }
209
      // If asked see if we can find issuer in trusted store first
210
0
      if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) {
211
0
        X509 *issuer = get_trusted_issuer(ctx, x);
212
0
        if (issuer != nullptr) {
213
          // Free the certificate. It will be picked up again later.
214
0
          X509_free(issuer);
215
0
          break;
216
0
        }
217
0
      }
218
219
      // If we were passed a cert chain, use it first
220
0
      if (sktmp != nullptr) {
221
0
        X509 *issuer = find_issuer(ctx, sktmp, x);
222
0
        if (issuer != nullptr) {
223
0
          if (!sk_X509_push(ctx->chain, issuer)) {
224
0
            ctx->error = X509_V_ERR_OUT_OF_MEM;
225
0
            goto end;
226
0
          }
227
0
          X509_up_ref(issuer);
228
0
          (void)sk_X509_delete_ptr(sktmp, issuer);
229
0
          ctx->last_untrusted++;
230
0
          x = issuer;
231
0
          num++;
232
          // reparse the full chain for the next one
233
0
          continue;
234
0
        }
235
0
      }
236
0
      break;
237
0
    }
238
239
    // Remember how many untrusted certs we have
240
0
    j = num;
241
    // at this point, chain should contain a list of untrusted certificates.
242
    // We now need to add at least one trusted one, if possible, otherwise we
243
    // complain.
244
245
0
    do {
246
      // Examine last certificate in chain and see if it is self signed.
247
0
      i = (int)sk_X509_num(ctx->chain);
248
0
      x = sk_X509_value(ctx->chain, i - 1);
249
250
0
      int is_self_signed;
251
0
      if (!cert_self_signed(x, &is_self_signed)) {
252
0
        ctx->error = X509_V_ERR_INVALID_EXTENSION;
253
0
        goto end;
254
0
      }
255
256
0
      if (is_self_signed) {
257
        // we have a self signed certificate
258
0
        if (sk_X509_num(ctx->chain) == 1) {
259
          // We have a single self signed certificate: see if we can
260
          // find it in the store. We must have an exact match to avoid
261
          // possible impersonation.
262
0
          X509 *issuer = get_trusted_issuer(ctx, x);
263
0
          if (issuer == nullptr || X509_cmp(x, issuer) != 0) {
264
0
            X509_free(issuer);
265
0
            ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
266
0
            ctx->current_cert = x;
267
0
            ctx->error_depth = i - 1;
268
0
            bad_chain = 1;
269
0
            if (!call_verify_cb(0, ctx)) {
270
0
              goto end;
271
0
            }
272
0
          } else {
273
            // We have a match: replace certificate with store
274
            // version so we get any trust settings.
275
0
            X509_free(x);
276
0
            x = issuer;
277
0
            (void)sk_X509_set(ctx->chain, i - 1, x);
278
0
            ctx->last_untrusted = 0;
279
0
          }
280
0
        } else {
281
          // extract and save self signed certificate for later use
282
0
          chain_ss = sk_X509_pop(ctx->chain);
283
0
          ctx->last_untrusted--;
284
0
          num--;
285
0
          j--;
286
0
          x = sk_X509_value(ctx->chain, num - 1);
287
0
        }
288
0
      }
289
      // We now lookup certs from the certificate store
290
0
      for (;;) {
291
0
        if (num >= max_chain) {
292
          // FIXME: If this happens, we should take note of it and, if
293
          // appropriate, use the X509_V_ERR_CERT_CHAIN_TOO_LONG error code
294
          // later.
295
0
          break;
296
0
        }
297
0
        if (!cert_self_signed(x, &is_self_signed)) {
298
0
          ctx->error = X509_V_ERR_INVALID_EXTENSION;
299
0
          goto end;
300
0
        }
301
        // If we are self signed, we break
302
0
        if (is_self_signed) {
303
0
          break;
304
0
        }
305
0
        X509 *issuer = get_trusted_issuer(ctx, x);
306
0
        if (issuer == nullptr) {
307
0
          break;
308
0
        }
309
0
        x = issuer;
310
0
        if (!sk_X509_push(ctx->chain, x)) {
311
0
          X509_free(issuer);
312
0
          ctx->error = X509_V_ERR_OUT_OF_MEM;
313
0
          goto end;
314
0
        }
315
0
        num++;
316
0
      }
317
318
      // we now have our chain, lets check it...
319
0
      trust = check_trust(ctx);
320
321
      // If explicitly rejected error
322
0
      if (trust == X509_TRUST_REJECTED) {
323
0
        goto end;
324
0
      }
325
      // If it's not explicitly trusted then check if there is an alternative
326
      // chain that could be used. We only do this if we haven't already
327
      // checked via TRUSTED_FIRST and the user hasn't switched off alternate
328
      // chain checking
329
0
      retry = 0;
330
0
      if (trust != X509_TRUST_TRUSTED &&
331
0
          !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) &&
332
0
          !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
333
0
        while (j-- > 1) {
334
0
          X509 *issuer =
335
0
              get_trusted_issuer(ctx, sk_X509_value(ctx->chain, j - 1));
336
          // Check if we found an alternate chain
337
0
          if (issuer != nullptr) {
338
            // Free up the found cert we'll add it again later
339
0
            X509_free(issuer);
340
341
            // Dump all the certs above this point - we've found an
342
            // alternate chain
343
0
            while (num > j) {
344
0
              X509_free(sk_X509_pop(ctx->chain));
345
0
              num--;
346
0
            }
347
0
            ctx->last_untrusted = (int)sk_X509_num(ctx->chain);
348
0
            retry = 1;
349
0
            break;
350
0
          }
351
0
        }
352
0
      }
353
0
    } while (retry);
354
355
    // If not explicitly trusted then indicate error unless it's a single
356
    // self signed certificate in which case we've indicated an error already
357
    // and set bad_chain == 1
358
0
    if (trust != X509_TRUST_TRUSTED && !bad_chain) {
359
0
      if (chain_ss == nullptr ||
360
0
          !x509_check_issued_with_callback(ctx, x, chain_ss)) {
361
0
        if (ctx->last_untrusted >= num) {
362
0
          ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
363
0
        } else {
364
0
          ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
365
0
        }
366
0
        ctx->current_cert = x;
367
0
      } else {
368
0
        if (!sk_X509_push(ctx->chain, chain_ss)) {
369
0
          ctx->error = X509_V_ERR_OUT_OF_MEM;
370
0
          goto end;
371
0
        }
372
0
        num++;
373
0
        ctx->last_untrusted = num;
374
0
        ctx->current_cert = chain_ss;
375
0
        ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
376
0
        chain_ss = nullptr;
377
0
      }
378
379
0
      ctx->error_depth = num - 1;
380
0
      bad_chain = 1;
381
0
      if (!call_verify_cb(0, ctx)) {
382
0
        goto end;
383
0
      }
384
0
    }
385
386
    // We have the chain complete: now we need to check its purpose
387
0
    if (!check_chain_extensions(ctx) ||  //
388
0
        !check_id(ctx) ||
389
        // We check revocation status after copying parameters because they may
390
        // be needed for CRL signature verification.
391
0
        !check_revocation(ctx) ||  //
392
0
        !internal_verify(ctx) ||   //
393
0
        !check_name_constraints(ctx) ||
394
        // TODO(davidben): Does `check_policy` still need to be conditioned on
395
        // |!bad_chain|? DoS concerns have been resolved.
396
0
        (!bad_chain && !check_policy(ctx))) {
397
0
      goto end;
398
0
    }
399
400
0
    ok = 1;
401
0
  }
402
403
0
end:
404
0
  sk_X509_free(sktmp);
405
0
  X509_free(chain_ss);
406
407
  // Safety net, error returns must set ctx->error
408
0
  if (!ok && ctx->error == X509_V_OK) {
409
0
    ctx->error = X509_V_ERR_UNSPECIFIED;
410
0
  }
411
0
  return ok;
412
0
}
413
414
// Given a STACK_OF(X509) find the issuer of cert (if any)
415
416
0
static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x) {
417
0
  size_t i;
418
0
  X509 *issuer;
419
0
  for (i = 0; i < sk_X509_num(sk); i++) {
420
0
    issuer = sk_X509_value(sk, i);
421
0
    if (x509_check_issued_with_callback(ctx, x, issuer)) {
422
0
      return issuer;
423
0
    }
424
0
  }
425
0
  return nullptr;
426
0
}
427
428
// Given a possible certificate and issuer check them
429
430
int bssl::x509_check_issued_with_callback(X509_STORE_CTX *ctx, const X509 *x,
431
0
                                          const X509 *issuer) {
432
0
  int ret;
433
0
  ret = X509_check_issued(issuer, x);
434
0
  if (ret == X509_V_OK) {
435
0
    return 1;
436
0
  }
437
  // If we haven't asked for issuer errors don't set ctx
438
0
  if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK)) {
439
0
    return 0;
440
0
  }
441
442
0
  ctx->error = ret;
443
0
  ctx->current_cert = const_cast<X509 *>(x);
444
0
  return call_verify_cb(0, ctx);
445
0
}
446
447
0
static X509 *get_trusted_issuer(X509_STORE_CTX *ctx, X509 *x) {
448
0
  X509 *issuer;
449
0
  if (ctx->trusted_stack != nullptr) {
450
    // Ignore the store and use the configured stack instead.
451
0
    issuer = find_issuer(ctx, ctx->trusted_stack, x);
452
0
    if (issuer != nullptr) {
453
0
      X509_up_ref(issuer);
454
0
    }
455
0
    return issuer;
456
0
  }
457
458
0
  if (!X509_STORE_CTX_get1_issuer(&issuer, ctx, x)) {
459
0
    return nullptr;
460
0
  }
461
0
  return issuer;
462
0
}
463
464
// Check a certificate chains extensions for consistency with the supplied
465
// purpose
466
467
0
static int check_chain_extensions(X509_STORE_CTX *ctx) {
468
0
  int plen = 0;
469
0
  int purpose = ctx->param->purpose;
470
471
  // Check all untrusted certificates
472
0
  for (int i = 0; i < ctx->last_untrusted; i++) {
473
0
    X509Impl *x = FromOpaque(sk_X509_value(ctx->chain, i));
474
0
    if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) &&
475
0
        (x->ex_flags & EXFLAG_CRITICAL)) {
476
0
      ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
477
0
      ctx->error_depth = i;
478
0
      ctx->current_cert = x;
479
0
      if (!call_verify_cb(0, ctx)) {
480
0
        return 0;
481
0
      }
482
0
    }
483
484
0
    int must_be_ca = i > 0;
485
0
    if (must_be_ca && !X509_check_ca(x)) {
486
0
      ctx->error = X509_V_ERR_INVALID_CA;
487
0
      ctx->error_depth = i;
488
0
      ctx->current_cert = x;
489
0
      if (!call_verify_cb(0, ctx)) {
490
0
        return 0;
491
0
      }
492
0
    }
493
0
    if (ctx->param->purpose > 0 &&
494
0
        X509_check_purpose(x, purpose, must_be_ca) != 1) {
495
0
      ctx->error = X509_V_ERR_INVALID_PURPOSE;
496
0
      ctx->error_depth = i;
497
0
      ctx->current_cert = x;
498
0
      if (!call_verify_cb(0, ctx)) {
499
0
        return 0;
500
0
      }
501
0
    }
502
    // Check path length constraints. See steps (l) and (m) of RFC 5280,
503
    // section 6.1.4. Note the spec is structured differently from this
504
    // logic. Section 6.1.4 runs from root to leaf and does not run on
505
    // the leaf. `plen` counts the number of times step (l) would have
506
    // run. The constraint is violated if some `x->ex_pathlen`, read in
507
    // step (m), is too low to be decremented `plen` times.
508
    //
509
    // Note that path lengths of self-issued certificates still have to be
510
    // considered - they are just not counted as part of the path length!
511
0
    if (i > 1 && x->ex_pathlen != -1 && plen > x->ex_pathlen + 1) {
512
0
      ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
513
0
      ctx->error_depth = i;
514
0
      ctx->current_cert = x;
515
0
      if (!call_verify_cb(0, ctx)) {
516
0
        return 0;
517
0
      }
518
0
    }
519
    // Increment path length if not self issued. As only self-issued
520
    // _intermediates_ are skipped in (l) of RFC 5280 (simply because it
521
    // operates on certificate chain _edges_), always increment for the first
522
    // (the leaf) in the chain.
523
0
    if (i == 0 || !(x->ex_flags & EXFLAG_SI)) {
524
0
      plen++;
525
0
    }
526
0
  }
527
528
0
  return 1;
529
0
}
530
531
0
static int reject_dns_name_in_common_name(X509 *x509) {
532
0
  const X509_NAME *name = X509_get_subject_name(x509);
533
0
  int i = -1;
534
0
  for (;;) {
535
0
    i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
536
0
    if (i == -1) {
537
0
      return X509_V_OK;
538
0
    }
539
540
0
    const X509_NAME_ENTRY *entry = X509_NAME_get_entry(name, i);
541
0
    const ASN1_STRING *common_name = X509_NAME_ENTRY_get_data(entry);
542
0
    unsigned char *idval;
543
0
    int idlen = ASN1_STRING_to_UTF8(&idval, common_name);
544
0
    if (idlen < 0) {
545
0
      return X509_V_ERR_OUT_OF_MEM;
546
0
    }
547
    // Only process attributes that look like host names. Note it is
548
    // important that this check be mirrored in `X509_check_host`.
549
0
    int looks_like_dns = x509v3_looks_like_dns_name(idval, (size_t)idlen);
550
0
    OPENSSL_free(idval);
551
0
    if (looks_like_dns) {
552
0
      return X509_V_ERR_NAME_CONSTRAINTS_WITHOUT_SANS;
553
0
    }
554
0
  }
555
0
}
556
557
0
static int check_name_constraints(X509_STORE_CTX *ctx) {
558
0
  int i, j, rv;
559
0
  int has_name_constraints = 0;
560
  // Check name constraints for all certificates
561
0
  for (i = (int)sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
562
0
    X509Impl *x = FromOpaque(sk_X509_value(ctx->chain, i));
563
    // Ignore self issued certs unless last in chain
564
0
    if (i && (x->ex_flags & EXFLAG_SI)) {
565
0
      continue;
566
0
    }
567
    // Check against constraints for all certificates higher in chain
568
    // including trust anchor. Trust anchor not strictly speaking needed
569
    // but if it includes constraints it is to be assumed it expects them
570
    // to be obeyed.
571
0
    for (j = (int)sk_X509_num(ctx->chain) - 1; j > i; j--) {
572
0
      NAME_CONSTRAINTS *nc = FromOpaque(sk_X509_value(ctx->chain, j))->nc.get();
573
0
      if (nc) {
574
0
        has_name_constraints = 1;
575
0
        rv = NAME_CONSTRAINTS_check(x, nc);
576
0
        switch (rv) {
577
0
          case X509_V_OK:
578
0
            continue;
579
0
          case X509_V_ERR_OUT_OF_MEM:
580
0
            ctx->error = rv;
581
0
            return 0;
582
0
          default:
583
0
            ctx->error = rv;
584
0
            ctx->error_depth = i;
585
0
            ctx->current_cert = x;
586
0
            if (!call_verify_cb(0, ctx)) {
587
0
              return 0;
588
0
            }
589
0
            break;
590
0
        }
591
0
      }
592
0
    }
593
0
  }
594
595
  // Name constraints do not match against the common name, but
596
  // `X509_check_host` still implements the legacy behavior where, on
597
  // certificates lacking a SAN list, DNS-like names in the common name are
598
  // checked instead.
599
  //
600
  // While we could apply the name constraints to the common name, name
601
  // constraints are rare enough that can hold such certificates to a higher
602
  // standard. Note this does not make "DNS-like" heuristic failures any
603
  // worse. A decorative common-name misidentified as a DNS name would fail
604
  // the name constraint anyway.
605
0
  X509Impl *leaf = FromOpaque(sk_X509_value(ctx->chain, 0));
606
0
  if (has_name_constraints && leaf->altname == nullptr) {
607
0
    rv = reject_dns_name_in_common_name(leaf);
608
0
    switch (rv) {
609
0
      case X509_V_OK:
610
0
        break;
611
0
      case X509_V_ERR_OUT_OF_MEM:
612
0
        ctx->error = rv;
613
0
        return 0;
614
0
      default:
615
0
        ctx->error = rv;
616
0
        ctx->error_depth = 0;
617
0
        ctx->current_cert = leaf;
618
0
        if (!call_verify_cb(0, ctx)) {
619
0
          return 0;
620
0
        }
621
0
        break;
622
0
    }
623
0
  }
624
625
0
  return 1;
626
0
}
627
628
0
static int check_id_error(X509_STORE_CTX *ctx, int errcode) {
629
0
  ctx->error = errcode;
630
0
  ctx->current_cert = ctx->cert;
631
0
  ctx->error_depth = 0;
632
0
  return call_verify_cb(0, ctx);
633
0
}
634
635
0
static int check_hosts(X509 *x, X509_VERIFY_PARAM *param) {
636
0
  size_t i;
637
0
  size_t n = sk_OPENSSL_STRING_num(param->hosts);
638
0
  char *name;
639
640
0
  for (i = 0; i < n; ++i) {
641
0
    name = sk_OPENSSL_STRING_value(param->hosts, i);
642
0
    if (X509_check_host(x, name, strlen(name), param->hostflags, nullptr) > 0) {
643
0
      return 1;
644
0
    }
645
0
  }
646
0
  return n == 0;
647
0
}
648
649
0
static int check_id(X509_STORE_CTX *ctx) {
650
0
  X509_VERIFY_PARAM *vpm = ctx->param;
651
0
  X509 *x = ctx->cert;
652
0
  if (vpm->poison) {
653
0
    if (!check_id_error(ctx, X509_V_ERR_INVALID_CALL)) {
654
0
      return 0;
655
0
    }
656
0
  }
657
0
  if (vpm->hosts && check_hosts(x, vpm) <= 0) {
658
0
    if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH)) {
659
0
      return 0;
660
0
    }
661
0
  }
662
0
  if (vpm->email && X509_check_email(x, vpm->email, vpm->emaillen, 0) <= 0) {
663
0
    if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH)) {
664
0
      return 0;
665
0
    }
666
0
  }
667
0
  if (vpm->ip && X509_check_ip(x, vpm->ip, vpm->iplen, 0) <= 0) {
668
0
    if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH)) {
669
0
      return 0;
670
0
    }
671
0
  }
672
0
  return 1;
673
0
}
674
675
0
static int check_trust(X509_STORE_CTX *ctx) {
676
0
  X509 *x = nullptr;
677
  // Check all trusted certificates in chain
678
0
  for (size_t i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) {
679
0
    x = sk_X509_value(ctx->chain, i);
680
0
    int trust = X509_check_trust(x, ctx->param->trust, 0);
681
    // If explicitly trusted return trusted
682
0
    if (trust == X509_TRUST_TRUSTED) {
683
0
      return X509_TRUST_TRUSTED;
684
0
    }
685
    // If explicitly rejected notify callback and reject if not
686
    // overridden.
687
0
    if (trust == X509_TRUST_REJECTED) {
688
0
      ctx->error_depth = (int)i;
689
0
      ctx->current_cert = x;
690
0
      ctx->error = X509_V_ERR_CERT_REJECTED;
691
0
      if (!call_verify_cb(0, ctx)) {
692
0
        return X509_TRUST_REJECTED;
693
0
      }
694
0
    }
695
0
  }
696
  // If we accept partial chains and have at least one trusted certificate
697
  // return success.
698
0
  if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
699
0
    X509 *mx;
700
0
    if (ctx->last_untrusted < (int)sk_X509_num(ctx->chain)) {
701
0
      return X509_TRUST_TRUSTED;
702
0
    }
703
0
    x = sk_X509_value(ctx->chain, 0);
704
0
    mx = lookup_cert_match(ctx, x);
705
0
    if (mx) {
706
0
      (void)sk_X509_set(ctx->chain, 0, mx);
707
0
      X509_free(x);
708
0
      ctx->last_untrusted = 0;
709
0
      return X509_TRUST_TRUSTED;
710
0
    }
711
0
  }
712
713
  // If no trusted certs in chain at all return untrusted and allow
714
  // standard (no issuer cert) etc errors to be indicated.
715
0
  return X509_TRUST_UNTRUSTED;
716
0
}
717
718
0
static int check_revocation(X509_STORE_CTX *ctx) {
719
0
  if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK)) {
720
0
    return 1;
721
0
  }
722
0
  int last;
723
0
  if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL) {
724
0
    last = (int)sk_X509_num(ctx->chain) - 1;
725
0
  } else {
726
0
    last = 0;
727
0
  }
728
0
  for (int i = 0; i <= last; i++) {
729
0
    ctx->error_depth = i;
730
0
    if (!check_cert(ctx)) {
731
0
      return 0;
732
0
    }
733
0
  }
734
0
  return 1;
735
0
}
736
737
0
static int check_cert(X509_STORE_CTX *ctx) {
738
0
  X509_CRL *crl = nullptr;
739
0
  int ok = 0, cnum = ctx->error_depth;
740
0
  X509 *x = sk_X509_value(ctx->chain, cnum);
741
0
  ctx->current_cert = x;
742
0
  ctx->current_crl_issuer = nullptr;
743
0
  ctx->current_crl_score = 0;
744
745
  // Try to retrieve the relevant CRL. Note that `get_crl` sets
746
  // `current_crl_issuer` and `current_crl_score`, which `check_crl` then reads.
747
  //
748
  // TODO(davidben): The awkward internal calling convention is a historical
749
  // artifact of when these functions were user-overridable callbacks, even
750
  // though there was no way to set them correctly. These callbacks have since
751
  // been removed, so we can pass input and output parameters more directly.
752
0
  if (!get_crl(ctx, &crl, x)) {
753
0
    ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
754
0
    ok = call_verify_cb(0, ctx);
755
0
    goto err;
756
0
  }
757
758
0
  ctx->current_crl = crl;
759
0
  if (!check_crl(ctx, crl) ||  //
760
0
      !cert_crl(ctx, crl, x)) {
761
0
    goto err;
762
0
  }
763
764
0
  ok = 1;
765
766
0
err:
767
0
  X509_CRL_free(crl);
768
0
  ctx->current_crl = nullptr;
769
0
  return ok;
770
0
}
771
772
// Check CRL times against values in X509_STORE_CTX
773
0
static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) {
774
0
  if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) {
775
0
    return 1;
776
0
  }
777
778
0
  if (notify) {
779
0
    ctx->current_crl = crl;
780
0
  }
781
0
  int64_t ptime;
782
0
  if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) {
783
0
    ptime = ctx->param->check_time;
784
0
  } else {
785
0
    ptime = time(nullptr);
786
0
  }
787
788
0
  int i = X509_cmp_time_posix(X509_CRL_get0_lastUpdate(crl), ptime);
789
0
  if (i == 0) {
790
0
    if (!notify) {
791
0
      return 0;
792
0
    }
793
0
    ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
794
0
    if (!call_verify_cb(0, ctx)) {
795
0
      return 0;
796
0
    }
797
0
  }
798
799
0
  if (i > 0) {
800
0
    if (!notify) {
801
0
      return 0;
802
0
    }
803
0
    ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
804
0
    if (!call_verify_cb(0, ctx)) {
805
0
      return 0;
806
0
    }
807
0
  }
808
809
0
  if (X509_CRL_get0_nextUpdate(crl)) {
810
0
    i = X509_cmp_time_posix(X509_CRL_get0_nextUpdate(crl), ptime);
811
812
0
    if (i == 0) {
813
0
      if (!notify) {
814
0
        return 0;
815
0
      }
816
0
      ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
817
0
      if (!call_verify_cb(0, ctx)) {
818
0
        return 0;
819
0
      }
820
0
    }
821
0
    if (i < 0) {
822
0
      if (!notify) {
823
0
        return 0;
824
0
      }
825
0
      ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
826
0
      if (!call_verify_cb(0, ctx)) {
827
0
        return 0;
828
0
      }
829
0
    }
830
0
  }
831
832
0
  if (notify) {
833
0
    ctx->current_crl = nullptr;
834
0
  }
835
836
0
  return 1;
837
0
}
838
839
static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 **pissuer,
840
0
                      int *pscore, STACK_OF(X509_CRL) *crls) {
841
0
  int crl_score, best_score = *pscore;
842
0
  X509 *x = ctx->current_cert;
843
0
  X509_CRL *best_crl = nullptr;
844
0
  X509 *crl_issuer = nullptr, *best_crl_issuer = nullptr;
845
846
0
  for (size_t i = 0; i < sk_X509_CRL_num(crls); i++) {
847
0
    X509_CRL *crl = sk_X509_CRL_value(crls, i);
848
0
    crl_score = get_crl_score(ctx, &crl_issuer, crl, x);
849
0
    if (crl_score < best_score || crl_score == 0) {
850
0
      continue;
851
0
    }
852
    // If current CRL is equivalent use it if it is newer
853
0
    if (crl_score == best_score && best_crl != nullptr) {
854
0
      int day, sec;
855
0
      if (ASN1_TIME_diff(&day, &sec, X509_CRL_get0_lastUpdate(best_crl),
856
0
                         X509_CRL_get0_lastUpdate(crl)) == 0) {
857
0
        continue;
858
0
      }
859
      // ASN1_TIME_diff never returns inconsistent signs for `day`
860
      // and `sec`.
861
0
      if (day <= 0 && sec <= 0) {
862
0
        continue;
863
0
      }
864
0
    }
865
0
    best_crl = crl;
866
0
    best_crl_issuer = crl_issuer;
867
0
    best_score = crl_score;
868
0
  }
869
870
0
  if (best_crl) {
871
0
    if (*pcrl) {
872
0
      X509_CRL_free(*pcrl);
873
0
    }
874
0
    *pcrl = best_crl;
875
0
    *pissuer = best_crl_issuer;
876
0
    *pscore = best_score;
877
0
    X509_CRL_up_ref(best_crl);
878
0
  }
879
880
0
  if (best_score >= CRL_SCORE_VALID) {
881
0
    return 1;
882
0
  }
883
884
0
  return 0;
885
0
}
886
887
// For a given CRL return how suitable it is for the supplied certificate
888
// 'x'. The return value is a mask of several criteria. If the issuer is not
889
// the certificate issuer this is returned in *pissuer.
890
static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, X509_CRL *crl,
891
0
                         X509 *x) {
892
0
  int crl_score = 0;
893
894
  // First see if we can reject CRL straight away
895
896
  // Invalid IDP cannot be processed
897
0
  if (crl->idp_flags & IDP_INVALID) {
898
0
    return 0;
899
0
  }
900
  // Reason codes and indirect CRLs are not supported.
901
0
  if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS)) {
902
0
    return 0;
903
0
  }
904
  // We do not support indirect CRLs, so the issuer names must match.
905
0
  if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
906
0
    return 0;
907
0
  }
908
0
  crl_score |= CRL_SCORE_ISSUER_NAME;
909
910
0
  if (!(crl->flags & EXFLAG_CRITICAL)) {
911
0
    crl_score |= CRL_SCORE_NOCRITICAL;
912
0
  }
913
914
  // Check expiry
915
0
  if (check_crl_time(ctx, crl, 0)) {
916
0
    crl_score |= CRL_SCORE_TIME;
917
0
  }
918
919
  // Check authority key ID and locate certificate issuer
920
0
  if (!crl_akid_check(ctx, crl, pissuer, &crl_score)) {
921
    // If we can't locate certificate issuer at this point forget it
922
0
    return 0;
923
0
  }
924
925
  // Check cert for matching CRL distribution points
926
0
  if (crl_crldp_check(x, crl, crl_score)) {
927
0
    crl_score |= CRL_SCORE_SCOPE;
928
0
  }
929
930
0
  return crl_score;
931
0
}
932
933
static int crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
934
0
                          int *pcrl_score) {
935
0
  X509 *crl_issuer = nullptr;
936
0
  X509_NAME *cnm = X509_CRL_get_issuer(crl);
937
0
  int cidx = ctx->error_depth;
938
939
0
  if ((size_t)cidx != sk_X509_num(ctx->chain) - 1) {
940
0
    cidx++;
941
0
  }
942
943
0
  crl_issuer = sk_X509_value(ctx->chain, cidx);
944
945
0
  if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
946
0
    *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
947
0
    *pissuer = crl_issuer;
948
0
    return 1;
949
0
  }
950
951
0
  for (cidx++; cidx < (int)sk_X509_num(ctx->chain); cidx++) {
952
0
    crl_issuer = sk_X509_value(ctx->chain, cidx);
953
0
    if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm)) {
954
0
      continue;
955
0
    }
956
0
    if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
957
0
      *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
958
0
      *pissuer = crl_issuer;
959
0
      return 1;
960
0
    }
961
0
  }
962
963
0
  return 0;
964
0
}
965
966
// Check for match between two dist point names: three separate cases. 1.
967
// Both are relative names and compare X509_NAME types. 2. One full, one
968
// relative. Compare X509_NAME to GENERAL_NAMES. 3. Both are full names and
969
// compare two GENERAL_NAMES. 4. One is NULL: automatic match.
970
0
static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b) {
971
0
  X509_NAME *nm = nullptr;
972
0
  GENERAL_NAMES *gens = nullptr;
973
0
  GENERAL_NAME *gena, *genb;
974
0
  size_t i, j;
975
0
  if (!a || !b) {
976
0
    return 1;
977
0
  }
978
0
  if (a->type == 1) {
979
0
    if (!a->dpname) {
980
0
      return 0;
981
0
    }
982
    // Case 1: two X509_NAME
983
0
    if (b->type == 1) {
984
0
      if (!b->dpname) {
985
0
        return 0;
986
0
      }
987
0
      if (!X509_NAME_cmp(a->dpname, b->dpname)) {
988
0
        return 1;
989
0
      } else {
990
0
        return 0;
991
0
      }
992
0
    }
993
    // Case 2: set name and GENERAL_NAMES appropriately
994
0
    nm = a->dpname;
995
0
    gens = b->name.fullname;
996
0
  } else if (b->type == 1) {
997
0
    if (!b->dpname) {
998
0
      return 0;
999
0
    }
1000
    // Case 2: set name and GENERAL_NAMES appropriately
1001
0
    gens = a->name.fullname;
1002
0
    nm = b->dpname;
1003
0
  }
1004
1005
  // Handle case 2 with one GENERAL_NAMES and one X509_NAME
1006
0
  if (nm) {
1007
0
    for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1008
0
      gena = sk_GENERAL_NAME_value(gens, i);
1009
0
      if (gena->type != GEN_DIRNAME) {
1010
0
        continue;
1011
0
      }
1012
0
      if (!X509_NAME_cmp(nm, gena->d.directoryName)) {
1013
0
        return 1;
1014
0
      }
1015
0
    }
1016
0
    return 0;
1017
0
  }
1018
1019
  // Else case 3: two GENERAL_NAMES
1020
1021
0
  for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1022
0
    gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1023
0
    for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1024
0
      genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1025
0
      if (!GENERAL_NAME_cmp(gena, genb)) {
1026
0
        return 1;
1027
0
      }
1028
0
    }
1029
0
  }
1030
1031
0
  return 0;
1032
0
}
1033
1034
// Check CRLDP and IDP
1035
0
static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score) {
1036
0
  auto *impl = FromOpaque(x);
1037
  // TODO(bbe): crbug.com/409778435 Make tests for the corner cases we hit
1038
  // here so that we stay correct for RFC 5280 6.3.3 steps b.1 and b.2
1039
0
  if (crl->idp_flags & IDP_ONLYATTR) {
1040
0
    return 0;
1041
0
  }
1042
0
  if (impl->ex_flags & EXFLAG_CA) {
1043
0
    if (crl->idp_flags & IDP_ONLYUSER) {
1044
0
      return 0;
1045
0
    }
1046
0
  } else {
1047
0
    if (crl->idp_flags & IDP_ONLYCA) {
1048
0
      return 0;
1049
0
    }
1050
0
  }
1051
0
  for (size_t i = 0; i < sk_DIST_POINT_num(impl->crldp.get()); i++) {
1052
0
    DIST_POINT *dp = sk_DIST_POINT_value(impl->crldp.get(), i);
1053
    // Skip distribution points with a reasons field or a CRL issuer:
1054
    //
1055
    // We do not support CRLs partitioned by reason code. RFC 5280 requires CAs
1056
    // include at least one DistributionPoint that covers all reasons.
1057
    //
1058
    // We also do not support indirect CRLs, and a CRL issuer can only match
1059
    // indirect CRLs (RFC 5280, section 6.3.3, step b.1).
1060
0
    if (dp->reasons != nullptr || dp->CRLissuer != nullptr) {
1061
0
      continue;
1062
0
    }
1063
    // At this point we have already checked that the CRL issuer matches
1064
    // the certificate issuer (and set CRL_SCORE_ISSUER_NAME);
1065
1066
    // RFC 5280 Section 6.3.3 step b.2
1067
0
    if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)){
1068
0
      return 1;
1069
0
    }
1070
0
  }
1071
1072
  // If the CRL does not specify an issuing distribution point, allow it to
1073
  // match anything.
1074
  //
1075
  // TODO(davidben): Does this match RFC 5280? It's hard to follow because RFC
1076
  // 5280 starts from distribution points, while this starts from CRLs.
1077
0
  return !crl->idp || !crl->idp->distpoint;
1078
0
}
1079
1080
// Retrieve CRL corresponding to current certificate.
1081
0
static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x) {
1082
0
  X509 *issuer = nullptr;
1083
0
  int crl_score = 0;
1084
0
  X509_CRL *crl = nullptr;
1085
0
  STACK_OF(X509_CRL) *skcrl = nullptr;
1086
0
  if (get_crl_sk(ctx, &crl, &issuer, &crl_score, ctx->crls)) {
1087
0
    goto done;
1088
0
  }
1089
1090
  // Lookup CRLs from store
1091
0
  skcrl = X509_STORE_CTX_get1_crls(ctx, X509_get_issuer_name(x));
1092
1093
  // If no CRLs found and a near match from get_crl_sk use that
1094
0
  if (!skcrl && crl) {
1095
0
    goto done;
1096
0
  }
1097
1098
0
  get_crl_sk(ctx, &crl, &issuer, &crl_score, skcrl);
1099
1100
0
  sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1101
1102
0
done:
1103
  // If we got any kind of CRL use it and return success
1104
0
  if (crl) {
1105
0
    ctx->current_crl_issuer = issuer;
1106
0
    ctx->current_crl_score = crl_score;
1107
0
    *pcrl = crl;
1108
0
    return 1;
1109
0
  }
1110
1111
0
  return 0;
1112
0
}
1113
1114
// Check CRL validity
1115
0
static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) {
1116
0
  X509Impl *issuer = nullptr;
1117
0
  int cnum = ctx->error_depth;
1118
0
  int chnum = (int)sk_X509_num(ctx->chain) - 1;
1119
  // If we have an alternative CRL issuer cert use that. Otherwise, it is the
1120
  // issuer of the current certificate.
1121
0
  if (ctx->current_crl_issuer) {
1122
0
    issuer = FromOpaque(ctx->current_crl_issuer);
1123
0
  } else if (cnum < chnum) {
1124
0
    issuer = FromOpaque(sk_X509_value(ctx->chain, cnum + 1));
1125
0
  } else {
1126
0
    issuer = FromOpaque(sk_X509_value(ctx->chain, chnum));
1127
    // If not self signed, can't check signature
1128
0
    if (!x509_check_issued_with_callback(ctx, issuer, issuer)) {
1129
0
      ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1130
0
      if (!call_verify_cb(0, ctx)) {
1131
0
        return 0;
1132
0
      }
1133
0
    }
1134
0
  }
1135
1136
0
  if (issuer) {
1137
    // Check for cRLSign bit if keyUsage present
1138
0
    if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1139
0
        !(issuer->ex_kusage & X509v3_KU_CRL_SIGN)) {
1140
0
      ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1141
0
      if (!call_verify_cb(0, ctx)) {
1142
0
        return 0;
1143
0
      }
1144
0
    }
1145
1146
0
    if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
1147
0
      ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1148
0
      if (!call_verify_cb(0, ctx)) {
1149
0
        return 0;
1150
0
      }
1151
0
    }
1152
1153
0
    if (crl->idp_flags & IDP_INVALID) {
1154
0
      ctx->error = X509_V_ERR_INVALID_EXTENSION;
1155
0
      if (!call_verify_cb(0, ctx)) {
1156
0
        return 0;
1157
0
      }
1158
0
    }
1159
1160
0
    if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1161
0
      if (!check_crl_time(ctx, crl, 1)) {
1162
0
        return 0;
1163
0
      }
1164
0
    }
1165
1166
    // Attempt to get issuer certificate public key
1167
0
    EVP_PKEY *ikey = X509_get0_pubkey(issuer);
1168
0
    if (!ikey) {
1169
0
      ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1170
0
      if (!call_verify_cb(0, ctx)) {
1171
0
        return 0;
1172
0
      }
1173
0
    } else {
1174
      // Verify CRL signature
1175
0
      if (X509_CRL_verify(crl, ikey) <= 0) {
1176
0
        ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1177
0
        if (!call_verify_cb(0, ctx)) {
1178
0
          return 0;
1179
0
        }
1180
0
      }
1181
0
    }
1182
0
  }
1183
1184
0
  return 1;
1185
0
}
1186
1187
// Check certificate against CRL
1188
0
static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x) {
1189
  // The rules changed for this... previously if a CRL contained unhandled
1190
  // critical extensions it could still be used to indicate a certificate
1191
  // was revoked. This has since been changed since critical extension can
1192
  // change the meaning of CRL entries.
1193
0
  if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) &&
1194
0
      (crl->flags & EXFLAG_CRITICAL)) {
1195
0
    ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
1196
0
    if (!call_verify_cb(0, ctx)) {
1197
0
      return 0;
1198
0
    }
1199
0
  }
1200
  // Look for serial number of certificate in CRL.
1201
0
  X509_REVOKED *rev;
1202
0
  if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1203
0
    ctx->error = X509_V_ERR_CERT_REVOKED;
1204
0
    if (!call_verify_cb(0, ctx)) {
1205
0
      return 0;
1206
0
    }
1207
0
  }
1208
1209
0
  return 1;
1210
0
}
1211
1212
0
static int check_policy(X509_STORE_CTX *ctx) {
1213
0
  X509 *current_cert = nullptr;
1214
0
  int ret = X509_policy_check(ctx->chain, ctx->param->policies,
1215
0
                              ctx->param->flags, &current_cert);
1216
0
  if (ret != X509_V_OK) {
1217
0
    ctx->current_cert = current_cert;
1218
0
    ctx->error = ret;
1219
0
    if (ret == X509_V_ERR_OUT_OF_MEM) {
1220
0
      return 0;
1221
0
    }
1222
0
    return call_verify_cb(0, ctx);
1223
0
  }
1224
1225
0
  return 1;
1226
0
}
1227
1228
0
static int check_cert_time(X509_STORE_CTX *ctx, X509 *x) {
1229
0
  if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) {
1230
0
    return 1;
1231
0
  }
1232
1233
0
  int64_t ptime;
1234
0
  if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) {
1235
0
    ptime = ctx->param->check_time;
1236
0
  } else {
1237
0
    ptime = time(nullptr);
1238
0
  }
1239
1240
0
  int i = X509_cmp_time_posix(X509_get_notBefore(x), ptime);
1241
0
  if (i == 0) {
1242
0
    ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
1243
0
    ctx->current_cert = x;
1244
0
    if (!call_verify_cb(0, ctx)) {
1245
0
      return 0;
1246
0
    }
1247
0
  }
1248
1249
0
  if (i > 0) {
1250
0
    ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
1251
0
    ctx->current_cert = x;
1252
0
    if (!call_verify_cb(0, ctx)) {
1253
0
      return 0;
1254
0
    }
1255
0
  }
1256
1257
0
  i = X509_cmp_time_posix(X509_get_notAfter(x), ptime);
1258
0
  if (i == 0) {
1259
0
    ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
1260
0
    ctx->current_cert = x;
1261
0
    if (!call_verify_cb(0, ctx)) {
1262
0
      return 0;
1263
0
    }
1264
0
  }
1265
1266
0
  if (i < 0) {
1267
0
    ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
1268
0
    ctx->current_cert = x;
1269
0
    if (!call_verify_cb(0, ctx)) {
1270
0
      return 0;
1271
0
    }
1272
0
  }
1273
1274
0
  return 1;
1275
0
}
1276
1277
0
static int internal_verify(X509_STORE_CTX *ctx) {
1278
  // TODO(davidben): This logic is incredibly confusing. Rewrite this:
1279
  //
1280
  // First, don't allow the verify callback to suppress
1281
  // X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY, which will simplify the
1282
  // signature check. Then replace jumping into the middle of the loop. It's
1283
  // trying to ensure that all certificates see `check_cert_time`, then checking
1284
  // the root's self signature when requested, but not breaking partial chains
1285
  // in the process.
1286
0
  int n = (int)sk_X509_num(ctx->chain);
1287
0
  ctx->error_depth = n - 1;
1288
0
  n--;
1289
0
  X509 *xi = sk_X509_value(ctx->chain, n);
1290
0
  X509 *xs;
1291
0
  if (x509_check_issued_with_callback(ctx, xi, xi)) {
1292
0
    xs = xi;
1293
0
  } else {
1294
0
    if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
1295
0
      xs = xi;
1296
0
      goto check_cert;
1297
0
    }
1298
0
    if (n <= 0) {
1299
0
      ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
1300
0
      ctx->current_cert = xi;
1301
0
      return call_verify_cb(0, ctx);
1302
0
    }
1303
0
    n--;
1304
0
    ctx->error_depth = n;
1305
0
    xs = sk_X509_value(ctx->chain, n);
1306
0
  }
1307
1308
  //      ctx->error=0;  not needed
1309
0
  while (n >= 0) {
1310
0
    ctx->error_depth = n;
1311
1312
    // Skip signature check for self signed certificates unless
1313
    // explicitly asked for. It doesn't add any security and just wastes
1314
    // time.
1315
0
    if (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)) {
1316
0
      EVP_PKEY *pkey = X509_get0_pubkey(xi);
1317
0
      if (pkey == nullptr) {
1318
0
        ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1319
0
        ctx->current_cert = xi;
1320
0
        if (!call_verify_cb(0, ctx)) {
1321
0
          return 0;
1322
0
        }
1323
0
      } else if (X509_verify(xs, pkey) <= 0) {
1324
0
        ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
1325
0
        ctx->current_cert = xs;
1326
0
        if (!call_verify_cb(0, ctx)) {
1327
0
          return 0;
1328
0
        }
1329
0
      }
1330
0
    }
1331
1332
0
  check_cert:
1333
0
    if (!check_cert_time(ctx, xs)) {
1334
0
      return 0;
1335
0
    }
1336
1337
    // The last error (if any) is still in the error value
1338
0
    ctx->current_cert = xs;
1339
0
    if (!call_verify_cb(1, ctx)) {
1340
0
      return 0;
1341
0
    }
1342
1343
0
    n--;
1344
0
    if (n >= 0) {
1345
0
      xi = xs;
1346
0
      xs = sk_X509_value(ctx->chain, n);
1347
0
    }
1348
0
  }
1349
1350
0
  return 1;
1351
0
}
1352
1353
0
int X509_cmp_current_time(const ASN1_TIME *ctm) {
1354
0
  return X509_cmp_time_posix(ctm, time(nullptr));
1355
0
}
1356
1357
0
int X509_cmp_time(const ASN1_TIME *ctm, const time_t *cmp_time) {
1358
0
  int64_t compare_time = (cmp_time == nullptr) ? time(nullptr) : *cmp_time;
1359
0
  return X509_cmp_time_posix(ctm, compare_time);
1360
0
}
1361
1362
0
int X509_cmp_time_posix(const ASN1_TIME *ctm, int64_t cmp_time) {
1363
0
  int64_t ctm_time;
1364
0
  if (!ASN1_TIME_to_posix(ctm, &ctm_time)) {
1365
0
    return 0;
1366
0
  }
1367
  // The return value 0 is reserved for errors.
1368
0
  return (ctm_time - cmp_time <= 0) ? -1 : 1;
1369
0
}
1370
1371
0
ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long offset_sec) {
1372
0
  return X509_time_adj(s, offset_sec, nullptr);
1373
0
}
1374
1375
0
ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, const time_t *in_tm) {
1376
0
  return X509_time_adj_ex(s, 0, offset_sec, in_tm);
1377
0
}
1378
1379
ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, int offset_day, long offset_sec,
1380
0
                            const time_t *in_tm) {
1381
0
  int64_t t = 0;
1382
1383
0
  if (in_tm) {
1384
0
    t = *in_tm;
1385
0
  } else {
1386
0
    t = time(nullptr);
1387
0
  }
1388
1389
0
  return ASN1_TIME_adj(s, t, offset_day, offset_sec);
1390
0
}
1391
1392
int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
1393
                                    CRYPTO_EX_unused *unused,
1394
                                    CRYPTO_EX_dup *dup_unused,
1395
0
                                    CRYPTO_EX_free *free_func) {
1396
0
  return CRYPTO_get_ex_new_index_ex(&g_ex_data_class, argl, argp, free_func);
1397
0
}
1398
1399
8.16k
int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data) {
1400
8.16k
  return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
1401
8.16k
}
1402
1403
0
void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx) {
1404
0
  return CRYPTO_get_ex_data(&ctx->ex_data, idx);
1405
0
}
1406
1407
8.16k
int X509_STORE_CTX_get_error(const X509_STORE_CTX *ctx) { return ctx->error; }
1408
1409
0
void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err) {
1410
0
  ctx->error = err;
1411
0
}
1412
1413
0
int X509_STORE_CTX_get_error_depth(const X509_STORE_CTX *ctx) {
1414
0
  return ctx->error_depth;
1415
0
}
1416
1417
0
X509 *X509_STORE_CTX_get_current_cert(const X509_STORE_CTX *ctx) {
1418
0
  return ctx->current_cert;
1419
0
}
1420
1421
0
STACK_OF(X509) *X509_STORE_CTX_get_chain(const X509_STORE_CTX *ctx) {
1422
0
  return ctx->chain;
1423
0
}
1424
1425
0
STACK_OF(X509) *X509_STORE_CTX_get0_chain(const X509_STORE_CTX *ctx) {
1426
0
  return ctx->chain;
1427
0
}
1428
1429
0
STACK_OF(X509) *X509_STORE_CTX_get1_chain(const X509_STORE_CTX *ctx) {
1430
0
  if (!ctx->chain) {
1431
0
    return nullptr;
1432
0
  }
1433
0
  return X509_chain_up_ref(ctx->chain);
1434
0
}
1435
1436
0
X509_CRL *X509_STORE_CTX_get0_current_crl(const X509_STORE_CTX *ctx) {
1437
0
  return ctx->current_crl;
1438
0
}
1439
1440
0
X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(const X509_STORE_CTX *ctx) {
1441
  // In OpenSSL, an `X509_STORE_CTX` sometimes has a parent context during CRL
1442
  // path validation for indirect CRLs. We require the CRL to be issued
1443
  // somewhere along the certificate path, so this is always NULL.
1444
0
  return nullptr;
1445
0
}
1446
1447
0
void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) {
1448
0
  ctx->untrusted = sk;
1449
0
}
1450
1451
0
STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(const X509_STORE_CTX *ctx) {
1452
0
  return ctx->untrusted;
1453
0
}
1454
1455
0
void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk) {
1456
0
  ctx->crls = sk;
1457
0
}
1458
1459
0
int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose) {
1460
  // If `purpose` is zero, this function historically silently did nothing.
1461
0
  if (purpose == 0) {
1462
0
    return 1;
1463
0
  }
1464
1465
0
  const X509_PURPOSE *pobj = X509_PURPOSE_get0(purpose);
1466
0
  if (pobj == nullptr) {
1467
0
    OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID);
1468
0
    return 0;
1469
0
  }
1470
1471
0
  int trust = X509_PURPOSE_get_trust(pobj);
1472
0
  if (!X509_STORE_CTX_set_trust(ctx, trust)) {
1473
0
    return 0;
1474
0
  }
1475
1476
0
  if (ctx->param->purpose == 0) {
1477
0
    ctx->param->purpose = purpose;
1478
0
  }
1479
0
  return 1;
1480
0
}
1481
1482
0
int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust) {
1483
  // If `trust` is zero, this function historically silently did nothing.
1484
0
  if (trust == 0) {
1485
0
    return 1;
1486
0
  }
1487
1488
0
  if (!X509_is_valid_trust_id(trust)) {
1489
0
    OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_TRUST_ID);
1490
0
    return 0;
1491
0
  }
1492
1493
0
  if (ctx->param->trust == 0) {
1494
0
    ctx->param->trust = trust;
1495
0
  }
1496
0
  return 1;
1497
0
}
1498
1499
8.16k
X509_STORE_CTX *X509_STORE_CTX_new() { return New<X509_STORE_CTX>(); }
1500
1501
8.16k
void X509_STORE_CTX_free(X509_STORE_CTX *ctx) {
1502
8.16k
  if (ctx == nullptr) {
1503
0
    return;
1504
0
  }
1505
8.16k
  X509_STORE_CTX_cleanup(ctx);
1506
8.16k
  Delete(ctx);
1507
8.16k
}
1508
1509
int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
1510
8.16k
                        STACK_OF(X509) *chain) {
1511
8.16k
  X509_STORE_CTX_cleanup(ctx);
1512
1513
8.16k
  ctx->ctx = store;
1514
8.16k
  ctx->cert = x509;
1515
8.16k
  ctx->untrusted = chain;
1516
1517
8.16k
  CRYPTO_new_ex_data(&ctx->ex_data);
1518
1519
8.16k
  if (store == nullptr) {
1520
0
    OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
1521
0
    goto err;
1522
0
  }
1523
1524
8.16k
  ctx->param = X509_VERIFY_PARAM_new();
1525
8.16k
  if (!ctx->param) {
1526
0
    goto err;
1527
0
  }
1528
1529
8.16k
  {
1530
    // Inherit callbacks and flags from X509_STORE.
1531
1532
8.16k
    auto *store_impl = FromOpaque(store);
1533
8.16k
    ctx->verify_cb = store_impl->verify_cb;
1534
1535
8.16k
    if (!X509_VERIFY_PARAM_inherit(ctx->param, store_impl->param.get()) ||
1536
8.16k
        !X509_VERIFY_PARAM_inherit(ctx->param,
1537
8.16k
                                   X509_VERIFY_PARAM_lookup("default"))) {
1538
0
      goto err;
1539
0
    }
1540
1541
8.16k
    if (store_impl->verify_cb) {
1542
0
      ctx->verify_cb = store_impl->verify_cb;
1543
8.16k
    } else {
1544
8.16k
      ctx->verify_cb = null_callback;
1545
8.16k
    }
1546
8.16k
  }
1547
1548
0
  return 1;
1549
1550
0
err:
1551
0
  X509_STORE_CTX_cleanup(ctx);
1552
0
  return 0;
1553
8.16k
}
1554
1555
// Set alternative lookup method: just a STACK of trusted certificates. This
1556
// avoids X509_STORE nastiness where it isn't needed.
1557
1558
void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx,
1559
0
                                       STACK_OF(X509) *sk) {
1560
0
  ctx->trusted_stack = sk;
1561
0
}
1562
1563
0
void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) {
1564
0
  X509_STORE_CTX_set0_trusted_stack(ctx, sk);
1565
0
}
1566
1567
16.3k
void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx) {
1568
16.3k
  CRYPTO_free_ex_data(&g_ex_data_class, &ctx->ex_data);
1569
16.3k
  X509_VERIFY_PARAM_free(ctx->param);
1570
16.3k
  sk_X509_pop_free(ctx->chain, X509_free);
1571
16.3k
  OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX));
1572
16.3k
}
1573
1574
0
void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth) {
1575
0
  X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1576
0
}
1577
1578
0
void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags) {
1579
0
  X509_VERIFY_PARAM_set_flags(ctx->param, flags);
1580
0
}
1581
1582
void X509_STORE_CTX_set_time_posix(X509_STORE_CTX *ctx, unsigned long flags,
1583
0
                                   int64_t t) {
1584
0
  X509_VERIFY_PARAM_set_time_posix(ctx->param, t);
1585
0
}
1586
1587
void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
1588
0
                             time_t t) {
1589
0
  X509_STORE_CTX_set_time_posix(ctx, flags, t);
1590
0
}
1591
1592
0
X509 *X509_STORE_CTX_get0_cert(const X509_STORE_CTX *ctx) { return ctx->cert; }
1593
1594
void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
1595
0
                                  int (*verify_cb)(int, X509_STORE_CTX *)) {
1596
0
  ctx->verify_cb = verify_cb;
1597
0
}
1598
1599
8.16k
int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name) {
1600
8.16k
  const X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_lookup(name);
1601
8.16k
  if (!param) {
1602
0
    return 0;
1603
0
  }
1604
8.16k
  return X509_VERIFY_PARAM_inherit(ctx->param, param);
1605
8.16k
}
1606
1607
8.16k
X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx) {
1608
8.16k
  return ctx->param;
1609
8.16k
}
1610
1611
0
void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param) {
1612
0
  if (ctx->param) {
1613
0
    X509_VERIFY_PARAM_free(ctx->param);
1614
0
  }
1615
0
  ctx->param = param;
1616
0
}