| 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/extensions/extension_creator.h" | 5 #include "chrome/browser/extensions/extension_creator.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" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/files/scoped_file.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/memory/scoped_handle.h" | |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "chrome/browser/extensions/extension_creator_filter.h" | 16 #include "chrome/browser/extensions/extension_creator_filter.h" |
| 17 #include "crypto/rsa_private_key.h" | 17 #include "crypto/rsa_private_key.h" |
| 18 #include "crypto/signature_creator.h" | 18 #include "crypto/signature_creator.h" |
| 19 #include "extensions/common/crx_file.h" | 19 #include "extensions/common/crx_file.h" |
| 20 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
| 21 #include "extensions/common/file_util.h" | 21 #include "extensions/common/file_util.h" |
| 22 #include "extensions/common/id_util.h" | 22 #include "extensions/common/id_util.h" |
| 23 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
| 24 #include "third_party/zlib/google/zip.h" | 24 #include "third_party/zlib/google/zip.h" |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 } | 205 } |
| 206 | 206 |
| 207 return true; | 207 return true; |
| 208 } | 208 } |
| 209 | 209 |
| 210 bool ExtensionCreator::SignZip(const base::FilePath& zip_path, | 210 bool ExtensionCreator::SignZip(const base::FilePath& zip_path, |
| 211 crypto::RSAPrivateKey* private_key, | 211 crypto::RSAPrivateKey* private_key, |
| 212 std::vector<uint8>* signature) { | 212 std::vector<uint8>* signature) { |
| 213 scoped_ptr<crypto::SignatureCreator> signature_creator( | 213 scoped_ptr<crypto::SignatureCreator> signature_creator( |
| 214 crypto::SignatureCreator::Create(private_key)); | 214 crypto::SignatureCreator::Create(private_key)); |
| 215 ScopedStdioHandle zip_handle(base::OpenFile(zip_path, "rb")); | 215 base::ScopedFILE zip_handle(base::OpenFile(zip_path, "rb")); |
| 216 size_t buffer_size = 1 << 16; | 216 size_t buffer_size = 1 << 16; |
| 217 scoped_ptr<uint8[]> buffer(new uint8[buffer_size]); | 217 scoped_ptr<uint8[]> buffer(new uint8[buffer_size]); |
| 218 int bytes_read = -1; | 218 int bytes_read = -1; |
| 219 while ((bytes_read = fread(buffer.get(), 1, buffer_size, | 219 while ((bytes_read = fread(buffer.get(), 1, buffer_size, |
| 220 zip_handle.get())) > 0) { | 220 zip_handle.get())) > 0) { |
| 221 if (!signature_creator->Update(buffer.get(), bytes_read)) { | 221 if (!signature_creator->Update(buffer.get(), bytes_read)) { |
| 222 error_message_ = | 222 error_message_ = |
| 223 l10n_util::GetStringUTF8(IDS_EXTENSION_ERROR_WHILE_SIGNING); | 223 l10n_util::GetStringUTF8(IDS_EXTENSION_ERROR_WHILE_SIGNING); |
| 224 return false; | 224 return false; |
| 225 } | 225 } |
| 226 } | 226 } |
| 227 zip_handle.Close(); | 227 zip_handle.reset(); |
| 228 | 228 |
| 229 if (!signature_creator->Final(signature)) { | 229 if (!signature_creator->Final(signature)) { |
| 230 error_message_ = | 230 error_message_ = |
| 231 l10n_util::GetStringUTF8(IDS_EXTENSION_ERROR_WHILE_SIGNING); | 231 l10n_util::GetStringUTF8(IDS_EXTENSION_ERROR_WHILE_SIGNING); |
| 232 return false; | 232 return false; |
| 233 } | 233 } |
| 234 return true; | 234 return true; |
| 235 } | 235 } |
| 236 | 236 |
| 237 bool ExtensionCreator::WriteCRX(const base::FilePath& zip_path, | 237 bool ExtensionCreator::WriteCRX(const base::FilePath& zip_path, |
| 238 crypto::RSAPrivateKey* private_key, | 238 crypto::RSAPrivateKey* private_key, |
| 239 const std::vector<uint8>& signature, | 239 const std::vector<uint8>& signature, |
| 240 const base::FilePath& crx_path) { | 240 const base::FilePath& crx_path) { |
| 241 if (base::PathExists(crx_path)) | 241 if (base::PathExists(crx_path)) |
| 242 base::DeleteFile(crx_path, false); | 242 base::DeleteFile(crx_path, false); |
| 243 ScopedStdioHandle crx_handle(base::OpenFile(crx_path, "wb")); | 243 base::ScopedFILE crx_handle(base::OpenFile(crx_path, "wb")); |
| 244 if (!crx_handle.get()) { | 244 if (!crx_handle.get()) { |
| 245 error_message_ = l10n_util::GetStringUTF8(IDS_EXTENSION_SHARING_VIOLATION); | 245 error_message_ = l10n_util::GetStringUTF8(IDS_EXTENSION_SHARING_VIOLATION); |
| 246 return false; | 246 return false; |
| 247 } | 247 } |
| 248 | 248 |
| 249 std::vector<uint8> public_key; | 249 std::vector<uint8> public_key; |
| 250 CHECK(private_key->ExportPublicKey(&public_key)); | 250 CHECK(private_key->ExportPublicKey(&public_key)); |
| 251 | 251 |
| 252 CrxFile::Error error; | 252 CrxFile::Error error; |
| 253 scoped_ptr<CrxFile> crx( | 253 scoped_ptr<CrxFile> crx( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 265 PLOG(ERROR) << "fwrite failed to write public_key.front"; | 265 PLOG(ERROR) << "fwrite failed to write public_key.front"; |
| 266 } | 266 } |
| 267 if (fwrite(&signature.front(), sizeof(uint8), signature.size(), | 267 if (fwrite(&signature.front(), sizeof(uint8), signature.size(), |
| 268 crx_handle.get()) != signature.size()) { | 268 crx_handle.get()) != signature.size()) { |
| 269 PLOG(ERROR) << "fwrite failed to write signature.front"; | 269 PLOG(ERROR) << "fwrite failed to write signature.front"; |
| 270 } | 270 } |
| 271 | 271 |
| 272 size_t buffer_size = 1 << 16; | 272 size_t buffer_size = 1 << 16; |
| 273 scoped_ptr<uint8[]> buffer(new uint8[buffer_size]); | 273 scoped_ptr<uint8[]> buffer(new uint8[buffer_size]); |
| 274 size_t bytes_read = 0; | 274 size_t bytes_read = 0; |
| 275 ScopedStdioHandle zip_handle(base::OpenFile(zip_path, "rb")); | 275 base::ScopedFILE zip_handle(base::OpenFile(zip_path, "rb")); |
| 276 while ((bytes_read = fread(buffer.get(), 1, buffer_size, | 276 while ((bytes_read = fread(buffer.get(), 1, buffer_size, |
| 277 zip_handle.get())) > 0) { | 277 zip_handle.get())) > 0) { |
| 278 if (fwrite(buffer.get(), sizeof(char), bytes_read, crx_handle.get()) != | 278 if (fwrite(buffer.get(), sizeof(char), bytes_read, crx_handle.get()) != |
| 279 bytes_read) { | 279 bytes_read) { |
| 280 PLOG(ERROR) << "fwrite failed to write buffer"; | 280 PLOG(ERROR) << "fwrite failed to write buffer"; |
| 281 } | 281 } |
| 282 } | 282 } |
| 283 | 283 |
| 284 return true; | 284 return true; |
| 285 } | 285 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 SignZip(zip_path, key_pair.get(), &signature) && | 322 SignZip(zip_path, key_pair.get(), &signature) && |
| 323 WriteCRX(zip_path, key_pair.get(), signature, crx_path)) { | 323 WriteCRX(zip_path, key_pair.get(), signature, crx_path)) { |
| 324 result = true; | 324 result = true; |
| 325 } | 325 } |
| 326 | 326 |
| 327 base::DeleteFile(zip_path, false); | 327 base::DeleteFile(zip_path, false); |
| 328 return result; | 328 return result; |
| 329 } | 329 } |
| 330 | 330 |
| 331 } // namespace extensions | 331 } // namespace extensions |
| OLD | NEW |