| 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 |
| 18 PairingRegistry::PairingRegistry(scoped_ptr<Delegate> delegate) | 55 PairingRegistry::PairingRegistry(scoped_ptr<Delegate> delegate) |
| 19 : delegate_(delegate.Pass()) { | 56 : delegate_(delegate.Pass()) { |
| 20 DCHECK(delegate_); | 57 DCHECK(delegate_); |
| 21 } | 58 } |
| 22 | 59 |
| 23 PairingRegistry::~PairingRegistry() { | 60 PairingRegistry::~PairingRegistry() { |
| 24 } | 61 } |
| 25 | 62 |
| 26 PairingRegistry::Pairing PairingRegistry::CreatePairing( | 63 PairingRegistry::Pairing PairingRegistry::CreatePairing( |
| 27 const std::string& client_name) { | 64 const std::string& client_name) { |
| 28 DCHECK(CalledOnValidThread()); | 65 DCHECK(CalledOnValidThread()); |
| 29 | 66 Pairing result = Pairing::Create(client_name); |
| 30 Pairing result; | 67 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; | 68 return result; |
| 45 } | 69 } |
| 46 | 70 |
| 47 void PairingRegistry::GetPairing(const std::string& client_id, | 71 void PairingRegistry::GetPairing(const std::string& client_id, |
| 48 const GetPairingCallback& callback) { | 72 const GetPairingCallback& callback) { |
| 49 DCHECK(CalledOnValidThread()); | 73 DCHECK(CalledOnValidThread()); |
| 50 delegate_->GetPairing(client_id, callback); | 74 delegate_->GetPairing(client_id, callback); |
| 51 } | 75 } |
| 52 | 76 |
| 53 void NotImplementedPairingRegistryDelegate::AddPairing( | 77 void NotImplementedPairingRegistryDelegate::AddPairing( |
| 54 const PairingRegistry::Pairing& new_paired_client) { | 78 const PairingRegistry::Pairing& new_paired_client, |
| 79 const PairingRegistry::AddPairingCallback& callback) { |
| 55 NOTIMPLEMENTED(); | 80 NOTIMPLEMENTED(); |
| 81 if (!callback.is_null()) { |
| 82 callback.Run(false); |
| 83 } |
| 56 } | 84 } |
| 57 | 85 |
| 58 void NotImplementedPairingRegistryDelegate::GetPairing( | 86 void NotImplementedPairingRegistryDelegate::GetPairing( |
| 59 const std::string& client_id, | 87 const std::string& client_id, |
| 60 const PairingRegistry::GetPairingCallback& callback) { | 88 const PairingRegistry::GetPairingCallback& callback) { |
| 61 NOTIMPLEMENTED(); | 89 NOTIMPLEMENTED(); |
| 62 callback.Run(PairingRegistry::Pairing()); | 90 callback.Run(PairingRegistry::Pairing(base::Time(), "", "", "")); |
| 63 } | 91 } |
| 64 | 92 |
| 65 | |
| 66 } // namespace protocol | 93 } // namespace protocol |
| 67 } // namespace remoting | 94 } // namespace remoting |
| OLD | NEW |