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

Side by Side Diff: remoting/base/rsa_key_pair.cc

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

Powered by Google App Engine
This is Rietveld 408576698