summaryrefslogtreecommitdiffstats
path: root/dom/quota/CipherKeyManager.h
blob: abafc25dd9da33b1bd3b1e67775e96c98d93fd7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef DOM_QUOTA_CIPHERKEYMANAGER_H_
#define DOM_QUOTA_CIPHERKEYMANAGER_H_

#include "mozilla/DataMutex.h"
#include "mozilla/dom/FlippedOnce.h"
#include "mozilla/dom/quota/QuotaCommon.h"
#include "nsTHashMap.h"

namespace mozilla::dom::quota {

using mozilla::FlippedOnce;

template <typename CipherStrategy>
class CipherKeyManager {
  // This helper class is used by quota clients to store/retrieve cipher
  // keys in private browsing mode. All data in private mode must be encrypted
  // using a cipher key and unique IV (Initialization Vector).

  // This class uses hashmap (represented by mCipherKeys) to store cipher keys
  // and is currently used by IndexedDB and Cache quota clients. At any given
  // time, IndexedDB may contain multiple instances of this class where each is
  // used to cipherkeys relevant to a particular database. Unlike IndexedDB,
  // CacheAPI only  has one physical sqlite db  per origin, so all cipher keys
  // corresponding to an origin in cacheAPI gets stored together in this
  // hashmap.

  // Bug1859558: It could be better if QuotaManager owns cipherKeys for
  // all the quota clients and exposes, methods like
  // GetOrCreateCipherManager(aOrigin, aDatabaseName, aClientType) for
  // clients to access their respective cipherKeys scoped.
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CipherKeyManager)

  using CipherKey = typename CipherStrategy::KeyType;

 public:
  explicit CipherKeyManager(const char* aName) : mCipherKeys(aName){};

  Maybe<CipherKey> Get(const nsACString& aKeyId = "default"_ns) {
    auto lockedCipherKeys = mCipherKeys.Lock();

    MOZ_ASSERT(!mInvalidated);

    return lockedCipherKeys->MaybeGet(aKeyId);
  }

  CipherKey Ensure(const nsACString& aKeyId = "default"_ns) {
    auto lockedCipherKeys = mCipherKeys.Lock();

    MOZ_ASSERT(!mInvalidated);

    return lockedCipherKeys->LookupOrInsertWith(aKeyId, [] {
      // Generate a new key if one corresponding to keyStoreId does not exist
      // already.

      QM_TRY_RETURN(CipherStrategy::GenerateKey(), [](const auto&) {
        // Bug1800110 Propagate the error to the caller rather than asserting.
        MOZ_RELEASE_ASSERT(false);

        return CipherKey{};
      });
    });
  }

  bool Invalidated() {
    auto lockedCipherKeys = mCipherKeys.Lock();

    return mInvalidated;
  }

  // After calling this method, callers should not call any more methods on this
  // class.
  void Invalidate() {
    auto lockedCipherKeys = mCipherKeys.Lock();

    mInvalidated.Flip();

    lockedCipherKeys->Clear();
  }

 private:
  ~CipherKeyManager() = default;
  // XXX Maybe we can avoid a mutex here by moving all accesses to the
  // background thread.
  DataMutex<nsTHashMap<nsCStringHashKey, CipherKey>> mCipherKeys;

  FlippedOnce<false> mInvalidated;
};

}  // namespace mozilla::dom::quota

#endif  // DOM_QUOTA_CIPHERKEYMANAGER_H_