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.
5 #include "components/ownership/owner_key_util_impl.h"
10 #include "base/files/file_util.h"
11 #include "base/logging.h"
12 #include "base/system/sys_info.h"
13 #include "build/build_config.h"
14 #include "crypto/nss_key_util.h"
18 OwnerKeyUtilImpl::OwnerKeyUtilImpl(const base::FilePath& public_key_file)
19 : public_key_file_(public_key_file) {
22 OwnerKeyUtilImpl::~OwnerKeyUtilImpl() {
25 bool OwnerKeyUtilImpl::ImportPublicKey(std::vector<uint8_t>* output) {
26 // Get the file size (must fit in a 32 bit int for NSS).
28 if (!base::GetFileSize(public_key_file_, &file_size)) {
29 #if defined(OS_CHROMEOS)
30 LOG_IF(ERROR, base::SysInfo::IsRunningOnChromeOS())
31 << "Could not get size of " << public_key_file_.value();
32 #endif // defined(OS_CHROMEOS)
35 if (file_size > static_cast<int64_t>(std::numeric_limits<int>::max())) {
36 LOG(ERROR) << public_key_file_.value() << "is " << file_size
37 << "bytes!!! Too big!";
40 int32_t safe_file_size = static_cast<int32_t>(file_size);
42 output->resize(safe_file_size);
44 if (safe_file_size == 0) {
45 LOG(WARNING) << "Public key file is empty. This seems wrong.";
49 // Get the key data off of disk
51 base::ReadFile(public_key_file_, reinterpret_cast<char*>(output->data()),
53 return data_read == safe_file_size;
56 crypto::ScopedSECKEYPrivateKey OwnerKeyUtilImpl::FindPrivateKeyInSlot(
57 const std::vector<uint8_t>& key,
62 crypto::ScopedSECKEYPrivateKey private_key(
63 crypto::FindNSSKeyFromPublicKeyInfoInSlot(key, slot));
64 if (!private_key || SECKEY_GetPrivateKeyType(private_key.get()) != rsaKey)
69 bool OwnerKeyUtilImpl::IsPublicKeyPresent() {
70 return base::PathExists(public_key_file_);
73 } // namespace ownership