diff options
Diffstat (limited to 'chrome/browser/extensions/extension_creator.cc')
-rwxr-xr-x | chrome/browser/extensions/extension_creator.cc | 144 |
1 files changed, 47 insertions, 97 deletions
diff --git a/chrome/browser/extensions/extension_creator.cc b/chrome/browser/extensions/extension_creator.cc index d5400e0..4b6c8a9 100755 --- a/chrome/browser/extensions/extension_creator.cc +++ b/chrome/browser/extensions/extension_creator.cc @@ -12,12 +12,7 @@ #include "base/file_util.h" #include "base/scoped_handle.h" #include "base/string_util.h" -#include "chrome/browser/extensions/extensions_service.h" #include "chrome/common/extensions/extension.h" -#include "chrome/common/extensions/extension_error_reporter.h" -#include "chrome/common/chrome_switches.h" -#include "chrome/common/extensions/extension.h" -#include "chrome/common/json_value_serializer.h" #include "chrome/common/zip.h" #include "net/base/base64.h" @@ -25,7 +20,9 @@ namespace { const int kRSAKeySize = 1024; }; -DictionaryValue* ExtensionCreator::InitializeInput( +const char ExtensionCreator::kExtensionHeaderMagic[] = "Cr24"; + +bool ExtensionCreator::InitializeInput( const FilePath& extension_dir, const FilePath& private_key_path, const FilePath& private_key_output_path) { @@ -53,31 +50,7 @@ DictionaryValue* ExtensionCreator::InitializeInput( return false; } - // Read the manifest. - FilePath manifest_path = extension_dir.AppendASCII( - Extension::kManifestFilename); - if (!file_util::PathExists(manifest_path)) { - error_message_ = "Extension must contain '"; - error_message_.append(Extension::kManifestFilename); - error_message_.append("'."); - return false; - } - - JSONFileValueSerializer serializer(manifest_path); - std::string serialization_error; - Value* input_manifest = (serializer.Deserialize(&serialization_error)); - if (!input_manifest) { - error_message_ = "Invalid manifest.json: "; - error_message_.append(serialization_error); - return false; - } - - if (!input_manifest->IsType(Value::TYPE_DICTIONARY)) { - error_message_ = "Invalid manifest.json"; - return false; - } - - return static_cast<DictionaryValue*>(input_manifest); + return true; } base::RSAPrivateKey* ExtensionCreator::ReadInputKey(const FilePath& @@ -145,10 +118,8 @@ base::RSAPrivateKey* ExtensionCreator::GenerateKey(const FilePath& return key_pair.release(); } -bool ExtensionCreator::CreateAndSignZip(const FilePath& extension_dir, - base::RSAPrivateKey *key_pair, - FilePath* zip_path, - std::string* signature) { +bool ExtensionCreator::CreateZip(const FilePath& extension_dir, + FilePath* zip_path) { file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("chrome_"), zip_path); *zip_path = zip_path->Append(FILE_PATH_LITERAL("extension.zip")); @@ -157,9 +128,15 @@ bool ExtensionCreator::CreateAndSignZip(const FilePath& extension_dir, return false; } + return true; +} + +bool ExtensionCreator::SignZip(const FilePath& zip_path, + base::RSAPrivateKey* private_key, + std::vector<uint8>* signature) { scoped_ptr<base::SignatureCreator> signature_creator( - base::SignatureCreator::Create(key_pair)); - ScopedStdioHandle zip_handle(file_util::OpenFile(*zip_path, "rb")); + base::SignatureCreator::Create(private_key)); + ScopedStdioHandle zip_handle(file_util::OpenFile(zip_path, "rb")); uint8 buffer[1 << 16]; int bytes_read = -1; while ((bytes_read = fread(buffer, 1, sizeof(buffer), @@ -171,61 +148,36 @@ bool ExtensionCreator::CreateAndSignZip(const FilePath& extension_dir, } zip_handle.Close(); - std::vector<uint8> signature_vector; - signature_creator->Final(&signature_vector); - std::string signature_bytes(signature_vector.begin(), signature_vector.end()); - bool result = net::Base64Encode(signature_bytes, signature); - DCHECK(result); + signature_creator->Final(signature); return true; } -bool ExtensionCreator::PrepareManifestForExport(base::RSAPrivateKey *key_pair, - const std::string& signature, - DictionaryValue* manifest) { - std::vector<uint8> public_key_vector; - if (!key_pair->ExportPublicKey(&public_key_vector)) { - error_message_ = "Failed to export public key."; - return false; - } - - std::string public_key_bytes(public_key_vector.begin(), - public_key_vector.end()); - std::string public_key; - if (!net::Base64Encode(public_key_bytes, &public_key)) { - error_message_ = "Error while signing extension."; - return false; - } - - manifest->SetString(Extension::kSignatureKey, signature); - manifest->SetString(Extension::kPublicKeyKey, public_key); - - return true; -} - -bool ExtensionCreator::WriteCRX(const FilePath& crx_path, - DictionaryValue* manifest, - const FilePath& zip_path) { - std::string manifest_string; - JSONStringValueSerializer serializer(&manifest_string); - if (!serializer.Serialize(*manifest)) { - error_message_ = "Failed to write crx."; - return false; - } - +bool ExtensionCreator::WriteCRX(const FilePath& zip_path, + base::RSAPrivateKey* private_key, + const std::vector<uint8>& signature, + const FilePath& crx_path) { if (file_util::PathExists(crx_path)) file_util::Delete(crx_path, false); ScopedStdioHandle crx_handle(file_util::OpenFile(crx_path, "wb")); - ExtensionsService::ExtensionHeader header; - memcpy(&header.magic, ExtensionsService::kExtensionFileMagic, - sizeof(ExtensionsService::kExtensionFileMagic)); - header.version = 1; // kExpectedVersion - header.header_size = sizeof(ExtensionsService::ExtensionHeader); - header.manifest_size = manifest_string.size(); + std::vector<uint8> public_key; + if (!private_key->ExportPublicKey(&public_key)) { + error_message_ = "Failed to export public key."; + return false; + } - fwrite(&header, sizeof(ExtensionsService::ExtensionHeader), 1, + ExtensionCreator::ExtensionHeader header; + memcpy(&header.magic, ExtensionCreator::kExtensionHeaderMagic, + ExtensionCreator::kExtensionHeaderMagicSize); + header.version = kCurrentVersion; + header.key_size = public_key.size(); + header.signature_size = signature.size(); + + fwrite(&header, sizeof(ExtensionCreator::ExtensionHeader), 1, + crx_handle.get()); + fwrite(&public_key.front(), sizeof(uint8), public_key.size(), crx_handle.get()); - fwrite(manifest_string.c_str(), sizeof(char), manifest_string.size(), + fwrite(&signature.front(), sizeof(uint8), signature.size(), crx_handle.get()); uint8 buffer[1 << 16]; @@ -244,10 +196,10 @@ bool ExtensionCreator::Run(const FilePath& extension_dir, const FilePath& private_key_path, const FilePath& output_private_key_path) { // Check input diretory and read manifest. - scoped_ptr<DictionaryValue> manifest(InitializeInput(extension_dir, - private_key_path, output_private_key_path)); - if (!manifest.get()) + if (!InitializeInput(extension_dir, private_key_path, + output_private_key_path)) { return false; + } // Initialize Key Pair scoped_ptr<base::RSAPrivateKey> key_pair; @@ -260,16 +212,14 @@ bool ExtensionCreator::Run(const FilePath& extension_dir, // Zip up the extension. FilePath zip_path; - std::string signature; - if (!CreateAndSignZip(extension_dir, key_pair.get(), &zip_path, &signature)) - return false; - - if (!PrepareManifestForExport(key_pair.get(), signature, manifest.get())) - return false; - - // Write the final crx out to disk. - if (!WriteCRX(crx_path, manifest.get(), zip_path)) - return false; + std::vector<uint8> signature; + bool result = false; + if (CreateZip(extension_dir, &zip_path) && + SignZip(zip_path, key_pair.get(), &signature) && + WriteCRX(zip_path, key_pair.get(), signature, crx_path)) { + result = true; + } - return true; + file_util::Delete(zip_path, false); + return result; } |