nsSiteSecurityService.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef __nsSiteSecurityService_h__
  5. #define __nsSiteSecurityService_h__
  6. #include "mozilla/DataStorage.h"
  7. #include "nsCOMPtr.h"
  8. #include "nsIObserver.h"
  9. #include "nsISiteSecurityService.h"
  10. #include "nsString.h"
  11. #include "nsTArray.h"
  12. #include "pkix/pkixtypes.h"
  13. #include "prtime.h"
  14. class nsIURI;
  15. class nsISSLStatus;
  16. // 91ea3803-9c79-45d9-97bf-88bc80269236
  17. #define NS_SITE_SECURITY_SERVICE_CID \
  18. { 0x91ea3803, 0x9c79, 0x45d9, \
  19. { 0x97, 0xbf, 0x88, 0xbc, 0x80, 0x26, 0x92, 0x36 } }
  20. /**
  21. * SecurityPropertyState: A utility enum for representing the different states
  22. * a security property can be in.
  23. * SecurityPropertySet and SecurityPropertyUnset correspond to indicating
  24. * a site has or does not have the security property in question, respectively.
  25. * SecurityPropertyKnockout indicates a value on a preloaded list is being
  26. * overridden, and the associated site does not have the security property
  27. * in question.
  28. */
  29. enum SecurityPropertyState {
  30. SecurityPropertyUnset = 0,
  31. SecurityPropertySet = 1,
  32. SecurityPropertyKnockout = 2,
  33. SecurityPropertyNegative = 3,
  34. };
  35. /**
  36. * SiteHSTSState: A utility class that encodes/decodes a string describing
  37. * the security state of a site. Currently only handles HSTS.
  38. * HSTS state consists of:
  39. * - Expiry time (PRTime (aka int64_t) in milliseconds)
  40. * - A state flag (SecurityPropertyState, default SecurityPropertyUnset)
  41. * - An include subdomains flag (bool, default false)
  42. */
  43. class SiteHSTSState
  44. {
  45. public:
  46. explicit SiteHSTSState(nsCString& aStateString);
  47. SiteHSTSState(PRTime aHSTSExpireTime, SecurityPropertyState aHSTSState,
  48. bool aHSTSIncludeSubdomains);
  49. PRTime mHSTSExpireTime;
  50. SecurityPropertyState mHSTSState;
  51. bool mHSTSIncludeSubdomains;
  52. bool IsExpired(uint32_t aType)
  53. {
  54. // If mHSTSExpireTime is 0, this entry never expires (this is the case for
  55. // knockout entries).
  56. if (mHSTSExpireTime == 0) {
  57. return false;
  58. }
  59. PRTime now = PR_Now() / PR_USEC_PER_MSEC;
  60. if (now > mHSTSExpireTime) {
  61. return true;
  62. }
  63. return false;
  64. }
  65. void ToString(nsCString &aString);
  66. };
  67. class nsSiteSecurityService : public nsISiteSecurityService
  68. , public nsIObserver
  69. {
  70. public:
  71. NS_DECL_THREADSAFE_ISUPPORTS
  72. NS_DECL_NSIOBSERVER
  73. NS_DECL_NSISITESECURITYSERVICE
  74. nsSiteSecurityService();
  75. nsresult Init();
  76. protected:
  77. virtual ~nsSiteSecurityService();
  78. private:
  79. nsresult GetHost(nsIURI *aURI, nsACString &aResult);
  80. nsresult SetHSTSState(uint32_t aType, nsIURI* aSourceURI, int64_t maxage,
  81. bool includeSubdomains, uint32_t flags,
  82. SecurityPropertyState aHSTSState);
  83. nsresult ProcessHeaderInternal(uint32_t aType, nsIURI* aSourceURI,
  84. const char* aHeader, nsISSLStatus* aSSLStatus,
  85. uint32_t aFlags, uint64_t* aMaxAge,
  86. bool* aIncludeSubdomains,
  87. uint32_t* aFailureResult);
  88. nsresult ProcessSTSHeader(nsIURI* aSourceURI, const char* aHeader,
  89. uint32_t flags, uint64_t* aMaxAge,
  90. bool* aIncludeSubdomains, uint32_t* aFailureResult);
  91. bool mUseStsService;
  92. int64_t mPreloadListTimeOffset;
  93. RefPtr<mozilla::DataStorage> mSiteStateStorage;
  94. };
  95. #endif // __nsSiteSecurityService_h__