OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/crypto/rsa_private_key.h" | |
11 #include "base/crypto/signature_creator.h" | |
12 #include "base/file_util.h" | 10 #include "base/file_util.h" |
13 #include "base/memory/scoped_handle.h" | 11 #include "base/memory/scoped_handle.h" |
14 #include "base/memory/scoped_temp_dir.h" | 12 #include "base/memory/scoped_temp_dir.h" |
15 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "crypto/rsa_private_key.h" |
| 15 #include "crypto/signature_creator.h" |
16 #include "chrome/browser/extensions/sandboxed_extension_unpacker.h" | 16 #include "chrome/browser/extensions/sandboxed_extension_unpacker.h" |
17 #include "chrome/common/extensions/extension.h" | 17 #include "chrome/common/extensions/extension.h" |
18 #include "chrome/common/extensions/extension_file_util.h" | 18 #include "chrome/common/extensions/extension_file_util.h" |
19 #include "chrome/common/zip.h" | 19 #include "chrome/common/zip.h" |
20 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
21 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
22 | 22 |
23 namespace { | 23 namespace { |
24 const int kRSAKeySize = 1024; | 24 const int kRSAKeySize = 1024; |
25 }; | 25 }; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 extension_file_util::LoadExtension(absolute_extension_dir, | 67 extension_file_util::LoadExtension(absolute_extension_dir, |
68 Extension::INTERNAL, | 68 Extension::INTERNAL, |
69 Extension::STRICT_ERROR_CHECKS, | 69 Extension::STRICT_ERROR_CHECKS, |
70 &error_message_)); | 70 &error_message_)); |
71 if (!extension.get()) | 71 if (!extension.get()) |
72 return false; // LoadExtension already set error_message_. | 72 return false; // LoadExtension already set error_message_. |
73 | 73 |
74 return true; | 74 return true; |
75 } | 75 } |
76 | 76 |
77 base::RSAPrivateKey* ExtensionCreator::ReadInputKey(const FilePath& | 77 crypto::RSAPrivateKey* ExtensionCreator::ReadInputKey(const FilePath& |
78 private_key_path) { | 78 private_key_path) { |
79 if (!file_util::PathExists(private_key_path)) { | 79 if (!file_util::PathExists(private_key_path)) { |
80 error_message_ = | 80 error_message_ = |
81 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_NO_EXISTS); | 81 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_NO_EXISTS); |
82 return NULL; | 82 return NULL; |
83 } | 83 } |
84 | 84 |
85 std::string private_key_contents; | 85 std::string private_key_contents; |
86 if (!file_util::ReadFileToString(private_key_path, | 86 if (!file_util::ReadFileToString(private_key_path, |
87 &private_key_contents)) { | 87 &private_key_contents)) { |
88 error_message_ = | 88 error_message_ = |
89 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_READ); | 89 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_READ); |
90 return NULL; | 90 return NULL; |
91 } | 91 } |
92 | 92 |
93 std::string private_key_bytes; | 93 std::string private_key_bytes; |
94 if (!Extension::ParsePEMKeyBytes(private_key_contents, | 94 if (!Extension::ParsePEMKeyBytes(private_key_contents, |
95 &private_key_bytes)) { | 95 &private_key_bytes)) { |
96 error_message_ = | 96 error_message_ = |
97 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_INVALID); | 97 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_INVALID); |
98 return NULL; | 98 return NULL; |
99 } | 99 } |
100 | 100 |
101 return base::RSAPrivateKey::CreateFromPrivateKeyInfo( | 101 return crypto::RSAPrivateKey::CreateFromPrivateKeyInfo( |
102 std::vector<uint8>(private_key_bytes.begin(), private_key_bytes.end())); | 102 std::vector<uint8>(private_key_bytes.begin(), private_key_bytes.end())); |
103 } | 103 } |
104 | 104 |
105 base::RSAPrivateKey* ExtensionCreator::GenerateKey(const FilePath& | 105 crypto::RSAPrivateKey* ExtensionCreator::GenerateKey(const FilePath& |
106 output_private_key_path) { | 106 output_private_key_path) { |
107 scoped_ptr<base::RSAPrivateKey> key_pair( | 107 scoped_ptr<crypto::RSAPrivateKey> key_pair( |
108 base::RSAPrivateKey::Create(kRSAKeySize)); | 108 crypto::RSAPrivateKey::Create(kRSAKeySize)); |
109 if (!key_pair.get()) { | 109 if (!key_pair.get()) { |
110 error_message_ = | 110 error_message_ = |
111 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_GENERATE); | 111 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_GENERATE); |
112 return NULL; | 112 return NULL; |
113 } | 113 } |
114 | 114 |
115 std::vector<uint8> private_key_vector; | 115 std::vector<uint8> private_key_vector; |
116 if (!key_pair->ExportPrivateKey(&private_key_vector)) { | 116 if (!key_pair->ExportPrivateKey(&private_key_vector)) { |
117 error_message_ = | 117 error_message_ = |
118 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_EXPORT); | 118 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_EXPORT); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 if (!Zip(extension_dir, *zip_path, false)) { // no hidden files | 156 if (!Zip(extension_dir, *zip_path, false)) { // no hidden files |
157 error_message_ = | 157 error_message_ = |
158 l10n_util::GetStringUTF8(IDS_EXTENSION_FAILED_DURING_PACKAGING); | 158 l10n_util::GetStringUTF8(IDS_EXTENSION_FAILED_DURING_PACKAGING); |
159 return false; | 159 return false; |
160 } | 160 } |
161 | 161 |
162 return true; | 162 return true; |
163 } | 163 } |
164 | 164 |
165 bool ExtensionCreator::SignZip(const FilePath& zip_path, | 165 bool ExtensionCreator::SignZip(const FilePath& zip_path, |
166 base::RSAPrivateKey* private_key, | 166 crypto::RSAPrivateKey* private_key, |
167 std::vector<uint8>* signature) { | 167 std::vector<uint8>* signature) { |
168 scoped_ptr<base::SignatureCreator> signature_creator( | 168 scoped_ptr<crypto::SignatureCreator> signature_creator( |
169 base::SignatureCreator::Create(private_key)); | 169 crypto::SignatureCreator::Create(private_key)); |
170 ScopedStdioHandle zip_handle(file_util::OpenFile(zip_path, "rb")); | 170 ScopedStdioHandle zip_handle(file_util::OpenFile(zip_path, "rb")); |
171 size_t buffer_size = 1 << 16; | 171 size_t buffer_size = 1 << 16; |
172 scoped_array<uint8> buffer(new uint8[buffer_size]); | 172 scoped_array<uint8> buffer(new uint8[buffer_size]); |
173 int bytes_read = -1; | 173 int bytes_read = -1; |
174 while ((bytes_read = fread(buffer.get(), 1, buffer_size, | 174 while ((bytes_read = fread(buffer.get(), 1, buffer_size, |
175 zip_handle.get())) > 0) { | 175 zip_handle.get())) > 0) { |
176 if (!signature_creator->Update(buffer.get(), bytes_read)) { | 176 if (!signature_creator->Update(buffer.get(), bytes_read)) { |
177 error_message_ = | 177 error_message_ = |
178 l10n_util::GetStringUTF8(IDS_EXTENSION_ERROR_WHILE_SIGNING); | 178 l10n_util::GetStringUTF8(IDS_EXTENSION_ERROR_WHILE_SIGNING); |
179 return false; | 179 return false; |
180 } | 180 } |
181 } | 181 } |
182 zip_handle.Close(); | 182 zip_handle.Close(); |
183 | 183 |
184 signature_creator->Final(signature); | 184 signature_creator->Final(signature); |
185 return true; | 185 return true; |
186 } | 186 } |
187 | 187 |
188 bool ExtensionCreator::WriteCRX(const FilePath& zip_path, | 188 bool ExtensionCreator::WriteCRX(const FilePath& zip_path, |
189 base::RSAPrivateKey* private_key, | 189 crypto::RSAPrivateKey* private_key, |
190 const std::vector<uint8>& signature, | 190 const std::vector<uint8>& signature, |
191 const FilePath& crx_path) { | 191 const FilePath& crx_path) { |
192 if (file_util::PathExists(crx_path)) | 192 if (file_util::PathExists(crx_path)) |
193 file_util::Delete(crx_path, false); | 193 file_util::Delete(crx_path, false); |
194 ScopedStdioHandle crx_handle(file_util::OpenFile(crx_path, "wb")); | 194 ScopedStdioHandle crx_handle(file_util::OpenFile(crx_path, "wb")); |
195 | 195 |
196 std::vector<uint8> public_key; | 196 std::vector<uint8> public_key; |
197 if (!private_key->ExportPublicKey(&public_key)) { | 197 if (!private_key->ExportPublicKey(&public_key)) { |
198 error_message_ = | 198 error_message_ = |
199 l10n_util::GetStringUTF8(IDS_EXTENSION_PUBLIC_KEY_FAILED_TO_EXPORT); | 199 l10n_util::GetStringUTF8(IDS_EXTENSION_PUBLIC_KEY_FAILED_TO_EXPORT); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 const FilePath& crx_path, | 239 const FilePath& crx_path, |
240 const FilePath& private_key_path, | 240 const FilePath& private_key_path, |
241 const FilePath& output_private_key_path) { | 241 const FilePath& output_private_key_path) { |
242 // Check input diretory and read manifest. | 242 // Check input diretory and read manifest. |
243 if (!InitializeInput(extension_dir, private_key_path, | 243 if (!InitializeInput(extension_dir, private_key_path, |
244 output_private_key_path)) { | 244 output_private_key_path)) { |
245 return false; | 245 return false; |
246 } | 246 } |
247 | 247 |
248 // Initialize Key Pair | 248 // Initialize Key Pair |
249 scoped_ptr<base::RSAPrivateKey> key_pair; | 249 scoped_ptr<crypto::RSAPrivateKey> key_pair; |
250 if (!private_key_path.value().empty()) | 250 if (!private_key_path.value().empty()) |
251 key_pair.reset(ReadInputKey(private_key_path)); | 251 key_pair.reset(ReadInputKey(private_key_path)); |
252 else | 252 else |
253 key_pair.reset(GenerateKey(output_private_key_path)); | 253 key_pair.reset(GenerateKey(output_private_key_path)); |
254 if (!key_pair.get()) | 254 if (!key_pair.get()) |
255 return false; | 255 return false; |
256 | 256 |
257 ScopedTempDir temp_dir; | 257 ScopedTempDir temp_dir; |
258 if (!temp_dir.CreateUniqueTempDir()) | 258 if (!temp_dir.CreateUniqueTempDir()) |
259 return false; | 259 return false; |
260 | 260 |
261 // Zip up the extension. | 261 // Zip up the extension. |
262 FilePath zip_path; | 262 FilePath zip_path; |
263 std::vector<uint8> signature; | 263 std::vector<uint8> signature; |
264 bool result = false; | 264 bool result = false; |
265 if (CreateZip(extension_dir, temp_dir.path(), &zip_path) && | 265 if (CreateZip(extension_dir, temp_dir.path(), &zip_path) && |
266 SignZip(zip_path, key_pair.get(), &signature) && | 266 SignZip(zip_path, key_pair.get(), &signature) && |
267 WriteCRX(zip_path, key_pair.get(), signature, crx_path)) { | 267 WriteCRX(zip_path, key_pair.get(), signature, crx_path)) { |
268 result = true; | 268 result = true; |
269 } | 269 } |
270 | 270 |
271 file_util::Delete(zip_path, false); | 271 file_util::Delete(zip_path, false); |
272 return result; | 272 return result; |
273 } | 273 } |
OLD | NEW |