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

Side by Side Diff: remoting/host/host_key_pair.cc

Issue 3087003: Added HostKeyPair class, signatures for heartbeat messages. (Closed)
Patch Set: - Created 10 years, 4 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
« no previous file with comments | « remoting/host/host_key_pair.h ('k') | remoting/host/host_key_pair_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "remoting/host/host_key_pair.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/base64.h"
11 #include "base/crypto/rsa_private_key.h"
12 #include "base/crypto/signature_creator.h"
13 #include "base/logging.h"
14 #include "base/task.h"
15 #include "remoting/host/host_config.h"
16
17 namespace remoting {
18
19 HostKeyPair::HostKeyPair() { }
20
21 HostKeyPair::~HostKeyPair() { }
22
23 void HostKeyPair::Generate() {
24 key_.reset(base::RSAPrivateKey::Create(2048));
25 }
26
27 bool HostKeyPair::LoadFromString(const std::string& key_base64) {
28 std::string key_str;
29 if (!base::Base64Decode(key_base64, &key_str)) {
30 LOG(ERROR) << "Failed to decode private key.";
31 return false;
32 }
33
34 std::vector<uint8> key_buf(key_str.begin(), key_str.end());
35 key_.reset(base::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf));
36 if (key_.get() == NULL) {
37 LOG(ERROR) << "Invalid private key.";
38 return false;
39 }
40
41 return true;
42 }
43
44 bool HostKeyPair::Load(HostConfig* host_config) {
45 std::string key_base64;
46 if (!host_config->GetString(kPrivateKeyConfigPath, &key_base64)) {
47 LOG(ERROR) << "Private key wasn't found in the config file.";
48 return false;
49 }
50 return LoadFromString(key_base64);
51 }
52
53 void HostKeyPair::Save(MutableHostConfig* host_config) {
54 // Check that the key initialized.
55 DCHECK(key_.get() != NULL);
56
57 host_config->Update(
58 NewRunnableMethod(this, &HostKeyPair::DoSave, host_config));
59 }
60
61 void HostKeyPair::DoSave(MutableHostConfig* host_config) const {
62 std::vector<uint8> key_buf;
63 key_->ExportPrivateKey(&key_buf);
64 std::string key_str(key_buf.begin(), key_buf.end());
65 std::string key_base64;
66 base::Base64Encode(key_str, &key_base64);
67 host_config->SetString(kPrivateKeyConfigPath, key_base64);
68 }
69
70 std::string HostKeyPair::GetPublicKey() const {
71 std::vector<uint8> public_key;
72 key_->ExportPublicKey(&public_key);
73 std::string public_key_str(public_key.begin(), public_key.end());
74 std::string public_key_base64;
75 base::Base64Encode(public_key_str, &public_key_base64);
76 return public_key_base64;
77 }
78
79 std::string HostKeyPair::GetSignature(const std::string& message) const {
80 scoped_ptr<base::SignatureCreator> signature_creator(
81 base::SignatureCreator::Create(key_.get()));
82 signature_creator->Update(reinterpret_cast<const uint8*>(message.c_str()),
83 message.length());
84 std::vector<uint8> signature_buf;
85 signature_creator->Final(&signature_buf);
86 std::string signature_str(signature_buf.begin(), signature_buf.end());
87 std::string signature_base64;
88 base::Base64Encode(signature_str, &signature_base64);
89 return signature_base64;
90 }
91
92 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/host_key_pair.h ('k') | remoting/host/host_key_pair_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698