2012-12-07 07:05:27 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "SharedSSLState.h"
|
|
|
|
#include "nsClientAuthRemember.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsICertOverrideService.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsCRT.h"
|
2012-12-08 02:50:43 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "PSMRunnable.h"
|
|
|
|
#include "PublicSSL.h"
|
|
|
|
#include "ssl.h"
|
|
|
|
#include "nsNetCID.h"
|
2013-08-12 13:51:49 +04:00
|
|
|
#include "mozilla/Atomics.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2012-12-08 02:50:43 +04:00
|
|
|
|
|
|
|
using mozilla::psm::SyncRunnableBase;
|
2013-08-12 13:51:49 +04:00
|
|
|
using mozilla::Atomic;
|
2015-11-02 08:53:26 +03:00
|
|
|
using mozilla::Unused;
|
2012-12-08 02:50:43 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-02-07 10:17:07 +04:00
|
|
|
static Atomic<bool> sCertOverrideSvcExists(false);
|
2012-12-08 02:50:43 +04:00
|
|
|
|
|
|
|
class MainThreadClearer : public SyncRunnableBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MainThreadClearer() : mShouldClearSessionCache(false) {}
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
void RunOnTargetThread() override {
|
2012-12-08 02:50:43 +04:00
|
|
|
// In some cases it's possible to cause PSM/NSS to initialize while XPCOM shutdown
|
|
|
|
// is in progress. We want to avoid this, since they do not handle the situation well,
|
|
|
|
// hence the flags to avoid instantiating the services if they don't already exist.
|
|
|
|
|
2014-02-07 10:17:07 +04:00
|
|
|
bool certOverrideSvcExists = sCertOverrideSvcExists.exchange(false);
|
2012-12-08 02:50:43 +04:00
|
|
|
if (certOverrideSvcExists) {
|
2014-02-07 10:17:07 +04:00
|
|
|
sCertOverrideSvcExists = true;
|
2012-12-08 02:50:43 +04:00
|
|
|
nsCOMPtr<nsICertOverrideService> icos = do_GetService(NS_CERTOVERRIDE_CONTRACTID);
|
|
|
|
if (icos) {
|
|
|
|
icos->ClearValidityOverride(
|
|
|
|
NS_LITERAL_CSTRING("all:temporary-certificates"),
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This needs to be checked on the main thread to avoid racing with NSS
|
|
|
|
// initialization.
|
|
|
|
mShouldClearSessionCache = mozilla::psm::PrivateSSLState() &&
|
|
|
|
mozilla::psm::PrivateSSLState()->SocketCreated();
|
|
|
|
}
|
|
|
|
bool mShouldClearSessionCache;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2012-12-07 07:05:27 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2012-12-08 02:50:43 +04:00
|
|
|
|
|
|
|
void ClearPrivateSSLState()
|
|
|
|
{
|
|
|
|
// This only works if it is called on the socket transport
|
|
|
|
// service thread immediately after closing all private SSL
|
|
|
|
// connections.
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIEventTarget> sts
|
|
|
|
= do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
bool onSTSThread;
|
|
|
|
rv = sts->IsOnCurrentThread(&onSTSThread);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv) && onSTSThread);
|
|
|
|
#endif
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MainThreadClearer> runnable = new MainThreadClearer;
|
2012-12-08 02:50:43 +04:00
|
|
|
runnable->DispatchToMainThreadAndWait();
|
|
|
|
|
|
|
|
// If NSS isn't initialized, this throws an assertion. We guard it by checking if
|
|
|
|
// the session cache might even have anything worth clearing.
|
|
|
|
if (runnable->mShouldClearSessionCache) {
|
|
|
|
SSL_ClearSessionCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 07:05:27 +04:00
|
|
|
namespace psm {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class PrivateBrowsingObserver : public nsIObserver {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
2014-09-01 03:26:27 +04:00
|
|
|
explicit PrivateBrowsingObserver(SharedSSLState* aOwner) : mOwner(aOwner) {}
|
2014-06-24 02:40:03 +04:00
|
|
|
protected:
|
2012-12-07 07:05:27 +04:00
|
|
|
virtual ~PrivateBrowsingObserver() {}
|
|
|
|
private:
|
|
|
|
SharedSSLState* mOwner;
|
|
|
|
};
|
|
|
|
|
|
|
|
SharedSSLState* gPublicState;
|
|
|
|
SharedSSLState* gPrivateState;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2012-12-07 07:05:27 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(PrivateBrowsingObserver, nsIObserver)
|
2012-12-07 07:05:27 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PrivateBrowsingObserver::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *aData)
|
2012-12-07 07:05:27 +04:00
|
|
|
{
|
|
|
|
if (!nsCRT::strcmp(aTopic, "last-pb-context-exited")) {
|
|
|
|
mOwner->ResetStoredData();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:04:08 +03:00
|
|
|
SharedSSLState::SharedSSLState(uint32_t aTlsFlags)
|
|
|
|
: mIOLayerHelpers(aTlsFlags)
|
2012-12-08 02:50:43 +04:00
|
|
|
, mMutex("SharedSSLState::mMutex")
|
|
|
|
, mSocketCreated(false)
|
2013-06-18 03:45:49 +04:00
|
|
|
, mOCSPStaplingEnabled(false)
|
2015-11-26 09:40:00 +03:00
|
|
|
, mOCSPMustStapleEnabled(false)
|
2017-10-31 13:04:40 +03:00
|
|
|
, mSignedCertTimestampsEnabled(false)
|
2012-12-07 07:05:27 +04:00
|
|
|
{
|
|
|
|
mIOLayerHelpers.Init();
|
2017-08-29 16:04:08 +03:00
|
|
|
if (!aTlsFlags) { // the per socket flags don't need memory
|
|
|
|
mClientAuthRemember = new nsClientAuthRememberService();
|
|
|
|
mClientAuthRemember->Init();
|
|
|
|
}
|
2012-12-07 07:05:27 +04:00
|
|
|
}
|
|
|
|
|
2012-12-08 02:50:43 +04:00
|
|
|
SharedSSLState::~SharedSSLState()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-12-07 07:05:27 +04:00
|
|
|
void
|
|
|
|
SharedSSLState::NotePrivateBrowsingStatus()
|
|
|
|
{
|
2012-12-08 02:50:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not on main thread");
|
2012-12-07 07:05:27 +04:00
|
|
|
mObserver = new PrivateBrowsingObserver(this);
|
|
|
|
nsCOMPtr<nsIObserverService> obsSvc = mozilla::services::GetObserverService();
|
|
|
|
obsSvc->AddObserver(mObserver, "last-pb-context-exited", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSSLState::ResetStoredData()
|
|
|
|
{
|
2017-08-29 16:04:08 +03:00
|
|
|
if (!mClientAuthRemember) {
|
|
|
|
return;
|
|
|
|
}
|
2012-12-08 02:50:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not on main thread");
|
2012-12-07 07:05:27 +04:00
|
|
|
mClientAuthRemember->ClearRememberedDecisions();
|
|
|
|
mIOLayerHelpers.clearStoredData();
|
|
|
|
}
|
|
|
|
|
2012-12-08 02:50:43 +04:00
|
|
|
void
|
|
|
|
SharedSSLState::NoteSocketCreated()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mSocketCreated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SharedSSLState::SocketCreated()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mSocketCreated;
|
|
|
|
}
|
|
|
|
|
2012-12-07 07:05:27 +04:00
|
|
|
/*static*/ void
|
|
|
|
SharedSSLState::GlobalInit()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not on main thread");
|
|
|
|
gPublicState = new SharedSSLState();
|
|
|
|
gPrivateState = new SharedSSLState();
|
|
|
|
gPrivateState->NotePrivateBrowsingStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ void
|
|
|
|
SharedSSLState::GlobalCleanup()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not on main thread");
|
|
|
|
|
2013-01-14 19:53:30 +04:00
|
|
|
if (gPrivateState) {
|
|
|
|
gPrivateState->Cleanup();
|
|
|
|
delete gPrivateState;
|
|
|
|
gPrivateState = nullptr;
|
|
|
|
}
|
2012-12-07 07:05:27 +04:00
|
|
|
|
2013-01-14 19:53:30 +04:00
|
|
|
if (gPublicState) {
|
|
|
|
gPublicState->Cleanup();
|
|
|
|
delete gPublicState;
|
|
|
|
gPublicState = nullptr;
|
|
|
|
}
|
2012-12-07 07:05:27 +04:00
|
|
|
}
|
|
|
|
|
2012-12-08 02:50:43 +04:00
|
|
|
/*static*/ void
|
|
|
|
SharedSSLState::NoteCertOverrideServiceInstantiated()
|
|
|
|
{
|
2014-02-07 10:17:07 +04:00
|
|
|
sCertOverrideSvcExists = true;
|
2012-12-08 02:50:43 +04:00
|
|
|
}
|
|
|
|
|
2012-12-07 07:05:27 +04:00
|
|
|
void
|
|
|
|
SharedSSLState::Cleanup()
|
|
|
|
{
|
|
|
|
mIOLayerHelpers.Cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedSSLState*
|
|
|
|
PublicSSLState()
|
|
|
|
{
|
|
|
|
return gPublicState;
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedSSLState*
|
|
|
|
PrivateSSLState()
|
|
|
|
{
|
|
|
|
return gPrivateState;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace psm
|
|
|
|
} // namespace mozilla
|