unguessable_token.h (4953B)
1 // Copyright 2016 The Chromium Authors 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/check.h" 15 #include "base/containers/span.h" 16 #include "base/token.h" 17 18 namespace base { 19 20 struct UnguessableTokenHash; 21 22 // UnguessableToken is, like Token, a randomly chosen 128-bit value. Unlike 23 // Token, a new UnguessableToken is always generated at runtime from a 24 // cryptographically strong random source (or copied or serialized and 25 // deserialized from another such UnguessableToken). Also unlike Token, the == 26 // and != operators are constant time. It can be used as part of a larger 27 // aggregate type, or as an ID in and of itself. 28 // 29 // An UnguessableToken is a strong *bearer token*. Bearer tokens are like HTTP 30 // cookies: if a caller has the token, the callee thereby considers the caller 31 // authorized to request the operation the callee performs. 32 // 33 // UnguessableToken can be used when the resource associated with the ID needs 34 // to be protected against manipulation by other untrusted agents in the system, 35 // and there is no other convenient way to verify the authority of the agent to 36 // do so (because the resource is part of a table shared across processes, for 37 // instance). In such a scheme, knowledge of the token value in and of itself is 38 // sufficient proof of authority to carry out an operation on the associated 39 // resource. 40 // 41 // Use Create() for creating new UnguessableTokens. 42 // 43 // NOTE: It is illegal to send empty UnguessableTokens across processes, and 44 // sending/receiving empty tokens should be treated as a security issue. If 45 // there is a valid scenario for sending "no token" across processes, use 46 // absl::optional instead of an empty token. 47 48 class BASE_EXPORT UnguessableToken { 49 public: 50 // Create a unique UnguessableToken. It's guaranteed to be nonempty. 51 static UnguessableToken Create(); 52 53 // Returns a reference to a global null UnguessableToken. This should only be 54 // used for functions that need to return a reference to an UnguessableToken, 55 // and should not be used as a general-purpose substitute for invoking the 56 // default constructor. 57 static const UnguessableToken& Null(); 58 59 // Return an UnguessableToken built from the high/low bytes provided. 60 // It should only be used in deserialization scenarios. 61 // 62 // NOTE: If the returned `absl::optional` does not have a value, it means that 63 // `high` and `low` correspond to an `UnguesssableToken` that was never 64 // initialized via Create(). This is a security issue, and should be handled. 65 static absl::optional<UnguessableToken> Deserialize(uint64_t high, 66 uint64_t low); 67 68 // Creates an empty UnguessableToken. 69 // Assign to it with Create() before using it. 70 constexpr UnguessableToken() = default; 71 72 constexpr UnguessableToken(const UnguessableToken&) = default; 73 constexpr UnguessableToken& operator=(const UnguessableToken&) = default; 74 constexpr UnguessableToken(UnguessableToken&&) noexcept = default; 75 constexpr UnguessableToken& operator=(UnguessableToken&&) = default; 76 77 // NOTE: Serializing an empty UnguessableToken is an illegal operation. 78 uint64_t GetHighForSerialization() const { 79 DCHECK(!is_empty()); 80 return token_.high(); 81 } 82 83 // NOTE: Serializing an empty UnguessableToken is an illegal operation. 84 uint64_t GetLowForSerialization() const { 85 DCHECK(!is_empty()); 86 return token_.low(); 87 } 88 89 constexpr bool is_empty() const { return token_.is_zero(); } 90 91 // Hex representation of the unguessable token. 92 std::string ToString() const { return token_.ToString(); } 93 94 explicit constexpr operator bool() const { return !is_empty(); } 95 96 span<const uint8_t, 16> AsBytes() const { return token_.AsBytes(); } 97 98 constexpr bool operator<(const UnguessableToken& other) const { 99 return token_ < other.token_; 100 } 101 102 bool operator==(const UnguessableToken& other) const; 103 104 bool operator!=(const UnguessableToken& other) const { 105 return !(*this == other); 106 } 107 108 #if defined(UNIT_TEST) 109 static UnguessableToken CreateForTesting(uint64_t high, uint64_t low) { 110 absl::optional<UnguessableToken> token = Deserialize(high, low); 111 DCHECK(token.has_value()); 112 return token.value(); 113 } 114 #endif 115 116 private: 117 friend struct UnguessableTokenHash; 118 explicit UnguessableToken(const Token& token); 119 120 base::Token token_; 121 }; 122 123 BASE_EXPORT std::ostream& operator<<(std::ostream& out, 124 const UnguessableToken& token); 125 126 // For use in std::unordered_map. 127 struct UnguessableTokenHash { 128 size_t operator()(const base::UnguessableToken& token) const { 129 DCHECK(token); 130 return TokenHash()(token.token_); 131 } 132 }; 133 134 } // namespace base 135 136 #endif // BASE_UNGUESSABLE_TOKEN_H_