xref: /aosp_15_r20/external/cronet/crypto/unexportable_key.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2021 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #include "crypto/unexportable_key.h"
6*6777b538SAndroid Build Coastguard Worker 
7*6777b538SAndroid Build Coastguard Worker #include "base/check.h"
8*6777b538SAndroid Build Coastguard Worker #include "base/functional/bind.h"
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker namespace crypto {
11*6777b538SAndroid Build Coastguard Worker 
12*6777b538SAndroid Build Coastguard Worker namespace {
13*6777b538SAndroid Build Coastguard Worker std::unique_ptr<UnexportableKeyProvider> (*g_mock_provider)() = nullptr;
14*6777b538SAndroid Build Coastguard Worker }  // namespace
15*6777b538SAndroid Build Coastguard Worker 
16*6777b538SAndroid Build Coastguard Worker UnexportableSigningKey::~UnexportableSigningKey() = default;
17*6777b538SAndroid Build Coastguard Worker UnexportableKeyProvider::~UnexportableKeyProvider() = default;
18*6777b538SAndroid Build Coastguard Worker 
19*6777b538SAndroid Build Coastguard Worker VirtualUnexportableSigningKey::~VirtualUnexportableSigningKey() = default;
20*6777b538SAndroid Build Coastguard Worker VirtualUnexportableKeyProvider::~VirtualUnexportableKeyProvider() = default;
21*6777b538SAndroid Build Coastguard Worker 
22*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
23*6777b538SAndroid Build Coastguard Worker std::unique_ptr<UnexportableKeyProvider> GetUnexportableKeyProviderWin();
24*6777b538SAndroid Build Coastguard Worker std::unique_ptr<VirtualUnexportableKeyProvider>
25*6777b538SAndroid Build Coastguard Worker GetVirtualUnexportableKeyProviderWin();
26*6777b538SAndroid Build Coastguard Worker #elif BUILDFLAG(IS_MAC)
27*6777b538SAndroid Build Coastguard Worker std::unique_ptr<UnexportableKeyProvider> GetUnexportableKeyProviderMac(
28*6777b538SAndroid Build Coastguard Worker     UnexportableKeyProvider::Config config);
29*6777b538SAndroid Build Coastguard Worker #endif
30*6777b538SAndroid Build Coastguard Worker 
31*6777b538SAndroid Build Coastguard Worker // Implemented in unexportable_key_software_unsecure.cc.
32*6777b538SAndroid Build Coastguard Worker std::unique_ptr<UnexportableKeyProvider>
33*6777b538SAndroid Build Coastguard Worker GetUnexportableKeyProviderSoftwareUnsecure();
34*6777b538SAndroid Build Coastguard Worker 
GetUnexportableKeyProvider(UnexportableKeyProvider::Config config)35*6777b538SAndroid Build Coastguard Worker std::unique_ptr<UnexportableKeyProvider> GetUnexportableKeyProvider(
36*6777b538SAndroid Build Coastguard Worker     UnexportableKeyProvider::Config config) {
37*6777b538SAndroid Build Coastguard Worker   if (g_mock_provider) {
38*6777b538SAndroid Build Coastguard Worker     return g_mock_provider();
39*6777b538SAndroid Build Coastguard Worker   }
40*6777b538SAndroid Build Coastguard Worker 
41*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
42*6777b538SAndroid Build Coastguard Worker   return GetUnexportableKeyProviderWin();
43*6777b538SAndroid Build Coastguard Worker #elif BUILDFLAG(IS_MAC)
44*6777b538SAndroid Build Coastguard Worker   return GetUnexportableKeyProviderMac(std::move(config));
45*6777b538SAndroid Build Coastguard Worker #else
46*6777b538SAndroid Build Coastguard Worker   return nullptr;
47*6777b538SAndroid Build Coastguard Worker #endif
48*6777b538SAndroid Build Coastguard Worker }
49*6777b538SAndroid Build Coastguard Worker 
50*6777b538SAndroid Build Coastguard Worker std::unique_ptr<VirtualUnexportableKeyProvider>
GetVirtualUnexportableKeyProvider_DO_NOT_USE_METRICS_ONLY()51*6777b538SAndroid Build Coastguard Worker GetVirtualUnexportableKeyProvider_DO_NOT_USE_METRICS_ONLY() {
52*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
53*6777b538SAndroid Build Coastguard Worker   return GetVirtualUnexportableKeyProviderWin();
54*6777b538SAndroid Build Coastguard Worker #else
55*6777b538SAndroid Build Coastguard Worker   return nullptr;
56*6777b538SAndroid Build Coastguard Worker #endif
57*6777b538SAndroid Build Coastguard Worker }
58*6777b538SAndroid Build Coastguard Worker 
59*6777b538SAndroid Build Coastguard Worker namespace internal {
60*6777b538SAndroid Build Coastguard Worker 
SetUnexportableKeyProviderForTesting(std::unique_ptr<UnexportableKeyProvider> (* func)())61*6777b538SAndroid Build Coastguard Worker void SetUnexportableKeyProviderForTesting(
62*6777b538SAndroid Build Coastguard Worker     std::unique_ptr<UnexportableKeyProvider> (*func)()) {
63*6777b538SAndroid Build Coastguard Worker   if (g_mock_provider) {
64*6777b538SAndroid Build Coastguard Worker     // Nesting ScopedMockUnexportableSigningKeyForTesting is not supported.
65*6777b538SAndroid Build Coastguard Worker     CHECK(!func);
66*6777b538SAndroid Build Coastguard Worker     g_mock_provider = nullptr;
67*6777b538SAndroid Build Coastguard Worker   } else {
68*6777b538SAndroid Build Coastguard Worker     g_mock_provider = func;
69*6777b538SAndroid Build Coastguard Worker   }
70*6777b538SAndroid Build Coastguard Worker }
71*6777b538SAndroid Build Coastguard Worker 
72*6777b538SAndroid Build Coastguard Worker }  // namespace internal
73*6777b538SAndroid Build Coastguard Worker }  // namespace crypto
74