1 // Copyright (c) 2012 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 "chrome/browser/chromeos/settings/owner_key_util.h" 6 7 #include <limits> 8 9 #include "base/file_util.h" 10 #include "base/logging.h" 11 #include "base/path_service.h" 12 #include "base/stl_util.h" 13 #include "chromeos/chromeos_paths.h" 14 #include "crypto/rsa_private_key.h" 15 16 namespace chromeos { 17 18 /////////////////////////////////////////////////////////////////////////// 19 // PublicKey 20 21 PublicKey::PublicKey() { 22 } 23 24 PublicKey::~PublicKey() { 25 } 26 27 /////////////////////////////////////////////////////////////////////////// 28 // PrivateKey 29 30 PrivateKey::PrivateKey(crypto::RSAPrivateKey* key) : key_(key) { 31 } 32 33 PrivateKey::~PrivateKey() { 34 } 35 36 /////////////////////////////////////////////////////////////////////////// 37 // OwnerKeyUtil 38 39 OwnerKeyUtil* OwnerKeyUtil::Create() { 40 base::FilePath owner_key_path; 41 CHECK(PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path)); 42 return new OwnerKeyUtilImpl(owner_key_path); 43 } 44 45 OwnerKeyUtil::OwnerKeyUtil() {} 46 47 OwnerKeyUtil::~OwnerKeyUtil() {} 48 49 /////////////////////////////////////////////////////////////////////////// 50 // OwnerKeyUtilImpl 51 52 OwnerKeyUtilImpl::OwnerKeyUtilImpl(const base::FilePath& key_file) 53 : key_file_(key_file) {} 54 55 OwnerKeyUtilImpl::~OwnerKeyUtilImpl() {} 56 57 bool OwnerKeyUtilImpl::ImportPublicKey(std::vector<uint8>* output) { 58 // Get the file size (must fit in a 32 bit int for NSS). 59 int64 file_size; 60 if (!base::GetFileSize(key_file_, &file_size)) { 61 LOG(ERROR) << "Could not get size of " << key_file_.value(); 62 return false; 63 } 64 if (file_size > static_cast<int64>(std::numeric_limits<int>::max())) { 65 LOG(ERROR) << key_file_.value() << "is " 66 << file_size << "bytes!!! Too big!"; 67 return false; 68 } 69 int32 safe_file_size = static_cast<int32>(file_size); 70 71 output->resize(safe_file_size); 72 73 if (safe_file_size == 0) { 74 LOG(WARNING) << "Public key file is empty. This seems wrong."; 75 return false; 76 } 77 78 // Get the key data off of disk 79 int data_read = base::ReadFile( 80 key_file_, 81 reinterpret_cast<char*>(vector_as_array(output)), 82 safe_file_size); 83 return data_read == safe_file_size; 84 } 85 86 crypto::RSAPrivateKey* OwnerKeyUtilImpl::FindPrivateKey( 87 const std::vector<uint8>& key) { 88 return crypto::RSAPrivateKey::FindFromPublicKeyInfo(key); 89 } 90 91 crypto::RSAPrivateKey* OwnerKeyUtilImpl::FindPrivateKeyInSlot( 92 const std::vector<uint8>& key, 93 PK11SlotInfo* slot) { 94 return crypto::RSAPrivateKey::FindFromPublicKeyInfoInSlot(key, slot); 95 } 96 97 bool OwnerKeyUtilImpl::IsPublicKeyPresent() { 98 return base::PathExists(key_file_); 99 } 100 101 } // namespace chromeos 102