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