| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <vector> | 7 #include <vector> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "app/l10n_util.h" |
| 10 #include "base/crypto/rsa_private_key.h" | 11 #include "base/crypto/rsa_private_key.h" |
| 11 #include "base/crypto/signature_creator.h" | 12 #include "base/crypto/signature_creator.h" |
| 12 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 13 #include "base/scoped_handle.h" | 14 #include "base/scoped_handle.h" |
| 14 #include "base/scoped_temp_dir.h" | 15 #include "base/scoped_temp_dir.h" |
| 15 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 16 #include "chrome/browser/extensions/sandboxed_extension_unpacker.h" | 17 #include "chrome/browser/extensions/sandboxed_extension_unpacker.h" |
| 17 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 18 #include "chrome/common/extensions/extension_file_util.h" | 19 #include "chrome/common/extensions/extension_file_util.h" |
| 19 #include "chrome/common/zip.h" | 20 #include "chrome/common/zip.h" |
| 21 #include "grit/generated_resources.h" |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 const int kRSAKeySize = 1024; | 24 const int kRSAKeySize = 1024; |
| 23 }; | 25 }; |
| 24 | 26 |
| 25 bool ExtensionCreator::InitializeInput( | 27 bool ExtensionCreator::InitializeInput( |
| 26 const FilePath& extension_dir, | 28 const FilePath& extension_dir, |
| 27 const FilePath& private_key_path, | 29 const FilePath& private_key_path, |
| 28 const FilePath& private_key_output_path) { | 30 const FilePath& private_key_output_path) { |
| 29 // Validate input |extension_dir|. | 31 // Validate input |extension_dir|. |
| 30 if (extension_dir.value().empty() || | 32 if (extension_dir.value().empty() || |
| 31 !file_util::DirectoryExists(extension_dir)) { | 33 !file_util::DirectoryExists(extension_dir)) { |
| 32 error_message_ = "Input directory must exist."; | 34 error_message_ = |
| 35 l10n_util::GetStringUTF8(IDS_EXTENSION_DIRECTORY_NO_EXISTS); |
| 33 return false; | 36 return false; |
| 34 } | 37 } |
| 35 | 38 |
| 36 // Validate input |private_key| (if provided). | 39 // Validate input |private_key| (if provided). |
| 37 if (!private_key_path.value().empty() && | 40 if (!private_key_path.value().empty() && |
| 38 !file_util::PathExists(private_key_path)) { | 41 !file_util::PathExists(private_key_path)) { |
| 39 error_message_ = "Input value for private key must be a valid path."; | 42 error_message_ = |
| 43 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_INVALID_PATH); |
| 40 return false; | 44 return false; |
| 41 } | 45 } |
| 42 | 46 |
| 43 // If an |output_private_key| path is given, make sure it doesn't over-write | 47 // If an |output_private_key| path is given, make sure it doesn't over-write |
| 44 // an existing private key. | 48 // an existing private key. |
| 45 if (private_key_path.value().empty() && | 49 if (private_key_path.value().empty() && |
| 46 !private_key_output_path.value().empty() && | 50 !private_key_output_path.value().empty() && |
| 47 file_util::PathExists(private_key_output_path)) { | 51 file_util::PathExists(private_key_output_path)) { |
| 48 error_message_ = "A private key for specified extension already exists. " | 52 error_message_ = |
| 49 "Reuse that key or delete it first."; | 53 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_EXISTS); |
| 50 return false; | 54 return false; |
| 51 } | 55 } |
| 52 | 56 |
| 53 // Load the extension once. We don't really need it, but this does a lot of | 57 // Load the extension once. We don't really need it, but this does a lot of |
| 54 // useful validation of the structure. | 58 // useful validation of the structure. |
| 55 scoped_ptr<Extension> extension( | 59 scoped_ptr<Extension> extension( |
| 56 extension_file_util::LoadExtension(extension_dir, | 60 extension_file_util::LoadExtension(extension_dir, |
| 57 false, // key not required | 61 false, // key not required |
| 58 &error_message_)); | 62 &error_message_)); |
| 59 if (!extension.get()) | 63 if (!extension.get()) |
| 60 return false; // LoadExtension already set error_message_. | 64 return false; // LoadExtension already set error_message_. |
| 61 | 65 |
| 62 return true; | 66 return true; |
| 63 } | 67 } |
| 64 | 68 |
| 65 base::RSAPrivateKey* ExtensionCreator::ReadInputKey(const FilePath& | 69 base::RSAPrivateKey* ExtensionCreator::ReadInputKey(const FilePath& |
| 66 private_key_path) { | 70 private_key_path) { |
| 67 if (!file_util::PathExists(private_key_path)) { | 71 if (!file_util::PathExists(private_key_path)) { |
| 68 error_message_ = "Input value for private key must exist."; | 72 error_message_ = |
| 73 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_NO_EXISTS); |
| 69 return false; | 74 return false; |
| 70 } | 75 } |
| 71 | 76 |
| 72 std::string private_key_contents; | 77 std::string private_key_contents; |
| 73 if (!file_util::ReadFileToString(private_key_path, | 78 if (!file_util::ReadFileToString(private_key_path, |
| 74 &private_key_contents)) { | 79 &private_key_contents)) { |
| 75 error_message_ = "Failed to read private key."; | 80 error_message_ = |
| 81 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_READ); |
| 76 return false; | 82 return false; |
| 77 } | 83 } |
| 78 | 84 |
| 79 std::string private_key_bytes; | 85 std::string private_key_bytes; |
| 80 if (!Extension::ParsePEMKeyBytes(private_key_contents, | 86 if (!Extension::ParsePEMKeyBytes(private_key_contents, |
| 81 &private_key_bytes)) { | 87 &private_key_bytes)) { |
| 82 error_message_ = "Invalid private key."; | 88 error_message_ = |
| 89 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_INVALID); |
| 83 return false; | 90 return false; |
| 84 } | 91 } |
| 85 | 92 |
| 86 return base::RSAPrivateKey::CreateFromPrivateKeyInfo( | 93 return base::RSAPrivateKey::CreateFromPrivateKeyInfo( |
| 87 std::vector<uint8>(private_key_bytes.begin(), private_key_bytes.end())); | 94 std::vector<uint8>(private_key_bytes.begin(), private_key_bytes.end())); |
| 88 } | 95 } |
| 89 | 96 |
| 90 base::RSAPrivateKey* ExtensionCreator::GenerateKey(const FilePath& | 97 base::RSAPrivateKey* ExtensionCreator::GenerateKey(const FilePath& |
| 91 output_private_key_path) { | 98 output_private_key_path) { |
| 92 scoped_ptr<base::RSAPrivateKey> key_pair( | 99 scoped_ptr<base::RSAPrivateKey> key_pair( |
| 93 base::RSAPrivateKey::Create(kRSAKeySize)); | 100 base::RSAPrivateKey::Create(kRSAKeySize)); |
| 94 if (!key_pair.get()) { | 101 if (!key_pair.get()) { |
| 95 error_message_ = "Yikes! Failed to generate random RSA private key."; | 102 error_message_ = |
| 103 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_GENERATE); |
| 96 return NULL; | 104 return NULL; |
| 97 } | 105 } |
| 98 | 106 |
| 99 std::vector<uint8> private_key_vector; | 107 std::vector<uint8> private_key_vector; |
| 100 if (!key_pair->ExportPrivateKey(&private_key_vector)) { | 108 if (!key_pair->ExportPrivateKey(&private_key_vector)) { |
| 101 error_message_ = "Failed to export private key."; | 109 error_message_ = |
| 110 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_EXPORT); |
| 102 return NULL; | 111 return NULL; |
| 103 } | 112 } |
| 104 std::string private_key_bytes( | 113 std::string private_key_bytes( |
| 105 reinterpret_cast<char*>(&private_key_vector.front()), | 114 reinterpret_cast<char*>(&private_key_vector.front()), |
| 106 private_key_vector.size()); | 115 private_key_vector.size()); |
| 107 | 116 |
| 108 std::string private_key; | 117 std::string private_key; |
| 109 if (!Extension::ProducePEM(private_key_bytes, &private_key)) { | 118 if (!Extension::ProducePEM(private_key_bytes, &private_key)) { |
| 110 error_message_ = "Failed to output private key."; | 119 error_message_ = |
| 120 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_OUTPUT); |
| 111 return NULL; | 121 return NULL; |
| 112 } | 122 } |
| 113 std::string pem_output; | 123 std::string pem_output; |
| 114 if (!Extension::FormatPEMForFileOutput(private_key, &pem_output, | 124 if (!Extension::FormatPEMForFileOutput(private_key, &pem_output, |
| 115 false)) { | 125 false)) { |
| 116 error_message_ = "Failed to output private key."; | 126 error_message_ = |
| 127 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_OUTPUT); |
| 117 return NULL; | 128 return NULL; |
| 118 } | 129 } |
| 119 | 130 |
| 120 if (!output_private_key_path.empty()) { | 131 if (!output_private_key_path.empty()) { |
| 121 if (-1 == file_util::WriteFile(output_private_key_path, | 132 if (-1 == file_util::WriteFile(output_private_key_path, |
| 122 pem_output.c_str(), pem_output.size())) { | 133 pem_output.c_str(), pem_output.size())) { |
| 123 error_message_ = "Failed to write private key."; | 134 error_message_ = |
| 135 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_OUTPUT); |
| 124 return NULL; | 136 return NULL; |
| 125 } | 137 } |
| 126 } | 138 } |
| 127 | 139 |
| 128 return key_pair.release(); | 140 return key_pair.release(); |
| 129 } | 141 } |
| 130 | 142 |
| 131 bool ExtensionCreator::CreateZip(const FilePath& extension_dir, | 143 bool ExtensionCreator::CreateZip(const FilePath& extension_dir, |
| 132 const FilePath& temp_path, | 144 const FilePath& temp_path, |
| 133 FilePath* zip_path) { | 145 FilePath* zip_path) { |
| 134 *zip_path = temp_path.Append(FILE_PATH_LITERAL("extension.zip")); | 146 *zip_path = temp_path.Append(FILE_PATH_LITERAL("extension.zip")); |
| 135 | 147 |
| 136 if (!Zip(extension_dir, *zip_path, false)) { // no hidden files | 148 if (!Zip(extension_dir, *zip_path, false)) { // no hidden files |
| 137 error_message_ = "Failed to create temporary zip file during packaging."; | 149 error_message_ = |
| 150 l10n_util::GetStringUTF8(IDS_EXTENSION_FAILED_DURING_PACKAGING); |
| 138 return false; | 151 return false; |
| 139 } | 152 } |
| 140 | 153 |
| 141 return true; | 154 return true; |
| 142 } | 155 } |
| 143 | 156 |
| 144 bool ExtensionCreator::SignZip(const FilePath& zip_path, | 157 bool ExtensionCreator::SignZip(const FilePath& zip_path, |
| 145 base::RSAPrivateKey* private_key, | 158 base::RSAPrivateKey* private_key, |
| 146 std::vector<uint8>* signature) { | 159 std::vector<uint8>* signature) { |
| 147 scoped_ptr<base::SignatureCreator> signature_creator( | 160 scoped_ptr<base::SignatureCreator> signature_creator( |
| 148 base::SignatureCreator::Create(private_key)); | 161 base::SignatureCreator::Create(private_key)); |
| 149 ScopedStdioHandle zip_handle(file_util::OpenFile(zip_path, "rb")); | 162 ScopedStdioHandle zip_handle(file_util::OpenFile(zip_path, "rb")); |
| 150 size_t buffer_size = 1 << 16; | 163 size_t buffer_size = 1 << 16; |
| 151 scoped_array<uint8> buffer(new uint8[buffer_size]); | 164 scoped_array<uint8> buffer(new uint8[buffer_size]); |
| 152 int bytes_read = -1; | 165 int bytes_read = -1; |
| 153 while ((bytes_read = fread(buffer.get(), 1, buffer_size, | 166 while ((bytes_read = fread(buffer.get(), 1, buffer_size, |
| 154 zip_handle.get())) > 0) { | 167 zip_handle.get())) > 0) { |
| 155 if (!signature_creator->Update(buffer.get(), bytes_read)) { | 168 if (!signature_creator->Update(buffer.get(), bytes_read)) { |
| 156 error_message_ = "Error while signing extension."; | 169 error_message_ = |
| 170 l10n_util::GetStringUTF8(IDS_EXTENSION_ERROR_WHILE_SIGNING); |
| 157 return false; | 171 return false; |
| 158 } | 172 } |
| 159 } | 173 } |
| 160 zip_handle.Close(); | 174 zip_handle.Close(); |
| 161 | 175 |
| 162 signature_creator->Final(signature); | 176 signature_creator->Final(signature); |
| 163 return true; | 177 return true; |
| 164 } | 178 } |
| 165 | 179 |
| 166 bool ExtensionCreator::WriteCRX(const FilePath& zip_path, | 180 bool ExtensionCreator::WriteCRX(const FilePath& zip_path, |
| 167 base::RSAPrivateKey* private_key, | 181 base::RSAPrivateKey* private_key, |
| 168 const std::vector<uint8>& signature, | 182 const std::vector<uint8>& signature, |
| 169 const FilePath& crx_path) { | 183 const FilePath& crx_path) { |
| 170 if (file_util::PathExists(crx_path)) | 184 if (file_util::PathExists(crx_path)) |
| 171 file_util::Delete(crx_path, false); | 185 file_util::Delete(crx_path, false); |
| 172 ScopedStdioHandle crx_handle(file_util::OpenFile(crx_path, "wb")); | 186 ScopedStdioHandle crx_handle(file_util::OpenFile(crx_path, "wb")); |
| 173 | 187 |
| 174 std::vector<uint8> public_key; | 188 std::vector<uint8> public_key; |
| 175 if (!private_key->ExportPublicKey(&public_key)) { | 189 if (!private_key->ExportPublicKey(&public_key)) { |
| 176 error_message_ = "Failed to export public key."; | 190 error_message_ = |
| 191 l10n_util::GetStringUTF8(IDS_EXTENSION_PUBLIC_KEY_FAILED_TO_EXPORT); |
| 177 return false; | 192 return false; |
| 178 } | 193 } |
| 179 | 194 |
| 180 SandboxedExtensionUnpacker::ExtensionHeader header; | 195 SandboxedExtensionUnpacker::ExtensionHeader header; |
| 181 memcpy(&header.magic, SandboxedExtensionUnpacker::kExtensionHeaderMagic, | 196 memcpy(&header.magic, SandboxedExtensionUnpacker::kExtensionHeaderMagic, |
| 182 SandboxedExtensionUnpacker::kExtensionHeaderMagicSize); | 197 SandboxedExtensionUnpacker::kExtensionHeaderMagicSize); |
| 183 header.version = SandboxedExtensionUnpacker::kCurrentVersion; | 198 header.version = SandboxedExtensionUnpacker::kCurrentVersion; |
| 184 header.key_size = public_key.size(); | 199 header.key_size = public_key.size(); |
| 185 header.signature_size = signature.size(); | 200 header.signature_size = signature.size(); |
| 186 | 201 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 bool result = false; | 256 bool result = false; |
| 242 if (CreateZip(extension_dir, temp_dir.path(), &zip_path) && | 257 if (CreateZip(extension_dir, temp_dir.path(), &zip_path) && |
| 243 SignZip(zip_path, key_pair.get(), &signature) && | 258 SignZip(zip_path, key_pair.get(), &signature) && |
| 244 WriteCRX(zip_path, key_pair.get(), signature, crx_path)) { | 259 WriteCRX(zip_path, key_pair.get(), signature, crx_path)) { |
| 245 result = true; | 260 result = true; |
| 246 } | 261 } |
| 247 | 262 |
| 248 file_util::Delete(zip_path, false); | 263 file_util::Delete(zip_path, false); |
| 249 return result; | 264 return result; |
| 250 } | 265 } |
| OLD | NEW |