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.
5 #ifndef BASE_UNGUESSABLE_TOKEN_H_
6 #define BASE_UNGUESSABLE_TOKEN_H_
13 #include "base/base_export.h"
14 #include "base/hash.h"
15 #include "base/logging.h"
16 #include "base/token.h"
20 struct UnguessableTokenHash;
22 // UnguessableToken is, like Token, a randomly chosen 128-bit value. Unlike
23 // Token however, a new UnguessableToken must always be generated at runtime
24 // from a cryptographically strong random source (or copied or serialized and
25 // deserialized from another such UnguessableToken). It can be used as part of a
26 // larger aggregate type, or as an ID in and of itself.
28 // UnguessableToken can be used to implement "Capability-Based Security".
29 // In other words, UnguessableToken can be used when the resource associated
30 // with the ID needs to be protected against manipulation by other untrusted
31 // agents in the system, and there is no other convenient way to verify the
32 // authority of the agent to do so (because the resource is part of a table
33 // shared across processes, for instance). In such a scheme, knowledge of the
34 // token value in and of itself is sufficient proof of authority to carry out
35 // an operation against the associated resource.
37 // Use Create() for creating new UnguessableTokens.
39 // NOTE: It is illegal to send empty UnguessableTokens across processes, and
40 // sending/receiving empty tokens should be treated as a security issue.
41 // If there is a valid scenario for sending "no token" across processes,
42 // base::Optional should be used instead of an empty token.
43 class BASE_EXPORT UnguessableToken {
45 // Create a unique UnguessableToken.
46 static UnguessableToken Create();
48 // Returns a reference to a global null UnguessableToken. This should only be
49 // used for functions that need to return a reference to an UnguessableToken,
50 // and should not be used as a general-purpose substitute for invoking the
51 // default constructor.
52 static const UnguessableToken& Null();
54 // Return a UnguessableToken built from the high/low bytes provided.
55 // It should only be used in deserialization scenarios.
57 // NOTE: If the deserialized token is empty, it means that it was never
58 // initialized via Create(). This is a security issue, and should be handled.
59 static UnguessableToken Deserialize(uint64_t high, uint64_t low);
61 // Creates an empty UnguessableToken.
62 // Assign to it with Create() before using it.
63 constexpr UnguessableToken() = default;
65 // NOTE: Serializing an empty UnguessableToken is an illegal operation.
66 uint64_t GetHighForSerialization() const {
71 // NOTE: Serializing an empty UnguessableToken is an illegal operation.
72 uint64_t GetLowForSerialization() const {
77 bool is_empty() const { return token_.is_zero(); }
79 // Hex representation of the unguessable token.
80 std::string ToString() const { return token_.ToString(); }
82 explicit operator bool() const { return !is_empty(); }
84 bool operator<(const UnguessableToken& other) const {
85 return token_ < other.token_;
88 bool operator==(const UnguessableToken& other) const {
89 return token_ == other.token_;
92 bool operator!=(const UnguessableToken& other) const {
93 return !(*this == other);
97 friend struct UnguessableTokenHash;
98 explicit UnguessableToken(const Token& token);
103 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
104 const UnguessableToken& token);
106 // For use in std::unordered_map.
107 struct UnguessableTokenHash {
108 size_t operator()(const base::UnguessableToken& token) const {
110 return TokenHash()(token.token_);
116 #endif // BASE_UNGUESSABLE_TOKEN_H_