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

Side by Side Diff: google_apis/cup/client_update_protocol.cc

Issue 15793005: Per discussion, implement the Omaha Client Update Protocol (CUP) in src/crypto. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 6 months 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 | Annotate | Revision Log
« no previous file with comments | « google_apis/cup/client_update_protocol.h ('k') | google_apis/cup/client_update_protocol_nss.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 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
5 #include "google_apis/cup/client_update_protocol.h"
6
7 #include "base/base64.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/sha1.h"
11 #include "base/string_util.h"
12 #include "base/stringprintf.h"
13 #include "crypto/hmac.h"
14 #include "crypto/random.h"
15
16 namespace {
17
18 base::StringPiece ByteVectorToSP(const std::vector<uint8>& vec) {
19 if (vec.empty())
20 return base::StringPiece();
21
22 return base::StringPiece(reinterpret_cast<const char*>(&vec[0]), vec.size());
23 }
24
25 // This class needs to implement the same hashing and signing functions as the
26 // Google Update server; for now, this is SHA-1 and HMAC-SHA1, but this may
27 // change to SHA-256 in the near future. For this reason, all primitives are
28 // wrapped. The name "SymSign" is used to mirror the CUP specification.
29 size_t HashDigestSize() {
30 return base::kSHA1Length;
31 }
32
33 std::vector<uint8> Hash(const std::vector<uint8>& data) {
34 std::vector<uint8> result(HashDigestSize());
35 base::SHA1HashBytes(data.empty() ? NULL : &data[0],
36 data.size(),
37 &result[0]);
38 return result;
39 }
40
41 std::vector<uint8> Hash(const base::StringPiece& sdata) {
42 std::vector<uint8> result(HashDigestSize());
43 base::SHA1HashBytes(sdata.empty() ?
44 NULL :
45 reinterpret_cast<const unsigned char*>(sdata.data()),
46 sdata.length(),
47 &result[0]);
48 return result;
49 }
50
51 std::vector<uint8> SymConcat(uint8 id,
52 const std::vector<uint8>* h1,
53 const std::vector<uint8>* h2,
54 const std::vector<uint8>* h3) {
55 std::vector<uint8> result;
56 result.push_back(id);
57 const std::vector<uint8>* args[] = { h1, h2, h3 };
58 for (size_t i = 0; i != arraysize(args); ++i) {
59 if (args[i]) {
60 DCHECK_EQ(args[i]->size(), HashDigestSize());
61 result.insert(result.end(), args[i]->begin(), args[i]->end());
62 }
63 }
64
65 return result;
66 }
67
68 std::vector<uint8> SymSign(const std::vector<uint8>& key,
69 const std::vector<uint8>& hashes) {
70 DCHECK(!key.empty() && !hashes.empty());
71
72 crypto::HMAC hmac(crypto::HMAC::SHA1);
73 if (!hmac.Init(&key[0], key.size()))
74 return std::vector<uint8>();
75
76 std::vector<uint8> result(hmac.DigestLength());
77 if (!hmac.Sign(ByteVectorToSP(hashes), &result[0], result.size()))
78 return std::vector<uint8>();
79
80 return result;
81 }
82
83 bool SymSignVerify(const std::vector<uint8>& key,
84 const std::vector<uint8>& hashes,
85 const std::vector<uint8>& server_proof) {
86 DCHECK(!key.empty() && !hashes.empty() && !server_proof.empty());
Ryan Sleevi 2013/06/06 22:45:16 Make this three DCHECKs - it's clearer when the fa
Ryan Myers (chromium) 2013/06/06 23:07:20 Done (here and elsewhere).
87
88 crypto::HMAC hmac(crypto::HMAC::SHA1);
89 if (!hmac.Init(&key[0], key.size()))
90 return false;
91
92 return hmac.Verify(ByteVectorToSP(hashes), ByteVectorToSP(server_proof));
93 }
94
95 // RsaPad() is implemented as described in the CUP spec. It is NOT a general
96 // purpose padding algorithm.
97 std::vector<uint8> RsaPad(size_t rsa_key_size,
98 const std::vector<uint8>& entropy) {
99 DCHECK_GE(rsa_key_size, HashDigestSize());
100
101 // The result gets padded with zeros if the result size is greater than
102 // the size of the buffer provided by the caller.
103 std::vector<uint8> result(entropy);
104 result.resize(rsa_key_size - HashDigestSize());
105
106 // For use with RSA, the input needs to be smaller than the RSA modulus,
107 // which has always the msb set.
108 result[0] &= 127; // Reset msb
109 result[0] |= 64; // Set second highest bit.
110
111 std::vector<uint8> digest = Hash(result);
112 result.insert(result.end(), digest.begin(), digest.end());
113 DCHECK_EQ(result.size(), rsa_key_size);
114 return result;
115 }
116
117 // CUP passes the versioned secret in the query portion of the URL for the
118 // update check service -- that means we need URL-safe variants of Base64.
119 // Omaha has its own implementation in base/security/b64.c; for Chromium,
Ryan Sleevi 2013/06/06 22:45:16 nit: Don't mention this path in public code.
Ryan Myers (chromium) 2013/06/06 23:07:20 Done.
120 // call the standard Base64 encoder/decoder and then apply fixups.
121 std::string UrlSafeB64Encode(const std::vector<uint8>& data) {
122 std::string result;
123 if (!base::Base64Encode(ByteVectorToSP(data), &result))
124 return std::string();
125
126 // Do an tr|+/|-_| on the output, and strip any '=' padding.
127 for (std::string::iterator it = result.begin(); it != result.end(); ++it) {
128 switch (*it) {
129 case '+':
130 *it = '-';
131 break;
132 case '/':
133 *it = '_';
134 break;
135 default:
136 break;
137 }
138 }
139 TrimString(result, "=", &result);
140
141 return result;
142 }
143
144 std::vector<uint8> UrlSafeB64Decode(const base::StringPiece& input) {
145 std::string unsafe(input.begin(), input.end());
146 for (std::string::iterator it = unsafe.begin(); it != unsafe.end(); ++it) {
147 switch (*it) {
148 case '-':
149 *it = '+';
150 break;
151 case '_':
152 *it = '/';
153 break;
154 default:
155 break;
156 }
157 }
158 if (unsafe.length() % 4)
159 unsafe.append(4 - (unsafe.length() % 4), '=');
160
161 std::string decoded;
162 if (!base::Base64Decode(unsafe, &decoded)) {
163 return std::vector<uint8>();
164 }
Ryan Sleevi 2013/06/06 22:45:16 nit: inconsistent braces
Ryan Myers (chromium) 2013/06/06 23:07:20 Done.
165
166 return std::vector<uint8>(decoded.begin(), decoded.end());
167 }
168
169 } // end namespace
170
171 ClientUpdateProtocol::ClientUpdateProtocol(int key_version)
172 : pub_key_version_(key_version) {
173 }
174
175 scoped_ptr<ClientUpdateProtocol> ClientUpdateProtocol::Create(
176 int key_version,
177 const base::StringPiece& public_key) {
178 DCHECK_GT(key_version, 0);
179 DCHECK(!public_key.empty());
180
181 scoped_ptr<ClientUpdateProtocol> result(
182 new ClientUpdateProtocol(key_version));
183 if (!result.get())
Ryan Sleevi 2013/06/06 22:45:16 if (!result) should compile fine, and is more idio
Ryan Myers (chromium) 2013/06/06 23:07:20 Done.
184 return scoped_ptr<ClientUpdateProtocol>();
185
186 if (!result->LoadPublicKey(public_key))
187 return scoped_ptr<ClientUpdateProtocol>();
188
189 size_t key_size = result->PublicKeyLength();
190 if (key_size < HashDigestSize())
191 return scoped_ptr<ClientUpdateProtocol>();
192
193 if (!result->BuildSharedKey(key_size))
194 return scoped_ptr<ClientUpdateProtocol>();
195
196 return result.Pass();
197 }
198
199 std::string ClientUpdateProtocol::GetVersionedSecret() const {
200 return base::StringPrintf("%d:%s",
201 pub_key_version_,
202 UrlSafeB64Encode(encrypted_key_source_).c_str());
203 }
204
205 bool ClientUpdateProtocol::SignRequest(const base::StringPiece& url,
206 const base::StringPiece& request_body,
207 std::string* client_proof) {
208 DCHECK(!encrypted_key_source_.empty());
209 DCHECK(!url.empty() && !request_body.empty() && client_proof);
210
211 // Compute the challenge hash:
212 // hw = HASH(HASH(v|w)|HASH(request_url)|HASH(body)).
213 // Keep the challenge hash for later to validate the server's response.
214 std::vector<uint8> internal_hashes;
215
216 std::vector<uint8> h;
217 h = Hash(GetVersionedSecret());
218 internal_hashes.insert(internal_hashes.end(), h.begin(), h.end());
219 h = Hash(url);
220 internal_hashes.insert(internal_hashes.end(), h.begin(), h.end());
221 h = Hash(request_body);
222 internal_hashes.insert(internal_hashes.end(), h.begin(), h.end());
223 DCHECK_EQ(internal_hashes.size(), 3 * HashDigestSize());
224
225 client_challenge_hash_ = Hash(internal_hashes);
226
227 // Sign the challenge hash (hw) using the shared key (sk) to produce the
228 // client proof (cp).
229 std::vector<uint8> raw_client_proof =
230 SymSign(shared_key_, SymConcat(3, &client_challenge_hash_, NULL, NULL));
231 if (raw_client_proof.empty()) {
232 client_challenge_hash_.clear();
233 return false;
234 }
235
236 *client_proof = UrlSafeB64Encode(raw_client_proof);
237 return true;
238 }
239
240 bool ClientUpdateProtocol::ValidateResponse(
241 const base::StringPiece& response_body,
242 const base::StringPiece& server_cookie,
243 const base::StringPiece& server_proof) {
244 DCHECK(!client_challenge_hash_.empty());
245
246 if (response_body.empty() || server_cookie.empty() || server_proof.empty())
247 return false;
248
249 // Decode the server proof from URL-safe Base64 to a binary HMAC for the
250 // response.
251 std::vector<uint8> sp_decoded = UrlSafeB64Decode(server_proof);
252 if (sp_decoded.empty())
253 return false;
254
255 // If the request was received by the server, the server will use its
256 // private key to decrypt |w_|, yielding the original contents of |r_|.
257 // The server can then recreate |sk_|, compute |hw_|, and SymSign(3|hw)
258 // to ensure that the cp matches the contents. It will then use |sk_|
259 // to sign its response, producing the server proof |sp|.
260 std::vector<uint8> hm = Hash(response_body);
261 std::vector<uint8> hc = Hash(server_cookie);
262 return SymSignVerify(shared_key_,
263 SymConcat(1, &client_challenge_hash_, &hm, &hc),
264 sp_decoded);
265 }
266
267 bool ClientUpdateProtocol::BuildSharedKey(size_t public_key_length) {
268 DCHECK_GE(public_key_length, HashDigestSize());
269
270 // Start by generating some random bytes that are suitable to be encrypted;
271 // this will be the source of the shared HMAC key that client and server use.
272 // (CUP specification calls this "r".)
273 std::vector<uint8> key_source;
274 std::vector<uint8> entropy(public_key_length - HashDigestSize());
275 crypto::RandBytes(&entropy[0], entropy.size());
Ryan Sleevi 2013/06/06 22:45:16 I guess I didn't realize how little you'd be able
Ryan Myers (chromium) 2013/06/06 23:07:20 Done. PTAL.
276 key_source = RsaPad(public_key_length, entropy);
277 DCHECK_EQ(public_key_length, key_source.size());
278
279 // Hash the key source (r) to generate a new shared HMAC key (sk').
280 shared_key_ = Hash(key_source);
281
282 // Encrypt the key source (r) using the public key (pk[v]) to generate the
283 // encrypted key source (w).
284 if (!EncryptKeySource(key_source))
285 return false;
286 if (encrypted_key_source_.size() != public_key_length)
287 return false;
288
289 return true;
290 }
291
292 bool ClientUpdateProtocol::BuildSharedKeyForTesting(
293 size_t public_key_length,
294 const std::vector<uint8>& key_source) {
295 DCHECK(!key_source.empty());
296 DCHECK_GE(key_source.size(), HashDigestSize());
297 DCHECK_EQ(public_key_length, key_source.size());
298
299 // Hash the key source (r) to generate a new shared HMAC key (sk').
300 shared_key_ = Hash(key_source);
301
302 // Encrypt the key source (r) using the public key (pk[v]) to generate the
303 // encrypted key source (w).
304 if (!EncryptKeySource(key_source))
305 return false;
306 if (encrypted_key_source_.size() != public_key_length)
307 return false;
308
309 return true;
310 }
311
312
OLDNEW
« no previous file with comments | « google_apis/cup/client_update_protocol.h ('k') | google_apis/cup/client_update_protocol_nss.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698