xref: /aosp_15_r20/external/cronet/crypto/chaps_support.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2020 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/chaps_support.h"
6*6777b538SAndroid Build Coastguard Worker 
7*6777b538SAndroid Build Coastguard Worker #include <dlfcn.h>
8*6777b538SAndroid Build Coastguard Worker #include <secmod.h>
9*6777b538SAndroid Build Coastguard Worker #include <secmodt.h>
10*6777b538SAndroid Build Coastguard Worker 
11*6777b538SAndroid Build Coastguard Worker #include <string_view>
12*6777b538SAndroid Build Coastguard Worker 
13*6777b538SAndroid Build Coastguard Worker #include "base/logging.h"
14*6777b538SAndroid Build Coastguard Worker #include "base/memory/raw_ptr_exclusion.h"
15*6777b538SAndroid Build Coastguard Worker #include "base/memory/stack_allocated.h"
16*6777b538SAndroid Build Coastguard Worker #include "base/threading/scoped_blocking_call.h"
17*6777b538SAndroid Build Coastguard Worker #include "crypto/scoped_nss_types.h"
18*6777b538SAndroid Build Coastguard Worker #include "nss_util_internal.h"
19*6777b538SAndroid Build Coastguard Worker 
20*6777b538SAndroid Build Coastguard Worker namespace crypto {
21*6777b538SAndroid Build Coastguard Worker 
22*6777b538SAndroid Build Coastguard Worker namespace {
23*6777b538SAndroid Build Coastguard Worker 
24*6777b538SAndroid Build Coastguard Worker // Constants for loading the Chrome OS TPM-backed PKCS #11 library.
25*6777b538SAndroid Build Coastguard Worker const char kChapsModuleName[] = "Chaps";
26*6777b538SAndroid Build Coastguard Worker const char kChapsPath[] = "libchaps.so";
27*6777b538SAndroid Build Coastguard Worker 
28*6777b538SAndroid Build Coastguard Worker class ScopedChapsLoadFixup {
29*6777b538SAndroid Build Coastguard Worker   STACK_ALLOCATED();
30*6777b538SAndroid Build Coastguard Worker 
31*6777b538SAndroid Build Coastguard Worker  public:
32*6777b538SAndroid Build Coastguard Worker   ScopedChapsLoadFixup();
33*6777b538SAndroid Build Coastguard Worker   ~ScopedChapsLoadFixup();
34*6777b538SAndroid Build Coastguard Worker 
35*6777b538SAndroid Build Coastguard Worker  private:
36*6777b538SAndroid Build Coastguard Worker #if defined(COMPONENT_BUILD)
37*6777b538SAndroid Build Coastguard Worker   // This field stores a handle and is not a pointer to PA memory.
38*6777b538SAndroid Build Coastguard Worker   // Also, this class is always stack-allocated and visibility is limited.
39*6777b538SAndroid Build Coastguard Worker   // Hence no benefit from using raw_ptr<void>.
40*6777b538SAndroid Build Coastguard Worker   RAW_PTR_EXCLUSION void* chaps_handle_;
41*6777b538SAndroid Build Coastguard Worker #endif
42*6777b538SAndroid Build Coastguard Worker };
43*6777b538SAndroid Build Coastguard Worker 
44*6777b538SAndroid Build Coastguard Worker #if defined(COMPONENT_BUILD)
45*6777b538SAndroid Build Coastguard Worker 
ScopedChapsLoadFixup()46*6777b538SAndroid Build Coastguard Worker ScopedChapsLoadFixup::ScopedChapsLoadFixup() {
47*6777b538SAndroid Build Coastguard Worker   // HACK: libchaps links the system protobuf and there are symbol conflicts
48*6777b538SAndroid Build Coastguard Worker   // with the bundled copy. Load chaps with RTLD_DEEPBIND to workaround.
49*6777b538SAndroid Build Coastguard Worker   chaps_handle_ = dlopen(kChapsPath, RTLD_LOCAL | RTLD_NOW | RTLD_DEEPBIND);
50*6777b538SAndroid Build Coastguard Worker }
51*6777b538SAndroid Build Coastguard Worker 
~ScopedChapsLoadFixup()52*6777b538SAndroid Build Coastguard Worker ScopedChapsLoadFixup::~ScopedChapsLoadFixup() {
53*6777b538SAndroid Build Coastguard Worker   // LoadNSSModule() will have taken a 2nd reference.
54*6777b538SAndroid Build Coastguard Worker   if (chaps_handle_)
55*6777b538SAndroid Build Coastguard Worker     dlclose(chaps_handle_);
56*6777b538SAndroid Build Coastguard Worker }
57*6777b538SAndroid Build Coastguard Worker 
58*6777b538SAndroid Build Coastguard Worker #else
59*6777b538SAndroid Build Coastguard Worker 
60*6777b538SAndroid Build Coastguard Worker ScopedChapsLoadFixup::ScopedChapsLoadFixup() = default;
61*6777b538SAndroid Build Coastguard Worker ScopedChapsLoadFixup::~ScopedChapsLoadFixup() = default;
62*6777b538SAndroid Build Coastguard Worker 
63*6777b538SAndroid Build Coastguard Worker #endif  // defined(COMPONENT_BUILD)
64*6777b538SAndroid Build Coastguard Worker 
65*6777b538SAndroid Build Coastguard Worker }  // namespace
66*6777b538SAndroid Build Coastguard Worker 
LoadChaps()67*6777b538SAndroid Build Coastguard Worker SECMODModule* LoadChaps() {
68*6777b538SAndroid Build Coastguard Worker   // NSS functions may reenter //net via extension hooks. If the reentered
69*6777b538SAndroid Build Coastguard Worker   // code needs to synchronously wait for a task to run but the thread pool in
70*6777b538SAndroid Build Coastguard Worker   // which that task must run doesn't have enough threads to schedule it, a
71*6777b538SAndroid Build Coastguard Worker   // deadlock occurs. To prevent that, the base::ScopedBlockingCall below
72*6777b538SAndroid Build Coastguard Worker   // increments the thread pool capacity for the duration of the TPM
73*6777b538SAndroid Build Coastguard Worker   // initialization.
74*6777b538SAndroid Build Coastguard Worker   base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
75*6777b538SAndroid Build Coastguard Worker                                                 base::BlockingType::WILL_BLOCK);
76*6777b538SAndroid Build Coastguard Worker 
77*6777b538SAndroid Build Coastguard Worker   ScopedChapsLoadFixup chaps_loader;
78*6777b538SAndroid Build Coastguard Worker 
79*6777b538SAndroid Build Coastguard Worker   DVLOG(3) << "Loading chaps...";
80*6777b538SAndroid Build Coastguard Worker   return LoadNSSModule(
81*6777b538SAndroid Build Coastguard Worker       kChapsModuleName, kChapsPath,
82*6777b538SAndroid Build Coastguard Worker       // For more details on these parameters, see:
83*6777b538SAndroid Build Coastguard Worker       // https://developer.mozilla.org/en/PKCS11_Module_Specs
84*6777b538SAndroid Build Coastguard Worker       // slotFlags=[PublicCerts] -- Certificates and public keys can be
85*6777b538SAndroid Build Coastguard Worker       //   read from this slot without requiring a call to C_Login.
86*6777b538SAndroid Build Coastguard Worker       // askpw=only -- Only authenticate to the token when necessary.
87*6777b538SAndroid Build Coastguard Worker       "NSS=\"slotParams=(0={slotFlags=[PublicCerts] askpw=only})\"");
88*6777b538SAndroid Build Coastguard Worker }
89*6777b538SAndroid Build Coastguard Worker 
GetChapsSlot(SECMODModule * chaps_module,CK_SLOT_ID slot_id)90*6777b538SAndroid Build Coastguard Worker ScopedPK11Slot GetChapsSlot(SECMODModule* chaps_module, CK_SLOT_ID slot_id) {
91*6777b538SAndroid Build Coastguard Worker   DCHECK(chaps_module);
92*6777b538SAndroid Build Coastguard Worker 
93*6777b538SAndroid Build Coastguard Worker   // NSS functions may reenter //net via extension hooks. If the reentered
94*6777b538SAndroid Build Coastguard Worker   // code needs to synchronously wait for a task to run but the thread pool in
95*6777b538SAndroid Build Coastguard Worker   // which that task must run doesn't have enough threads to schedule it, a
96*6777b538SAndroid Build Coastguard Worker   // deadlock occurs. To prevent that, the base::ScopedBlockingCall below
97*6777b538SAndroid Build Coastguard Worker   // increments the thread pool capacity for the duration of the TPM
98*6777b538SAndroid Build Coastguard Worker   // initialization.
99*6777b538SAndroid Build Coastguard Worker   base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
100*6777b538SAndroid Build Coastguard Worker                                                 base::BlockingType::WILL_BLOCK);
101*6777b538SAndroid Build Coastguard Worker 
102*6777b538SAndroid Build Coastguard Worker   DVLOG(3) << "Poking chaps module.";
103*6777b538SAndroid Build Coastguard Worker   SECStatus rv = SECMOD_UpdateSlotList(chaps_module);
104*6777b538SAndroid Build Coastguard Worker   if (rv != SECSuccess)
105*6777b538SAndroid Build Coastguard Worker     LOG(ERROR) << "SECMOD_UpdateSlotList failed: " << PORT_GetError();
106*6777b538SAndroid Build Coastguard Worker 
107*6777b538SAndroid Build Coastguard Worker   ScopedPK11Slot slot =
108*6777b538SAndroid Build Coastguard Worker       ScopedPK11Slot(SECMOD_LookupSlot(chaps_module->moduleID, slot_id));
109*6777b538SAndroid Build Coastguard Worker   if (!slot)
110*6777b538SAndroid Build Coastguard Worker     LOG(ERROR) << "TPM slot " << slot_id << " not found.";
111*6777b538SAndroid Build Coastguard Worker   return slot;
112*6777b538SAndroid Build Coastguard Worker }
113*6777b538SAndroid Build Coastguard Worker 
IsChapsModule(SECMODModule * pk11_module)114*6777b538SAndroid Build Coastguard Worker bool IsChapsModule(SECMODModule* pk11_module) {
115*6777b538SAndroid Build Coastguard Worker   return pk11_module && std::string_view(pk11_module->commonName) ==
116*6777b538SAndroid Build Coastguard Worker                             std::string_view(kChapsModuleName);
117*6777b538SAndroid Build Coastguard Worker }
118*6777b538SAndroid Build Coastguard Worker 
IsSlotProvidedByChaps(PK11SlotInfo * slot)119*6777b538SAndroid Build Coastguard Worker bool IsSlotProvidedByChaps(PK11SlotInfo* slot) {
120*6777b538SAndroid Build Coastguard Worker   return slot && IsChapsModule(PK11_GetModule(slot));
121*6777b538SAndroid Build Coastguard Worker }
122*6777b538SAndroid Build Coastguard Worker 
123*6777b538SAndroid Build Coastguard Worker }  // namespace crypto
124