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