Blame security/manager/ssl/RootCertificateTelemetryUtils.cpp

Packit f0b94e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit f0b94e
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
#include "RootCertificateTelemetryUtils.h"
Packit f0b94e
Packit f0b94e
#include "mozilla/Logging.h"
Packit f0b94e
#include "RootHashes.inc"  // Note: Generated by genRootCAHashes.js
Packit f0b94e
#include "ScopedNSSTypes.h"
Packit f0b94e
#include "mozilla/ArrayUtils.h"
Packit f0b94e
Packit f0b94e
namespace mozilla {
Packit f0b94e
namespace psm {
Packit f0b94e
Packit f0b94e
mozilla::LazyLogModule gPublicKeyPinningTelemetryLog(
Packit f0b94e
    "PublicKeyPinningTelemetryService");
Packit f0b94e
Packit f0b94e
// Used in the BinarySearch method, this does a memcmp between the pointer
Packit f0b94e
// provided to its construtor and whatever the binary search is looking for.
Packit f0b94e
//
Packit f0b94e
// This implementation assumes everything to be of HASH_LEN, so it should not
Packit f0b94e
// be used generically.
Packit f0b94e
class BinaryHashSearchArrayComparator {
Packit f0b94e
 public:
Packit f0b94e
  explicit BinaryHashSearchArrayComparator(const uint8_t* aTarget, size_t len)
Packit f0b94e
      : mTarget(aTarget) {
Packit f0b94e
    MOZ_ASSERT(len == HASH_LEN, "Hashes should be of the same length.");
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  int operator()(const CertAuthorityHash val) const {
Packit f0b94e
    return memcmp(mTarget, val.hash, HASH_LEN);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
 private:
Packit f0b94e
  const uint8_t* mTarget;
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
// Perform a hash of the provided cert, then search in the RootHashes.inc data
Packit f0b94e
// structure for a matching bin number.
Packit f0b94e
int32_t RootCABinNumber(const SECItem* cert) {
Packit f0b94e
  Digest digest;
Packit f0b94e
Packit f0b94e
  // Compute SHA256 hash of the certificate
Packit f0b94e
  nsresult rv = digest.DigestBuf(SEC_OID_SHA256, cert->data, cert->len);
Packit f0b94e
  if (NS_WARN_IF(NS_FAILED(rv))) {
Packit f0b94e
    return ROOT_CERTIFICATE_HASH_FAILURE;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // Compare against list of stored hashes
Packit f0b94e
  size_t idx;
Packit f0b94e
Packit f0b94e
  MOZ_LOG(
Packit f0b94e
      gPublicKeyPinningTelemetryLog, LogLevel::Debug,
Packit f0b94e
      ("pkpinTelem: First bytes %02x %02x %02x %02x\n", digest.get().data[0],
Packit f0b94e
       digest.get().data[1], digest.get().data[2], digest.get().data[3]));
Packit f0b94e
Packit f0b94e
  if (mozilla::BinarySearchIf(
Packit f0b94e
          ROOT_TABLE, 0, ArrayLength(ROOT_TABLE),
Packit f0b94e
          BinaryHashSearchArrayComparator(
Packit f0b94e
              static_cast<uint8_t*>(digest.get().data), digest.get().len),
Packit f0b94e
          &idx)) {
Packit f0b94e
    MOZ_LOG(gPublicKeyPinningTelemetryLog, LogLevel::Debug,
Packit f0b94e
            ("pkpinTelem: Telemetry index was %zu, bin is %d\n", idx,
Packit f0b94e
             ROOT_TABLE[idx].binNumber));
Packit f0b94e
    return (int32_t)ROOT_TABLE[idx].binNumber;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // Didn't match.
Packit f0b94e
  return ROOT_CERTIFICATE_UNKNOWN;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
// Attempt to increment the appropriate bin in the provided Telemetry probe ID.
Packit f0b94e
// If there was a hash failure, we do nothing.
Packit f0b94e
void AccumulateTelemetryForRootCA(mozilla::Telemetry::HistogramID probe,
Packit f0b94e
                                  const CERTCertificate* cert) {
Packit f0b94e
  int32_t binId = RootCABinNumber(&cert->derCert);
Packit f0b94e
Packit f0b94e
  if (binId != ROOT_CERTIFICATE_HASH_FAILURE) {
Packit f0b94e
    Accumulate(probe, binId);
Packit f0b94e
  }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
}  // namespace psm
Packit f0b94e
}  // namespace mozilla