File: encryption_module_interface.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (92 lines) | stat: -rw-r--r-- 3,541 bytes parent folder | download | duplicates (7)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_REPORTING_ENCRYPTION_ENCRYPTION_MODULE_INTERFACE_H_
#define COMPONENTS_REPORTING_ENCRYPTION_ENCRYPTION_MODULE_INTERFACE_H_

#include <atomic>
#include <string_view>

#include "base/feature_list.h"
#include "base/functional/callback.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "components/reporting/proto/synced/record.pb.h"
#include "components/reporting/util/status.h"
#include "components/reporting/util/statusor.h"

namespace reporting {

// Feature to enable/disable encryption.
// By default encryption is enabled and supported by server.
// Disabled only for testing/stress purposes.
BASE_DECLARE_FEATURE(kEncryptedReportingFeature);

class EncryptionModuleInterface
    : public base::RefCountedThreadSafe<EncryptionModuleInterface> {
 public:
  // Public key id, as defined by Keystore.
  using PublicKeyId = int32_t;

  explicit EncryptionModuleInterface(
      base::TimeDelta renew_encryption_key_period = base::Days(1));
  EncryptionModuleInterface(const EncryptionModuleInterface& other) = delete;
  EncryptionModuleInterface& operator=(const EncryptionModuleInterface& other) =
      delete;

  // EncryptRecord will attempt to encrypt the provided |record| and respond
  // with the callback. On success the returned EncryptedRecord will contain
  // the encrypted string and encryption information. EncryptedRecord then can
  // be further updated by the caller.
  void EncryptRecord(
      std::string_view record,
      base::OnceCallback<void(StatusOr<EncryptedRecord>)> cb) const;

  // Records current public asymmetric key. Makes a not about last update time.
  void UpdateAsymmetricKey(std::string_view new_public_key,
                           PublicKeyId new_public_key_id,
                           base::OnceCallback<void(Status)> response_cb);

  // Returns `false` if encryption key has not been set yet, and `true`
  // otherwise. The result is lazy: the method may return `false` for some time
  // even after the key has already been set - this is harmless, since resetting
  // or even changing the key is OK at any time.
  bool has_encryption_key() const;

  // Returns `true` if encryption key has not been set yet or it is too old
  // (received more than |renew_encryption_key_period| ago).
  bool need_encryption_key() const;

  // Returns 'true' if |kEncryptedReporting| feature is enabled.
  static bool is_enabled();

 protected:
  virtual ~EncryptionModuleInterface();

 private:
  friend base::RefCountedThreadSafe<EncryptionModuleInterface>;

  // Implements EncryptRecord for the actual module.
  virtual void EncryptRecordImpl(
      std::string_view record,
      base::OnceCallback<void(StatusOr<EncryptedRecord>)> cb) const = 0;

  // Implements UpdateAsymmetricKey for the actual module.
  virtual void UpdateAsymmetricKeyImpl(
      std::string_view new_public_key,
      PublicKeyId new_public_key_id,
      base::OnceCallback<void(Status)> response_cb) = 0;

  // Timestamp of the last public asymmetric key update by
  // |UpdateAsymmetricKey|. Initial value base::TimeTicks() indicates key is not
  // set yet.
  std::atomic<base::TimeTicks> last_encryption_key_update_{base::TimeTicks()};

  // Period of encryption key update.
  const base::TimeDelta renew_encryption_key_period_;
};

}  // namespace reporting

#endif  // COMPONENTS_REPORTING_ENCRYPTION_ENCRYPTION_MODULE_INTERFACE_H_