/work/obj-fuzz/dist/include/mozilla/dom/U2F.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef mozilla_dom_U2F_h |
8 | | #define mozilla_dom_U2F_h |
9 | | |
10 | | #include "js/TypeDecls.h" |
11 | | #include "mozilla/Attributes.h" |
12 | | #include "mozilla/dom/BindingDeclarations.h" |
13 | | #include "mozilla/dom/Nullable.h" |
14 | | #include "mozilla/dom/U2FBinding.h" |
15 | | #include "mozilla/dom/WebAuthnManagerBase.h" |
16 | | #include "mozilla/ErrorResult.h" |
17 | | #include "mozilla/MozPromise.h" |
18 | | #include "nsProxyRelease.h" |
19 | | #include "nsWrapperCache.h" |
20 | | #include "U2FAuthenticator.h" |
21 | | |
22 | | namespace mozilla { |
23 | | namespace dom { |
24 | | |
25 | | class WebAuthnMakeCredentialResult; |
26 | | class WebAuthnGetAssertionResult; |
27 | | |
28 | | class U2FRegisterCallback; |
29 | | class U2FSignCallback; |
30 | | |
31 | | // Defined in U2FBinding.h by the U2F.webidl; their use requires a JSContext. |
32 | | struct RegisterRequest; |
33 | | struct RegisteredKey; |
34 | | |
35 | | class U2FTransaction |
36 | | { |
37 | | typedef Variant<nsMainThreadPtrHandle<U2FRegisterCallback>, |
38 | | nsMainThreadPtrHandle<U2FSignCallback>> U2FCallback; |
39 | | |
40 | | public: |
41 | | explicit U2FTransaction(const U2FCallback&& aCallback) |
42 | | : mCallback(std::move(aCallback)) |
43 | | , mId(NextId()) |
44 | 0 | { |
45 | 0 | MOZ_ASSERT(mId > 0); |
46 | 0 | } |
47 | | |
48 | 0 | bool HasRegisterCallback() { |
49 | 0 | return mCallback.is<nsMainThreadPtrHandle<U2FRegisterCallback>>(); |
50 | 0 | } |
51 | | |
52 | 0 | auto& GetRegisterCallback() { |
53 | 0 | return mCallback.as<nsMainThreadPtrHandle<U2FRegisterCallback>>(); |
54 | 0 | } |
55 | | |
56 | 0 | bool HasSignCallback() { |
57 | 0 | return mCallback.is<nsMainThreadPtrHandle<U2FSignCallback>>(); |
58 | 0 | } |
59 | | |
60 | 0 | auto& GetSignCallback() { |
61 | 0 | return mCallback.as<nsMainThreadPtrHandle<U2FSignCallback>>(); |
62 | 0 | } |
63 | | |
64 | | // The callback passed to the API. |
65 | | U2FCallback mCallback; |
66 | | |
67 | | // Unique transaction id. |
68 | | uint64_t mId; |
69 | | |
70 | | private: |
71 | | // Generates a unique id for new transactions. This doesn't have to be unique |
72 | | // forever, it's sufficient to differentiate between temporally close |
73 | | // transactions, where messages can intersect. Can overflow. |
74 | 0 | static uint64_t NextId() { |
75 | 0 | static uint64_t id = 0; |
76 | 0 | return ++id; |
77 | 0 | } |
78 | | }; |
79 | | |
80 | | class U2F final : public WebAuthnManagerBase |
81 | | , public nsWrapperCache |
82 | | { |
83 | | public: |
84 | | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
85 | | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(U2F) |
86 | | |
87 | | explicit U2F(nsPIDOMWindowInner* aParent) |
88 | | : WebAuthnManagerBase(aParent) |
89 | 0 | { } |
90 | | |
91 | | nsPIDOMWindowInner* |
92 | | GetParentObject() const |
93 | 0 | { |
94 | 0 | return mParent; |
95 | 0 | } |
96 | | |
97 | | void |
98 | | Init(ErrorResult& aRv); |
99 | | |
100 | | virtual JSObject* |
101 | | WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; |
102 | | |
103 | | void |
104 | | Register(const nsAString& aAppId, |
105 | | const Sequence<RegisterRequest>& aRegisterRequests, |
106 | | const Sequence<RegisteredKey>& aRegisteredKeys, |
107 | | U2FRegisterCallback& aCallback, |
108 | | const Optional<Nullable<int32_t>>& opt_aTimeoutSeconds, |
109 | | ErrorResult& aRv); |
110 | | |
111 | | void |
112 | | Sign(const nsAString& aAppId, |
113 | | const nsAString& aChallenge, |
114 | | const Sequence<RegisteredKey>& aRegisteredKeys, |
115 | | U2FSignCallback& aCallback, |
116 | | const Optional<Nullable<int32_t>>& opt_aTimeoutSeconds, |
117 | | ErrorResult& aRv); |
118 | | |
119 | | // WebAuthnManagerBase |
120 | | |
121 | | void |
122 | | FinishMakeCredential(const uint64_t& aTransactionId, |
123 | | const WebAuthnMakeCredentialResult& aResult) override; |
124 | | |
125 | | void |
126 | | FinishGetAssertion(const uint64_t& aTransactionId, |
127 | | const WebAuthnGetAssertionResult& aResult) override; |
128 | | |
129 | | void |
130 | | RequestAborted(const uint64_t& aTransactionId, |
131 | | const nsresult& aError) override; |
132 | | |
133 | | protected: |
134 | | // Cancels the current transaction (by sending a Cancel message to the |
135 | | // parent) and rejects it by calling RejectTransaction(). |
136 | | void CancelTransaction(const nsresult& aError) override; |
137 | | |
138 | | private: |
139 | | ~U2F(); |
140 | | |
141 | | template<typename T, typename C> |
142 | | void ExecuteCallback(T& aResp, nsMainThreadPtrHandle<C>& aCb); |
143 | | |
144 | | // Clears all information we have about the current transaction. |
145 | | void ClearTransaction(); |
146 | | // Rejects the current transaction and clears it. |
147 | | void RejectTransaction(const nsresult& aError); |
148 | | |
149 | | nsString mOrigin; |
150 | | |
151 | | // The current transaction, if any. |
152 | | Maybe<U2FTransaction> mTransaction; |
153 | | }; |
154 | | |
155 | | } // namespace dom |
156 | | } // namespace mozilla |
157 | | |
158 | | #endif // mozilla_dom_U2F_h |