blob: 0d36ede6e6608bfb63b3c983cae69a057ce42d1e [file] [log] [blame]
ygorshenin39e36782014-08-29 13:09:511// 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 "components/ownership/owner_key_util_impl.h"
6
stevenjbbf16eec2015-07-30 17:23:187#include <keythi.h>
ygorshenin39e36782014-08-29 13:09:518#include <limits>
9
thestig819adcc82014-09-10 22:24:5310#include "base/files/file_util.h"
ygorshenin39e36782014-08-29 13:09:5111#include "base/logging.h"
stevenjbbf16eec2015-07-30 17:23:1812#include "base/sys_info.h"
avif57136c12015-12-25 23:27:4513#include "build/build_config.h"
davidben85bad9e2015-05-11 20:20:1014#include "crypto/nss_key_util.h"
ygorshenin39e36782014-08-29 13:09:5115
16namespace ownership {
17
18OwnerKeyUtilImpl::OwnerKeyUtilImpl(const base::FilePath& public_key_file)
19 : public_key_file_(public_key_file) {
20}
21
22OwnerKeyUtilImpl::~OwnerKeyUtilImpl() {
23}
24
avif57136c12015-12-25 23:27:4525bool OwnerKeyUtilImpl::ImportPublicKey(std::vector<uint8_t>* output) {
ygorshenin39e36782014-08-29 13:09:5126 // Get the file size (must fit in a 32 bit int for NSS).
avif57136c12015-12-25 23:27:4527 int64_t file_size;
ygorshenin39e36782014-08-29 13:09:5128 if (!base::GetFileSize(public_key_file_, &file_size)) {
29#if defined(OS_CHROMEOS)
stevenjbbf16eec2015-07-30 17:23:1830 LOG_IF(ERROR, base::SysInfo::IsRunningOnChromeOS())
31 << "Could not get size of " << public_key_file_.value();
ygorshenin39e36782014-08-29 13:09:5132#endif // defined(OS_CHROMEOS)
33 return false;
34 }
avif57136c12015-12-25 23:27:4535 if (file_size > static_cast<int64_t>(std::numeric_limits<int>::max())) {
ygorshenin39e36782014-08-29 13:09:5136 LOG(ERROR) << public_key_file_.value() << "is " << file_size
37 << "bytes!!! Too big!";
38 return false;
39 }
avif57136c12015-12-25 23:27:4540 int32_t safe_file_size = static_cast<int32_t>(file_size);
ygorshenin39e36782014-08-29 13:09:5141
42 output->resize(safe_file_size);
43
44 if (safe_file_size == 0) {
45 LOG(WARNING) << "Public key file is empty. This seems wrong.";
46 return false;
47 }
48
49 // Get the key data off of disk
50 int data_read =
davidbenaa62f382015-11-20 22:10:0151 base::ReadFile(public_key_file_, reinterpret_cast<char*>(output->data()),
ygorshenin39e36782014-08-29 13:09:5152 safe_file_size);
53 return data_read == safe_file_size;
54}
55
davidbenee92e382015-05-26 20:25:4556crypto::ScopedSECKEYPrivateKey OwnerKeyUtilImpl::FindPrivateKeyInSlot(
avif57136c12015-12-25 23:27:4557 const std::vector<uint8_t>& key,
ygorshenin39e36782014-08-29 13:09:5158 PK11SlotInfo* slot) {
davidben85bad9e2015-05-11 20:20:1059 if (!slot)
60 return nullptr;
61
62 crypto::ScopedSECKEYPrivateKey private_key(
63 crypto::FindNSSKeyFromPublicKeyInfoInSlot(key, slot));
64 if (!private_key || SECKEY_GetPrivateKeyType(private_key.get()) != rsaKey)
65 return nullptr;
davidbenee92e382015-05-26 20:25:4566 return private_key.Pass();
ygorshenin39e36782014-08-29 13:09:5167}
ygorshenin39e36782014-08-29 13:09:5168
69bool OwnerKeyUtilImpl::IsPublicKeyPresent() {
70 return base::PathExists(public_key_file_);
71}
72
73} // namespace ownership