|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef __NSCLIENTAUTHREMEMBER_H__ |
|
8 #define __NSCLIENTAUTHREMEMBER_H__ |
|
9 |
|
10 #include "mozilla/ReentrantMonitor.h" |
|
11 #include "nsTHashtable.h" |
|
12 #include "nsIObserver.h" |
|
13 #include "nsIX509Cert.h" |
|
14 #include "nsNSSCertificate.h" |
|
15 #include "nsString.h" |
|
16 #include "nsWeakReference.h" |
|
17 #include "mozilla/Attributes.h" |
|
18 |
|
19 class nsClientAuthRemember |
|
20 { |
|
21 public: |
|
22 |
|
23 nsClientAuthRemember() |
|
24 { |
|
25 } |
|
26 |
|
27 nsClientAuthRemember(const nsClientAuthRemember &other) |
|
28 { |
|
29 this->operator=(other); |
|
30 } |
|
31 |
|
32 nsClientAuthRemember &operator=(const nsClientAuthRemember &other) |
|
33 { |
|
34 mAsciiHost = other.mAsciiHost; |
|
35 mFingerprint = other.mFingerprint; |
|
36 mDBKey = other.mDBKey; |
|
37 return *this; |
|
38 } |
|
39 |
|
40 nsCString mAsciiHost; |
|
41 nsCString mFingerprint; |
|
42 nsCString mDBKey; |
|
43 }; |
|
44 |
|
45 |
|
46 // hash entry class |
|
47 class nsClientAuthRememberEntry MOZ_FINAL : public PLDHashEntryHdr |
|
48 { |
|
49 public: |
|
50 // Hash methods |
|
51 typedef const char* KeyType; |
|
52 typedef const char* KeyTypePointer; |
|
53 |
|
54 // do nothing with aHost - we require mHead to be set before we're live! |
|
55 nsClientAuthRememberEntry(KeyTypePointer aHostWithCertUTF8) |
|
56 { |
|
57 } |
|
58 |
|
59 nsClientAuthRememberEntry(const nsClientAuthRememberEntry& toCopy) |
|
60 { |
|
61 mSettings = toCopy.mSettings; |
|
62 } |
|
63 |
|
64 ~nsClientAuthRememberEntry() |
|
65 { |
|
66 } |
|
67 |
|
68 KeyType GetKey() const |
|
69 { |
|
70 return HostWithCertPtr(); |
|
71 } |
|
72 |
|
73 KeyTypePointer GetKeyPointer() const |
|
74 { |
|
75 return HostWithCertPtr(); |
|
76 } |
|
77 |
|
78 bool KeyEquals(KeyTypePointer aKey) const |
|
79 { |
|
80 return !strcmp(HostWithCertPtr(), aKey); |
|
81 } |
|
82 |
|
83 static KeyTypePointer KeyToPointer(KeyType aKey) |
|
84 { |
|
85 return aKey; |
|
86 } |
|
87 |
|
88 static PLDHashNumber HashKey(KeyTypePointer aKey) |
|
89 { |
|
90 // PL_DHashStringKey doesn't use the table parameter, so we can safely |
|
91 // pass nullptr |
|
92 return PL_DHashStringKey(nullptr, aKey); |
|
93 } |
|
94 |
|
95 enum { ALLOW_MEMMOVE = false }; |
|
96 |
|
97 // get methods |
|
98 inline const nsCString &HostWithCert() const { return mHostWithCert; } |
|
99 |
|
100 inline KeyTypePointer HostWithCertPtr() const |
|
101 { |
|
102 return mHostWithCert.get(); |
|
103 } |
|
104 |
|
105 nsClientAuthRemember mSettings; |
|
106 nsCString mHostWithCert; |
|
107 }; |
|
108 |
|
109 class nsClientAuthRememberService MOZ_FINAL : public nsIObserver, |
|
110 public nsSupportsWeakReference |
|
111 { |
|
112 public: |
|
113 NS_DECL_THREADSAFE_ISUPPORTS |
|
114 NS_DECL_NSIOBSERVER |
|
115 |
|
116 nsClientAuthRememberService(); |
|
117 ~nsClientAuthRememberService(); |
|
118 |
|
119 nsresult Init(); |
|
120 |
|
121 static void GetHostWithCert(const nsACString & aHostName, |
|
122 const nsACString & nickname, nsACString& _retval); |
|
123 |
|
124 nsresult RememberDecision(const nsACString & aHostName, |
|
125 CERTCertificate *aServerCert, CERTCertificate *aClientCert); |
|
126 nsresult HasRememberedDecision(const nsACString & aHostName, |
|
127 CERTCertificate *aServerCert, |
|
128 nsACString & aCertDBKey, bool *_retval); |
|
129 |
|
130 void ClearRememberedDecisions(); |
|
131 static void ClearAllRememberedDecisions(); |
|
132 |
|
133 protected: |
|
134 mozilla::ReentrantMonitor monitor; |
|
135 nsTHashtable<nsClientAuthRememberEntry> mSettingsTable; |
|
136 |
|
137 void RemoveAllFromMemory(); |
|
138 nsresult AddEntryToList(const nsACString &host, |
|
139 const nsACString &server_fingerprint, |
|
140 const nsACString &db_key); |
|
141 }; |
|
142 |
|
143 #endif |