OLD | NEW |
---|---|
(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 <vector> | |
awong
2010/08/02 19:53:49
Alphabetical order
Sergey Ulanov
2010/08/03 02:10:39
Done.
| |
8 #include <string> | |
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() { }; | |
awong
2010/08/02 19:53:49
Trailing semicolons not needed. Here and below.
Sergey Ulanov
2010/08/03 02:10:39
Done.
| |
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." << key_base64; | |
awong
2010/08/02 19:53:49
Do we want to dump the private key to screen even
Sergey Ulanov
2010/08/03 02:10:39
Done.
| |
31 return false; | |
32 } | |
33 | |
34 std::vector<uint8> key_buf(key_str.begin(), key_str.end()); | |
35 base::RSAPrivateKey *new_key = | |
awong
2010/08/02 19:53:49
Just use key_.reset() here?
Sergey Ulanov
2010/08/03 02:10:39
This would reset key_ even if the key cannot be pa
awong
2010/08/03 19:21:02
hmm...I feel like this may actually even be prefer
| |
36 base::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf); | |
37 if (new_key == NULL) { | |
38 LOG(ERROR) << "Invalid private key."; | |
39 return false; | |
40 } | |
41 | |
42 key_.reset(new_key); | |
43 | |
44 return true; | |
45 } | |
46 | |
47 bool HostKeyPair::Load(HostConfig* host_config) { | |
awong
2010/08/02 19:53:49
These functions feel funny...almost like they shou
Sergey Ulanov
2010/08/03 02:10:39
HostConfig is just a store for all config settings
awong
2010/08/03 19:21:02
A third class is deifnitely overkill. Why would a
Sergey Ulanov
2010/08/04 01:41:12
HostConfig is probably a bad name for the class, s
| |
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 // Check that the key initialized. | |
58 DCHECK(key_.get() != NULL); | |
59 | |
60 host_config->Update( | |
61 NewRunnableMethod(this, &HostKeyPair::DoSave, host_config)); | |
62 } | |
63 | |
64 void HostKeyPair::DoSave(MutableHostConfig* host_config) const { | |
65 std::vector<uint8> key_buf; | |
66 key_->ExportPrivateKey(&key_buf); | |
67 std::string key_str(key_buf.begin(), key_buf.end()); | |
68 std::string key_base64; | |
69 base::Base64Encode(key_str, &key_base64); | |
70 host_config->SetString(kPrivateKeyConfigPath, key_base64); | |
71 } | |
72 | |
73 std::string HostKeyPair::GetPublicKey() const { | |
74 std::vector<uint8> public_key; | |
75 key_->ExportPublicKey(&public_key); | |
76 std::string public_key_str(public_key.begin(), public_key.end()); | |
77 std::string public_key_base64; | |
78 base::Base64Encode(public_key_str, &public_key_base64); | |
79 return public_key_base64; | |
80 } | |
81 | |
82 std::string HostKeyPair::GetSignature(const std::string& message) const { | |
83 scoped_ptr<base::SignatureCreator> signature_creator( | |
84 base::SignatureCreator::Create(key_.get())); | |
85 signature_creator->Update(reinterpret_cast<const uint8*>(message.c_str()), | |
86 message.length()); | |
87 std::vector<uint8> signature_buf; | |
88 signature_creator->Final(&signature_buf); | |
89 std::string signature_str(signature_buf.begin(), signature_buf.end()); | |
90 std::string signature_base64; | |
91 base::Base64Encode(signature_str, &signature_base64); | |
92 return signature_base64; | |
93 } | |
94 | |
95 } // namespace remoting | |
OLD | NEW |