| 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/secure_endpoint.h" |
| 22 |
|
| 23 |
#include <inttypes.h> |
| 24 |
|
| 25 |
#include <algorithm> |
| 26 |
#include <atomic> |
| 27 |
#include <memory> |
| 28 |
|
| 29 |
#include "absl/base/thread_annotations.h" |
| 30 |
#include "absl/status/status.h" |
| 31 |
#include "absl/strings/str_cat.h" |
| 32 |
#include "absl/strings/string_view.h" |
| 33 |
#include "absl/types/optional.h" |
| 34 |
|
| 35 |
#include <grpc/event_engine/memory_allocator.h> |
| 36 |
#include <grpc/event_engine/memory_request.h> |
| 37 |
#include <grpc/slice.h> |
| 38 |
#include <grpc/slice_buffer.h> |
| 39 |
#include <grpc/support/alloc.h> |
| 40 |
#include <grpc/support/atm.h> |
| 41 |
#include <grpc/support/log.h> |
| 42 |
#include <grpc/support/sync.h> |
| 43 |
|
| 44 |
#include "src/core/lib/debug/trace.h" |
| 45 |
#include "src/core/lib/gpr/string.h" |
| 46 |
#include "src/core/lib/gprpp/debug_location.h" |
| 47 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 48 |
#include "src/core/lib/gprpp/sync.h" |
| 49 |
#include "src/core/lib/iomgr/closure.h" |
| 50 |
#include "src/core/lib/iomgr/error.h" |
| 51 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 52 |
#include "src/core/lib/iomgr/iomgr_fwd.h" |
| 53 |
#include "src/core/lib/resource_quota/api.h" |
| 54 |
#include "src/core/lib/resource_quota/memory_quota.h" |
| 55 |
#include "src/core/lib/resource_quota/resource_quota.h" |
| 56 |
#include "src/core/lib/resource_quota/trace.h" |
| 57 |
#include "src/core/lib/security/transport/tsi_error.h" |
| 58 |
#include "src/core/lib/slice/slice.h" |
| 59 |
#include "src/core/lib/slice/slice_string_helpers.h" |
| 60 |
#include "src/core/tsi/transport_security_grpc.h" |
| 61 |
#include "src/core/tsi/transport_security_interface.h" |
| 62 |
|
| 63 |
#define STAGING_BUFFER_SIZE 8192 |
| 64 |
|
| 65 |
static void on_read(void* user_data, grpc_error_handle error); |
| 66 |
|
| 67 |
namespace { |
| 68 |
struct secure_endpoint { |
| 69 |
secure_endpoint(const grpc_endpoint_vtable* vtable, |
| 70 |
tsi_frame_protector* protector, |
| 71 |
tsi_zero_copy_grpc_protector* zero_copy_protector, |
| 72 |
grpc_endpoint* transport, grpc_slice* leftover_slices, |
| 73 |
const grpc_channel_args* channel_args, |
| 74 |
size_t leftover_nslices) |
| 75 |
: wrapped_ep(transport), |
| 76 |
protector(protector), |
| 77 |
zero_copy_protector(zero_copy_protector) { |
| 78 |
base.vtable = vtable; |
| 79 |
gpr_mu_init(&protector_mu); |
| 80 |
GRPC_CLOSURE_INIT(&on_read, ::on_read, this, grpc_schedule_on_exec_ctx); |
| 81 |
grpc_slice_buffer_init(&source_buffer); |
| 82 |
grpc_slice_buffer_init(&leftover_bytes); |
| 83 |
for (size_t i = 0; i < leftover_nslices; i++) { |
| 84 |
grpc_slice_buffer_add(&leftover_bytes, |
| 85 |
grpc_core::CSliceRef(leftover_slices[i])); |
| 86 |
} |
| 87 |
grpc_slice_buffer_init(&output_buffer); |
| 88 |
memory_owner = |
| 89 |
grpc_core::ResourceQuotaFromChannelArgs(channel_args) |
| 90 |
->memory_quota() |
| 91 |
->CreateMemoryOwner(absl::StrCat(grpc_endpoint_get_peer(transport), |
| 92 |
":secure_endpoint")); |
| 93 |
self_reservation = memory_owner.MakeReservation(sizeof(*this)); |
| 94 |
if (zero_copy_protector) { |
| 95 |
read_staging_buffer = grpc_empty_slice(); |
| 96 |
write_staging_buffer = grpc_empty_slice(); |
| 97 |
} else { |
| 98 |
read_staging_buffer = |
| 99 |
memory_owner.MakeSlice(grpc_core::MemoryRequest(STAGING_BUFFER_SIZE)); |
| 100 |
write_staging_buffer = |
| 101 |
memory_owner.MakeSlice(grpc_core::MemoryRequest(STAGING_BUFFER_SIZE)); |
| 102 |
} |
| 103 |
has_posted_reclaimer.store(false, std::memory_order_relaxed); |
| 104 |
min_progress_size = 1; |
| 105 |
grpc_slice_buffer_init(&protector_staging_buffer); |
| 106 |
gpr_ref_init(&ref, 1); |
| 107 |
} |
| 108 |
|
| 109 |
~secure_endpoint() { |
| 110 |
grpc_endpoint_destroy(wrapped_ep); |
| 111 |
tsi_frame_protector_destroy(protector); |
| 112 |
tsi_zero_copy_grpc_protector_destroy(zero_copy_protector); |
| 113 |
grpc_slice_buffer_destroy(&source_buffer); |
| 114 |
grpc_slice_buffer_destroy(&leftover_bytes); |
| 115 |
grpc_core::CSliceUnref(read_staging_buffer); |
| 116 |
grpc_core::CSliceUnref(write_staging_buffer); |
| 117 |
grpc_slice_buffer_destroy(&output_buffer); |
| 118 |
grpc_slice_buffer_destroy(&protector_staging_buffer); |
| 119 |
gpr_mu_destroy(&protector_mu); |
| 120 |
} |
| 121 |
|
| 122 |
grpc_endpoint base; |
| 123 |
grpc_endpoint* wrapped_ep; |
| 124 |
struct tsi_frame_protector* protector; |
| 125 |
struct tsi_zero_copy_grpc_protector* zero_copy_protector; |
| 126 |
gpr_mu protector_mu; |
| 127 |
grpc_core::Mutex read_mu; |
| 128 |
grpc_core::Mutex write_mu; |
| 129 |
/* saved upper level callbacks and user_data. */ |
| 130 |
grpc_closure* read_cb = nullptr; |
| 131 |
grpc_closure* write_cb = nullptr; |
| 132 |
grpc_closure on_read; |
| 133 |
grpc_slice_buffer* read_buffer = nullptr; |
| 134 |
grpc_slice_buffer source_buffer; |
| 135 |
/* saved handshaker leftover data to unprotect. */ |
| 136 |
grpc_slice_buffer leftover_bytes; |
| 137 |
/* buffers for read and write */ |
| 138 |
grpc_slice read_staging_buffer ABSL_GUARDED_BY(read_mu); |
| 139 |
grpc_slice write_staging_buffer ABSL_GUARDED_BY(write_mu); |
| 140 |
grpc_slice_buffer output_buffer; |
| 141 |
grpc_core::MemoryOwner memory_owner; |
| 142 |
grpc_core::MemoryAllocator::Reservation self_reservation; |
| 143 |
std::atomic<bool> has_posted_reclaimer; |
| 144 |
int min_progress_size; |
| 145 |
grpc_slice_buffer protector_staging_buffer; |
| 146 |
gpr_refcount ref; |
| 147 |
}; |
| 148 |
} // namespace |
| 149 |
|
| 150 |
grpc_core::TraceFlag grpc_trace_secure_endpoint(false, "secure_endpoint"); |
| 151 |
|
| 152 |
static void destroy(secure_endpoint* ep) { delete ep; } |
| 153 |
|
| 154 |
#ifndef NDEBUG |
| 155 |
#define SECURE_ENDPOINT_UNREF(ep, reason) \ |
| 156 |
secure_endpoint_unref((ep), (reason), __FILE__, __LINE__) |
| 157 |
#define SECURE_ENDPOINT_REF(ep, reason) \ |
| 158 |
secure_endpoint_ref((ep), (reason), __FILE__, __LINE__) |
| 159 |
static void secure_endpoint_unref(secure_endpoint* ep, const char* reason, |
| 160 |
const char* file, int line) { |
| 161 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_trace_secure_endpoint)) { |
| 162 |
gpr_atm val = gpr_atm_no_barrier_load(&ep->ref.count); |
| 163 |
gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 164 |
"SECENDP unref %p : %s %" PRIdPTR " -> %" PRIdPTR, ep, reason, val, |
| 165 |
val - 1); |
| 166 |
} |
| 167 |
if (gpr_unref(&ep->ref)) { |
| 168 |
destroy(ep); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
static void secure_endpoint_ref(secure_endpoint* ep, const char* reason, |
| 173 |
const char* file, int line) { |
| 174 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_trace_secure_endpoint)) { |
| 175 |
gpr_atm val = gpr_atm_no_barrier_load(&ep->ref.count); |
| 176 |
gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 177 |
"SECENDP ref %p : %s %" PRIdPTR " -> %" PRIdPTR, ep, reason, val, |
| 178 |
val + 1); |
| 179 |
} |
| 180 |
gpr_ref(&ep->ref); |
| 181 |
} |
| 182 |
#else |
| 183 |
#define SECURE_ENDPOINT_UNREF(ep, reason) secure_endpoint_unref((ep)) |
| 184 |
#define SECURE_ENDPOINT_REF(ep, reason) secure_endpoint_ref((ep)) |
| 185 |
static void secure_endpoint_unref(secure_endpoint* ep) { |
| 186 |
if (gpr_unref(&ep->ref)) { |
| 187 |
destroy(ep); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
static void secure_endpoint_ref(secure_endpoint* ep) { gpr_ref(&ep->ref); } |
| 192 |
#endif |
| 193 |
|
| 194 |
static void maybe_post_reclaimer(secure_endpoint* ep) { |
| 195 |
if (!ep->has_posted_reclaimer) { |
| 196 |
SECURE_ENDPOINT_REF(ep, "benign_reclaimer"); |
| 197 |
ep->has_posted_reclaimer.exchange(true, std::memory_order_relaxed); |
| 198 |
ep->memory_owner.PostReclaimer( |
| 199 |
grpc_core::ReclamationPass::kBenign, |
| 200 |
[ep](absl::optional<grpc_core::ReclamationSweep> sweep) { |
| 201 |
if (sweep.has_value()) { |
| 202 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_resource_quota_trace)) { |
| 203 |
gpr_log(GPR_INFO, |
| 204 |
"secure endpoint: benign reclamation to free memory"); |
| 205 |
} |
| 206 |
grpc_slice temp_read_slice; |
| 207 |
grpc_slice temp_write_slice; |
| 208 |
|
| 209 |
ep->read_mu.Lock(); |
| 210 |
temp_read_slice = ep->read_staging_buffer; |
| 211 |
ep->read_staging_buffer = grpc_empty_slice(); |
| 212 |
ep->read_mu.Unlock(); |
| 213 |
|
| 214 |
ep->write_mu.Lock(); |
| 215 |
temp_write_slice = ep->write_staging_buffer; |
| 216 |
ep->write_staging_buffer = grpc_empty_slice(); |
| 217 |
ep->write_mu.Unlock(); |
| 218 |
|
| 219 |
grpc_core::CSliceUnref(temp_read_slice); |
| 220 |
grpc_core::CSliceUnref(temp_write_slice); |
| 221 |
ep->has_posted_reclaimer.exchange(false, std::memory_order_relaxed); |
| 222 |
} |
| 223 |
SECURE_ENDPOINT_UNREF(ep, "benign_reclaimer"); |
| 224 |
}); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
static void flush_read_staging_buffer(secure_endpoint* ep, uint8_t** cur, |
| 229 |
uint8_t** end) |
| 230 |
ABSL_EXCLUSIVE_LOCKS_REQUIRED(ep->read_mu) { |
| 231 |
grpc_slice_buffer_add_indexed(ep->read_buffer, ep->read_staging_buffer); |
| 232 |
ep->read_staging_buffer = |
| 233 |
ep->memory_owner.MakeSlice(grpc_core::MemoryRequest(STAGING_BUFFER_SIZE)); |
| 234 |
*cur = GRPC_SLICE_START_PTR(ep->read_staging_buffer); |
| 235 |
*end = GRPC_SLICE_END_PTR(ep->read_staging_buffer); |
| 236 |
} |
| 237 |
|
| 238 |
static void call_read_cb(secure_endpoint* ep, grpc_error_handle error) { |
| 239 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_trace_secure_endpoint)) { |
| 240 |
size_t i; |
| 241 |
for (i = 0; i < ep->read_buffer->count; i++) { |
| 242 |
char* data = grpc_dump_slice(ep->read_buffer->slices[i], |
| 243 |
GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 244 |
gpr_log(GPR_INFO, "READ %p: %s", ep, data); |
| 245 |
gpr_free(data); |
| 246 |
} |
| 247 |
} |
| 248 |
ep->read_buffer = nullptr; |
| 249 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, ep->read_cb, error); |
| 250 |
SECURE_ENDPOINT_UNREF(ep, "read"); |
| 251 |
} |
| 252 |
|
| 253 |
static void on_read(void* user_data, grpc_error_handle error) { |
| 254 |
unsigned i; |
| 255 |
uint8_t keep_looping = 0; |
| 256 |
tsi_result result = TSI_OK; |
| 257 |
secure_endpoint* ep = static_cast<secure_endpoint*>(user_data); |
| 258 |
|
| 259 |
{ |
| 260 |
grpc_core::MutexLock l(&ep->read_mu); |
| 261 |
uint8_t* cur = GRPC_SLICE_START_PTR(ep->read_staging_buffer); |
| 262 |
uint8_t* end = GRPC_SLICE_END_PTR(ep->read_staging_buffer); |
| 263 |
|
| 264 |
if (!error.ok()) { |
| 265 |
grpc_slice_buffer_reset_and_unref(ep->read_buffer); |
| 266 |
call_read_cb( |
| 267 |
ep, GRPC_ERROR_CREATE_REFERENCING("Secure read failed", &error, 1)); |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
if (ep->zero_copy_protector != nullptr) { |
| 272 |
// Use zero-copy grpc protector to unprotect. |
| 273 |
int min_progress_size = 1; |
| 274 |
// Get the size of the last frame which is not yet fully decrypted. |
| 275 |
// This estimated frame size is stored in ep->min_progress_size which is |
| 276 |
// passed to the TCP layer to indicate the minimum number of |
| 277 |
// bytes that need to be read to make meaningful progress. This would |
| 278 |
// avoid reading of small slices from the network. |
| 279 |
// TODO(vigneshbabu): Set min_progress_size in the regular (non-zero-copy) |
| 280 |
// frame protector code path as well. |
| 281 |
result = tsi_zero_copy_grpc_protector_unprotect( |
| 282 |
ep->zero_copy_protector, &ep->source_buffer, ep->read_buffer, |
| 283 |
&min_progress_size); |
| 284 |
min_progress_size = std::max(1, min_progress_size); |
| 285 |
ep->min_progress_size = result != TSI_OK ? 1 : min_progress_size; |
| 286 |
} else { |
| 287 |
// Use frame protector to unprotect. |
| 288 |
/* TODO(yangg) check error, maybe bail out early */ |
| 289 |
for (i = 0; i < ep->source_buffer.count; i++) { |
| 290 |
grpc_slice encrypted = ep->source_buffer.slices[i]; |
| 291 |
uint8_t* message_bytes = GRPC_SLICE_START_PTR(encrypted); |
| 292 |
size_t message_size = GRPC_SLICE_LENGTH(encrypted); |
| 293 |
|
| 294 |
while (message_size > 0 || keep_looping) { |
| 295 |
size_t unprotected_buffer_size_written = |
| 296 |
static_cast<size_t>(end - cur); |
| 297 |
size_t processed_message_size = message_size; |
| 298 |
gpr_mu_lock(&ep->protector_mu); |
| 299 |
result = tsi_frame_protector_unprotect( |
| 300 |
ep->protector, message_bytes, &processed_message_size, cur, |
| 301 |
&unprotected_buffer_size_written); |
| 302 |
gpr_mu_unlock(&ep->protector_mu); |
| 303 |
if (result != TSI_OK) { |
| 304 |
gpr_log(GPR_ERROR, "Decryption error: %s", |
| 305 |
tsi_result_to_string(result)); |
| 306 |
break; |
| 307 |
} |
| 308 |
message_bytes += processed_message_size; |
| 309 |
message_size -= processed_message_size; |
| 310 |
cur += unprotected_buffer_size_written; |
| 311 |
|
| 312 |
if (cur == end) { |
| 313 |
flush_read_staging_buffer(ep, &cur, &end); |
| 314 |
/* Force to enter the loop again to extract buffered bytes in |
| 315 |
protector. The bytes could be buffered because of running out of |
| 316 |
staging_buffer. If this happens at the end of all slices, doing |
| 317 |
another unprotect avoids leaving data in the protector. */ |
| 318 |
keep_looping = 1; |
| 319 |
} else if (unprotected_buffer_size_written > 0) { |
| 320 |
keep_looping = 1; |
| 321 |
} else { |
| 322 |
keep_looping = 0; |
| 323 |
} |
| 324 |
} |
| 325 |
if (result != TSI_OK) break; |
| 326 |
} |
| 327 |
|
| 328 |
if (cur != GRPC_SLICE_START_PTR(ep->read_staging_buffer)) { |
| 329 |
grpc_slice_buffer_add( |
| 330 |
ep->read_buffer, |
| 331 |
grpc_slice_split_head( |
| 332 |
&ep->read_staging_buffer, |
| 333 |
static_cast<size_t>( |
| 334 |
cur - GRPC_SLICE_START_PTR(ep->read_staging_buffer)))); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
/* TODO(yangg) experiment with moving this block after read_cb to see if it |
| 340 |
helps latency */ |
| 341 |
grpc_slice_buffer_reset_and_unref(&ep->source_buffer); |
| 342 |
|
| 343 |
if (result != TSI_OK) { |
| 344 |
grpc_slice_buffer_reset_and_unref(ep->read_buffer); |
| 345 |
call_read_cb(ep, grpc_set_tsi_error_result( |
| 346 |
GRPC_ERROR_CREATE("Unwrap failed"), result)); |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
call_read_cb(ep, absl::OkStatus()); |
| 351 |
} |
| 352 |
|
| 353 |
static void endpoint_read(grpc_endpoint* secure_ep, grpc_slice_buffer* slices, |
| 354 |
grpc_closure* cb, bool urgent, |
| 355 |
int /*min_progress_size*/) { |
| 356 |
secure_endpoint* ep = reinterpret_cast<secure_endpoint*>(secure_ep); |
| 357 |
ep->read_cb = cb; |
| 358 |
ep->read_buffer = slices; |
| 359 |
grpc_slice_buffer_reset_and_unref(ep->read_buffer); |
| 360 |
|
| 361 |
SECURE_ENDPOINT_REF(ep, "read"); |
| 362 |
if (ep->leftover_bytes.count) { |
| 363 |
grpc_slice_buffer_swap(&ep->leftover_bytes, &ep->source_buffer); |
| 364 |
GPR_ASSERT(ep->leftover_bytes.count == 0); |
| 365 |
on_read(ep, absl::OkStatus()); |
| 366 |
return; |
| 367 |
} |
| 368 |
|
| 369 |
grpc_endpoint_read(ep->wrapped_ep, &ep->source_buffer, &ep->on_read, urgent, |
| 370 |
/*min_progress_size=*/ep->min_progress_size); |
| 371 |
} |
| 372 |
|
| 373 |
static void flush_write_staging_buffer(secure_endpoint* ep, uint8_t** cur, |
| 374 |
uint8_t** end) |
| 375 |
ABSL_EXCLUSIVE_LOCKS_REQUIRED(ep->write_mu) { |
| 376 |
grpc_slice_buffer_add_indexed(&ep->output_buffer, ep->write_staging_buffer); |
| 377 |
ep->write_staging_buffer = |
| 378 |
ep->memory_owner.MakeSlice(grpc_core::MemoryRequest(STAGING_BUFFER_SIZE)); |
| 379 |
*cur = GRPC_SLICE_START_PTR(ep->write_staging_buffer); |
| 380 |
*end = GRPC_SLICE_END_PTR(ep->write_staging_buffer); |
| 381 |
maybe_post_reclaimer(ep); |
| 382 |
} |
| 383 |
|
| 384 |
static void endpoint_write(grpc_endpoint* secure_ep, grpc_slice_buffer* slices, |
| 385 |
grpc_closure* cb, void* arg, int max_frame_size) { |
| 386 |
unsigned i; |
| 387 |
tsi_result result = TSI_OK; |
| 388 |
secure_endpoint* ep = reinterpret_cast<secure_endpoint*>(secure_ep); |
| 389 |
|
| 390 |
{ |
| 391 |
grpc_core::MutexLock l(&ep->write_mu); |
| 392 |
uint8_t* cur = GRPC_SLICE_START_PTR(ep->write_staging_buffer); |
| 393 |
uint8_t* end = GRPC_SLICE_END_PTR(ep->write_staging_buffer); |
| 394 |
|
| 395 |
grpc_slice_buffer_reset_and_unref(&ep->output_buffer); |
| 396 |
|
| 397 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_trace_secure_endpoint)) { |
| 398 |
for (i = 0; i < slices->count; i++) { |
| 399 |
char* data = |
| 400 |
grpc_dump_slice(slices->slices[i], GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 401 |
gpr_log(GPR_INFO, "WRITE %p: %s", ep, data); |
| 402 |
gpr_free(data); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
if (ep->zero_copy_protector != nullptr) { |
| 407 |
// Use zero-copy grpc protector to protect. |
| 408 |
result = TSI_OK; |
| 409 |
// Break the input slices into chunks of size = max_frame_size and call |
| 410 |
// tsi_zero_copy_grpc_protector_protect on each chunk. This ensures that |
| 411 |
// the protector cannot create frames larger than the specified |
| 412 |
// max_frame_size. |
| 413 |
while (slices->length > static_cast<size_t>(max_frame_size) && |
| 414 |
result == TSI_OK) { |
| 415 |
grpc_slice_buffer_move_first(slices, |
| 416 |
static_cast<size_t>(max_frame_size), |
| 417 |
&ep->protector_staging_buffer); |
| 418 |
result = tsi_zero_copy_grpc_protector_protect( |
| 419 |
ep->zero_copy_protector, &ep->protector_staging_buffer, |
| 420 |
&ep->output_buffer); |
| 421 |
} |
| 422 |
if (result == TSI_OK && slices->length > 0) { |
| 423 |
result = tsi_zero_copy_grpc_protector_protect( |
| 424 |
ep->zero_copy_protector, slices, &ep->output_buffer); |
| 425 |
} |
| 426 |
grpc_slice_buffer_reset_and_unref(&ep->protector_staging_buffer); |
| 427 |
} else { |
| 428 |
// Use frame protector to protect. |
| 429 |
for (i = 0; i < slices->count; i++) { |
| 430 |
grpc_slice plain = slices->slices[i]; |
| 431 |
uint8_t* message_bytes = GRPC_SLICE_START_PTR(plain); |
| 432 |
size_t message_size = GRPC_SLICE_LENGTH(plain); |
| 433 |
while (message_size > 0) { |
| 434 |
size_t protected_buffer_size_to_send = static_cast<size_t>(end - cur); |
| 435 |
size_t processed_message_size = message_size; |
| 436 |
gpr_mu_lock(&ep->protector_mu); |
| 437 |
result = tsi_frame_protector_protect(ep->protector, message_bytes, |
| 438 |
&processed_message_size, cur, |
| 439 |
&protected_buffer_size_to_send); |
| 440 |
gpr_mu_unlock(&ep->protector_mu); |
| 441 |
if (result != TSI_OK) { |
| 442 |
gpr_log(GPR_ERROR, "Encryption error: %s", |
| 443 |
tsi_result_to_string(result)); |
| 444 |
break; |
| 445 |
} |
| 446 |
message_bytes += processed_message_size; |
| 447 |
message_size -= processed_message_size; |
| 448 |
cur += protected_buffer_size_to_send; |
| 449 |
|
| 450 |
if (cur == end) { |
| 451 |
flush_write_staging_buffer(ep, &cur, &end); |
| 452 |
} |
| 453 |
} |
| 454 |
if (result != TSI_OK) break; |
| 455 |
} |
| 456 |
if (result == TSI_OK) { |
| 457 |
size_t still_pending_size; |
| 458 |
do { |
| 459 |
size_t protected_buffer_size_to_send = static_cast<size_t>(end - cur); |
| 460 |
gpr_mu_lock(&ep->protector_mu); |
| 461 |
result = tsi_frame_protector_protect_flush( |
| 462 |
ep->protector, cur, &protected_buffer_size_to_send, |
| 463 |
&still_pending_size); |
| 464 |
gpr_mu_unlock(&ep->protector_mu); |
| 465 |
if (result != TSI_OK) break; |
| 466 |
cur += protected_buffer_size_to_send; |
| 467 |
if (cur == end) { |
| 468 |
flush_write_staging_buffer(ep, &cur, &end); |
| 469 |
} |
| 470 |
} while (still_pending_size > 0); |
| 471 |
if (cur != GRPC_SLICE_START_PTR(ep->write_staging_buffer)) { |
| 472 |
grpc_slice_buffer_add( |
| 473 |
&ep->output_buffer, |
| 474 |
grpc_slice_split_head( |
| 475 |
&ep->write_staging_buffer, |
| 476 |
static_cast<size_t>( |
| 477 |
cur - GRPC_SLICE_START_PTR(ep->write_staging_buffer)))); |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
if (result != TSI_OK) { |
| 484 |
/* TODO(yangg) do different things according to the error type? */ |
| 485 |
grpc_slice_buffer_reset_and_unref(&ep->output_buffer); |
| 486 |
grpc_core::ExecCtx::Run( |
| 487 |
DEBUG_LOCATION, cb, |
| 488 |
grpc_set_tsi_error_result(GRPC_ERROR_CREATE("Wrap failed"), result)); |
| 489 |
return; |
| 490 |
} |
| 491 |
|
| 492 |
grpc_endpoint_write(ep->wrapped_ep, &ep->output_buffer, cb, arg, |
| 493 |
max_frame_size); |
| 494 |
} |
| 495 |
|
| 496 |
static void endpoint_shutdown(grpc_endpoint* secure_ep, grpc_error_handle why) { |
| 497 |
secure_endpoint* ep = reinterpret_cast<secure_endpoint*>(secure_ep); |
| 498 |
grpc_endpoint_shutdown(ep->wrapped_ep, why); |
| 499 |
} |
| 500 |
|
| 501 |
static void endpoint_destroy(grpc_endpoint* secure_ep) { |
| 502 |
secure_endpoint* ep = reinterpret_cast<secure_endpoint*>(secure_ep); |
| 503 |
ep->memory_owner.Reset(); |
| 504 |
SECURE_ENDPOINT_UNREF(ep, "destroy"); |
| 505 |
} |
| 506 |
|
| 507 |
static void endpoint_add_to_pollset(grpc_endpoint* secure_ep, |
| 508 |
grpc_pollset* pollset) { |
| 509 |
secure_endpoint* ep = reinterpret_cast<secure_endpoint*>(secure_ep); |
| 510 |
grpc_endpoint_add_to_pollset(ep->wrapped_ep, pollset); |
| 511 |
} |
| 512 |
|
| 513 |
static void endpoint_add_to_pollset_set(grpc_endpoint* secure_ep, |
| 514 |
grpc_pollset_set* pollset_set) { |
| 515 |
secure_endpoint* ep = reinterpret_cast<secure_endpoint*>(secure_ep); |
| 516 |
grpc_endpoint_add_to_pollset_set(ep->wrapped_ep, pollset_set); |
| 517 |
} |
| 518 |
|
| 519 |
static void endpoint_delete_from_pollset_set(grpc_endpoint* secure_ep, |
| 520 |
grpc_pollset_set* pollset_set) { |
| 521 |
secure_endpoint* ep = reinterpret_cast<secure_endpoint*>(secure_ep); |
| 522 |
grpc_endpoint_delete_from_pollset_set(ep->wrapped_ep, pollset_set); |
| 523 |
} |
| 524 |
|
| 525 |
static absl::string_view endpoint_get_peer(grpc_endpoint* secure_ep) { |
| 526 |
secure_endpoint* ep = reinterpret_cast<secure_endpoint*>(secure_ep); |
| 527 |
return grpc_endpoint_get_peer(ep->wrapped_ep); |
| 528 |
} |
| 529 |
|
| 530 |
static absl::string_view endpoint_get_local_address(grpc_endpoint* secure_ep) { |
| 531 |
secure_endpoint* ep = reinterpret_cast<secure_endpoint*>(secure_ep); |
| 532 |
return grpc_endpoint_get_local_address(ep->wrapped_ep); |
| 533 |
} |
| 534 |
|
| 535 |
static int endpoint_get_fd(grpc_endpoint* secure_ep) { |
| 536 |
secure_endpoint* ep = reinterpret_cast<secure_endpoint*>(secure_ep); |
| 537 |
return grpc_endpoint_get_fd(ep->wrapped_ep); |
| 538 |
} |
| 539 |
|
| 540 |
static bool endpoint_can_track_err(grpc_endpoint* secure_ep) { |
| 541 |
secure_endpoint* ep = reinterpret_cast<secure_endpoint*>(secure_ep); |
| 542 |
return grpc_endpoint_can_track_err(ep->wrapped_ep); |
| 543 |
} |
| 544 |
|
| 545 |
static const grpc_endpoint_vtable vtable = {endpoint_read, |
| 546 |
endpoint_write, |
| 547 |
endpoint_add_to_pollset, |
| 548 |
endpoint_add_to_pollset_set, |
| 549 |
endpoint_delete_from_pollset_set, |
| 550 |
endpoint_shutdown, |
| 551 |
endpoint_destroy, |
| 552 |
endpoint_get_peer, |
| 553 |
endpoint_get_local_address, |
| 554 |
endpoint_get_fd, |
| 555 |
endpoint_can_track_err}; |
| 556 |
|
| 557 |
grpc_endpoint* grpc_secure_endpoint_create( |
| 558 |
struct tsi_frame_protector* protector, |
| 559 |
struct tsi_zero_copy_grpc_protector* zero_copy_protector, |
| 560 |
grpc_endpoint* to_wrap, grpc_slice* leftover_slices, |
| 561 |
const grpc_channel_args* channel_args, size_t leftover_nslices) { |
| 562 |
secure_endpoint* ep = |
| 563 |
new secure_endpoint(&vtable, protector, zero_copy_protector, to_wrap, |
| 564 |
leftover_slices, channel_args, leftover_nslices); |
| 565 |
return &ep->base; |
| 566 |
} |