/security/manager/ssl/src/nsNSSIOLayer.h

http://github.com/zpao/v8monkey · C Header · 328 lines · 226 code · 56 blank · 46 comment · 1 complexity · 03d42ad379235133056655626dabab66 MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. *
  3. * ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is mozilla.org code.
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Netscape Communications Corporation.
  20. * Portions created by the Initial Developer are Copyright (C) 1998
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. * Brian Ryner <bryner@brianryner.com>
  25. * Kai Engert <kengert@redhat.com>
  26. *
  27. * Alternatively, the contents of this file may be used under the terms of
  28. * either the GNU General Public License Version 2 or later (the "GPL"), or
  29. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30. * in which case the provisions of the GPL or the LGPL are applicable instead
  31. * of those above. If you wish to allow use of your version of this file only
  32. * under the terms of either the GPL or the LGPL, and not to allow others to
  33. * use your version of this file under the terms of the MPL, indicate your
  34. * decision by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL or the LGPL. If you do not delete
  36. * the provisions above, a recipient may use your version of this file under
  37. * the terms of any one of the MPL, the GPL or the LGPL.
  38. *
  39. * ***** END LICENSE BLOCK ***** */
  40. #ifndef _NSNSSIOLAYER_H
  41. #define _NSNSSIOLAYER_H
  42. #include "prtypes.h"
  43. #include "prio.h"
  44. #include "certt.h"
  45. #include "mozilla/Mutex.h"
  46. #include "nsString.h"
  47. #include "nsIInterfaceRequestor.h"
  48. #include "nsIInterfaceRequestorUtils.h"
  49. #include "nsITransportSecurityInfo.h"
  50. #include "nsISSLSocketControl.h"
  51. #include "nsSSLStatus.h"
  52. #include "nsISSLStatusProvider.h"
  53. #include "nsIAssociatedContentSecurity.h"
  54. #include "nsXPIDLString.h"
  55. #include "nsNSSShutDown.h"
  56. #include "nsIClientAuthDialogs.h"
  57. #include "nsAutoPtr.h"
  58. #include "nsNSSCertificate.h"
  59. #include "nsDataHashtable.h"
  60. namespace mozilla {
  61. class MutexAutoLock;
  62. namespace psm {
  63. enum SSLErrorMessageType {
  64. OverridableCertErrorMessage = 1, // for *overridable* certificate errors
  65. PlainErrorMessage = 2 // all other errors (or "no error")
  66. };
  67. } // namespace psm
  68. } // namespace mozilla
  69. class nsNSSSocketInfo : public nsITransportSecurityInfo,
  70. public nsISSLSocketControl,
  71. public nsIInterfaceRequestor,
  72. public nsISSLStatusProvider,
  73. public nsIAssociatedContentSecurity,
  74. public nsISerializable,
  75. public nsIClassInfo,
  76. public nsIClientAuthUserDecision,
  77. public nsNSSShutDownObject,
  78. public nsOnPK11LogoutCancelObject
  79. {
  80. public:
  81. nsNSSSocketInfo();
  82. virtual ~nsNSSSocketInfo();
  83. NS_DECL_ISUPPORTS
  84. NS_DECL_NSITRANSPORTSECURITYINFO
  85. NS_DECL_NSISSLSOCKETCONTROL
  86. NS_DECL_NSIINTERFACEREQUESTOR
  87. NS_DECL_NSISSLSTATUSPROVIDER
  88. NS_DECL_NSIASSOCIATEDCONTENTSECURITY
  89. NS_DECL_NSISERIALIZABLE
  90. NS_DECL_NSICLASSINFO
  91. NS_DECL_NSICLIENTAUTHUSERDECISION
  92. nsresult SetSecurityState(PRUint32 aState);
  93. nsresult SetShortSecurityDescription(const PRUnichar *aText);
  94. nsresult SetForSTARTTLS(bool aForSTARTTLS);
  95. nsresult GetForSTARTTLS(bool *aForSTARTTLS);
  96. nsresult GetFileDescPtr(PRFileDesc** aFilePtr);
  97. nsresult SetFileDescPtr(PRFileDesc* aFilePtr);
  98. nsresult GetHandshakePending(bool *aHandshakePending);
  99. nsresult SetHandshakePending(bool aHandshakePending);
  100. const char * GetHostName() const {
  101. return mHostName.get();
  102. }
  103. nsresult GetHostName(char **aHostName);
  104. nsresult SetHostName(const char *aHostName);
  105. nsresult GetPort(PRInt32 *aPort);
  106. nsresult SetPort(PRInt32 aPort);
  107. void GetPreviousCert(nsIX509Cert** _result);
  108. PRErrorCode GetErrorCode() const;
  109. void SetCanceled(PRErrorCode errorCode,
  110. ::mozilla::psm::SSLErrorMessageType errorMessageType);
  111. void SetHasCleartextPhase(bool aHasCleartextPhase);
  112. bool GetHasCleartextPhase();
  113. void SetHandshakeInProgress(bool aIsIn);
  114. bool GetHandshakeInProgress() { return mHandshakeInProgress; }
  115. bool HandshakeTimeout();
  116. void SetAllowTLSIntoleranceTimeout(bool aAllow);
  117. nsresult RememberCAChain(CERTCertList *aCertList);
  118. /* Set SSL Status values */
  119. nsresult SetSSLStatus(nsSSLStatus *aSSLStatus);
  120. nsSSLStatus* SSLStatus() { return mSSLStatus; }
  121. void SetStatusErrorBits(nsIX509Cert & cert, PRUint32 collected_errors);
  122. PRStatus CloseSocketAndDestroy(
  123. const nsNSSShutDownPreventionLock & proofOfLock);
  124. bool IsCertIssuerBlacklisted() const {
  125. return mIsCertIssuerBlacklisted;
  126. }
  127. void SetCertIssuerBlacklisted() {
  128. mIsCertIssuerBlacklisted = true;
  129. }
  130. void SetNegotiatedNPN(const char *value, PRUint32 length);
  131. void SetHandshakeCompleted() { mHandshakeCompleted = true; }
  132. bool GetJoined() { return mJoined; }
  133. void SetSentClientCert() { mSentClientCert = true; }
  134. // XXX: These are only used on for diagnostic purposes
  135. enum CertVerificationState {
  136. before_cert_verification,
  137. waiting_for_cert_verification,
  138. after_cert_verification
  139. };
  140. void SetCertVerificationWaiting();
  141. // Use errorCode == 0 to indicate success; in that case, errorMessageType is
  142. // ignored.
  143. void SetCertVerificationResult(PRErrorCode errorCode,
  144. ::mozilla::psm::SSLErrorMessageType errorMessageType);
  145. // for logging only
  146. PRBool IsWaitingForCertVerification() const
  147. {
  148. return mCertVerificationState == waiting_for_cert_verification;
  149. }
  150. bool IsSSL3Enabled() const { return mSSL3Enabled; }
  151. void SetSSL3Enabled(bool enabled) { mSSL3Enabled = enabled; }
  152. bool IsTLSEnabled() const { return mTLSEnabled; }
  153. void SetTLSEnabled(bool enabled) { mTLSEnabled = enabled; }
  154. protected:
  155. mutable ::mozilla::Mutex mMutex;
  156. nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
  157. PRFileDesc* mFd;
  158. CertVerificationState mCertVerificationState;
  159. PRIntervalTime mCertVerificationStarted;
  160. PRIntervalTime mCertVerificationEnded;
  161. PRUint32 mSecurityState;
  162. PRInt32 mSubRequestsHighSecurity;
  163. PRInt32 mSubRequestsLowSecurity;
  164. PRInt32 mSubRequestsBrokenSecurity;
  165. PRInt32 mSubRequestsNoSecurity;
  166. nsString mShortDesc;
  167. PRErrorCode mErrorCode;
  168. ::mozilla::psm::SSLErrorMessageType mErrorMessageType;
  169. nsString mErrorMessageCached;
  170. nsresult formatErrorMessage(::mozilla::MutexAutoLock const & proofOfLock);
  171. bool mDocShellDependentStuffKnown;
  172. bool mExternalErrorReporting; // DocShellDependent
  173. bool mForSTARTTLS;
  174. bool mSSL3Enabled;
  175. bool mTLSEnabled;
  176. bool mHandshakePending;
  177. bool mHasCleartextPhase;
  178. bool mHandshakeInProgress;
  179. bool mAllowTLSIntoleranceTimeout;
  180. bool mRememberClientAuthCertificate;
  181. PRIntervalTime mHandshakeStartTime;
  182. PRInt32 mPort;
  183. nsXPIDLCString mHostName;
  184. PRErrorCode mIsCertIssuerBlacklisted;
  185. /* SSL Status */
  186. nsRefPtr<nsSSLStatus> mSSLStatus;
  187. nsresult ActivateSSL();
  188. nsCString mNegotiatedNPN;
  189. bool mNPNCompleted;
  190. bool mHandshakeCompleted;
  191. bool mJoined;
  192. bool mSentClientCert;
  193. private:
  194. virtual void virtualDestroyNSSReference();
  195. void destructorSafeDestroyNSSReference();
  196. };
  197. class nsCStringHashSet;
  198. class nsSSLStatus;
  199. class nsNSSSocketInfo;
  200. class nsPSMRememberCertErrorsTable
  201. {
  202. private:
  203. struct CertStateBits
  204. {
  205. bool mIsDomainMismatch;
  206. bool mIsNotValidAtThisTime;
  207. bool mIsUntrusted;
  208. };
  209. nsDataHashtableMT<nsCStringHashKey, CertStateBits> mErrorHosts;
  210. nsresult GetHostPortKey(nsNSSSocketInfo* infoObject, nsCAutoString& result);
  211. public:
  212. friend class nsSSLIOLayerHelpers;
  213. nsPSMRememberCertErrorsTable();
  214. void RememberCertHasError(nsNSSSocketInfo* infoObject,
  215. nsSSLStatus* status,
  216. SECStatus certVerificationResult);
  217. void LookupCertErrorBits(nsNSSSocketInfo* infoObject,
  218. nsSSLStatus* status);
  219. };
  220. class nsSSLIOLayerHelpers
  221. {
  222. public:
  223. static nsresult Init();
  224. static void Cleanup();
  225. static bool nsSSLIOLayerInitialized;
  226. static PRDescIdentity nsSSLIOLayerIdentity;
  227. static PRIOMethods nsSSLIOLayerMethods;
  228. static mozilla::Mutex *mutex;
  229. static nsCStringHashSet *mTLSIntolerantSites;
  230. static nsCStringHashSet *mTLSTolerantSites;
  231. static nsPSMRememberCertErrorsTable* mHostsWithCertErrors;
  232. static nsCStringHashSet *mRenegoUnrestrictedSites;
  233. static bool mTreatUnsafeNegotiationAsBroken;
  234. static PRInt32 mWarnLevelMissingRFC5746;
  235. static void setTreatUnsafeNegotiationAsBroken(bool broken);
  236. static bool treatUnsafeNegotiationAsBroken();
  237. static void setWarnLevelMissingRFC5746(PRInt32 level);
  238. static PRInt32 getWarnLevelMissingRFC5746();
  239. static void getSiteKey(nsNSSSocketInfo *socketInfo, nsCSubstring &key);
  240. static bool rememberPossibleTLSProblemSite(nsNSSSocketInfo *socketInfo);
  241. static void rememberTolerantSite(nsNSSSocketInfo *socketInfo);
  242. static void addIntolerantSite(const nsCString &str);
  243. static void removeIntolerantSite(const nsCString &str);
  244. static bool isKnownAsIntolerantSite(const nsCString &str);
  245. static void setRenegoUnrestrictedSites(const nsCString &str);
  246. static bool isRenegoUnrestrictedSite(const nsCString &str);
  247. };
  248. nsresult nsSSLIOLayerNewSocket(PRInt32 family,
  249. const char *host,
  250. PRInt32 port,
  251. const char *proxyHost,
  252. PRInt32 proxyPort,
  253. PRFileDesc **fd,
  254. nsISupports **securityInfo,
  255. bool forSTARTTLS,
  256. bool anonymousLoad);
  257. nsresult nsSSLIOLayerAddToSocket(PRInt32 family,
  258. const char *host,
  259. PRInt32 port,
  260. const char *proxyHost,
  261. PRInt32 proxyPort,
  262. PRFileDesc *fd,
  263. nsISupports **securityInfo,
  264. bool forSTARTTLS,
  265. bool anonymousLoad);
  266. nsresult nsSSLIOLayerFreeTLSIntolerantSites();
  267. nsresult displayUnknownCertErrorAlert(nsNSSSocketInfo *infoObject, int error);
  268. // 16786594-0296-4471-8096-8f84497ca428
  269. #define NS_NSSSOCKETINFO_CID \
  270. { 0x16786594, 0x0296, 0x4471, \
  271. { 0x80, 0x96, 0x8f, 0x84, 0x49, 0x7c, 0xa4, 0x28 } }
  272. #endif /* _NSNSSIOLAYER_H */