security/manager/ssl/src/nsCertOverrideService.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef __NSCERTOVERRIDESERVICE_H__
michael@0 8 #define __NSCERTOVERRIDESERVICE_H__
michael@0 9
michael@0 10 #include "mozilla/ReentrantMonitor.h"
michael@0 11 #include "nsICertOverrideService.h"
michael@0 12 #include "nsTHashtable.h"
michael@0 13 #include "nsIObserver.h"
michael@0 14 #include "nsString.h"
michael@0 15 #include "nsIFile.h"
michael@0 16 #include "secoidt.h"
michael@0 17 #include "nsWeakReference.h"
michael@0 18 #include "mozilla/Attributes.h"
michael@0 19
michael@0 20 class nsCertOverride
michael@0 21 {
michael@0 22 public:
michael@0 23
michael@0 24 enum OverrideBits { ob_None=0, ob_Untrusted=1, ob_Mismatch=2,
michael@0 25 ob_Time_error=4 };
michael@0 26
michael@0 27 nsCertOverride()
michael@0 28 :mPort(-1)
michael@0 29 ,mOverrideBits(ob_None)
michael@0 30 {
michael@0 31 }
michael@0 32
michael@0 33 nsCertOverride(const nsCertOverride &other)
michael@0 34 {
michael@0 35 this->operator=(other);
michael@0 36 }
michael@0 37
michael@0 38 nsCertOverride &operator=(const nsCertOverride &other)
michael@0 39 {
michael@0 40 mAsciiHost = other.mAsciiHost;
michael@0 41 mPort = other.mPort;
michael@0 42 mIsTemporary = other.mIsTemporary;
michael@0 43 mFingerprintAlgOID = other.mFingerprintAlgOID;
michael@0 44 mFingerprint = other.mFingerprint;
michael@0 45 mOverrideBits = other.mOverrideBits;
michael@0 46 mDBKey = other.mDBKey;
michael@0 47 mCert = other.mCert;
michael@0 48 return *this;
michael@0 49 }
michael@0 50
michael@0 51 nsCString mAsciiHost;
michael@0 52 int32_t mPort;
michael@0 53 bool mIsTemporary; // true: session only, false: stored on disk
michael@0 54 nsCString mFingerprint;
michael@0 55 nsCString mFingerprintAlgOID;
michael@0 56 OverrideBits mOverrideBits;
michael@0 57 nsCString mDBKey;
michael@0 58 nsCOMPtr <nsIX509Cert> mCert;
michael@0 59
michael@0 60 static void convertBitsToString(OverrideBits ob, nsACString &str);
michael@0 61 static void convertStringToBits(const nsACString &str, OverrideBits &ob);
michael@0 62 };
michael@0 63
michael@0 64
michael@0 65 // hash entry class
michael@0 66 class nsCertOverrideEntry MOZ_FINAL : public PLDHashEntryHdr
michael@0 67 {
michael@0 68 public:
michael@0 69 // Hash methods
michael@0 70 typedef const char* KeyType;
michael@0 71 typedef const char* KeyTypePointer;
michael@0 72
michael@0 73 // do nothing with aHost - we require mHead to be set before we're live!
michael@0 74 nsCertOverrideEntry(KeyTypePointer aHostWithPortUTF8)
michael@0 75 {
michael@0 76 }
michael@0 77
michael@0 78 nsCertOverrideEntry(const nsCertOverrideEntry& toCopy)
michael@0 79 {
michael@0 80 mSettings = toCopy.mSettings;
michael@0 81 mHostWithPort = toCopy.mHostWithPort;
michael@0 82 }
michael@0 83
michael@0 84 ~nsCertOverrideEntry()
michael@0 85 {
michael@0 86 }
michael@0 87
michael@0 88 KeyType GetKey() const
michael@0 89 {
michael@0 90 return HostWithPortPtr();
michael@0 91 }
michael@0 92
michael@0 93 KeyTypePointer GetKeyPointer() const
michael@0 94 {
michael@0 95 return HostWithPortPtr();
michael@0 96 }
michael@0 97
michael@0 98 bool KeyEquals(KeyTypePointer aKey) const
michael@0 99 {
michael@0 100 return !strcmp(HostWithPortPtr(), aKey);
michael@0 101 }
michael@0 102
michael@0 103 static KeyTypePointer KeyToPointer(KeyType aKey)
michael@0 104 {
michael@0 105 return aKey;
michael@0 106 }
michael@0 107
michael@0 108 static PLDHashNumber HashKey(KeyTypePointer aKey)
michael@0 109 {
michael@0 110 // PL_DHashStringKey doesn't use the table parameter, so we can safely
michael@0 111 // pass nullptr
michael@0 112 return PL_DHashStringKey(nullptr, aKey);
michael@0 113 }
michael@0 114
michael@0 115 enum { ALLOW_MEMMOVE = false };
michael@0 116
michael@0 117 // get methods
michael@0 118 inline const nsCString &HostWithPort() const { return mHostWithPort; }
michael@0 119
michael@0 120 inline KeyTypePointer HostWithPortPtr() const
michael@0 121 {
michael@0 122 return mHostWithPort.get();
michael@0 123 }
michael@0 124
michael@0 125 nsCertOverride mSettings;
michael@0 126 nsCString mHostWithPort;
michael@0 127 };
michael@0 128
michael@0 129 class nsCertOverrideService MOZ_FINAL : public nsICertOverrideService
michael@0 130 , public nsIObserver
michael@0 131 , public nsSupportsWeakReference
michael@0 132 {
michael@0 133 public:
michael@0 134 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 135 NS_DECL_NSICERTOVERRIDESERVICE
michael@0 136 NS_DECL_NSIOBSERVER
michael@0 137
michael@0 138 nsCertOverrideService();
michael@0 139 ~nsCertOverrideService();
michael@0 140
michael@0 141 nsresult Init();
michael@0 142 void RemoveAllTemporaryOverrides();
michael@0 143
michael@0 144 typedef void
michael@0 145 (*CertOverrideEnumerator)(const nsCertOverride &aSettings,
michael@0 146 void *aUserData);
michael@0 147
michael@0 148 // aCert == null: return all overrides
michael@0 149 // aCert != null: return overrides that match the given cert
michael@0 150 nsresult EnumerateCertOverrides(nsIX509Cert *aCert,
michael@0 151 CertOverrideEnumerator enumerator,
michael@0 152 void *aUserData);
michael@0 153
michael@0 154 // Concates host name and the port number. If the port number is -1 then
michael@0 155 // port 443 is automatically used. This method ensures there is always a port
michael@0 156 // number separated with colon.
michael@0 157 static void GetHostWithPort(const nsACString & aHostName, int32_t aPort, nsACString& _retval);
michael@0 158
michael@0 159 protected:
michael@0 160 mozilla::ReentrantMonitor monitor;
michael@0 161 nsCOMPtr<nsIFile> mSettingsFile;
michael@0 162 nsTHashtable<nsCertOverrideEntry> mSettingsTable;
michael@0 163
michael@0 164 SECOidTag mOidTagForStoringNewHashes;
michael@0 165 nsCString mDottedOidForStoringNewHashes;
michael@0 166
michael@0 167 void RemoveAllFromMemory();
michael@0 168 nsresult Read();
michael@0 169 nsresult Write();
michael@0 170 nsresult AddEntryToList(const nsACString &host, int32_t port,
michael@0 171 nsIX509Cert *aCert,
michael@0 172 const bool aIsTemporary,
michael@0 173 const nsACString &algo_oid,
michael@0 174 const nsACString &fingerprint,
michael@0 175 nsCertOverride::OverrideBits ob,
michael@0 176 const nsACString &dbKey);
michael@0 177 };
michael@0 178
michael@0 179 #define NS_CERTOVERRIDE_CID { /* 67ba681d-5485-4fff-952c-2ee337ffdcd6 */ \
michael@0 180 0x67ba681d, \
michael@0 181 0x5485, \
michael@0 182 0x4fff, \
michael@0 183 {0x95, 0x2c, 0x2e, 0xe3, 0x37, 0xff, 0xdc, 0xd6} \
michael@0 184 }
michael@0 185
michael@0 186 #endif

mercurial