Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(663)

Side by Side Diff: components/gcm_driver/crypto/gcm_encryption_provider.cc

Issue 1509683002: Convert the encryption header parsers to be iterator-based. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "components/gcm_driver/crypto/gcm_encryption_provider.h" 5 #include "components/gcm_driver/crypto/gcm_encryption_provider.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 const DecryptionFailedCallback& failure_callback) { 78 const DecryptionFailedCallback& failure_callback) {
79 DCHECK(key_store_); 79 DCHECK(key_store_);
80 80
81 const auto& encryption_header = message.data.find(kEncryptionProperty); 81 const auto& encryption_header = message.data.find(kEncryptionProperty);
82 const auto& crypto_key_header = message.data.find(kCryptoKeyProperty); 82 const auto& crypto_key_header = message.data.find(kCryptoKeyProperty);
83 83
84 // Callers are expected to call IsEncryptedMessage() prior to this method. 84 // Callers are expected to call IsEncryptedMessage() prior to this method.
85 DCHECK(encryption_header != message.data.end()); 85 DCHECK(encryption_header != message.data.end());
86 DCHECK(crypto_key_header != message.data.end()); 86 DCHECK(crypto_key_header != message.data.end());
87 87
88 std::vector<EncryptionHeaderValues> encryption_header_values; 88 EncryptionHeaderIterator encryption_header_iterator(
89 if (!ParseEncryptionHeader(encryption_header->second, 89 encryption_header->second.begin(), encryption_header->second.end());
90 &encryption_header_values)) { 90 if (!encryption_header_iterator.GetNext()) {
91 DLOG(ERROR) << "Unable to parse the value of the Encryption header"; 91 DLOG(ERROR) << "Unable to parse the value of the Encryption header";
92 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_HEADER); 92 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_HEADER);
93 return; 93 return;
94 } 94 }
95 95
96 if (encryption_header_values.size() != 1u || 96 if (encryption_header_iterator.salt().size() !=
97 encryption_header_values[0].salt.size() !=
98 GCMMessageCryptographer::kSaltSize) { 97 GCMMessageCryptographer::kSaltSize) {
99 DLOG(ERROR) << "Invalid values supplied in the Encryption header"; 98 DLOG(ERROR) << "Invalid values supplied in the Encryption header";
100 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_HEADER); 99 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_HEADER);
101 return; 100 return;
102 } 101 }
103 102
104 std::vector<CryptoKeyHeaderValues> crypto_key_header_values; 103 CryptoKeyHeaderIterator crypto_key_header_iterator(
105 if (!ParseCryptoKeyHeader(crypto_key_header->second, 104 crypto_key_header->second.begin(), crypto_key_header->second.end());
106 &crypto_key_header_values)) { 105 if (!crypto_key_header_iterator.GetNext()) {
107 DLOG(ERROR) << "Unable to parse the value of the Crypto-Key header"; 106 DLOG(ERROR) << "Unable to parse the value of the Crypto-Key header";
108 failure_callback.Run(DECRYPTION_FAILURE_INVALID_CRYPTO_KEY_HEADER); 107 failure_callback.Run(DECRYPTION_FAILURE_INVALID_CRYPTO_KEY_HEADER);
109 return; 108 return;
110 } 109 }
111 110
112 if (crypto_key_header_values.size() != 1u || 111 if (!crypto_key_header_iterator.dh().size()) {
Ryan Sleevi 2015/12/08 00:22:00 Use .empty(), not .size(), to test for empty STL c
Peter Beverloo 2015/12/16 21:09:57 Done.
113 !crypto_key_header_values[0].dh.size()) {
114 DLOG(ERROR) << "Invalid values supplied in the Crypto-Key header"; 112 DLOG(ERROR) << "Invalid values supplied in the Crypto-Key header";
115 failure_callback.Run(DECRYPTION_FAILURE_INVALID_CRYPTO_KEY_HEADER); 113 failure_callback.Run(DECRYPTION_FAILURE_INVALID_CRYPTO_KEY_HEADER);
116 return; 114 return;
117 } 115 }
118 116
119 key_store_->GetKeys( 117 key_store_->GetKeys(
120 app_id, base::Bind(&GCMEncryptionProvider::DecryptMessageWithKey, 118 app_id, base::Bind(&GCMEncryptionProvider::DecryptMessageWithKey,
121 weak_ptr_factory_.GetWeakPtr(), message, 119 weak_ptr_factory_.GetWeakPtr(), message,
122 success_callback, failure_callback, 120 success_callback, failure_callback,
123 encryption_header_values[0].salt, 121 encryption_header_iterator.salt(),
124 crypto_key_header_values[0].dh, 122 crypto_key_header_iterator.dh(),
125 encryption_header_values[0].rs)); 123 encryption_header_iterator.rs()));
126 } 124 }
127 125
128 void GCMEncryptionProvider::DidGetPublicKey(const std::string& app_id, 126 void GCMEncryptionProvider::DidGetPublicKey(const std::string& app_id,
129 const PublicKeyCallback& callback, 127 const PublicKeyCallback& callback,
130 const KeyPair& pair, 128 const KeyPair& pair,
131 const std::string& auth_secret) { 129 const std::string& auth_secret) {
132 if (!pair.IsInitialized()) { 130 if (!pair.IsInitialized()) {
133 key_store_->CreateKeys( 131 key_store_->CreateKeys(
134 app_id, base::Bind(&GCMEncryptionProvider::DidCreatePublicKey, 132 app_id, base::Bind(&GCMEncryptionProvider::DidCreatePublicKey,
135 weak_ptr_factory_.GetWeakPtr(), callback)); 133 weak_ptr_factory_.GetWeakPtr(), callback));
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 decrypted_message.decrypted = true; 195 decrypted_message.decrypted = true;
198 196
199 // There must be no data associated with the decrypted message at this point, 197 // There must be no data associated with the decrypted message at this point,
200 // to make sure that we don't end up in an infinite decryption loop. 198 // to make sure that we don't end up in an infinite decryption loop.
201 DCHECK_EQ(0u, decrypted_message.data.size()); 199 DCHECK_EQ(0u, decrypted_message.data.size());
202 200
203 success_callback.Run(decrypted_message); 201 success_callback.Run(decrypted_message);
204 } 202 }
205 203
206 } // namespace gcm 204 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698