| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ownership/owner_key_util_impl.h" | 5 #include "components/ownership/owner_key_util_impl.h" |
| 6 | 6 |
| 7 #include <keythi.h> | 7 #include <keythi.h> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
| 13 #include "build/build_config.h" |
| 13 #include "crypto/nss_key_util.h" | 14 #include "crypto/nss_key_util.h" |
| 14 | 15 |
| 15 namespace ownership { | 16 namespace ownership { |
| 16 | 17 |
| 17 OwnerKeyUtilImpl::OwnerKeyUtilImpl(const base::FilePath& public_key_file) | 18 OwnerKeyUtilImpl::OwnerKeyUtilImpl(const base::FilePath& public_key_file) |
| 18 : public_key_file_(public_key_file) { | 19 : public_key_file_(public_key_file) { |
| 19 } | 20 } |
| 20 | 21 |
| 21 OwnerKeyUtilImpl::~OwnerKeyUtilImpl() { | 22 OwnerKeyUtilImpl::~OwnerKeyUtilImpl() { |
| 22 } | 23 } |
| 23 | 24 |
| 24 bool OwnerKeyUtilImpl::ImportPublicKey(std::vector<uint8>* output) { | 25 bool OwnerKeyUtilImpl::ImportPublicKey(std::vector<uint8_t>* output) { |
| 25 // Get the file size (must fit in a 32 bit int for NSS). | 26 // Get the file size (must fit in a 32 bit int for NSS). |
| 26 int64 file_size; | 27 int64_t file_size; |
| 27 if (!base::GetFileSize(public_key_file_, &file_size)) { | 28 if (!base::GetFileSize(public_key_file_, &file_size)) { |
| 28 #if defined(OS_CHROMEOS) | 29 #if defined(OS_CHROMEOS) |
| 29 LOG_IF(ERROR, base::SysInfo::IsRunningOnChromeOS()) | 30 LOG_IF(ERROR, base::SysInfo::IsRunningOnChromeOS()) |
| 30 << "Could not get size of " << public_key_file_.value(); | 31 << "Could not get size of " << public_key_file_.value(); |
| 31 #endif // defined(OS_CHROMEOS) | 32 #endif // defined(OS_CHROMEOS) |
| 32 return false; | 33 return false; |
| 33 } | 34 } |
| 34 if (file_size > static_cast<int64>(std::numeric_limits<int>::max())) { | 35 if (file_size > static_cast<int64_t>(std::numeric_limits<int>::max())) { |
| 35 LOG(ERROR) << public_key_file_.value() << "is " << file_size | 36 LOG(ERROR) << public_key_file_.value() << "is " << file_size |
| 36 << "bytes!!! Too big!"; | 37 << "bytes!!! Too big!"; |
| 37 return false; | 38 return false; |
| 38 } | 39 } |
| 39 int32 safe_file_size = static_cast<int32>(file_size); | 40 int32_t safe_file_size = static_cast<int32_t>(file_size); |
| 40 | 41 |
| 41 output->resize(safe_file_size); | 42 output->resize(safe_file_size); |
| 42 | 43 |
| 43 if (safe_file_size == 0) { | 44 if (safe_file_size == 0) { |
| 44 LOG(WARNING) << "Public key file is empty. This seems wrong."; | 45 LOG(WARNING) << "Public key file is empty. This seems wrong."; |
| 45 return false; | 46 return false; |
| 46 } | 47 } |
| 47 | 48 |
| 48 // Get the key data off of disk | 49 // Get the key data off of disk |
| 49 int data_read = | 50 int data_read = |
| 50 base::ReadFile(public_key_file_, reinterpret_cast<char*>(output->data()), | 51 base::ReadFile(public_key_file_, reinterpret_cast<char*>(output->data()), |
| 51 safe_file_size); | 52 safe_file_size); |
| 52 return data_read == safe_file_size; | 53 return data_read == safe_file_size; |
| 53 } | 54 } |
| 54 | 55 |
| 55 crypto::ScopedSECKEYPrivateKey OwnerKeyUtilImpl::FindPrivateKeyInSlot( | 56 crypto::ScopedSECKEYPrivateKey OwnerKeyUtilImpl::FindPrivateKeyInSlot( |
| 56 const std::vector<uint8>& key, | 57 const std::vector<uint8_t>& key, |
| 57 PK11SlotInfo* slot) { | 58 PK11SlotInfo* slot) { |
| 58 if (!slot) | 59 if (!slot) |
| 59 return nullptr; | 60 return nullptr; |
| 60 | 61 |
| 61 crypto::ScopedSECKEYPrivateKey private_key( | 62 crypto::ScopedSECKEYPrivateKey private_key( |
| 62 crypto::FindNSSKeyFromPublicKeyInfoInSlot(key, slot)); | 63 crypto::FindNSSKeyFromPublicKeyInfoInSlot(key, slot)); |
| 63 if (!private_key || SECKEY_GetPrivateKeyType(private_key.get()) != rsaKey) | 64 if (!private_key || SECKEY_GetPrivateKeyType(private_key.get()) != rsaKey) |
| 64 return nullptr; | 65 return nullptr; |
| 65 return private_key.Pass(); | 66 return private_key.Pass(); |
| 66 } | 67 } |
| 67 | 68 |
| 68 bool OwnerKeyUtilImpl::IsPublicKeyPresent() { | 69 bool OwnerKeyUtilImpl::IsPublicKeyPresent() { |
| 69 return base::PathExists(public_key_file_); | 70 return base::PathExists(public_key_file_); |
| 70 } | 71 } |
| 71 | 72 |
| 72 } // namespace ownership | 73 } // namespace ownership |
| OLD | NEW |