[email protected] | de0fdca2 | 2014-08-19 05:26:09 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 5 | #include "components/update_client/component_unpacker.h" |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 6 | |
sorin | 5cb1f549 | 2014-09-23 04:07:44 | [diff] [blame] | 7 | #include <stdint.h> |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 8 | #include <string> |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 9 | #include <utility> |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
thestig | 819adcc8 | 2014-09-10 22:24:53 | [diff] [blame] | 14 | #include "base/files/file_util.h" |
[email protected] | 0910bae | 2014-06-10 17:53:21 | [diff] [blame] | 15 | #include "base/files/scoped_file.h" |
[email protected] | 9e1685a | 2014-02-13 15:08:34 | [diff] [blame] | 16 | #include "base/location.h" |
[email protected] | 871bdf1 | 2013-10-26 10:52:06 | [diff] [blame] | 17 | #include "base/logging.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 18 | #include "base/macros.h" |
[email protected] | de0fdca2 | 2014-08-19 05:26:09 | [diff] [blame] | 19 | #include "base/numerics/safe_conversions.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 20 | #include "base/strings/string_number_conversions.h" |
[email protected] | e746341 | 2013-06-10 22:53:46 | [diff] [blame] | 21 | #include "base/strings/stringprintf.h" |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 22 | #include "base/threading/sequenced_task_runner_handle.h" |
waffles | 5918d5f | 2017-05-23 01:45:28 | [diff] [blame] | 23 | #include "components/crx_file/crx_verifier.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 24 | #include "components/update_client/component_patcher.h" |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 25 | #include "components/update_client/patcher.h" |
| 26 | #include "components/update_client/unzipper.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 27 | #include "components/update_client/update_client.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 28 | #include "components/update_client/update_client_errors.h" |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 29 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 30 | namespace update_client { |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 31 | |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 32 | ComponentUnpacker::Result::Result() {} |
| 33 | |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 34 | ComponentUnpacker::ComponentUnpacker(const std::vector<uint8_t>& pk_hash, |
| 35 | const base::FilePath& path, |
| 36 | scoped_refptr<CrxInstaller> installer, |
| 37 | std::unique_ptr<Unzipper> unzipper, |
| 38 | scoped_refptr<Patcher> patcher, |
| 39 | crx_file::VerifierFormat crx_format) |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 40 | : pk_hash_(pk_hash), |
| 41 | path_(path), |
| 42 | is_delta_(false), |
| 43 | installer_(installer), |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 44 | unzipper_(std::move(unzipper)), |
| 45 | patcher_tool_(patcher), |
Joshua Pawlicki | f4b33f38 | 2018-08-17 17:36:51 | [diff] [blame] | 46 | crx_format_(crx_format), |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 47 | error_(UnpackerError::kNone), |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 48 | extended_error_(0) {} |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 49 | |
| 50 | ComponentUnpacker::~ComponentUnpacker() {} |
| 51 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 52 | void ComponentUnpacker::Unpack(Callback callback) { |
| 53 | callback_ = std::move(callback); |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 54 | if (!Verify() || !BeginUnzipping()) |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 55 | EndUnpacking(); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool ComponentUnpacker::Verify() { |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame] | 59 | VLOG(1) << "Verifying component: " << path_.value(); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 60 | if (pk_hash_.empty() || path_.empty()) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 61 | error_ = UnpackerError::kInvalidParams; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 62 | return false; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 63 | } |
waffles | 5918d5f | 2017-05-23 01:45:28 | [diff] [blame] | 64 | const std::vector<std::vector<uint8_t>> required_keys = {pk_hash_}; |
Joshua Pawlicki | f4b33f38 | 2018-08-17 17:36:51 | [diff] [blame] | 65 | const crx_file::VerifierResult result = |
| 66 | crx_file::Verify(path_, crx_format_, required_keys, |
| 67 | std::vector<uint8_t>(), &public_key_, nullptr); |
waffles | 5918d5f | 2017-05-23 01:45:28 | [diff] [blame] | 68 | if (result != crx_file::VerifierResult::OK_FULL && |
| 69 | result != crx_file::VerifierResult::OK_DELTA) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 70 | error_ = UnpackerError::kInvalidFile; |
Sorin Jianu | 3091104c | 2017-06-26 18:07:20 | [diff] [blame] | 71 | extended_error_ = static_cast<int>(result); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 72 | return false; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 73 | } |
waffles | 5918d5f | 2017-05-23 01:45:28 | [diff] [blame] | 74 | is_delta_ = result == crx_file::VerifierResult::OK_DELTA; |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame] | 75 | VLOG(1) << "Verification successful: " << path_.value(); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 79 | bool ComponentUnpacker::BeginUnzipping() { |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 80 | // Mind the reference to non-const type, passed as an argument below. |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 81 | base::FilePath& destination = is_delta_ ? unpack_diff_path_ : unpack_path_; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 82 | if (!base::CreateNewTempDirectory(base::FilePath::StringType(), |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 83 | &destination)) { |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame] | 84 | VLOG(1) << "Unable to create temporary directory for unpacking."; |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 85 | error_ = UnpackerError::kUnzipPathError; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 86 | return false; |
| 87 | } |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 88 | VLOG(1) << "Unpacking in: " << destination.value(); |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 89 | unzipper_->Unzip(path_, destination, |
| 90 | base::BindOnce(&ComponentUnpacker::EndUnzipping, this)); |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
| 94 | void ComponentUnpacker::EndUnzipping(bool result) { |
| 95 | if (!result) { |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame] | 96 | VLOG(1) << "Unzipping failed."; |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 97 | error_ = UnpackerError::kUnzipFailed; |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 98 | EndUnpacking(); |
| 99 | return; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 100 | } |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame] | 101 | VLOG(1) << "Unpacked successfully"; |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 102 | BeginPatching(); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 103 | } |
| 104 | |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 105 | bool ComponentUnpacker::BeginPatching() { |
| 106 | if (is_delta_) { // Package is a diff package. |
| 107 | // Use a different temp directory for the patch output files. |
| 108 | if (!base::CreateNewTempDirectory(base::FilePath::StringType(), |
| 109 | &unpack_path_)) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 110 | error_ = UnpackerError::kUnzipPathError; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 111 | return false; |
| 112 | } |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 113 | patcher_ = base::MakeRefCounted<ComponentPatcher>( |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 114 | unpack_diff_path_, unpack_path_, installer_, patcher_tool_); |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 115 | base::SequencedTaskRunnerHandle::Get()->PostTask( |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 116 | FROM_HERE, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 117 | base::BindOnce(&ComponentPatcher::Start, patcher_, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 118 | base::BindOnce(&ComponentUnpacker::EndPatching, |
| 119 | scoped_refptr<ComponentUnpacker>(this)))); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 120 | } else { |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 121 | base::SequencedTaskRunnerHandle::Get()->PostTask( |
| 122 | FROM_HERE, base::BindOnce(&ComponentUnpacker::EndPatching, |
| 123 | scoped_refptr<ComponentUnpacker>(this), |
| 124 | UnpackerError::kNone, 0)); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 129 | void ComponentUnpacker::EndPatching(UnpackerError error, int extended_error) { |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 130 | error_ = error; |
| 131 | extended_error_ = extended_error; |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 132 | patcher_ = nullptr; |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 133 | |
| 134 | EndUnpacking(); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 135 | } |
| 136 | |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 137 | void ComponentUnpacker::EndUnpacking() { |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 138 | if (!unpack_diff_path_.empty()) |
| 139 | base::DeleteFile(unpack_diff_path_, true); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 140 | if (error_ != UnpackerError::kNone && !unpack_path_.empty()) |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 141 | base::DeleteFile(unpack_path_, true); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 142 | |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 143 | Result result; |
| 144 | result.error = error_; |
| 145 | result.extended_error = extended_error_; |
Minh X. Nguyen | aafd763 | 2017-10-19 21:12:35 | [diff] [blame] | 146 | if (error_ == UnpackerError::kNone) { |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 147 | result.unpack_path = unpack_path_; |
Minh X. Nguyen | aafd763 | 2017-10-19 21:12:35 | [diff] [blame] | 148 | result.public_key = public_key_; |
| 149 | } |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 150 | |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 151 | base::SequencedTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 152 | FROM_HERE, base::BindOnce(std::move(callback_), result)); |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 153 | } |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 154 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 155 | } // namespace update_client |