summaryrefslogtreecommitdiff
path: root/chromium/net/base/keygen_handler_nss.cc
blob: ad0f0ebe6722c4066ec276bc046b4e627023fa01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/keygen_handler.h"

#include "base/logging.h"
#include "crypto/nss_crypto_module_delegate.h"
#include "crypto/nss_util.h"
#include "crypto/nss_util_internal.h"
#include "crypto/scoped_nss_types.h"
#include "net/third_party/mozilla_security_manager/nsKeygenHandler.h"

// PSM = Mozilla's Personal Security Manager.
namespace psm = mozilla_security_manager;

namespace net {

std::string KeygenHandler::GenKeyAndSignChallenge() {
  // Ensure NSS is initialized.
  crypto::EnsureNSSInit();

  crypto::ScopedPK11Slot slot;
  if (crypto_module_delegate_)
    slot = crypto_module_delegate_->RequestSlot().Pass();
  else
    slot.reset(crypto::GetPrivateNSSKeySlot());
  if (!slot.get()) {
    LOG(ERROR) << "Couldn't get private key slot from NSS!";
    return std::string();
  }

  // Authenticate to the token.
  if (SECSuccess !=
      PK11_Authenticate(
          slot.get(),
          PR_TRUE,
          crypto_module_delegate_ ? crypto_module_delegate_->wincx() : NULL)) {
    LOG(ERROR) << "Couldn't authenticate to private key slot!";
    return std::string();
  }

  return psm::GenKeyAndSignChallenge(key_size_in_bits_, challenge_, url_,
                                     slot.get(), stores_key_);
}

void KeygenHandler::set_crypto_module_delegate(
      scoped_ptr<crypto::NSSCryptoModuleDelegate> delegate) {
  crypto_module_delegate_ = delegate.Pass();
}

}  // namespace net