Coverage Report

Created: 2026-03-19 06:22

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 pathlen if not self issued
503
0
    if (i > 1 && !(x->ex_flags & EXFLAG_SI) && x->ex_pathlen != -1 &&
504
0
        plen > x->ex_pathlen + 1) {
505
0
      ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
506
0
      ctx->error_depth = i;
507
0
      ctx->current_cert = x;
508
0
      if (!call_verify_cb(0, ctx)) {
509
0
        return 0;
510
0
      }
511
0
    }
512
    // Increment path length if not self issued
513
0
    if (!(x->ex_flags & EXFLAG_SI)) {
514
0
      plen++;
515
0
    }
516
0
  }
517
518
0
  return 1;
519
0
}
520
521
0
static int reject_dns_name_in_common_name(X509 *x509) {
522
0
  const X509_NAME *name = X509_get_subject_name(x509);
523
0
  int i = -1;
524
0
  for (;;) {
525
0
    i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
526
0
    if (i == -1) {
527
0
      return X509_V_OK;
528
0
    }
529
530
0
    const X509_NAME_ENTRY *entry = X509_NAME_get_entry(name, i);
531
0
    const ASN1_STRING *common_name = X509_NAME_ENTRY_get_data(entry);
532
0
    unsigned char *idval;
533
0
    int idlen = ASN1_STRING_to_UTF8(&idval, common_name);
534
0
    if (idlen < 0) {
535
0
      return X509_V_ERR_OUT_OF_MEM;
536
0
    }
537
    // Only process attributes that look like host names. Note it is
538
    // important that this check be mirrored in |X509_check_host|.
539
0
    int looks_like_dns = x509v3_looks_like_dns_name(idval, (size_t)idlen);
540
0
    OPENSSL_free(idval);
541
0
    if (looks_like_dns) {
542
0
      return X509_V_ERR_NAME_CONSTRAINTS_WITHOUT_SANS;
543
0
    }
544
0
  }
545
0
}
546
547
0
static int check_name_constraints(X509_STORE_CTX *ctx) {
548
0
  int i, j, rv;
549
0
  int has_name_constraints = 0;
550
  // Check name constraints for all certificates
551
0
  for (i = (int)sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
552
0
    X509Impl *x = FromOpaque(sk_X509_value(ctx->chain, i));
553
    // Ignore self issued certs unless last in chain
554
0
    if (i && (x->ex_flags & EXFLAG_SI)) {
555
0
      continue;
556
0
    }
557
    // Check against constraints for all certificates higher in chain
558
    // including trust anchor. Trust anchor not strictly speaking needed
559
    // but if it includes constraints it is to be assumed it expects them
560
    // to be obeyed.
561
0
    for (j = (int)sk_X509_num(ctx->chain) - 1; j > i; j--) {
562
0
      NAME_CONSTRAINTS *nc = FromOpaque(sk_X509_value(ctx->chain, j))->nc;
563
0
      if (nc) {
564
0
        has_name_constraints = 1;
565
0
        rv = NAME_CONSTRAINTS_check(x, nc);
566
0
        switch (rv) {
567
0
          case X509_V_OK:
568
0
            continue;
569
0
          case X509_V_ERR_OUT_OF_MEM:
570
0
            ctx->error = rv;
571
0
            return 0;
572
0
          default:
573
0
            ctx->error = rv;
574
0
            ctx->error_depth = i;
575
0
            ctx->current_cert = x;
576
0
            if (!call_verify_cb(0, ctx)) {
577
0
              return 0;
578
0
            }
579
0
            break;
580
0
        }
581
0
      }
582
0
    }
583
0
  }
584
585
  // Name constraints do not match against the common name, but
586
  // |X509_check_host| still implements the legacy behavior where, on
587
  // certificates lacking a SAN list, DNS-like names in the common name are
588
  // checked instead.
589
  //
590
  // While we could apply the name constraints to the common name, name
591
  // constraints are rare enough that can hold such certificates to a higher
592
  // standard. Note this does not make "DNS-like" heuristic failures any
593
  // worse. A decorative common-name misidentified as a DNS name would fail
594
  // the name constraint anyway.
595
0
  X509Impl *leaf = FromOpaque(sk_X509_value(ctx->chain, 0));
596
0
  if (has_name_constraints && leaf->altname == nullptr) {
597
0
    rv = reject_dns_name_in_common_name(leaf);
598
0
    switch (rv) {
599
0
      case X509_V_OK:
600
0
        break;
601
0
      case X509_V_ERR_OUT_OF_MEM:
602
0
        ctx->error = rv;
603
0
        return 0;
604
0
      default:
605
0
        ctx->error = rv;
606
0
        ctx->error_depth = i;
607
0
        ctx->current_cert = leaf;
608
0
        if (!call_verify_cb(0, ctx)) {
609
0
          return 0;
610
0
        }
611
0
        break;
612
0
    }
613
0
  }
614
615
0
  return 1;
616
0
}
617
618
0
static int check_id_error(X509_STORE_CTX *ctx, int errcode) {
619
0
  ctx->error = errcode;
620
0
  ctx->current_cert = ctx->cert;
621
0
  ctx->error_depth = 0;
622
0
  return call_verify_cb(0, ctx);
623
0
}
624
625
0
static int check_hosts(X509 *x, X509_VERIFY_PARAM *param) {
626
0
  size_t i;
627
0
  size_t n = sk_OPENSSL_STRING_num(param->hosts);
628
0
  char *name;
629
630
0
  for (i = 0; i < n; ++i) {
631
0
    name = sk_OPENSSL_STRING_value(param->hosts, i);
632
0
    if (X509_check_host(x, name, strlen(name), param->hostflags, nullptr) > 0) {
633
0
      return 1;
634
0
    }
635
0
  }
636
0
  return n == 0;
637
0
}
638
639
0
static int check_id(X509_STORE_CTX *ctx) {
640
0
  X509_VERIFY_PARAM *vpm = ctx->param;
641
0
  X509 *x = ctx->cert;
642
0
  if (vpm->poison) {
643
0
    if (!check_id_error(ctx, X509_V_ERR_INVALID_CALL)) {
644
0
      return 0;
645
0
    }
646
0
  }
647
0
  if (vpm->hosts && check_hosts(x, vpm) <= 0) {
648
0
    if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH)) {
649
0
      return 0;
650
0
    }
651
0
  }
652
0
  if (vpm->email && X509_check_email(x, vpm->email, vpm->emaillen, 0) <= 0) {
653
0
    if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH)) {
654
0
      return 0;
655
0
    }
656
0
  }
657
0
  if (vpm->ip && X509_check_ip(x, vpm->ip, vpm->iplen, 0) <= 0) {
658
0
    if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH)) {
659
0
      return 0;
660
0
    }
661
0
  }
662
0
  return 1;
663
0
}
664
665
0
static int check_trust(X509_STORE_CTX *ctx) {
666
0
  X509 *x = nullptr;
667
  // Check all trusted certificates in chain
668
0
  for (size_t i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) {
669
0
    x = sk_X509_value(ctx->chain, i);
670
0
    int trust = X509_check_trust(x, ctx->param->trust, 0);
671
    // If explicitly trusted return trusted
672
0
    if (trust == X509_TRUST_TRUSTED) {
673
0
      return X509_TRUST_TRUSTED;
674
0
    }
675
    // If explicitly rejected notify callback and reject if not
676
    // overridden.
677
0
    if (trust == X509_TRUST_REJECTED) {
678
0
      ctx->error_depth = (int)i;
679
0
      ctx->current_cert = x;
680
0
      ctx->error = X509_V_ERR_CERT_REJECTED;
681
0
      if (!call_verify_cb(0, ctx)) {
682
0
        return X509_TRUST_REJECTED;
683
0
      }
684
0
    }
685
0
  }
686
  // If we accept partial chains and have at least one trusted certificate
687
  // return success.
688
0
  if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
689
0
    X509 *mx;
690
0
    if (ctx->last_untrusted < (int)sk_X509_num(ctx->chain)) {
691
0
      return X509_TRUST_TRUSTED;
692
0
    }
693
0
    x = sk_X509_value(ctx->chain, 0);
694
0
    mx = lookup_cert_match(ctx, x);
695
0
    if (mx) {
696
0
      (void)sk_X509_set(ctx->chain, 0, mx);
697
0
      X509_free(x);
698
0
      ctx->last_untrusted = 0;
699
0
      return X509_TRUST_TRUSTED;
700
0
    }
701
0
  }
702
703
  // If no trusted certs in chain at all return untrusted and allow
704
  // standard (no issuer cert) etc errors to be indicated.
705
0
  return X509_TRUST_UNTRUSTED;
706
0
}
707
708
0
static int check_revocation(X509_STORE_CTX *ctx) {
709
0
  if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK)) {
710
0
    return 1;
711
0
  }
712
0
  int last;
713
0
  if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL) {
714
0
    last = (int)sk_X509_num(ctx->chain) - 1;
715
0
  } else {
716
0
    last = 0;
717
0
  }
718
0
  for (int i = 0; i <= last; i++) {
719
0
    ctx->error_depth = i;
720
0
    if (!check_cert(ctx)) {
721
0
      return 0;
722
0
    }
723
0
  }
724
0
  return 1;
725
0
}
726
727
0
static int check_cert(X509_STORE_CTX *ctx) {
728
0
  X509_CRL *crl = nullptr;
729
0
  int ok = 0, cnum = ctx->error_depth;
730
0
  X509 *x = sk_X509_value(ctx->chain, cnum);
731
0
  ctx->current_cert = x;
732
0
  ctx->current_crl_issuer = nullptr;
733
0
  ctx->current_crl_score = 0;
734
735
  // Try to retrieve the relevant CRL. Note that |get_crl| sets
736
  // |current_crl_issuer| and |current_crl_score|, which |check_crl| then reads.
737
  //
738
  // TODO(davidben): The awkward internal calling convention is a historical
739
  // artifact of when these functions were user-overridable callbacks, even
740
  // though there was no way to set them correctly. These callbacks have since
741
  // been removed, so we can pass input and output parameters more directly.
742
0
  if (!get_crl(ctx, &crl, x)) {
743
0
    ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
744
0
    ok = call_verify_cb(0, ctx);
745
0
    goto err;
746
0
  }
747
748
0
  ctx->current_crl = crl;
749
0
  if (!check_crl(ctx, crl) ||  //
750
0
      !cert_crl(ctx, crl, x)) {
751
0
    goto err;
752
0
  }
753
754
0
  ok = 1;
755
756
0
err:
757
0
  X509_CRL_free(crl);
758
0
  ctx->current_crl = nullptr;
759
0
  return ok;
760
0
}
761
762
// Check CRL times against values in X509_STORE_CTX
763
0
static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) {
764
0
  if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) {
765
0
    return 1;
766
0
  }
767
768
0
  if (notify) {
769
0
    ctx->current_crl = crl;
770
0
  }
771
0
  int64_t ptime;
772
0
  if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) {
773
0
    ptime = ctx->param->check_time;
774
0
  } else {
775
0
    ptime = time(nullptr);
776
0
  }
777
778
0
  int i = X509_cmp_time_posix(X509_CRL_get0_lastUpdate(crl), ptime);
779
0
  if (i == 0) {
780
0
    if (!notify) {
781
0
      return 0;
782
0
    }
783
0
    ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
784
0
    if (!call_verify_cb(0, ctx)) {
785
0
      return 0;
786
0
    }
787
0
  }
788
789
0
  if (i > 0) {
790
0
    if (!notify) {
791
0
      return 0;
792
0
    }
793
0
    ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
794
0
    if (!call_verify_cb(0, ctx)) {
795
0
      return 0;
796
0
    }
797
0
  }
798
799
0
  if (X509_CRL_get0_nextUpdate(crl)) {
800
0
    i = X509_cmp_time_posix(X509_CRL_get0_nextUpdate(crl), ptime);
801
802
0
    if (i == 0) {
803
0
      if (!notify) {
804
0
        return 0;
805
0
      }
806
0
      ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
807
0
      if (!call_verify_cb(0, ctx)) {
808
0
        return 0;
809
0
      }
810
0
    }
811
0
    if (i < 0) {
812
0
      if (!notify) {
813
0
        return 0;
814
0
      }
815
0
      ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
816
0
      if (!call_verify_cb(0, ctx)) {
817
0
        return 0;
818
0
      }
819
0
    }
820
0
  }
821
822
0
  if (notify) {
823
0
    ctx->current_crl = nullptr;
824
0
  }
825
826
0
  return 1;
827
0
}
828
829
static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 **pissuer,
830
0
                      int *pscore, STACK_OF(X509_CRL) *crls) {
831
0
  int crl_score, best_score = *pscore;
832
0
  X509 *x = ctx->current_cert;
833
0
  X509_CRL *best_crl = nullptr;
834
0
  X509 *crl_issuer = nullptr, *best_crl_issuer = nullptr;
835
836
0
  for (size_t i = 0; i < sk_X509_CRL_num(crls); i++) {
837
0
    X509_CRL *crl = sk_X509_CRL_value(crls, i);
838
0
    crl_score = get_crl_score(ctx, &crl_issuer, crl, x);
839
0
    if (crl_score < best_score || crl_score == 0) {
840
0
      continue;
841
0
    }
842
    // If current CRL is equivalent use it if it is newer
843
0
    if (crl_score == best_score && best_crl != nullptr) {
844
0
      int day, sec;
845
0
      if (ASN1_TIME_diff(&day, &sec, X509_CRL_get0_lastUpdate(best_crl),
846
0
                         X509_CRL_get0_lastUpdate(crl)) == 0) {
847
0
        continue;
848
0
      }
849
      // ASN1_TIME_diff never returns inconsistent signs for |day|
850
      // and |sec|.
851
0
      if (day <= 0 && sec <= 0) {
852
0
        continue;
853
0
      }
854
0
    }
855
0
    best_crl = crl;
856
0
    best_crl_issuer = crl_issuer;
857
0
    best_score = crl_score;
858
0
  }
859
860
0
  if (best_crl) {
861
0
    if (*pcrl) {
862
0
      X509_CRL_free(*pcrl);
863
0
    }
864
0
    *pcrl = best_crl;
865
0
    *pissuer = best_crl_issuer;
866
0
    *pscore = best_score;
867
0
    X509_CRL_up_ref(best_crl);
868
0
  }
869
870
0
  if (best_score >= CRL_SCORE_VALID) {
871
0
    return 1;
872
0
  }
873
874
0
  return 0;
875
0
}
876
877
// For a given CRL return how suitable it is for the supplied certificate
878
// 'x'. The return value is a mask of several criteria. If the issuer is not
879
// the certificate issuer this is returned in *pissuer.
880
static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, X509_CRL *crl,
881
0
                         X509 *x) {
882
0
  int crl_score = 0;
883
884
  // First see if we can reject CRL straight away
885
886
  // Invalid IDP cannot be processed
887
0
  if (crl->idp_flags & IDP_INVALID) {
888
0
    return 0;
889
0
  }
890
  // Reason codes and indirect CRLs are not supported.
891
0
  if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS)) {
892
0
    return 0;
893
0
  }
894
  // We do not support indirect CRLs, so the issuer names must match.
895
0
  if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
896
0
    return 0;
897
0
  }
898
0
  crl_score |= CRL_SCORE_ISSUER_NAME;
899
900
0
  if (!(crl->flags & EXFLAG_CRITICAL)) {
901
0
    crl_score |= CRL_SCORE_NOCRITICAL;
902
0
  }
903
904
  // Check expiry
905
0
  if (check_crl_time(ctx, crl, 0)) {
906
0
    crl_score |= CRL_SCORE_TIME;
907
0
  }
908
909
  // Check authority key ID and locate certificate issuer
910
0
  if (!crl_akid_check(ctx, crl, pissuer, &crl_score)) {
911
    // If we can't locate certificate issuer at this point forget it
912
0
    return 0;
913
0
  }
914
915
  // Check cert for matching CRL distribution points
916
0
  if (crl_crldp_check(x, crl, crl_score)) {
917
0
    crl_score |= CRL_SCORE_SCOPE;
918
0
  }
919
920
0
  return crl_score;
921
0
}
922
923
static int crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
924
0
                          int *pcrl_score) {
925
0
  X509 *crl_issuer = nullptr;
926
0
  X509_NAME *cnm = X509_CRL_get_issuer(crl);
927
0
  int cidx = ctx->error_depth;
928
929
0
  if ((size_t)cidx != sk_X509_num(ctx->chain) - 1) {
930
0
    cidx++;
931
0
  }
932
933
0
  crl_issuer = sk_X509_value(ctx->chain, cidx);
934
935
0
  if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
936
0
    *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
937
0
    *pissuer = crl_issuer;
938
0
    return 1;
939
0
  }
940
941
0
  for (cidx++; cidx < (int)sk_X509_num(ctx->chain); cidx++) {
942
0
    crl_issuer = sk_X509_value(ctx->chain, cidx);
943
0
    if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm)) {
944
0
      continue;
945
0
    }
946
0
    if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
947
0
      *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
948
0
      *pissuer = crl_issuer;
949
0
      return 1;
950
0
    }
951
0
  }
952
953
0
  return 0;
954
0
}
955
956
// Check for match between two dist point names: three separate cases. 1.
957
// Both are relative names and compare X509_NAME types. 2. One full, one
958
// relative. Compare X509_NAME to GENERAL_NAMES. 3. Both are full names and
959
// compare two GENERAL_NAMES. 4. One is NULL: automatic match.
960
0
static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b) {
961
0
  X509_NAME *nm = nullptr;
962
0
  GENERAL_NAMES *gens = nullptr;
963
0
  GENERAL_NAME *gena, *genb;
964
0
  size_t i, j;
965
0
  if (!a || !b) {
966
0
    return 1;
967
0
  }
968
0
  if (a->type == 1) {
969
0
    if (!a->dpname) {
970
0
      return 0;
971
0
    }
972
    // Case 1: two X509_NAME
973
0
    if (b->type == 1) {
974
0
      if (!b->dpname) {
975
0
        return 0;
976
0
      }
977
0
      if (!X509_NAME_cmp(a->dpname, b->dpname)) {
978
0
        return 1;
979
0
      } else {
980
0
        return 0;
981
0
      }
982
0
    }
983
    // Case 2: set name and GENERAL_NAMES appropriately
984
0
    nm = a->dpname;
985
0
    gens = b->name.fullname;
986
0
  } else if (b->type == 1) {
987
0
    if (!b->dpname) {
988
0
      return 0;
989
0
    }
990
    // Case 2: set name and GENERAL_NAMES appropriately
991
0
    gens = a->name.fullname;
992
0
    nm = b->dpname;
993
0
  }
994
995
  // Handle case 2 with one GENERAL_NAMES and one X509_NAME
996
0
  if (nm) {
997
0
    for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
998
0
      gena = sk_GENERAL_NAME_value(gens, i);
999
0
      if (gena->type != GEN_DIRNAME) {
1000
0
        continue;
1001
0
      }
1002
0
      if (!X509_NAME_cmp(nm, gena->d.directoryName)) {
1003
0
        return 1;
1004
0
      }
1005
0
    }
1006
0
    return 0;
1007
0
  }
1008
1009
  // Else case 3: two GENERAL_NAMES
1010
1011
0
  for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1012
0
    gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1013
0
    for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1014
0
      genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1015
0
      if (!GENERAL_NAME_cmp(gena, genb)) {
1016
0
        return 1;
1017
0
      }
1018
0
    }
1019
0
  }
1020
1021
0
  return 0;
1022
0
}
1023
1024
// Check CRLDP and IDP
1025
0
static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score) {
1026
0
  auto *impl = FromOpaque(x);
1027
  // TODO(bbe): crbug.com/409778435 Make tests for the corner cases we hit
1028
  // here so that we stay correct for RFC 5280 6.3.3 steps b.1 and b.2
1029
0
  if (crl->idp_flags & IDP_ONLYATTR) {
1030
0
    return 0;
1031
0
  }
1032
0
  if (impl->ex_flags & EXFLAG_CA) {
1033
0
    if (crl->idp_flags & IDP_ONLYUSER) {
1034
0
      return 0;
1035
0
    }
1036
0
  } else {
1037
0
    if (crl->idp_flags & IDP_ONLYCA) {
1038
0
      return 0;
1039
0
    }
1040
0
  }
1041
0
  for (size_t i = 0; i < sk_DIST_POINT_num(impl->crldp); i++) {
1042
0
    DIST_POINT *dp = sk_DIST_POINT_value(impl->crldp, i);
1043
    // Skip distribution points with a reasons field or a CRL issuer:
1044
    //
1045
    // We do not support CRLs partitioned by reason code. RFC 5280 requires CAs
1046
    // include at least one DistributionPoint that covers all reasons.
1047
    //
1048
    // We also do not support indirect CRLs, and a CRL issuer can only match
1049
    // indirect CRLs (RFC 5280, section 6.3.3, step b.1).
1050
0
    if (dp->reasons != nullptr || dp->CRLissuer != nullptr) {
1051
0
      continue;
1052
0
    }
1053
    // At this point we have already checked that the CRL issuer matches
1054
    // the certificate issuer (and set CRL_SCORE_ISSUER_NAME);
1055
1056
    // RFC 5280 Section 6.3.3 step b.2
1057
0
    if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)){
1058
0
      return 1;
1059
0
    }
1060
0
  }
1061
1062
  // If the CRL does not specify an issuing distribution point, allow it to
1063
  // match anything.
1064
  //
1065
  // TODO(davidben): Does this match RFC 5280? It's hard to follow because RFC
1066
  // 5280 starts from distribution points, while this starts from CRLs.
1067
0
  return !crl->idp || !crl->idp->distpoint;
1068
0
}
1069
1070
// Retrieve CRL corresponding to current certificate.
1071
0
static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x) {
1072
0
  X509 *issuer = nullptr;
1073
0
  int crl_score = 0;
1074
0
  X509_CRL *crl = nullptr;
1075
0
  STACK_OF(X509_CRL) *skcrl = nullptr;
1076
0
  if (get_crl_sk(ctx, &crl, &issuer, &crl_score, ctx->crls)) {
1077
0
    goto done;
1078
0
  }
1079
1080
  // Lookup CRLs from store
1081
0
  skcrl = X509_STORE_CTX_get1_crls(ctx, X509_get_issuer_name(x));
1082
1083
  // If no CRLs found and a near match from get_crl_sk use that
1084
0
  if (!skcrl && crl) {
1085
0
    goto done;
1086
0
  }
1087
1088
0
  get_crl_sk(ctx, &crl, &issuer, &crl_score, skcrl);
1089
1090
0
  sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1091
1092
0
done:
1093
  // If we got any kind of CRL use it and return success
1094
0
  if (crl) {
1095
0
    ctx->current_crl_issuer = issuer;
1096
0
    ctx->current_crl_score = crl_score;
1097
0
    *pcrl = crl;
1098
0
    return 1;
1099
0
  }
1100
1101
0
  return 0;
1102
0
}
1103
1104
// Check CRL validity
1105
0
static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) {
1106
0
  X509Impl *issuer = nullptr;
1107
0
  int cnum = ctx->error_depth;
1108
0
  int chnum = (int)sk_X509_num(ctx->chain) - 1;
1109
  // If we have an alternative CRL issuer cert use that. Otherwise, it is the
1110
  // issuer of the current certificate.
1111
0
  if (ctx->current_crl_issuer) {
1112
0
    issuer = FromOpaque(ctx->current_crl_issuer);
1113
0
  } else if (cnum < chnum) {
1114
0
    issuer = FromOpaque(sk_X509_value(ctx->chain, cnum + 1));
1115
0
  } else {
1116
0
    issuer = FromOpaque(sk_X509_value(ctx->chain, chnum));
1117
    // If not self signed, can't check signature
1118
0
    if (!x509_check_issued_with_callback(ctx, issuer, issuer)) {
1119
0
      ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1120
0
      if (!call_verify_cb(0, ctx)) {
1121
0
        return 0;
1122
0
      }
1123
0
    }
1124
0
  }
1125
1126
0
  if (issuer) {
1127
    // Check for cRLSign bit if keyUsage present
1128
0
    if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1129
0
        !(issuer->ex_kusage & X509v3_KU_CRL_SIGN)) {
1130
0
      ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1131
0
      if (!call_verify_cb(0, ctx)) {
1132
0
        return 0;
1133
0
      }
1134
0
    }
1135
1136
0
    if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
1137
0
      ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1138
0
      if (!call_verify_cb(0, ctx)) {
1139
0
        return 0;
1140
0
      }
1141
0
    }
1142
1143
0
    if (crl->idp_flags & IDP_INVALID) {
1144
0
      ctx->error = X509_V_ERR_INVALID_EXTENSION;
1145
0
      if (!call_verify_cb(0, ctx)) {
1146
0
        return 0;
1147
0
      }
1148
0
    }
1149
1150
0
    if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1151
0
      if (!check_crl_time(ctx, crl, 1)) {
1152
0
        return 0;
1153
0
      }
1154
0
    }
1155
1156
    // Attempt to get issuer certificate public key
1157
0
    EVP_PKEY *ikey = X509_get0_pubkey(issuer);
1158
0
    if (!ikey) {
1159
0
      ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1160
0
      if (!call_verify_cb(0, ctx)) {
1161
0
        return 0;
1162
0
      }
1163
0
    } else {
1164
      // Verify CRL signature
1165
0
      if (X509_CRL_verify(crl, ikey) <= 0) {
1166
0
        ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1167
0
        if (!call_verify_cb(0, ctx)) {
1168
0
          return 0;
1169
0
        }
1170
0
      }
1171
0
    }
1172
0
  }
1173
1174
0
  return 1;
1175
0
}
1176
1177
// Check certificate against CRL
1178
0
static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x) {
1179
  // The rules changed for this... previously if a CRL contained unhandled
1180
  // critical extensions it could still be used to indicate a certificate
1181
  // was revoked. This has since been changed since critical extension can
1182
  // change the meaning of CRL entries.
1183
0
  if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) &&
1184
0
      (crl->flags & EXFLAG_CRITICAL)) {
1185
0
    ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
1186
0
    if (!call_verify_cb(0, ctx)) {
1187
0
      return 0;
1188
0
    }
1189
0
  }
1190
  // Look for serial number of certificate in CRL.
1191
0
  X509_REVOKED *rev;
1192
0
  if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1193
0
    ctx->error = X509_V_ERR_CERT_REVOKED;
1194
0
    if (!call_verify_cb(0, ctx)) {
1195
0
      return 0;
1196
0
    }
1197
0
  }
1198
1199
0
  return 1;
1200
0
}
1201
1202
0
static int check_policy(X509_STORE_CTX *ctx) {
1203
0
  X509 *current_cert = nullptr;
1204
0
  int ret = X509_policy_check(ctx->chain, ctx->param->policies,
1205
0
                              ctx->param->flags, &current_cert);
1206
0
  if (ret != X509_V_OK) {
1207
0
    ctx->current_cert = current_cert;
1208
0
    ctx->error = ret;
1209
0
    if (ret == X509_V_ERR_OUT_OF_MEM) {
1210
0
      return 0;
1211
0
    }
1212
0
    return call_verify_cb(0, ctx);
1213
0
  }
1214
1215
0
  return 1;
1216
0
}
1217
1218
0
static int check_cert_time(X509_STORE_CTX *ctx, X509 *x) {
1219
0
  if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) {
1220
0
    return 1;
1221
0
  }
1222
1223
0
  int64_t ptime;
1224
0
  if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) {
1225
0
    ptime = ctx->param->check_time;
1226
0
  } else {
1227
0
    ptime = time(nullptr);
1228
0
  }
1229
1230
0
  int i = X509_cmp_time_posix(X509_get_notBefore(x), ptime);
1231
0
  if (i == 0) {
1232
0
    ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
1233
0
    ctx->current_cert = x;
1234
0
    if (!call_verify_cb(0, ctx)) {
1235
0
      return 0;
1236
0
    }
1237
0
  }
1238
1239
0
  if (i > 0) {
1240
0
    ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
1241
0
    ctx->current_cert = x;
1242
0
    if (!call_verify_cb(0, ctx)) {
1243
0
      return 0;
1244
0
    }
1245
0
  }
1246
1247
0
  i = X509_cmp_time_posix(X509_get_notAfter(x), ptime);
1248
0
  if (i == 0) {
1249
0
    ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
1250
0
    ctx->current_cert = x;
1251
0
    if (!call_verify_cb(0, ctx)) {
1252
0
      return 0;
1253
0
    }
1254
0
  }
1255
1256
0
  if (i < 0) {
1257
0
    ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
1258
0
    ctx->current_cert = x;
1259
0
    if (!call_verify_cb(0, ctx)) {
1260
0
      return 0;
1261
0
    }
1262
0
  }
1263
1264
0
  return 1;
1265
0
}
1266
1267
0
static int internal_verify(X509_STORE_CTX *ctx) {
1268
  // TODO(davidben): This logic is incredibly confusing. Rewrite this:
1269
  //
1270
  // First, don't allow the verify callback to suppress
1271
  // X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY, which will simplify the
1272
  // signature check. Then replace jumping into the middle of the loop. It's
1273
  // trying to ensure that all certificates see |check_cert_time|, then checking
1274
  // the root's self signature when requested, but not breaking partial chains
1275
  // in the process.
1276
0
  int n = (int)sk_X509_num(ctx->chain);
1277
0
  ctx->error_depth = n - 1;
1278
0
  n--;
1279
0
  X509 *xi = sk_X509_value(ctx->chain, n);
1280
0
  X509 *xs;
1281
0
  if (x509_check_issued_with_callback(ctx, xi, xi)) {
1282
0
    xs = xi;
1283
0
  } else {
1284
0
    if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
1285
0
      xs = xi;
1286
0
      goto check_cert;
1287
0
    }
1288
0
    if (n <= 0) {
1289
0
      ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
1290
0
      ctx->current_cert = xi;
1291
0
      return call_verify_cb(0, ctx);
1292
0
    }
1293
0
    n--;
1294
0
    ctx->error_depth = n;
1295
0
    xs = sk_X509_value(ctx->chain, n);
1296
0
  }
1297
1298
  //      ctx->error=0;  not needed
1299
0
  while (n >= 0) {
1300
0
    ctx->error_depth = n;
1301
1302
    // Skip signature check for self signed certificates unless
1303
    // explicitly asked for. It doesn't add any security and just wastes
1304
    // time.
1305
0
    if (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)) {
1306
0
      EVP_PKEY *pkey = X509_get0_pubkey(xi);
1307
0
      if (pkey == nullptr) {
1308
0
        ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1309
0
        ctx->current_cert = xi;
1310
0
        if (!call_verify_cb(0, ctx)) {
1311
0
          return 0;
1312
0
        }
1313
0
      } else if (X509_verify(xs, pkey) <= 0) {
1314
0
        ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
1315
0
        ctx->current_cert = xs;
1316
0
        if (!call_verify_cb(0, ctx)) {
1317
0
          return 0;
1318
0
        }
1319
0
      }
1320
0
    }
1321
1322
0
  check_cert:
1323
0
    if (!check_cert_time(ctx, xs)) {
1324
0
      return 0;
1325
0
    }
1326
1327
    // The last error (if any) is still in the error value
1328
0
    ctx->current_cert = xs;
1329
0
    if (!call_verify_cb(1, ctx)) {
1330
0
      return 0;
1331
0
    }
1332
1333
0
    n--;
1334
0
    if (n >= 0) {
1335
0
      xi = xs;
1336
0
      xs = sk_X509_value(ctx->chain, n);
1337
0
    }
1338
0
  }
1339
1340
0
  return 1;
1341
0
}
1342
1343
0
int X509_cmp_current_time(const ASN1_TIME *ctm) {
1344
0
  return X509_cmp_time_posix(ctm, time(nullptr));
1345
0
}
1346
1347
0
int X509_cmp_time(const ASN1_TIME *ctm, const time_t *cmp_time) {
1348
0
  int64_t compare_time = (cmp_time == nullptr) ? time(nullptr) : *cmp_time;
1349
0
  return X509_cmp_time_posix(ctm, compare_time);
1350
0
}
1351
1352
0
int X509_cmp_time_posix(const ASN1_TIME *ctm, int64_t cmp_time) {
1353
0
  int64_t ctm_time;
1354
0
  if (!ASN1_TIME_to_posix(ctm, &ctm_time)) {
1355
0
    return 0;
1356
0
  }
1357
  // The return value 0 is reserved for errors.
1358
0
  return (ctm_time - cmp_time <= 0) ? -1 : 1;
1359
0
}
1360
1361
0
ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long offset_sec) {
1362
0
  return X509_time_adj(s, offset_sec, nullptr);
1363
0
}
1364
1365
0
ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, const time_t *in_tm) {
1366
0
  return X509_time_adj_ex(s, 0, offset_sec, in_tm);
1367
0
}
1368
1369
ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, int offset_day, long offset_sec,
1370
0
                            const time_t *in_tm) {
1371
0
  int64_t t = 0;
1372
1373
0
  if (in_tm) {
1374
0
    t = *in_tm;
1375
0
  } else {
1376
0
    t = time(nullptr);
1377
0
  }
1378
1379
0
  return ASN1_TIME_adj(s, t, offset_day, offset_sec);
1380
0
}
1381
1382
int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
1383
                                    CRYPTO_EX_unused *unused,
1384
                                    CRYPTO_EX_dup *dup_unused,
1385
0
                                    CRYPTO_EX_free *free_func) {
1386
0
  return CRYPTO_get_ex_new_index_ex(&g_ex_data_class, argl, argp, free_func);
1387
0
}
1388
1389
7.31k
int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data) {
1390
7.31k
  return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
1391
7.31k
}
1392
1393
0
void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx) {
1394
0
  return CRYPTO_get_ex_data(&ctx->ex_data, idx);
1395
0
}
1396
1397
7.31k
int X509_STORE_CTX_get_error(const X509_STORE_CTX *ctx) { return ctx->error; }
1398
1399
0
void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err) {
1400
0
  ctx->error = err;
1401
0
}
1402
1403
0
int X509_STORE_CTX_get_error_depth(const X509_STORE_CTX *ctx) {
1404
0
  return ctx->error_depth;
1405
0
}
1406
1407
0
X509 *X509_STORE_CTX_get_current_cert(const X509_STORE_CTX *ctx) {
1408
0
  return ctx->current_cert;
1409
0
}
1410
1411
0
STACK_OF(X509) *X509_STORE_CTX_get_chain(const X509_STORE_CTX *ctx) {
1412
0
  return ctx->chain;
1413
0
}
1414
1415
0
STACK_OF(X509) *X509_STORE_CTX_get0_chain(const X509_STORE_CTX *ctx) {
1416
0
  return ctx->chain;
1417
0
}
1418
1419
0
STACK_OF(X509) *X509_STORE_CTX_get1_chain(const X509_STORE_CTX *ctx) {
1420
0
  if (!ctx->chain) {
1421
0
    return nullptr;
1422
0
  }
1423
0
  return X509_chain_up_ref(ctx->chain);
1424
0
}
1425
1426
0
X509_CRL *X509_STORE_CTX_get0_current_crl(const X509_STORE_CTX *ctx) {
1427
0
  return ctx->current_crl;
1428
0
}
1429
1430
0
X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(const X509_STORE_CTX *ctx) {
1431
  // In OpenSSL, an |X509_STORE_CTX| sometimes has a parent context during CRL
1432
  // path validation for indirect CRLs. We require the CRL to be issued
1433
  // somewhere along the certificate path, so this is always NULL.
1434
0
  return nullptr;
1435
0
}
1436
1437
0
void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) {
1438
0
  ctx->untrusted = sk;
1439
0
}
1440
1441
0
STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(const X509_STORE_CTX *ctx) {
1442
0
  return ctx->untrusted;
1443
0
}
1444
1445
0
void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk) {
1446
0
  ctx->crls = sk;
1447
0
}
1448
1449
0
int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose) {
1450
  // If |purpose| is zero, this function historically silently did nothing.
1451
0
  if (purpose == 0) {
1452
0
    return 1;
1453
0
  }
1454
1455
0
  const X509_PURPOSE *pobj = X509_PURPOSE_get0(purpose);
1456
0
  if (pobj == nullptr) {
1457
0
    OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID);
1458
0
    return 0;
1459
0
  }
1460
1461
0
  int trust = X509_PURPOSE_get_trust(pobj);
1462
0
  if (!X509_STORE_CTX_set_trust(ctx, trust)) {
1463
0
    return 0;
1464
0
  }
1465
1466
0
  if (ctx->param->purpose == 0) {
1467
0
    ctx->param->purpose = purpose;
1468
0
  }
1469
0
  return 1;
1470
0
}
1471
1472
0
int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust) {
1473
  // If |trust| is zero, this function historically silently did nothing.
1474
0
  if (trust == 0) {
1475
0
    return 1;
1476
0
  }
1477
1478
0
  if (!X509_is_valid_trust_id(trust)) {
1479
0
    OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_TRUST_ID);
1480
0
    return 0;
1481
0
  }
1482
1483
0
  if (ctx->param->trust == 0) {
1484
0
    ctx->param->trust = trust;
1485
0
  }
1486
0
  return 1;
1487
0
}
1488
1489
7.31k
X509_STORE_CTX *X509_STORE_CTX_new() { return New<X509_STORE_CTX>(); }
1490
1491
7.31k
void X509_STORE_CTX_free(X509_STORE_CTX *ctx) {
1492
7.31k
  if (ctx == nullptr) {
1493
0
    return;
1494
0
  }
1495
7.31k
  X509_STORE_CTX_cleanup(ctx);
1496
7.31k
  Delete(ctx);
1497
7.31k
}
1498
1499
int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
1500
7.31k
                        STACK_OF(X509) *chain) {
1501
7.31k
  X509_STORE_CTX_cleanup(ctx);
1502
1503
7.31k
  ctx->ctx = store;
1504
7.31k
  ctx->cert = x509;
1505
7.31k
  ctx->untrusted = chain;
1506
1507
7.31k
  CRYPTO_new_ex_data(&ctx->ex_data);
1508
1509
7.31k
  if (store == nullptr) {
1510
0
    OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
1511
0
    goto err;
1512
0
  }
1513
1514
7.31k
  ctx->param = X509_VERIFY_PARAM_new();
1515
7.31k
  if (!ctx->param) {
1516
0
    goto err;
1517
0
  }
1518
1519
7.31k
  {
1520
    // Inherit callbacks and flags from X509_STORE.
1521
1522
7.31k
    auto *store_impl = FromOpaque(store);
1523
7.31k
    ctx->verify_cb = store_impl->verify_cb;
1524
1525
7.31k
    if (!X509_VERIFY_PARAM_inherit(ctx->param, store_impl->param.get()) ||
1526
7.31k
        !X509_VERIFY_PARAM_inherit(ctx->param,
1527
7.31k
                                   X509_VERIFY_PARAM_lookup("default"))) {
1528
0
      goto err;
1529
0
    }
1530
1531
7.31k
    if (store_impl->verify_cb) {
1532
0
      ctx->verify_cb = store_impl->verify_cb;
1533
7.31k
    } else {
1534
7.31k
      ctx->verify_cb = null_callback;
1535
7.31k
    }
1536
7.31k
  }
1537
1538
0
  return 1;
1539
1540
0
err:
1541
0
  X509_STORE_CTX_cleanup(ctx);
1542
0
  return 0;
1543
7.31k
}
1544
1545
// Set alternative lookup method: just a STACK of trusted certificates. This
1546
// avoids X509_STORE nastiness where it isn't needed.
1547
1548
void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx,
1549
0
                                       STACK_OF(X509) *sk) {
1550
0
  ctx->trusted_stack = sk;
1551
0
}
1552
1553
0
void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) {
1554
0
  X509_STORE_CTX_set0_trusted_stack(ctx, sk);
1555
0
}
1556
1557
14.6k
void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx) {
1558
14.6k
  CRYPTO_free_ex_data(&g_ex_data_class, &ctx->ex_data);
1559
14.6k
  X509_VERIFY_PARAM_free(ctx->param);
1560
14.6k
  sk_X509_pop_free(ctx->chain, X509_free);
1561
14.6k
  OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX));
1562
14.6k
}
1563
1564
0
void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth) {
1565
0
  X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1566
0
}
1567
1568
0
void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags) {
1569
0
  X509_VERIFY_PARAM_set_flags(ctx->param, flags);
1570
0
}
1571
1572
void X509_STORE_CTX_set_time_posix(X509_STORE_CTX *ctx, unsigned long flags,
1573
0
                                   int64_t t) {
1574
0
  X509_VERIFY_PARAM_set_time_posix(ctx->param, t);
1575
0
}
1576
1577
void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
1578
0
                             time_t t) {
1579
0
  X509_STORE_CTX_set_time_posix(ctx, flags, t);
1580
0
}
1581
1582
0
X509 *X509_STORE_CTX_get0_cert(const X509_STORE_CTX *ctx) { return ctx->cert; }
1583
1584
void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
1585
0
                                  int (*verify_cb)(int, X509_STORE_CTX *)) {
1586
0
  ctx->verify_cb = verify_cb;
1587
0
}
1588
1589
7.31k
int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name) {
1590
7.31k
  const X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_lookup(name);
1591
7.31k
  if (!param) {
1592
0
    return 0;
1593
0
  }
1594
7.31k
  return X509_VERIFY_PARAM_inherit(ctx->param, param);
1595
7.31k
}
1596
1597
7.31k
X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx) {
1598
7.31k
  return ctx->param;
1599
7.31k
}
1600
1601
0
void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param) {
1602
0
  if (ctx->param) {
1603
0
    X509_VERIFY_PARAM_free(ctx->param);
1604
0
  }
1605
0
  ctx->param = param;
1606
0
}