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 16namespace chromeos { 17 18/////////////////////////////////////////////////////////////////////////// 19// OwnerKeyUtil 20 21OwnerKeyUtil* OwnerKeyUtil::Create() { 22 base::FilePath owner_key_path; 23 CHECK(PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path)); 24 return new OwnerKeyUtilImpl(owner_key_path); 25} 26 27OwnerKeyUtil::OwnerKeyUtil() {} 28 29OwnerKeyUtil::~OwnerKeyUtil() {} 30 31/////////////////////////////////////////////////////////////////////////// 32// OwnerKeyUtilImpl 33 34OwnerKeyUtilImpl::OwnerKeyUtilImpl(const base::FilePath& key_file) 35 : key_file_(key_file) {} 36 37OwnerKeyUtilImpl::~OwnerKeyUtilImpl() {} 38 39bool OwnerKeyUtilImpl::ImportPublicKey(std::vector<uint8>* output) { 40 // Get the file size (must fit in a 32 bit int for NSS). 41 int64 file_size; 42 if (!file_util::GetFileSize(key_file_, &file_size)) { 43 LOG(ERROR) << "Could not get size of " << key_file_.value(); 44 return false; 45 } 46 if (file_size > static_cast<int64>(std::numeric_limits<int>::max())) { 47 LOG(ERROR) << key_file_.value() << "is " 48 << file_size << "bytes!!! Too big!"; 49 return false; 50 } 51 int32 safe_file_size = static_cast<int32>(file_size); 52 53 output->resize(safe_file_size); 54 55 if (safe_file_size == 0) { 56 LOG(WARNING) << "Public key file is empty. This seems wrong."; 57 return false; 58 } 59 60 // Get the key data off of disk 61 int data_read = file_util::ReadFile( 62 key_file_, 63 reinterpret_cast<char*>(vector_as_array(output)), 64 safe_file_size); 65 return data_read == safe_file_size; 66} 67 68crypto::RSAPrivateKey* OwnerKeyUtilImpl::FindPrivateKey( 69 const std::vector<uint8>& key) { 70 return crypto::RSAPrivateKey::FindFromPublicKeyInfo(key); 71} 72 73bool OwnerKeyUtilImpl::IsPublicKeyPresent() { 74 return base::PathExists(key_file_); 75} 76 77} // namespace chromeos 78