Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "crypto/cup.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 crypto { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 base::StringPiece ByteVectorToSP(const std::vector<uint8>& vec) { | |
| 21 return base::StringPiece(reinterpret_cast<const char*>(&vec[0]), vec.size()); | |
| 22 } | |
| 23 | |
| 24 // This class needs to implement the same hashing and signing functions as the | |
| 25 // Google Update server; for now, this is SHA-1 and HMAC-SHA1, but this may | |
| 26 // change to SHA-256 in the near future. For this reason, all primitives are | |
| 27 // wrapped. The name "SymSign" is used to mirror the CUP specification. | |
| 28 size_t HashDigestSize() { | |
| 29 return base::kSHA1Length; | |
| 30 } | |
| 31 | |
| 32 std::vector<uint8> Hash(const std::vector<uint8>& data) { | |
| 33 std::vector<uint8> result(HashDigestSize()); | |
| 34 base::SHA1HashBytes(data.empty() ? NULL : &data[0], | |
| 35 data.size(), | |
| 36 &result[0]); | |
| 37 return result; | |
| 38 } | |
| 39 | |
| 40 std::vector<uint8> Hash(const base::StringPiece& sdata) { | |
| 41 std::vector<uint8> result(HashDigestSize()); | |
| 42 base::SHA1HashBytes(sdata.empty() ? | |
| 43 NULL : | |
| 44 reinterpret_cast<const unsigned char*>(sdata.data()), | |
| 45 sdata.length(), | |
| 46 &result[0]); | |
| 47 return result; | |
| 48 } | |
| 49 | |
| 50 std::vector<uint8> SymConcat(uint8 id, | |
| 51 const std::vector<uint8>* h1, | |
| 52 const std::vector<uint8>* h2, | |
| 53 const std::vector<uint8>* h3) { | |
| 54 std::vector<uint8> result; | |
| 55 result.push_back(id); | |
| 56 const std::vector<uint8>* args[] = { h1, h2, h3 }; | |
| 57 for (size_t i = 0; i != arraysize(args); ++i) { | |
| 58 if (args[i]) { | |
| 59 DCHECK_EQ(args[i]->size(), HashDigestSize()); | |
| 60 result.insert(result.end(), args[i]->begin(), args[i]->end()); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 return result; | |
| 65 } | |
| 66 | |
| 67 std::vector<uint8> SymSign(const std::vector<uint8>& key, | |
| 68 const std::vector<uint8>& hashes) { | |
| 69 crypto::HMAC hmac(crypto::HMAC::SHA1); | |
| 70 if (!hmac.Init(&key[0], key.size())) | |
| 71 return std::vector<uint8>(); | |
| 72 | |
| 73 std::vector<uint8> result(hmac.DigestLength()); | |
| 74 if (!hmac.Sign(ByteVectorToSP(hashes), &result[0], result.size())) | |
| 75 return std::vector<uint8>(); | |
| 76 | |
| 77 return result; | |
| 78 } | |
| 79 | |
| 80 bool SymSignVerify(const std::vector<uint8>& key, | |
| 81 const std::vector<uint8>& hashes, | |
| 82 const std::vector<uint8>& server_proof) { | |
| 83 crypto::HMAC hmac(crypto::HMAC::SHA1); | |
| 84 if (!hmac.Init(&key[0], key.size())) | |
| 85 return false; | |
| 86 | |
| 87 return hmac.Verify(ByteVectorToSP(hashes), ByteVectorToSP(server_proof)); | |
| 88 } | |
| 89 | |
| 90 // RsaPad() is implemented as described in the CUP spec. It is NOT a general | |
| 91 // purpose padding algorithm. | |
| 92 std::vector<uint8> RsaPad(size_t rsa_key_size, | |
| 93 const std::vector<uint8>& entropy) { | |
| 94 DCHECK_GE(rsa_key_size, HashDigestSize()); | |
| 95 | |
| 96 // The result gets padded with zeros if the result size is greater than | |
| 97 // the size of the buffer provided by the caller. | |
| 98 std::vector<uint8> result(entropy); | |
| 99 result.resize(rsa_key_size - HashDigestSize()); | |
| 100 | |
| 101 // For use with RSA, the input needs to be smaller than the RSA modulus, | |
| 102 // which has always the msb set. | |
| 103 result[0] &= 127; // Reset msb | |
| 104 result[0] |= 64; // Set second highest bit. | |
| 105 | |
| 106 std::vector<uint8> digest = Hash(result); | |
| 107 result.insert(result.end(), digest.begin(), digest.end()); | |
| 108 DCHECK_EQ(result.size(), rsa_key_size); | |
| 109 return result; | |
| 110 } | |
| 111 | |
| 112 // CUP passes the versioned secret in the query portion of the URL for the | |
| 113 // update check service -- that means we need URL-safe variants of Base64. | |
| 114 // Omaha has its own implementation in base/security/b64.c; for Chromium, | |
| 115 // call the standard Base64 encoder/decoder and then apply fixups. | |
| 116 std::string UrlSafeB64Encode(const std::vector<uint8>& data) { | |
| 117 std::string result; | |
| 118 if (!base::Base64Encode(ByteVectorToSP(data), &result)) { | |
| 119 return std::string(); | |
| 120 } | |
| 121 | |
| 122 // Do an tr|+/|-_| on the output, and strip any '=' padding. | |
| 123 for (std::string::iterator it = result.begin(); it != result.end(); ++it) { | |
| 124 switch (*it) { | |
| 125 case '+': | |
| 126 *it = '-'; | |
| 127 continue; | |
| 128 case '/': | |
| 129 *it = '_'; | |
| 130 continue; | |
| 131 default: | |
| 132 continue; | |
|
wtc
2013/05/30 21:38:56
I would replace these three 'continue' statements
Ryan Myers (chromium)
2013/05/30 21:55:11
Done.
| |
| 133 } | |
| 134 } | |
| 135 TrimString(result, "=", &result); | |
| 136 | |
| 137 return result; | |
| 138 } | |
| 139 | |
| 140 std::vector<uint8> UrlSafeB64Decode(const base::StringPiece& input) { | |
| 141 std::string unsafe(input.begin(), input.end()); | |
| 142 for (std::string::iterator it = unsafe.begin(); it != unsafe.end(); ++it) { | |
| 143 switch (*it) { | |
| 144 case '-': | |
| 145 *it = '+'; | |
| 146 continue; | |
| 147 case '_': | |
| 148 *it = '/'; | |
| 149 continue; | |
| 150 default: | |
| 151 continue; | |
| 152 } | |
| 153 } | |
| 154 while (unsafe.length() % 4 != 0) { | |
| 155 unsafe.append("="); | |
| 156 } | |
| 157 | |
| 158 std::string decoded; | |
| 159 if (!base::Base64Decode(unsafe, &decoded)) { | |
| 160 return std::vector<uint8>(); | |
| 161 } | |
| 162 | |
| 163 return std::vector<uint8>(decoded.begin(), decoded.end()); | |
| 164 } | |
| 165 | |
| 166 } // end namespace | |
| 167 | |
| 168 ClientUpdateProtocol::ClientUpdateProtocol(int key_version) | |
| 169 : pub_key_version_(key_version) { | |
| 170 } | |
| 171 | |
| 172 ClientUpdateProtocol::~ClientUpdateProtocol() { | |
| 173 } | |
| 174 | |
| 175 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 if (key_version <= 0 || public_key.empty()) | |
| 181 return NULL; // At least one mandatory parameter is not valid. | |
| 182 | |
| 183 scoped_ptr<ClientUpdateProtocol> result( | |
| 184 new ClientUpdateProtocol(key_version)); | |
| 185 | |
| 186 size_t key_size = result->LoadPublicKey(public_key); | |
| 187 if (key_size < HashDigestSize()) | |
| 188 return NULL; // Public key couldn't be loaded, or is too small to be used. | |
| 189 | |
| 190 if (!result->BuildSharedKey(key_size, NULL)) | |
| 191 return NULL; // Failed to generate w. | |
| 192 | |
| 193 return result.release(); | |
| 194 } | |
| 195 | |
| 196 std::string ClientUpdateProtocol::GetVersionedSecret() const { | |
| 197 return base::StringPrintf("%d:%s", | |
| 198 pub_key_version_, | |
| 199 UrlSafeB64Encode(encrypted_key_source_).c_str()); | |
| 200 } | |
| 201 | |
| 202 bool ClientUpdateProtocol::SignRequest(const base::StringPiece& url, | |
| 203 const base::StringPiece& request_body, | |
| 204 std::string* client_proof_out) { | |
| 205 if (encrypted_key_source_.empty()) | |
| 206 return false; // Init() hasn't been called, and/or BuildSharedKey failed. | |
| 207 | |
| 208 // Compute the challenge hash: | |
| 209 // hw = HASH(HASH(v|w)|HASH(request_url)|HASH(body)). | |
| 210 // Keep the challenge hash for later to validate the server's response. | |
| 211 std::vector<uint8> internal_hashes; | |
| 212 | |
| 213 std::vector<uint8> h; | |
| 214 h = Hash(GetVersionedSecret()); | |
| 215 internal_hashes.insert(internal_hashes.end(), h.begin(), h.end()); | |
| 216 h = Hash(url); | |
| 217 internal_hashes.insert(internal_hashes.end(), h.begin(), h.end()); | |
| 218 h = Hash(request_body); | |
| 219 internal_hashes.insert(internal_hashes.end(), h.begin(), h.end()); | |
| 220 DCHECK_EQ(internal_hashes.size(), 3 * HashDigestSize()); | |
| 221 | |
| 222 client_challenge_hash_ = Hash(internal_hashes); | |
| 223 | |
| 224 // Sign the challenge hash (hw) using the shared key (sk) to produce the | |
| 225 // client proof (cp). | |
| 226 std::vector<uint8> client_proof = | |
| 227 SymSign(shared_key_, SymConcat(3, &client_challenge_hash_, NULL, NULL)); | |
| 228 if (client_proof.empty()) { | |
| 229 client_challenge_hash_.clear(); | |
| 230 return false; // HMAC failed for some reason. | |
| 231 } | |
| 232 | |
| 233 if (client_proof_out) | |
| 234 *client_proof_out = UrlSafeB64Encode(client_proof); | |
| 235 | |
| 236 return true; | |
| 237 } | |
| 238 | |
| 239 bool ClientUpdateProtocol::ValidateResponse( | |
| 240 const base::StringPiece& response_body, | |
| 241 const base::StringPiece& cookie_in, | |
| 242 const base::StringPiece& server_proof_in) { | |
| 243 if (client_challenge_hash_.empty()) | |
| 244 return false; // There hasn't been a call to SignRequest() yet. | |
| 245 | |
| 246 // Decode the server proof from URL-safe Base64 to a binary HMAC for the | |
| 247 // response. | |
| 248 std::vector<uint8> sp_decoded = UrlSafeB64Decode(server_proof_in); | |
| 249 if (sp_decoded.empty()) | |
| 250 return false; | |
| 251 | |
| 252 // If the request was received by the server, the server will use its | |
| 253 // private key to decrypt |w_|, yielding the original contents of |r_|. | |
| 254 // The server can then recreate |sk_|, compute |hw_|, and SymSign(3|hw) | |
| 255 // to ensure that the cp matches the contents. It will then use |sk_| | |
| 256 // to sign its response, producing the server proof |sp|. | |
| 257 std::vector<uint8> hm = Hash(response_body); | |
| 258 std::vector<uint8> hc = Hash(cookie_in); | |
| 259 return SymSignVerify(shared_key_, | |
| 260 SymConcat(1, &client_challenge_hash_, &hm, &hc), | |
| 261 sp_decoded); | |
| 262 } | |
| 263 | |
| 264 bool ClientUpdateProtocol::BuildSharedKey(size_t public_key_length, | |
| 265 const uint8* opt_key_source) { | |
| 266 // Start by generating some random bytes that are suitable to be encrypted; | |
| 267 // this will be the source of the shared HMAC key that client and server use. | |
| 268 // (CUP specification calls this "r".) | |
| 269 | |
| 270 DCHECK_GE(public_key_length, HashDigestSize()); | |
| 271 if (public_key_length < HashDigestSize()) | |
| 272 return false; | |
| 273 | |
| 274 std::vector<uint8> key_source; | |
| 275 if (opt_key_source) { | |
| 276 key_source.assign(opt_key_source, opt_key_source + public_key_length); | |
| 277 } else { | |
| 278 std::vector<uint8> entropy(public_key_length - HashDigestSize()); | |
| 279 crypto::RandBytes(&entropy[0], entropy.size()); | |
| 280 | |
| 281 key_source = RsaPad(public_key_length, entropy); | |
| 282 } | |
| 283 DCHECK_EQ(public_key_length, key_source.size()); | |
| 284 | |
| 285 // Hash the key source (r) to generate a new shared HMAC key (sk'). | |
| 286 shared_key_ = Hash(key_source); | |
| 287 | |
| 288 // Encrypt the key source (r) using the public key (pk[v]) to generate the | |
| 289 // encrypted key source (w). | |
| 290 if (!EncryptKeySource(key_source)) | |
| 291 return false; | |
| 292 if (encrypted_key_source_.size() != public_key_length) | |
| 293 return false; | |
| 294 | |
| 295 return true; | |
| 296 } | |
| 297 | |
| 298 } // namespace crypto | |
| 299 | |
| OLD | NEW |