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/. */
5 #ifndef SSLTokensCache_h_
6 #define SSLTokensCache_h_
8 #include "CertVerifier.h" // For EVStatus
9 #include "mozilla/Maybe.h"
10 #include "mozilla/StaticMutex.h"
11 #include "mozilla/StaticPrefs_network.h"
12 #include "mozilla/StaticPtr.h"
13 #include "nsClassHashtable.h"
14 #include "nsIMemoryReporter.h"
15 #include "nsITransportSecurityInfo.h"
17 #include "nsTHashMap.h"
18 #include "nsXULAppAPI.h"
20 class CommonSocketControl
;
25 struct SessionCacheInfo
{
26 SessionCacheInfo
Clone() const;
28 psm::EVStatus mEVStatus
= psm::EVStatus::NotEV
;
29 uint16_t mCertificateTransparencyStatus
=
30 nsITransportSecurityInfo::CERTIFICATE_TRANSPARENCY_NOT_APPLICABLE
;
31 nsTArray
<uint8_t> mServerCertBytes
;
32 Maybe
<nsTArray
<nsTArray
<uint8_t>>> mSucceededCertChainBytes
;
33 Maybe
<bool> mIsBuiltCertChainRootBuiltInRoot
;
34 nsITransportSecurityInfo::OverridableErrorCategory mOverridableErrorCategory
;
35 Maybe
<nsTArray
<nsTArray
<uint8_t>>> mFailedCertChainBytes
;
38 class SSLTokensCache
: public nsIMemoryReporter
{
40 NS_DECL_THREADSAFE_ISUPPORTS
41 NS_DECL_NSIMEMORYREPORTER
43 friend class ExpirationComparator
;
45 static nsresult
Init();
46 static nsresult
Shutdown();
48 static nsresult
Put(const nsACString
& aKey
, const uint8_t* aToken
,
49 uint32_t aTokenLen
, CommonSocketControl
* aSocketControl
);
50 static nsresult
Put(const nsACString
& aKey
, const uint8_t* aToken
,
51 uint32_t aTokenLen
, CommonSocketControl
* aSocketControl
,
52 PRUint32 aExpirationTime
);
53 static nsresult
Get(const nsACString
& aKey
, nsTArray
<uint8_t>& aToken
,
54 SessionCacheInfo
& aResult
, uint64_t* aTokenId
= nullptr);
55 static nsresult
Remove(const nsACString
& aKey
, uint64_t aId
);
56 static nsresult
RemoveAll(const nsACString
& aKey
);
61 virtual ~SSLTokensCache();
63 nsresult
RemoveLocked(const nsACString
& aKey
, uint64_t aId
);
64 nsresult
RemovAllLocked(const nsACString
& aKey
);
65 nsresult
GetLocked(const nsACString
& aKey
, nsTArray
<uint8_t>& aToken
,
66 SessionCacheInfo
& aResult
, uint64_t* aTokenId
);
68 void EvictIfNecessary();
71 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf
) const;
73 static mozilla::StaticRefPtr
<SSLTokensCache
> gInstance
;
74 static StaticMutex sLock MOZ_UNANNOTATED
;
75 static uint64_t sRecordId
;
77 uint32_t mCacheSize
{0}; // Actual cache size in bytes
79 class TokenCacheRecord
{
83 uint32_t Size() const;
87 PRUint32 mExpirationTime
= 0;
88 nsTArray
<uint8_t> mToken
;
89 SessionCacheInfo mSessionCacheInfo
;
90 // An unique id to identify the record. Mostly used when we want to remove a
91 // record from TokenCacheEntry.
95 class TokenCacheEntry
{
97 uint32_t Size() const;
98 // Add a record into |mRecords|. To make sure |mRecords| is sorted, we
99 // iterate |mRecords| everytime to find a right place to insert the new
101 void AddRecord(UniquePtr
<TokenCacheRecord
>&& aRecord
,
102 nsTArray
<TokenCacheRecord
*>& aExpirationArray
);
103 // This function returns the first record in |mRecords|.
104 const UniquePtr
<TokenCacheRecord
>& Get();
105 UniquePtr
<TokenCacheRecord
> RemoveWithId(uint64_t aId
);
106 uint32_t RecordCount() const { return mRecords
.Length(); }
107 const nsTArray
<UniquePtr
<TokenCacheRecord
>>& Records() { return mRecords
; }
110 // The records in this array are ordered by the expiration time.
111 nsTArray
<UniquePtr
<TokenCacheRecord
>> mRecords
;
114 void OnRecordDestroyed(TokenCacheRecord
* aRec
);
116 nsClassHashtable
<nsCStringHashKey
, TokenCacheEntry
> mTokenCacheRecords
;
117 nsTArray
<TokenCacheRecord
*> mExpirationArray
;
121 } // namespace mozilla
123 #endif // SSLTokensCache_h_