[email protected] | f105043 | 2012-02-15 01:35:46 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
| 5 | #include "chrome/browser/component_updater/component_unpacker.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 11 | #include "base/file_util.h" |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 13 | #include "base/json/json_file_value_serializer.h" |
[email protected] | 9e1685a | 2014-02-13 15:08:34 | [diff] [blame] | 14 | #include "base/location.h" |
[email protected] | 871bdf1 | 2013-10-26 10:52:06 | [diff] [blame] | 15 | #include "base/logging.h" |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 16 | #include "base/memory/scoped_handle.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 17 | #include "base/strings/string_number_conversions.h" |
[email protected] | e746341 | 2013-06-10 22:53:46 | [diff] [blame] | 18 | #include "base/strings/stringprintf.h" |
[email protected] | 871bdf1 | 2013-10-26 10:52:06 | [diff] [blame] | 19 | #include "base/values.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 20 | #include "chrome/browser/component_updater/component_patcher.h" |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 21 | #include "chrome/browser/component_updater/component_updater_service.h" |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 22 | #include "crypto/secure_hash.h" |
| 23 | #include "crypto/signature_verifier.h" |
[email protected] | cda103d | 2014-04-04 16:22:39 | [diff] [blame] | 24 | #include "extensions/common/constants.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 25 | #include "extensions/common/crx_file.h" |
[email protected] | 4170d3a | 2013-05-03 23:02:57 | [diff] [blame] | 26 | #include "third_party/zlib/google/zip.h" |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 27 | |
| 28 | using crypto::SecureHash; |
| 29 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 30 | namespace component_updater { |
| 31 | |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 32 | namespace { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 33 | |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 34 | // This class makes sure that the CRX digital signature is valid |
| 35 | // and well formed. |
| 36 | class CRXValidator { |
| 37 | public: |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 38 | explicit CRXValidator(FILE* crx_file) : valid_(false), is_delta_(false) { |
[email protected] | bf3d9df | 2012-07-24 23:20:27 | [diff] [blame] | 39 | extensions::CrxFile::Header header; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 40 | size_t len = fread(&header, 1, sizeof(header), crx_file); |
[email protected] | 802a30e | 2012-06-26 10:19:41 | [diff] [blame] | 41 | if (len < sizeof(header)) |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 42 | return; |
[email protected] | 802a30e | 2012-06-26 10:19:41 | [diff] [blame] | 43 | |
[email protected] | bf3d9df | 2012-07-24 23:20:27 | [diff] [blame] | 44 | extensions::CrxFile::Error error; |
| 45 | scoped_ptr<extensions::CrxFile> crx( |
| 46 | extensions::CrxFile::Parse(header, &error)); |
[email protected] | 802a30e | 2012-06-26 10:19:41 | [diff] [blame] | 47 | if (!crx.get()) |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 48 | return; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 49 | is_delta_ = extensions::CrxFile::HeaderIsDelta(header); |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 50 | |
| 51 | std::vector<uint8> key(header.key_size); |
| 52 | len = fread(&key[0], sizeof(uint8), header.key_size, crx_file); |
[email protected] | 802a30e | 2012-06-26 10:19:41 | [diff] [blame] | 53 | if (len < header.key_size) |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 54 | return; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 55 | |
| 56 | std::vector<uint8> signature(header.signature_size); |
| 57 | len = fread(&signature[0], sizeof(uint8), header.signature_size, crx_file); |
[email protected] | 802a30e | 2012-06-26 10:19:41 | [diff] [blame] | 58 | if (len < header.signature_size) |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 59 | return; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 60 | |
| 61 | crypto::SignatureVerifier verifier; |
| 62 | if (!verifier.VerifyInit(extension_misc::kSignatureAlgorithm, |
| 63 | sizeof(extension_misc::kSignatureAlgorithm), |
| 64 | &signature[0], signature.size(), |
| 65 | &key[0], key.size())) { |
| 66 | // Signature verification initialization failed. This is most likely |
| 67 | // caused by a public key in the wrong format (should encode algorithm). |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | |
| 71 | const size_t kBufSize = 8 * 1024; |
[email protected] | d06bece | 2013-04-06 06:48:06 | [diff] [blame] | 72 | scoped_ptr<uint8[]> buf(new uint8[kBufSize]); |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 73 | while ((len = fread(buf.get(), 1, kBufSize, crx_file)) > 0) |
| 74 | verifier.VerifyUpdate(buf.get(), len); |
| 75 | |
[email protected] | 802a30e | 2012-06-26 10:19:41 | [diff] [blame] | 76 | if (!verifier.VerifyFinal()) |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 77 | return; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 78 | |
| 79 | public_key_.swap(key); |
[email protected] | 802a30e | 2012-06-26 10:19:41 | [diff] [blame] | 80 | valid_ = true; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 802a30e | 2012-06-26 10:19:41 | [diff] [blame] | 83 | bool valid() const { return valid_; } |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 84 | |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 85 | bool is_delta() const { return is_delta_; } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 86 | |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 87 | const std::vector<uint8>& public_key() const { return public_key_; } |
| 88 | |
| 89 | private: |
[email protected] | 802a30e | 2012-06-26 10:19:41 | [diff] [blame] | 90 | bool valid_; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 91 | bool is_delta_; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 92 | std::vector<uint8> public_key_; |
| 93 | }; |
| 94 | |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 95 | } // namespace |
| 96 | |
| 97 | ComponentUnpacker::ComponentUnpacker( |
| 98 | const std::vector<uint8>& pk_hash, |
| 99 | const base::FilePath& path, |
| 100 | const std::string& fingerprint, |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 101 | ComponentInstaller* installer, |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 102 | bool in_process, |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 103 | scoped_refptr<base::SequencedTaskRunner> task_runner) |
| 104 | : pk_hash_(pk_hash), |
| 105 | path_(path), |
| 106 | is_delta_(false), |
| 107 | fingerprint_(fingerprint), |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 108 | installer_(installer), |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 109 | in_process_(in_process), |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 110 | error_(kNone), |
| 111 | extended_error_(0), |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 112 | task_runner_(task_runner) { |
| 113 | } |
[email protected] | 871bdf1 | 2013-10-26 10:52:06 | [diff] [blame] | 114 | |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 115 | // TODO(cpu): add a specific attribute check to a component json that the |
| 116 | // extension unpacker will reject, so that a component cannot be installed |
| 117 | // as an extension. |
[email protected] | 871bdf1 | 2013-10-26 10:52:06 | [diff] [blame] | 118 | scoped_ptr<base::DictionaryValue> ReadManifest( |
| 119 | const base::FilePath& unpack_path) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 120 | base::FilePath manifest = |
| 121 | unpack_path.Append(FILE_PATH_LITERAL("manifest.json")); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 122 | if (!base::PathExists(manifest)) |
[email protected] | 871bdf1 | 2013-10-26 10:52:06 | [diff] [blame] | 123 | return scoped_ptr<base::DictionaryValue>(); |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 124 | JSONFileValueSerializer serializer(manifest); |
| 125 | std::string error; |
| 126 | scoped_ptr<base::Value> root(serializer.Deserialize(NULL, &error)); |
| 127 | if (!root.get()) |
[email protected] | 871bdf1 | 2013-10-26 10:52:06 | [diff] [blame] | 128 | return scoped_ptr<base::DictionaryValue>(); |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 129 | if (!root->IsType(base::Value::TYPE_DICTIONARY)) |
[email protected] | 871bdf1 | 2013-10-26 10:52:06 | [diff] [blame] | 130 | return scoped_ptr<base::DictionaryValue>(); |
| 131 | return scoped_ptr<base::DictionaryValue>( |
| 132 | static_cast<base::DictionaryValue*>(root.release())).Pass(); |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 135 | bool ComponentUnpacker::UnpackInternal() { |
| 136 | return Verify() && Unzip() && BeginPatching(); |
| 137 | } |
| 138 | |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 139 | void ComponentUnpacker::Unpack(const Callback& callback) { |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 140 | callback_ = callback; |
| 141 | if (!UnpackInternal()) |
| 142 | Finish(); |
| 143 | } |
| 144 | |
| 145 | bool ComponentUnpacker::Verify() { |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame^] | 146 | VLOG(1) << "Verifying component: " << path_.value(); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 147 | if (pk_hash_.empty() || path_.empty()) { |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 148 | error_ = kInvalidParams; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 149 | return false; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 150 | } |
| 151 | // First, validate the CRX header and signature. As of today |
| 152 | // this is SHA1 with RSA 1024. |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 153 | ScopedStdioHandle file(base::OpenFile(path_, "rb")); |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 154 | if (!file.get()) { |
| 155 | error_ = kInvalidFile; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 156 | return false; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 157 | } |
| 158 | CRXValidator validator(file.get()); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 159 | file.Close(); |
[email protected] | 802a30e | 2012-06-26 10:19:41 | [diff] [blame] | 160 | if (!validator.valid()) { |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 161 | error_ = kInvalidFile; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 162 | return false; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 163 | } |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 164 | is_delta_ = validator.is_delta(); |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 165 | |
[email protected] | f105043 | 2012-02-15 01:35:46 | [diff] [blame] | 166 | // File is valid and the digital signature matches. Now make sure |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 167 | // the public key hash matches the expected hash. If they do we fully |
| 168 | // trust this CRX. |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 169 | uint8 hash[32] = {}; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 170 | scoped_ptr<SecureHash> sha256(SecureHash::Create(SecureHash::SHA256)); |
| 171 | sha256->Update(&(validator.public_key()[0]), validator.public_key().size()); |
| 172 | sha256->Finish(hash, arraysize(hash)); |
| 173 | |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 174 | if (!std::equal(pk_hash_.begin(), pk_hash_.end(), hash)) { |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame^] | 175 | VLOG(1) << "Hash mismatch: " << path_.value(); |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 176 | error_ = kInvalidId; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 177 | return false; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 178 | } |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame^] | 179 | VLOG(1) << "Verification successful: " << path_.value(); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 180 | return true; |
| 181 | } |
| 182 | |
| 183 | bool ComponentUnpacker::Unzip() { |
| 184 | base::FilePath& destination = is_delta_ ? unpack_diff_path_ : unpack_path_; |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame^] | 185 | VLOG(1) << "Unpacking in: " << destination.value(); |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 186 | if (!base::CreateNewTempDirectory(base::FilePath::StringType(), |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 187 | &destination)) { |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame^] | 188 | VLOG(1) << "Unable to create temporary directory for unpacking."; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 189 | error_ = kUnzipPathError; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 190 | return false; |
| 191 | } |
| 192 | if (!zip::Unzip(path_, destination)) { |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame^] | 193 | VLOG(1) << "Unzipping failed."; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 194 | error_ = kUnzipFailed; |
| 195 | return false; |
| 196 | } |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame^] | 197 | VLOG(1) << "Unpacked successfully"; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | bool ComponentUnpacker::BeginPatching() { |
| 203 | if (is_delta_) { // Package is a diff package. |
| 204 | // Use a different temp directory for the patch output files. |
| 205 | if (!base::CreateNewTempDirectory(base::FilePath::StringType(), |
| 206 | &unpack_path_)) { |
| 207 | error_ = kUnzipPathError; |
| 208 | return false; |
| 209 | } |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 210 | patcher_ = new ComponentPatcher(unpack_diff_path_, |
| 211 | unpack_path_, |
| 212 | installer_, |
| 213 | in_process_, |
| 214 | task_runner_); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 215 | task_runner_->PostTask( |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 216 | FROM_HERE, |
| 217 | base::Bind(&ComponentPatcher::Start, |
| 218 | patcher_, |
| 219 | base::Bind(&ComponentUnpacker::EndPatching, |
| 220 | scoped_refptr<ComponentUnpacker>(this)))); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 221 | } else { |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 222 | task_runner_->PostTask(FROM_HERE, |
| 223 | base::Bind(&ComponentUnpacker::EndPatching, |
| 224 | scoped_refptr<ComponentUnpacker>(this), |
| 225 | kNone, |
| 226 | 0)); |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 227 | } |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | void ComponentUnpacker::EndPatching(Error error, int extended_error) { |
| 232 | error_ = error; |
| 233 | extended_error_ = extended_error; |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 234 | patcher_ = NULL; |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 235 | if (error_ != kNone) { |
| 236 | Finish(); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 237 | return; |
| 238 | } |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 239 | // Optimization: clean up patch files early, in case disk space is too low to |
| 240 | // install otherwise. |
| 241 | if (!unpack_diff_path_.empty()) { |
| 242 | base::DeleteFile(unpack_diff_path_, true); |
| 243 | unpack_diff_path_.clear(); |
| 244 | } |
| 245 | Install(); |
| 246 | Finish(); |
| 247 | } |
| 248 | |
| 249 | void ComponentUnpacker::Install() { |
| 250 | // Write the fingerprint to disk. |
| 251 | if (static_cast<int>(fingerprint_.size()) != |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 252 | base::WriteFile( |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 253 | unpack_path_.Append(FILE_PATH_LITERAL("manifest.fingerprint")), |
| 254 | fingerprint_.c_str(), |
| 255 | fingerprint_.size())) { |
| 256 | error_ = kFingerprintWriteFailed; |
| 257 | return; |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 258 | } |
| 259 | scoped_ptr<base::DictionaryValue> manifest(ReadManifest(unpack_path_)); |
| 260 | if (!manifest.get()) { |
| 261 | error_ = kBadManifest; |
| 262 | return; |
| 263 | } |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 264 | DCHECK(error_ == kNone); |
| 265 | if (!installer_->Install(*manifest, unpack_path_)) { |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 266 | error_ = kInstallerError; |
| 267 | return; |
| 268 | } |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void ComponentUnpacker::Finish() { |
| 272 | if (!unpack_diff_path_.empty()) |
| 273 | base::DeleteFile(unpack_diff_path_, true); |
| 274 | if (!unpack_path_.empty()) |
| 275 | base::DeleteFile(unpack_path_, true); |
| 276 | callback_.Run(error_, extended_error_); |
| 277 | } |
| 278 | |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 279 | ComponentUnpacker::~ComponentUnpacker() { |
[email protected] | 2e114e73 | 2011-07-22 02:55:04 | [diff] [blame] | 280 | } |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 281 | |
| 282 | } // namespace component_updater |