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