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/gtest_prod_util.h" |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 class FilePath; | 17 class FilePath; |
17 class SequencedTaskRunner; | 18 class SequencedTaskRunner; |
18 } | 19 } |
19 | 20 |
20 namespace gcm { | 21 namespace gcm { |
21 | 22 |
22 class GCMKeyStore; | 23 class GCMKeyStore; |
| 24 struct IncomingMessage; |
23 class KeyPair; | 25 class KeyPair; |
24 | 26 |
25 // Provider that enables the GCM Driver to deal with encryption key management | 27 // Provider that enables the GCM Driver to deal with encryption key management |
26 // and decryption of incoming messages. | 28 // and decryption of incoming messages. |
27 class GCMEncryptionProvider { | 29 class GCMEncryptionProvider { |
28 public: | 30 public: |
29 // Callback to be invoked when the public encryption key is available. | 31 // Callback to be invoked when the public encryption key is available. |
30 using PublicKeyCallback = base::Callback<void(const std::string&)>; | 32 using PublicKeyCallback = base::Callback<void(const std::string&)>; |
31 | 33 |
| 34 // Callback to be invoked when a message has been decrypted. |
| 35 using MessageDecryptedCallback = base::Callback<void(const IncomingMessage&)>; |
| 36 |
| 37 // Reasons why the decryption of an incoming message can fail. |
| 38 enum DecryptionFailure { |
| 39 DECRYPTION_FAILURE_UNKNOWN, |
| 40 |
| 41 // The contents of the Encryption HTTP header could not be parsed. |
| 42 DECRYPTION_FAILURE_INVALID_ENCRYPTION_HEADER, |
| 43 |
| 44 // The contents of the Encryption-Key HTTP header could not be parsed. |
| 45 DECRYPTION_FAILURE_INVALID_ENCRYPTION_KEY_HEADER, |
| 46 |
| 47 // No public/private key-pair was associated with the app_id. |
| 48 DECRYPTION_FAILURE_NO_KEYS, |
| 49 |
| 50 // The payload could not be decrypted as AES-128-GCM. |
| 51 DECRYPTION_FAILURE_INVALID_PAYLOAD |
| 52 }; |
| 53 |
| 54 // Callback to be invoked when a message cannot be decoded. |
| 55 using DecryptionFailedCallback = base::Callback<void(DecryptionFailure)>; |
| 56 |
32 GCMEncryptionProvider(); | 57 GCMEncryptionProvider(); |
33 ~GCMEncryptionProvider(); | 58 ~GCMEncryptionProvider(); |
34 | 59 |
35 // Initializes the encryption provider with the |store_path| and the | 60 // Initializes the encryption provider with the |store_path| and the |
36 // |blocking_task_runner|. Done separately from the constructor in order to | 61 // |blocking_task_runner|. Done separately from the constructor in order to |
37 // avoid needing a blocking task runner for anything using GCMDriver. | 62 // avoid needing a blocking task runner for anything using GCMDriver. |
38 void Init( | 63 void Init( |
39 const base::FilePath& store_path, | 64 const base::FilePath& store_path, |
40 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner); | 65 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner); |
41 | 66 |
42 // Retrieves the public encryption key belonging to |app_id|. If no keys have | 67 // Retrieves the public encryption key belonging to |app_id|. If no keys have |
43 // been associated with |app_id| yet, they will be created. | 68 // been associated with |app_id| yet, they will be created. |
44 void GetPublicKey(const std::string& app_id, | 69 void GetPublicKey(const std::string& app_id, |
45 const PublicKeyCallback& callback); | 70 const PublicKeyCallback& callback); |
46 | 71 |
| 72 // Determines whether |message| contains encrypted content. |
| 73 bool IsEncryptedMessage(const IncomingMessage& message) const; |
| 74 |
| 75 // Asynchronously decrypts |message|. The |success_callback| will be invoked |
| 76 // the message could be decrypted successfully, accompanied by the decrypted |
| 77 // payload of the message. When decryption failed, the |failure_callback| will |
| 78 // be invoked with the reason that encryption failed. |
| 79 void DecryptMessage(const std::string& app_id, |
| 80 const IncomingMessage& message, |
| 81 const MessageDecryptedCallback& success_callback, |
| 82 const DecryptionFailedCallback& failure_callback); |
| 83 |
47 private: | 84 private: |
| 85 FRIEND_TEST_ALL_PREFIXES(GCMEncryptionProviderTest, EncryptionRoundTrip); |
| 86 |
48 void DidGetPublicKey(const std::string& app_id, | 87 void DidGetPublicKey(const std::string& app_id, |
49 const PublicKeyCallback& callback, | 88 const PublicKeyCallback& callback, |
50 const KeyPair& pair); | 89 const KeyPair& pair); |
51 | 90 |
52 void DidCreatePublicKey(const PublicKeyCallback& callback, | 91 void DidCreatePublicKey(const PublicKeyCallback& callback, |
53 const KeyPair& pair); | 92 const KeyPair& pair); |
54 | 93 |
| 94 void DecryptMessageWithKey(const IncomingMessage& message, |
| 95 const MessageDecryptedCallback& success_callback, |
| 96 const DecryptionFailedCallback& failure_callback, |
| 97 const std::string& salt, |
| 98 const std::string& dh, |
| 99 uint64_t rs, |
| 100 const KeyPair& pair); |
| 101 |
55 scoped_ptr<GCMKeyStore> key_store_; | 102 scoped_ptr<GCMKeyStore> key_store_; |
56 | 103 |
57 base::WeakPtrFactory<GCMEncryptionProvider> weak_ptr_factory_; | 104 base::WeakPtrFactory<GCMEncryptionProvider> weak_ptr_factory_; |
58 | 105 |
59 DISALLOW_COPY_AND_ASSIGN(GCMEncryptionProvider); | 106 DISALLOW_COPY_AND_ASSIGN(GCMEncryptionProvider); |
60 }; | 107 }; |
61 | 108 |
62 } // namespace gcm | 109 } // namespace gcm |
63 | 110 |
64 #endif // COMPONENTS_GCM_DRIVER_CRYPTO_GCM_ENCRYPTION_PROVIDER_H_ | 111 #endif // COMPONENTS_GCM_DRIVER_CRYPTO_GCM_ENCRYPTION_PROVIDER_H_ |
OLD | NEW |