2014-03-13 00:08:48 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-05-14 17:37:25 +04:00
|
|
|
/* This code is made available to you under your choice of the following sets
|
|
|
|
* of licensing terms:
|
|
|
|
*/
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
/* Copyright 2013 Mozilla Contributors
|
2014-03-13 00:08:48 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "OCSPCache.h"
|
|
|
|
|
2014-05-15 04:46:32 +04:00
|
|
|
#include <limits>
|
|
|
|
|
2014-03-13 00:08:48 +04:00
|
|
|
#include "NSSCertDBTrustDomain.h"
|
|
|
|
#include "pk11pub.h"
|
2014-07-18 22:48:49 +04:00
|
|
|
#include "pkix/pkixnss.h"
|
2014-07-07 02:55:38 +04:00
|
|
|
#include "ScopedNSSTypes.h"
|
2014-03-13 00:08:48 +04:00
|
|
|
#include "secerr.h"
|
|
|
|
|
|
|
|
extern PRLogModuleInfo* gCertVerifierLog;
|
|
|
|
|
2014-06-20 21:10:51 +04:00
|
|
|
using namespace mozilla::pkix;
|
|
|
|
|
2014-03-13 00:08:48 +04:00
|
|
|
namespace mozilla { namespace psm {
|
|
|
|
|
|
|
|
// Let derIssuer be the DER encoding of the issuer of aCert.
|
|
|
|
// Let derPublicKey be the DER encoding of the public key of aIssuerCert.
|
|
|
|
// Let serialNumber be the bytes of the serial number of aCert.
|
|
|
|
// The value calculated is SHA384(derIssuer || derPublicKey || serialNumber).
|
|
|
|
// Because the DER encodings include the length of the data encoded,
|
|
|
|
// there do not exist A(derIssuerA, derPublicKeyA, serialNumberA) and
|
|
|
|
// B(derIssuerB, derPublicKeyB, serialNumberB) such that the concatenation of
|
|
|
|
// each triplet results in the same string of bytes but where each part in A is
|
|
|
|
// not equal to its counterpart in B. This is important because as a result it
|
|
|
|
// is computationally infeasible to find collisions that would subvert this
|
|
|
|
// cache (given that SHA384 is a cryptographically-secure hash function).
|
|
|
|
static SECStatus
|
2014-06-20 21:10:51 +04:00
|
|
|
CertIDHash(SHA384Buffer& buf, const CertID& certID)
|
2014-03-13 00:08:48 +04:00
|
|
|
{
|
|
|
|
ScopedPK11Context context(PK11_CreateDigestContext(SEC_OID_SHA384));
|
|
|
|
if (!context) {
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
SECStatus rv = PK11_DigestBegin(context.get());
|
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-07-31 23:17:31 +04:00
|
|
|
SECItem certIDIssuer = UnsafeMapInputToSECItem(certID.issuer);
|
2014-07-19 09:30:51 +04:00
|
|
|
rv = PK11_DigestOp(context.get(), certIDIssuer.data, certIDIssuer.len);
|
2014-03-13 00:08:48 +04:00
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-07-19 09:30:51 +04:00
|
|
|
SECItem certIDIssuerSubjectPublicKeyInfo =
|
2014-07-31 23:17:31 +04:00
|
|
|
UnsafeMapInputToSECItem(certID.issuerSubjectPublicKeyInfo);
|
2014-07-19 09:30:51 +04:00
|
|
|
rv = PK11_DigestOp(context.get(), certIDIssuerSubjectPublicKeyInfo.data,
|
|
|
|
certIDIssuerSubjectPublicKeyInfo.len);
|
2014-03-13 00:08:48 +04:00
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-07-19 09:30:51 +04:00
|
|
|
SECItem certIDSerialNumber =
|
2014-07-31 23:17:31 +04:00
|
|
|
UnsafeMapInputToSECItem(certID.serialNumber);
|
2014-07-19 09:30:51 +04:00
|
|
|
rv = PK11_DigestOp(context.get(), certIDSerialNumber.data,
|
|
|
|
certIDSerialNumber.len);
|
2014-03-13 00:08:48 +04:00
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
uint32_t outLen = 0;
|
|
|
|
rv = PK11_DigestFinal(context.get(), buf, &outLen, SHA384_LENGTH);
|
|
|
|
if (outLen != SHA384_LENGTH) {
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-07-18 22:48:49 +04:00
|
|
|
Result
|
2014-08-02 19:49:12 +04:00
|
|
|
OCSPCache::Entry::Init(const CertID& aCertID)
|
2014-03-13 00:08:48 +04:00
|
|
|
{
|
2014-07-18 22:48:49 +04:00
|
|
|
SECStatus srv = CertIDHash(mIDHash, aCertID);
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapPRErrorCodeToResult(PR_GetError());
|
|
|
|
}
|
|
|
|
return Success;
|
2014-03-13 00:08:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
OCSPCache::OCSPCache()
|
|
|
|
: mMutex("OCSPCache-mutex")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
OCSPCache::~OCSPCache()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
2014-05-15 04:46:32 +04:00
|
|
|
// Returns false with index in an undefined state if no matching entry was
|
|
|
|
// found.
|
|
|
|
bool
|
2014-06-20 21:10:51 +04:00
|
|
|
OCSPCache::FindInternal(const CertID& aCertID, /*out*/ size_t& index,
|
2014-03-13 00:08:48 +04:00
|
|
|
const MutexAutoLock& /* aProofOfLock */)
|
|
|
|
{
|
|
|
|
if (mEntries.length() == 0) {
|
2014-05-15 04:46:32 +04:00
|
|
|
return false;
|
2014-03-13 00:08:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SHA384Buffer idHash;
|
2014-06-20 21:10:51 +04:00
|
|
|
SECStatus rv = CertIDHash(idHash, aCertID);
|
2014-03-13 00:08:48 +04:00
|
|
|
if (rv != SECSuccess) {
|
2014-05-15 04:46:32 +04:00
|
|
|
return false;
|
2014-03-13 00:08:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// mEntries is sorted with the most-recently-used entry at the end.
|
|
|
|
// Thus, searching from the end will often be fastest.
|
2014-05-15 04:46:32 +04:00
|
|
|
index = mEntries.length();
|
|
|
|
while (index > 0) {
|
|
|
|
--index;
|
|
|
|
if (memcmp(mEntries[index]->mIDHash, idHash, SHA384_LENGTH) == 0) {
|
|
|
|
return true;
|
2014-03-13 00:08:48 +04:00
|
|
|
}
|
|
|
|
}
|
2014-05-15 04:46:32 +04:00
|
|
|
return false;
|
2014-03-13 00:08:48 +04:00
|
|
|
}
|
|
|
|
|
2014-06-20 21:10:51 +04:00
|
|
|
static inline void
|
|
|
|
LogWithCertID(const char* aMessage, const CertID& aCertID)
|
2014-03-13 00:08:48 +04:00
|
|
|
{
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gCertVerifierLog, LogLevel::Debug, (aMessage, &aCertID));
|
2014-03-13 00:08:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OCSPCache::MakeMostRecentlyUsed(size_t aIndex,
|
|
|
|
const MutexAutoLock& /* aProofOfLock */)
|
|
|
|
{
|
|
|
|
Entry* entry = mEntries[aIndex];
|
|
|
|
// Since mEntries is sorted with the most-recently-used entry at the end,
|
|
|
|
// aIndex is likely to be near the end, so this is likely to be fast.
|
|
|
|
mEntries.erase(mEntries.begin() + aIndex);
|
|
|
|
mEntries.append(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-08-02 19:49:12 +04:00
|
|
|
OCSPCache::Get(const CertID& aCertID, Result& aResult, Time& aValidThrough)
|
2014-03-13 00:08:48 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2014-05-15 04:46:32 +04:00
|
|
|
size_t index;
|
2014-06-20 21:10:51 +04:00
|
|
|
if (!FindInternal(aCertID, index, lock)) {
|
|
|
|
LogWithCertID("OCSPCache::Get(%p) not in cache", aCertID);
|
2014-03-13 00:08:48 +04:00
|
|
|
return false;
|
|
|
|
}
|
2014-06-20 21:10:51 +04:00
|
|
|
LogWithCertID("OCSPCache::Get(%p) in cache", aCertID);
|
2014-07-18 22:48:49 +04:00
|
|
|
aResult = mEntries[index]->mResult;
|
2014-03-13 00:08:48 +04:00
|
|
|
aValidThrough = mEntries[index]->mValidThrough;
|
|
|
|
MakeMostRecentlyUsed(index, lock);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-18 22:48:49 +04:00
|
|
|
Result
|
|
|
|
OCSPCache::Put(const CertID& aCertID, Result aResult,
|
2014-08-02 19:49:12 +04:00
|
|
|
Time aThisUpdate, Time aValidThrough)
|
2014-03-13 00:08:48 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2014-05-15 04:46:32 +04:00
|
|
|
size_t index;
|
2014-06-20 21:10:51 +04:00
|
|
|
if (FindInternal(aCertID, index, lock)) {
|
2014-03-13 00:08:48 +04:00
|
|
|
// Never replace an entry indicating a revoked certificate.
|
2014-07-18 22:48:49 +04:00
|
|
|
if (mEntries[index]->mResult == Result::ERROR_REVOKED_CERTIFICATE) {
|
2014-06-20 21:10:51 +04:00
|
|
|
LogWithCertID("OCSPCache::Put(%p) already in cache as revoked - "
|
|
|
|
"not replacing", aCertID);
|
2014-03-13 00:08:48 +04:00
|
|
|
MakeMostRecentlyUsed(index, lock);
|
2014-07-18 22:48:49 +04:00
|
|
|
return Success;
|
2014-03-13 00:08:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Never replace a newer entry with an older one unless the older entry
|
|
|
|
// indicates a revoked certificate, which we want to remember.
|
|
|
|
if (mEntries[index]->mThisUpdate > aThisUpdate &&
|
2014-07-18 22:48:49 +04:00
|
|
|
aResult != Result::ERROR_REVOKED_CERTIFICATE) {
|
2014-06-20 21:10:51 +04:00
|
|
|
LogWithCertID("OCSPCache::Put(%p) already in cache with more recent "
|
|
|
|
"validity - not replacing", aCertID);
|
2014-03-13 00:08:48 +04:00
|
|
|
MakeMostRecentlyUsed(index, lock);
|
2014-07-18 22:48:49 +04:00
|
|
|
return Success;
|
2014-03-13 00:08:48 +04:00
|
|
|
}
|
|
|
|
|
2014-04-29 03:38:15 +04:00
|
|
|
// Only known good responses or responses indicating an unknown
|
|
|
|
// or revoked certificate should replace previously known responses.
|
2014-07-18 22:48:49 +04:00
|
|
|
if (aResult != Success &&
|
|
|
|
aResult != Result::ERROR_OCSP_UNKNOWN_CERT &&
|
|
|
|
aResult != Result::ERROR_REVOKED_CERTIFICATE) {
|
2014-06-20 21:10:51 +04:00
|
|
|
LogWithCertID("OCSPCache::Put(%p) already in cache - not replacing "
|
|
|
|
"with less important status", aCertID);
|
2014-04-29 03:38:15 +04:00
|
|
|
MakeMostRecentlyUsed(index, lock);
|
2014-07-18 22:48:49 +04:00
|
|
|
return Success;
|
2014-04-29 03:38:15 +04:00
|
|
|
}
|
|
|
|
|
2014-06-20 21:10:51 +04:00
|
|
|
LogWithCertID("OCSPCache::Put(%p) already in cache - replacing", aCertID);
|
2014-07-18 22:48:49 +04:00
|
|
|
mEntries[index]->mResult = aResult;
|
2014-03-13 00:08:48 +04:00
|
|
|
mEntries[index]->mThisUpdate = aThisUpdate;
|
|
|
|
mEntries[index]->mValidThrough = aValidThrough;
|
|
|
|
MakeMostRecentlyUsed(index, lock);
|
2014-07-18 22:48:49 +04:00
|
|
|
return Success;
|
2014-03-13 00:08:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mEntries.length() == MaxEntries) {
|
2014-06-20 21:10:51 +04:00
|
|
|
LogWithCertID("OCSPCache::Put(%p) too full - evicting an entry", aCertID);
|
2014-03-13 00:08:48 +04:00
|
|
|
for (Entry** toEvict = mEntries.begin(); toEvict != mEntries.end();
|
|
|
|
toEvict++) {
|
|
|
|
// Never evict an entry that indicates a revoked or unknokwn certificate,
|
|
|
|
// because revoked responses are more security-critical to remember.
|
2014-07-18 22:48:49 +04:00
|
|
|
if ((*toEvict)->mResult != Result::ERROR_REVOKED_CERTIFICATE &&
|
|
|
|
(*toEvict)->mResult != Result::ERROR_OCSP_UNKNOWN_CERT) {
|
2014-03-13 00:08:48 +04:00
|
|
|
delete *toEvict;
|
|
|
|
mEntries.erase(toEvict);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Well, we tried, but apparently everything is revoked or unknown.
|
|
|
|
// We don't want to remove a cached revoked or unknown response. If we're
|
|
|
|
// trying to insert a good response, we can just return "successfully"
|
|
|
|
// without doing so. This means we'll lose some speed, but it's not a
|
|
|
|
// security issue. If we're trying to insert a revoked or unknown response,
|
|
|
|
// we can't. We should return with an error that causes the current
|
|
|
|
// verification to fail.
|
|
|
|
if (mEntries.length() == MaxEntries) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return aResult;
|
2014-03-13 00:08:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-02 19:49:12 +04:00
|
|
|
Entry* newEntry = new (std::nothrow) Entry(aResult, aThisUpdate,
|
|
|
|
aValidThrough);
|
2014-03-13 00:08:48 +04:00
|
|
|
// Normally we don't have to do this in Gecko, because OOM is fatal.
|
|
|
|
// However, if we want to embed this in another project, OOM might not
|
|
|
|
// be fatal, so handle this case.
|
|
|
|
if (!newEntry) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::FATAL_ERROR_NO_MEMORY;
|
2014-03-13 00:08:48 +04:00
|
|
|
}
|
2014-08-02 19:49:12 +04:00
|
|
|
Result rv = newEntry->Init(aCertID);
|
2014-07-18 22:48:49 +04:00
|
|
|
if (rv != Success) {
|
2014-06-17 20:14:00 +04:00
|
|
|
delete newEntry;
|
2014-03-13 00:08:48 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mEntries.append(newEntry);
|
2014-06-20 21:10:51 +04:00
|
|
|
LogWithCertID("OCSPCache::Put(%p) added to cache", aCertID);
|
2014-07-18 22:48:49 +04:00
|
|
|
return Success;
|
2014-03-13 00:08:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OCSPCache::Clear()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gCertVerifierLog, LogLevel::Debug, ("OCSPCache::Clear: clearing cache"));
|
2014-03-13 00:08:48 +04:00
|
|
|
// First go through and delete the memory being pointed to by the pointers
|
|
|
|
// in the vector.
|
|
|
|
for (Entry** entry = mEntries.begin(); entry < mEntries.end();
|
|
|
|
entry++) {
|
|
|
|
delete *entry;
|
|
|
|
}
|
|
|
|
// Then remove the pointers themselves.
|
|
|
|
mEntries.clearAndFree();
|
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace mozilla::psm
|