OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/protocol/pairing_registry.h" | 5 #include "remoting/protocol/pairing_registry.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/guid.h" | 9 #include "base/guid.h" |
10 #include "crypto/random.h" | 10 #include "crypto/random.h" |
11 | 11 |
12 namespace remoting { | 12 namespace remoting { |
13 namespace protocol { | 13 namespace protocol { |
14 | 14 |
15 // How many bytes of random data to use for the client id and shared secret. | 15 // How many bytes of random data to use for the shared secret. |
16 const int kKeySize = 16; | 16 const int kKeySize = 16; |
17 | 17 |
| 18 PairingRegistry::Pairing::Pairing() { |
| 19 } |
| 20 |
| 21 PairingRegistry::Pairing::Pairing(const base::Time& created_time, |
| 22 const std::string& client_name, |
| 23 const std::string& client_id, |
| 24 const std::string& shared_secret) |
| 25 : created_time_(created_time), |
| 26 client_name_(client_name), |
| 27 client_id_(client_id), |
| 28 shared_secret_(shared_secret) { |
| 29 } |
| 30 |
| 31 PairingRegistry::Pairing PairingRegistry::Pairing::Create( |
| 32 const std::string& client_name) { |
| 33 base::Time created_time = base::Time::Now(); |
| 34 std::string client_id = base::GenerateGUID(); |
| 35 std::string shared_secret; |
| 36 char buffer[kKeySize]; |
| 37 crypto::RandBytes(buffer, arraysize(buffer)); |
| 38 if (!base::Base64Encode(base::StringPiece(buffer, arraysize(buffer)), |
| 39 &shared_secret)) { |
| 40 LOG(FATAL) << "Base64Encode failed."; |
| 41 } |
| 42 return Pairing(created_time, client_name, client_id, shared_secret); |
| 43 } |
| 44 |
| 45 PairingRegistry::Pairing::~Pairing() { |
| 46 } |
| 47 |
| 48 bool PairingRegistry::Pairing::operator==(const Pairing& other) const { |
| 49 return created_time_ == other.created_time_ && |
| 50 client_id_ == other.client_id_ && |
| 51 client_name_ == other.client_name_ && |
| 52 shared_secret_ == other.shared_secret_; |
| 53 } |
| 54 |
| 55 bool PairingRegistry::Pairing::is_valid() const { |
| 56 return !client_id_.empty() && !shared_secret_.empty(); |
| 57 } |
| 58 |
18 PairingRegistry::PairingRegistry(scoped_ptr<Delegate> delegate) | 59 PairingRegistry::PairingRegistry(scoped_ptr<Delegate> delegate) |
19 : delegate_(delegate.Pass()) { | 60 : delegate_(delegate.Pass()) { |
20 DCHECK(delegate_); | 61 DCHECK(delegate_); |
21 } | 62 } |
22 | 63 |
23 PairingRegistry::~PairingRegistry() { | 64 PairingRegistry::~PairingRegistry() { |
24 } | 65 } |
25 | 66 |
26 PairingRegistry::Pairing PairingRegistry::CreatePairing( | 67 PairingRegistry::Pairing PairingRegistry::CreatePairing( |
27 const std::string& client_name) { | 68 const std::string& client_name) { |
28 DCHECK(CalledOnValidThread()); | 69 DCHECK(CalledOnValidThread()); |
29 | 70 Pairing result = Pairing::Create(client_name); |
30 Pairing result; | 71 delegate_->AddPairing(result, AddPairingCallback()); |
31 result.client_name = client_name; | |
32 result.client_id = base::GenerateGUID(); | |
33 | |
34 // Create a random shared secret to authenticate this client. | |
35 char buffer[kKeySize]; | |
36 crypto::RandBytes(buffer, arraysize(buffer)); | |
37 if (!base::Base64Encode(base::StringPiece(buffer, arraysize(buffer)), | |
38 &result.shared_secret)) { | |
39 LOG(FATAL) << "Base64Encode failed."; | |
40 } | |
41 | |
42 // Save the result via the Delegate and return it to the caller. | |
43 delegate_->AddPairing(result); | |
44 return result; | 72 return result; |
45 } | 73 } |
46 | 74 |
47 void PairingRegistry::GetPairing(const std::string& client_id, | 75 void PairingRegistry::GetPairing(const std::string& client_id, |
48 const GetPairingCallback& callback) { | 76 const GetPairingCallback& callback) { |
49 DCHECK(CalledOnValidThread()); | 77 DCHECK(CalledOnValidThread()); |
50 delegate_->GetPairing(client_id, callback); | 78 delegate_->GetPairing(client_id, callback); |
51 } | 79 } |
52 | 80 |
53 void NotImplementedPairingRegistryDelegate::AddPairing( | 81 void NotImplementedPairingRegistryDelegate::AddPairing( |
54 const PairingRegistry::Pairing& new_paired_client) { | 82 const PairingRegistry::Pairing& new_paired_client, |
| 83 const PairingRegistry::AddPairingCallback& callback) { |
55 NOTIMPLEMENTED(); | 84 NOTIMPLEMENTED(); |
| 85 if (!callback.is_null()) { |
| 86 callback.Run(false); |
| 87 } |
56 } | 88 } |
57 | 89 |
58 void NotImplementedPairingRegistryDelegate::GetPairing( | 90 void NotImplementedPairingRegistryDelegate::GetPairing( |
59 const std::string& client_id, | 91 const std::string& client_id, |
60 const PairingRegistry::GetPairingCallback& callback) { | 92 const PairingRegistry::GetPairingCallback& callback) { |
61 NOTIMPLEMENTED(); | 93 NOTIMPLEMENTED(); |
62 callback.Run(PairingRegistry::Pairing()); | 94 callback.Run(PairingRegistry::Pairing()); |
63 } | 95 } |
64 | 96 |
65 | |
66 } // namespace protocol | 97 } // namespace protocol |
67 } // namespace remoting | 98 } // namespace remoting |
OLD | NEW |