OLD | NEW |
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/base/rsa_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 { |
21 | 20 |
22 HostKeyPair::HostKeyPair() { } | 21 RsaKeyPair::RsaKeyPair(scoped_ptr<crypto::RSAPrivateKey> key) |
23 | 22 : key_(key.Pass()){ |
24 HostKeyPair::~HostKeyPair() { } | 23 DCHECK(key_); |
25 | |
26 void HostKeyPair::Generate() { | |
27 key_.reset(crypto::RSAPrivateKey::Create(2048)); | |
28 } | 24 } |
29 | 25 |
30 bool HostKeyPair::LoadFromString(const std::string& key_base64) { | 26 RsaKeyPair::~RsaKeyPair() {} |
| 27 |
| 28 //static |
| 29 scoped_refptr<RsaKeyPair> RsaKeyPair::Generate() { |
| 30 scoped_ptr<crypto::RSAPrivateKey> key(crypto::RSAPrivateKey::Create(2048)); |
| 31 if (!key) { |
| 32 LOG(ERROR) << "Cannot generate private key."; |
| 33 return NULL; |
| 34 } |
| 35 return new RsaKeyPair(key.Pass()); |
| 36 } |
| 37 |
| 38 //static |
| 39 scoped_refptr<RsaKeyPair> RsaKeyPair::FromString( |
| 40 const std::string& key_base64) { |
31 std::string key_str; | 41 std::string key_str; |
32 if (!base::Base64Decode(key_base64, &key_str)) { | 42 if (!base::Base64Decode(key_base64, &key_str)) { |
33 LOG(ERROR) << "Failed to decode private key."; | 43 LOG(ERROR) << "Failed to decode private key."; |
34 return false; | 44 return NULL; |
35 } | 45 } |
36 | 46 |
37 std::vector<uint8> key_buf(key_str.begin(), key_str.end()); | 47 std::vector<uint8> key_buf(key_str.begin(), key_str.end()); |
38 key_.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf)); | 48 scoped_ptr<crypto::RSAPrivateKey> key( |
39 if (key_.get() == NULL) { | 49 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf)); |
| 50 if (!key) { |
40 LOG(ERROR) << "Invalid private key."; | 51 LOG(ERROR) << "Invalid private key."; |
41 return false; | 52 return NULL; |
42 } | 53 } |
43 | 54 |
44 return true; | 55 return new RsaKeyPair(key.Pass()); |
45 } | 56 } |
46 | 57 |
47 bool HostKeyPair::Load(const HostConfig& host_config) { | 58 std::string RsaKeyPair::ToString() const { |
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. | 59 // Check that the key initialized. |
62 DCHECK(key_.get() != NULL); | 60 DCHECK(key_.get() != NULL); |
63 | 61 |
64 std::vector<uint8> key_buf; | 62 std::vector<uint8> key_buf; |
65 key_->ExportPrivateKey(&key_buf); | 63 CHECK(key_->ExportPrivateKey(&key_buf)); |
66 std::string key_str(key_buf.begin(), key_buf.end()); | 64 std::string key_str(key_buf.begin(), key_buf.end()); |
67 std::string key_base64; | 65 std::string key_base64; |
68 if (!base::Base64Encode(key_str, &key_base64)) { | 66 if (!base::Base64Encode(key_str, &key_base64)) { |
69 LOG(FATAL) << "Base64Encode failed"; | 67 LOG(FATAL) << "Base64Encode failed"; |
70 } | 68 } |
71 return key_base64; | 69 return key_base64; |
72 } | 70 } |
73 | 71 |
74 std::string HostKeyPair::GetPublicKey() const { | 72 std::string RsaKeyPair::GetPublicKey() const { |
75 std::vector<uint8> public_key; | 73 std::vector<uint8> public_key; |
76 key_->ExportPublicKey(&public_key); | 74 CHECK(key_->ExportPublicKey(&public_key)); |
77 std::string public_key_str(public_key.begin(), public_key.end()); | 75 std::string public_key_str(public_key.begin(), public_key.end()); |
78 std::string public_key_base64; | 76 std::string public_key_base64; |
79 base::Base64Encode(public_key_str, &public_key_base64); | 77 base::Base64Encode(public_key_str, &public_key_base64); |
80 return public_key_base64; | 78 return public_key_base64; |
81 } | 79 } |
82 | 80 |
83 std::string HostKeyPair::GetSignature(const std::string& message) const { | 81 std::string RsaKeyPair::SignMessage(const std::string& message) const { |
84 scoped_ptr<crypto::SignatureCreator> signature_creator( | 82 scoped_ptr<crypto::SignatureCreator> signature_creator( |
85 crypto::SignatureCreator::Create(key_.get())); | 83 crypto::SignatureCreator::Create(key_.get())); |
86 signature_creator->Update(reinterpret_cast<const uint8*>(message.c_str()), | 84 signature_creator->Update(reinterpret_cast<const uint8*>(message.c_str()), |
87 message.length()); | 85 message.length()); |
88 std::vector<uint8> signature_buf; | 86 std::vector<uint8> signature_buf; |
89 signature_creator->Final(&signature_buf); | 87 signature_creator->Final(&signature_buf); |
90 std::string signature_str(signature_buf.begin(), signature_buf.end()); | 88 std::string signature_str(signature_buf.begin(), signature_buf.end()); |
91 std::string signature_base64; | 89 std::string signature_base64; |
92 base::Base64Encode(signature_str, &signature_base64); | 90 base::Base64Encode(signature_str, &signature_base64); |
93 return signature_base64; | 91 return signature_base64; |
94 } | 92 } |
95 | 93 |
96 crypto::RSAPrivateKey* HostKeyPair::CopyPrivateKey() const { | 94 std::string RsaKeyPair::GenerateCertificate() 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 = | 95 scoped_refptr<net::X509Certificate> cert = |
104 net::X509Certificate::CreateSelfSigned( | 96 net::X509Certificate::CreateSelfSigned( |
105 key_.get(), "CN=chromoting", | 97 key_.get(), "CN=chromoting", |
106 base::RandInt(1, std::numeric_limits<int>::max()), | 98 base::RandInt(1, std::numeric_limits<int>::max()), |
107 base::TimeDelta::FromDays(1)); | 99 base::TimeDelta::FromDays(1)); |
108 if (!cert) | 100 if (!cert) |
109 return std::string(); | 101 return std::string(); |
110 | 102 |
111 std::string encoded; | 103 std::string encoded; |
112 bool result = net::X509Certificate::GetDEREncoded(cert->os_cert_handle(), | 104 bool result = net::X509Certificate::GetDEREncoded(cert->os_cert_handle(), |
113 &encoded); | 105 &encoded); |
114 CHECK(result); | 106 CHECK(result); |
115 return encoded; | 107 return encoded; |
116 } | 108 } |
117 | 109 |
118 } // namespace remoting | 110 } // namespace remoting |
OLD | NEW |