// Copyright (c) 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "quiche/quic/core/crypto/cert_compressor.h" #include <cstdint> #include <memory> #include <string> #include <utility> #include <vector> #include "absl/strings/string_view.h" #include "quiche/quic/core/quic_utils.h" #include "quiche/quic/platform/api/quic_bug_tracker.h" #include "quiche/quic/platform/api/quic_flag_utils.h" #include "quiche/quic/platform/api/quic_flags.h" #include "zlib.h" namespace quic { namespace { // kCommonCertSubstrings contains ~1500 bytes of common certificate substrings // in order to help zlib. This was generated via a fairly dumb algorithm from // the Alexa Top 5000 set - we could probably do better. static const unsigned char kCommonCertSubstrings[] = …; // CertEntry represents a certificate in compressed form. Each entry is one of // the three types enumerated in |Type|. struct CertEntry { … }; // MatchCerts returns a vector of CertEntries describing how to most // efficiently represent |certs| to a peer who has cached the certificates // with the 64-bit, FNV-1a hashes in |client_cached_cert_hashes|. std::vector<CertEntry> MatchCerts(const std::vector<std::string>& certs, absl::string_view client_cached_cert_hashes) { … } // CertEntriesSize returns the size, in bytes, of the serialised form of // |entries|. size_t CertEntriesSize(const std::vector<CertEntry>& entries) { … } // SerializeCertEntries serialises |entries| to |out|, which must have enough // space to contain them. void SerializeCertEntries(uint8_t* out, const std::vector<CertEntry>& entries) { … } // ZlibDictForEntries returns a string that contains the zlib pre-shared // dictionary to use in order to decompress a zlib block following |entries|. // |certs| is one-to-one with |entries| and contains the certificates for those // entries that are CACHED. std::string ZlibDictForEntries(const std::vector<CertEntry>& entries, const std::vector<std::string>& certs) { … } // HashCerts returns the FNV-1a hashes of |certs|. std::vector<uint64_t> HashCerts(const std::vector<std::string>& certs) { … } // ParseEntries parses the serialised form of a vector of CertEntries from // |in_out| and writes them to |out_entries|. CACHED entries are resolved using // |cached_certs| and written to |out_certs|. |in_out| is updated to contain // the trailing data. bool ParseEntries(absl::string_view* in_out, const std::vector<std::string>& cached_certs, std::vector<CertEntry>* out_entries, std::vector<std::string>* out_certs) { … } // ScopedZLib deals with the automatic destruction of a zlib context. class ScopedZLib { … }; } // anonymous namespace // static std::string CertCompressor::CompressChain( const std::vector<std::string>& certs, absl::string_view client_cached_cert_hashes) { … } // static bool CertCompressor::DecompressChain( absl::string_view in, const std::vector<std::string>& cached_certs, std::vector<std::string>* out_certs) { … } } // namespace quic