Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_NET_PACKED_CT_EV_WHITELIST_H_ | |
| 6 #define CHROME_BROWSER_NET_PACKED_CT_EV_WHITELIST_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <set> | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/gtest_prod_util.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/strings/string_piece.h" | |
| 16 #include "net/cert/ct_ev_whitelist.h" | |
| 17 | |
| 18 namespace base { | |
| 19 class FilePath; | |
| 20 } | |
| 21 | |
| 22 namespace net { | |
| 23 class SSLConfigService; | |
| 24 } // namespace net | |
| 25 | |
| 26 namespace internal { | |
| 27 | |
| 28 // A class for reading individual bits from a buffer of bytes they are packed | |
| 29 // into. Bits are read MSB-first from the stream. | |
| 30 // It is limited to 64-bit reads and is inefficient as a design choice - Since | |
| 31 // it is used infrequently to unpack the Golomb-coded EV certificate hashes | |
| 32 // whitelist in a blocking thread. | |
| 33 class BitStreamReader { | |
|
Ryan Sleevi
2014/10/01 20:15:42
Split into separate files?
Eran Messeri
2014/10/03 12:00:11
Done.
| |
| 34 public: | |
| 35 explicit BitStreamReader(const base::StringPiece& source); | |
| 36 | |
| 37 // Reads unary-encoded number into |out|. Returns true if | |
| 38 // there was at least one bit to read, false otherwise. | |
| 39 bool ReadUnaryEncoding(uint64_t* out); | |
| 40 | |
| 41 // Reads |num_bits| (up to 64) into |out|. |out| is filled from the MSB to the | |
| 42 // LSB. If |num_bits| is less than 64, the most significant |64 - num_bits| | |
| 43 // bits are unused and left as zeros. Returns true if the stream had the | |
| 44 // requested |num_bits|, false otherwise. | |
| 45 bool ReadBits(uint8_t num_bits, uint64_t* out); | |
| 46 | |
| 47 // Returns the number of bits left in the stream. | |
| 48 uint64_t BitsLeft() const; | |
| 49 | |
| 50 private: | |
| 51 // Reads a single bit. Within a byte, the bits are read from the MSB to the | |
| 52 // LSB. | |
| 53 uint8_t ReadBit(); | |
| 54 | |
| 55 const base::StringPiece source_; | |
| 56 | |
| 57 // Index of the byte currently being read from. | |
| 58 uint64_t current_byte_; | |
| 59 | |
| 60 // Index of the last bit read within |current_byte_|. Since bits are read | |
| 61 // from the MSB to the LSB, this value is initialized to 7 and decremented | |
| 62 // after each read. | |
| 63 int8 current_bit_; | |
| 64 }; | |
| 65 | |
| 66 } // namespace internal | |
| 67 | |
| 68 class PackedEVCertsWhitelist : public net::ct::EVCertsWhitelist { | |
| 69 public: | |
| 70 explicit PackedEVCertsWhitelist(const std::string& compressed_whitelist); | |
| 71 | |
| 72 // Returns true if the |certificate_hash| appears in the EV certificate hashes | |
| 73 // whitelist. | |
| 74 virtual bool ContainsCertificateHash( | |
| 75 const std::string& certificate_hash) const OVERRIDE; | |
| 76 | |
| 77 // Returns true if the global EV certificate hashes whitelist is non-empty, | |
| 78 // false otherwise. | |
| 79 virtual bool IsValid() const OVERRIDE; | |
| 80 | |
| 81 protected: | |
| 82 virtual ~PackedEVCertsWhitelist(); | |
| 83 // Given a Golomb-coded list of hashes in |compressed_whitelist|, unpack into | |
| 84 // |uncompressed_list|. Returns true if the format of the compressed whitelist | |
| 85 // is valid, false otherwise. | |
| 86 static bool UncompressEVWhitelist(const std::string& compressed_whitelist, | |
| 87 std::set<std::string>* uncompressed_list); | |
| 88 | |
| 89 FRIEND_TEST_ALL_PREFIXES(PackedEVCertsWhitelistTest, | |
| 90 UncompressFailsForTooShortList); | |
| 91 FRIEND_TEST_ALL_PREFIXES(PackedEVCertsWhitelistTest, | |
| 92 UncompressFailsForTruncatedList); | |
| 93 FRIEND_TEST_ALL_PREFIXES(PackedEVCertsWhitelistTest, | |
| 94 UncompressesWhitelistCorrectly); | |
| 95 | |
| 96 private: | |
| 97 friend class base::RefCountedThreadSafe<net::ct::EVCertsWhitelist>; | |
| 98 | |
| 99 bool is_whitelist_valid_; | |
| 100 std::set<std::string> whitelist_; | |
| 101 }; | |
| 102 | |
| 103 // Sets the EV certificate hashes whitelist from |compressed_whitelist_file| | |
| 104 // in |ssl_config_service|, after uncompressing it. | |
| 105 // If the data in |compressed_whitelist_file| is not a valid compressed | |
| 106 // whitelist, does nothing. | |
| 107 void SetEVWhitelistFromFile( | |
| 108 const base::FilePath& compressed_whitelist_file, | |
| 109 scoped_refptr<net::SSLConfigService> ssl_config_service); | |
| 110 | |
| 111 #endif // CHROME_BROWSER_NET_PACKED_CT_EV_WHITELIST_H_ | |
| OLD | NEW |