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 "chrome/browser/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" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "extensions/common/constants.h" | 24 #include "extensions/common/constants.h" |
25 #include "extensions/common/crx_file.h" | 25 #include "extensions/common/crx_file.h" |
26 #include "third_party/zlib/google/zip.h" | 26 #include "third_party/zlib/google/zip.h" |
27 | 27 |
28 using crypto::SecureHash; | 28 using crypto::SecureHash; |
29 | 29 |
30 namespace component_updater { | 30 namespace component_updater { |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
| 34 const char kKeyNameFingerprint[] = "fingerprint"; |
| 35 const char kKeyNamePublicKeyHash[] = "public_key_hash"; |
| 36 const char kKeyNameInProcess[] = "in_process"; |
| 37 |
| 38 const base::FilePath::CharType kCrxFileName[] = |
| 39 FILE_PATH_LITERAL("saved_installer.crx"); |
| 40 const base::FilePath::CharType kInstallDataFileName[] = |
| 41 FILE_PATH_LITERAL("metadata.json"); |
| 42 |
34 // This class makes sure that the CRX digital signature is valid | 43 // This class makes sure that the CRX digital signature is valid |
35 // and well formed. | 44 // and well formed. |
36 class CRXValidator { | 45 class CRXValidator { |
37 public: | 46 public: |
38 explicit CRXValidator(FILE* crx_file) : valid_(false), is_delta_(false) { | 47 explicit CRXValidator(FILE* crx_file) : valid_(false), is_delta_(false) { |
39 extensions::CrxFile::Header header; | 48 extensions::CrxFile::Header header; |
40 size_t len = fread(&header, 1, sizeof(header), crx_file); | 49 size_t len = fread(&header, 1, sizeof(header), crx_file); |
41 if (len < sizeof(header)) | 50 if (len < sizeof(header)) |
42 return; | 51 return; |
43 | 52 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // TODO(cpu): add a specific attribute check to a component json that the | 126 // TODO(cpu): add a specific attribute check to a component json that the |
118 // extension unpacker will reject, so that a component cannot be installed | 127 // extension unpacker will reject, so that a component cannot be installed |
119 // as an extension. | 128 // as an extension. |
120 scoped_ptr<base::DictionaryValue> ReadManifest( | 129 scoped_ptr<base::DictionaryValue> ReadManifest( |
121 const base::FilePath& unpack_path) { | 130 const base::FilePath& unpack_path) { |
122 base::FilePath manifest = | 131 base::FilePath manifest = |
123 unpack_path.Append(FILE_PATH_LITERAL("manifest.json")); | 132 unpack_path.Append(FILE_PATH_LITERAL("manifest.json")); |
124 if (!base::PathExists(manifest)) | 133 if (!base::PathExists(manifest)) |
125 return scoped_ptr<base::DictionaryValue>(); | 134 return scoped_ptr<base::DictionaryValue>(); |
126 JSONFileValueSerializer serializer(manifest); | 135 JSONFileValueSerializer serializer(manifest); |
127 std::string error; | 136 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); |
128 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, &error)); | |
129 if (!root.get()) | 137 if (!root.get()) |
130 return scoped_ptr<base::DictionaryValue>(); | 138 return scoped_ptr<base::DictionaryValue>(); |
131 if (!root->IsType(base::Value::TYPE_DICTIONARY)) | 139 if (!root->IsType(base::Value::TYPE_DICTIONARY)) |
132 return scoped_ptr<base::DictionaryValue>(); | 140 return scoped_ptr<base::DictionaryValue>(); |
133 return scoped_ptr<base::DictionaryValue>( | 141 return scoped_ptr<base::DictionaryValue>( |
134 static_cast<base::DictionaryValue*>(root.release())).Pass(); | 142 static_cast<base::DictionaryValue*>(root.release())).Pass(); |
135 } | 143 } |
136 | 144 |
137 bool ComponentUnpacker::UnpackInternal() { | 145 bool ComponentUnpacker::UnpackInternal() { |
138 return Verify() && Unzip() && BeginPatching(); | 146 return Verify() && Unzip() && BeginPatching(); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 error_ = kFingerprintWriteFailed; | 265 error_ = kFingerprintWriteFailed; |
258 return; | 266 return; |
259 } | 267 } |
260 scoped_ptr<base::DictionaryValue> manifest(ReadManifest(unpack_path_)); | 268 scoped_ptr<base::DictionaryValue> manifest(ReadManifest(unpack_path_)); |
261 if (!manifest.get()) { | 269 if (!manifest.get()) { |
262 error_ = kBadManifest; | 270 error_ = kBadManifest; |
263 return; | 271 return; |
264 } | 272 } |
265 DCHECK(error_ == kNone); | 273 DCHECK(error_ == kNone); |
266 if (!installer_->Install(*manifest, unpack_path_)) { | 274 if (!installer_->Install(*manifest, unpack_path_)) { |
| 275 installer_->OnInstallError( |
| 276 base::Bind(&ComponentUnpacker::SaveInstallSource, this)); |
267 error_ = kInstallerError; | 277 error_ = kInstallerError; |
268 return; | 278 return; |
269 } | 279 } |
270 } | 280 } |
271 | 281 |
272 void ComponentUnpacker::Finish() { | 282 void ComponentUnpacker::Finish() { |
273 if (!unpack_diff_path_.empty()) | 283 if (!unpack_diff_path_.empty()) |
274 base::DeleteFile(unpack_diff_path_, true); | 284 base::DeleteFile(unpack_diff_path_, true); |
275 if (!unpack_path_.empty()) | 285 if (!unpack_path_.empty()) |
276 base::DeleteFile(unpack_path_, true); | 286 base::DeleteFile(unpack_path_, true); |
277 callback_.Run(error_, extended_error_); | 287 callback_.Run(error_, extended_error_); |
278 } | 288 } |
279 | 289 |
280 ComponentUnpacker::~ComponentUnpacker() { | 290 ComponentUnpacker::~ComponentUnpacker() { |
281 } | 291 } |
282 | 292 |
| 293 bool ComponentUnpacker::SaveInstallSource( |
| 294 const base::FilePath& backup_path) const { |
| 295 if (!base::CreateDirectory(backup_path)) { |
| 296 return false; |
| 297 } |
| 298 |
| 299 const base::FilePath crx_path = backup_path.Append(kCrxFileName); |
| 300 if (!base::CopyFile(path_, crx_path)) { |
| 301 return false; |
| 302 } |
| 303 |
| 304 const base::FilePath install_data_file_path = |
| 305 backup_path.Append(kInstallDataFileName); |
| 306 JSONFileValueSerializer serializer(install_data_file_path); |
| 307 |
| 308 scoped_ptr<base::DictionaryValue> root(new base::DictionaryValue); |
| 309 root->SetString(kKeyNameFingerprint, fingerprint_); |
| 310 root->SetBoolean(kKeyNameInProcess, in_process_); |
| 311 scoped_ptr<base::ListValue> pk_hash_list_value(new base::ListValue); |
| 312 for (size_t i = 0; i < pk_hash_.size(); ++i) { |
| 313 pk_hash_list_value->Append(base::Value::CreateIntegerValue( |
| 314 static_cast<int>(pk_hash_[i]))); |
| 315 } |
| 316 root->Set(kKeyNamePublicKeyHash, pk_hash_list_value.release()); |
| 317 if (!serializer.Serialize(*root)) { |
| 318 return false; |
| 319 } |
| 320 |
| 321 return true; |
| 322 } |
| 323 |
| 324 scoped_refptr<ComponentUnpacker> ComponentUnpacker::CreateFromBackup( |
| 325 const base::FilePath& backup_path, |
| 326 ComponentInstaller* installer, |
| 327 scoped_refptr<base::SequencedTaskRunner> task_runner) { |
| 328 const base::FilePath crx_path = backup_path.Append(kCrxFileName); |
| 329 if (!base::PathExists(crx_path) || base::DirectoryExists(crx_path)) { |
| 330 return scoped_refptr<ComponentUnpacker>(); |
| 331 } |
| 332 |
| 333 std::string fingerprint; |
| 334 bool in_process = false; |
| 335 |
| 336 const base::FilePath install_data_file_path = |
| 337 backup_path.Append(kInstallDataFileName); |
| 338 if (!base::PathExists(install_data_file_path)) { |
| 339 return scoped_refptr<ComponentUnpacker>(); |
| 340 } |
| 341 |
| 342 JSONFileValueSerializer serializer(install_data_file_path); |
| 343 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); |
| 344 if (!root.get() || !root->IsType(base::Value::TYPE_DICTIONARY)) { |
| 345 return scoped_refptr<ComponentUnpacker>(); |
| 346 } |
| 347 |
| 348 scoped_ptr<base::DictionaryValue> install_metadata( |
| 349 static_cast<base::DictionaryValue*>(root.release())); |
| 350 |
| 351 base::ListValue* pk_hash_list_value = NULL; |
| 352 if (!install_metadata->GetStringASCII(kKeyNameFingerprint, &fingerprint) || |
| 353 !install_metadata->GetBoolean(kKeyNameInProcess, &in_process) || |
| 354 !install_metadata->GetList(kKeyNamePublicKeyHash, &pk_hash_list_value) || |
| 355 pk_hash_list_value == NULL) { |
| 356 return scoped_refptr<ComponentUnpacker>(); |
| 357 } |
| 358 |
| 359 std::vector<uint8> pk_hash; |
| 360 for (size_t i = 0; i < pk_hash_list_value->GetSize(); ++i) { |
| 361 int value = 0; |
| 362 if (pk_hash_list_value->GetInteger(i, &value)) { |
| 363 pk_hash.push_back(static_cast<uint8>(value)); |
| 364 } |
| 365 } |
| 366 |
| 367 return make_scoped_refptr(new ComponentUnpacker( |
| 368 pk_hash, crx_path, fingerprint, installer, in_process, task_runner)); |
| 369 } |
| 370 |
283 } // namespace component_updater | 371 } // namespace component_updater |
OLD | NEW |