2011-12-01 22:37:23 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-31 09:33:35 +00:00
|
|
|
* 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/. */
|
2011-12-01 22:37:57 +00:00
|
|
|
|
2014-01-22 01:30:44 +00:00
|
|
|
// For connections that are not processed on the socket transport thread, we do
|
|
|
|
// NOT use the async logic described below. Instead, we authenticate the
|
|
|
|
// certificate on the thread that the connection's I/O happens on,
|
|
|
|
// synchronously. This allows us to do certificate verification for blocking
|
|
|
|
// (not non-blocking) sockets and sockets that have their I/O processed on a
|
|
|
|
// thread other than the socket transport service thread. Also, we DO NOT
|
|
|
|
// support blocking sockets on the socket transport service thread at all.
|
|
|
|
//
|
|
|
|
// During certificate authentication, we call CERT_PKIXVerifyCert or
|
|
|
|
// CERT_VerifyCert. These functions may make zero or more HTTP requests
|
|
|
|
// for OCSP responses, CRLs, intermediate certificates, etc. Our fetching logic
|
|
|
|
// for these requests processes them on the socket transport service thread.
|
|
|
|
//
|
|
|
|
// If the connection for which we are verifying the certificate is happening
|
|
|
|
// on the socket transport thread (the usually case, at least for HTTP), then
|
|
|
|
// if our cert auth hook were to call the CERT_*Verify* functions directly,
|
|
|
|
// there would be a deadlock: The CERT_*Verify* function would cause an event
|
|
|
|
// to be asynchronously posted to the socket transport thread, and then it
|
|
|
|
// would block the socket transport thread waiting to be notified of the HTTP
|
|
|
|
// response. However, the HTTP request would never actually be processed
|
|
|
|
// because the socket transport thread would be blocked and so it wouldn't be
|
|
|
|
// able process HTTP requests. (i.e. Deadlock.)
|
|
|
|
//
|
|
|
|
// Consequently, when we are asked to verify a certificate on the socket
|
|
|
|
// transport service thread, we must always call the CERT_*Verify* cert
|
|
|
|
// functions on another thread. To accomplish this, our auth cert hook
|
|
|
|
// dispatches a SSLServerCertVerificationJob to a pool of background threads,
|
|
|
|
// and then immediatley return SECWouldBlock to libssl. These jobs are where
|
|
|
|
// the CERT_*Verify* functions are actually called.
|
|
|
|
//
|
|
|
|
// When our auth cert hook returns SECWouldBlock, libssl will carry on the
|
|
|
|
// handshake while we validate the certificate. This will free up the socket
|
|
|
|
// transport thread so that HTTP requests--in particular, the OCSP/CRL/cert
|
|
|
|
// requests needed for cert verification as mentioned above--can be processed.
|
|
|
|
//
|
|
|
|
// Once the CERT_*Verify* function returns, the cert verification job
|
|
|
|
// dispatches a SSLServerCertVerificationResult to the socket transport thread;
|
|
|
|
// the SSLServerCertVerificationResult will notify libssl that the certificate
|
|
|
|
// authentication is complete. Once libssl is notified that the authentication
|
|
|
|
// is complete, it will continue the SSL handshake (if it hasn't already
|
|
|
|
// finished) and it will begin allowing us to send/receive data on the
|
|
|
|
// connection.
|
|
|
|
//
|
|
|
|
// Timeline of events (for connections managed by the socket transport service):
|
|
|
|
//
|
|
|
|
// * libssl calls SSLServerCertVerificationJob::Dispatch on the socket
|
|
|
|
// transport thread.
|
|
|
|
// * SSLServerCertVerificationJob::Dispatch queues a job
|
|
|
|
// (instance of SSLServerCertVerificationJob) to its background thread
|
|
|
|
// pool and returns.
|
|
|
|
// * One of the background threads calls CERT_*Verify*, which may enqueue
|
|
|
|
// some HTTP request(s) onto the socket transport thread, and then
|
|
|
|
// blocks that background thread waiting for the responses and/or timeouts
|
|
|
|
// or errors for those requests.
|
|
|
|
// * Once those HTTP responses have all come back or failed, the
|
|
|
|
// CERT_*Verify* function returns a result indicating that the validation
|
|
|
|
// succeeded or failed.
|
|
|
|
// * If the validation succeeded, then a SSLServerCertVerificationResult
|
|
|
|
// event is posted to the socket transport thread, and the cert
|
|
|
|
// verification thread becomes free to verify other certificates.
|
|
|
|
// * Otherwise, a CertErrorRunnable is posted to the socket transport thread
|
|
|
|
// and then to the main thread (blocking both, see CertErrorRunnable) to
|
|
|
|
// do cert override processing and bad cert listener notification. Then
|
|
|
|
// the cert verification thread becomes free to verify other certificates.
|
|
|
|
// * After processing cert overrides, the CertErrorRunnable will dispatch a
|
|
|
|
// SSLServerCertVerificationResult event to the socket transport thread to
|
|
|
|
// notify it of the result of the override processing; then it returns,
|
|
|
|
// freeing up the main thread.
|
|
|
|
// * The SSLServerCertVerificationResult event will either wake up the
|
|
|
|
// socket (using SSL_RestartHandshakeAfterServerCert) if validation
|
|
|
|
// succeeded or there was an error override, or it will set an error flag
|
|
|
|
// so that the next I/O operation on the socket will fail, causing the
|
|
|
|
// socket transport thread to close the connection.
|
|
|
|
//
|
|
|
|
// Cert override processing must happen on the main thread because it accesses
|
|
|
|
// the nsICertOverrideService, and that service must be accessed on the main
|
|
|
|
// thread because some extensions (Selenium, in particular) replace it with a
|
|
|
|
// Javascript implementation, and chrome JS must always be run on the main
|
|
|
|
// thread.
|
|
|
|
//
|
|
|
|
// SSLServerCertVerificationResult must be dispatched to the socket transport
|
|
|
|
// thread because we must only call SSL_* functions on the socket transport
|
|
|
|
// thread since they may do I/O, because many parts of nsNSSSocketInfo (the
|
|
|
|
// subclass of TransportSecurityInfo used when validating certificates during
|
|
|
|
// an SSL handshake) and the PSM NSS I/O layer are not thread-safe, and because
|
|
|
|
// we need the event to interrupt the PR_Poll that may waiting for I/O on the
|
|
|
|
// socket for which we are validating the cert.
|
2011-12-01 22:37:57 +00:00
|
|
|
|
|
|
|
#include "SSLServerCertVerification.h"
|
2014-01-13 03:31:40 +00:00
|
|
|
|
2013-07-08 23:30:59 +00:00
|
|
|
#include <cstring>
|
2014-01-13 03:31:40 +00:00
|
|
|
|
2014-03-20 21:29:21 +00:00
|
|
|
#include "pkix/pkixtypes.h"
|
2014-08-22 19:07:08 +00:00
|
|
|
#include "pkix/pkixnss.h"
|
2012-10-27 07:11:35 +00:00
|
|
|
#include "CertVerifier.h"
|
2014-01-27 03:36:28 +00:00
|
|
|
#include "CryptoTask.h"
|
|
|
|
#include "ExtendedValidation.h"
|
2013-07-08 23:30:59 +00:00
|
|
|
#include "NSSCertDBTrustDomain.h"
|
2012-01-31 16:04:57 +00:00
|
|
|
#include "nsIBadCertListener2.h"
|
|
|
|
#include "nsICertOverrideService.h"
|
2013-08-02 22:48:37 +00:00
|
|
|
#include "nsISiteSecurityService.h"
|
2011-12-01 22:37:23 +00:00
|
|
|
#include "nsNSSComponent.h"
|
|
|
|
#include "nsNSSIOLayer.h"
|
2012-11-22 20:57:59 +00:00
|
|
|
#include "nsNSSShutDown.h"
|
2011-12-01 22:37:23 +00:00
|
|
|
|
2012-01-31 16:05:06 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
2013-01-02 16:14:07 +00:00
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
2014-02-05 22:14:54 +00:00
|
|
|
#include "mozilla/unused.h"
|
2011-12-01 22:37:57 +00:00
|
|
|
#include "nsIThreadPool.h"
|
2013-06-22 22:57:15 +00:00
|
|
|
#include "nsNetUtil.h"
|
2011-12-01 22:37:57 +00:00
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2012-01-31 16:04:57 +00:00
|
|
|
#include "PSMRunnable.h"
|
2012-12-07 03:05:27 +00:00
|
|
|
#include "SharedSSLState.h"
|
2013-09-26 18:07:29 +00:00
|
|
|
#include "nsContentUtils.h"
|
2011-12-01 22:37:57 +00:00
|
|
|
|
2011-12-01 22:37:23 +00:00
|
|
|
#include "ssl.h"
|
|
|
|
#include "secerr.h"
|
2012-01-31 16:04:57 +00:00
|
|
|
#include "secport.h"
|
2011-12-01 22:37:23 +00:00
|
|
|
#include "sslerr.h"
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
extern PRLogModuleInfo* gPIPNSSLog;
|
|
|
|
#endif
|
|
|
|
|
2014-08-02 15:49:12 +00:00
|
|
|
using namespace mozilla::pkix;
|
|
|
|
|
2011-12-01 22:37:57 +00:00
|
|
|
namespace mozilla { namespace psm {
|
|
|
|
|
|
|
|
namespace {
|
2012-01-31 16:04:57 +00:00
|
|
|
|
2011-12-01 22:37:57 +00:00
|
|
|
// do not use a nsCOMPtr to avoid static initializer/destructor
|
2014-01-22 01:30:44 +00:00
|
|
|
nsIThreadPool* gCertVerificationThreadPool = nullptr;
|
2013-01-02 16:14:07 +00:00
|
|
|
|
|
|
|
// We avoid using a mutex for the success case to avoid lock-related
|
|
|
|
// performance issues. However, we do use a lock in the error case to simplify
|
|
|
|
// the code, since performance in the error case is not important.
|
2014-01-22 01:30:44 +00:00
|
|
|
Mutex* gSSLVerificationTelemetryMutex = nullptr;
|
2013-01-02 16:14:07 +00:00
|
|
|
|
2013-12-09 17:12:47 +00:00
|
|
|
// We add a mutex to serialize PKCS11 database operations
|
2014-01-22 01:30:44 +00:00
|
|
|
Mutex* gSSLVerificationPK11Mutex = nullptr;
|
2013-12-09 17:12:47 +00:00
|
|
|
|
2011-12-01 22:37:57 +00:00
|
|
|
} // unnamed namespace
|
|
|
|
|
|
|
|
// Called when the socket transport thread starts, to initialize the SSL cert
|
|
|
|
// verification thread pool. By tying the thread pool startup/shutdown directly
|
|
|
|
// to the STS thread's lifetime, we ensure that they are *always* available for
|
|
|
|
// SSL connections and that there are no races during startup and especially
|
|
|
|
// shutdown. (Previously, we have had multiple problems with races in PSM
|
|
|
|
// background threads, and the race-prevention/shutdown logic used there is
|
|
|
|
// brittle. Since this service is critical to things like downloading updates,
|
|
|
|
// we take no chances.) Also, by doing things this way, we avoid the need for
|
|
|
|
// locks, since gCertVerificationThreadPool is only ever accessed on the socket
|
|
|
|
// transport thread.
|
|
|
|
void
|
|
|
|
InitializeSSLServerCertVerificationThreads()
|
|
|
|
{
|
2013-01-02 16:14:07 +00:00
|
|
|
gSSLVerificationTelemetryMutex = new Mutex("SSLVerificationTelemetryMutex");
|
2013-12-09 17:12:47 +00:00
|
|
|
gSSLVerificationPK11Mutex = new Mutex("SSLVerificationPK11Mutex");
|
2011-12-01 22:37:57 +00:00
|
|
|
// TODO: tuning, make parameters preferences
|
|
|
|
// XXX: instantiate nsThreadPool directly, to make this more bulletproof.
|
|
|
|
// Currently, the nsThreadPool.h header isn't exported for us to do so.
|
|
|
|
nsresult rv = CallCreateInstance(NS_THREADPOOL_CONTRACTID,
|
|
|
|
&gCertVerificationThreadPool);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to create SSL cert verification threads.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) gCertVerificationThreadPool->SetIdleThreadLimit(5);
|
|
|
|
(void) gCertVerificationThreadPool->SetIdleThreadTimeout(30 * 1000);
|
|
|
|
(void) gCertVerificationThreadPool->SetThreadLimit(5);
|
2012-06-12 17:06:20 +00:00
|
|
|
(void) gCertVerificationThreadPool->SetName(NS_LITERAL_CSTRING("SSL Cert"));
|
2011-12-01 22:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the socket transport thread finishes, to destroy the thread
|
|
|
|
// pool. Since the socket transport service has stopped processing events, it
|
|
|
|
// will not attempt any more SSL I/O operations, so it is clearly safe to shut
|
|
|
|
// down the SSL cert verification infrastructure. Also, the STS will not
|
|
|
|
// dispatch many SSL verification result events at this point, so any pending
|
|
|
|
// cert verifications will (correctly) fail at the point they are dispatched.
|
|
|
|
//
|
|
|
|
// The other shutdown race condition that is possible is a race condition with
|
|
|
|
// shutdown of the nsNSSComponent service. We use the
|
|
|
|
// nsNSSShutdownPreventionLock where needed (not here) to prevent that.
|
|
|
|
void StopSSLServerCertVerificationThreads()
|
|
|
|
{
|
|
|
|
if (gCertVerificationThreadPool) {
|
|
|
|
gCertVerificationThreadPool->Shutdown();
|
|
|
|
NS_RELEASE(gCertVerificationThreadPool);
|
|
|
|
}
|
2013-01-02 16:14:07 +00:00
|
|
|
if (gSSLVerificationTelemetryMutex) {
|
|
|
|
delete gSSLVerificationTelemetryMutex;
|
|
|
|
gSSLVerificationTelemetryMutex = nullptr;
|
|
|
|
}
|
2013-12-09 17:12:47 +00:00
|
|
|
if (gSSLVerificationPK11Mutex) {
|
|
|
|
delete gSSLVerificationPK11Mutex;
|
|
|
|
gSSLVerificationPK11Mutex = nullptr;
|
|
|
|
}
|
2011-12-01 22:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-08-28 13:32:34 +00:00
|
|
|
void
|
2014-01-22 01:30:44 +00:00
|
|
|
LogInvalidCertError(TransportSecurityInfo* socketInfo,
|
2012-08-28 13:32:34 +00:00
|
|
|
PRErrorCode errorCode,
|
2014-01-17 19:57:57 +00:00
|
|
|
::mozilla::psm::SSLErrorMessageType errorMessageType)
|
2012-08-28 13:32:34 +00:00
|
|
|
{
|
|
|
|
nsString message;
|
|
|
|
socketInfo->GetErrorLogMessage(errorCode, errorMessageType, message);
|
|
|
|
if (!message.IsEmpty()) {
|
2013-09-26 18:07:29 +00:00
|
|
|
nsContentUtils::LogSimpleConsoleError(message, "SSL");
|
2012-08-28 13:32:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 04:00:29 +00:00
|
|
|
// Dispatched to the STS thread to notify the infoObject of the verification
|
2012-01-31 16:04:57 +00:00
|
|
|
// result.
|
|
|
|
//
|
|
|
|
// This will cause the PR_Poll in the STS thread to return, so things work
|
|
|
|
// correctly even if the STS thread is blocked polling (only) on the file
|
|
|
|
// descriptor that is waiting for this result.
|
|
|
|
class SSLServerCertVerificationResult : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2014-01-22 01:30:44 +00:00
|
|
|
SSLServerCertVerificationResult(TransportSecurityInfo* infoObject,
|
2012-01-31 16:04:57 +00:00
|
|
|
PRErrorCode errorCode,
|
2013-01-02 16:14:07 +00:00
|
|
|
Telemetry::ID telemetryID = Telemetry::HistogramCount,
|
|
|
|
uint32_t telemetryValue = -1,
|
|
|
|
SSLErrorMessageType errorMessageType =
|
2012-01-31 16:04:57 +00:00
|
|
|
PlainErrorMessage);
|
|
|
|
|
|
|
|
void Dispatch();
|
|
|
|
private:
|
2012-10-17 20:48:36 +00:00
|
|
|
const RefPtr<TransportSecurityInfo> mInfoObject;
|
2012-01-31 16:05:11 +00:00
|
|
|
public:
|
2012-01-31 16:04:57 +00:00
|
|
|
const PRErrorCode mErrorCode;
|
|
|
|
const SSLErrorMessageType mErrorMessageType;
|
2013-01-02 16:14:07 +00:00
|
|
|
const Telemetry::ID mTelemetryID;
|
|
|
|
const uint32_t mTelemetryValue;
|
2012-01-31 16:04:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CertErrorRunnable : public SyncRunnableBase
|
|
|
|
{
|
|
|
|
public:
|
2014-01-22 01:30:44 +00:00
|
|
|
CertErrorRunnable(const void* fdForLogging,
|
|
|
|
nsIX509Cert* cert,
|
|
|
|
TransportSecurityInfo* infoObject,
|
2012-01-31 16:04:57 +00:00
|
|
|
PRErrorCode defaultErrorCodeToReport,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t collectedErrors,
|
2012-01-31 16:04:57 +00:00
|
|
|
PRErrorCode errorCodeTrust,
|
|
|
|
PRErrorCode errorCodeMismatch,
|
2012-12-07 03:05:27 +00:00
|
|
|
PRErrorCode errorCodeExpired,
|
|
|
|
uint32_t providerFlags)
|
2012-01-31 16:04:57 +00:00
|
|
|
: mFdForLogging(fdForLogging), mCert(cert), mInfoObject(infoObject),
|
|
|
|
mDefaultErrorCodeToReport(defaultErrorCodeToReport),
|
|
|
|
mCollectedErrors(collectedErrors),
|
|
|
|
mErrorCodeTrust(errorCodeTrust),
|
|
|
|
mErrorCodeMismatch(errorCodeMismatch),
|
2012-12-07 03:05:27 +00:00
|
|
|
mErrorCodeExpired(errorCodeExpired),
|
|
|
|
mProviderFlags(providerFlags)
|
2012-01-31 16:04:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void RunOnTargetThread();
|
2012-10-17 20:48:36 +00:00
|
|
|
RefPtr<SSLServerCertVerificationResult> mResult; // out
|
2012-01-31 16:04:57 +00:00
|
|
|
private:
|
2014-01-22 01:30:44 +00:00
|
|
|
SSLServerCertVerificationResult* CheckCertOverrides();
|
|
|
|
|
|
|
|
const void* const mFdForLogging; // may become an invalid pointer; do not dereference
|
2012-01-31 16:04:57 +00:00
|
|
|
const nsCOMPtr<nsIX509Cert> mCert;
|
2012-10-17 20:48:36 +00:00
|
|
|
const RefPtr<TransportSecurityInfo> mInfoObject;
|
2012-01-31 16:04:57 +00:00
|
|
|
const PRErrorCode mDefaultErrorCodeToReport;
|
2012-08-22 15:56:38 +00:00
|
|
|
const uint32_t mCollectedErrors;
|
2012-01-31 16:04:57 +00:00
|
|
|
const PRErrorCode mErrorCodeTrust;
|
|
|
|
const PRErrorCode mErrorCodeMismatch;
|
|
|
|
const PRErrorCode mErrorCodeExpired;
|
2012-12-07 03:05:27 +00:00
|
|
|
const uint32_t mProviderFlags;
|
2012-01-31 16:04:57 +00:00
|
|
|
};
|
|
|
|
|
2014-02-13 22:53:29 +00:00
|
|
|
// A probe value of 1 means "no error".
|
|
|
|
uint32_t
|
|
|
|
MapCertErrorToProbeValue(PRErrorCode errorCode)
|
|
|
|
{
|
|
|
|
switch (errorCode)
|
|
|
|
{
|
|
|
|
case SEC_ERROR_UNKNOWN_ISSUER: return 2;
|
|
|
|
case SEC_ERROR_UNTRUSTED_ISSUER: return 4;
|
|
|
|
case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE: return 5;
|
|
|
|
case SEC_ERROR_UNTRUSTED_CERT: return 6;
|
2014-03-13 23:49:12 +00:00
|
|
|
case SEC_ERROR_INADEQUATE_KEY_USAGE: return 7;
|
2014-02-13 22:53:29 +00:00
|
|
|
case SEC_ERROR_CERT_SIGNATURE_ALGORITHM_DISABLED: return 8;
|
|
|
|
case SSL_ERROR_BAD_CERT_DOMAIN: return 9;
|
|
|
|
case SEC_ERROR_EXPIRED_CERTIFICATE: return 10;
|
2014-08-22 19:07:08 +00:00
|
|
|
case mozilla::pkix::MOZILLA_PKIX_ERROR_CA_CERT_USED_AS_END_ENTITY: return 11;
|
2014-02-13 22:53:29 +00:00
|
|
|
}
|
|
|
|
NS_WARNING("Unknown certificate error code. Does MapCertErrorToProbeValue "
|
2014-08-22 19:07:08 +00:00
|
|
|
"handle everything in DetermineCertOverrideErrors?");
|
2014-02-13 22:53:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-23 03:08:06 +00:00
|
|
|
SECStatus
|
2014-06-17 06:13:29 +00:00
|
|
|
DetermineCertOverrideErrors(CERTCertificate* cert, const char* hostName,
|
|
|
|
PRTime now, PRErrorCode defaultErrorCodeToReport,
|
|
|
|
/*out*/ uint32_t& collectedErrors,
|
|
|
|
/*out*/ PRErrorCode& errorCodeTrust,
|
|
|
|
/*out*/ PRErrorCode& errorCodeMismatch,
|
|
|
|
/*out*/ PRErrorCode& errorCodeExpired)
|
2014-02-23 03:08:06 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(cert);
|
|
|
|
MOZ_ASSERT(hostName);
|
|
|
|
MOZ_ASSERT(collectedErrors == 0);
|
|
|
|
MOZ_ASSERT(errorCodeTrust == 0);
|
|
|
|
MOZ_ASSERT(errorCodeMismatch == 0);
|
|
|
|
MOZ_ASSERT(errorCodeExpired == 0);
|
|
|
|
|
2014-03-20 21:29:21 +00:00
|
|
|
// Assumes the error prioritization described in mozilla::pkix's
|
2014-02-23 03:08:06 +00:00
|
|
|
// BuildForward function. Also assumes that CERT_VerifyCertName was only
|
|
|
|
// called if CertVerifier::VerifyCert succeeded.
|
|
|
|
switch (defaultErrorCodeToReport) {
|
|
|
|
case SEC_ERROR_CERT_SIGNATURE_ALGORITHM_DISABLED:
|
2014-08-02 06:16:21 +00:00
|
|
|
case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE:
|
2014-02-23 03:08:06 +00:00
|
|
|
case SEC_ERROR_UNKNOWN_ISSUER:
|
2014-08-22 19:07:08 +00:00
|
|
|
case mozilla::pkix::MOZILLA_PKIX_ERROR_CA_CERT_USED_AS_END_ENTITY:
|
2014-02-23 03:08:06 +00:00
|
|
|
{
|
|
|
|
collectedErrors = nsICertOverrideService::ERROR_UNTRUSTED;
|
|
|
|
errorCodeTrust = defaultErrorCodeToReport;
|
|
|
|
|
|
|
|
SECCertTimeValidity validity = CERT_CheckCertValidTimes(cert, now, false);
|
|
|
|
if (validity == secCertTimeUndetermined) {
|
|
|
|
PR_SetError(defaultErrorCodeToReport, 0);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
if (validity != secCertTimeValid) {
|
|
|
|
collectedErrors |= nsICertOverrideService::ERROR_TIME;
|
|
|
|
errorCodeExpired = SEC_ERROR_EXPIRED_CERTIFICATE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SEC_ERROR_EXPIRED_CERTIFICATE:
|
|
|
|
collectedErrors = nsICertOverrideService::ERROR_TIME;
|
|
|
|
errorCodeExpired = SEC_ERROR_EXPIRED_CERTIFICATE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_ERROR_BAD_CERT_DOMAIN:
|
|
|
|
collectedErrors = nsICertOverrideService::ERROR_MISMATCH;
|
|
|
|
errorCodeMismatch = SSL_ERROR_BAD_CERT_DOMAIN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
NS_ERROR("No error code set during certificate validation failure.");
|
|
|
|
PR_SetError(PR_INVALID_STATE_ERROR, 0);
|
|
|
|
return SECFailure;
|
|
|
|
|
|
|
|
default:
|
|
|
|
PR_SetError(defaultErrorCodeToReport, 0);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defaultErrorCodeToReport != SSL_ERROR_BAD_CERT_DOMAIN) {
|
|
|
|
if (CERT_VerifyCertName(cert, hostName) != SECSuccess) {
|
|
|
|
if (PR_GetError() != SSL_ERROR_BAD_CERT_DOMAIN) {
|
|
|
|
PR_SetError(defaultErrorCodeToReport, 0);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
collectedErrors |= nsICertOverrideService::ERROR_MISMATCH;
|
|
|
|
errorCodeMismatch = SSL_ERROR_BAD_CERT_DOMAIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SECSuccess;
|
|
|
|
}
|
|
|
|
|
2014-01-22 01:30:44 +00:00
|
|
|
SSLServerCertVerificationResult*
|
2012-01-31 16:04:57 +00:00
|
|
|
CertErrorRunnable::CheckCertOverrides()
|
|
|
|
{
|
2012-01-31 16:05:06 +00:00
|
|
|
PR_LOG(gPIPNSSLog, PR_LOG_DEBUG, ("[%p][%p] top of CheckCertOverrides\n",
|
2012-01-31 16:04:57 +00:00
|
|
|
mFdForLogging, this));
|
2014-02-05 21:39:27 +00:00
|
|
|
// "Use" mFdForLogging in non-PR_LOGGING builds, too, to suppress
|
|
|
|
// clang's -Wunused-private-field build warning for this variable:
|
|
|
|
unused << mFdForLogging;
|
2012-01-31 16:04:57 +00:00
|
|
|
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_ERROR("CertErrorRunnable::CheckCertOverrides called off main thread");
|
2012-04-30 04:00:29 +00:00
|
|
|
return new SSLServerCertVerificationResult(mInfoObject,
|
2012-01-31 16:04:57 +00:00
|
|
|
mDefaultErrorCodeToReport);
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t port;
|
2012-01-31 16:04:57 +00:00
|
|
|
mInfoObject->GetPort(&port);
|
|
|
|
|
|
|
|
nsCString hostWithPortString;
|
2013-10-11 07:17:19 +00:00
|
|
|
hostWithPortString.AppendASCII(mInfoObject->GetHostNameRaw());
|
2014-05-22 03:48:51 +00:00
|
|
|
hostWithPortString.Append(':');
|
2012-01-31 16:04:57 +00:00
|
|
|
hostWithPortString.AppendInt(port);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t remaining_display_errors = mCollectedErrors;
|
2012-01-31 16:04:57 +00:00
|
|
|
|
|
|
|
nsresult nsrv;
|
|
|
|
|
|
|
|
// Enforce Strict-Transport-Security for hosts that are "STS" hosts:
|
|
|
|
// connections must be dropped when there are any certificate errors
|
|
|
|
// (STS Spec section 7.3).
|
|
|
|
bool strictTransportSecurityEnabled = false;
|
2013-08-02 22:48:37 +00:00
|
|
|
nsCOMPtr<nsISiteSecurityService> sss
|
|
|
|
= do_GetService(NS_SSSERVICE_CONTRACTID, &nsrv);
|
2012-01-31 16:04:57 +00:00
|
|
|
if (NS_SUCCEEDED(nsrv)) {
|
2013-08-02 23:23:18 +00:00
|
|
|
nsrv = sss->IsSecureHost(nsISiteSecurityService::HEADER_HSTS,
|
2013-10-11 07:17:19 +00:00
|
|
|
mInfoObject->GetHostNameRaw(),
|
2013-08-02 23:23:18 +00:00
|
|
|
mProviderFlags,
|
|
|
|
&strictTransportSecurityEnabled);
|
2012-01-31 16:04:57 +00:00
|
|
|
}
|
|
|
|
if (NS_FAILED(nsrv)) {
|
2012-04-30 04:00:29 +00:00
|
|
|
return new SSLServerCertVerificationResult(mInfoObject,
|
2012-01-31 16:04:57 +00:00
|
|
|
mDefaultErrorCodeToReport);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strictTransportSecurityEnabled) {
|
|
|
|
nsCOMPtr<nsICertOverrideService> overrideService =
|
|
|
|
do_GetService(NS_CERTOVERRIDE_CONTRACTID);
|
|
|
|
// it is fine to continue without the nsICertOverrideService
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t overrideBits = 0;
|
2012-01-31 16:04:57 +00:00
|
|
|
|
|
|
|
if (overrideService)
|
|
|
|
{
|
|
|
|
bool haveOverride;
|
|
|
|
bool isTemporaryOverride; // we don't care
|
2013-08-05 20:18:06 +00:00
|
|
|
nsCString hostString(mInfoObject->GetHostName());
|
2012-01-31 16:04:57 +00:00
|
|
|
nsrv = overrideService->HasMatchingOverride(hostString, port,
|
|
|
|
mCert,
|
|
|
|
&overrideBits,
|
2014-01-22 01:30:44 +00:00
|
|
|
&isTemporaryOverride,
|
2012-01-31 16:04:57 +00:00
|
|
|
&haveOverride);
|
2014-01-22 01:30:44 +00:00
|
|
|
if (NS_SUCCEEDED(nsrv) && haveOverride)
|
2012-01-31 16:04:57 +00:00
|
|
|
{
|
|
|
|
// remove the errors that are already overriden
|
2012-09-28 18:05:26 +00:00
|
|
|
remaining_display_errors &= ~overrideBits;
|
2012-01-31 16:04:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!remaining_display_errors) {
|
2014-02-13 22:53:29 +00:00
|
|
|
// This can double- or triple-count one certificate with multiple
|
|
|
|
// different types of errors. Since this is telemetry and we just
|
|
|
|
// want a ballpark answer, we don't care.
|
|
|
|
if (mErrorCodeTrust != 0) {
|
|
|
|
uint32_t probeValue = MapCertErrorToProbeValue(mErrorCodeTrust);
|
|
|
|
Telemetry::Accumulate(Telemetry::SSL_CERT_ERROR_OVERRIDES, probeValue);
|
|
|
|
}
|
|
|
|
if (mErrorCodeMismatch != 0) {
|
|
|
|
uint32_t probeValue = MapCertErrorToProbeValue(mErrorCodeMismatch);
|
|
|
|
Telemetry::Accumulate(Telemetry::SSL_CERT_ERROR_OVERRIDES, probeValue);
|
|
|
|
}
|
|
|
|
if (mErrorCodeExpired != 0) {
|
|
|
|
uint32_t probeValue = MapCertErrorToProbeValue(mErrorCodeExpired);
|
|
|
|
Telemetry::Accumulate(Telemetry::SSL_CERT_ERROR_OVERRIDES, probeValue);
|
|
|
|
}
|
|
|
|
|
2012-01-31 16:04:57 +00:00
|
|
|
// all errors are covered by override rules, so let's accept the cert
|
|
|
|
PR_LOG(gPIPNSSLog, PR_LOG_DEBUG,
|
|
|
|
("[%p][%p] All errors covered by override rules\n",
|
|
|
|
mFdForLogging, this));
|
2012-04-30 04:00:29 +00:00
|
|
|
return new SSLServerCertVerificationResult(mInfoObject, 0);
|
2012-01-31 16:04:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PR_LOG(gPIPNSSLog, PR_LOG_DEBUG,
|
|
|
|
("[%p][%p] Strict-Transport-Security is violated: untrusted "
|
|
|
|
"transport layer\n", mFdForLogging, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_LOG(gPIPNSSLog, PR_LOG_DEBUG,
|
|
|
|
("[%p][%p] Certificate error was not overridden\n",
|
|
|
|
mFdForLogging, this));
|
|
|
|
|
|
|
|
// Ok, this is a full stop.
|
|
|
|
// First, deliver the technical details of the broken SSL status.
|
|
|
|
|
|
|
|
// Try to get a nsIBadCertListener2 implementation from the socket consumer.
|
2012-04-30 04:00:29 +00:00
|
|
|
nsCOMPtr<nsISSLSocketControl> sslSocketControl = do_QueryInterface(
|
|
|
|
NS_ISUPPORTS_CAST(nsITransportSecurityInfo*, mInfoObject));
|
|
|
|
if (sslSocketControl) {
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> cb;
|
|
|
|
sslSocketControl->GetNotificationCallbacks(getter_AddRefs(cb));
|
|
|
|
if (cb) {
|
|
|
|
nsCOMPtr<nsIBadCertListener2> bcl = do_GetInterface(cb);
|
|
|
|
if (bcl) {
|
2014-01-22 01:30:44 +00:00
|
|
|
nsIInterfaceRequestor* csi
|
2012-04-30 04:00:29 +00:00
|
|
|
= static_cast<nsIInterfaceRequestor*>(mInfoObject);
|
|
|
|
bool suppressMessage = false; // obsolete, ignored
|
|
|
|
nsrv = bcl->NotifyCertProblem(csi, mInfoObject->SSLStatus(),
|
|
|
|
hostWithPortString, &suppressMessage);
|
|
|
|
}
|
2012-01-31 16:04:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pick the error code to report by priority
|
|
|
|
PRErrorCode errorCodeToReport = mErrorCodeTrust ? mErrorCodeTrust
|
|
|
|
: mErrorCodeMismatch ? mErrorCodeMismatch
|
|
|
|
: mErrorCodeExpired ? mErrorCodeExpired
|
|
|
|
: mDefaultErrorCodeToReport;
|
2014-01-22 01:30:44 +00:00
|
|
|
|
|
|
|
SSLServerCertVerificationResult* result =
|
|
|
|
new SSLServerCertVerificationResult(mInfoObject,
|
2012-08-28 13:32:34 +00:00
|
|
|
errorCodeToReport,
|
2013-01-02 16:14:07 +00:00
|
|
|
Telemetry::HistogramCount,
|
|
|
|
-1,
|
2012-08-28 13:32:34 +00:00
|
|
|
OverridableCertErrorMessage);
|
|
|
|
|
|
|
|
LogInvalidCertError(mInfoObject,
|
|
|
|
result->mErrorCode,
|
2014-01-17 19:57:57 +00:00
|
|
|
result->mErrorMessageType);
|
2012-08-28 13:32:34 +00:00
|
|
|
|
|
|
|
return result;
|
2012-01-31 16:04:57 +00:00
|
|
|
}
|
|
|
|
|
2014-01-22 01:30:44 +00:00
|
|
|
void
|
2012-01-31 16:04:57 +00:00
|
|
|
CertErrorRunnable::RunOnTargetThread()
|
|
|
|
{
|
2012-01-31 16:05:06 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mResult = CheckCertOverrides();
|
2014-01-22 01:30:44 +00:00
|
|
|
|
2012-01-31 16:05:06 +00:00
|
|
|
MOZ_ASSERT(mResult);
|
2012-01-31 16:04:57 +00:00
|
|
|
}
|
|
|
|
|
2014-02-23 03:08:06 +00:00
|
|
|
// Returns null with the error code (PR_GetError()) set if it does not create
|
|
|
|
// the CertErrorRunnable.
|
|
|
|
CertErrorRunnable*
|
|
|
|
CreateCertErrorRunnable(CertVerifier& certVerifier,
|
|
|
|
PRErrorCode defaultErrorCodeToReport,
|
|
|
|
TransportSecurityInfo* infoObject,
|
|
|
|
CERTCertificate* cert,
|
|
|
|
const void* fdForLogging,
|
|
|
|
uint32_t providerFlags,
|
|
|
|
PRTime now)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(infoObject);
|
|
|
|
MOZ_ASSERT(cert);
|
|
|
|
|
|
|
|
uint32_t collected_errors = 0;
|
|
|
|
PRErrorCode errorCodeTrust = 0;
|
|
|
|
PRErrorCode errorCodeMismatch = 0;
|
|
|
|
PRErrorCode errorCodeExpired = 0;
|
2014-06-17 06:13:29 +00:00
|
|
|
if (DetermineCertOverrideErrors(cert, infoObject->GetHostNameRaw(), now,
|
|
|
|
defaultErrorCodeToReport, collected_errors,
|
|
|
|
errorCodeTrust, errorCodeMismatch,
|
|
|
|
errorCodeExpired) != SECSuccess) {
|
2014-02-23 03:08:06 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<nsNSSCertificate> nssCert(nsNSSCertificate::Create(cert));
|
|
|
|
if (!nssCert) {
|
|
|
|
NS_ERROR("nsNSSCertificate::Create failed");
|
|
|
|
PR_SetError(SEC_ERROR_NO_MEMORY, 0);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!collected_errors) {
|
2012-01-31 16:04:57 +00:00
|
|
|
// This will happen when CERT_*Verify* only returned error(s) that are
|
|
|
|
// not on our whitelist of overridable certificate errors.
|
|
|
|
PR_LOG(gPIPNSSLog, PR_LOG_DEBUG, ("[%p] !collected_errors: %d\n",
|
|
|
|
fdForLogging, static_cast<int>(defaultErrorCodeToReport)));
|
|
|
|
PR_SetError(defaultErrorCodeToReport, 0);
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2012-01-31 16:04:57 +00:00
|
|
|
}
|
|
|
|
|
2012-04-30 04:00:29 +00:00
|
|
|
infoObject->SetStatusErrorBits(*nssCert, collected_errors);
|
2012-01-31 16:04:57 +00:00
|
|
|
|
2014-01-22 01:30:44 +00:00
|
|
|
return new CertErrorRunnable(fdForLogging,
|
2012-01-31 16:05:06 +00:00
|
|
|
static_cast<nsIX509Cert*>(nssCert.get()),
|
2014-01-22 01:30:44 +00:00
|
|
|
infoObject, defaultErrorCodeToReport,
|
|
|
|
collected_errors, errorCodeTrust,
|
2012-12-07 03:05:27 +00:00
|
|
|
errorCodeMismatch, errorCodeExpired,
|
|
|
|
providerFlags);
|
2012-01-31 16:05:06 +00:00
|
|
|
}
|
2012-01-31 16:04:57 +00:00
|
|
|
|
2012-01-31 16:05:06 +00:00
|
|
|
// When doing async cert processing, we dispatch one of these runnables to the
|
|
|
|
// socket transport service thread, which blocks the socket transport
|
|
|
|
// service thread while it waits for the inner CertErrorRunnable to execute
|
|
|
|
// CheckCertOverrides on the main thread. CheckCertOverrides must block events
|
2014-01-22 01:30:44 +00:00
|
|
|
// on both of these threads because it calls TransportSecurityInfo::GetInterface(),
|
2012-01-31 16:05:06 +00:00
|
|
|
// which may call nsHttpConnection::GetInterface() through
|
2012-04-30 04:00:29 +00:00
|
|
|
// TransportSecurityInfo::mCallbacks. nsHttpConnection::GetInterface must always
|
2012-01-31 16:05:06 +00:00
|
|
|
// execute on the main thread, with the socket transport service thread
|
|
|
|
// blocked.
|
|
|
|
class CertErrorRunnableRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2014-08-31 23:26:27 +00:00
|
|
|
explicit CertErrorRunnableRunnable(CertErrorRunnable* certErrorRunnable)
|
2012-01-31 16:05:06 +00:00
|
|
|
: mCertErrorRunnable(certErrorRunnable)
|
|
|
|
{
|
2012-01-31 16:04:57 +00:00
|
|
|
}
|
2012-01-31 16:05:06 +00:00
|
|
|
private:
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
nsresult rv = mCertErrorRunnable->DispatchToMainThreadAndWait();
|
|
|
|
// The result must run on the socket transport thread, which we are already
|
|
|
|
// on, so we can just run it directly, instead of dispatching it.
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = mCertErrorRunnable->mResult ? mCertErrorRunnable->mResult->Run()
|
|
|
|
: NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return rv;
|
2012-01-31 16:04:57 +00:00
|
|
|
}
|
2012-10-17 20:48:36 +00:00
|
|
|
RefPtr<CertErrorRunnable> mCertErrorRunnable;
|
2012-01-31 16:05:06 +00:00
|
|
|
};
|
2012-01-31 16:04:57 +00:00
|
|
|
|
2011-12-01 22:37:57 +00:00
|
|
|
class SSLServerCertVerificationJob : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Must be called only on the socket transport thread
|
2014-01-19 22:05:40 +00:00
|
|
|
static SECStatus Dispatch(const RefPtr<SharedCertVerifier>& certVerifier,
|
|
|
|
const void* fdForLogging,
|
|
|
|
TransportSecurityInfo* infoObject,
|
2014-01-22 01:30:44 +00:00
|
|
|
CERTCertificate* serverCert,
|
2014-08-15 18:27:22 +00:00
|
|
|
ScopedCERTCertList& peerCertChain,
|
2014-01-22 01:30:44 +00:00
|
|
|
SECItem* stapledOCSPResponse,
|
2013-09-28 02:53:36 +00:00
|
|
|
uint32_t providerFlags,
|
2014-08-02 15:49:12 +00:00
|
|
|
Time time,
|
|
|
|
PRTime prtime);
|
2011-12-01 22:37:57 +00:00
|
|
|
private:
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
// Must be called only on the socket transport thread
|
2014-01-19 22:05:40 +00:00
|
|
|
SSLServerCertVerificationJob(const RefPtr<SharedCertVerifier>& certVerifier,
|
|
|
|
const void* fdForLogging,
|
|
|
|
TransportSecurityInfo* infoObject,
|
2014-01-22 01:30:44 +00:00
|
|
|
CERTCertificate* cert,
|
2014-08-15 18:27:22 +00:00
|
|
|
CERTCertList* peerCertChain,
|
2014-01-22 01:30:44 +00:00
|
|
|
SECItem* stapledOCSPResponse,
|
2013-09-28 02:53:36 +00:00
|
|
|
uint32_t providerFlags,
|
2014-08-02 15:49:12 +00:00
|
|
|
Time time,
|
|
|
|
PRTime prtime);
|
2014-01-19 22:05:40 +00:00
|
|
|
const RefPtr<SharedCertVerifier> mCertVerifier;
|
2014-01-22 01:30:44 +00:00
|
|
|
const void* const mFdForLogging;
|
2014-01-19 22:05:40 +00:00
|
|
|
const RefPtr<TransportSecurityInfo> mInfoObject;
|
2014-07-04 04:49:56 +00:00
|
|
|
const ScopedCERTCertificate mCert;
|
2014-08-15 18:27:22 +00:00
|
|
|
ScopedCERTCertList mPeerCertChain;
|
2012-12-07 03:05:27 +00:00
|
|
|
const uint32_t mProviderFlags;
|
2014-08-02 15:49:12 +00:00
|
|
|
const Time mTime;
|
|
|
|
const PRTime mPRTime;
|
2013-01-02 16:14:07 +00:00
|
|
|
const TimeStamp mJobStartTime;
|
2013-10-02 22:08:07 +00:00
|
|
|
const ScopedSECItem mStapledOCSPResponse;
|
2011-12-01 22:37:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SSLServerCertVerificationJob::SSLServerCertVerificationJob(
|
2014-01-19 22:05:40 +00:00
|
|
|
const RefPtr<SharedCertVerifier>& certVerifier, const void* fdForLogging,
|
|
|
|
TransportSecurityInfo* infoObject, CERTCertificate* cert,
|
2014-08-15 18:27:22 +00:00
|
|
|
CERTCertList* peerCertChain, SECItem* stapledOCSPResponse,
|
|
|
|
uint32_t providerFlags, Time time, PRTime prtime)
|
2014-01-19 22:05:40 +00:00
|
|
|
: mCertVerifier(certVerifier)
|
|
|
|
, mFdForLogging(fdForLogging)
|
2012-04-30 04:00:29 +00:00
|
|
|
, mInfoObject(infoObject)
|
|
|
|
, mCert(CERT_DupCertificate(cert))
|
2014-08-15 18:27:22 +00:00
|
|
|
, mPeerCertChain(peerCertChain)
|
2012-12-07 03:05:27 +00:00
|
|
|
, mProviderFlags(providerFlags)
|
2013-09-28 02:53:36 +00:00
|
|
|
, mTime(time)
|
2014-08-02 15:49:12 +00:00
|
|
|
, mPRTime(prtime)
|
2013-01-02 16:14:07 +00:00
|
|
|
, mJobStartTime(TimeStamp::Now())
|
2013-10-02 22:08:07 +00:00
|
|
|
, mStapledOCSPResponse(SECITEM_DupItem(stapledOCSPResponse))
|
2011-12-01 22:37:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-12-13 15:55:50 +00:00
|
|
|
// This function assumes that we will only use the SPDY connection coalescing
|
|
|
|
// feature on connections where we have negotiated SPDY using NPN. If we ever
|
|
|
|
// talk SPDY without having negotiated it with SPDY, this code will give wrong
|
|
|
|
// and perhaps unsafe results.
|
|
|
|
//
|
|
|
|
// Returns SECSuccess on the initial handshake of all connections, on
|
|
|
|
// renegotiations for any connections where we did not negotiate SPDY, or on any
|
|
|
|
// SPDY connection where the server's certificate did not change.
|
|
|
|
//
|
|
|
|
// Prohibit changing the server cert only if we negotiated SPDY,
|
|
|
|
// in order to support SPDY's cross-origin connection pooling.
|
|
|
|
static SECStatus
|
2014-01-22 01:30:44 +00:00
|
|
|
BlockServerCertChangeForSpdy(nsNSSSocketInfo* infoObject,
|
|
|
|
CERTCertificate* serverCert)
|
2011-12-13 15:55:50 +00:00
|
|
|
{
|
|
|
|
// Get the existing cert. If there isn't one, then there is
|
|
|
|
// no cert change to worry about.
|
|
|
|
nsCOMPtr<nsIX509Cert> cert;
|
|
|
|
|
2012-10-17 20:48:36 +00:00
|
|
|
RefPtr<nsSSLStatus> status(infoObject->SSLStatus());
|
2011-12-13 15:55:50 +00:00
|
|
|
if (!status) {
|
|
|
|
// If we didn't have a status, then this is the
|
|
|
|
// first handshake on this connection, not a
|
|
|
|
// renegotiation.
|
|
|
|
return SECSuccess;
|
|
|
|
}
|
2014-01-22 01:30:44 +00:00
|
|
|
|
2011-12-13 15:55:50 +00:00
|
|
|
status->GetServerCert(getter_AddRefs(cert));
|
2014-07-04 05:09:24 +00:00
|
|
|
if (!cert) {
|
2011-12-13 15:55:50 +00:00
|
|
|
NS_NOTREACHED("every nsSSLStatus must have a cert"
|
2014-07-04 05:09:24 +00:00
|
|
|
"that implements nsIX509Cert");
|
2011-12-13 15:55:50 +00:00
|
|
|
PR_SetError(SEC_ERROR_LIBRARY_FAILURE, 0);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out sockets that did not neogtiate SPDY via NPN
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString negotiatedNPN;
|
2011-12-13 15:55:50 +00:00
|
|
|
nsresult rv = infoObject->GetNegotiatedNPN(negotiatedNPN);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"GetNegotiatedNPN() failed during renegotiation");
|
|
|
|
|
2012-05-25 21:37:01 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && !StringBeginsWith(negotiatedNPN,
|
2014-07-04 05:09:24 +00:00
|
|
|
NS_LITERAL_CSTRING("spdy/"))) {
|
2011-12-13 15:55:50 +00:00
|
|
|
return SECSuccess;
|
2014-07-04 05:09:24 +00:00
|
|
|
}
|
2011-12-13 15:55:50 +00:00
|
|
|
// If GetNegotiatedNPN() failed we will assume spdy for safety's safe
|
2013-06-24 07:57:51 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-12-13 15:55:50 +00:00
|
|
|
PR_LOG(gPIPNSSLog, PR_LOG_DEBUG,
|
|
|
|
("BlockServerCertChangeForSpdy failed GetNegotiatedNPN() call."
|
|
|
|
" Assuming spdy.\n"));
|
2013-06-24 07:57:51 +00:00
|
|
|
}
|
2011-12-13 15:55:50 +00:00
|
|
|
|
|
|
|
// Check to see if the cert has actually changed
|
2014-07-04 05:09:24 +00:00
|
|
|
ScopedCERTCertificate c(cert->GetCert());
|
2011-12-13 15:55:50 +00:00
|
|
|
NS_ASSERTION(c, "very bad and hopefully impossible state");
|
|
|
|
bool sameCert = CERT_CompareCerts(c, serverCert);
|
2014-07-04 05:09:24 +00:00
|
|
|
if (sameCert) {
|
2011-12-13 15:55:50 +00:00
|
|
|
return SECSuccess;
|
2014-07-04 05:09:24 +00:00
|
|
|
}
|
2011-12-13 15:55:50 +00:00
|
|
|
|
|
|
|
// Report an error - changed cert is confirmed
|
|
|
|
PR_LOG(gPIPNSSLog, PR_LOG_DEBUG,
|
|
|
|
("SPDY Refused to allow new cert during renegotiation\n"));
|
|
|
|
PR_SetError(SSL_ERROR_RENEGOTIATION_NOT_ALLOWED, 0);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
2011-12-01 22:37:57 +00:00
|
|
|
SECStatus
|
2014-08-15 18:27:22 +00:00
|
|
|
AuthCertificate(CertVerifier& certVerifier,
|
|
|
|
TransportSecurityInfo* infoObject,
|
|
|
|
CERTCertificate* cert,
|
|
|
|
ScopedCERTCertList& peerCertChain,
|
|
|
|
SECItem* stapledOCSPResponse,
|
|
|
|
uint32_t providerFlags,
|
|
|
|
Time time)
|
2011-12-01 22:37:57 +00:00
|
|
|
{
|
2014-02-05 21:39:26 +00:00
|
|
|
MOZ_ASSERT(infoObject);
|
|
|
|
MOZ_ASSERT(cert);
|
|
|
|
|
2013-10-02 22:08:07 +00:00
|
|
|
SECStatus rv;
|
2013-09-28 02:53:36 +00:00
|
|
|
|
2014-02-05 21:39:26 +00:00
|
|
|
// We want to avoid storing any intermediate cert information when browsing
|
|
|
|
// in private, transient contexts.
|
|
|
|
bool saveIntermediates =
|
2013-07-08 23:30:59 +00:00
|
|
|
!(providerFlags & nsISocketProvider::NO_PERMANENT_STORAGE);
|
2014-01-23 01:13:19 +00:00
|
|
|
|
2012-10-27 07:11:35 +00:00
|
|
|
SECOidTag evOidPolicy;
|
2013-09-28 02:53:36 +00:00
|
|
|
rv = certVerifier.VerifySSLServerCert(cert, stapledOCSPResponse,
|
|
|
|
time, infoObject,
|
2013-07-08 23:30:59 +00:00
|
|
|
infoObject->GetHostNameRaw(),
|
2014-09-02 23:49:51 +00:00
|
|
|
saveIntermediates, 0, nullptr,
|
2013-07-08 23:30:59 +00:00
|
|
|
&evOidPolicy);
|
2011-12-01 22:37:23 +00:00
|
|
|
|
|
|
|
// We want to remember the CA certs in the temp db, so that the application can find the
|
|
|
|
// complete chain at any time it might need it.
|
|
|
|
// But we keep only those CA certs in the temp db, that we didn't already know.
|
|
|
|
|
2012-10-17 20:48:36 +00:00
|
|
|
RefPtr<nsSSLStatus> status(infoObject->SSLStatus());
|
|
|
|
RefPtr<nsNSSCertificate> nsc;
|
2011-12-01 22:37:23 +00:00
|
|
|
|
2011-12-01 22:37:57 +00:00
|
|
|
if (!status || !status->mServerCert) {
|
2012-10-27 07:11:35 +00:00
|
|
|
if( rv == SECSuccess ){
|
|
|
|
nsc = nsNSSCertificate::Create(cert, &evOidPolicy);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsc = nsNSSCertificate::Create(cert);
|
|
|
|
}
|
2011-12-01 22:37:57 +00:00
|
|
|
}
|
2011-12-01 22:37:23 +00:00
|
|
|
|
2013-07-08 23:30:59 +00:00
|
|
|
if (rv == SECSuccess) {
|
2011-12-01 22:37:23 +00:00
|
|
|
// The connection may get terminated, for example, if the server requires
|
|
|
|
// a client cert. Let's provide a minimal SSLStatus
|
|
|
|
// to the caller that contains at least the cert and its status.
|
|
|
|
if (!status) {
|
|
|
|
status = new nsSSLStatus();
|
2012-04-30 04:00:29 +00:00
|
|
|
infoObject->SetSSLStatus(status);
|
2011-12-01 22:37:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rv == SECSuccess) {
|
|
|
|
// Certificate verification succeeded delete any potential record
|
|
|
|
// of certificate error bits.
|
2012-04-30 04:00:29 +00:00
|
|
|
RememberCertErrorsTable::GetInstance().RememberCertHasError(infoObject,
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr, rv);
|
2011-12-01 22:37:23 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Certificate verification failed, update the status' bits.
|
2012-04-30 04:00:22 +00:00
|
|
|
RememberCertErrorsTable::GetInstance().LookupCertErrorBits(
|
2012-04-30 04:00:29 +00:00
|
|
|
infoObject, status);
|
2011-12-01 22:37:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status && !status->mServerCert) {
|
|
|
|
status->mServerCert = nsc;
|
|
|
|
PR_LOG(gPIPNSSLog, PR_LOG_DEBUG,
|
2011-12-01 22:37:57 +00:00
|
|
|
("AuthCertificate setting NEW cert %p\n", status->mServerCert.get()));
|
2011-12-01 22:37:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 18:27:22 +00:00
|
|
|
if (rv != SECSuccess) {
|
|
|
|
// Certificate validation failed; store the peer certificate chain on
|
|
|
|
// infoObject so it can be used for error reporting. Note: infoObject
|
|
|
|
// indirectly takes ownership of peerCertChain.
|
|
|
|
infoObject->SetFailedCertChain(peerCertChain);
|
|
|
|
}
|
|
|
|
|
2011-12-01 22:37:23 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2011-12-01 22:37:57 +00:00
|
|
|
|
|
|
|
/*static*/ SECStatus
|
2014-01-19 22:05:40 +00:00
|
|
|
SSLServerCertVerificationJob::Dispatch(
|
|
|
|
const RefPtr<SharedCertVerifier>& certVerifier,
|
|
|
|
const void* fdForLogging,
|
|
|
|
TransportSecurityInfo* infoObject,
|
|
|
|
CERTCertificate* serverCert,
|
2014-08-15 18:27:22 +00:00
|
|
|
ScopedCERTCertList& peerCertChain,
|
2014-01-19 22:05:40 +00:00
|
|
|
SECItem* stapledOCSPResponse,
|
2013-09-28 02:53:36 +00:00
|
|
|
uint32_t providerFlags,
|
2014-08-02 15:49:12 +00:00
|
|
|
Time time,
|
|
|
|
PRTime prtime)
|
2011-12-01 22:37:57 +00:00
|
|
|
{
|
|
|
|
// Runs on the socket transport thread
|
2014-01-19 22:05:40 +00:00
|
|
|
if (!certVerifier || !infoObject || !serverCert) {
|
2011-12-01 22:37:57 +00:00
|
|
|
NS_ERROR("Invalid parameters for SSL server cert validation");
|
2012-01-31 13:19:52 +00:00
|
|
|
PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
|
2011-12-01 22:37:57 +00:00
|
|
|
return SECFailure;
|
|
|
|
}
|
2014-01-22 01:30:44 +00:00
|
|
|
|
2014-08-15 18:27:22 +00:00
|
|
|
// Copy the certificate list so the runnable can take ownership of it in the
|
|
|
|
// constructor.
|
|
|
|
// We can safely skip checking if NSS has already shut down here since we're
|
|
|
|
// in the middle of verifying a certificate.
|
|
|
|
nsNSSShutDownPreventionLock lock;
|
|
|
|
CERTCertList* peerCertChainCopy = nsNSSCertList::DupCertList(peerCertChain, lock);
|
|
|
|
|
2012-10-17 20:48:36 +00:00
|
|
|
RefPtr<SSLServerCertVerificationJob> job(
|
2014-01-19 22:05:40 +00:00
|
|
|
new SSLServerCertVerificationJob(certVerifier, fdForLogging, infoObject,
|
2014-08-15 18:27:22 +00:00
|
|
|
serverCert, peerCertChainCopy,
|
|
|
|
stapledOCSPResponse, providerFlags,
|
|
|
|
time, prtime));
|
2011-12-01 22:37:57 +00:00
|
|
|
|
|
|
|
nsresult nrv;
|
|
|
|
if (!gCertVerificationThreadPool) {
|
|
|
|
nrv = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
} else {
|
|
|
|
nrv = gCertVerificationThreadPool->Dispatch(job, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
if (NS_FAILED(nrv)) {
|
2012-01-31 13:19:52 +00:00
|
|
|
// We can't call SetCertVerificationResult here to change
|
|
|
|
// mCertVerificationState because SetCertVerificationResult will call
|
|
|
|
// libssl functions that acquire SSL locks that are already being held at
|
2012-04-30 04:00:29 +00:00
|
|
|
// this point. infoObject->mCertVerificationState will be stuck at
|
2012-01-31 13:19:52 +00:00
|
|
|
// waiting_for_cert_verification here, but that is OK because we already
|
|
|
|
// have to be able to handle cases where we encounter non-cert errors while
|
|
|
|
// in that state.
|
2011-12-01 22:37:57 +00:00
|
|
|
PRErrorCode error = nrv == NS_ERROR_OUT_OF_MEMORY
|
|
|
|
? SEC_ERROR_NO_MEMORY
|
|
|
|
: PR_INVALID_STATE_ERROR;
|
|
|
|
PORT_SetError(error);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
PORT_SetError(PR_WOULD_BLOCK_ERROR);
|
2014-01-22 01:30:44 +00:00
|
|
|
return SECWouldBlock;
|
2011-12-01 22:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SSLServerCertVerificationJob::Run()
|
|
|
|
{
|
|
|
|
// Runs on a cert verification thread
|
|
|
|
|
|
|
|
PR_LOG(gPIPNSSLog, PR_LOG_DEBUG,
|
2012-04-30 04:00:29 +00:00
|
|
|
("[%p] SSLServerCertVerificationJob::Run\n", mInfoObject.get()));
|
2011-12-01 22:37:57 +00:00
|
|
|
|
|
|
|
PRErrorCode error;
|
|
|
|
|
|
|
|
nsNSSShutDownPreventionLock nssShutdownPrevention;
|
2012-04-30 04:00:29 +00:00
|
|
|
if (mInfoObject->isAlreadyShutDown()) {
|
2011-12-01 22:37:57 +00:00
|
|
|
error = SEC_ERROR_USER_CANCELLED;
|
|
|
|
} else {
|
2014-06-17 06:13:29 +00:00
|
|
|
Telemetry::ID successTelemetry
|
|
|
|
= Telemetry::SSL_SUCCESFUL_CERT_VALIDATION_TIME_MOZILLAPKIX;
|
|
|
|
Telemetry::ID failureTelemetry
|
|
|
|
= Telemetry::SSL_INITIAL_FAILED_CERT_VALIDATION_TIME_MOZILLAPKIX;
|
2014-01-19 22:05:40 +00:00
|
|
|
|
2011-12-01 22:37:57 +00:00
|
|
|
// Reset the error code here so we can detect if AuthCertificate fails to
|
|
|
|
// set the error code if/when it fails.
|
2013-10-02 22:08:07 +00:00
|
|
|
PR_SetError(0, 0);
|
2014-01-23 01:13:19 +00:00
|
|
|
SECStatus rv = AuthCertificate(*mCertVerifier, mInfoObject, mCert.get(),
|
2014-08-15 18:27:22 +00:00
|
|
|
mPeerCertChain, mStapledOCSPResponse,
|
|
|
|
mProviderFlags, mTime);
|
2011-12-01 22:37:57 +00:00
|
|
|
if (rv == SECSuccess) {
|
2013-01-02 16:14:07 +00:00
|
|
|
uint32_t interval = (uint32_t) ((TimeStamp::Now() - mJobStartTime).ToMilliseconds());
|
2012-10-17 20:48:36 +00:00
|
|
|
RefPtr<SSLServerCertVerificationResult> restart(
|
2013-01-02 16:14:07 +00:00
|
|
|
new SSLServerCertVerificationResult(mInfoObject, 0,
|
2014-01-19 22:05:40 +00:00
|
|
|
successTelemetry, interval));
|
2011-12-01 22:37:57 +00:00
|
|
|
restart->Dispatch();
|
2014-02-13 22:53:29 +00:00
|
|
|
Telemetry::Accumulate(Telemetry::SSL_CERT_ERROR_OVERRIDES, 1);
|
2011-12-01 22:37:57 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-02 16:14:07 +00:00
|
|
|
// Note: the interval is not calculated once as PR_GetError MUST be called
|
|
|
|
// before any other function call
|
2011-12-01 22:37:57 +00:00
|
|
|
error = PR_GetError();
|
2013-01-02 16:14:07 +00:00
|
|
|
{
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
MutexAutoLock telemetryMutex(*gSSLVerificationTelemetryMutex);
|
2014-01-19 22:05:40 +00:00
|
|
|
Telemetry::AccumulateTimeDelta(failureTelemetry, mJobStartTime, now);
|
2013-01-02 16:14:07 +00:00
|
|
|
}
|
2011-12-01 22:37:57 +00:00
|
|
|
if (error != 0) {
|
2014-01-19 22:05:40 +00:00
|
|
|
RefPtr<CertErrorRunnable> runnable(
|
2014-01-23 01:13:19 +00:00
|
|
|
CreateCertErrorRunnable(*mCertVerifier, error, mInfoObject,
|
2014-06-21 02:43:09 +00:00
|
|
|
mCert.get(), mFdForLogging, mProviderFlags,
|
2014-08-02 15:49:12 +00:00
|
|
|
mPRTime));
|
2012-01-31 16:05:06 +00:00
|
|
|
if (!runnable) {
|
|
|
|
// CreateCertErrorRunnable set a new error code
|
2014-01-22 01:30:44 +00:00
|
|
|
error = PR_GetError();
|
2012-01-31 16:05:06 +00:00
|
|
|
} else {
|
|
|
|
// We must block the the socket transport service thread while the
|
|
|
|
// main thread executes the CertErrorRunnable. The CertErrorRunnable
|
|
|
|
// will dispatch the result asynchronously, so we don't have to block
|
|
|
|
// this thread waiting for it.
|
|
|
|
|
|
|
|
PR_LOG(gPIPNSSLog, PR_LOG_DEBUG,
|
|
|
|
("[%p][%p] Before dispatching CertErrorRunnable\n",
|
|
|
|
mFdForLogging, runnable.get()));
|
|
|
|
|
|
|
|
nsresult nrv;
|
|
|
|
nsCOMPtr<nsIEventTarget> stsTarget
|
|
|
|
= do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &nrv);
|
|
|
|
if (NS_SUCCEEDED(nrv)) {
|
|
|
|
nrv = stsTarget->Dispatch(new CertErrorRunnableRunnable(runnable),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(nrv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ERROR("Failed to dispatch CertErrorRunnable");
|
|
|
|
error = PR_INVALID_STATE_ERROR;
|
2011-12-01 22:37:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error == 0) {
|
|
|
|
NS_NOTREACHED("no error set during certificate validation failure");
|
|
|
|
error = PR_INVALID_STATE_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-10-17 20:48:36 +00:00
|
|
|
RefPtr<SSLServerCertVerificationResult> failure(
|
|
|
|
new SSLServerCertVerificationResult(mInfoObject, error));
|
2011-12-01 22:37:57 +00:00
|
|
|
failure->Dispatch();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // unnamed namespace
|
|
|
|
|
|
|
|
// Extracts whatever information we need out of fd (using SSL_*) and passes it
|
|
|
|
// to SSLServerCertVerificationJob::Dispatch. SSLServerCertVerificationJob should
|
|
|
|
// never do anything with fd except logging.
|
|
|
|
SECStatus
|
2014-01-22 01:30:44 +00:00
|
|
|
AuthCertificateHook(void* arg, PRFileDesc* fd, PRBool checkSig, PRBool isServer)
|
2011-12-01 22:37:57 +00:00
|
|
|
{
|
2014-01-19 22:05:40 +00:00
|
|
|
RefPtr<SharedCertVerifier> certVerifier(GetDefaultCertVerifier());
|
|
|
|
if (!certVerifier) {
|
|
|
|
PR_SetError(SEC_ERROR_NOT_INITIALIZED, 0);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
2011-12-01 22:37:57 +00:00
|
|
|
// Runs on the socket transport thread
|
|
|
|
|
|
|
|
PR_LOG(gPIPNSSLog, PR_LOG_DEBUG,
|
|
|
|
("[%p] starting AuthCertificateHook\n", fd));
|
|
|
|
|
2012-10-25 16:32:24 +00:00
|
|
|
// Modern libssl always passes PR_TRUE for checkSig, and we have no means of
|
2011-12-01 22:37:57 +00:00
|
|
|
// doing verification without checking signatures.
|
|
|
|
NS_ASSERTION(checkSig, "AuthCertificateHook: checkSig unexpectedly false");
|
|
|
|
|
2012-10-25 16:32:24 +00:00
|
|
|
// PSM never causes libssl to call this function with PR_TRUE for isServer,
|
2011-12-01 22:37:57 +00:00
|
|
|
// and many things in PSM assume that we are a client.
|
|
|
|
NS_ASSERTION(!isServer, "AuthCertificateHook: isServer unexpectedly true");
|
|
|
|
|
2014-01-22 01:30:44 +00:00
|
|
|
nsNSSSocketInfo* socketInfo = static_cast<nsNSSSocketInfo*>(arg);
|
|
|
|
|
2012-11-12 17:42:28 +00:00
|
|
|
ScopedCERTCertificate serverCert(SSL_PeerCertificate(fd));
|
2012-04-30 04:00:29 +00:00
|
|
|
|
|
|
|
if (!checkSig || isServer || !socketInfo || !serverCert) {
|
2011-12-01 22:37:57 +00:00
|
|
|
PR_SetError(PR_INVALID_STATE_ERROR, 0);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
2013-06-17 23:45:49 +00:00
|
|
|
|
2014-08-15 18:27:22 +00:00
|
|
|
// Get the peer certificate chain for error reporting
|
|
|
|
ScopedCERTCertList peerCertChain(SSL_PeerCertificateChain(fd));
|
|
|
|
|
2013-10-15 08:14:50 +00:00
|
|
|
socketInfo->SetFullHandshake();
|
|
|
|
|
2014-08-02 15:49:12 +00:00
|
|
|
Time now(Now());
|
|
|
|
PRTime prnow(PR_Now());
|
2014-01-19 22:05:40 +00:00
|
|
|
|
2012-04-30 04:00:29 +00:00
|
|
|
if (BlockServerCertChangeForSpdy(socketInfo, serverCert) != SECSuccess)
|
|
|
|
return SECFailure;
|
2012-01-31 16:05:11 +00:00
|
|
|
|
|
|
|
bool onSTSThread;
|
|
|
|
nsresult nrv;
|
|
|
|
nsCOMPtr<nsIEventTarget> sts
|
|
|
|
= do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &nrv);
|
|
|
|
if (NS_SUCCEEDED(nrv)) {
|
|
|
|
nrv = sts->IsOnCurrentThread(&onSTSThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(nrv)) {
|
|
|
|
NS_ERROR("Could not get STS service or IsOnCurrentThread failed");
|
|
|
|
PR_SetError(PR_UNKNOWN_ERROR, 0);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
2012-12-07 03:05:27 +00:00
|
|
|
|
2013-10-02 22:08:07 +00:00
|
|
|
// SSL_PeerStapledOCSPResponses will never return a non-empty response if
|
|
|
|
// OCSP stapling wasn't enabled because libssl wouldn't have let the server
|
|
|
|
// return a stapled OCSP response.
|
|
|
|
// We don't own these pointers.
|
2014-01-22 01:30:44 +00:00
|
|
|
const SECItemArray* csa = SSL_PeerStapledOCSPResponses(fd);
|
|
|
|
SECItem* stapledOCSPResponse = nullptr;
|
2013-10-02 22:08:07 +00:00
|
|
|
// we currently only support single stapled responses
|
|
|
|
if (csa && csa->len == 1) {
|
|
|
|
stapledOCSPResponse = &csa->items[0];
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:05:27 +00:00
|
|
|
uint32_t providerFlags = 0;
|
|
|
|
socketInfo->GetProviderFlags(&providerFlags);
|
|
|
|
|
2012-01-31 16:05:11 +00:00
|
|
|
if (onSTSThread) {
|
2012-12-07 03:05:27 +00:00
|
|
|
|
2012-01-31 16:05:11 +00:00
|
|
|
// We *must* do certificate verification on a background thread because
|
|
|
|
// we need the socket transport thread to be free for our OCSP requests,
|
|
|
|
// and we *want* to do certificate verification on a background thread
|
|
|
|
// because of the performance benefits of doing so.
|
2012-04-30 04:00:29 +00:00
|
|
|
socketInfo->SetCertVerificationWaiting();
|
2012-01-31 16:05:11 +00:00
|
|
|
SECStatus rv = SSLServerCertVerificationJob::Dispatch(
|
2014-01-19 22:05:40 +00:00
|
|
|
certVerifier, static_cast<const void*>(fd), socketInfo,
|
2014-08-15 18:27:22 +00:00
|
|
|
serverCert, peerCertChain, stapledOCSPResponse,
|
|
|
|
providerFlags, now, prnow);
|
2012-01-31 16:05:11 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2014-01-22 01:30:44 +00:00
|
|
|
|
2012-01-31 16:05:11 +00:00
|
|
|
// We can't do certificate verification on a background thread, because the
|
|
|
|
// thread doing the network I/O may not interrupt its network I/O on receipt
|
|
|
|
// of our SSLServerCertVerificationResult event, and/or it might not even be
|
|
|
|
// a non-blocking socket.
|
2013-10-02 22:08:07 +00:00
|
|
|
|
2014-01-19 22:05:40 +00:00
|
|
|
SECStatus rv = AuthCertificate(*certVerifier, socketInfo, serverCert,
|
2014-08-15 18:27:22 +00:00
|
|
|
peerCertChain, stapledOCSPResponse,
|
|
|
|
providerFlags, now);
|
2012-01-31 16:05:11 +00:00
|
|
|
if (rv == SECSuccess) {
|
2014-02-13 22:53:29 +00:00
|
|
|
Telemetry::Accumulate(Telemetry::SSL_CERT_ERROR_OVERRIDES, 1);
|
2012-01-31 16:05:11 +00:00
|
|
|
return SECSuccess;
|
|
|
|
}
|
2011-12-01 22:37:57 +00:00
|
|
|
|
2012-01-31 16:05:11 +00:00
|
|
|
PRErrorCode error = PR_GetError();
|
|
|
|
if (error != 0) {
|
2014-01-19 22:05:40 +00:00
|
|
|
RefPtr<CertErrorRunnable> runnable(
|
|
|
|
CreateCertErrorRunnable(*certVerifier, error, socketInfo, serverCert,
|
|
|
|
static_cast<const void*>(fd), providerFlags,
|
2014-08-02 15:49:12 +00:00
|
|
|
prnow));
|
2012-01-31 16:05:11 +00:00
|
|
|
if (!runnable) {
|
|
|
|
// CreateCertErrorRunnable sets a new error code when it fails
|
|
|
|
error = PR_GetError();
|
|
|
|
} else {
|
|
|
|
// We have to return SECSuccess or SECFailure based on the result of the
|
|
|
|
// override processing, so we must block this thread waiting for it. The
|
|
|
|
// CertErrorRunnable will NOT dispatch the result at all, since we passed
|
|
|
|
// false for CreateCertErrorRunnable's async parameter
|
|
|
|
nrv = runnable->DispatchToMainThreadAndWait();
|
|
|
|
if (NS_FAILED(nrv)) {
|
|
|
|
NS_ERROR("Failed to dispatch CertErrorRunnable");
|
|
|
|
PR_SetError(PR_INVALID_STATE_ERROR, 0);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
2011-12-01 22:37:57 +00:00
|
|
|
|
2012-01-31 16:05:11 +00:00
|
|
|
if (!runnable->mResult) {
|
|
|
|
NS_ERROR("CertErrorRunnable did not set result");
|
|
|
|
PR_SetError(PR_INVALID_STATE_ERROR, 0);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runnable->mResult->mErrorCode == 0) {
|
|
|
|
return SECSuccess; // cert error override occurred.
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must call SetCanceled here to set the error message type
|
|
|
|
// in case it isn't PlainErrorMessage, which is what we would
|
|
|
|
// default to if we just called
|
|
|
|
// PR_SetError(runnable->mResult->mErrorCode, 0) and returned
|
|
|
|
// SECFailure without doing this.
|
|
|
|
socketInfo->SetCanceled(runnable->mResult->mErrorCode,
|
|
|
|
runnable->mResult->mErrorMessageType);
|
|
|
|
error = runnable->mResult->mErrorCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error == 0) {
|
|
|
|
NS_ERROR("error code not set");
|
|
|
|
error = PR_UNKNOWN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_SetError(error, 0);
|
|
|
|
return SECFailure;
|
2011-12-01 22:37:57 +00:00
|
|
|
}
|
|
|
|
|
2014-02-24 06:15:53 +00:00
|
|
|
#ifndef MOZ_NO_EV_CERTS
|
2014-01-27 03:36:28 +00:00
|
|
|
class InitializeIdentityInfo : public CryptoTask
|
2012-11-22 20:57:59 +00:00
|
|
|
{
|
2014-01-27 03:36:28 +00:00
|
|
|
virtual nsresult CalculateResult() MOZ_OVERRIDE
|
2012-11-22 20:57:59 +00:00
|
|
|
{
|
2014-01-27 03:36:28 +00:00
|
|
|
EnsureIdentityInfoLoaded();
|
2012-11-22 20:57:59 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-27 03:36:28 +00:00
|
|
|
virtual void ReleaseNSSResources() MOZ_OVERRIDE { } // no-op
|
|
|
|
virtual void CallCallback(nsresult rv) MOZ_OVERRIDE { } // no-op
|
2012-11-22 20:57:59 +00:00
|
|
|
};
|
2013-04-18 00:17:10 +00:00
|
|
|
#endif
|
2012-11-22 20:57:59 +00:00
|
|
|
|
|
|
|
void EnsureServerVerificationInitialized()
|
|
|
|
{
|
2014-02-24 06:15:53 +00:00
|
|
|
#ifndef MOZ_NO_EV_CERTS
|
2012-11-22 20:57:59 +00:00
|
|
|
// Should only be called from socket transport thread due to the static
|
|
|
|
// variable and the reference to gCertVerificationThreadPool
|
|
|
|
|
|
|
|
static bool triggeredCertVerifierInit = false;
|
|
|
|
if (triggeredCertVerifierInit)
|
|
|
|
return;
|
|
|
|
triggeredCertVerifierInit = true;
|
|
|
|
|
|
|
|
RefPtr<InitializeIdentityInfo> initJob = new InitializeIdentityInfo();
|
|
|
|
if (gCertVerificationThreadPool)
|
|
|
|
gCertVerificationThreadPool->Dispatch(initJob, NS_DISPATCH_NORMAL);
|
2013-04-18 00:17:10 +00:00
|
|
|
#endif
|
2012-11-22 20:57:59 +00:00
|
|
|
}
|
|
|
|
|
2011-12-01 22:37:57 +00:00
|
|
|
SSLServerCertVerificationResult::SSLServerCertVerificationResult(
|
2014-01-22 01:30:44 +00:00
|
|
|
TransportSecurityInfo* infoObject, PRErrorCode errorCode,
|
2013-01-02 16:14:07 +00:00
|
|
|
Telemetry::ID telemetryID, uint32_t telemetryValue,
|
2011-12-01 22:37:57 +00:00
|
|
|
SSLErrorMessageType errorMessageType)
|
2012-04-30 04:00:29 +00:00
|
|
|
: mInfoObject(infoObject)
|
2011-12-01 22:37:57 +00:00
|
|
|
, mErrorCode(errorCode)
|
|
|
|
, mErrorMessageType(errorMessageType)
|
2013-01-02 16:14:07 +00:00
|
|
|
, mTelemetryID(telemetryID)
|
|
|
|
, mTelemetryValue(telemetryValue)
|
2011-12-01 22:37:57 +00:00
|
|
|
{
|
2013-01-02 16:14:07 +00:00
|
|
|
// We accumulate telemetry for (only) successful validations on the main thread
|
|
|
|
// to avoid adversely affecting performance by acquiring the mutex that we use
|
|
|
|
// when accumulating the telemetry for unsuccessful validations. Unsuccessful
|
|
|
|
// validations times are accumulated elsewhere.
|
|
|
|
MOZ_ASSERT(telemetryID == Telemetry::HistogramCount || errorCode == 0);
|
2011-12-01 22:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SSLServerCertVerificationResult::Dispatch()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIEventTarget> stsTarget
|
|
|
|
= do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ASSERTION(stsTarget,
|
|
|
|
"Failed to get socket transport service event target");
|
|
|
|
rv = stsTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
2014-01-22 01:30:44 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
2011-12-01 22:37:57 +00:00
|
|
|
"Failed to dispatch SSLServerCertVerificationResult");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SSLServerCertVerificationResult::Run()
|
|
|
|
{
|
|
|
|
// TODO: Assert that we're on the socket transport thread
|
2013-01-02 16:14:07 +00:00
|
|
|
if (mTelemetryID != Telemetry::HistogramCount) {
|
|
|
|
Telemetry::Accumulate(mTelemetryID, mTelemetryValue);
|
|
|
|
}
|
2012-04-30 04:00:29 +00:00
|
|
|
// XXX: This cast will be removed by the next patch
|
2014-01-22 01:30:44 +00:00
|
|
|
((nsNSSSocketInfo*) mInfoObject.get())
|
2012-04-30 04:00:29 +00:00
|
|
|
->SetCertVerificationResult(mErrorCode, mErrorMessageType);
|
2011-12-01 22:37:57 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace mozilla::psm
|