forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunexportable_key.cc
99 lines (83 loc) · 2.82 KB
/
unexportable_key.cc
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
// Copyright (c) 2021 The Chromium Authors. All rights reserved.
// 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 "base/bind.h"
#include "base/check.h"
#include "base/metrics/histogram_functions.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
namespace crypto {
UnexportableSigningKey::~UnexportableSigningKey() = default;
UnexportableKeyProvider::~UnexportableKeyProvider() = default;
#if defined(OS_WIN)
std::unique_ptr<UnexportableKeyProvider> GetUnexportableKeyProviderWin();
#endif
static std::unique_ptr<UnexportableKeyProvider> (*g_mock_provider)() = nullptr;
std::unique_ptr<UnexportableKeyProvider> GetUnexportableKeyProvider() {
if (g_mock_provider) {
return g_mock_provider();
}
#if defined(OS_WIN)
return GetUnexportableKeyProviderWin();
#else
return nullptr;
#endif
}
#if defined(OS_WIN)
// Note that values here are used in a recorded histogram. Don't change the
// values of existing members.
enum TPMSupport {
kNone = 0,
kRSA = 1,
kECDSA = 2,
kMaxValue = 2,
};
void MeasureTPMAvailability() {
// Temporarily measure the fraction of Windows machines that have TPMs, and
// what the best supported algorithm is.
// TODO: remove by 2021-04-01.
base::ThreadPool::PostTask(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
base::BindOnce([]() {
TPMSupport result = TPMSupport::kNone;
std::unique_ptr<UnexportableKeyProvider> provider =
GetUnexportableKeyProvider();
if (provider) {
const SignatureVerifier::SignatureAlgorithm kAllAlgorithms[] = {
SignatureVerifier::SignatureAlgorithm::ECDSA_SHA256,
SignatureVerifier::SignatureAlgorithm::RSA_PKCS1_SHA256,
};
base::Optional<SignatureVerifier::SignatureAlgorithm> algo =
provider->SelectAlgorithm(kAllAlgorithms);
if (algo) {
switch (*algo) {
case SignatureVerifier::SignatureAlgorithm::ECDSA_SHA256:
result = TPMSupport::kECDSA;
break;
case SignatureVerifier::SignatureAlgorithm::RSA_PKCS1_SHA256:
result = TPMSupport::kRSA;
break;
default:
break;
}
}
}
base::UmaHistogramEnumeration("Crypto.TPMSupport", result);
}));
}
#endif // OS_WIN
namespace internal {
void SetUnexportableKeyProviderForTesting(
std::unique_ptr<UnexportableKeyProvider> (*func)()) {
if (g_mock_provider) {
// Nesting ScopedMockUnexportableSigningKeyForTesting is not supported.
CHECK(!func);
g_mock_provider = nullptr;
} else {
g_mock_provider = func;
}
}
} // namespace internal
} // namespace crypto