keygen_handler_nss.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/keygen_handler.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "crypto/nss_crypto_module_delegate.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "crypto/nss_util.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "crypto/nss_util_internal.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "crypto/scoped_nss_types.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/third_party/mozilla_security_manager/nsKeygenHandler.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PSM = Mozilla's Personal Security Manager.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace psm = mozilla_security_manager;
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string KeygenHandler::GenKeyAndSignChallenge() {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Ensure NSS is initialized.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  crypto::EnsureNSSInit();
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  crypto::ScopedPK11Slot slot;
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (crypto_module_delegate_)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    slot = crypto_module_delegate_->RequestSlot().Pass();
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  else
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    slot.reset(crypto::GetPrivateNSSKeySlot());
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!slot.get()) {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Couldn't get private key slot from NSS!";
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return std::string();
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Authenticate to the token.
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (SECSuccess !=
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PK11_Authenticate(
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          slot.get(),
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          PR_TRUE,
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          crypto_module_delegate_ ? crypto_module_delegate_->wincx() : NULL)) {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Couldn't authenticate to private key slot!";
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return std::string();
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return psm::GenKeyAndSignChallenge(key_size_in_bits_, challenge_, url_,
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     slot.get(), stores_key_);
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void KeygenHandler::set_crypto_module_delegate(
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<crypto::NSSCryptoModuleDelegate> delegate) {
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  crypto_module_delegate_ = delegate.Pass();
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
53