File: unexportable_key_utils_chromeos.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (196 lines) | stat: -rw-r--r-- 7,003 bytes parent folder | download | duplicates (3)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/webauthn/unexportable_key_utils.h"

#include <cstdint>
#include <string>

#include "ash/constants/ash_features.h"
#include "ash/public/cpp/auth/active_session_auth_controller.h"
#include "ash/public/cpp/webauthn_dialog_controller.h"
#include "base/base64.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "chromeos/ash/components/osauth/impl/request/webauthn_auth_request.h"
#include "crypto/unexportable_key.h"
#include "crypto/user_verifying_key.h"
#include "third_party/abseil-cpp/absl/functional/overload.h"
#include "ui/aura/window.h"

namespace {

std::unique_ptr<crypto::UserVerifyingKeyProvider> (*g_test_override)() =
    nullptr;

// UserVerifyingSigningKeyCros wraps an `UnexportableSigningKey` and presents an
// Ash InSessionAuth dialog prior to releasing a signature. The wrapped key is
// currently not hardware bound.
class UserVerifyingSigningKeyCros : public crypto::UserVerifyingSigningKey {
 public:
  explicit UserVerifyingSigningKeyCros(
      UserVerifyingKeyProviderConfigChromeos config,
      std::unique_ptr<crypto::UnexportableSigningKey> signing_key,
      std::string label)
      : config_(std::move(config)),
        signing_key_(std::move(signing_key)),
        label_(std::move(label)) {
    CHECK(signing_key_);
  }

  ~UserVerifyingSigningKeyCros() override = default;

  void Sign(base::span<const uint8_t> data,
            UserVerifyingKeySignatureCallback callback) override {
    CHECK(config_.window);
    VerifyUser(base::BindOnce(
        &UserVerifyingSigningKeyCros::DoSign, weak_factory_.GetWeakPtr(),
        std::vector<uint8_t>(data.begin(), data.end()), std::move(callback)));
  }

  std::vector<uint8_t> GetPublicKey() const override {
    return signing_key_->GetSubjectPublicKeyInfo();
  }

  const crypto::UserVerifyingKeyLabel& GetKeyLabel() const override {
    return label_;
  }

 private:
  void VerifyUser(base::OnceCallback<void(bool)> callback) {
    auto visitor = absl::Overload(
        // Legacy code path.
        [&](raw_ptr<ash::WebAuthNDialogController> controller) {
          CHECK(controller);
          controller->ShowAuthenticationDialog(config_.window, config_.rp_id,
                                               std::move(callback));
        },

        // New code path.
        [&](raw_ptr<ash::ActiveSessionAuthController> controller) {
          CHECK(controller);
          auto webauthn_auth_request =
              std::make_unique<ash::WebAuthNAuthRequest>(config_.rp_id,
                                                         std::move(callback));

          controller->ShowAuthDialog(std::move(webauthn_auth_request));
        });

    std::visit(visitor, config_.dialog_controller);
  }

  void DoSign(std::vector<uint8_t> data,
              UserVerifyingKeySignatureCallback callback,
              bool uv_success) {
    if (!uv_success) {
      std::move(callback).Run(base::unexpected(
          crypto::UserVerifyingKeySigningError::kUserCancellation));
      return;
    }
    std::optional<std::vector<uint8_t>> signature =
        signing_key_->SignSlowly(data);
    if (!signature) {
      std::move(callback).Run(base::unexpected(
          crypto::UserVerifyingKeySigningError::kUnknownError));
      return;
    }
    std::move(callback).Run(std::move(*signature));
  }

  const UserVerifyingKeyProviderConfigChromeos config_;
  const std::unique_ptr<crypto::UnexportableSigningKey> signing_key_;
  const std::string label_;

  base::WeakPtrFactory<UserVerifyingSigningKeyCros> weak_factory_{this};
};

class UserVerifyingKeyProviderCros : public crypto::UserVerifyingKeyProvider {
 public:
  explicit UserVerifyingKeyProviderCros(
      UserVerifyingKeyProviderConfigChromeos config)
      : config_(config) {}

  ~UserVerifyingKeyProviderCros() override = default;

  void GenerateUserVerifyingSigningKey(
      base::span<const crypto::SignatureVerifier::SignatureAlgorithm>
          acceptable_algorithms,
      UserVerifyingKeyCreationCallback callback) override {
    std::unique_ptr<crypto::UnexportableSigningKey> key =
        crypto::GetSoftwareUnsecureUnexportableKeyProvider()
            ->GenerateSigningKeySlowly(acceptable_algorithms);
    std::string label = base::Base64Encode(key->GetWrappedKey());
    std::move(callback).Run(std::make_unique<UserVerifyingSigningKeyCros>(
        config_, std::move(key), std::move(label)));
  }

  void GetUserVerifyingSigningKey(
      crypto::UserVerifyingKeyLabel key_label,
      UserVerifyingKeyCreationCallback callback) override {
    std::optional<std::vector<uint8_t>> wrapped_private_key =
        base::Base64Decode(key_label);
    if (!wrapped_private_key) {
      std::move(callback).Run(
          base::unexpected(crypto::UserVerifyingKeyCreationError::kNotFound));
      return;
    }
    std::unique_ptr<crypto::UnexportableSigningKey> signing_key =
        crypto::GetSoftwareUnsecureUnexportableKeyProvider()
            ->FromWrappedSigningKeySlowly(*wrapped_private_key);
    std::move(callback).Run(std::make_unique<UserVerifyingSigningKeyCros>(
        config_, std::move(signing_key), std::move(key_label)));
  }

  void DeleteUserVerifyingKey(
      crypto::UserVerifyingKeyLabel key_label,
      base::OnceCallback<void(bool)> callback) override {
    // UserVerifyingSigningKeyCros is stateless, so this is a no-op.
    std::move(callback).Run(true);
  }

 private:
  UserVerifyingKeyProviderConfigChromeos config_;
};

}  // namespace

UserVerifyingKeyProviderConfigChromeos::UserVerifyingKeyProviderConfigChromeos(
    AuthDialogController dialog_controller,
    aura::Window* window,
    std::string rp_id)
    : dialog_controller(dialog_controller),
      window(window),
      rp_id(std::move(rp_id)) {}

UserVerifyingKeyProviderConfigChromeos::
    ~UserVerifyingKeyProviderConfigChromeos() = default;

UserVerifyingKeyProviderConfigChromeos::UserVerifyingKeyProviderConfigChromeos(
    const UserVerifyingKeyProviderConfigChromeos&) = default;

UserVerifyingKeyProviderConfigChromeos&
UserVerifyingKeyProviderConfigChromeos::operator=(
    const UserVerifyingKeyProviderConfigChromeos&) = default;

std::unique_ptr<crypto::UserVerifyingKeyProvider>
GetWebAuthnUserVerifyingKeyProvider(
    UserVerifyingKeyProviderConfigChromeos config) {
  if (g_test_override) {
    return g_test_override();
  }
  return std::make_unique<UserVerifyingKeyProviderCros>(std::move(config));
}

// ChromeOS doesn't use the UserVerifyingKeyProvider provided by //crypto, so
// the test override is handled separately as well.
void OverrideWebAuthnChromeosUserVerifyingKeyProviderForTesting(
    std::unique_ptr<crypto::UserVerifyingKeyProvider> (*func)()) {
  if (g_test_override) {
    // Prevent nesting of scoped providers.
    CHECK(!func);
    g_test_override = nullptr;
  } else {
    g_test_override = func;
  }
}