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 |
(...skipping 10 matching lines...) Expand all Loading... |
21 namespace gcm { | 21 namespace gcm { |
22 | 22 |
23 class GCMKeyStore; | 23 class GCMKeyStore; |
24 struct IncomingMessage; | 24 struct IncomingMessage; |
25 class KeyPair; | 25 class KeyPair; |
26 | 26 |
27 // 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 |
28 // and decryption of incoming messages. | 28 // and decryption of incoming messages. |
29 class GCMEncryptionProvider { | 29 class GCMEncryptionProvider { |
30 public: | 30 public: |
| 31 // Result of decrypting an incoming message. The values of these reasons must |
| 32 // not be changed, because they are being recorded using UMA. |
| 33 enum DecryptionResult { |
| 34 // The message had not been encrypted by the sender. |
| 35 DECRYPTION_RESULT_UNENCRYPTED = 0, |
| 36 |
| 37 // The message had been encrypted by the sender, and could successfully be |
| 38 // decrypted for the registration it has been received for. |
| 39 DECRYPTION_RESULT_DECRYPTED = 1, |
| 40 |
| 41 // The contents of the Encryption HTTP header could not be parsed. |
| 42 DECRYPTION_RESULT_INVALID_ENCRYPTION_HEADER = 2, |
| 43 |
| 44 // The contents of the Crypto-Key HTTP header could not be parsed. |
| 45 DECRYPTION_RESULT_INVALID_CRYPTO_KEY_HEADER = 3, |
| 46 |
| 47 // No public/private key-pair was associated with the app_id. |
| 48 DECRYPTION_RESULT_NO_KEYS = 4, |
| 49 |
| 50 // The shared secret cannot be derived from the keying material. |
| 51 DECRYPTION_RESULT_INVALID_SHARED_SECRET = 5, |
| 52 |
| 53 // The payload could not be decrypted as AES-128-GCM. |
| 54 DECRYPTION_RESULT_INVALID_PAYLOAD = 6, |
| 55 |
| 56 DECRYPTION_RESULT_LAST = DECRYPTION_RESULT_INVALID_PAYLOAD |
| 57 }; |
| 58 |
31 // Callback to be invoked when the public key and auth secret are available. | 59 // Callback to be invoked when the public key and auth secret are available. |
32 using EncryptionInfoCallback = base::Callback<void(const std::string&, | 60 using EncryptionInfoCallback = base::Callback<void(const std::string&, |
33 const std::string&)>; | 61 const std::string&)>; |
34 | 62 |
35 // Callback to be invoked when a message has been decrypted. | 63 // Callback to be invoked when a message may have been decrypted, as indicated |
36 using MessageDecryptedCallback = base::Callback<void(const IncomingMessage&)>; | 64 // by the |result|. The |message| contains the dispatchable message in success |
| 65 // cases, or will be initialized to an empty, default state for failure. |
| 66 using MessageCallback = base::Callback<void(DecryptionResult result, |
| 67 const IncomingMessage& message)>; |
37 | 68 |
38 // Reasons why the decryption of an incoming message can fail. | 69 // Converts |result| to a string describing the details of said result. |
39 enum DecryptionFailure { | 70 static std::string ToDecryptionResultDetailsString(DecryptionResult result); |
40 DECRYPTION_FAILURE_UNKNOWN, | |
41 | |
42 // The contents of the Encryption HTTP header could not be parsed. | |
43 DECRYPTION_FAILURE_INVALID_ENCRYPTION_HEADER, | |
44 | |
45 // The contents of the Crypto-Key HTTP header could not be parsed. | |
46 DECRYPTION_FAILURE_INVALID_CRYPTO_KEY_HEADER, | |
47 | |
48 // No public/private key-pair was associated with the app_id. | |
49 DECRYPTION_FAILURE_NO_KEYS, | |
50 | |
51 // The public key provided in the Crypto-Key header is invalid. | |
52 DECRYPTION_FAILURE_INVALID_PUBLIC_KEY, | |
53 | |
54 // The payload could not be decrypted as AES-128-GCM. | |
55 DECRYPTION_FAILURE_INVALID_PAYLOAD | |
56 }; | |
57 | |
58 // Callback to be invoked when a message cannot be decoded. | |
59 using DecryptionFailedCallback = base::Callback<void(DecryptionFailure)>; | |
60 | |
61 // Converts |reason| to a string describing the details of said reason. | |
62 static std::string ToDecryptionFailureDetailsString(DecryptionFailure reason); | |
63 | 71 |
64 GCMEncryptionProvider(); | 72 GCMEncryptionProvider(); |
65 ~GCMEncryptionProvider(); | 73 ~GCMEncryptionProvider(); |
66 | 74 |
67 // Initializes the encryption provider with the |store_path| and the | 75 // Initializes the encryption provider with the |store_path| and the |
68 // |blocking_task_runner|. Done separately from the constructor in order to | 76 // |blocking_task_runner|. Done separately from the constructor in order to |
69 // avoid needing a blocking task runner for anything using GCMDriver. | 77 // avoid needing a blocking task runner for anything using GCMDriver. |
70 void Init( | 78 void Init( |
71 const base::FilePath& store_path, | 79 const base::FilePath& store_path, |
72 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner); | 80 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner); |
73 | 81 |
74 // Retrieves the public key and authentication secret associated with the | 82 // Retrieves the public key and authentication secret associated with the |
75 // |app_id|. If none have been associated yet, they will be created. | 83 // |app_id|. If none have been associated yet, they will be created. |
76 void GetEncryptionInfo(const std::string& app_id, | 84 void GetEncryptionInfo(const std::string& app_id, |
77 const EncryptionInfoCallback& callback); | 85 const EncryptionInfoCallback& callback); |
78 | 86 |
79 // Removes all encryption information associated with the |app_id|. Will | 87 // Removes all encryption information associated with the |app_id|. Will |
80 // invoke the |callback| when this has finished. | 88 // invoke the |callback| when this has finished. |
81 void RemoveEncryptionInfo(const std::string& app_id, | 89 void RemoveEncryptionInfo(const std::string& app_id, |
82 const base::Closure& callback); | 90 const base::Closure& callback); |
83 | 91 |
84 // Determines whether |message| contains encrypted content. | 92 // Determines whether |message| contains encrypted content. |
85 bool IsEncryptedMessage(const IncomingMessage& message) const; | 93 bool IsEncryptedMessage(const IncomingMessage& message) const; |
86 | 94 |
87 // Asynchronously decrypts |message|. The |success_callback| will be invoked | 95 // Attempts to decrypt the |message|. If the |message| is not encrypted, the |
88 // the message could be decrypted successfully, accompanied by the decrypted | 96 // |callback| will be invoked immediately. Otherwise |callback| will be called |
89 // payload of the message. When decryption failed, the |failure_callback| will | 97 // asynchronously when |message| has been decrypted. A dispatchable message |
90 // be invoked with the reason that encryption failed. | 98 // will be used in case of success, an empty message in case of failure. |
91 void DecryptMessage(const std::string& app_id, | 99 void DecryptMessage(const std::string& app_id, |
92 const IncomingMessage& message, | 100 const IncomingMessage& message, |
93 const MessageDecryptedCallback& success_callback, | 101 const MessageCallback& callback); |
94 const DecryptionFailedCallback& failure_callback); | |
95 | 102 |
96 private: | 103 private: |
97 FRIEND_TEST_ALL_PREFIXES(GCMEncryptionProviderTest, EncryptionRoundTrip); | 104 FRIEND_TEST_ALL_PREFIXES(GCMEncryptionProviderTest, EncryptionRoundTrip); |
98 | 105 |
99 void DidGetEncryptionInfo(const std::string& app_id, | 106 void DidGetEncryptionInfo(const std::string& app_id, |
100 const EncryptionInfoCallback& callback, | 107 const EncryptionInfoCallback& callback, |
101 const KeyPair& pair, | 108 const KeyPair& pair, |
102 const std::string& auth_secret); | 109 const std::string& auth_secret); |
103 | 110 |
104 void DidCreateEncryptionInfo(const EncryptionInfoCallback& callback, | 111 void DidCreateEncryptionInfo(const EncryptionInfoCallback& callback, |
105 const KeyPair& pair, | 112 const KeyPair& pair, |
106 const std::string& auth_secret); | 113 const std::string& auth_secret); |
107 | 114 |
108 void DecryptMessageWithKey(const IncomingMessage& message, | 115 void DecryptMessageWithKey(const IncomingMessage& message, |
109 const MessageDecryptedCallback& success_callback, | 116 const MessageCallback& callback, |
110 const DecryptionFailedCallback& failure_callback, | |
111 const std::string& salt, | 117 const std::string& salt, |
112 const std::string& dh, | 118 const std::string& dh, |
113 uint64_t rs, | 119 uint64_t rs, |
114 const KeyPair& pair, | 120 const KeyPair& pair, |
115 const std::string& auth_secret); | 121 const std::string& auth_secret); |
116 | 122 |
117 scoped_ptr<GCMKeyStore> key_store_; | 123 scoped_ptr<GCMKeyStore> key_store_; |
118 | 124 |
119 base::WeakPtrFactory<GCMEncryptionProvider> weak_ptr_factory_; | 125 base::WeakPtrFactory<GCMEncryptionProvider> weak_ptr_factory_; |
120 | 126 |
121 DISALLOW_COPY_AND_ASSIGN(GCMEncryptionProvider); | 127 DISALLOW_COPY_AND_ASSIGN(GCMEncryptionProvider); |
122 }; | 128 }; |
123 | 129 |
124 } // namespace gcm | 130 } // namespace gcm |
125 | 131 |
126 #endif // COMPONENTS_GCM_DRIVER_CRYPTO_GCM_ENCRYPTION_PROVIDER_H_ | 132 #endif // COMPONENTS_GCM_DRIVER_CRYPTO_GCM_ENCRYPTION_PROVIDER_H_ |
OLD | NEW |