Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/WebAuthnManager.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_WebAuthnManager_h
8
#define mozilla_dom_WebAuthnManager_h
9
10
#include "mozilla/MozPromise.h"
11
#include "mozilla/dom/PWebAuthnTransaction.h"
12
#include "mozilla/dom/WebAuthnManagerBase.h"
13
14
/*
15
 * Content process manager for the WebAuthn protocol. Created on calls to the
16
 * WebAuthentication DOM object, this manager handles establishing IPC channels
17
 * for WebAuthn transactions, as well as keeping track of JS Promise objects
18
 * representing transactions in flight.
19
 *
20
 * The WebAuthn spec (https://www.w3.org/TR/webauthn/) allows for two different
21
 * types of transactions: registration and signing. When either of these is
22
 * requested via the DOM API, the following steps are executed in the
23
 * WebAuthnManager:
24
 *
25
 * - Validation of the request. Return a failed promise to js if request does
26
 *   not have correct parameters.
27
 *
28
 * - If request is valid, open a new IPC channel for running the transaction. If
29
 *   another transaction is already running in this content process, cancel it.
30
 *   Return a pending promise to js.
31
 *
32
 * - Send transaction information to parent process (by running the Start*
33
 *   functions of WebAuthnManager). Assuming another transaction is currently in
34
 *   flight in another content process, parent will handle canceling it.
35
 *
36
 * - On return of successful transaction information from parent process, turn
37
 *   information into DOM object format required by spec, and resolve promise
38
 *   (by running the Finish* functions of WebAuthnManager). On cancellation
39
 *   request from parent, reject promise with corresponding error code. Either
40
 *   outcome will also close the IPC channel.
41
 *
42
 */
43
44
namespace mozilla {
45
namespace dom {
46
47
class WebAuthnTransaction
48
{
49
public:
50
  explicit WebAuthnTransaction(const RefPtr<Promise>& aPromise)
51
    : mPromise(aPromise)
52
    , mId(NextId())
53
0
  {
54
0
    MOZ_ASSERT(mId > 0);
55
0
  }
56
57
  // JS Promise representing the transaction status.
58
  RefPtr<Promise> mPromise;
59
60
  // Unique transaction id.
61
  uint64_t mId;
62
63
private:
64
  // Generates a unique id for new transactions. This doesn't have to be unique
65
  // forever, it's sufficient to differentiate between temporally close
66
  // transactions, where messages can intersect. Can overflow.
67
0
  static uint64_t NextId() {
68
0
    static uint64_t id = 0;
69
0
    return ++id;
70
0
  }
71
};
72
73
class WebAuthnManager final : public WebAuthnManagerBase
74
                            , public AbortFollower
75
{
76
public:
77
  NS_DECL_ISUPPORTS
78
79
  explicit WebAuthnManager(nsPIDOMWindowInner* aParent)
80
    : WebAuthnManagerBase(aParent)
81
0
  { }
82
83
  already_AddRefed<Promise>
84
  MakeCredential(const PublicKeyCredentialCreationOptions& aOptions,
85
                 const Optional<OwningNonNull<AbortSignal>>& aSignal);
86
87
  already_AddRefed<Promise>
88
  GetAssertion(const PublicKeyCredentialRequestOptions& aOptions,
89
               const Optional<OwningNonNull<AbortSignal>>& aSignal);
90
91
  already_AddRefed<Promise>
92
  Store(const Credential& aCredential);
93
94
  // WebAuthnManagerBase
95
96
  void
97
  FinishMakeCredential(const uint64_t& aTransactionId,
98
                       const WebAuthnMakeCredentialResult& aResult) override;
99
100
  void
101
  FinishGetAssertion(const uint64_t& aTransactionId,
102
                     const WebAuthnGetAssertionResult& aResult) override;
103
104
  void
105
  RequestAborted(const uint64_t& aTransactionId,
106
                 const nsresult& aError) override;
107
108
  // AbortFollower
109
110
  void Abort() override;
111
112
protected:
113
  // Cancels the current transaction (by sending a Cancel message to the
114
  // parent) and rejects it by calling RejectTransaction().
115
  void CancelTransaction(const nsresult& aError) override;
116
117
private:
118
  virtual ~WebAuthnManager();
119
120
  // Clears all information we have about the current transaction.
121
  void ClearTransaction();
122
  // Rejects the current transaction and calls ClearTransaction().
123
  void RejectTransaction(const nsresult& aError);
124
125
  // The current transaction, if any.
126
  Maybe<WebAuthnTransaction> mTransaction;
127
};
128
129
} // namespace dom
130
} // namespace mozilla
131
132
#endif // mozilla_dom_WebAuthnManager_h