| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_GCM_DRIVER_CRYPTO_GCM_ENCRYPTION_PROVIDER_H_ | 5 #ifndef COMPONENTS_GCM_DRIVER_CRYPTO_GCM_ENCRYPTION_PROVIDER_H_ |
| 6 #define COMPONENTS_GCM_DRIVER_CRYPTO_GCM_ENCRYPTION_PROVIDER_H_ | 6 #define COMPONENTS_GCM_DRIVER_CRYPTO_GCM_ENCRYPTION_PROVIDER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 class FilePath; | 17 class FilePath; |
| 18 class SequencedTaskRunner; | 18 class SequencedTaskRunner; |
| 19 } | 19 } |
| 20 | 20 |
| 21 namespace gcm { | 21 namespace gcm { |
| 22 | 22 |
| 23 struct EncryptionHeaderValue; |
| 24 struct EncryptionKeyHeaderValue; |
| 23 class GCMKeyStore; | 25 class GCMKeyStore; |
| 26 struct IncomingMessage; |
| 24 class KeyPair; | 27 class KeyPair; |
| 25 | 28 |
| 26 // Provider that enables the GCM Driver to deal with encryption key management | 29 // Provider that enables the GCM Driver to deal with encryption key management |
| 27 // and decryption of incoming messages. | 30 // and decryption of incoming messages. |
| 28 class GCMEncryptionProvider { | 31 class GCMEncryptionProvider { |
| 29 public: | 32 public: |
| 30 // Callback to be invoked when the public encryption key is available. | 33 // Callback to be invoked when the public encryption key is available. |
| 31 using PublicKeyCallback = base::Callback<void(const std::string&)>; | 34 using PublicKeyCallback = base::Callback<void(const std::string&)>; |
| 32 | 35 |
| 36 // Callback to be invoked when a message may have been decrypted. |
| 37 using MessageCallback = |
| 38 base::Callback<void(const IncomingMessage&, bool decryption_successful)>; |
| 39 |
| 33 GCMEncryptionProvider(); | 40 GCMEncryptionProvider(); |
| 34 ~GCMEncryptionProvider(); | 41 ~GCMEncryptionProvider(); |
| 35 | 42 |
| 36 // Initializes the encryption provider with the |store_path| and the | 43 // Initializes the encryption provider with the |store_path| and the |
| 37 // |blocking_task_runner|. Done separately from the constructor in order to | 44 // |blocking_task_runner|. Done separately from the constructor in order to |
| 38 // avoid needing a blocking task runner for anything using GCMDriver. | 45 // avoid needing a blocking task runner for anything using GCMDriver. |
| 39 void Init( | 46 void Init( |
| 40 const base::FilePath& store_path, | 47 const base::FilePath& store_path, |
| 41 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner); | 48 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner); |
| 42 | 49 |
| 43 // Retrieves the public encryption key belonging to |app_id|. If no keys have | 50 // Retrieves the public encryption key belonging to |app_id|. If no keys have |
| 44 // been associated with |app_id| yet, they will be created. | 51 // been associated with |app_id| yet, they will be created. |
| 45 void GetPublicKey(const std::string& app_id, | 52 void GetPublicKey(const std::string& app_id, |
| 46 const PublicKeyCallback& callback); | 53 const PublicKeyCallback& callback); |
| 47 | 54 |
| 55 // Determines whether |message| contains encrypted content. |
| 56 bool IsEncryptedMessage(const IncomingMessage& message) const; |
| 57 |
| 58 // Decrypts |message| and stores the result in |decrypted_message|. The return |
| 59 // value indicates whether the message could be decrypted successfully. |
| 60 void DecryptMessage(const std::string& app_id, |
| 61 const IncomingMessage& message, |
| 62 const MessageCallback& callback); |
| 63 |
| 48 private: | 64 private: |
| 49 void DidGetPublicKey(const std::string& app_id, | 65 void DidGetPublicKey(const std::string& app_id, |
| 50 const PublicKeyCallback& callback, | 66 const PublicKeyCallback& callback, |
| 51 const KeyPair& pair); | 67 const KeyPair& pair); |
| 52 | 68 |
| 53 void DidCreatePublicKey(const PublicKeyCallback& callback, | 69 void DidCreatePublicKey(const PublicKeyCallback& callback, |
| 54 const KeyPair& pair); | 70 const KeyPair& pair); |
| 55 | 71 |
| 72 void DecryptMessageWithKey( |
| 73 const IncomingMessage& message, |
| 74 const MessageCallback& callback, |
| 75 const EncryptionHeaderValue& encryption_header, |
| 76 const EncryptionKeyHeaderValue& encryption_key_header, |
| 77 const std::string& ciphertext, |
| 78 const KeyPair& pair); |
| 79 |
| 56 scoped_refptr<GCMKeyStore> key_store_; | 80 scoped_refptr<GCMKeyStore> key_store_; |
| 57 | 81 |
| 58 base::WeakPtrFactory<GCMEncryptionProvider> weak_ptr_factory_; | 82 base::WeakPtrFactory<GCMEncryptionProvider> weak_ptr_factory_; |
| 59 | 83 |
| 60 DISALLOW_COPY_AND_ASSIGN(GCMEncryptionProvider); | 84 DISALLOW_COPY_AND_ASSIGN(GCMEncryptionProvider); |
| 61 }; | 85 }; |
| 62 | 86 |
| 63 } // namespace gcm | 87 } // namespace gcm |
| 64 | 88 |
| 65 #endif // COMPONENTS_GCM_DRIVER_CRYPTO_GCM_ENCRYPTION_PROVIDER_H_ | 89 #endif // COMPONENTS_GCM_DRIVER_CRYPTO_GCM_ENCRYPTION_PROVIDER_H_ |
| OLD | NEW |