Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / base / unguessable_token.h
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 #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 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.
27 //
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.
36 //
37 // Use Create() for creating new UnguessableTokens.
38 //
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 {
44  public:
45   // Create a unique UnguessableToken.
46   static UnguessableToken Create();
47
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();
53
54   // Return a UnguessableToken built from the high/low bytes provided.
55   // It should only be used in deserialization scenarios.
56   //
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);
60
61   // Creates an empty UnguessableToken.
62   // Assign to it with Create() before using it.
63   constexpr UnguessableToken() = default;
64
65   // NOTE: Serializing an empty UnguessableToken is an illegal operation.
66   uint64_t GetHighForSerialization() const {
67     DCHECK(!is_empty());
68     return token_.high();
69   }
70
71   // NOTE: Serializing an empty UnguessableToken is an illegal operation.
72   uint64_t GetLowForSerialization() const {
73     DCHECK(!is_empty());
74     return token_.low();
75   }
76
77   bool is_empty() const { return token_.is_zero(); }
78
79   // Hex representation of the unguessable token.
80   std::string ToString() const { return token_.ToString(); }
81
82   explicit operator bool() const { return !is_empty(); }
83
84   bool operator<(const UnguessableToken& other) const {
85     return token_ < other.token_;
86   }
87
88   bool operator==(const UnguessableToken& other) const {
89     return token_ == other.token_;
90   }
91
92   bool operator!=(const UnguessableToken& other) const {
93     return !(*this == other);
94   }
95
96  private:
97   friend struct UnguessableTokenHash;
98   explicit UnguessableToken(const Token& token);
99
100   base::Token token_;
101 };
102
103 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
104                                      const UnguessableToken& token);
105
106 // For use in std::unordered_map.
107 struct UnguessableTokenHash {
108   size_t operator()(const base::UnguessableToken& token) const {
109     DCHECK(token);
110     return TokenHash()(token.token_);
111   }
112 };
113
114 }  // namespace base
115
116 #endif  // BASE_UNGUESSABLE_TOKEN_H_