| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/security/transport/security_handshaker.h" |
| 22 |
|
| 23 |
#include <limits.h> |
| 24 |
#include <stdint.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#include <algorithm> |
| 28 |
#include <memory> |
| 29 |
#include <string> |
| 30 |
|
| 31 |
#include "absl/base/attributes.h" |
| 32 |
#include "absl/status/status.h" |
| 33 |
#include "absl/strings/str_cat.h" |
| 34 |
#include "absl/strings/string_view.h" |
| 35 |
#include "absl/types/optional.h" |
| 36 |
|
| 37 |
#include <grpc/grpc_security.h> |
| 38 |
#include <grpc/grpc_security_constants.h> |
| 39 |
#include <grpc/slice.h> |
| 40 |
#include <grpc/slice_buffer.h> |
| 41 |
#include <grpc/support/alloc.h> |
| 42 |
#include <grpc/support/log.h> |
| 43 |
|
| 44 |
#include "src/core/lib/channel/channel_args.h" |
| 45 |
#include "src/core/lib/channel/channelz.h" |
| 46 |
#include "src/core/lib/config/core_configuration.h" |
| 47 |
#include "src/core/lib/gprpp/debug_location.h" |
| 48 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 49 |
#include "src/core/lib/gprpp/status_helper.h" |
| 50 |
#include "src/core/lib/gprpp/sync.h" |
| 51 |
#include "src/core/lib/gprpp/unique_type_name.h" |
| 52 |
#include "src/core/lib/iomgr/closure.h" |
| 53 |
#include "src/core/lib/iomgr/endpoint.h" |
| 54 |
#include "src/core/lib/iomgr/error.h" |
| 55 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 56 |
#include "src/core/lib/iomgr/iomgr_fwd.h" |
| 57 |
#include "src/core/lib/iomgr/tcp_server.h" |
| 58 |
#include "src/core/lib/security/context/security_context.h" |
| 59 |
#include "src/core/lib/security/transport/secure_endpoint.h" |
| 60 |
#include "src/core/lib/security/transport/tsi_error.h" |
| 61 |
#include "src/core/lib/slice/slice.h" |
| 62 |
#include "src/core/lib/slice/slice_internal.h" |
| 63 |
#include "src/core/lib/transport/handshaker.h" |
| 64 |
#include "src/core/lib/transport/handshaker_factory.h" |
| 65 |
#include "src/core/lib/transport/handshaker_registry.h" |
| 66 |
#include "src/core/tsi/transport_security_grpc.h" |
| 67 |
|
| 68 |
#define GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE 256 |
| 69 |
|
| 70 |
namespace grpc_core { |
| 71 |
|
| 72 |
namespace { |
| 73 |
|
| 74 |
class SecurityHandshaker : public Handshaker { |
| 75 |
public: |
| 76 |
SecurityHandshaker(tsi_handshaker* handshaker, |
| 77 |
grpc_security_connector* connector, |
| 78 |
const ChannelArgs& args); |
| 79 |
~SecurityHandshaker() override; |
| 80 |
void Shutdown(grpc_error_handle why) override; |
| 81 |
void DoHandshake(grpc_tcp_server_acceptor* acceptor, |
| 82 |
grpc_closure* on_handshake_done, |
| 83 |
HandshakerArgs* args) override; |
| 84 |
const char* name() const override { return "security"; } |
| 85 |
|
| 86 |
private: |
| 87 |
grpc_error_handle DoHandshakerNextLocked(const unsigned char* bytes_received, |
| 88 |
size_t bytes_received_size); |
| 89 |
|
| 90 |
grpc_error_handle OnHandshakeNextDoneLocked( |
| 91 |
tsi_result result, const unsigned char* bytes_to_send, |
| 92 |
size_t bytes_to_send_size, tsi_handshaker_result* handshaker_result); |
| 93 |
void HandshakeFailedLocked(grpc_error_handle error); |
| 94 |
void CleanupArgsForFailureLocked(); |
| 95 |
|
| 96 |
static void OnHandshakeDataReceivedFromPeerFn(void* arg, |
| 97 |
grpc_error_handle error); |
| 98 |
static void OnHandshakeDataSentToPeerFn(void* arg, grpc_error_handle error); |
| 99 |
static void OnHandshakeDataReceivedFromPeerFnScheduler( |
| 100 |
void* arg, grpc_error_handle error); |
| 101 |
static void OnHandshakeDataSentToPeerFnScheduler(void* arg, |
| 102 |
grpc_error_handle error); |
| 103 |
static void OnHandshakeNextDoneGrpcWrapper( |
| 104 |
tsi_result result, void* user_data, const unsigned char* bytes_to_send, |
| 105 |
size_t bytes_to_send_size, tsi_handshaker_result* handshaker_result); |
| 106 |
static void OnPeerCheckedFn(void* arg, grpc_error_handle error); |
| 107 |
void OnPeerCheckedInner(grpc_error_handle error); |
| 108 |
size_t MoveReadBufferIntoHandshakeBuffer(); |
| 109 |
grpc_error_handle CheckPeerLocked(); |
| 110 |
|
| 111 |
// State set at creation time. |
| 112 |
tsi_handshaker* handshaker_; |
| 113 |
RefCountedPtr<grpc_security_connector> connector_; |
| 114 |
|
| 115 |
Mutex mu_; |
| 116 |
|
| 117 |
bool is_shutdown_ = false; |
| 118 |
// Endpoint and read buffer to destroy after a shutdown. |
| 119 |
grpc_endpoint* endpoint_to_destroy_ = nullptr; |
| 120 |
grpc_slice_buffer* read_buffer_to_destroy_ = nullptr; |
| 121 |
|
| 122 |
// State saved while performing the handshake. |
| 123 |
HandshakerArgs* args_ = nullptr; |
| 124 |
grpc_closure* on_handshake_done_ = nullptr; |
| 125 |
|
| 126 |
size_t handshake_buffer_size_; |
| 127 |
unsigned char* handshake_buffer_; |
| 128 |
grpc_slice_buffer outgoing_; |
| 129 |
grpc_closure on_handshake_data_sent_to_peer_; |
| 130 |
grpc_closure on_handshake_data_received_from_peer_; |
| 131 |
grpc_closure on_peer_checked_; |
| 132 |
RefCountedPtr<grpc_auth_context> auth_context_; |
| 133 |
tsi_handshaker_result* handshaker_result_ = nullptr; |
| 134 |
size_t max_frame_size_ = 0; |
| 135 |
std::string tsi_handshake_error_; |
| 136 |
}; |
| 137 |
|
| 138 |
SecurityHandshaker::SecurityHandshaker(tsi_handshaker* handshaker, |
| 139 |
grpc_security_connector* connector, |
| 140 |
const ChannelArgs& args) |
| 141 |
: handshaker_(handshaker), |
| 142 |
connector_(connector->Ref(DEBUG_LOCATION, "handshake")), |
| 143 |
handshake_buffer_size_(GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE), |
| 144 |
handshake_buffer_( |
| 145 |
static_cast<uint8_t*>(gpr_malloc(handshake_buffer_size_))), |
| 146 |
max_frame_size_( |
| 147 |
std::max(0, args.GetInt(GRPC_ARG_TSI_MAX_FRAME_SIZE).value_or(0))) { |
| 148 |
grpc_slice_buffer_init(&outgoing_); |
| 149 |
GRPC_CLOSURE_INIT(&on_peer_checked_, &SecurityHandshaker::OnPeerCheckedFn, |
| 150 |
this, grpc_schedule_on_exec_ctx); |
| 151 |
} |
| 152 |
|
| 153 |
SecurityHandshaker::~SecurityHandshaker() { |
| 154 |
tsi_handshaker_destroy(handshaker_); |
| 155 |
tsi_handshaker_result_destroy(handshaker_result_); |
| 156 |
if (endpoint_to_destroy_ != nullptr) { |
| 157 |
grpc_endpoint_destroy(endpoint_to_destroy_); |
| 158 |
} |
| 159 |
if (read_buffer_to_destroy_ != nullptr) { |
| 160 |
grpc_slice_buffer_destroy(read_buffer_to_destroy_); |
| 161 |
gpr_free(read_buffer_to_destroy_); |
| 162 |
} |
| 163 |
gpr_free(handshake_buffer_); |
| 164 |
grpc_slice_buffer_destroy(&outgoing_); |
| 165 |
auth_context_.reset(DEBUG_LOCATION, "handshake"); |
| 166 |
connector_.reset(DEBUG_LOCATION, "handshake"); |
| 167 |
} |
| 168 |
|
| 169 |
size_t SecurityHandshaker::MoveReadBufferIntoHandshakeBuffer() { |
| 170 |
size_t bytes_in_read_buffer = args_->read_buffer->length; |
| 171 |
if (handshake_buffer_size_ < bytes_in_read_buffer) { |
| 172 |
handshake_buffer_ = static_cast<uint8_t*>( |
| 173 |
gpr_realloc(handshake_buffer_, bytes_in_read_buffer)); |
| 174 |
handshake_buffer_size_ = bytes_in_read_buffer; |
| 175 |
} |
| 176 |
size_t offset = 0; |
| 177 |
while (args_->read_buffer->count > 0) { |
| 178 |
grpc_slice* next_slice = grpc_slice_buffer_peek_first(args_->read_buffer); |
| 179 |
memcpy(handshake_buffer_ + offset, GRPC_SLICE_START_PTR(*next_slice), |
| 180 |
GRPC_SLICE_LENGTH(*next_slice)); |
| 181 |
offset += GRPC_SLICE_LENGTH(*next_slice); |
| 182 |
grpc_slice_buffer_remove_first(args_->read_buffer); |
| 183 |
} |
| 184 |
return bytes_in_read_buffer; |
| 185 |
} |
| 186 |
|
| 187 |
// Set args_ fields to NULL, saving the endpoint and read buffer for |
| 188 |
// later destruction. |
| 189 |
void SecurityHandshaker::CleanupArgsForFailureLocked() { |
| 190 |
endpoint_to_destroy_ = args_->endpoint; |
| 191 |
args_->endpoint = nullptr; |
| 192 |
read_buffer_to_destroy_ = args_->read_buffer; |
| 193 |
args_->read_buffer = nullptr; |
| 194 |
args_->args = ChannelArgs(); |
| 195 |
} |
| 196 |
|
| 197 |
// If the handshake failed or we're shutting down, clean up and invoke the |
| 198 |
// callback with the error. |
| 199 |
void SecurityHandshaker::HandshakeFailedLocked(grpc_error_handle error) { |
| 200 |
if (error.ok()) { |
| 201 |
// If we were shut down after the handshake succeeded but before an |
| 202 |
// endpoint callback was invoked, we need to generate our own error. |
| 203 |
error = GRPC_ERROR_CREATE("Handshaker shutdown"); |
| 204 |
} |
| 205 |
gpr_log(GPR_DEBUG, "Security handshake failed: %s", |
| 206 |
StatusToString(error).c_str()); |
| 207 |
if (!is_shutdown_) { |
| 208 |
tsi_handshaker_shutdown(handshaker_); |
| 209 |
// TODO(ctiller): It is currently necessary to shutdown endpoints |
| 210 |
// before destroying them, even if we know that there are no |
| 211 |
// pending read/write callbacks. This should be fixed, at which |
| 212 |
// point this can be removed. |
| 213 |
grpc_endpoint_shutdown(args_->endpoint, error); |
| 214 |
// Not shutting down, so the write failed. Clean up before |
| 215 |
// invoking the callback. |
| 216 |
CleanupArgsForFailureLocked(); |
| 217 |
// Set shutdown to true so that subsequent calls to |
| 218 |
// security_handshaker_shutdown() do nothing. |
| 219 |
is_shutdown_ = true; |
| 220 |
} |
| 221 |
// Invoke callback. |
| 222 |
ExecCtx::Run(DEBUG_LOCATION, on_handshake_done_, error); |
| 223 |
} |
| 224 |
|
| 225 |
namespace { |
| 226 |
|
| 227 |
RefCountedPtr<channelz::SocketNode::Security> |
| 228 |
MakeChannelzSecurityFromAuthContext(grpc_auth_context* auth_context) { |
| 229 |
RefCountedPtr<channelz::SocketNode::Security> security = |
| 230 |
MakeRefCounted<channelz::SocketNode::Security>(); |
| 231 |
// TODO(yashykt): Currently, we are assuming TLS by default and are only able |
| 232 |
// to fill in the remote certificate but we should ideally be able to fill in |
| 233 |
// other fields in |
| 234 |
// https://github.com/grpc/grpc/blob/fcd43e90304862a823316b224ee733d17a8cfd90/src/proto/grpc/channelz/channelz.proto#L326 |
| 235 |
// from grpc_auth_context. |
| 236 |
security->type = channelz::SocketNode::Security::ModelType::kTls; |
| 237 |
security->tls = absl::make_optional<channelz::SocketNode::Security::Tls>(); |
| 238 |
grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name( |
| 239 |
auth_context, GRPC_X509_PEM_CERT_PROPERTY_NAME); |
| 240 |
const grpc_auth_property* prop = grpc_auth_property_iterator_next(&it); |
| 241 |
if (prop != nullptr) { |
| 242 |
security->tls->remote_certificate = |
| 243 |
std::string(prop->value, prop->value_length); |
| 244 |
} |
| 245 |
return security; |
| 246 |
} |
| 247 |
|
| 248 |
} // namespace |
| 249 |
|
| 250 |
void SecurityHandshaker::OnPeerCheckedInner(grpc_error_handle error) { |
| 251 |
MutexLock lock(&mu_); |
| 252 |
if (!error.ok() || is_shutdown_) { |
| 253 |
HandshakeFailedLocked(error); |
| 254 |
return; |
| 255 |
} |
| 256 |
// Get unused bytes. |
| 257 |
const unsigned char* unused_bytes = nullptr; |
| 258 |
size_t unused_bytes_size = 0; |
| 259 |
tsi_result result = tsi_handshaker_result_get_unused_bytes( |
| 260 |
handshaker_result_, &unused_bytes, &unused_bytes_size); |
| 261 |
if (result != TSI_OK) { |
| 262 |
HandshakeFailedLocked(grpc_set_tsi_error_result( |
| 263 |
GRPC_ERROR_CREATE( |
| 264 |
"TSI handshaker result does not provide unused bytes"), |
| 265 |
result)); |
| 266 |
return; |
| 267 |
} |
| 268 |
// Check whether we need to wrap the endpoint. |
| 269 |
tsi_frame_protector_type frame_protector_type; |
| 270 |
result = tsi_handshaker_result_get_frame_protector_type( |
| 271 |
handshaker_result_, &frame_protector_type); |
| 272 |
if (result != TSI_OK) { |
| 273 |
HandshakeFailedLocked(grpc_set_tsi_error_result( |
| 274 |
GRPC_ERROR_CREATE("TSI handshaker result does not implement " |
| 275 |
"get_frame_protector_type"), |
| 276 |
result)); |
| 277 |
return; |
| 278 |
} |
| 279 |
tsi_zero_copy_grpc_protector* zero_copy_protector = nullptr; |
| 280 |
tsi_frame_protector* protector = nullptr; |
| 281 |
switch (frame_protector_type) { |
| 282 |
case TSI_FRAME_PROTECTOR_ZERO_COPY: |
| 283 |
ABSL_FALLTHROUGH_INTENDED; |
| 284 |
case TSI_FRAME_PROTECTOR_NORMAL_OR_ZERO_COPY: |
| 285 |
// Create zero-copy frame protector. |
| 286 |
result = tsi_handshaker_result_create_zero_copy_grpc_protector( |
| 287 |
handshaker_result_, max_frame_size_ == 0 ? nullptr : &max_frame_size_, |
| 288 |
&zero_copy_protector); |
| 289 |
if (result != TSI_OK) { |
| 290 |
HandshakeFailedLocked(grpc_set_tsi_error_result( |
| 291 |
GRPC_ERROR_CREATE("Zero-copy frame protector creation failed"), |
| 292 |
result)); |
| 293 |
return; |
| 294 |
} |
| 295 |
break; |
| 296 |
case TSI_FRAME_PROTECTOR_NORMAL: |
| 297 |
// Create normal frame protector. |
| 298 |
result = tsi_handshaker_result_create_frame_protector( |
| 299 |
handshaker_result_, max_frame_size_ == 0 ? nullptr : &max_frame_size_, |
| 300 |
&protector); |
| 301 |
if (result != TSI_OK) { |
| 302 |
HandshakeFailedLocked(grpc_set_tsi_error_result( |
| 303 |
GRPC_ERROR_CREATE("Frame protector creation failed"), result)); |
| 304 |
return; |
| 305 |
} |
| 306 |
break; |
| 307 |
case TSI_FRAME_PROTECTOR_NONE: |
| 308 |
break; |
| 309 |
} |
| 310 |
bool has_frame_protector = |
| 311 |
zero_copy_protector != nullptr || protector != nullptr; |
| 312 |
// If we have a frame protector, create a secure endpoint. |
| 313 |
if (has_frame_protector) { |
| 314 |
if (unused_bytes_size > 0) { |
| 315 |
grpc_slice slice = grpc_slice_from_copied_buffer( |
| 316 |
reinterpret_cast<const char*>(unused_bytes), unused_bytes_size); |
| 317 |
args_->endpoint = grpc_secure_endpoint_create( |
| 318 |
protector, zero_copy_protector, args_->endpoint, &slice, |
| 319 |
args_->args.ToC().get(), 1); |
| 320 |
CSliceUnref(slice); |
| 321 |
} else { |
| 322 |
args_->endpoint = grpc_secure_endpoint_create( |
| 323 |
protector, zero_copy_protector, args_->endpoint, nullptr, |
| 324 |
args_->args.ToC().get(), 0); |
| 325 |
} |
| 326 |
} else if (unused_bytes_size > 0) { |
| 327 |
// Not wrapping the endpoint, so just pass along unused bytes. |
| 328 |
grpc_slice slice = grpc_slice_from_copied_buffer( |
| 329 |
reinterpret_cast<const char*>(unused_bytes), unused_bytes_size); |
| 330 |
grpc_slice_buffer_add(args_->read_buffer, slice); |
| 331 |
} |
| 332 |
// Done with handshaker result. |
| 333 |
tsi_handshaker_result_destroy(handshaker_result_); |
| 334 |
handshaker_result_ = nullptr; |
| 335 |
args_->args = args_->args.SetObject(auth_context_); |
| 336 |
// Add channelz channel args only if frame protector is created. |
| 337 |
if (has_frame_protector) { |
| 338 |
args_->args = args_->args.SetObject( |
| 339 |
MakeChannelzSecurityFromAuthContext(auth_context_.get())); |
| 340 |
} |
| 341 |
// Invoke callback. |
| 342 |
ExecCtx::Run(DEBUG_LOCATION, on_handshake_done_, absl::OkStatus()); |
| 343 |
// Set shutdown to true so that subsequent calls to |
| 344 |
// security_handshaker_shutdown() do nothing. |
| 345 |
is_shutdown_ = true; |
| 346 |
} |
| 347 |
|
| 348 |
void SecurityHandshaker::OnPeerCheckedFn(void* arg, grpc_error_handle error) { |
| 349 |
RefCountedPtr<SecurityHandshaker>(static_cast<SecurityHandshaker*>(arg)) |
| 350 |
->OnPeerCheckedInner(error); |
| 351 |
} |
| 352 |
|
| 353 |
grpc_error_handle SecurityHandshaker::CheckPeerLocked() { |
| 354 |
tsi_peer peer; |
| 355 |
tsi_result result = |
| 356 |
tsi_handshaker_result_extract_peer(handshaker_result_, &peer); |
| 357 |
if (result != TSI_OK) { |
| 358 |
return grpc_set_tsi_error_result( |
| 359 |
GRPC_ERROR_CREATE("Peer extraction failed"), result); |
| 360 |
} |
| 361 |
connector_->check_peer(peer, args_->endpoint, args_->args, &auth_context_, |
| 362 |
&on_peer_checked_); |
| 363 |
return absl::OkStatus(); |
| 364 |
} |
| 365 |
|
| 366 |
grpc_error_handle SecurityHandshaker::OnHandshakeNextDoneLocked( |
| 367 |
tsi_result result, const unsigned char* bytes_to_send, |
| 368 |
size_t bytes_to_send_size, tsi_handshaker_result* handshaker_result) { |
| 369 |
grpc_error_handle error; |
| 370 |
// Handshaker was shutdown. |
| 371 |
if (is_shutdown_) { |
| 372 |
return GRPC_ERROR_CREATE("Handshaker shutdown"); |
| 373 |
} |
| 374 |
// Read more if we need to. |
| 375 |
if (result == TSI_INCOMPLETE_DATA) { |
| 376 |
GPR_ASSERT(bytes_to_send_size == 0); |
| 377 |
grpc_endpoint_read( |
| 378 |
args_->endpoint, args_->read_buffer, |
| 379 |
GRPC_CLOSURE_INIT( |
| 380 |
&on_handshake_data_received_from_peer_, |
| 381 |
&SecurityHandshaker::OnHandshakeDataReceivedFromPeerFnScheduler, |
| 382 |
this, grpc_schedule_on_exec_ctx), |
| 383 |
/*urgent=*/true, /*min_progress_size=*/1); |
| 384 |
return error; |
| 385 |
} |
| 386 |
if (result != TSI_OK) { |
| 387 |
auto* security_connector = args_->args.GetObject<grpc_security_connector>(); |
| 388 |
absl::string_view connector_type = "<unknown>"; |
| 389 |
if (security_connector != nullptr) { |
| 390 |
connector_type = security_connector->type().name(); |
| 391 |
} |
| 392 |
return grpc_set_tsi_error_result( |
| 393 |
GRPC_ERROR_CREATE(absl::StrCat( |
| 394 |
connector_type, " handshake failed", |
| 395 |
(tsi_handshake_error_.empty() ? "" : ": "), tsi_handshake_error_)), |
| 396 |
result); |
| 397 |
} |
| 398 |
// Update handshaker result. |
| 399 |
if (handshaker_result != nullptr) { |
| 400 |
GPR_ASSERT(handshaker_result_ == nullptr); |
| 401 |
handshaker_result_ = handshaker_result; |
| 402 |
} |
| 403 |
if (bytes_to_send_size > 0) { |
| 404 |
// Send data to peer, if needed. |
| 405 |
grpc_slice to_send = grpc_slice_from_copied_buffer( |
| 406 |
reinterpret_cast<const char*>(bytes_to_send), bytes_to_send_size); |
| 407 |
grpc_slice_buffer_reset_and_unref(&outgoing_); |
| 408 |
grpc_slice_buffer_add(&outgoing_, to_send); |
| 409 |
grpc_endpoint_write( |
| 410 |
args_->endpoint, &outgoing_, |
| 411 |
GRPC_CLOSURE_INIT( |
| 412 |
&on_handshake_data_sent_to_peer_, |
| 413 |
&SecurityHandshaker::OnHandshakeDataSentToPeerFnScheduler, this, |
| 414 |
grpc_schedule_on_exec_ctx), |
| 415 |
nullptr, /*max_frame_size=*/INT_MAX); |
| 416 |
} else if (handshaker_result == nullptr) { |
| 417 |
// There is nothing to send, but need to read from peer. |
| 418 |
grpc_endpoint_read( |
| 419 |
args_->endpoint, args_->read_buffer, |
| 420 |
GRPC_CLOSURE_INIT( |
| 421 |
&on_handshake_data_received_from_peer_, |
| 422 |
&SecurityHandshaker::OnHandshakeDataReceivedFromPeerFnScheduler, |
| 423 |
this, grpc_schedule_on_exec_ctx), |
| 424 |
/*urgent=*/true, /*min_progress_size=*/1); |
| 425 |
} else { |
| 426 |
// Handshake has finished, check peer and so on. |
| 427 |
error = CheckPeerLocked(); |
| 428 |
} |
| 429 |
return error; |
| 430 |
} |
| 431 |
|
| 432 |
void SecurityHandshaker::OnHandshakeNextDoneGrpcWrapper( |
| 433 |
tsi_result result, void* user_data, const unsigned char* bytes_to_send, |
| 434 |
size_t bytes_to_send_size, tsi_handshaker_result* handshaker_result) { |
| 435 |
RefCountedPtr<SecurityHandshaker> h( |
| 436 |
static_cast<SecurityHandshaker*>(user_data)); |
| 437 |
MutexLock lock(&h->mu_); |
| 438 |
grpc_error_handle error = h->OnHandshakeNextDoneLocked( |
| 439 |
result, bytes_to_send, bytes_to_send_size, handshaker_result); |
| 440 |
if (!error.ok()) { |
| 441 |
h->HandshakeFailedLocked(error); |
| 442 |
} else { |
| 443 |
h.release(); // Avoid unref |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
grpc_error_handle SecurityHandshaker::DoHandshakerNextLocked( |
| 448 |
const unsigned char* bytes_received, size_t bytes_received_size) { |
| 449 |
// Invoke TSI handshaker. |
| 450 |
const unsigned char* bytes_to_send = nullptr; |
| 451 |
size_t bytes_to_send_size = 0; |
| 452 |
tsi_handshaker_result* hs_result = nullptr; |
| 453 |
tsi_result result = tsi_handshaker_next( |
| 454 |
handshaker_, bytes_received, bytes_received_size, &bytes_to_send, |
| 455 |
&bytes_to_send_size, &hs_result, &OnHandshakeNextDoneGrpcWrapper, this, |
| 456 |
&tsi_handshake_error_); |
| 457 |
if (result == TSI_ASYNC) { |
| 458 |
// Handshaker operating asynchronously. Nothing else to do here; |
| 459 |
// callback will be invoked in a TSI thread. |
| 460 |
return absl::OkStatus(); |
| 461 |
} |
| 462 |
// Handshaker returned synchronously. Invoke callback directly in |
| 463 |
// this thread with our existing exec_ctx. |
| 464 |
return OnHandshakeNextDoneLocked(result, bytes_to_send, bytes_to_send_size, |
| 465 |
hs_result); |
| 466 |
} |
| 467 |
|
| 468 |
// This callback might be run inline while we are still holding on to the mutex, |
| 469 |
// so schedule OnHandshakeDataReceivedFromPeerFn on ExecCtx to avoid a deadlock. |
| 470 |
void SecurityHandshaker::OnHandshakeDataReceivedFromPeerFnScheduler( |
| 471 |
void* arg, grpc_error_handle error) { |
| 472 |
SecurityHandshaker* h = static_cast<SecurityHandshaker*>(arg); |
| 473 |
ExecCtx::Run( |
| 474 |
DEBUG_LOCATION, |
| 475 |
GRPC_CLOSURE_INIT(&h->on_handshake_data_received_from_peer_, |
| 476 |
&SecurityHandshaker::OnHandshakeDataReceivedFromPeerFn, |
| 477 |
h, grpc_schedule_on_exec_ctx), |
| 478 |
error); |
| 479 |
} |
| 480 |
|
| 481 |
void SecurityHandshaker::OnHandshakeDataReceivedFromPeerFn( |
| 482 |
void* arg, grpc_error_handle error) { |
| 483 |
RefCountedPtr<SecurityHandshaker> h(static_cast<SecurityHandshaker*>(arg)); |
| 484 |
MutexLock lock(&h->mu_); |
| 485 |
if (!error.ok() || h->is_shutdown_) { |
| 486 |
h->HandshakeFailedLocked( |
| 487 |
GRPC_ERROR_CREATE_REFERENCING("Handshake read failed", &error, 1)); |
| 488 |
return; |
| 489 |
} |
| 490 |
// Copy all slices received. |
| 491 |
size_t bytes_received_size = h->MoveReadBufferIntoHandshakeBuffer(); |
| 492 |
// Call TSI handshaker. |
| 493 |
error = h->DoHandshakerNextLocked(h->handshake_buffer_, bytes_received_size); |
| 494 |
if (!error.ok()) { |
| 495 |
h->HandshakeFailedLocked(error); |
| 496 |
} else { |
| 497 |
h.release(); // Avoid unref |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
// This callback might be run inline while we are still holding on to the mutex, |
| 502 |
// so schedule OnHandshakeDataSentToPeerFn on ExecCtx to avoid a deadlock. |
| 503 |
void SecurityHandshaker::OnHandshakeDataSentToPeerFnScheduler( |
| 504 |
void* arg, grpc_error_handle error) { |
| 505 |
SecurityHandshaker* h = static_cast<SecurityHandshaker*>(arg); |
| 506 |
ExecCtx::Run( |
| 507 |
DEBUG_LOCATION, |
| 508 |
GRPC_CLOSURE_INIT(&h->on_handshake_data_sent_to_peer_, |
| 509 |
&SecurityHandshaker::OnHandshakeDataSentToPeerFn, h, |
| 510 |
grpc_schedule_on_exec_ctx), |
| 511 |
error); |
| 512 |
} |
| 513 |
|
| 514 |
void SecurityHandshaker::OnHandshakeDataSentToPeerFn(void* arg, |
| 515 |
grpc_error_handle error) { |
| 516 |
RefCountedPtr<SecurityHandshaker> h(static_cast<SecurityHandshaker*>(arg)); |
| 517 |
MutexLock lock(&h->mu_); |
| 518 |
if (!error.ok() || h->is_shutdown_) { |
| 519 |
h->HandshakeFailedLocked( |
| 520 |
GRPC_ERROR_CREATE_REFERENCING("Handshake write failed", &error, 1)); |
| 521 |
return; |
| 522 |
} |
| 523 |
// We may be done. |
| 524 |
if (h->handshaker_result_ == nullptr) { |
| 525 |
grpc_endpoint_read( |
| 526 |
h->args_->endpoint, h->args_->read_buffer, |
| 527 |
GRPC_CLOSURE_INIT( |
| 528 |
&h->on_handshake_data_received_from_peer_, |
| 529 |
&SecurityHandshaker::OnHandshakeDataReceivedFromPeerFnScheduler, |
| 530 |
h.get(), grpc_schedule_on_exec_ctx), |
| 531 |
/*urgent=*/true, /*min_progress_size=*/1); |
| 532 |
} else { |
| 533 |
error = h->CheckPeerLocked(); |
| 534 |
if (!error.ok()) { |
| 535 |
h->HandshakeFailedLocked(error); |
| 536 |
return; |
| 537 |
} |
| 538 |
} |
| 539 |
h.release(); // Avoid unref |
| 540 |
} |
| 541 |
|
| 542 |
// |
| 543 |
// public handshaker API |
| 544 |
// |
| 545 |
|
| 546 |
void SecurityHandshaker::Shutdown(grpc_error_handle why) { |
| 547 |
MutexLock lock(&mu_); |
| 548 |
if (!is_shutdown_) { |
| 549 |
is_shutdown_ = true; |
| 550 |
connector_->cancel_check_peer(&on_peer_checked_, why); |
| 551 |
tsi_handshaker_shutdown(handshaker_); |
| 552 |
grpc_endpoint_shutdown(args_->endpoint, why); |
| 553 |
CleanupArgsForFailureLocked(); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
void SecurityHandshaker::DoHandshake(grpc_tcp_server_acceptor* /*acceptor*/, |
| 558 |
grpc_closure* on_handshake_done, |
| 559 |
HandshakerArgs* args) { |
| 560 |
auto ref = Ref(); |
| 561 |
MutexLock lock(&mu_); |
| 562 |
args_ = args; |
| 563 |
on_handshake_done_ = on_handshake_done; |
| 564 |
size_t bytes_received_size = MoveReadBufferIntoHandshakeBuffer(); |
| 565 |
grpc_error_handle error = |
| 566 |
DoHandshakerNextLocked(handshake_buffer_, bytes_received_size); |
| 567 |
if (!error.ok()) { |
| 568 |
HandshakeFailedLocked(error); |
| 569 |
} else { |
| 570 |
ref.release(); // Avoid unref |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
// |
| 575 |
// FailHandshaker |
| 576 |
// |
| 577 |
|
| 578 |
class FailHandshaker : public Handshaker { |
| 579 |
public: |
| 580 |
const char* name() const override { return "security_fail"; } |
| 581 |
void Shutdown(grpc_error_handle /*why*/) override {} |
| 582 |
void DoHandshake(grpc_tcp_server_acceptor* /*acceptor*/, |
| 583 |
grpc_closure* on_handshake_done, |
| 584 |
HandshakerArgs* args) override { |
| 585 |
grpc_error_handle error = |
| 586 |
GRPC_ERROR_CREATE("Failed to create security handshaker"); |
| 587 |
grpc_endpoint_shutdown(args->endpoint, error); |
| 588 |
grpc_endpoint_destroy(args->endpoint); |
| 589 |
args->endpoint = nullptr; |
| 590 |
args->args = ChannelArgs(); |
| 591 |
grpc_slice_buffer_destroy(args->read_buffer); |
| 592 |
gpr_free(args->read_buffer); |
| 593 |
args->read_buffer = nullptr; |
| 594 |
ExecCtx::Run(DEBUG_LOCATION, on_handshake_done, error); |
| 595 |
} |
| 596 |
|
| 597 |
private: |
| 598 |
~FailHandshaker() override = default; |
| 599 |
}; |
| 600 |
|
| 601 |
// |
| 602 |
// handshaker factories |
| 603 |
// |
| 604 |
|
| 605 |
class ClientSecurityHandshakerFactory : public HandshakerFactory { |
| 606 |
public: |
| 607 |
void AddHandshakers(const ChannelArgs& args, |
| 608 |
grpc_pollset_set* interested_parties, |
| 609 |
HandshakeManager* handshake_mgr) override { |
| 610 |
auto* security_connector = |
| 611 |
args.GetObject<grpc_channel_security_connector>(); |
| 612 |
if (security_connector) { |
| 613 |
security_connector->add_handshakers(args, interested_parties, |
| 614 |
handshake_mgr); |
| 615 |
} |
| 616 |
} |
| 617 |
HandshakerPriority Priority() override { |
| 618 |
return HandshakerPriority::kSecurityHandshakers; |
| 619 |
} |
| 620 |
~ClientSecurityHandshakerFactory() override = default; |
| 621 |
}; |
| 622 |
|
| 623 |
class ServerSecurityHandshakerFactory : public HandshakerFactory { |
| 624 |
public: |
| 625 |
void AddHandshakers(const ChannelArgs& args, |
| 626 |
grpc_pollset_set* interested_parties, |
| 627 |
HandshakeManager* handshake_mgr) override { |
| 628 |
auto* security_connector = args.GetObject<grpc_server_security_connector>(); |
| 629 |
if (security_connector) { |
| 630 |
security_connector->add_handshakers(args, interested_parties, |
| 631 |
handshake_mgr); |
| 632 |
} |
| 633 |
} |
| 634 |
HandshakerPriority Priority() override { |
| 635 |
return HandshakerPriority::kSecurityHandshakers; |
| 636 |
} |
| 637 |
~ServerSecurityHandshakerFactory() override = default; |
| 638 |
}; |
| 639 |
|
| 640 |
} // namespace |
| 641 |
|
| 642 |
// |
| 643 |
// exported functions |
| 644 |
// |
| 645 |
|
| 646 |
RefCountedPtr<Handshaker> SecurityHandshakerCreate( |
| 647 |
tsi_handshaker* handshaker, grpc_security_connector* connector, |
| 648 |
const ChannelArgs& args) { |
| 649 |
// If no TSI handshaker was created, return a handshaker that always fails. |
| 650 |
// Otherwise, return a real security handshaker. |
| 651 |
if (handshaker == nullptr) { |
| 652 |
return MakeRefCounted<FailHandshaker>(); |
| 653 |
} else { |
| 654 |
return MakeRefCounted<SecurityHandshaker>(handshaker, connector, args); |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
void SecurityRegisterHandshakerFactories(CoreConfiguration::Builder* builder) { |
| 659 |
builder->handshaker_registry()->RegisterHandshakerFactory( |
| 660 |
HANDSHAKER_CLIENT, std::make_unique<ClientSecurityHandshakerFactory>()); |
| 661 |
builder->handshaker_registry()->RegisterHandshakerFactory( |
| 662 |
HANDSHAKER_SERVER, std::make_unique<ServerSecurityHandshakerFactory>()); |
| 663 |
} |
| 664 |
|
| 665 |
} // namespace grpc_core |
| 666 |
|
| 667 |
grpc_handshaker* grpc_security_handshaker_create( |
| 668 |
tsi_handshaker* handshaker, grpc_security_connector* connector, |
| 669 |
const grpc_channel_args* args) { |
| 670 |
return SecurityHandshakerCreate(handshaker, connector, |
| 671 |
grpc_core::ChannelArgs::FromC(args)) |
| 672 |
.release(); |
| 673 |
} |