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

Side by Side Diff: remoting/host/host_key_pair.cc

Issue 12316083: Move HostKeyPair into protocol::KeyPair. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebase Created 7 years, 9 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
« no previous file with comments | « remoting/host/host_key_pair.h ('k') | remoting/host/host_key_pair_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "remoting/host/host_key_pair.h"
6
7 #include <limits>
8 #include <string>
9 #include <vector>
10
11 #include "base/base64.h"
12 #include "base/logging.h"
13 #include "base/rand_util.h"
14 #include "base/time.h"
15 #include "crypto/rsa_private_key.h"
16 #include "crypto/signature_creator.h"
17 #include "net/base/x509_certificate.h"
18 #include "remoting/host/host_config.h"
19
20 namespace remoting {
21
22 HostKeyPair::HostKeyPair() { }
23
24 HostKeyPair::~HostKeyPair() { }
25
26 void HostKeyPair::Generate() {
27 key_.reset(crypto::RSAPrivateKey::Create(2048));
28 }
29
30 bool HostKeyPair::LoadFromString(const std::string& key_base64) {
31 std::string key_str;
32 if (!base::Base64Decode(key_base64, &key_str)) {
33 LOG(ERROR) << "Failed to decode private key.";
34 return false;
35 }
36
37 std::vector<uint8> key_buf(key_str.begin(), key_str.end());
38 key_.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf));
39 if (key_.get() == NULL) {
40 LOG(ERROR) << "Invalid private key.";
41 return false;
42 }
43
44 return true;
45 }
46
47 bool HostKeyPair::Load(const HostConfig& host_config) {
48 std::string key_base64;
49 if (!host_config.GetString(kPrivateKeyConfigPath, &key_base64)) {
50 LOG(ERROR) << "Private key wasn't found in the config file.";
51 return false;
52 }
53 return LoadFromString(key_base64);
54 }
55
56 void HostKeyPair::Save(MutableHostConfig* host_config) {
57 host_config->SetString(kPrivateKeyConfigPath, GetAsString());
58 }
59
60 std::string HostKeyPair::GetAsString() const {
61 // Check that the key initialized.
62 DCHECK(key_.get() != NULL);
63
64 std::vector<uint8> key_buf;
65 key_->ExportPrivateKey(&key_buf);
66 std::string key_str(key_buf.begin(), key_buf.end());
67 std::string key_base64;
68 if (!base::Base64Encode(key_str, &key_base64)) {
69 LOG(FATAL) << "Base64Encode failed";
70 }
71 return key_base64;
72 }
73
74 std::string HostKeyPair::GetPublicKey() const {
75 std::vector<uint8> public_key;
76 key_->ExportPublicKey(&public_key);
77 std::string public_key_str(public_key.begin(), public_key.end());
78 std::string public_key_base64;
79 base::Base64Encode(public_key_str, &public_key_base64);
80 return public_key_base64;
81 }
82
83 std::string HostKeyPair::GetSignature(const std::string& message) const {
84 scoped_ptr<crypto::SignatureCreator> signature_creator(
85 crypto::SignatureCreator::Create(key_.get()));
86 signature_creator->Update(reinterpret_cast<const uint8*>(message.c_str()),
87 message.length());
88 std::vector<uint8> signature_buf;
89 signature_creator->Final(&signature_buf);
90 std::string signature_str(signature_buf.begin(), signature_buf.end());
91 std::string signature_base64;
92 base::Base64Encode(signature_str, &signature_base64);
93 return signature_base64;
94 }
95
96 crypto::RSAPrivateKey* HostKeyPair::CopyPrivateKey() const {
97 std::vector<uint8> key_bytes;
98 CHECK(key_->ExportPrivateKey(&key_bytes));
99 return crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_bytes);
100 }
101
102 std::string HostKeyPair::GenerateCertificate() const {
103 scoped_refptr<net::X509Certificate> cert =
104 net::X509Certificate::CreateSelfSigned(
105 key_.get(), "CN=chromoting",
106 base::RandInt(1, std::numeric_limits<int>::max()),
107 base::TimeDelta::FromDays(1));
108 if (!cert)
109 return std::string();
110
111 std::string encoded;
112 bool result = net::X509Certificate::GetDEREncoded(cert->os_cert_handle(),
113 &encoded);
114 CHECK(result);
115 return encoded;
116 }
117
118 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/host_key_pair.h ('k') | remoting/host/host_key_pair_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698