[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" |
kjellander | f0e410b | 2017-01-04 14:45:01 | [diff] [blame] | 11 | #include "third_party/libjingle_xmpp/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 | 843ef12 | 2016-03-17 01:44:17 | [diff] [blame] | 25 | void PairingHostAuthenticator::Initialize( |
| 26 | const std::string& client_id, |
| 27 | Authenticator::State preferred_initial_state, |
| 28 | const base::Closure& resume_callback) { |
| 29 | DCHECK(!spake2_authenticator_); |
| 30 | |
| 31 | if (client_id.empty()) { |
| 32 | using_paired_secret_ = false; |
| 33 | error_message_ = "client-id-unknown"; |
| 34 | spake2_authenticator_ = |
| 35 | create_base_authenticator_callback_.Run(pin_, MESSAGE_READY); |
| 36 | resume_callback.Run(); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | using_paired_secret_ = true; |
| 41 | waiting_for_paired_secret_ = true; |
| 42 | pairing_registry_->GetPairing( |
| 43 | client_id, base::Bind(&PairingHostAuthenticator::InitializeWithPairing, |
| 44 | weak_factory_.GetWeakPtr(), preferred_initial_state, |
| 45 | resume_callback)); |
| 46 | } |
| 47 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame^] | 48 | PairingHostAuthenticator::~PairingHostAuthenticator() = default; |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 49 | |
| 50 | Authenticator::State PairingHostAuthenticator::state() const { |
| 51 | if (protocol_error_) { |
| 52 | return REJECTED; |
[email protected] | 40dade3 | 2013-06-14 07:08:11 | [diff] [blame] | 53 | } else if (waiting_for_paired_secret_) { |
| 54 | return PROCESSING_MESSAGE; |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 55 | } |
| 56 | return PairingAuthenticatorBase::state(); |
| 57 | } |
| 58 | |
| 59 | Authenticator::RejectionReason |
| 60 | PairingHostAuthenticator::rejection_reason() const { |
| 61 | if (protocol_error_) { |
| 62 | return PROTOCOL_ERROR; |
| 63 | } |
| 64 | return PairingAuthenticatorBase::rejection_reason(); |
| 65 | } |
| 66 | |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame] | 67 | void PairingHostAuthenticator::CreateSpakeAuthenticatorWithPin( |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 68 | State initial_state, |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame] | 69 | const base::Closure& resume_callback) { |
| 70 | spake2_authenticator_ = |
| 71 | create_base_authenticator_callback_.Run(pin_, initial_state); |
| 72 | resume_callback.Run(); |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 73 | } |
| 74 | |
sergeyu | 843ef12 | 2016-03-17 01:44:17 | [diff] [blame] | 75 | void PairingHostAuthenticator::InitializeWithPairing( |
| 76 | Authenticator::State preferred_initial_state, |
[email protected] | 40dade3 | 2013-06-14 07:08:11 | [diff] [blame] | 77 | const base::Closure& resume_callback, |
| 78 | PairingRegistry::Pairing pairing) { |
sergeyu | 843ef12 | 2016-03-17 01:44:17 | [diff] [blame] | 79 | DCHECK(waiting_for_paired_secret_); |
[email protected] | 40dade3 | 2013-06-14 07:08:11 | [diff] [blame] | 80 | waiting_for_paired_secret_ = false; |
sergeyu | 843ef12 | 2016-03-17 01:44:17 | [diff] [blame] | 81 | std::string pairing_secret = pairing.shared_secret(); |
| 82 | if (pairing_secret.empty()) { |
[email protected] | 5cbe3cf | 2013-11-25 17:05:04 | [diff] [blame] | 83 | VLOG(0) << "Unknown client id"; |
[email protected] | 40dade3 | 2013-06-14 07:08:11 | [diff] [blame] | 84 | error_message_ = "unknown-client-id"; |
sergeyu | 843ef12 | 2016-03-17 01:44:17 | [diff] [blame] | 85 | using_paired_secret_ = false; |
| 86 | // If pairing wasn't found then always start in the MESSAGE_READY state. |
sergeyu | 12e320a | 2016-03-08 18:10:28 | [diff] [blame] | 87 | spake2_authenticator_ = |
| 88 | create_base_authenticator_callback_.Run(pin_, MESSAGE_READY); |
sergeyu | 843ef12 | 2016-03-17 01:44:17 | [diff] [blame] | 89 | } else { |
| 90 | using_paired_secret_ = true; |
| 91 | spake2_authenticator_ = create_base_authenticator_callback_.Run( |
| 92 | pairing_secret, preferred_initial_state); |
[email protected] | 40dade3 | 2013-06-14 07:08:11 | [diff] [blame] | 93 | } |
sergeyu | 843ef12 | 2016-03-17 01:44:17 | [diff] [blame] | 94 | resume_callback.Run(); |
[email protected] | 40dade3 | 2013-06-14 07:08:11 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | 6434bfe | 2013-05-22 09:00:23 | [diff] [blame] | 97 | } // namespace protocol |
| 98 | } // namespace remoting |