1 // Copyright 2016 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 BASE_UNGUESSABLE_TOKEN_H_ 6 #define BASE_UNGUESSABLE_TOKEN_H_ 7 8 #include <stdint.h> 9 #include <string.h> 10 #include <iosfwd> 11 #include <tuple> 12 13 #include "base/base_export.h" 14 #include "base/hash.h" 15 #include "base/logging.h" 16 17 namespace base { 18 19 struct UnguessableTokenHash; 20 21 // A UnguessableToken is an 128-bit token generated from a cryptographically 22 // strong random source. It can be used as part of a larger aggregate type, 23 // or as an ID in and of itself. 24 // 25 // UnguessableToken can be used to implement "Capability-Based Security". 26 // In other words, UnguessableToken can be used when the resource associated 27 // with the ID needs to be protected against manipulation by other untrusted 28 // agents in the system, and there is no other convenient way to verify the 29 // authority of the agent to do so (because the resource is part of a table 30 // shared across processes, for instance). In such a scheme, knowledge of the 31 // token value in and of itself is sufficient proof of authority to carry out 32 // an operation against the associated resource. 33 // 34 // Use Create() for creating new UnguessableTokens. 35 // 36 // NOTE: It is illegal to send empty UnguessableTokens across processes, and 37 // sending/receiving empty tokens should be treated as a security issue. 38 // If there is a valid scenario for sending "no token" across processes, 39 // base::Optional should be used instead of an empty token. 40 class BASE_EXPORT UnguessableToken { 41 public: 42 // Create a unique UnguessableToken. 43 static UnguessableToken Create(); 44 45 // Return a UnguessableToken built from the high/low bytes provided. 46 // It should only be used in deserialization scenarios. 47 // 48 // NOTE: If the deserialized token is empty, it means that it was never 49 // initialized via Create(). This is a security issue, and should be handled. 50 static UnguessableToken Deserialize(uint64_t high, uint64_t low); 51 52 // Creates an empty UnguessableToken. 53 // Assign to it with Create() before using it. 54 constexpr UnguessableToken() = default; 55 56 // NOTE: Serializing an empty UnguessableToken is an illegal operation. 57 uint64_t GetHighForSerialization() const { 58 DCHECK(!is_empty()); 59 return high_; 60 } 61 62 // NOTE: Serializing an empty UnguessableToken is an illegal operation. 63 uint64_t GetLowForSerialization() const { 64 DCHECK(!is_empty()); 65 return low_; 66 } 67 68 bool is_empty() const { return high_ == 0 && low_ == 0; } 69 70 // Hex representation of the unguessable token. 71 std::string ToString() const; 72 73 explicit operator bool() const { return !is_empty(); } 74 75 bool operator<(const UnguessableToken& other) const { 76 return std::tie(high_, low_) < std::tie(other.high_, other.low_); 77 } 78 79 bool operator==(const UnguessableToken& other) const { 80 return high_ == other.high_ && low_ == other.low_; 81 } 82 83 bool operator!=(const UnguessableToken& other) const { 84 return !(*this == other); 85 } 86 87 private: 88 friend struct UnguessableTokenHash; 89 UnguessableToken(uint64_t high, uint64_t low); 90 91 // Note: Two uint64_t are used instead of uint8_t[16], in order to have a 92 // simpler ToString() and is_empty(). 93 uint64_t high_ = 0; 94 uint64_t low_ = 0; 95 }; 96 97 BASE_EXPORT std::ostream& operator<<(std::ostream& out, 98 const UnguessableToken& token); 99 100 // For use in std::unordered_map. 101 struct UnguessableTokenHash { 102 size_t operator()(const base::UnguessableToken& token) const { 103 DCHECK(token); 104 return base::HashInts64(token.high_, token.low_); 105 } 106 }; 107 108 } // namespace base 109 110 #endif // BASE_UNGUESSABLE_TOKEN_H_ 111