2007-10-03 11:43:54 +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/. */
|
2007-10-03 11:43:54 +00:00
|
|
|
|
|
|
|
#ifndef __NSCERTOVERRIDESERVICE_H__
|
|
|
|
#define __NSCERTOVERRIDESERVICE_H__
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2007-10-03 11:43:54 +00:00
|
|
|
#include "nsICertOverrideService.h"
|
|
|
|
#include "nsTHashtable.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "secoidt.h"
|
2008-02-01 06:04:56 +00:00
|
|
|
#include "nsWeakReference.h"
|
2012-07-19 04:37:09 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2007-10-03 11:43:54 +00:00
|
|
|
|
|
|
|
class nsCertOverride
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum OverrideBits { ob_None=0, ob_Untrusted=1, ob_Mismatch=2,
|
|
|
|
ob_Time_error=4 };
|
|
|
|
|
|
|
|
nsCertOverride()
|
2008-04-22 20:03:20 +00:00
|
|
|
:mPort(-1)
|
|
|
|
,mOverrideBits(ob_None)
|
2007-10-03 11:43:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCertOverride(const nsCertOverride &other)
|
|
|
|
{
|
|
|
|
this->operator=(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCertOverride &operator=(const nsCertOverride &other)
|
|
|
|
{
|
2008-04-22 20:03:20 +00:00
|
|
|
mAsciiHost = other.mAsciiHost;
|
|
|
|
mPort = other.mPort;
|
2007-11-19 15:32:43 +00:00
|
|
|
mIsTemporary = other.mIsTemporary;
|
2007-10-03 11:43:54 +00:00
|
|
|
mFingerprintAlgOID = other.mFingerprintAlgOID;
|
|
|
|
mFingerprint = other.mFingerprint;
|
|
|
|
mOverrideBits = other.mOverrideBits;
|
|
|
|
mDBKey = other.mDBKey;
|
2008-10-23 02:16:06 +00:00
|
|
|
mCert = other.mCert;
|
2007-10-03 11:43:54 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-04-22 20:03:20 +00:00
|
|
|
nsCString mAsciiHost;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mPort;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mIsTemporary; // true: session only, false: stored on disk
|
2007-10-03 11:43:54 +00:00
|
|
|
nsCString mFingerprint;
|
|
|
|
nsCString mFingerprintAlgOID;
|
|
|
|
OverrideBits mOverrideBits;
|
|
|
|
nsCString mDBKey;
|
2008-10-23 02:16:06 +00:00
|
|
|
nsCOMPtr <nsIX509Cert> mCert;
|
2007-10-03 11:43:54 +00:00
|
|
|
|
|
|
|
static void convertBitsToString(OverrideBits ob, nsACString &str);
|
|
|
|
static void convertStringToBits(const nsACString &str, OverrideBits &ob);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// hash entry class
|
2015-03-21 16:28:04 +00:00
|
|
|
class nsCertOverrideEntry final : public PLDHashEntryHdr
|
2007-10-03 11:43:54 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Hash methods
|
|
|
|
typedef const char* KeyType;
|
|
|
|
typedef const char* KeyTypePointer;
|
|
|
|
|
|
|
|
// do nothing with aHost - we require mHead to be set before we're live!
|
2014-08-31 23:26:27 +00:00
|
|
|
explicit nsCertOverrideEntry(KeyTypePointer aHostWithPortUTF8)
|
2007-10-03 11:43:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCertOverrideEntry(const nsCertOverrideEntry& toCopy)
|
|
|
|
{
|
|
|
|
mSettings = toCopy.mSettings;
|
2008-06-18 19:20:16 +00:00
|
|
|
mHostWithPort = toCopy.mHostWithPort;
|
2007-10-03 11:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~nsCertOverrideEntry()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyType GetKey() const
|
|
|
|
{
|
|
|
|
return HostWithPortPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyTypePointer GetKeyPointer() const
|
|
|
|
{
|
|
|
|
return HostWithPortPtr();
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const
|
2007-10-03 11:43:54 +00:00
|
|
|
{
|
|
|
|
return !strcmp(HostWithPortPtr(), aKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey)
|
|
|
|
{
|
|
|
|
return aKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
2015-09-14 21:23:47 +00:00
|
|
|
// PLDHashTable::HashStringKey doesn't use the table parameter, so we can
|
|
|
|
// safely pass nullptr
|
|
|
|
return PLDHashTable::HashStringKey(nullptr, aKey);
|
2007-10-03 11:43:54 +00:00
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
enum { ALLOW_MEMMOVE = false };
|
2007-10-03 11:43:54 +00:00
|
|
|
|
|
|
|
// get methods
|
2008-04-22 20:03:20 +00:00
|
|
|
inline const nsCString &HostWithPort() const { return mHostWithPort; }
|
2007-10-03 11:43:54 +00:00
|
|
|
|
|
|
|
inline KeyTypePointer HostWithPortPtr() const
|
|
|
|
{
|
2008-04-22 20:03:20 +00:00
|
|
|
return mHostWithPort.get();
|
2007-10-03 11:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCertOverride mSettings;
|
2008-04-22 20:03:20 +00:00
|
|
|
nsCString mHostWithPort;
|
2007-10-03 11:43:54 +00:00
|
|
|
};
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class nsCertOverrideService final : public nsICertOverrideService
|
2015-03-27 18:52:19 +00:00
|
|
|
, public nsIObserver
|
|
|
|
, public nsSupportsWeakReference
|
2007-10-03 11:43:54 +00:00
|
|
|
{
|
|
|
|
public:
|
2013-07-19 02:24:14 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2007-10-03 11:43:54 +00:00
|
|
|
NS_DECL_NSICERTOVERRIDESERVICE
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
nsCertOverrideService();
|
|
|
|
|
|
|
|
nsresult Init();
|
2008-10-23 02:16:06 +00:00
|
|
|
void RemoveAllTemporaryOverrides();
|
2007-10-03 11:43:54 +00:00
|
|
|
|
|
|
|
typedef void
|
2012-09-25 16:18:38 +00:00
|
|
|
(*CertOverrideEnumerator)(const nsCertOverride &aSettings,
|
|
|
|
void *aUserData);
|
2007-10-03 11:43:54 +00:00
|
|
|
|
|
|
|
// aCert == null: return all overrides
|
|
|
|
// aCert != null: return overrides that match the given cert
|
|
|
|
nsresult EnumerateCertOverrides(nsIX509Cert *aCert,
|
|
|
|
CertOverrideEnumerator enumerator,
|
|
|
|
void *aUserData);
|
|
|
|
|
2008-04-22 20:03:20 +00:00
|
|
|
// Concates host name and the port number. If the port number is -1 then
|
|
|
|
// port 443 is automatically used. This method ensures there is always a port
|
|
|
|
// number separated with colon.
|
2012-08-22 15:56:38 +00:00
|
|
|
static void GetHostWithPort(const nsACString & aHostName, int32_t aPort, nsACString& _retval);
|
2008-04-22 20:03:20 +00:00
|
|
|
|
2007-10-03 11:43:54 +00:00
|
|
|
protected:
|
2014-06-23 22:40:03 +00:00
|
|
|
~nsCertOverrideService();
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
mozilla::ReentrantMonitor monitor;
|
2007-10-03 11:43:54 +00:00
|
|
|
nsCOMPtr<nsIFile> mSettingsFile;
|
|
|
|
nsTHashtable<nsCertOverrideEntry> mSettingsTable;
|
|
|
|
|
|
|
|
SECOidTag mOidTagForStoringNewHashes;
|
|
|
|
nsCString mDottedOidForStoringNewHashes;
|
|
|
|
|
2014-10-29 23:25:16 +00:00
|
|
|
void CountPermanentOverrideTelemetry();
|
|
|
|
|
2007-10-03 11:43:54 +00:00
|
|
|
void RemoveAllFromMemory();
|
|
|
|
nsresult Read();
|
|
|
|
nsresult Write();
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult AddEntryToList(const nsACString &host, int32_t port,
|
2008-10-23 02:16:06 +00:00
|
|
|
nsIX509Cert *aCert,
|
2011-09-29 06:19:26 +00:00
|
|
|
const bool aIsTemporary,
|
2007-10-03 11:43:54 +00:00
|
|
|
const nsACString &algo_oid,
|
|
|
|
const nsACString &fingerprint,
|
|
|
|
nsCertOverride::OverrideBits ob,
|
|
|
|
const nsACString &dbKey);
|
|
|
|
};
|
|
|
|
|
|
|
|
#define NS_CERTOVERRIDE_CID { /* 67ba681d-5485-4fff-952c-2ee337ffdcd6 */ \
|
|
|
|
0x67ba681d, \
|
|
|
|
0x5485, \
|
|
|
|
0x4fff, \
|
|
|
|
{0x95, 0x2c, 0x2e, 0xe3, 0x37, 0xff, 0xdc, 0xd6} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|