OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/net/keygen_handler.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/threading/worker_pool.h" |
| 11 #include "chrome/browser/net/nss_slot_factory.h" |
| 12 #include "chrome/browser/ui/crypto_module_password_dialog.h" |
| 13 #include "crypto/crypto_module_blocking_password_delegate.h" |
| 14 #include "crypto/scoped_nss_types.h" |
| 15 #include "net/base/keygen_handler.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 void KeygenOnWorkerThread( |
| 20 int key_size_in_bits, |
| 21 const std::string& challenge, |
| 22 const GURL& url, |
| 23 bool stores_key, |
| 24 crypto::ScopedPK11Slot slot, |
| 25 std::string* result) { |
| 26 scoped_ptr<crypto::CryptoModuleBlockingPasswordDelegate> |
| 27 crypto_module_password_delegate( |
| 28 chrome::NewCryptoModuleBlockingDialogDelegate( |
| 29 chrome::kCryptoModulePasswordKeygen, url.host())); |
| 30 // Authenticate to the token. |
| 31 if (SECSuccess != PK11_Authenticate(slot.get(), PR_TRUE, |
| 32 crypto_module_password_delegate.get())) { |
| 33 LOG(ERROR) << "Couldn't authenticate to private key slot!"; |
| 34 } |
| 35 |
| 36 net::KeygenHandler handler(key_size_in_bits, challenge, url); |
| 37 handler.set_stores_key(stores_key); |
| 38 handler.set_key_slot(slot.Pass()); |
| 39 *result = handler.GenKeyAndSignChallenge(); |
| 40 } |
| 41 |
| 42 void GenerateKeyInSlot(int key_size_in_bits, |
| 43 const std::string& challenge, |
| 44 const GURL& url, |
| 45 bool stores_key, |
| 46 const base::Callback<void(const std::string*)>& callback, |
| 47 crypto::ScopedPK11Slot slot) { |
| 48 if (!slot.get()) { |
| 49 LOG(ERROR) << "Couldn't get private key slot from NSS!"; |
| 50 callback.Run(NULL); |
| 51 return; |
| 52 } |
| 53 LOG(WARNING) << "keygen private slot name: " << PK11_GetSlotName(slot.get()) |
| 54 << " token name: " << PK11_GetTokenName(slot.get()) |
| 55 << " slot id: " << PK11_GetSlotID(slot.get()); |
| 56 |
| 57 VLOG(1) << "Dispatching keygen task to worker pool."; |
| 58 std::string* result(new std::string()); |
| 59 // Dispatch to worker pool, so we do not block the IO thread. |
| 60 if (!base::WorkerPool::PostTaskAndReply( |
| 61 FROM_HERE, |
| 62 base::Bind(&KeygenOnWorkerThread, |
| 63 key_size_in_bits, |
| 64 challenge, |
| 65 url, |
| 66 stores_key, |
| 67 base::Passed(&slot), |
| 68 result), |
| 69 base::Bind(callback, base::Owned(result)), |
| 70 true)) { |
| 71 NOTREACHED() << "Failed to dispatch keygen task to worker pool"; |
| 72 callback.Run(NULL); |
| 73 return; |
| 74 } |
| 75 } |
| 76 |
| 77 } // namespace |
| 78 |
| 79 namespace chrome_browser_net { |
| 80 |
| 81 void GenerateKey(content::ResourceContext* context, |
| 82 int key_size_in_bits, |
| 83 const std::string& challenge, |
| 84 const GURL& url, |
| 85 bool stores_key, |
| 86 const base::Callback<void(const std::string*)>& callback) { |
| 87 // TODO(mattm): allow choosing which slot to generate and store the key. |
| 88 OnPrivateNSSKeySlotForResourceContextReady(context, |
| 89 base::Bind(&GenerateKeyInSlot, |
| 90 key_size_in_bits, |
| 91 challenge, |
| 92 url, |
| 93 stores_key, |
| 94 callback)); |
| 95 } |
| 96 |
| 97 } // namespace chrome_browser_net |
OLD | NEW |