OLD | NEW |
(Empty) | |
| 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.h" |
| 6 |
| 7 #include <limits> |
| 8 |
| 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/sys_info.h" |
| 12 |
| 13 namespace ownership { |
| 14 |
| 15 /////////////////////////////////////////////////////////////////////////// |
| 16 // PublicKey |
| 17 |
| 18 PublicKey::PublicKey() { |
| 19 } |
| 20 |
| 21 PublicKey::~PublicKey() { |
| 22 } |
| 23 |
| 24 /////////////////////////////////////////////////////////////////////////// |
| 25 // PrivateKey |
| 26 |
| 27 PrivateKey::PrivateKey(crypto::RSAPrivateKey* key) : key_(key) { |
| 28 } |
| 29 |
| 30 PrivateKey::~PrivateKey() { |
| 31 } |
| 32 |
| 33 /////////////////////////////////////////////////////////////////////////// |
| 34 // OwnerKeyUtil |
| 35 |
| 36 OwnerKeyUtil::OwnerKeyUtil(const base::FilePath& public_key_file) |
| 37 : public_key_file_(public_key_file) { |
| 38 } |
| 39 |
| 40 OwnerKeyUtil::~OwnerKeyUtil() { |
| 41 } |
| 42 |
| 43 bool OwnerKeyUtil::ImportPublicKey(std::vector<uint8>* output) { |
| 44 // Get the file size (must fit in a 32 bit int for NSS). |
| 45 int64 file_size; |
| 46 if (!base::GetFileSize(public_key_file_, &file_size)) { |
| 47 #if defined(OS_CHROMEOS) |
| 48 LOG_IF(ERROR, base::SysInfo::IsRunningOnChromeOS()) |
| 49 << "Could not get size of " << public_key_file_.value(); |
| 50 #endif // defined(OS_CHROMEOS) |
| 51 return false; |
| 52 } |
| 53 if (file_size > static_cast<int64>(std::numeric_limits<int>::max())) { |
| 54 LOG(ERROR) << public_key_file_.value() << "is " << file_size |
| 55 << "bytes!!! Too big!"; |
| 56 return false; |
| 57 } |
| 58 int32 safe_file_size = static_cast<int32>(file_size); |
| 59 |
| 60 output->resize(safe_file_size); |
| 61 |
| 62 if (safe_file_size == 0) { |
| 63 LOG(WARNING) << "Public key file is empty. This seems wrong."; |
| 64 return false; |
| 65 } |
| 66 |
| 67 // Get the key data off of disk |
| 68 int data_read = |
| 69 base::ReadFile(public_key_file_, |
| 70 reinterpret_cast<char*>(vector_as_array(output)), |
| 71 safe_file_size); |
| 72 return data_read == safe_file_size; |
| 73 } |
| 74 |
| 75 #if defined(USE_NSS) |
| 76 crypto::RSAPrivateKey* OwnerKeyUtil::FindPrivateKeyInSlot( |
| 77 const std::vector<uint8>& key, |
| 78 PK11SlotInfo* slot) { |
| 79 return crypto::RSAPrivateKey::FindFromPublicKeyInfoInSlot(key, slot); |
| 80 } |
| 81 #endif // defined(USE_NSS) |
| 82 |
| 83 bool OwnerKeyUtil::IsPublicKeyPresent() { |
| 84 return base::PathExists(public_key_file_); |
| 85 } |
| 86 |
| 87 } // namespace ownership |
OLD | NEW |