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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/settings/owner_key_util.h"
9 #include "base/file_util.h"
10 #include "base/logging.h"
11 #include "base/path_service.h"
12 #include "base/stl_util.h"
13 #include "base/sys_info.h"
14 #include "chromeos/chromeos_paths.h"
15 #include "crypto/rsa_private_key.h"
19 ///////////////////////////////////////////////////////////////////////////
22 PublicKey::PublicKey() {
25 PublicKey::~PublicKey() {
28 ///////////////////////////////////////////////////////////////////////////
31 PrivateKey::PrivateKey(crypto::RSAPrivateKey* key) : key_(key) {
34 PrivateKey::~PrivateKey() {
37 ///////////////////////////////////////////////////////////////////////////
40 OwnerKeyUtil* OwnerKeyUtil::Create() {
41 base::FilePath owner_key_path;
42 CHECK(PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path));
43 return new OwnerKeyUtilImpl(owner_key_path);
46 OwnerKeyUtil::OwnerKeyUtil() {}
48 OwnerKeyUtil::~OwnerKeyUtil() {}
50 ///////////////////////////////////////////////////////////////////////////
53 OwnerKeyUtilImpl::OwnerKeyUtilImpl(const base::FilePath& key_file)
54 : key_file_(key_file) {}
56 OwnerKeyUtilImpl::~OwnerKeyUtilImpl() {}
58 bool OwnerKeyUtilImpl::ImportPublicKey(std::vector<uint8>* output) {
59 // Get the file size (must fit in a 32 bit int for NSS).
61 if (!base::GetFileSize(key_file_, &file_size)) {
62 LOG_IF(ERROR, base::SysInfo::IsRunningOnChromeOS())
63 << "Could not get size of " << key_file_.value();
66 if (file_size > static_cast<int64>(std::numeric_limits<int>::max())) {
67 LOG(ERROR) << key_file_.value() << "is "
68 << file_size << "bytes!!! Too big!";
71 int32 safe_file_size = static_cast<int32>(file_size);
73 output->resize(safe_file_size);
75 if (safe_file_size == 0) {
76 LOG(WARNING) << "Public key file is empty. This seems wrong.";
80 // Get the key data off of disk
81 int data_read = base::ReadFile(
83 reinterpret_cast<char*>(vector_as_array(output)),
85 return data_read == safe_file_size;
88 crypto::RSAPrivateKey* OwnerKeyUtilImpl::FindPrivateKeyInSlot(
89 const std::vector<uint8>& key,
91 return crypto::RSAPrivateKey::FindFromPublicKeyInfoInSlot(key, slot);
94 bool OwnerKeyUtilImpl::IsPublicKeyPresent() {
95 return base::PathExists(key_file_);
98 } // namespace chromeos