Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1432)

Side by Side Diff: components/ownership/owner_key_util.cc

Issue 494093002: OwnerKeyUtil is moved to components/ownership. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added forward declarations of RSAPrivateKey and PK11SlotInfoStr. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.h"
6
7 #include <limits>
8
9 #include "base/file_util.h"
10 #include "base/logging.h"
11 #include "base/sys_info.h"
12 #include "crypto/rsa_private_key.h"
13
14 namespace ownership {
15
16 ///////////////////////////////////////////////////////////////////////////
17 // PublicKey
18
19 PublicKey::PublicKey() {
20 }
21
22 PublicKey::~PublicKey() {
23 }
24
25 ///////////////////////////////////////////////////////////////////////////
26 // PrivateKey
27
28 PrivateKey::PrivateKey(crypto::RSAPrivateKey* key) : key_(key) {
29 }
30
31 PrivateKey::~PrivateKey() {
32 }
33
34 ///////////////////////////////////////////////////////////////////////////
35 // OwnerKeyUtil
36
37 OwnerKeyUtil::OwnerKeyUtil(const base::FilePath& public_key_file)
38 : public_key_file_(public_key_file) {
39 }
40
41 OwnerKeyUtil::~OwnerKeyUtil() {
42 }
43
44 bool OwnerKeyUtil::ImportPublicKey(std::vector<uint8>* output) {
45 // Get the file size (must fit in a 32 bit int for NSS).
46 int64 file_size;
47 if (!base::GetFileSize(public_key_file_, &file_size)) {
48 #if defined(OS_CHROMEOS)
49 LOG_IF(ERROR, base::SysInfo::IsRunningOnChromeOS())
50 << "Could not get size of " << public_key_file_.value();
51 #endif // defined(OS_CHROMEOS)
52 return false;
53 }
54 if (file_size > static_cast<int64>(std::numeric_limits<int>::max())) {
55 LOG(ERROR) << public_key_file_.value() << "is " << file_size
56 << "bytes!!! Too big!";
57 return false;
58 }
59 int32 safe_file_size = static_cast<int32>(file_size);
60
61 output->resize(safe_file_size);
62
63 if (safe_file_size == 0) {
64 LOG(WARNING) << "Public key file is empty. This seems wrong.";
65 return false;
66 }
67
68 // Get the key data off of disk
69 int data_read =
70 base::ReadFile(public_key_file_,
71 reinterpret_cast<char*>(vector_as_array(output)),
72 safe_file_size);
73 return data_read == safe_file_size;
74 }
75
76 #if defined(USE_NSS)
77 crypto::RSAPrivateKey* OwnerKeyUtil::FindPrivateKeyInSlot(
78 const std::vector<uint8>& key,
79 PK11SlotInfo* slot) {
80 return crypto::RSAPrivateKey::FindFromPublicKeyInfoInSlot(key, slot);
81 }
82 #endif // defined(USE_NSS)
83
84 bool OwnerKeyUtil::IsPublicKeyPresent() {
85 return base::PathExists(public_key_file_);
86 }
87
88 } // namespace ownership
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698