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

Side by Side Diff: remoting/protocol/key_pair.cc

Issue 12316083: Move HostKeyPair into protocol::KeyPair. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add missing files, move TestKeyPair. Created 7 years, 10 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "remoting/host/host_key_pair.h" 5 #include "remoting/protocol/key_pair.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/base64.h" 11 #include "base/base64.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/rand_util.h" 13 #include "base/rand_util.h"
14 #include "base/time.h" 14 #include "base/time.h"
15 #include "crypto/rsa_private_key.h" 15 #include "crypto/rsa_private_key.h"
16 #include "crypto/signature_creator.h" 16 #include "crypto/signature_creator.h"
17 #include "net/base/x509_certificate.h" 17 #include "net/base/x509_certificate.h"
18 #include "remoting/host/host_config.h"
19 18
20 namespace remoting { 19 namespace remoting {
20 namespace protocol {
21 21
22 HostKeyPair::HostKeyPair() { } 22 KeyPair::KeyPair() { }
23 23
24 HostKeyPair::~HostKeyPair() { } 24 KeyPair::~KeyPair() { }
25 25
26 void HostKeyPair::Generate() { 26 void KeyPair::Generate() {
27 key_.reset(crypto::RSAPrivateKey::Create(2048)); 27 key_.reset(crypto::RSAPrivateKey::Create(2048));
28 } 28 }
29 29
30 bool HostKeyPair::LoadFromString(const std::string& key_base64) { 30 bool KeyPair::LoadFromString(const std::string& key_base64) {
31 std::string key_str; 31 std::string key_str;
32 if (!base::Base64Decode(key_base64, &key_str)) { 32 if (!base::Base64Decode(key_base64, &key_str)) {
33 LOG(ERROR) << "Failed to decode private key."; 33 LOG(ERROR) << "Failed to decode private key.";
34 return false; 34 return false;
35 } 35 }
36 36
37 std::vector<uint8> key_buf(key_str.begin(), key_str.end()); 37 std::vector<uint8> key_buf(key_str.begin(), key_str.end());
38 key_.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf)); 38 key_.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf));
39 if (key_.get() == NULL) { 39 if (key_.get() == NULL) {
40 LOG(ERROR) << "Invalid private key."; 40 LOG(ERROR) << "Invalid private key.";
41 return false; 41 return false;
42 } 42 }
43 43
44 return true; 44 return true;
45 } 45 }
46 46
47 bool HostKeyPair::Load(const HostConfig& host_config) { 47 scoped_ptr<KeyPair> KeyPair::Copy() {
48 std::string key_base64; 48 scoped_ptr<KeyPair> pair(new KeyPair());
49 if (!host_config.GetString(kPrivateKeyConfigPath, &key_base64)) { 49 pair->LoadFromString(this->GetAsString());
Wez 2013/02/23 03:43:20 Doesn't RSAPrivateKey have a Copy() method now?
rmsousa 2013/02/26 02:38:52 Done.
50 LOG(ERROR) << "Private key wasn't found in the config file."; 50 return pair.Pass();
51 return false;
52 }
53 return LoadFromString(key_base64);
54 } 51 }
55 52
56 void HostKeyPair::Save(MutableHostConfig* host_config) { 53 std::string KeyPair::GetAsString() const {
57 host_config->SetString(kPrivateKeyConfigPath, GetAsString());
58 }
59
60 std::string HostKeyPair::GetAsString() const {
61 // Check that the key initialized. 54 // Check that the key initialized.
62 DCHECK(key_.get() != NULL); 55 DCHECK(key_.get() != NULL);
63 56
64 std::vector<uint8> key_buf; 57 std::vector<uint8> key_buf;
65 key_->ExportPrivateKey(&key_buf); 58 key_->ExportPrivateKey(&key_buf);
Wez 2013/02/23 03:43:20 IIRC, the reason we didn't provide Copy() before w
rmsousa 2013/02/26 02:38:52 Done.
66 std::string key_str(key_buf.begin(), key_buf.end()); 59 std::string key_str(key_buf.begin(), key_buf.end());
67 std::string key_base64; 60 std::string key_base64;
68 if (!base::Base64Encode(key_str, &key_base64)) { 61 if (!base::Base64Encode(key_str, &key_base64)) {
69 LOG(FATAL) << "Base64Encode failed"; 62 LOG(FATAL) << "Base64Encode failed";
70 } 63 }
71 return key_base64; 64 return key_base64;
72 } 65 }
73 66
74 std::string HostKeyPair::GetPublicKey() const { 67 std::string KeyPair::GetPublicKey() const {
75 std::vector<uint8> public_key; 68 std::vector<uint8> public_key;
76 key_->ExportPublicKey(&public_key); 69 key_->ExportPublicKey(&public_key);
77 std::string public_key_str(public_key.begin(), public_key.end()); 70 std::string public_key_str(public_key.begin(), public_key.end());
78 std::string public_key_base64; 71 std::string public_key_base64;
79 base::Base64Encode(public_key_str, &public_key_base64); 72 base::Base64Encode(public_key_str, &public_key_base64);
80 return public_key_base64; 73 return public_key_base64;
81 } 74 }
82 75
83 std::string HostKeyPair::GetSignature(const std::string& message) const { 76 std::string KeyPair::GetSignature(const std::string& message) const {
84 scoped_ptr<crypto::SignatureCreator> signature_creator( 77 scoped_ptr<crypto::SignatureCreator> signature_creator(
85 crypto::SignatureCreator::Create(key_.get())); 78 crypto::SignatureCreator::Create(key_.get()));
86 signature_creator->Update(reinterpret_cast<const uint8*>(message.c_str()), 79 signature_creator->Update(reinterpret_cast<const uint8*>(message.c_str()),
87 message.length()); 80 message.length());
88 std::vector<uint8> signature_buf; 81 std::vector<uint8> signature_buf;
89 signature_creator->Final(&signature_buf); 82 signature_creator->Final(&signature_buf);
90 std::string signature_str(signature_buf.begin(), signature_buf.end()); 83 std::string signature_str(signature_buf.begin(), signature_buf.end());
91 std::string signature_base64; 84 std::string signature_base64;
92 base::Base64Encode(signature_str, &signature_base64); 85 base::Base64Encode(signature_str, &signature_base64);
93 return signature_base64; 86 return signature_base64;
94 } 87 }
95 88
96 crypto::RSAPrivateKey* HostKeyPair::CopyPrivateKey() const { 89 crypto::RSAPrivateKey* KeyPair::CopyPrivateKey() const {
97 std::vector<uint8> key_bytes; 90 std::vector<uint8> key_bytes;
98 CHECK(key_->ExportPrivateKey(&key_bytes)); 91 CHECK(key_->ExportPrivateKey(&key_bytes));
99 return crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_bytes); 92 return crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_bytes);
100 } 93 }
101 94
102 std::string HostKeyPair::GenerateCertificate() const { 95 std::string KeyPair::GenerateCertificate() const {
103 scoped_refptr<net::X509Certificate> cert = 96 scoped_refptr<net::X509Certificate> cert =
104 net::X509Certificate::CreateSelfSigned( 97 net::X509Certificate::CreateSelfSigned(
105 key_.get(), "CN=chromoting", 98 key_.get(), "CN=chromoting",
106 base::RandInt(1, std::numeric_limits<int>::max()), 99 base::RandInt(1, std::numeric_limits<int>::max()),
107 base::TimeDelta::FromDays(1)); 100 base::TimeDelta::FromDays(1));
108 if (!cert) 101 if (!cert)
109 return std::string(); 102 return std::string();
110 103
111 std::string encoded; 104 std::string encoded;
112 bool result = net::X509Certificate::GetDEREncoded(cert->os_cert_handle(), 105 bool result = net::X509Certificate::GetDEREncoded(cert->os_cert_handle(),
113 &encoded); 106 &encoded);
114 CHECK(result); 107 CHECK(result);
115 return encoded; 108 return encoded;
116 } 109 }
117 110
111 } // namespace protocol
118 } // namespace remoting 112 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698