gcm_encryption_provider.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_GCM_DRIVER_CRYPTO_GCM_ENCRYPTION_PROVIDER_H_
  5. #define COMPONENTS_GCM_DRIVER_CRYPTO_GCM_ENCRYPTION_PROVIDER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/callback_forward.h"
  10. #include "base/gtest_prod_util.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "components/gcm_driver/crypto/gcm_message_cryptographer.h"
  13. namespace base {
  14. class FilePath;
  15. class SequencedTaskRunner;
  16. } // namespace base
  17. namespace crypto {
  18. class ECPrivateKey;
  19. } // namespace crypto
  20. namespace gcm {
  21. enum class GCMDecryptionResult;
  22. enum class GCMEncryptionResult;
  23. class GCMKeyStore;
  24. struct IncomingMessage;
  25. // Provider that enables the GCM Driver to deal with encryption key management
  26. // and decryption of incoming messages.
  27. class GCMEncryptionProvider {
  28. public:
  29. // Callback to be invoked when the public key and auth secret are available.
  30. using EncryptionInfoCallback =
  31. base::OnceCallback<void(std::string p256dh, std::string auth_secret)>;
  32. // Callback to be invoked when a message may have been decrypted, as indicated
  33. // by the |result|. The |message| contains the dispatchable message in success
  34. // cases, or will be initialized to an empty, default state for failure.
  35. using DecryptMessageCallback =
  36. base::OnceCallback<void(GCMDecryptionResult result,
  37. IncomingMessage message)>;
  38. // Callback to be invoked when a message may have been encrypted, as indicated
  39. // by the |result|. The |message| contains the dispatchable message in success
  40. // cases, or will be initialized to an empty, default state for failure.
  41. using EncryptMessageCallback =
  42. base::OnceCallback<void(GCMEncryptionResult result, std::string message)>;
  43. static const char kContentEncodingProperty[];
  44. // Content coding name defined by ietf-httpbis-encryption-encoding.
  45. static const char kContentCodingAes128Gcm[];
  46. GCMEncryptionProvider();
  47. GCMEncryptionProvider(const GCMEncryptionProvider&) = delete;
  48. GCMEncryptionProvider& operator=(const GCMEncryptionProvider&) = delete;
  49. ~GCMEncryptionProvider();
  50. // Initializes the encryption provider with the |store_path| and the
  51. // |blocking_task_runner|. Done separately from the constructor in order to
  52. // avoid needing a blocking task runner for anything using GCMDriver.
  53. void Init(
  54. const base::FilePath& store_path,
  55. const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner);
  56. // Retrieves the public key and authentication secret associated with the
  57. // |app_id| + |authorized_entity| pair. Will create this info if necessary.
  58. // |authorized_entity| should be the InstanceID token's authorized entity, or
  59. // "" for non-InstanceID GCM registrations.
  60. void GetEncryptionInfo(const std::string& app_id,
  61. const std::string& authorized_entity,
  62. EncryptionInfoCallback callback);
  63. // Removes all encryption information associated with the |app_id| +
  64. // |authorized_entity| pair, then invokes |callback|. |authorized_entity|
  65. // should be the InstanceID token's authorized entity, or "*" to remove for
  66. // all InstanceID tokens, or "" for non-InstanceID GCM registrations.
  67. void RemoveEncryptionInfo(const std::string& app_id,
  68. const std::string& authorized_entity,
  69. base::OnceClosure callback);
  70. // Determines whether |message| contains encrypted content.
  71. bool IsEncryptedMessage(const IncomingMessage& message) const;
  72. // Attempts to decrypt the |message|. If the |message| is not encrypted, the
  73. // |callback| will be invoked immediately. Otherwise |callback| will be called
  74. // asynchronously when |message| has been decrypted. A dispatchable message
  75. // will be used in case of success, an empty message in case of failure.
  76. void DecryptMessage(const std::string& app_id,
  77. const IncomingMessage& message,
  78. DecryptMessageCallback callback);
  79. // Attempts to encrypt the |message| using draft-ietf-webpush-encryption-08
  80. // scheme. |callback| will be called asynchronously when |message| has been
  81. // encrypted. A dispatchable message will be used in case of success, an empty
  82. // message in case of failure.
  83. void EncryptMessage(const std::string& app_id,
  84. const std::string& authorized_entity,
  85. const std::string& p256dh,
  86. const std::string& auth_secret,
  87. const std::string& message,
  88. EncryptMessageCallback callback);
  89. private:
  90. friend class GCMEncryptionProviderTest;
  91. FRIEND_TEST_ALL_PREFIXES(GCMEncryptionProviderTest,
  92. EncryptionRoundTripGCMRegistration);
  93. FRIEND_TEST_ALL_PREFIXES(GCMEncryptionProviderTest,
  94. EncryptionRoundTripInstanceIDToken);
  95. void DidGetEncryptionInfo(const std::string& app_id,
  96. const std::string& authorized_entity,
  97. EncryptionInfoCallback callback,
  98. std::unique_ptr<crypto::ECPrivateKey> key,
  99. const std::string& auth_secret);
  100. void DidCreateEncryptionInfo(EncryptionInfoCallback callback,
  101. std::unique_ptr<crypto::ECPrivateKey> key,
  102. const std::string& auth_secret);
  103. void DecryptMessageWithKey(const std::string& message_id,
  104. const std::string& collapse_key,
  105. const std::string& sender_id,
  106. const std::string& salt,
  107. const std::string& public_key,
  108. uint32_t record_size,
  109. const std::string& ciphertext,
  110. GCMMessageCryptographer::Version version,
  111. DecryptMessageCallback callback,
  112. std::unique_ptr<crypto::ECPrivateKey> key,
  113. const std::string& auth_secret);
  114. void EncryptMessageWithKey(const std::string& app_id,
  115. const std::string& authorized_entity,
  116. const std::string& p256dh,
  117. const std::string& auth_secret,
  118. const std::string& message,
  119. EncryptMessageCallback callback,
  120. std::unique_ptr<crypto::ECPrivateKey> key,
  121. const std::string& sender_auth_secret);
  122. std::unique_ptr<GCMKeyStore> key_store_;
  123. base::WeakPtrFactory<GCMEncryptionProvider> weak_ptr_factory_{this};
  124. };
  125. } // namespace gcm
  126. #endif // COMPONENTS_GCM_DRIVER_CRYPTO_GCM_ENCRYPTION_PROVIDER_H_