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