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