summaryrefslogtreecommitdiffstats
path: root/dom/u2f/U2F.h
blob: 9035ae2df297fb96a2c2e52cbf3dcbca06204aa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_U2F_h
#define mozilla_dom_U2F_h

#include "js/TypeDecls.h"
#include "mozilla/Attributes.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/Nullable.h"
#include "mozilla/dom/U2FBinding.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/MozPromise.h"
#include "mozilla/ReentrantMonitor.h"
#include "mozilla/SharedThreadPool.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIU2FToken.h"
#include "nsNSSShutDown.h"
#include "nsPIDOMWindow.h"
#include "nsProxyRelease.h"
#include "nsWrapperCache.h"

#include "USBToken.h"

namespace mozilla {
namespace dom {

class U2FRegisterCallback;
class U2FSignCallback;

// Defined in U2FBinding.h by the U2F.webidl; their use requires a JSContext.
struct RegisterRequest;
struct RegisteredKey;

// These structs are analogs to the WebIDL versions, but can be used on worker
// threads which lack a JSContext.
struct LocalRegisterRequest
{
  nsString mChallenge;
  nsString mVersion;
  CryptoBuffer mClientData;
};

struct LocalRegisteredKey
{
  nsString mKeyHandle;
  nsString mVersion;
  Nullable<nsString> mAppId;
  // TODO: Support transport preferences
  // Nullable<nsTArray<Transport>> mTransports;
};

// These enumerations are defined in the FIDO U2F Javascript API under the
// interface "ErrorCode" as constant integers, and thus in the U2F.webidl file.
// Any changes to these must occur in both locations.
enum class ErrorCode {
  OK = 0,
  OTHER_ERROR = 1,
  BAD_REQUEST = 2,
  CONFIGURATION_UNSUPPORTED = 3,
  DEVICE_INELIGIBLE = 4,
  TIMEOUT = 5
};

typedef nsCOMPtr<nsIU2FToken> Authenticator;
typedef MozPromise<nsString, ErrorCode, false> U2FPromise;
typedef MozPromise<Authenticator, ErrorCode, false> U2FPrepPromise;

// U2FPrepTasks return lists of Authenticators that are OK to
// proceed; they're useful for culling incompatible Authenticators.
// Currently, only IsRegistered is supported.
class U2FPrepTask : public Runnable
{
public:
  explicit U2FPrepTask(const Authenticator& aAuthenticator);

  RefPtr<U2FPrepPromise> Execute();

protected:
  virtual ~U2FPrepTask();

  Authenticator mAuthenticator;
  MozPromiseHolder<U2FPrepPromise> mPromise;
};

// Determine whether the provided Authenticator already knows
// of the provided Registered Key.
class U2FIsRegisteredTask final : public U2FPrepTask
{
public:
  U2FIsRegisteredTask(const Authenticator& aAuthenticator,
                      const LocalRegisteredKey& aRegisteredKey);

  NS_DECL_NSIRUNNABLE
private:
  ~U2FIsRegisteredTask();

  LocalRegisteredKey mRegisteredKey;
};

class U2FTask : public Runnable
{
public:
  U2FTask(const nsAString& aOrigin,
          const nsAString& aAppId,
          const Authenticator& aAuthenticator);

  RefPtr<U2FPromise> Execute();

  nsString mOrigin;
  nsString mAppId;
  Authenticator mAuthenticator;

protected:
  virtual ~U2FTask();

  MozPromiseHolder<U2FPromise> mPromise;
};

// Use the provided Authenticator to Register a new scoped credential
// for the provided application.
class U2FRegisterTask final : public U2FTask
{
public:
  U2FRegisterTask(const nsAString& aOrigin,
                  const nsAString& aAppId,
                  const Authenticator& aAuthenticator,
                  const CryptoBuffer& aAppParam,
                  const CryptoBuffer& aChallengeParam,
                  const LocalRegisterRequest& aRegisterEntry);

  NS_DECL_NSIRUNNABLE
private:
  ~U2FRegisterTask();

  CryptoBuffer mAppParam;
  CryptoBuffer mChallengeParam;
  LocalRegisterRequest mRegisterEntry;
};

// Generate an assertion using the provided Authenticator for the given origin
// and provided application to attest to ownership of a valid scoped credential.
class U2FSignTask final : public U2FTask
{
public:
  U2FSignTask(const nsAString& aOrigin,
              const nsAString& aAppId,
              const nsAString& aVersion,
              const Authenticator& aAuthenticator,
              const CryptoBuffer& aAppParam,
              const CryptoBuffer& aChallengeParam,
              const CryptoBuffer& aClientData,
              const CryptoBuffer& aKeyHandle);

  NS_DECL_NSIRUNNABLE
private:
  ~U2FSignTask();

  nsString mVersion;
  CryptoBuffer mAppParam;
  CryptoBuffer mChallengeParam;
  CryptoBuffer mClientData;
  CryptoBuffer mKeyHandle;
};

// Mediate inter-thread communication for multiple authenticators being queried
// in concert. Operates as a cyclic buffer with a stop-work method.
class U2FStatus
{
public:
  U2FStatus();

  void WaitGroupAdd();
  void WaitGroupDone();
  void WaitGroupWait();

  void Stop(const ErrorCode aErrorCode);
  void Stop(const ErrorCode aErrorCode, const nsAString& aResponse);
  bool IsStopped();
  ErrorCode GetErrorCode();
  nsString GetResponse();

  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(U2FStatus)

private:
  ~U2FStatus();

  uint16_t mCount;
  bool mIsStopped;
  nsString mResponse;
  MOZ_INIT_OUTSIDE_CTOR ErrorCode mErrorCode;
  ReentrantMonitor mReentrantMonitor;
};

// U2FRunnables run to completion, performing a single U2F operation such as
// registering, or signing.
class U2FRunnable : public Runnable
                  , public nsNSSShutDownObject
{
public:
  U2FRunnable(const nsAString& aOrigin, const nsAString& aAppId);

  // No NSS resources to release.
  virtual
  void virtualDestroyNSSReference() override {};

protected:
  virtual ~U2FRunnable();
  ErrorCode EvaluateAppID();

  nsString mOrigin;
  nsString mAppId;
};

// This U2FRunnable completes a single application-requested U2F Register
// operation.
class U2FRegisterRunnable : public U2FRunnable
{
public:
  U2FRegisterRunnable(const nsAString& aOrigin,
                      const nsAString& aAppId,
                      const Sequence<RegisterRequest>& aRegisterRequests,
                      const Sequence<RegisteredKey>& aRegisteredKeys,
                      const Sequence<Authenticator>& aAuthenticators,
                      U2FRegisterCallback* aCallback);

  void SendResponse(const RegisterResponse& aResponse);
  void SetTimeout(const int32_t aTimeoutMillis);

  NS_DECL_NSIRUNNABLE

private:
  ~U2FRegisterRunnable();

  nsTArray<LocalRegisterRequest> mRegisterRequests;
  nsTArray<LocalRegisteredKey> mRegisteredKeys;
  nsTArray<Authenticator> mAuthenticators;
  nsMainThreadPtrHandle<U2FRegisterCallback> mCallback;
  Nullable<int32_t> opt_mTimeoutSeconds;
};

// This U2FRunnable completes a single application-requested U2F Sign operation.
class U2FSignRunnable : public U2FRunnable
{
public:
  U2FSignRunnable(const nsAString& aOrigin,
                  const nsAString& aAppId,
                  const nsAString& aChallenge,
                  const Sequence<RegisteredKey>& aRegisteredKeys,
                  const Sequence<Authenticator>& aAuthenticators,
                  U2FSignCallback* aCallback);

  void SendResponse(const SignResponse& aResponse);
  void SetTimeout(const int32_t aTimeoutMillis);

  NS_DECL_NSIRUNNABLE

private:
  ~U2FSignRunnable();

  nsString mChallenge;
  CryptoBuffer mClientData;
  nsTArray<LocalRegisteredKey> mRegisteredKeys;
  nsTArray<Authenticator> mAuthenticators;
  nsMainThreadPtrHandle<U2FSignCallback> mCallback;
  Nullable<int32_t> opt_mTimeoutSeconds;
};

// The U2F Class is used by the JS engine to initiate U2F operations.
class U2F final : public nsISupports
                , public nsWrapperCache
                , public nsNSSShutDownObject
{
public:
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(U2F)

  U2F();

  nsPIDOMWindowInner*
  GetParentObject() const
  {
    return mParent;
  }

  void
  Init(nsPIDOMWindowInner* aParent, ErrorResult& aRv);

  virtual JSObject*
  WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;

  void
  Register(const nsAString& aAppId,
           const Sequence<RegisterRequest>& aRegisterRequests,
           const Sequence<RegisteredKey>& aRegisteredKeys,
           U2FRegisterCallback& aCallback,
           const Optional<Nullable<int32_t>>& opt_aTimeoutSeconds,
           ErrorResult& aRv);

  void
  Sign(const nsAString& aAppId,
       const nsAString& aChallenge,
       const Sequence<RegisteredKey>& aRegisteredKeys,
       U2FSignCallback& aCallback,
       const Optional<Nullable<int32_t>>& opt_aTimeoutSeconds,
       ErrorResult& aRv);

  // No NSS resources to release.
  virtual
  void virtualDestroyNSSReference() override {};

private:
  nsCOMPtr<nsPIDOMWindowInner> mParent;
  nsString mOrigin;
  Sequence<Authenticator> mAuthenticators;
  bool mInitialized;

  ~U2F();
};

} // namespace dom
} // namespace mozilla

#endif // mozilla_dom_U2F_h