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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_INVALID); | 141 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_INVALID); |
142 return NULL; | 142 return NULL; |
143 } | 143 } |
144 | 144 |
145 return crypto::RSAPrivateKey::CreateFromPrivateKeyInfo( | 145 return crypto::RSAPrivateKey::CreateFromPrivateKeyInfo( |
146 std::vector<uint8_t>(private_key_bytes.begin(), private_key_bytes.end())); | 146 std::vector<uint8_t>(private_key_bytes.begin(), private_key_bytes.end())); |
147 } | 147 } |
148 | 148 |
149 crypto::RSAPrivateKey* ExtensionCreator::GenerateKey(const base::FilePath& | 149 crypto::RSAPrivateKey* ExtensionCreator::GenerateKey(const base::FilePath& |
150 output_private_key_path) { | 150 output_private_key_path) { |
151 scoped_ptr<crypto::RSAPrivateKey> key_pair( | 151 std::unique_ptr<crypto::RSAPrivateKey> key_pair( |
152 crypto::RSAPrivateKey::Create(kRSAKeySize)); | 152 crypto::RSAPrivateKey::Create(kRSAKeySize)); |
153 if (!key_pair) { | 153 if (!key_pair) { |
154 error_message_ = | 154 error_message_ = |
155 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_GENERATE); | 155 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_FAILED_TO_GENERATE); |
156 return NULL; | 156 return NULL; |
157 } | 157 } |
158 | 158 |
159 std::vector<uint8_t> private_key_vector; | 159 std::vector<uint8_t> private_key_vector; |
160 if (!key_pair->ExportPrivateKey(&private_key_vector)) { | 160 if (!key_pair->ExportPrivateKey(&private_key_vector)) { |
161 error_message_ = | 161 error_message_ = |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 l10n_util::GetStringUTF8(IDS_EXTENSION_FAILED_DURING_PACKAGING); | 205 l10n_util::GetStringUTF8(IDS_EXTENSION_FAILED_DURING_PACKAGING); |
206 return false; | 206 return false; |
207 } | 207 } |
208 | 208 |
209 return true; | 209 return true; |
210 } | 210 } |
211 | 211 |
212 bool ExtensionCreator::SignZip(const base::FilePath& zip_path, | 212 bool ExtensionCreator::SignZip(const base::FilePath& zip_path, |
213 crypto::RSAPrivateKey* private_key, | 213 crypto::RSAPrivateKey* private_key, |
214 std::vector<uint8_t>* signature) { | 214 std::vector<uint8_t>* signature) { |
215 scoped_ptr<crypto::SignatureCreator> signature_creator( | 215 std::unique_ptr<crypto::SignatureCreator> signature_creator( |
216 crypto::SignatureCreator::Create(private_key, | 216 crypto::SignatureCreator::Create(private_key, |
217 crypto::SignatureCreator::SHA1)); | 217 crypto::SignatureCreator::SHA1)); |
218 base::ScopedFILE zip_handle(base::OpenFile(zip_path, "rb")); | 218 base::ScopedFILE zip_handle(base::OpenFile(zip_path, "rb")); |
219 size_t buffer_size = 1 << 16; | 219 size_t buffer_size = 1 << 16; |
220 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | 220 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
221 int bytes_read = -1; | 221 int bytes_read = -1; |
222 while ((bytes_read = fread(buffer.get(), 1, buffer_size, | 222 while ((bytes_read = fread(buffer.get(), 1, buffer_size, |
223 zip_handle.get())) > 0) { | 223 zip_handle.get())) > 0) { |
224 if (!signature_creator->Update(buffer.get(), bytes_read)) { | 224 if (!signature_creator->Update(buffer.get(), bytes_read)) { |
225 error_message_ = | 225 error_message_ = |
226 l10n_util::GetStringUTF8(IDS_EXTENSION_ERROR_WHILE_SIGNING); | 226 l10n_util::GetStringUTF8(IDS_EXTENSION_ERROR_WHILE_SIGNING); |
227 return false; | 227 return false; |
228 } | 228 } |
229 } | 229 } |
230 zip_handle.reset(); | 230 zip_handle.reset(); |
(...skipping 15 matching lines...) Expand all Loading... |
246 base::ScopedFILE crx_handle(base::OpenFile(crx_path, "wb")); | 246 base::ScopedFILE crx_handle(base::OpenFile(crx_path, "wb")); |
247 if (!crx_handle.get()) { | 247 if (!crx_handle.get()) { |
248 error_message_ = l10n_util::GetStringUTF8(IDS_EXTENSION_SHARING_VIOLATION); | 248 error_message_ = l10n_util::GetStringUTF8(IDS_EXTENSION_SHARING_VIOLATION); |
249 return false; | 249 return false; |
250 } | 250 } |
251 | 251 |
252 std::vector<uint8_t> public_key; | 252 std::vector<uint8_t> public_key; |
253 CHECK(private_key->ExportPublicKey(&public_key)); | 253 CHECK(private_key->ExportPublicKey(&public_key)); |
254 | 254 |
255 crx_file::CrxFile::Error error; | 255 crx_file::CrxFile::Error error; |
256 scoped_ptr<crx_file::CrxFile> crx( | 256 std::unique_ptr<crx_file::CrxFile> crx( |
257 crx_file::CrxFile::Create(public_key.size(), signature.size(), &error)); | 257 crx_file::CrxFile::Create(public_key.size(), signature.size(), &error)); |
258 if (!crx) { | 258 if (!crx) { |
259 LOG(ERROR) << "cannot create CrxFileHeader: " << error; | 259 LOG(ERROR) << "cannot create CrxFileHeader: " << error; |
260 } | 260 } |
261 const crx_file::CrxFile::Header header = crx->header(); | 261 const crx_file::CrxFile::Header header = crx->header(); |
262 | 262 |
263 if (fwrite(&header, sizeof(header), 1, crx_handle.get()) != 1) { | 263 if (fwrite(&header, sizeof(header), 1, crx_handle.get()) != 1) { |
264 PLOG(ERROR) << "fwrite failed to write header"; | 264 PLOG(ERROR) << "fwrite failed to write header"; |
265 } | 265 } |
266 if (fwrite(&public_key.front(), sizeof(uint8_t), public_key.size(), | 266 if (fwrite(&public_key.front(), sizeof(uint8_t), public_key.size(), |
267 crx_handle.get()) != public_key.size()) { | 267 crx_handle.get()) != public_key.size()) { |
268 PLOG(ERROR) << "fwrite failed to write public_key.front"; | 268 PLOG(ERROR) << "fwrite failed to write public_key.front"; |
269 } | 269 } |
270 if (fwrite(&signature.front(), sizeof(uint8_t), signature.size(), | 270 if (fwrite(&signature.front(), sizeof(uint8_t), signature.size(), |
271 crx_handle.get()) != signature.size()) { | 271 crx_handle.get()) != signature.size()) { |
272 PLOG(ERROR) << "fwrite failed to write signature.front"; | 272 PLOG(ERROR) << "fwrite failed to write signature.front"; |
273 } | 273 } |
274 | 274 |
275 size_t buffer_size = 1 << 16; | 275 size_t buffer_size = 1 << 16; |
276 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | 276 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
277 size_t bytes_read = 0; | 277 size_t bytes_read = 0; |
278 base::ScopedFILE zip_handle(base::OpenFile(zip_path, "rb")); | 278 base::ScopedFILE zip_handle(base::OpenFile(zip_path, "rb")); |
279 while ((bytes_read = fread(buffer.get(), 1, buffer_size, | 279 while ((bytes_read = fread(buffer.get(), 1, buffer_size, |
280 zip_handle.get())) > 0) { | 280 zip_handle.get())) > 0) { |
281 if (fwrite(buffer.get(), sizeof(char), bytes_read, crx_handle.get()) != | 281 if (fwrite(buffer.get(), sizeof(char), bytes_read, crx_handle.get()) != |
282 bytes_read) { | 282 bytes_read) { |
283 PLOG(ERROR) << "fwrite failed to write buffer"; | 283 PLOG(ERROR) << "fwrite failed to write buffer"; |
284 } | 284 } |
285 } | 285 } |
286 | 286 |
287 return true; | 287 return true; |
288 } | 288 } |
289 | 289 |
290 bool ExtensionCreator::Run(const base::FilePath& extension_dir, | 290 bool ExtensionCreator::Run(const base::FilePath& extension_dir, |
291 const base::FilePath& crx_path, | 291 const base::FilePath& crx_path, |
292 const base::FilePath& private_key_path, | 292 const base::FilePath& private_key_path, |
293 const base::FilePath& output_private_key_path, | 293 const base::FilePath& output_private_key_path, |
294 int run_flags) { | 294 int run_flags) { |
295 // Check input diretory and read manifest. | 295 // Check input diretory and read manifest. |
296 if (!InitializeInput(extension_dir, crx_path, private_key_path, | 296 if (!InitializeInput(extension_dir, crx_path, private_key_path, |
297 output_private_key_path, run_flags)) { | 297 output_private_key_path, run_flags)) { |
298 return false; | 298 return false; |
299 } | 299 } |
300 | 300 |
301 // Initialize Key Pair | 301 // Initialize Key Pair |
302 scoped_ptr<crypto::RSAPrivateKey> key_pair; | 302 std::unique_ptr<crypto::RSAPrivateKey> key_pair; |
303 if (!private_key_path.value().empty()) | 303 if (!private_key_path.value().empty()) |
304 key_pair.reset(ReadInputKey(private_key_path)); | 304 key_pair.reset(ReadInputKey(private_key_path)); |
305 else | 305 else |
306 key_pair.reset(GenerateKey(output_private_key_path)); | 306 key_pair.reset(GenerateKey(output_private_key_path)); |
307 if (!key_pair) | 307 if (!key_pair) |
308 return false; | 308 return false; |
309 | 309 |
310 // Perform some extra validation by loading the extension. | 310 // Perform some extra validation by loading the extension. |
311 // TODO(aa): Can this go before creating the key pair? This would mean not | 311 // TODO(aa): Can this go before creating the key pair? This would mean not |
312 // passing ID into LoadExtension which seems OK. | 312 // passing ID into LoadExtension which seems OK. |
(...skipping 12 matching lines...) Expand all Loading... |
325 SignZip(zip_path, key_pair.get(), &signature) && | 325 SignZip(zip_path, key_pair.get(), &signature) && |
326 WriteCRX(zip_path, key_pair.get(), signature, crx_path)) { | 326 WriteCRX(zip_path, key_pair.get(), signature, crx_path)) { |
327 result = true; | 327 result = true; |
328 } | 328 } |
329 | 329 |
330 base::DeleteFile(zip_path, false); | 330 base::DeleteFile(zip_path, false); |
331 return result; | 331 return result; |
332 } | 332 } |
333 | 333 |
334 } // namespace extensions | 334 } // namespace extensions |
OLD | NEW |