cert_test_util_nss.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2014 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 "net/test/cert_test_util.h"
6
7#include "base/file_util.h"
8#include "base/files/file_path.h"
9#include "base/path_service.h"
10#include "crypto/rsa_private_key.h"
11
12namespace net {
13
14scoped_ptr<crypto::RSAPrivateKey> ImportSensitiveKeyFromFile(
15    const base::FilePath& dir,
16    const std::string& key_filename,
17    PK11SlotInfo* slot) {
18  base::FilePath key_path = dir.AppendASCII(key_filename);
19  std::string key_pkcs8;
20  bool success = base::ReadFileToString(key_path, &key_pkcs8);
21  if (!success) {
22    LOG(ERROR) << "Failed to read file " << key_path.value();
23    return scoped_ptr<crypto::RSAPrivateKey>();
24  }
25
26  const uint8* key_pkcs8_begin =
27      reinterpret_cast<const uint8*>(key_pkcs8.data());
28  std::vector<uint8> key_vector(key_pkcs8_begin,
29                                key_pkcs8_begin + key_pkcs8.length());
30
31  scoped_ptr<crypto::RSAPrivateKey> private_key(
32      crypto::RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo(slot,
33                                                               key_vector));
34  LOG_IF(ERROR, !private_key) << "Could not create key from file "
35                              << key_path.value();
36  return private_key.Pass();
37}
38
39}  // namespace net
40