ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 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 "components/ownership/owner_key_util_impl.h" |
| 6 | |
stevenjb | bf16eec | 2015-07-30 17:23:18 | [diff] [blame] | 7 | #include <keythi.h> |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 8 | #include <limits> |
| 9 | |
thestig | 819adcc8 | 2014-09-10 22:24:53 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 11 | #include "base/logging.h" |
Sebastien Marchand | 75a7cdf | 2018-11-13 23:47:03 | [diff] [blame] | 12 | #include "base/system/sys_info.h" |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 13 | #include "build/build_config.h" |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 14 | #include "crypto/nss_key_util.h" |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 15 | |
| 16 | namespace ownership { |
| 17 | |
| 18 | OwnerKeyUtilImpl::OwnerKeyUtilImpl(const base::FilePath& public_key_file) |
| 19 | : public_key_file_(public_key_file) { |
| 20 | } |
| 21 | |
| 22 | OwnerKeyUtilImpl::~OwnerKeyUtilImpl() { |
| 23 | } |
| 24 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 25 | bool OwnerKeyUtilImpl::ImportPublicKey(std::vector<uint8_t>* output) { |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 26 | // Get the file size (must fit in a 32 bit int for NSS). |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 27 | int64_t file_size; |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 28 | if (!base::GetFileSize(public_key_file_, &file_size)) { |
| 29 | #if defined(OS_CHROMEOS) |
stevenjb | bf16eec | 2015-07-30 17:23:18 | [diff] [blame] | 30 | LOG_IF(ERROR, base::SysInfo::IsRunningOnChromeOS()) |
| 31 | << "Could not get size of " << public_key_file_.value(); |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 32 | #endif // defined(OS_CHROMEOS) |
| 33 | return false; |
| 34 | } |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 35 | if (file_size > static_cast<int64_t>(std::numeric_limits<int>::max())) { |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 36 | LOG(ERROR) << public_key_file_.value() << "is " << file_size |
| 37 | << "bytes!!! Too big!"; |
| 38 | return false; |
| 39 | } |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 40 | int32_t safe_file_size = static_cast<int32_t>(file_size); |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 41 | |
| 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 = |
davidben | aa62f38 | 2015-11-20 22:10:01 | [diff] [blame] | 51 | base::ReadFile(public_key_file_, reinterpret_cast<char*>(output->data()), |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 52 | safe_file_size); |
| 53 | return data_read == safe_file_size; |
| 54 | } |
| 55 | |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame] | 56 | crypto::ScopedSECKEYPrivateKey OwnerKeyUtilImpl::FindPrivateKeyInSlot( |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 57 | const std::vector<uint8_t>& key, |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 58 | PK11SlotInfo* slot) { |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 59 | 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; |
dcheng | dc5f124 | 2015-12-31 04:54:47 | [diff] [blame] | 66 | return private_key; |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 67 | } |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 68 | |
| 69 | bool OwnerKeyUtilImpl::IsPublicKeyPresent() { |
| 70 | return base::PathExists(public_key_file_); |
| 71 | } |
| 72 | |
| 73 | } // namespace ownership |