OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/component_updater/component_unpacker.h" | 5 #include "components/component_updater/component_unpacker.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/files/scoped_file.h" | 13 #include "base/files/scoped_file.h" |
14 #include "base/json/json_file_value_serializer.h" | 14 #include "base/json/json_file_value_serializer.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/numerics/safe_conversions.h" |
17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
19 #include "base/values.h" | 20 #include "base/values.h" |
20 #include "chrome/browser/component_updater/component_patcher.h" | 21 #include "components/component_updater/component_patcher.h" |
21 #include "chrome/browser/component_updater/component_patcher_operation.h" | 22 #include "components/component_updater/component_patcher_operation.h" |
22 #include "chrome/browser/component_updater/component_updater_service.h" | 23 #include "components/component_updater/component_updater_service.h" |
23 #include "components/crx_file/constants.h" | 24 #include "components/crx_file/constants.h" |
24 #include "components/crx_file/crx_file.h" | 25 #include "components/crx_file/crx_file.h" |
25 #include "crypto/secure_hash.h" | 26 #include "crypto/secure_hash.h" |
26 #include "crypto/signature_verifier.h" | 27 #include "crypto/signature_verifier.h" |
27 #include "third_party/zlib/google/zip.h" | 28 #include "third_party/zlib/google/zip.h" |
28 | 29 |
29 using crypto::SecureHash; | 30 using crypto::SecureHash; |
30 | 31 |
31 namespace component_updater { | 32 namespace component_updater { |
32 | 33 |
(...skipping 21 matching lines...) Expand all Loading... |
54 if (len < header.key_size) | 55 if (len < header.key_size) |
55 return; | 56 return; |
56 | 57 |
57 std::vector<uint8> signature(header.signature_size); | 58 std::vector<uint8> signature(header.signature_size); |
58 len = fread(&signature[0], sizeof(uint8), header.signature_size, crx_file); | 59 len = fread(&signature[0], sizeof(uint8), header.signature_size, crx_file); |
59 if (len < header.signature_size) | 60 if (len < header.signature_size) |
60 return; | 61 return; |
61 | 62 |
62 crypto::SignatureVerifier verifier; | 63 crypto::SignatureVerifier verifier; |
63 if (!verifier.VerifyInit(crx_file::kSignatureAlgorithm, | 64 if (!verifier.VerifyInit(crx_file::kSignatureAlgorithm, |
64 sizeof(crx_file::kSignatureAlgorithm), | 65 base::checked_cast<int>( |
| 66 sizeof(crx_file::kSignatureAlgorithm)), |
65 &signature[0], | 67 &signature[0], |
66 signature.size(), | 68 base::checked_cast<int>(signature.size()), |
67 &key[0], | 69 &key[0], |
68 key.size())) { | 70 base::checked_cast<int>(key.size()))) { |
69 // Signature verification initialization failed. This is most likely | 71 // Signature verification initialization failed. This is most likely |
70 // caused by a public key in the wrong format (should encode algorithm). | 72 // caused by a public key in the wrong format (should encode algorithm). |
71 return; | 73 return; |
72 } | 74 } |
73 | 75 |
74 const size_t kBufSize = 8 * 1024; | 76 const size_t kBufSize = 8 * 1024; |
75 scoped_ptr<uint8[]> buf(new uint8[kBufSize]); | 77 scoped_ptr<uint8[]> buf(new uint8[kBufSize]); |
76 while ((len = fread(buf.get(), 1, kBufSize, crx_file)) > 0) | 78 while ((len = fread(buf.get(), 1, kBufSize, crx_file)) > 0) |
77 verifier.VerifyUpdate(buf.get(), len); | 79 verifier.VerifyUpdate(buf.get(), base::checked_cast<int>(len)); |
78 | 80 |
79 if (!verifier.VerifyFinal()) | 81 if (!verifier.VerifyFinal()) |
80 return; | 82 return; |
81 | 83 |
82 public_key_.swap(key); | 84 public_key_.swap(key); |
83 valid_ = true; | 85 valid_ = true; |
84 } | 86 } |
85 | 87 |
86 bool valid() const { return valid_; } | 88 bool valid() const { return valid_; } |
87 | 89 |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 Install(); | 249 Install(); |
248 Finish(); | 250 Finish(); |
249 } | 251 } |
250 | 252 |
251 void ComponentUnpacker::Install() { | 253 void ComponentUnpacker::Install() { |
252 // Write the fingerprint to disk. | 254 // Write the fingerprint to disk. |
253 if (static_cast<int>(fingerprint_.size()) != | 255 if (static_cast<int>(fingerprint_.size()) != |
254 base::WriteFile( | 256 base::WriteFile( |
255 unpack_path_.Append(FILE_PATH_LITERAL("manifest.fingerprint")), | 257 unpack_path_.Append(FILE_PATH_LITERAL("manifest.fingerprint")), |
256 fingerprint_.c_str(), | 258 fingerprint_.c_str(), |
257 fingerprint_.size())) { | 259 base::checked_cast<int>(fingerprint_.size()))) { |
258 error_ = kFingerprintWriteFailed; | 260 error_ = kFingerprintWriteFailed; |
259 return; | 261 return; |
260 } | 262 } |
261 scoped_ptr<base::DictionaryValue> manifest(ReadManifest(unpack_path_)); | 263 scoped_ptr<base::DictionaryValue> manifest(ReadManifest(unpack_path_)); |
262 if (!manifest.get()) { | 264 if (!manifest.get()) { |
263 error_ = kBadManifest; | 265 error_ = kBadManifest; |
264 return; | 266 return; |
265 } | 267 } |
266 DCHECK(error_ == kNone); | 268 DCHECK(error_ == kNone); |
267 if (!installer_->Install(*manifest, unpack_path_)) { | 269 if (!installer_->Install(*manifest, unpack_path_)) { |
268 error_ = kInstallerError; | 270 error_ = kInstallerError; |
269 return; | 271 return; |
270 } | 272 } |
271 } | 273 } |
272 | 274 |
273 void ComponentUnpacker::Finish() { | 275 void ComponentUnpacker::Finish() { |
274 if (!unpack_diff_path_.empty()) | 276 if (!unpack_diff_path_.empty()) |
275 base::DeleteFile(unpack_diff_path_, true); | 277 base::DeleteFile(unpack_diff_path_, true); |
276 if (!unpack_path_.empty()) | 278 if (!unpack_path_.empty()) |
277 base::DeleteFile(unpack_path_, true); | 279 base::DeleteFile(unpack_path_, true); |
278 callback_.Run(error_, extended_error_); | 280 callback_.Run(error_, extended_error_); |
279 } | 281 } |
280 | 282 |
281 ComponentUnpacker::~ComponentUnpacker() { | 283 ComponentUnpacker::~ComponentUnpacker() { |
282 } | 284 } |
283 | 285 |
284 } // namespace component_updater | 286 } // namespace component_updater |
OLD | NEW |