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

ash / components / kcer / cert_cache.h [blame]

// 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.

#ifndef ASH_COMPONENTS_KCER_CERT_CACHE_H_
#define ASH_COMPONENTS_KCER_CERT_CACHE_H_

#include <stdint.h>

#include <set>
#include <vector>

#include "base/component_export.h"
#include "base/containers/span.h"
#include "base/memory/scoped_refptr.h"

namespace kcer {

class Cert;

namespace internal {

// Cache for a collection of scoped_refptr<const Cert>-s.
class COMPONENT_EXPORT(KCER) CertCache {
 public:
  // Empty cache.
  CertCache();
  // Cache that contains `certs` (`certs` can be unsorted).
  explicit CertCache(base::span<const scoped_refptr<const Cert>> certs);
  CertCache(CertCache&&);
  CertCache& operator=(CertCache&&);
  ~CertCache();

  // Searches for Cert certificate with the same content as `cert_der` and
  // returns a scoped_refptr<const Cert> on success, a nullptr if `cert` was not
  // found.
  scoped_refptr<const Cert> FindCert(
      const base::span<const uint8_t>& cert_der) const;

  // Returns ref-counting pointers to all certificate from the cache.
  std::vector<scoped_refptr<const Cert>> GetAllCerts() const;

 private:
  // Comparator for sorting scoped_refptr<const Cert>-s and for enabling
  // std::set::find() using the base::span<const uint8_t> representation of a
  // cert.
  struct CertComparator {
    using is_transparent = void;

    bool operator()(const scoped_refptr<const Cert>& a,
                    const base::span<const uint8_t>& b) const;
    bool operator()(const base::span<const uint8_t>& a,
                    const scoped_refptr<const Cert>& b) const;
    bool operator()(const scoped_refptr<const Cert>& a,
                    const scoped_refptr<const Cert>& b) const;
  };

  std::set<scoped_refptr<const Cert>, CertComparator> certs_;
};

}  // namespace internal
}  // namespace kcer

#endif  // ASH_COMPONENTS_KCER_CERT_CACHE_H_