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
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "crypto/unexportable_key.h"
#include <optional>
#include <tuple>
#include "base/logging.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "crypto/features.h"
#include "crypto/scoped_fake_unexportable_key_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_MAC)
#include "crypto/scoped_fake_apple_keychain_v2.h"
#endif // BUILDFLAG(IS_MAC)
#if BUILDFLAG(IS_WIN)
#include "crypto/scoped_cng_types.h"
#include "crypto/unexportable_key_win.h"
#endif // BUILDFLAG(IS_WIN)
namespace {
enum class Provider {
kTPM,
kFake,
kMicrosoftSoftware,
};
const Provider kAllProviders[] = {
Provider::kTPM,
Provider::kFake,
Provider::kMicrosoftSoftware,
};
const crypto::SignatureVerifier::SignatureAlgorithm kAllAlgorithms[] = {
crypto::SignatureVerifier::SignatureAlgorithm::ECDSA_SHA256,
crypto::SignatureVerifier::SignatureAlgorithm::RSA_PKCS1_SHA256,
};
#if BUILDFLAG(IS_MAC)
constexpr char kTestKeychainAccessGroup[] = "test-keychain-access-group";
#endif // BUILDFLAG(IS_MAC)
std::string ToString(Provider provider) {
switch (provider) {
case Provider::kTPM:
return "TPM";
case Provider::kFake:
return "Fake";
case Provider::kMicrosoftSoftware:
return "Microsoft Software";
}
}
class UnexportableKeySigningTest
: public testing::TestWithParam<
std::tuple<crypto::SignatureVerifier::SignatureAlgorithm,
Provider,
bool>> {
protected:
UnexportableKeySigningTest() {
scoped_feature_list_.InitWithFeatureState(
crypto::features::kIsHardwareBackedFixEnabled,
is_hardware_backed_fix_enabled());
}
std::unique_ptr<crypto::UnexportableKeyProvider> CreateProvider() {
if (provider_type() == Provider::kMicrosoftSoftware) {
return crypto::GetMicrosoftSoftwareUnexportableKeyProvider();
}
crypto::UnexportableKeyProvider::Config config{
#if BUILDFLAG(IS_MAC)
.keychain_access_group = kTestKeychainAccessGroup
#endif // BUILDLFAG(IS_MAC)
};
return crypto::GetUnexportableKeyProvider(std::move(config));
}
crypto::SignatureVerifier::SignatureAlgorithm algorithm() {
return std::get<0>(GetParam());
}
Provider provider_type() { return std::get<1>(GetParam()); }
bool is_hardware_backed_fix_enabled() { return std::get<2>(GetParam()); }
private:
base::test::ScopedFeatureList scoped_feature_list_;
#if BUILDFLAG(IS_MAC)
crypto::ScopedFakeAppleKeychainV2 scoped_fake_apple_keychain_{
kTestKeychainAccessGroup};
#endif // BUILDFLAG(IS_MAC)
};
INSTANTIATE_TEST_SUITE_P(All,
UnexportableKeySigningTest,
testing::Combine(testing::ValuesIn(kAllAlgorithms),
testing::ValuesIn(kAllProviders),
testing::Bool()));
TEST_P(UnexportableKeySigningTest, RoundTrip) {
const bool expected_is_hardware_backed =
provider_type() == Provider::kFake ? false
: is_hardware_backed_fix_enabled() ? (provider_type() == Provider::kTPM)
: true;
switch (algorithm()) {
case crypto::SignatureVerifier::SignatureAlgorithm::ECDSA_SHA256:
LOG(INFO) << "ECDSA P-256, provider=" << ToString(provider_type());
break;
case crypto::SignatureVerifier::SignatureAlgorithm::RSA_PKCS1_SHA256:
LOG(INFO) << "RSA, provider=" << ToString(provider_type());
break;
default:
ASSERT_TRUE(false);
}
SCOPED_TRACE(static_cast<int>(algorithm()));
SCOPED_TRACE(ToString(provider_type()));
std::optional<crypto::ScopedFakeUnexportableKeyProvider> fake;
if (provider_type() == Provider::kFake) {
fake.emplace();
}
const crypto::SignatureVerifier::SignatureAlgorithm algorithms[] = {
algorithm()};
std::unique_ptr<crypto::UnexportableKeyProvider> provider = CreateProvider();
if (!provider) {
LOG(INFO) << "Skipping test because of lack of hardware support.";
return;
}
if (!provider->SelectAlgorithm(algorithms)) {
LOG(INFO) << "Skipping test because of lack of support for this key type.";
return;
}
const base::TimeTicks generate_start = base::TimeTicks::Now();
std::unique_ptr<crypto::UnexportableSigningKey> key =
provider->GenerateSigningKeySlowly(algorithms);
if (algorithm() ==
crypto::SignatureVerifier::SignatureAlgorithm::ECDSA_SHA256) {
if (!key) {
GTEST_SKIP()
<< "Workaround for https://issues.chromium.org/issues/41494935";
}
}
ASSERT_TRUE(key);
EXPECT_EQ(key->IsHardwareBacked(), expected_is_hardware_backed);
LOG(INFO) << "Generation took " << (base::TimeTicks::Now() - generate_start);
ASSERT_EQ(key->Algorithm(), algorithm());
const std::vector<uint8_t> wrapped = key->GetWrappedKey();
const std::vector<uint8_t> spki = key->GetSubjectPublicKeyInfo();
const uint8_t msg[] = {1, 2, 3, 4};
const base::TimeTicks sign_start = base::TimeTicks::Now();
const std::optional<std::vector<uint8_t>> sig = key->SignSlowly(msg);
LOG(INFO) << "Signing took " << (base::TimeTicks::Now() - sign_start);
ASSERT_TRUE(sig);
crypto::SignatureVerifier verifier;
ASSERT_TRUE(verifier.VerifyInit(algorithm(), *sig, spki));
verifier.VerifyUpdate(msg);
ASSERT_TRUE(verifier.VerifyFinal());
const base::TimeTicks import2_start = base::TimeTicks::Now();
std::unique_ptr<crypto::UnexportableSigningKey> key2 =
provider->FromWrappedSigningKeySlowly(wrapped);
ASSERT_TRUE(key2);
LOG(INFO) << "Import took " << (base::TimeTicks::Now() - import2_start);
const base::TimeTicks sign2_start = base::TimeTicks::Now();
const std::optional<std::vector<uint8_t>> sig2 = key->SignSlowly(msg);
LOG(INFO) << "Signing took " << (base::TimeTicks::Now() - sign2_start);
ASSERT_TRUE(sig2);
crypto::SignatureVerifier verifier2;
ASSERT_TRUE(verifier2.VerifyInit(algorithm(), *sig2, spki));
verifier2.VerifyUpdate(msg);
ASSERT_TRUE(verifier2.VerifyFinal());
EXPECT_TRUE(provider->DeleteSigningKeySlowly(wrapped));
}
#if BUILDFLAG(IS_WIN)
TEST_P(UnexportableKeySigningTest, DuplicatePlatformKeyHandleSucceeds) {
if (provider_type() == Provider::kFake) {
GTEST_SKIP() << "Test only works with real platform keys.";
}
if (provider_type() == Provider::kMicrosoftSoftware &&
!is_hardware_backed_fix_enabled()) {
GTEST_SKIP() << "Fix for software keys is disabled.";
}
std::unique_ptr<crypto::UnexportableKeyProvider> provider = CreateProvider();
if (!provider) {
GTEST_SKIP() << "Skipping test because of lack of hardware support.";
}
const crypto::SignatureVerifier::SignatureAlgorithm algorithms[] = {
algorithm()};
auto key = provider->GenerateSigningKeySlowly(algorithms);
if (algorithm() ==
crypto::SignatureVerifier::SignatureAlgorithm::ECDSA_SHA256) {
if (!key) {
GTEST_SKIP()
<< "Workaround for https://issues.chromium.org/issues/41494935";
}
}
ASSERT_TRUE(key);
auto ncrypt_key = crypto::DuplicatePlatformKeyHandle(*key);
EXPECT_TRUE(ncrypt_key.is_valid());
}
#endif
} // namespace
|