| 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 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 return Verify() && Unzip() && BeginPatching(); | 136 return Verify() && Unzip() && BeginPatching(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void ComponentUnpacker::Unpack(const Callback& callback) { | 139 void ComponentUnpacker::Unpack(const Callback& callback) { |
| 140 callback_ = callback; | 140 callback_ = callback; |
| 141 if (!UnpackInternal()) | 141 if (!UnpackInternal()) |
| 142 Finish(); | 142 Finish(); |
| 143 } | 143 } |
| 144 | 144 |
| 145 bool ComponentUnpacker::Verify() { | 145 bool ComponentUnpacker::Verify() { |
| 146 VLOG(1) << "Verifying component: " << path_.value(); |
| 146 if (pk_hash_.empty() || path_.empty()) { | 147 if (pk_hash_.empty() || path_.empty()) { |
| 147 error_ = kInvalidParams; | 148 error_ = kInvalidParams; |
| 148 return false; | 149 return false; |
| 149 } | 150 } |
| 150 // First, validate the CRX header and signature. As of today | 151 // First, validate the CRX header and signature. As of today |
| 151 // this is SHA1 with RSA 1024. | 152 // this is SHA1 with RSA 1024. |
| 152 ScopedStdioHandle file(base::OpenFile(path_, "rb")); | 153 ScopedStdioHandle file(base::OpenFile(path_, "rb")); |
| 153 if (!file.get()) { | 154 if (!file.get()) { |
| 154 error_ = kInvalidFile; | 155 error_ = kInvalidFile; |
| 155 return false; | 156 return false; |
| 156 } | 157 } |
| 157 CRXValidator validator(file.get()); | 158 CRXValidator validator(file.get()); |
| 158 file.Close(); | 159 file.Close(); |
| 159 if (!validator.valid()) { | 160 if (!validator.valid()) { |
| 160 error_ = kInvalidFile; | 161 error_ = kInvalidFile; |
| 161 return false; | 162 return false; |
| 162 } | 163 } |
| 163 is_delta_ = validator.is_delta(); | 164 is_delta_ = validator.is_delta(); |
| 164 | 165 |
| 165 // File is valid and the digital signature matches. Now make sure | 166 // File is valid and the digital signature matches. Now make sure |
| 166 // the public key hash matches the expected hash. If they do we fully | 167 // the public key hash matches the expected hash. If they do we fully |
| 167 // trust this CRX. | 168 // trust this CRX. |
| 168 uint8 hash[32] = {}; | 169 uint8 hash[32] = {}; |
| 169 scoped_ptr<SecureHash> sha256(SecureHash::Create(SecureHash::SHA256)); | 170 scoped_ptr<SecureHash> sha256(SecureHash::Create(SecureHash::SHA256)); |
| 170 sha256->Update(&(validator.public_key()[0]), validator.public_key().size()); | 171 sha256->Update(&(validator.public_key()[0]), validator.public_key().size()); |
| 171 sha256->Finish(hash, arraysize(hash)); | 172 sha256->Finish(hash, arraysize(hash)); |
| 172 | 173 |
| 173 if (!std::equal(pk_hash_.begin(), pk_hash_.end(), hash)) { | 174 if (!std::equal(pk_hash_.begin(), pk_hash_.end(), hash)) { |
| 175 VLOG(1) << "Hash mismatch: " << path_.value(); |
| 174 error_ = kInvalidId; | 176 error_ = kInvalidId; |
| 175 return false; | 177 return false; |
| 176 } | 178 } |
| 179 VLOG(1) << "Verification successful: " << path_.value(); |
| 177 return true; | 180 return true; |
| 178 } | 181 } |
| 179 | 182 |
| 180 bool ComponentUnpacker::Unzip() { | 183 bool ComponentUnpacker::Unzip() { |
| 181 base::FilePath& destination = is_delta_ ? unpack_diff_path_ : unpack_path_; | 184 base::FilePath& destination = is_delta_ ? unpack_diff_path_ : unpack_path_; |
| 185 VLOG(1) << "Unpacking in: " << destination.value(); |
| 182 if (!base::CreateNewTempDirectory(base::FilePath::StringType(), | 186 if (!base::CreateNewTempDirectory(base::FilePath::StringType(), |
| 183 &destination)) { | 187 &destination)) { |
| 188 VLOG(1) << "Unable to create temporary directory for unpacking."; |
| 184 error_ = kUnzipPathError; | 189 error_ = kUnzipPathError; |
| 185 return false; | 190 return false; |
| 186 } | 191 } |
| 187 if (!zip::Unzip(path_, destination)) { | 192 if (!zip::Unzip(path_, destination)) { |
| 193 VLOG(1) << "Unzipping failed."; |
| 188 error_ = kUnzipFailed; | 194 error_ = kUnzipFailed; |
| 189 return false; | 195 return false; |
| 190 } | 196 } |
| 197 VLOG(1) << "Unpacked successfully"; |
| 191 return true; | 198 return true; |
| 192 } | 199 } |
| 193 | 200 |
| 194 | 201 |
| 195 bool ComponentUnpacker::BeginPatching() { | 202 bool ComponentUnpacker::BeginPatching() { |
| 196 if (is_delta_) { // Package is a diff package. | 203 if (is_delta_) { // Package is a diff package. |
| 197 // Use a different temp directory for the patch output files. | 204 // Use a different temp directory for the patch output files. |
| 198 if (!base::CreateNewTempDirectory(base::FilePath::StringType(), | 205 if (!base::CreateNewTempDirectory(base::FilePath::StringType(), |
| 199 &unpack_path_)) { | 206 &unpack_path_)) { |
| 200 error_ = kUnzipPathError; | 207 error_ = kUnzipPathError; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 base::DeleteFile(unpack_diff_path_, true); | 273 base::DeleteFile(unpack_diff_path_, true); |
| 267 if (!unpack_path_.empty()) | 274 if (!unpack_path_.empty()) |
| 268 base::DeleteFile(unpack_path_, true); | 275 base::DeleteFile(unpack_path_, true); |
| 269 callback_.Run(error_, extended_error_); | 276 callback_.Run(error_, extended_error_); |
| 270 } | 277 } |
| 271 | 278 |
| 272 ComponentUnpacker::~ComponentUnpacker() { | 279 ComponentUnpacker::~ComponentUnpacker() { |
| 273 } | 280 } |
| 274 | 281 |
| 275 } // namespace component_updater | 282 } // namespace component_updater |
| OLD | NEW |