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