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 | |
| 7 | #include <limits> |
| 8 | |
thestig | 819adcc8 | 2014-09-10 22:24:53 | [diff] [blame] | 9 | #include "base/files/file_util.h" |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 10 | #include "base/logging.h" |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 11 | |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 12 | #include <keythi.h> |
| 13 | #include "crypto/nss_key_util.h" |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 14 | |
| 15 | namespace ownership { |
| 16 | |
| 17 | OwnerKeyUtilImpl::OwnerKeyUtilImpl(const base::FilePath& public_key_file) |
| 18 | : public_key_file_(public_key_file) { |
| 19 | } |
| 20 | |
| 21 | OwnerKeyUtilImpl::~OwnerKeyUtilImpl() { |
| 22 | } |
| 23 | |
| 24 | bool OwnerKeyUtilImpl::ImportPublicKey(std::vector<uint8>* output) { |
| 25 | // Get the file size (must fit in a 32 bit int for NSS). |
| 26 | int64 file_size; |
| 27 | if (!base::GetFileSize(public_key_file_, &file_size)) { |
| 28 | #if defined(OS_CHROMEOS) |
| 29 | LOG(ERROR) << "Could not get size of " << public_key_file_.value(); |
| 30 | #endif // defined(OS_CHROMEOS) |
| 31 | return false; |
| 32 | } |
| 33 | if (file_size > static_cast<int64>(std::numeric_limits<int>::max())) { |
| 34 | LOG(ERROR) << public_key_file_.value() << "is " << file_size |
| 35 | << "bytes!!! Too big!"; |
| 36 | return false; |
| 37 | } |
| 38 | int32 safe_file_size = static_cast<int32>(file_size); |
| 39 | |
| 40 | output->resize(safe_file_size); |
| 41 | |
| 42 | if (safe_file_size == 0) { |
| 43 | LOG(WARNING) << "Public key file is empty. This seems wrong."; |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | // Get the key data off of disk |
| 48 | int data_read = |
| 49 | base::ReadFile(public_key_file_, |
| 50 | reinterpret_cast<char*>(vector_as_array(output)), |
| 51 | safe_file_size); |
| 52 | return data_read == safe_file_size; |
| 53 | } |
| 54 | |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame^] | 55 | crypto::ScopedSECKEYPrivateKey OwnerKeyUtilImpl::FindPrivateKeyInSlot( |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 56 | const std::vector<uint8>& key, |
| 57 | PK11SlotInfo* slot) { |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 58 | if (!slot) |
| 59 | return nullptr; |
| 60 | |
| 61 | crypto::ScopedSECKEYPrivateKey private_key( |
| 62 | crypto::FindNSSKeyFromPublicKeyInfoInSlot(key, slot)); |
| 63 | if (!private_key || SECKEY_GetPrivateKeyType(private_key.get()) != rsaKey) |
| 64 | return nullptr; |
davidben | ee92e38 | 2015-05-26 20:25:45 | [diff] [blame^] | 65 | return private_key.Pass(); |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 66 | } |
ygorshenin | 39e3678 | 2014-08-29 13:09:51 | [diff] [blame] | 67 | |
| 68 | bool OwnerKeyUtilImpl::IsPublicKeyPresent() { |
| 69 | return base::PathExists(public_key_file_); |
| 70 | } |
| 71 | |
| 72 | } // namespace ownership |