File: passkey_model_utils_unittest.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 (227 lines) | stat: -rw-r--r-- 9,766 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
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/webauthn/core/browser/passkey_model_utils.h"

#include "components/sync/protocol/webauthn_credential_specifics.pb.h"
#include "components/webauthn/core/browser/passkey_model.h"
#include "crypto/keypair.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace webauthn::passkey_model_utils {
namespace {

constexpr std::array<uint8_t, 32> kTestKey = {
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
constexpr int32_t kTestKeyVersion = 23;
constexpr std::string_view kRpId = "example.com";
static const PasskeyModel::UserEntity kTestUser(std::vector<uint8_t>{1, 2, 3},
                                                "user@example.com",
                                                "Example User");

// Test decryption of the `encrypted` case for
// `WebAuthnCredentialSpecifics.encrypted_data`.
TEST(PasskeyModelUtilsTest, DecryptWebauthnCredentialSpecificsData_Encrypted) {
  static const struct {
    base::span<const char> encrypted;
    bool result;
    struct {
      std::string private_key;
      std::string hmac_secret;
      std::string cred_blob;
      std::string large_blob;
      uint64_t large_blob_uncompressed_size;
    } expected;
  } kTestCases[] = {
      {"", false, {}},
      // Short ciphertext
      {"\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a", false, {}},
      // Invalid message
      {"\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x24\xd4\xf2\x4f\x65"
       "\xab\x39\x94\x89\x6c\x9d\x27\x83\x0e\xac\x1a\xff",
       false,
       {}},
      // Empty message
      {"\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x9d\xfb\xc6\xda\x41"
       "\x6f\x5f\x7c\x06\x84\x02\xf8\x7f\x13\x61\x2f\xe4\xae\x37\x1b\x40\x7b"
       "\x8a\x65\x94\x65",
       true,
       {}},
      {"\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x9d\xf5\xa0\xbf\x28"
       "\x1b\x0d\x0e\x47\xf2\x0f\x33\x7d\x71\xe7\x62\x00\x25\xe4\xde\x99\x78"
       "\x0c\x2a\xa8\xe3\x30\x4c\xc2\x1e\xa4\x53\x25\xba\xdc\xa1\x21\xfb\x11"
       "\x0c\x40\x92\x08\xa8\x8f\xb8\x9f\xaa\xad\x51\xfc\xf9\x75\x9a\xbe\x91"
       "\x0e\xaf\xb4\x5c\x46\x0a\x05\x9e\xb2\xda\x98\xd0\xb3\x87\xd2\x3c\x52"
       "\x57\xb2\x57\x08\xb7\x18",
       true,
       {"testprivatekey", "testhmacsecret", "testcredblob", "testlargeblob",
        23}},
  };
  int i = 0;
  for (const auto& t : kTestCases) {
    SCOPED_TRACE(testing::Message() << i++);
    sync_pb::WebauthnCredentialSpecifics in;
    in.set_encrypted({t.encrypted.begin(), t.encrypted.end() - 1});
    sync_pb::WebauthnCredentialSpecifics_Encrypted out;
    EXPECT_EQ(DecryptWebauthnCredentialSpecificsData(kTestKey, in, &out),
              t.result);
    EXPECT_EQ(out.private_key(), t.expected.private_key);
    EXPECT_EQ(out.hmac_secret(), t.expected.hmac_secret);
    EXPECT_EQ(out.cred_blob(), t.expected.cred_blob);
    EXPECT_EQ(out.large_blob(), t.expected.large_blob);
    EXPECT_EQ(out.large_blob_uncompressed_size(),
              t.expected.large_blob_uncompressed_size);
  }
}

// Test decryption of the `private_key` case for
// `WebAuthnCredentialSpecifics.encrypted_data`.
TEST(PasskeyModelUtilsTest, DecryptWebauthnCredentialSpecificsData_PrivateKey) {
  static const struct {
    base::span<const char> encrypted;
    bool result;
    std::string expected_private_key;
  } kTestCases[] = {
      {"", false, {}},
      // Short ciphertext
      {"\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a", false, ""},
      // Empty key
      {"\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x54\x2a\x4c\x37\xe0"
       "\x35\xbc\xc6\x64\x9d\x57\x9c\x8f\x12\xe6\xa3",
       true, ""},
      {"\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\xe3\x9e\xa7\xae\x2b"
       "\x1d\x14\x0a\x4f\xf0\x0b\x2c\x7d\x63\xc6\x88\x33\x45\x2f\xbb\x29\x73"
       "\xff\xdd\xc2\x63\xfc\x57\xae\x3a",
       true, "testprivatekey"},
  };
  int i = 0;
  for (const auto& t : kTestCases) {
    SCOPED_TRACE(testing::Message() << i++);
    sync_pb::WebauthnCredentialSpecifics in;
    in.set_private_key({t.encrypted.begin(), t.encrypted.end() - 1});
    sync_pb::WebauthnCredentialSpecifics_Encrypted out;
    EXPECT_EQ(DecryptWebauthnCredentialSpecificsData(kTestKey, in, &out),
              t.result);
    EXPECT_EQ(out.private_key(), t.expected_private_key);
    EXPECT_FALSE(out.has_hmac_secret());
    EXPECT_FALSE(out.has_cred_blob());
    EXPECT_FALSE(out.has_large_blob());
    EXPECT_FALSE(out.has_large_blob_uncompressed_size());
  }
}

TEST(PasskeyModelUtilsTest, DecryptWebauthnCredentialSpecificsData_NotSet) {
  sync_pb::WebauthnCredentialSpecifics in;
  sync_pb::WebauthnCredentialSpecifics_Encrypted out;
  EXPECT_FALSE(DecryptWebauthnCredentialSpecificsData(kTestKey, in, &out));
}

TEST(PasskeyModelUtilsTest, EncryptWebauthnCredentialSpecificsData) {
  sync_pb::WebauthnCredentialSpecifics_Encrypted plain;
  plain.set_private_key("a");
  plain.set_hmac_secret("b");
  plain.set_cred_blob("c");
  plain.set_large_blob("d");
  plain.set_large_blob_uncompressed_size(1u);
  sync_pb::WebauthnCredentialSpecifics encrypted;
  ASSERT_TRUE(
      EncryptWebauthnCredentialSpecificsData(kTestKey, plain, &encrypted));
  EXPECT_TRUE(encrypted.has_encrypted());

  sync_pb::WebauthnCredentialSpecifics_Encrypted decrypted;
  EXPECT_TRUE(
      DecryptWebauthnCredentialSpecificsData(kTestKey, encrypted, &decrypted));
  EXPECT_EQ(decrypted.private_key(), plain.private_key());
  EXPECT_EQ(decrypted.hmac_secret(), plain.hmac_secret());
  EXPECT_EQ(decrypted.cred_blob(), plain.cred_blob());
  EXPECT_EQ(decrypted.large_blob(), plain.large_blob());
  EXPECT_EQ(decrypted.large_blob_uncompressed_size(),
            plain.large_blob_uncompressed_size());
}

TEST(PasskeyModelUtilsTest, GeneratePasskeyAndEncryptSecrets) {
  auto [passkey, public_key_spki_der] = GeneratePasskeyAndEncryptSecrets(
      kRpId, kTestUser, kTestKey, kTestKeyVersion, /*extension_input_data=*/{},
      /*extension_output_data=*/nullptr);
  EXPECT_EQ(passkey.sync_id().size(), 16u);
  EXPECT_EQ(passkey.credential_id().size(), 16u);
  EXPECT_EQ(passkey.rp_id(), kRpId);
  EXPECT_EQ(passkey.user_id(),
            std::string(reinterpret_cast<const char*>(kTestUser.id.data()),
                        kTestUser.id.size()));
  EXPECT_EQ(passkey.user_name(), kTestUser.name);
  EXPECT_EQ(passkey.user_display_name(), kTestUser.display_name);
  EXPECT_FALSE(passkey.third_party_payments_support());
  EXPECT_EQ(passkey.last_used_time_windows_epoch_micros(), 0u);
  EXPECT_GT(passkey.creation_time(), 0u);
  EXPECT_EQ(passkey.key_version(), kTestKeyVersion);

  // Filled in by the Sync model.
  EXPECT_TRUE(passkey.newly_shadowed_credential_ids().empty());

  EXPECT_TRUE(passkey.has_encrypted());
  sync_pb::WebauthnCredentialSpecifics_Encrypted encrypted_data;
  ASSERT_TRUE(DecryptWebauthnCredentialSpecificsData(kTestKey, passkey,
                                                     &encrypted_data));
  EXPECT_FALSE(encrypted_data.private_key().empty());
  auto ec_key = crypto::keypair::PrivateKey::FromPrivateKeyInfo(
      base::as_byte_span(encrypted_data.private_key()));
  ASSERT_TRUE(ec_key.has_value());
  ASSERT_TRUE(ec_key->IsEc());
  std::vector<uint8_t> ec_key_pub = ec_key->ToSubjectPublicKeyInfo();
  EXPECT_EQ(ec_key_pub, public_key_spki_der);

  EXPECT_TRUE(encrypted_data.hmac_secret().empty());
  EXPECT_TRUE(encrypted_data.cred_blob().empty());
  EXPECT_TRUE(encrypted_data.large_blob().empty());
  EXPECT_EQ(encrypted_data.large_blob_uncompressed_size(), 0u);
}

TEST(PasskeyModelUtilsTest, GeneratePasskeyWithPRFAndEncryptSecrets) {
  std::vector<uint8_t> prf_input1, prf_input2;
  prf_input1.emplace_back('a');
  ExtensionInputData extension_input_data(prf_input1, prf_input2);
  ExtensionOutputData extension_output_data;
  auto [passkey, public_key_spki_der] = GeneratePasskeyAndEncryptSecrets(
      kRpId, kTestUser, kTestKey, kTestKeyVersion, extension_input_data,
      &extension_output_data);
  EXPECT_EQ(passkey.sync_id().size(), 16u);
  EXPECT_EQ(passkey.credential_id().size(), 16u);
  EXPECT_EQ(passkey.rp_id(), kRpId);
  EXPECT_EQ(passkey.user_id(),
            std::string(reinterpret_cast<const char*>(kTestUser.id.data()),
                        kTestUser.id.size()));
  EXPECT_EQ(passkey.user_name(), kTestUser.name);
  EXPECT_EQ(passkey.user_display_name(), kTestUser.display_name);
  EXPECT_FALSE(passkey.third_party_payments_support());
  EXPECT_EQ(passkey.last_used_time_windows_epoch_micros(), 0u);
  EXPECT_GT(passkey.creation_time(), 0u);
  EXPECT_EQ(passkey.key_version(), kTestKeyVersion);

  // Filled in by the Sync model.
  EXPECT_TRUE(passkey.newly_shadowed_credential_ids().empty());

  EXPECT_TRUE(passkey.has_encrypted());
  sync_pb::WebauthnCredentialSpecifics_Encrypted encrypted_data;
  ASSERT_TRUE(DecryptWebauthnCredentialSpecificsData(kTestKey, passkey,
                                                     &encrypted_data));
  EXPECT_FALSE(encrypted_data.private_key().empty());
  auto ec_key = crypto::keypair::PrivateKey::FromPrivateKeyInfo(
      base::as_byte_span(encrypted_data.private_key()));
  ASSERT_TRUE(ec_key.has_value());
  ASSERT_TRUE(ec_key->IsEc());
  std::vector<uint8_t> ec_key_pub = ec_key->ToSubjectPublicKeyInfo();
  EXPECT_EQ(ec_key_pub, public_key_spki_der);

  EXPECT_EQ(encrypted_data.hmac_secret().size(), 32u);
  EXPECT_TRUE(encrypted_data.cred_blob().empty());
  EXPECT_TRUE(encrypted_data.large_blob().empty());
  EXPECT_EQ(encrypted_data.large_blob_uncompressed_size(), 0u);

  EXPECT_EQ(extension_output_data.prf_result.size(), 32u);
}

}  // namespace
}  // namespace webauthn::passkey_model_utils