summaryrefslogtreecommitdiffstats
path: root/dom/webauthn/AndroidWebAuthnTokenManager.cpp
blob: bdf21b723b3760fcbe80fa161c81174c75636f62 (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "mozilla/ipc/BackgroundParent.h"
#include "mozilla/jni/GeckoBundleUtils.h"
#include "mozilla/StaticPtr.h"

#include "AndroidWebAuthnTokenManager.h"
#include "JavaBuiltins.h"
#include "JavaExceptions.h"
#include "mozilla/java/WebAuthnTokenManagerWrappers.h"
#include "mozilla/jni/Conversions.h"
#include "mozilla/StaticPrefs_security.h"
#include "WebAuthnEnumStrings.h"

namespace mozilla {
namespace jni {

template <>
dom::AndroidWebAuthnResult Java2Native(mozilla::jni::Object::Param aData,
                                       JNIEnv* aEnv) {
  // TODO:
  // AndroidWebAuthnResult stores successful both result and failure result.
  // We should split it into success and failure (Bug 1754157)
  if (aData.IsInstanceOf<jni::Throwable>()) {
    java::sdk::Throwable::LocalRef throwable(aData);
    return dom::AndroidWebAuthnResult(throwable->GetMessage()->ToString());
  }

  if (aData
          .IsInstanceOf<java::WebAuthnTokenManager::MakeCredentialResponse>()) {
    java::WebAuthnTokenManager::MakeCredentialResponse::LocalRef response(
        aData);
    return dom::AndroidWebAuthnResult(response);
  }

  MOZ_ASSERT(
      aData.IsInstanceOf<java::WebAuthnTokenManager::GetAssertionResponse>());
  java::WebAuthnTokenManager::GetAssertionResponse::LocalRef response(aData);
  return dom::AndroidWebAuthnResult(response);
}
}  // namespace jni

namespace dom {

static nsIThread* gAndroidPBackgroundThread;

StaticRefPtr<AndroidWebAuthnTokenManager> gAndroidWebAuthnManager;

/* static */ AndroidWebAuthnTokenManager*
AndroidWebAuthnTokenManager::GetInstance() {
  if (!gAndroidWebAuthnManager) {
    mozilla::ipc::AssertIsOnBackgroundThread();
    gAndroidWebAuthnManager = new AndroidWebAuthnTokenManager();
  }
  return gAndroidWebAuthnManager;
}

AndroidWebAuthnTokenManager::AndroidWebAuthnTokenManager() {
  mozilla::ipc::AssertIsOnBackgroundThread();
  MOZ_ASSERT(XRE_IsParentProcess());
  MOZ_ASSERT(!gAndroidWebAuthnManager);

  gAndroidPBackgroundThread = NS_GetCurrentThread();
  MOZ_ASSERT(gAndroidPBackgroundThread, "This should never be null!");
  gAndroidWebAuthnManager = this;
}

void AndroidWebAuthnTokenManager::AssertIsOnOwningThread() const {
  mozilla::ipc::AssertIsOnBackgroundThread();
  MOZ_ASSERT(gAndroidPBackgroundThread);
#ifdef DEBUG
  bool current;
  MOZ_ASSERT(
      NS_SUCCEEDED(gAndroidPBackgroundThread->IsOnCurrentThread(&current)));
  MOZ_ASSERT(current);
#endif
}

void AndroidWebAuthnTokenManager::Drop() {
  AssertIsOnOwningThread();

  ClearPromises();
  gAndroidWebAuthnManager = nullptr;
  gAndroidPBackgroundThread = nullptr;
}

RefPtr<U2FRegisterPromise> AndroidWebAuthnTokenManager::Register(
    const WebAuthnMakeCredentialInfo& aInfo, bool aForceNoneAttestation) {
  AssertIsOnOwningThread();

  ClearPromises();

  GetMainThreadSerialEventTarget()->Dispatch(NS_NewRunnableFunction(
      "java::WebAuthnTokenManager::WebAuthnMakeCredential",
      [self = RefPtr{this}, aInfo, aForceNoneAttestation]() {
        AssertIsOnMainThread();

        // Produce the credential exclusion list
        jni::ObjectArray::LocalRef idList =
            jni::ObjectArray::New(aInfo.ExcludeList().Length());

        nsTArray<uint8_t> transportBuf;
        int ix = 0;

        for (const WebAuthnScopedCredential& cred : aInfo.ExcludeList()) {
          jni::ByteBuffer::LocalRef id = jni::ByteBuffer::New(
              const_cast<void*>(static_cast<const void*>(cred.id().Elements())),
              cred.id().Length());

          idList->SetElement(ix, id);
          transportBuf.AppendElement(cred.transports());

          ix += 1;
        }

        jni::ByteBuffer::LocalRef transportList = jni::ByteBuffer::New(
            const_cast<void*>(
                static_cast<const void*>(transportBuf.Elements())),
            transportBuf.Length());

        const nsTArray<uint8_t>& challBuf = aInfo.Challenge();
        jni::ByteBuffer::LocalRef challenge = jni::ByteBuffer::New(
            const_cast<void*>(static_cast<const void*>(challBuf.Elements())),
            challBuf.Length());

        nsTArray<uint8_t> uidBuf;

        // Get authenticator selection criteria
        GECKOBUNDLE_START(authSelBundle);
        GECKOBUNDLE_START(extensionsBundle);
        GECKOBUNDLE_START(credentialBundle);

        const auto& rp = aInfo.Rp();
        const auto& user = aInfo.User();

        GECKOBUNDLE_PUT(credentialBundle, "isWebAuthn",
                        java::sdk::Integer::ValueOf(1));

        // Get the attestation preference and override if the user asked
        if (aForceNoneAttestation) {
          // Add UI support to trigger this, bug 1550164
          GECKOBUNDLE_PUT(authSelBundle, "attestationPreference",
                          jni::StringParam(u"none"_ns));
        } else {
          const nsString& attestation = aInfo.attestationConveyancePreference();
          GECKOBUNDLE_PUT(authSelBundle, "attestationPreference",
                          jni::StringParam(attestation));
        }

        const WebAuthnAuthenticatorSelection& sel =
            aInfo.AuthenticatorSelection();
        // Unfortunately, GMS's FIDO2 API has no option for Passkey. If using
        // residentKey, credential will be synced with Passkey via Google
        // account or credential provider service. So this is experimental.
        if (StaticPrefs::
                security_webauthn_webauthn_enable_android_fido2_residentkey()) {
          GECKOBUNDLE_PUT(authSelBundle, "residentKey",
                          jni::StringParam(sel.residentKey()));
        }

        if (sel.userVerificationRequirement().EqualsLiteral(
                MOZ_WEBAUTHN_USER_VERIFICATION_REQUIREMENT_REQUIRED)) {
          GECKOBUNDLE_PUT(authSelBundle, "requireUserVerification",
                          java::sdk::Integer::ValueOf(1));
        }

        if (sel.authenticatorAttachment().isSome()) {
          const nsString& authenticatorAttachment =
              sel.authenticatorAttachment().value();
          if (authenticatorAttachment.EqualsLiteral(
                  MOZ_WEBAUTHN_AUTHENTICATOR_ATTACHMENT_PLATFORM)) {
            GECKOBUNDLE_PUT(authSelBundle, "requirePlatformAttachment",
                            java::sdk::Integer::ValueOf(1));
          } else if (
              authenticatorAttachment.EqualsLiteral(
                  MOZ_WEBAUTHN_AUTHENTICATOR_ATTACHMENT_CROSS_PLATFORM)) {
            GECKOBUNDLE_PUT(authSelBundle, "requireCrossPlatformAttachment",
                            java::sdk::Integer::ValueOf(1));
          }
        }

        // Get extensions
        for (const WebAuthnExtension& ext : aInfo.Extensions()) {
          if (ext.type() == WebAuthnExtension::TWebAuthnExtensionAppId) {
            GECKOBUNDLE_PUT(
                extensionsBundle, "fidoAppId",
                jni::StringParam(
                    ext.get_WebAuthnExtensionAppId().appIdentifier()));
          }
        }

        uidBuf.Assign(user.Id());

        GECKOBUNDLE_PUT(credentialBundle, "rpName",
                        jni::StringParam(rp.Name()));
        GECKOBUNDLE_PUT(credentialBundle, "rpIcon",
                        jni::StringParam(rp.Icon()));
        GECKOBUNDLE_PUT(credentialBundle, "userName",
                        jni::StringParam(user.Name()));
        GECKOBUNDLE_PUT(credentialBundle, "userIcon",
                        jni::StringParam(user.Icon()));
        GECKOBUNDLE_PUT(credentialBundle, "userDisplayName",
                        jni::StringParam(user.DisplayName()));

        GECKOBUNDLE_PUT(credentialBundle, "rpId",
                        jni::StringParam(aInfo.RpId()));
        GECKOBUNDLE_PUT(credentialBundle, "origin",
                        jni::StringParam(aInfo.Origin()));
        GECKOBUNDLE_PUT(credentialBundle, "timeoutMS",
                        java::sdk::Double::New(aInfo.TimeoutMS()));

        GECKOBUNDLE_FINISH(authSelBundle);
        GECKOBUNDLE_FINISH(extensionsBundle);
        GECKOBUNDLE_FINISH(credentialBundle);

        // For non-WebAuthn cases, uidBuf is empty (and unused)
        jni::ByteBuffer::LocalRef uid = jni::ByteBuffer::New(
            const_cast<void*>(static_cast<const void*>(uidBuf.Elements())),
            uidBuf.Length());

        auto result = java::WebAuthnTokenManager::WebAuthnMakeCredential(
            credentialBundle, uid, challenge, idList, transportList,
            authSelBundle, extensionsBundle);
        auto geckoResult = java::GeckoResult::LocalRef(std::move(result));
        // This is likely running on the main thread, so we'll always dispatch
        // to the background for state updates.
        MozPromise<AndroidWebAuthnResult, AndroidWebAuthnResult,
                   true>::FromGeckoResult(geckoResult)
            ->Then(
                GetMainThreadSerialEventTarget(), __func__,
                [self = std::move(self)](AndroidWebAuthnResult&& aValue) {
                  self->HandleRegisterResult(std::move(aValue));
                },
                [self = std::move(self)](AndroidWebAuthnResult&& aValue) {
                  self->HandleRegisterResult(std::move(aValue));
                });
      }));

  return mRegisterPromise.Ensure(__func__);
}

void AndroidWebAuthnTokenManager::HandleRegisterResult(
    AndroidWebAuthnResult&& aResult) {
  if (!gAndroidPBackgroundThread) {
    // Promise is already rejected when shutting down background thread
    return;
  }
  // This is likely running on the main thread, so we'll always dispatch to the
  // background for state updates.
  if (aResult.IsError()) {
    nsresult aError = aResult.GetError();

    gAndroidPBackgroundThread->Dispatch(NS_NewRunnableFunction(
        "AndroidWebAuthnTokenManager::RegisterAbort",
        [self = RefPtr<AndroidWebAuthnTokenManager>(this), aError]() {
          self->mRegisterPromise.RejectIfExists(aError, __func__);
        }));
  } else {
    gAndroidPBackgroundThread->Dispatch(NS_NewRunnableFunction(
        "AndroidWebAuthnTokenManager::RegisterComplete",
        [self = RefPtr<AndroidWebAuthnTokenManager>(this),
         aResult = std::move(aResult)]() {
          nsTArray<WebAuthnExtensionResult> extensions;
          WebAuthnMakeCredentialResult result(aResult.mClientDataJSON,
                                              aResult.mAttObj,
                                              aResult.mKeyHandle, extensions);
          self->mRegisterPromise.Resolve(std::move(result), __func__);
        }));
  }
}

RefPtr<U2FSignPromise> AndroidWebAuthnTokenManager::Sign(
    const WebAuthnGetAssertionInfo& aInfo) {
  AssertIsOnOwningThread();

  ClearPromises();

  GetMainThreadSerialEventTarget()->Dispatch(NS_NewRunnableFunction(
      "java::WebAuthnTokenManager::WebAuthnGetAssertion",
      [self = RefPtr{this}, aInfo]() {
        AssertIsOnMainThread();

        jni::ObjectArray::LocalRef idList =
            jni::ObjectArray::New(aInfo.AllowList().Length());

        nsTArray<uint8_t> transportBuf;

        int ix = 0;
        for (const WebAuthnScopedCredential& cred : aInfo.AllowList()) {
          jni::ByteBuffer::LocalRef id = jni::ByteBuffer::New(
              const_cast<void*>(static_cast<const void*>(cred.id().Elements())),
              cred.id().Length());

          idList->SetElement(ix, id);
          transportBuf.AppendElement(cred.transports());

          ix += 1;
        }

        jni::ByteBuffer::LocalRef transportList = jni::ByteBuffer::New(
            const_cast<void*>(
                static_cast<const void*>(transportBuf.Elements())),
            transportBuf.Length());

        const nsTArray<uint8_t>& challBuf = aInfo.Challenge();
        jni::ByteBuffer::LocalRef challenge = jni::ByteBuffer::New(
            const_cast<void*>(static_cast<const void*>(challBuf.Elements())),
            challBuf.Length());

        // Get extensions
        GECKOBUNDLE_START(assertionBundle);
        GECKOBUNDLE_START(extensionsBundle);

        GECKOBUNDLE_PUT(assertionBundle, "isWebAuthn",
                        java::sdk::Integer::ValueOf(1));

        // User Verification Requirement is not currently used in the
        // Android FIDO API. Adding it should look like
        // AttestationConveyancePreference

        for (const WebAuthnExtension& ext : aInfo.Extensions()) {
          if (ext.type() == WebAuthnExtension::TWebAuthnExtensionAppId) {
            GECKOBUNDLE_PUT(
                extensionsBundle, "fidoAppId",
                jni::StringParam(
                    ext.get_WebAuthnExtensionAppId().appIdentifier()));
          }
        }

        GECKOBUNDLE_PUT(assertionBundle, "rpId",
                        jni::StringParam(aInfo.RpId()));
        GECKOBUNDLE_PUT(assertionBundle, "origin",
                        jni::StringParam(aInfo.Origin()));
        GECKOBUNDLE_PUT(assertionBundle, "timeoutMS",
                        java::sdk::Double::New(aInfo.TimeoutMS()));

        GECKOBUNDLE_FINISH(assertionBundle);
        GECKOBUNDLE_FINISH(extensionsBundle);

        auto result = java::WebAuthnTokenManager::WebAuthnGetAssertion(
            challenge, idList, transportList, assertionBundle,
            extensionsBundle);
        auto geckoResult = java::GeckoResult::LocalRef(std::move(result));
        MozPromise<AndroidWebAuthnResult, AndroidWebAuthnResult,
                   true>::FromGeckoResult(geckoResult)
            ->Then(
                GetMainThreadSerialEventTarget(), __func__,
                [self = std::move(self)](AndroidWebAuthnResult&& aValue) {
                  self->HandleSignResult(std::move(aValue));
                },
                [self = std::move(self)](AndroidWebAuthnResult&& aValue) {
                  self->HandleSignResult(std::move(aValue));
                });
      }));

  return mSignPromise.Ensure(__func__);
}

void AndroidWebAuthnTokenManager::HandleSignResult(
    AndroidWebAuthnResult&& aResult) {
  if (!gAndroidPBackgroundThread) {
    // Promise is already rejected when shutting down background thread
    return;
  }
  // This is likely running on the main thread, so we'll always dispatch to the
  // background for state updates.
  if (aResult.IsError()) {
    nsresult aError = aResult.GetError();

    gAndroidPBackgroundThread->Dispatch(NS_NewRunnableFunction(
        "AndroidWebAuthnTokenManager::SignAbort",
        [self = RefPtr<AndroidWebAuthnTokenManager>(this), aError]() {
          self->mSignPromise.RejectIfExists(aError, __func__);
        }));
  } else {
    gAndroidPBackgroundThread->Dispatch(NS_NewRunnableFunction(
        "AndroidWebAuthnTokenManager::SignComplete",
        [self = RefPtr<AndroidWebAuthnTokenManager>(this),
         aResult = std::move(aResult)]() {
          nsTArray<WebAuthnExtensionResult> emptyExtensions;
          WebAuthnGetAssertionResult result(
              aResult.mClientDataJSON, aResult.mKeyHandle, aResult.mSignature,
              aResult.mAuthData, emptyExtensions, aResult.mUserHandle);
          nsTArray<WebAuthnGetAssertionResultWrapper> results = {
              {result, mozilla::Nothing()}};
          self->mSignPromise.Resolve(std::move(results), __func__);
        }));
  }
}

void AndroidWebAuthnTokenManager::Cancel() {
  AssertIsOnOwningThread();

  ClearPromises();
}

AndroidWebAuthnResult::AndroidWebAuthnResult(
    const java::WebAuthnTokenManager::MakeCredentialResponse::LocalRef&
        aResponse) {
  mClientDataJSON.Assign(
      reinterpret_cast<const char*>(
          aResponse->ClientDataJson()->GetElements().Elements()),
      aResponse->ClientDataJson()->Length());
  mKeyHandle.Assign(reinterpret_cast<uint8_t*>(
                        aResponse->KeyHandle()->GetElements().Elements()),
                    aResponse->KeyHandle()->Length());
  mAttObj.Assign(reinterpret_cast<uint8_t*>(
                     aResponse->AttestationObject()->GetElements().Elements()),
                 aResponse->AttestationObject()->Length());
}

AndroidWebAuthnResult::AndroidWebAuthnResult(
    const java::WebAuthnTokenManager::GetAssertionResponse::LocalRef&
        aResponse) {
  mClientDataJSON.Assign(
      reinterpret_cast<const char*>(
          aResponse->ClientDataJson()->GetElements().Elements()),
      aResponse->ClientDataJson()->Length());
  mKeyHandle.Assign(reinterpret_cast<uint8_t*>(
                        aResponse->KeyHandle()->GetElements().Elements()),
                    aResponse->KeyHandle()->Length());
  mAuthData.Assign(reinterpret_cast<uint8_t*>(
                       aResponse->AuthData()->GetElements().Elements()),
                   aResponse->AuthData()->Length());
  mSignature.Assign(reinterpret_cast<uint8_t*>(
                        aResponse->Signature()->GetElements().Elements()),
                    aResponse->Signature()->Length());
  mUserHandle.Assign(reinterpret_cast<uint8_t*>(
                         aResponse->UserHandle()->GetElements().Elements()),
                     aResponse->UserHandle()->Length());
}

}  // namespace dom
}  // namespace mozilla