[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "remoting/protocol/pairing_host_authenticator.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/logging.h" |
| 9 | #include "remoting/base/constants.h" |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 10 | #include "remoting/protocol/channel_authenticator.h" |
niklase | af726f1 | 2014-09-08 18:43:02 | [diff] [blame] | 11 | #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 12 | |
| 13 | namespace remoting { |
| 14 | namespace protocol { |
| 15 | |
| 16 | PairingHostAuthenticator::PairingHostAuthenticator( |
| 17 | scoped_refptr<PairingRegistry> pairing_registry, |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame^] | 18 | const CreateBaseAuthenticatorCallback& create_base_authenticator_callback, |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 19 | const std::string& pin) |
| 20 | : pairing_registry_(pairing_registry), |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame^] | 21 | create_base_authenticator_callback_(create_base_authenticator_callback), |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 22 | pin_(pin), |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame^] | 23 | weak_factory_(this) {} |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 24 | |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame^] | 25 | PairingHostAuthenticator::~PairingHostAuthenticator() {} |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 26 | |
| 27 | Authenticator::State PairingHostAuthenticator::state() const { |
| 28 | if (protocol_error_) { |
| 29 | return REJECTED; |
[email protected] | 40dade3 | 2013-06-14 07:08:11 | [diff] [blame] | 30 | } else if (waiting_for_paired_secret_) { |
| 31 | return PROCESSING_MESSAGE; |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame^] | 32 | } else if (!spake2_authenticator_) { |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 33 | return WAITING_MESSAGE; |
| 34 | } |
| 35 | return PairingAuthenticatorBase::state(); |
| 36 | } |
| 37 | |
| 38 | Authenticator::RejectionReason |
| 39 | PairingHostAuthenticator::rejection_reason() const { |
| 40 | if (protocol_error_) { |
| 41 | return PROTOCOL_ERROR; |
| 42 | } |
| 43 | return PairingAuthenticatorBase::rejection_reason(); |
| 44 | } |
| 45 | |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame^] | 46 | void PairingHostAuthenticator::CreateSpakeAuthenticatorWithPin( |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 47 | State initial_state, |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame^] | 48 | const base::Closure& resume_callback) { |
| 49 | spake2_authenticator_ = |
| 50 | create_base_authenticator_callback_.Run(pin_, initial_state); |
| 51 | resume_callback.Run(); |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void PairingHostAuthenticator::ProcessMessage( |
| 55 | const buzz::XmlElement* message, |
| 56 | const base::Closure& resume_callback) { |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame^] | 57 | if (!spake2_authenticator_) { |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 58 | std::string client_id; |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 59 | |
| 60 | const buzz::XmlElement* pairing_tag = message->FirstNamed(kPairingInfoTag); |
| 61 | if (pairing_tag) { |
| 62 | client_id = pairing_tag->Attr(kClientIdAttribute); |
| 63 | } |
| 64 | |
| 65 | if (client_id.empty()) { |
| 66 | LOG(ERROR) << "No client id specified."; |
| 67 | protocol_error_ = true; |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 68 | return; |
| 69 | } |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame^] | 70 | |
| 71 | waiting_for_paired_secret_ = true; |
| 72 | pairing_registry_->GetPairing( |
| 73 | client_id, |
| 74 | base::Bind(&PairingHostAuthenticator::ProcessMessageWithPairing, |
| 75 | weak_factory_.GetWeakPtr(), |
| 76 | base::Owned(new buzz::XmlElement(*message)), |
| 77 | resume_callback)); |
| 78 | return; |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | PairingAuthenticatorBase::ProcessMessage(message, resume_callback); |
| 82 | } |
| 83 | |
| 84 | void PairingHostAuthenticator::AddPairingElements(buzz::XmlElement* message) { |
| 85 | // Nothing to do here |
| 86 | } |
| 87 | |
[email protected] | 40dade3 | 2013-06-14 07:08:11 | [diff] [blame] | 88 | void PairingHostAuthenticator::ProcessMessageWithPairing( |
| 89 | const buzz::XmlElement* message, |
| 90 | const base::Closure& resume_callback, |
| 91 | PairingRegistry::Pairing pairing) { |
| 92 | waiting_for_paired_secret_ = false; |
[email protected] | df5189f | 2013-06-18 12:12:05 | [diff] [blame] | 93 | std::string paired_secret = pairing.shared_secret(); |
[email protected] | 40dade3 | 2013-06-14 07:08:11 | [diff] [blame] | 94 | if (paired_secret.empty()) { |
[email protected] | 5cbe3cf | 2013-11-25 17:05:04 | [diff] [blame] | 95 | VLOG(0) << "Unknown client id"; |
[email protected] | 40dade3 | 2013-06-14 07:08:11 | [diff] [blame] | 96 | error_message_ = "unknown-client-id"; |
| 97 | } |
| 98 | |
| 99 | using_paired_secret_ = !paired_secret.empty(); |
| 100 | if (using_paired_secret_) { |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame^] | 101 | spake2_authenticator_ = |
| 102 | create_base_authenticator_callback_.Run(paired_secret, WAITING_MESSAGE); |
[email protected] | 40dade3 | 2013-06-14 07:08:11 | [diff] [blame] | 103 | PairingAuthenticatorBase::ProcessMessage(message, resume_callback); |
| 104 | } else { |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame^] | 105 | spake2_authenticator_ = |
| 106 | create_base_authenticator_callback_.Run(pin_, MESSAGE_READY); |
[email protected] | 40dade3 | 2013-06-14 07:08:11 | [diff] [blame] | 107 | // The client's optimistic SPAKE message is using a Paired Secret to |
| 108 | // which the host doesn't have access, so don't bother processing it. |
| 109 | resume_callback.Run(); |
| 110 | } |
| 111 | } |
| 112 | |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 113 | } // namespace protocol |
| 114 | } // namespace remoting |