File: negotiating_host_authenticator.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (206 lines) | stat: -rw-r--r-- 8,113 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/protocol/negotiating_host_authenticator.h"

#include <memory>
#include <sstream>
#include <utility>

#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/notreached.h"
#include "base/strings/string_split.h"
#include "remoting/base/rsa_key_pair.h"
#include "remoting/protocol/channel_authenticator.h"
#include "remoting/protocol/credentials_type.h"
#include "remoting/protocol/host_authentication_config.h"
#include "remoting/protocol/pairing_host_authenticator.h"
#include "remoting/protocol/pairing_registry.h"
#include "remoting/protocol/session_authz_authenticator.h"
#include "remoting/protocol/spake2_authenticator.h"
#include "third_party/libjingle_xmpp/xmllite/xmlelement.h"

namespace remoting::protocol {

NegotiatingHostAuthenticator::NegotiatingHostAuthenticator(
    std::string_view local_id,
    std::string_view remote_id,
    std::unique_ptr<HostAuthenticationConfig> config)
    : NegotiatingAuthenticatorBase(WAITING_MESSAGE),
      local_id_(local_id),
      remote_id_(remote_id),
      config_(std::move(config)) {
  methods_ = config_->GetSupportedMethods();
  DCHECK(!methods_.empty());
}

NegotiatingHostAuthenticator::~NegotiatingHostAuthenticator() = default;

void NegotiatingHostAuthenticator::ProcessMessage(
    const jingle_xmpp::XmlElement* message,
    base::OnceClosure resume_callback) {
  DCHECK_EQ(state(), WAITING_MESSAGE);
  state_ = PROCESSING_MESSAGE;

  const jingle_xmpp::XmlElement* pairing_tag =
      message->FirstNamed(kPairingInfoTag);
  if (pairing_tag) {
    client_id_ = pairing_tag->Attr(kClientIdAttribute);
  }

  std::string method_attr = message->Attr(kMethodAttributeQName);
  AuthenticationMethod method = ParseAuthenticationMethodString(method_attr);

  if (current_method_ != AuthenticationMethod::INVALID &&
      method != current_method_) {
    state_ = REJECTED;
    rejection_reason_ = RejectionReason::INVALID_STATE;
    rejection_details_ = RejectionDetails(
        "The host has already chosen an authentication method. "
        "The client cannot change it.");
    std::move(resume_callback).Run();
    return;
  }

  // If the client did not specify a preferred auth method, or specified an
  // unknown or unsupported method, then select the first known method from
  // the supported-methods attribute.
  if (method == AuthenticationMethod::INVALID ||
      !base::Contains(methods_, method)) {
    method = AuthenticationMethod::INVALID;

    std::string supported_methods_attr =
        message->Attr(kSupportedMethodsAttributeQName);
    if (supported_methods_attr.empty()) {
      state_ = REJECTED;
      rejection_reason_ = RejectionReason::INVALID_ARGUMENT;
      rejection_details_ = RejectionDetails(
          "Message contains neither the 'method' nor the 'supported-methods' "
          "attributes.");
      std::move(resume_callback).Run();
      return;
    }

    // Find the first mutually-supported method in the client's list of
    // supported-methods.
    for (const std::string& method_str : base::SplitString(
             supported_methods_attr, std::string(1, kSupportedMethodsSeparator),
             base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
      AuthenticationMethod value = ParseAuthenticationMethodString(method_str);
      if (value != AuthenticationMethod::INVALID &&
          base::Contains(methods_, value)) {
        // Found common method.
        method = value;
        break;
      }
    }

    if (method == AuthenticationMethod::INVALID) {
      state_ = REJECTED;
      rejection_reason_ = RejectionReason::NO_COMMON_AUTH_METHOD;
      rejection_details_ = RejectionDetails(
          "No common authentication method found between client and host.");
      std::move(resume_callback).Run();
      return;
    }

    // Drop the current message because we've chosen a different method.
    current_method_ = method;
    CreateAuthenticator(
        MESSAGE_READY,
        base::BindOnce(&NegotiatingHostAuthenticator::UpdateState,
                       base::Unretained(this), std::move(resume_callback)));
    return;
  }

  // If the client specified a supported method, and the host hasn't chosen a
  // method yet, use the client's preferred method and process the message.
  if (current_method_ == AuthenticationMethod::INVALID) {
    current_method_ = method;
    // Copy the message since the authenticator may process it asynchronously.
    CreateAuthenticator(
        WAITING_MESSAGE,
        base::BindOnce(&NegotiatingAuthenticatorBase::ProcessMessageInternal,
                       base::Unretained(this),
                       base::Owned(new jingle_xmpp::XmlElement(*message)),
                       std::move(resume_callback)));
    return;
  }

  // If the client is using the host's current method, just process the message.
  ProcessMessageInternal(message, std::move(resume_callback));
}

std::unique_ptr<jingle_xmpp::XmlElement>
NegotiatingHostAuthenticator::GetNextMessage() {
  return GetNextMessageInternal();
}

void NegotiatingHostAuthenticator::CreateAuthenticator(
    Authenticator::State preferred_initial_state,
    base::OnceClosure resume_callback) {
  DCHECK(current_method_ != AuthenticationMethod::INVALID);

  switch (current_method_) {
    case AuthenticationMethod::INVALID:
      NOTREACHED();

    case AuthenticationMethod::CLOUD_SESSION_AUTHZ_SPAKE2_CURVE25519: {
      DCHECK_EQ(config_->session_authz_client_factory->method(),
                AuthenticationMethod::CLOUD_SESSION_AUTHZ_SPAKE2_CURVE25519);
      auto authenticator = std::make_unique<SessionAuthzAuthenticator>(
          CredentialsType::CLOUD_SESSION_AUTHZ,
          config_->session_authz_client_factory->Create(),
          base::BindRepeating(&Spake2Authenticator::CreateForHost, local_id_,
                              remote_id_, config_->local_cert,
                              config_->key_pair));
      authenticator->Start(std::move(resume_callback));
      current_authenticator_ = std::move(authenticator);
      break;
    }

    case AuthenticationMethod::CORP_SESSION_AUTHZ_SPAKE2_CURVE25519: {
      DCHECK_EQ(config_->session_authz_client_factory->method(),
                AuthenticationMethod::CORP_SESSION_AUTHZ_SPAKE2_CURVE25519);
      auto authenticator = std::make_unique<SessionAuthzAuthenticator>(
          CredentialsType::CORP_SESSION_AUTHZ,
          config_->session_authz_client_factory->Create(),
          base::BindRepeating(&Spake2Authenticator::CreateForHost, local_id_,
                              remote_id_, config_->local_cert,
                              config_->key_pair));
      authenticator->Start(std::move(resume_callback));
      current_authenticator_ = std::move(authenticator);
      break;
    }

    case AuthenticationMethod::PAIRED_SPAKE2_CURVE25519: {
      PairingHostAuthenticator* pairing_authenticator =
          new PairingHostAuthenticator(
              config_->pairing_registry,
              base::BindRepeating(&Spake2Authenticator::CreateForHost,
                                  local_id_, remote_id_, config_->local_cert,
                                  config_->key_pair),
              config_->shared_secret_hash);
      current_authenticator_.reset(pairing_authenticator);
      pairing_authenticator->Initialize(client_id_, preferred_initial_state,
                                        std::move(resume_callback));
      break;
    }

    case AuthenticationMethod::SHARED_SECRET_SPAKE2_CURVE25519:
      current_authenticator_ = Spake2Authenticator::CreateForHost(
          local_id_, remote_id_, config_->local_cert, config_->key_pair,
          config_->shared_secret_hash, preferred_initial_state);
      std::move(resume_callback).Run();
      break;
  }

  ChainStateChangeAfterAcceptedWithUnderlying(*current_authenticator_);
}

}  // namespace remoting::protocol