File: client_cert_matcher.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (166 lines) | stat: -rw-r--r-- 5,320 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/ssl/client_cert_matcher.h"

#include <algorithm>

#include "base/containers/span.h"
#include "base/logging.h"
#include "net/cert/asn1_util.h"
#include "net/cert/x509_certificate.h"
#include "net/cert/x509_util.h"
#include "third_party/boringssl/src/include/openssl/pool.h"

namespace net {

namespace {

bool MatchClientCertificateIssuers(
    X509Certificate* cert,
    const std::vector<std::string>& cert_authorities,
    const ClientCertIssuerSourceCollection& sources,
    std::vector<bssl::UniquePtr<CRYPTO_BUFFER>>* intermediates) {
  constexpr size_t kMaxDepth = 20;
  intermediates->clear();

  // If the request didn't supply `cert_authorities`, all client certs are
  // returned.
  if (cert_authorities.empty()) {
    return true;
  }

  base::span<const uint8_t> current_issuer;
  base::span<const uint8_t> current_subject;
  if (!asn1::ExtractIssuerAndSubjectFromDERCert(
          cert->cert_span(), &current_issuer, &current_subject)) {
    return false;
  }

  while (intermediates->size() < kMaxDepth) {
    // If the current cert in the chain is issued by one of the names in
    // `cert_authorities`, this chain matches the request.
    for (const std::string& authority : cert_authorities) {
      if (base::as_byte_span(authority) == current_issuer) {
        return true;
      }
    }

    // If the chain reached a self-issued cert before matching the requested
    // `cert_authorities`, give up.
    if (current_issuer == current_subject) {
      return false;
    }

    // Look for an issuer of the current cert.
    bool found_issuer = false;
    for (const auto& source : sources) {
      std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> issuers =
          source->GetCertsByName(current_issuer);
      for (auto& issuer : issuers) {
        if (asn1::ExtractIssuerAndSubjectFromDERCert(
                x509_util::CryptoBufferAsSpan(issuer.get()), &current_issuer,
                &current_subject)) {
          // The first issuer found at each step is used. This algorithm doesn't
          // do a full graph exploration.
          found_issuer = true;
          intermediates->push_back(std::move(issuer));
          break;
        }
      }
    }

    if (!found_issuer) {
      // No issuers were found, give up.
      return false;
    }
  }

  return false;
}

}  // namespace

ClientCertIssuerSourceInMemory::ClientCertIssuerSourceInMemory(
    std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> certs)
    : certs_(std::move(certs)) {
  for (const auto& cert : certs_) {
    std::string_view subject;
    if (asn1::ExtractSubjectFromDERCert(
            x509_util::CryptoBufferAsStringPiece(cert.get()), &subject)) {
      cert_map_.emplace(base::as_byte_span(subject), cert.get());
    }
  }
}

ClientCertIssuerSourceInMemory::~ClientCertIssuerSourceInMemory() = default;

std::vector<bssl::UniquePtr<CRYPTO_BUFFER>>
ClientCertIssuerSourceInMemory::GetCertsByName(base::span<const uint8_t> name) {
  std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> result;
  auto range = cert_map_.equal_range(name);
  for (auto it = range.first; it != range.second; ++it) {
    result.push_back(bssl::UpRef(it->second));
  }
  return result;
}

void FilterMatchingClientCertIdentities(
    ClientCertIdentityList* identities,
    const SSLCertRequestInfo& request,
    const ClientCertIssuerSourceCollection& sources) {
  size_t num_raw = 0;

  auto keep_iter = identities->begin();

  base::Time now = base::Time::Now();

  for (auto examine_iter = identities->begin();
       examine_iter != identities->end(); ++examine_iter) {
    ++num_raw;

    X509Certificate* cert = (*examine_iter)->certificate();

    // Only offer unexpired certificates.
    // TODO(https://crbug.com/379943126): If the client system time is
    // incorrect this may prune certificates that the server would have
    // accepted (and we may still successfully validate the server certificate
    // by using secure time). Consider removing.
    if (now < cert->valid_start()) {
      DVLOG(2) << "is not yet valid: " << cert->subject().GetDisplayName();
      continue;
    }
    if (now > cert->valid_expiry()) {
      DVLOG(2) << "is expired: " << cert->subject().GetDisplayName();
      continue;
    }

    std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates;
    if (!MatchClientCertificateIssuers(cert, request.cert_authorities, sources,
                                       &intermediates)) {
      DVLOG(2) << "doesn't match: " << cert->subject().GetDisplayName();
      continue;
    } else {
      DVLOG(2) << "found a match: " << cert->subject().GetDisplayName();
    }

    // Retain a copy of the intermediates. Some deployments expect the client to
    // supply intermediates out of the local store. See
    // https://crbug.com/548631.
    (*examine_iter)->SetIntermediates(std::move(intermediates));

    if (examine_iter == keep_iter) {
      ++keep_iter;
    } else {
      *keep_iter++ = std::move(*examine_iter);
    }
  }
  identities->erase(keep_iter, identities->end());

  DVLOG(2) << "num_raw:" << num_raw << " num_filtered:" << identities->size();

  std::ranges::sort(*identities, ClientCertIdentitySorter());
}

}  // namespace net