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

Unified 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: 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « remoting/base/rsa_key_pair.h ('k') | remoting/base/rsa_key_pair_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: remoting/base/rsa_key_pair.cc
diff --git a/remoting/host/host_key_pair.cc b/remoting/base/rsa_key_pair.cc
similarity index 62%
rename from remoting/host/host_key_pair.cc
rename to remoting/base/rsa_key_pair.cc
index ff0f2cfcb380d33a5f1aed33b6738ea778f01aac..566bf0143aa965773df33725d5dd25deb7767911 100644
--- a/remoting/host/host_key_pair.cc
+++ b/remoting/base/rsa_key_pair.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "remoting/host/host_key_pair.h"
+#include "remoting/base/rsa_key_pair.h"
#include <limits>
#include <string>
@@ -15,54 +15,52 @@
#include "crypto/rsa_private_key.h"
#include "crypto/signature_creator.h"
#include "net/base/x509_certificate.h"
-#include "remoting/host/host_config.h"
namespace remoting {
-HostKeyPair::HostKeyPair() { }
+RsaKeyPair::RsaKeyPair(scoped_ptr<crypto::RSAPrivateKey> key)
+ : key_(key.Pass()){
+ DCHECK(key_);
+}
-HostKeyPair::~HostKeyPair() { }
+RsaKeyPair::~RsaKeyPair() {}
-void HostKeyPair::Generate() {
- key_.reset(crypto::RSAPrivateKey::Create(2048));
+//static
+scoped_refptr<RsaKeyPair> RsaKeyPair::Generate() {
+ scoped_ptr<crypto::RSAPrivateKey> key(crypto::RSAPrivateKey::Create(2048));
+ if (!key) {
+ LOG(ERROR) << "Cannot generate private key.";
+ return NULL;
+ }
+ return new RsaKeyPair(key.Pass());
}
-bool HostKeyPair::LoadFromString(const std::string& key_base64) {
+//static
+scoped_refptr<RsaKeyPair> RsaKeyPair::FromString(
+ const std::string& key_base64) {
std::string key_str;
if (!base::Base64Decode(key_base64, &key_str)) {
LOG(ERROR) << "Failed to decode private key.";
- return false;
+ return NULL;
}
std::vector<uint8> key_buf(key_str.begin(), key_str.end());
- key_.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf));
- if (key_.get() == NULL) {
+ scoped_ptr<crypto::RSAPrivateKey> key(
+ crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf));
+ if (!key) {
LOG(ERROR) << "Invalid private key.";
- return false;
- }
-
- return true;
-}
-
-bool HostKeyPair::Load(const HostConfig& host_config) {
- std::string key_base64;
- if (!host_config.GetString(kPrivateKeyConfigPath, &key_base64)) {
- LOG(ERROR) << "Private key wasn't found in the config file.";
- return false;
+ return NULL;
}
- return LoadFromString(key_base64);
-}
-void HostKeyPair::Save(MutableHostConfig* host_config) {
- host_config->SetString(kPrivateKeyConfigPath, GetAsString());
+ return new RsaKeyPair(key.Pass());
}
-std::string HostKeyPair::GetAsString() const {
+std::string RsaKeyPair::ToString() const {
// Check that the key initialized.
DCHECK(key_.get() != NULL);
std::vector<uint8> key_buf;
- key_->ExportPrivateKey(&key_buf);
+ CHECK(key_->ExportPrivateKey(&key_buf));
std::string key_str(key_buf.begin(), key_buf.end());
std::string key_base64;
if (!base::Base64Encode(key_str, &key_base64)) {
@@ -71,16 +69,16 @@ std::string HostKeyPair::GetAsString() const {
return key_base64;
}
-std::string HostKeyPair::GetPublicKey() const {
+std::string RsaKeyPair::GetPublicKey() const {
std::vector<uint8> public_key;
- key_->ExportPublicKey(&public_key);
+ CHECK(key_->ExportPublicKey(&public_key));
std::string public_key_str(public_key.begin(), public_key.end());
std::string public_key_base64;
base::Base64Encode(public_key_str, &public_key_base64);
return public_key_base64;
}
-std::string HostKeyPair::GetSignature(const std::string& message) const {
+std::string RsaKeyPair::SignMessage(const std::string& message) const {
scoped_ptr<crypto::SignatureCreator> signature_creator(
crypto::SignatureCreator::Create(key_.get()));
signature_creator->Update(reinterpret_cast<const uint8*>(message.c_str()),
@@ -93,13 +91,7 @@ std::string HostKeyPair::GetSignature(const std::string& message) const {
return signature_base64;
}
-crypto::RSAPrivateKey* HostKeyPair::CopyPrivateKey() const {
- std::vector<uint8> key_bytes;
- CHECK(key_->ExportPrivateKey(&key_bytes));
- return crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_bytes);
-}
-
-std::string HostKeyPair::GenerateCertificate() const {
+std::string RsaKeyPair::GenerateCertificate() const {
scoped_refptr<net::X509Certificate> cert =
net::X509Certificate::CreateSelfSigned(
key_.get(), "CN=chromoting",
« no previous file with comments | « remoting/base/rsa_key_pair.h ('k') | remoting/base/rsa_key_pair_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698