RootCertificateTelemetryUtils.cpp (4846B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "RootCertificateTelemetryUtils.h" 8 9 #include "PKCS11ModuleDB.h" 10 #include "RootHashes.inc" // Note: Generated by genRootCAHashes.js 11 #include "ScopedNSSTypes.h" 12 #include "mozilla/Logging.h" 13 #include "nsINSSComponent.h" 14 #include "nsServiceManagerUtils.h" 15 #include "pk11pub.h" 16 17 namespace mozilla { 18 namespace psm { 19 20 mozilla::LazyLogModule gPublicKeyPinningTelemetryLog( 21 "PublicKeyPinningTelemetryService"); 22 23 // Used in the BinarySearch method, this does a memcmp between the pointer 24 // provided to its construtor and whatever the binary search is looking for. 25 // 26 // This implementation assumes everything to be of HASH_LEN, so it should not 27 // be used generically. 28 class BinaryHashSearchArrayComparator { 29 public: 30 explicit BinaryHashSearchArrayComparator(const uint8_t* aTarget, size_t len) 31 : mTarget(aTarget) { 32 MOZ_ASSERT(len == HASH_LEN, "Hashes should be of the same length."); 33 } 34 35 int operator()(const CertAuthorityHash val) const { 36 return memcmp(mTarget, val.hash, HASH_LEN); 37 } 38 39 private: 40 const uint8_t* mTarget; 41 }; 42 43 // Perform a hash of the provided cert, then search in the RootHashes.inc data 44 // structure for a matching bin number. 45 // If no matching root is found, this may be a CA from the softoken (cert9.db), 46 // it may be a CA from an external PKCS#11 token, or it may be a CA from OS 47 // storage (Enterprise Root). 48 // See also the constants in RootCertificateTelemetryUtils.h. 49 int32_t RootCABinNumber(Span<const uint8_t> cert) { 50 nsTArray<uint8_t> digestArray; 51 52 // Compute SHA256 hash of the certificate 53 nsresult rv = Digest::DigestBuf(SEC_OID_SHA256, cert, digestArray); 54 if (NS_WARN_IF(NS_FAILED(rv))) { 55 return ROOT_CERTIFICATE_HASH_FAILURE; 56 } 57 58 // Compare against list of stored hashes 59 size_t idx; 60 61 MOZ_LOG(gPublicKeyPinningTelemetryLog, LogLevel::Debug, 62 ("pkpinTelem: First bytes %02x %02x %02x %02x\n", 63 digestArray.ElementAt(0), digestArray.ElementAt(1), 64 digestArray.ElementAt(2), digestArray.ElementAt(3))); 65 66 if (mozilla::BinarySearchIf(ROOT_TABLE, 0, std::size(ROOT_TABLE), 67 BinaryHashSearchArrayComparator( 68 digestArray.Elements(), digestArray.Length()), 69 &idx)) { 70 MOZ_LOG(gPublicKeyPinningTelemetryLog, LogLevel::Debug, 71 ("pkpinTelem: Telemetry index was %zu, bin is %d\n", idx, 72 ROOT_TABLE[idx].binNumber)); 73 return (int32_t)ROOT_TABLE[idx].binNumber; 74 } 75 76 // Didn't find this certificate in the built-in list. It may be an enterprise 77 // root (gathered from the OS) or it may be from the softoken or an external 78 // PKCS#11 token. 79 nsCOMPtr<nsINSSComponent> component(do_GetService(PSM_COMPONENT_CONTRACTID)); 80 if (!component) { 81 return ROOT_CERTIFICATE_UNKNOWN; 82 } 83 nsTArray<nsTArray<uint8_t>> enterpriseRoots; 84 rv = component->GetEnterpriseRoots(enterpriseRoots); 85 if (NS_FAILED(rv)) { 86 return ROOT_CERTIFICATE_UNKNOWN; 87 } 88 for (const auto& enterpriseRoot : enterpriseRoots) { 89 if (enterpriseRoot.Length() == cert.size() && 90 memcmp(enterpriseRoot.Elements(), cert.data(), 91 enterpriseRoot.Length()) == 0) { 92 return ROOT_CERTIFICATE_ENTERPRISE_ROOT; 93 } 94 } 95 96 SECItem certItem = {siBuffer, const_cast<uint8_t*>(cert.data()), 97 static_cast<unsigned int>(cert.size())}; 98 UniquePK11SlotInfo softokenSlot(PK11_GetInternalKeySlot()); 99 if (!softokenSlot) { 100 return ROOT_CERTIFICATE_UNKNOWN; 101 } 102 CK_OBJECT_HANDLE softokenCertHandle = 103 PK11_FindEncodedCertInSlot(softokenSlot.get(), &certItem, nullptr); 104 if (softokenCertHandle != CK_INVALID_HANDLE) { 105 return ROOT_CERTIFICATE_SOFTOKEN; 106 } 107 // In theory this should never find the certificate in the root module, 108 // because then it should have already matched our built-in list. This is 109 // here as a backstop to catch situations where a built-in root was added but 110 // the built-in telemetry information was not updated. 111 UniqueSECMODModule rootsModule(SECMOD_FindModule(kRootModuleName.get())); 112 AutoSECMODListReadLock secmodLock; 113 if (!rootsModule || rootsModule->slotCount != 1) { 114 return ROOT_CERTIFICATE_UNKNOWN; 115 } 116 CK_OBJECT_HANDLE builtinCertHandle = 117 PK11_FindEncodedCertInSlot(rootsModule->slots[0], &certItem, nullptr); 118 if (builtinCertHandle == CK_INVALID_HANDLE) { 119 return ROOT_CERTIFICATE_EXTERNAL_TOKEN; 120 } 121 122 // We have no idea what this is. 123 return ROOT_CERTIFICATE_UNKNOWN; 124 } 125 126 } // namespace psm 127 } // namespace mozilla