nsNSSComponent.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #ifndef _nsNSSComponent_h_
  7. #define _nsNSSComponent_h_
  8. #include "ScopedNSSTypes.h"
  9. #include "SharedCertVerifier.h"
  10. #include "mozilla/Mutex.h"
  11. #include "mozilla/RefPtr.h"
  12. #include "nsCOMPtr.h"
  13. #include "nsIObserver.h"
  14. #include "nsIStringBundle.h"
  15. #include "nsNSSCallbacks.h"
  16. #include "prerror.h"
  17. #include "sslt.h"
  18. #ifdef XP_WIN
  19. #include "windows.h" // this needs to be before the following includes
  20. #include "wincrypt.h"
  21. #endif // XP_WIN
  22. class nsIDOMWindow;
  23. class nsIPrompt;
  24. class nsIX509CertList;
  25. class SmartCardThreadList;
  26. namespace mozilla { namespace psm {
  27. MOZ_MUST_USE
  28. ::already_AddRefed<mozilla::psm::SharedCertVerifier>
  29. GetDefaultCertVerifier();
  30. } } // namespace mozilla::psm
  31. #define NS_NSSCOMPONENT_CID \
  32. {0x4cb64dfd, 0xca98, 0x4e24, {0xbe, 0xfd, 0x0d, 0x92, 0x85, 0xa3, 0x3b, 0xcb}}
  33. #define PSM_COMPONENT_CONTRACTID "@mozilla.org/psm;1"
  34. #define NS_INSSCOMPONENT_IID \
  35. { 0xa0a8f52b, 0xea18, 0x4abc, \
  36. { 0xa3, 0xca, 0xec, 0xcf, 0x70, 0x4f, 0xfe, 0x63 } }
  37. enum EnsureNSSOperator
  38. {
  39. nssLoadingComponent = 0,
  40. nssInitSucceeded = 1,
  41. nssInitFailed = 2,
  42. nssShutdown = 3,
  43. nssEnsure = 100,
  44. nssEnsureOnChromeOnly = 101,
  45. nssEnsureChromeOrContent = 102,
  46. };
  47. extern bool EnsureNSSInitializedChromeOrContent();
  48. extern bool EnsureNSSInitialized(EnsureNSSOperator op);
  49. class NS_NO_VTABLE nsINSSComponent : public nsISupports
  50. {
  51. public:
  52. NS_DECLARE_STATIC_IID_ACCESSOR(NS_INSSCOMPONENT_IID)
  53. NS_IMETHOD ShowAlertFromStringBundle(const char* messageID) = 0;
  54. NS_IMETHOD GetPIPNSSBundleString(const char* name,
  55. nsAString& outString) = 0;
  56. NS_IMETHOD PIPBundleFormatStringFromName(const char* name,
  57. const char16_t** params,
  58. uint32_t numParams,
  59. nsAString& outString) = 0;
  60. NS_IMETHOD GetNSSBundleString(const char* name,
  61. nsAString& outString) = 0;
  62. NS_IMETHOD LogoutAuthenticatedPK11() = 0;
  63. #ifndef MOZ_NO_SMART_CARDS
  64. NS_IMETHOD LaunchSmartCardThread(SECMODModule* module) = 0;
  65. NS_IMETHOD ShutdownSmartCardThread(SECMODModule* module) = 0;
  66. #endif
  67. NS_IMETHOD IsNSSInitialized(bool* initialized) = 0;
  68. #ifdef DEBUG
  69. NS_IMETHOD IsCertTestBuiltInRoot(CERTCertificate* cert, bool& result) = 0;
  70. #endif
  71. NS_IMETHOD IsCertContentSigningRoot(CERTCertificate* cert, bool& result) = 0;
  72. #ifdef XP_WIN
  73. NS_IMETHOD GetEnterpriseRoots(nsIX509CertList** enterpriseRoots) = 0;
  74. #endif
  75. virtual ::already_AddRefed<mozilla::psm::SharedCertVerifier>
  76. GetDefaultCertVerifier() = 0;
  77. };
  78. NS_DEFINE_STATIC_IID_ACCESSOR(nsINSSComponent, NS_INSSCOMPONENT_IID)
  79. class nsNSSShutDownList;
  80. // Implementation of the PSM component interface.
  81. class nsNSSComponent final : public nsINSSComponent
  82. , public nsIObserver
  83. {
  84. public:
  85. NS_DEFINE_STATIC_CID_ACCESSOR( NS_NSSCOMPONENT_CID )
  86. nsNSSComponent();
  87. NS_DECL_THREADSAFE_ISUPPORTS
  88. NS_DECL_NSIOBSERVER
  89. nsresult Init();
  90. static nsresult GetNewPrompter(nsIPrompt** result);
  91. static nsresult ShowAlertWithConstructedString(const nsString& message);
  92. NS_IMETHOD ShowAlertFromStringBundle(const char* messageID) override;
  93. NS_IMETHOD GetPIPNSSBundleString(const char* name,
  94. nsAString& outString) override;
  95. NS_IMETHOD PIPBundleFormatStringFromName(const char* name,
  96. const char16_t** params,
  97. uint32_t numParams,
  98. nsAString& outString) override;
  99. NS_IMETHOD GetNSSBundleString(const char* name, nsAString& outString) override;
  100. NS_IMETHOD LogoutAuthenticatedPK11() override;
  101. #ifndef MOZ_NO_SMART_CARDS
  102. NS_IMETHOD LaunchSmartCardThread(SECMODModule* module) override;
  103. NS_IMETHOD ShutdownSmartCardThread(SECMODModule* module) override;
  104. void LaunchSmartCardThreads();
  105. void ShutdownSmartCardThreads();
  106. nsresult DispatchEventToWindow(nsIDOMWindow* domWin,
  107. const nsAString& eventType,
  108. const nsAString& token);
  109. #endif
  110. NS_IMETHOD IsNSSInitialized(bool* initialized) override;
  111. #ifdef DEBUG
  112. NS_IMETHOD IsCertTestBuiltInRoot(CERTCertificate* cert, bool& result) override;
  113. #endif
  114. NS_IMETHOD IsCertContentSigningRoot(CERTCertificate* cert, bool& result) override;
  115. #ifdef XP_WIN
  116. NS_IMETHOD GetEnterpriseRoots(nsIX509CertList** enterpriseRoots) override;
  117. #endif
  118. ::already_AddRefed<mozilla::psm::SharedCertVerifier>
  119. GetDefaultCertVerifier() override;
  120. // The following two methods are thread-safe.
  121. static bool AreAnyWeakCiphersEnabled();
  122. static void UseWeakCiphersOnSocket(PRFileDesc* fd);
  123. static void FillTLSVersionRange(SSLVersionRange& rangeOut,
  124. uint32_t minFromPrefs,
  125. uint32_t maxFromPrefs,
  126. SSLVersionRange defaults);
  127. protected:
  128. virtual ~nsNSSComponent();
  129. private:
  130. nsresult InitializeNSS();
  131. void ShutdownNSS();
  132. void LoadLoadableRoots();
  133. void UnloadLoadableRoots();
  134. void setValidationOptions(bool isInitialSetting,
  135. const mozilla::MutexAutoLock& lock);
  136. nsresult setEnabledTLSVersions();
  137. nsresult InitializePIPNSSBundle();
  138. nsresult ConfigureInternalPKCS11Token();
  139. nsresult RegisterObservers();
  140. void DoProfileBeforeChange();
  141. void MaybeEnableFamilySafetyCompatibility();
  142. void MaybeImportEnterpriseRoots();
  143. #ifdef XP_WIN
  144. void ImportEnterpriseRootsForLocation(DWORD locationFlag);
  145. nsresult MaybeImportFamilySafetyRoot(PCCERT_CONTEXT certificate,
  146. bool& wasFamilySafetyRoot);
  147. nsresult LoadFamilySafetyRoot();
  148. void UnloadFamilySafetyRoot();
  149. void UnloadEnterpriseRoots();
  150. mozilla::UniqueCERTCertificate mFamilySafetyRoot;
  151. mozilla::UniqueCERTCertList mEnterpriseRoots;
  152. #endif // XP_WIN
  153. mozilla::Mutex mutex;
  154. nsCOMPtr<nsIStringBundle> mPIPNSSBundle;
  155. nsCOMPtr<nsIStringBundle> mNSSErrorsBundle;
  156. bool mNSSInitialized;
  157. static int mInstanceCount;
  158. #ifndef MOZ_NO_SMART_CARDS
  159. SmartCardThreadList* mThreadList;
  160. #endif
  161. #ifdef DEBUG
  162. nsString mTestBuiltInRootHash;
  163. #endif
  164. nsString mContentSigningRootHash;
  165. nsNSSHttpInterface mHttpForNSS;
  166. RefPtr<mozilla::psm::SharedCertVerifier> mDefaultCertVerifier;
  167. static PRStatus IdentityInfoInit(void);
  168. };
  169. class nsNSSErrors
  170. {
  171. public:
  172. static const char* getDefaultErrorStringName(PRErrorCode err);
  173. static const char* getOverrideErrorStringName(PRErrorCode aErrorCode);
  174. static nsresult getErrorMessageFromCode(PRErrorCode err,
  175. nsINSSComponent* component,
  176. nsString& returnedMessage);
  177. };
  178. #endif // _nsNSSComponent_h_