[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(); |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 60 | if (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 | } |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 64 | std::vector<std::vector<uint8_t>> required_keys; |
| 65 | if (!pk_hash_.empty()) |
| 66 | required_keys.push_back(pk_hash_); |
Joshua Pawlicki | f4b33f38 | 2018-08-17 17:36:51 | [diff] [blame] | 67 | const crx_file::VerifierResult result = |
| 68 | crx_file::Verify(path_, crx_format_, required_keys, |
| 69 | std::vector<uint8_t>(), &public_key_, nullptr); |
waffles | 5918d5f | 2017-05-23 01:45:28 | [diff] [blame] | 70 | if (result != crx_file::VerifierResult::OK_FULL && |
| 71 | result != crx_file::VerifierResult::OK_DELTA) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 72 | error_ = UnpackerError::kInvalidFile; |
Sorin Jianu | 3091104c | 2017-06-26 18:07:20 | [diff] [blame] | 73 | extended_error_ = static_cast<int>(result); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 74 | return false; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 75 | } |
waffles | 5918d5f | 2017-05-23 01:45:28 | [diff] [blame] | 76 | is_delta_ = result == crx_file::VerifierResult::OK_DELTA; |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame] | 77 | VLOG(1) << "Verification successful: " << path_.value(); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 78 | return true; |
| 79 | } |
| 80 | |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 81 | bool ComponentUnpacker::BeginUnzipping() { |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 82 | // Mind the reference to non-const type, passed as an argument below. |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 83 | base::FilePath& destination = is_delta_ ? unpack_diff_path_ : unpack_path_; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 84 | if (!base::CreateNewTempDirectory(base::FilePath::StringType(), |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 85 | &destination)) { |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame] | 86 | VLOG(1) << "Unable to create temporary directory for unpacking."; |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 87 | error_ = UnpackerError::kUnzipPathError; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 88 | return false; |
| 89 | } |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 90 | VLOG(1) << "Unpacking in: " << destination.value(); |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 91 | unzipper_->Unzip(path_, destination, |
| 92 | base::BindOnce(&ComponentUnpacker::EndUnzipping, this)); |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
| 96 | void ComponentUnpacker::EndUnzipping(bool result) { |
| 97 | if (!result) { |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame] | 98 | VLOG(1) << "Unzipping failed."; |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 99 | error_ = UnpackerError::kUnzipFailed; |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 100 | EndUnpacking(); |
| 101 | return; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 102 | } |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame] | 103 | VLOG(1) << "Unpacked successfully"; |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 104 | BeginPatching(); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 107 | bool ComponentUnpacker::BeginPatching() { |
| 108 | if (is_delta_) { // Package is a diff package. |
| 109 | // Use a different temp directory for the patch output files. |
| 110 | if (!base::CreateNewTempDirectory(base::FilePath::StringType(), |
| 111 | &unpack_path_)) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 112 | error_ = UnpackerError::kUnzipPathError; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 113 | return false; |
| 114 | } |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 115 | patcher_ = base::MakeRefCounted<ComponentPatcher>( |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 116 | unpack_diff_path_, unpack_path_, installer_, patcher_tool_); |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 117 | base::SequencedTaskRunnerHandle::Get()->PostTask( |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 118 | FROM_HERE, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 119 | base::BindOnce(&ComponentPatcher::Start, patcher_, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 120 | base::BindOnce(&ComponentUnpacker::EndPatching, |
| 121 | scoped_refptr<ComponentUnpacker>(this)))); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 122 | } else { |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 123 | base::SequencedTaskRunnerHandle::Get()->PostTask( |
| 124 | FROM_HERE, base::BindOnce(&ComponentUnpacker::EndPatching, |
| 125 | scoped_refptr<ComponentUnpacker>(this), |
| 126 | UnpackerError::kNone, 0)); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 131 | void ComponentUnpacker::EndPatching(UnpackerError error, int extended_error) { |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 132 | error_ = error; |
| 133 | extended_error_ = extended_error; |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 134 | patcher_ = nullptr; |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 135 | |
| 136 | EndUnpacking(); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 137 | } |
| 138 | |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 139 | void ComponentUnpacker::EndUnpacking() { |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 140 | if (!unpack_diff_path_.empty()) |
Lei Zhang | 091d423 | 2019-10-15 21:12:51 | [diff] [blame] | 141 | base::DeleteFileRecursively(unpack_diff_path_); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 142 | if (error_ != UnpackerError::kNone && !unpack_path_.empty()) |
Lei Zhang | 091d423 | 2019-10-15 21:12:51 | [diff] [blame] | 143 | base::DeleteFileRecursively(unpack_path_); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 144 | |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 145 | Result result; |
| 146 | result.error = error_; |
| 147 | result.extended_error = extended_error_; |
Minh X. Nguyen | aafd763 | 2017-10-19 21:12:35 | [diff] [blame] | 148 | if (error_ == UnpackerError::kNone) { |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 149 | result.unpack_path = unpack_path_; |
Minh X. Nguyen | aafd763 | 2017-10-19 21:12:35 | [diff] [blame] | 150 | result.public_key = public_key_; |
| 151 | } |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame] | 152 | |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 153 | base::SequencedTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 154 | FROM_HERE, base::BindOnce(std::move(callback_), result)); |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 155 | } |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 156 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 157 | } // namespace update_client |