alts_frame_protector.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 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 
20 
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 
26 #include <algorithm>
27 
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 
36 
37 constexpr size_t kMinFrameLength = 1024;
38 constexpr size_t kDefaultFrameLength = 16 * 1024;
39 constexpr size_t kMaxFrameLength = 1024 * 1024;
40 
41 // Limit k on number of frames such that at most 2^(8 * k) frames can be sent.
43 constexpr size_t kAltsRecordProtocolFrameLimit = 5;
44 
45 /* Main struct for alts_frame_protector. */
52  unsigned char* in_place_protect_buffer;
53  unsigned char* in_place_unprotect_buffer;
60 };
61 
63  char* error_details = nullptr;
64  size_t output_size = 0;
68  &output_size, &error_details);
69  impl->in_place_protect_bytes_buffered = output_size;
70  if (status != GRPC_STATUS_OK) {
71  gpr_log(GPR_ERROR, "%s", error_details);
72  gpr_free(error_details);
73  return TSI_INTERNAL_ERROR;
74  }
75  return TSI_OK;
76 }
77 
80 }
81 
83  unsigned char* protected_output_frames,
84  size_t* protected_output_frames_size,
85  size_t* still_pending_size) {
86  if (self == nullptr || protected_output_frames == nullptr ||
87  protected_output_frames_size == nullptr ||
88  still_pending_size == nullptr) {
89  gpr_log(GPR_ERROR, "Invalid nullptr arguments to alts_protect_flush().");
90  return TSI_INVALID_ARGUMENT;
91  }
92  alts_frame_protector* impl = reinterpret_cast<alts_frame_protector*>(self);
97  if (impl->in_place_protect_bytes_buffered == 0) {
98  *protected_output_frames_size = 0;
99  *still_pending_size = 0;
100  return TSI_OK;
101  }
107  if (alts_is_frame_writer_done(impl->writer)) {
108  tsi_result result = seal(impl);
109  if (result != TSI_OK) {
110  return result;
111  }
114  gpr_log(GPR_ERROR, "Couldn't reset frame writer.");
115  return TSI_INTERNAL_ERROR;
116  }
117  }
124  size_t written_frame_bytes = *protected_output_frames_size;
125  if (!alts_write_frame_bytes(impl->writer, protected_output_frames,
126  &written_frame_bytes)) {
127  gpr_log(GPR_ERROR, "Couldn't write frame bytes.");
128  return TSI_INTERNAL_ERROR;
129  }
130  *protected_output_frames_size = written_frame_bytes;
131  *still_pending_size = alts_get_num_writer_bytes_remaining(impl->writer);
136  if (alts_is_frame_writer_done(impl->writer)) {
138  }
139  return TSI_OK;
140 }
141 
143  const unsigned char* unprotected_bytes,
144  size_t* unprotected_bytes_size,
145  unsigned char* protected_output_frames,
146  size_t* protected_output_frames_size) {
147  if (self == nullptr || unprotected_bytes == nullptr ||
148  unprotected_bytes_size == nullptr || protected_output_frames == nullptr ||
149  protected_output_frames_size == nullptr) {
150  gpr_log(GPR_ERROR, "Invalid nullptr arguments to alts_protect().");
151  return TSI_INVALID_ARGUMENT;
152  }
153  alts_frame_protector* impl = reinterpret_cast<alts_frame_protector*>(self);
154 
161  size_t bytes_to_buffer = std::min(
162  *unprotected_bytes_size, max_encrypted_payload_bytes(impl) -
164  impl->overhead_length);
165  *unprotected_bytes_size = bytes_to_buffer;
166  if (bytes_to_buffer > 0) {
167  memcpy(
169  unprotected_bytes, bytes_to_buffer);
170  impl->in_place_protect_bytes_buffered += bytes_to_buffer;
171  }
172  } else {
173  *unprotected_bytes_size = 0;
174  }
181  if (max_encrypted_payload_bytes(impl) ==
185  size_t still_pending_size = 0;
186  return alts_protect_flush(self, protected_output_frames,
187  protected_output_frames_size,
188  &still_pending_size);
189  } else {
190  *protected_output_frames_size = 0;
191  return TSI_OK;
192  }
193 }
194 
196  char* error_details = nullptr;
197  size_t output_size = 0;
201  alts_get_output_bytes_read(impl->reader), &output_size, &error_details);
202  if (status != GRPC_STATUS_OK) {
203  gpr_log(GPR_ERROR, "%s", error_details);
204  gpr_free(error_details);
205  return TSI_DATA_CORRUPTED;
206  }
207  return TSI_OK;
208 }
209 
211  if (!alts_has_read_frame_length(impl->reader)) {
212  return;
213  }
214  size_t buffer_space_remaining = impl->max_unprotected_frame_size -
220  if (buffer_space_remaining < alts_get_reader_bytes_remaining(impl->reader)) {
221  size_t buffer_len = alts_get_output_bytes_read(impl->reader) +
223  unsigned char* buffer = static_cast<unsigned char*>(gpr_malloc(buffer_len));
226  impl->max_unprotected_frame_size = buffer_len;
231  }
232 }
233 
235  const unsigned char* protected_frames_bytes,
236  size_t* protected_frames_bytes_size,
237  unsigned char* unprotected_bytes,
238  size_t* unprotected_bytes_size) {
239  if (self == nullptr || protected_frames_bytes == nullptr ||
240  protected_frames_bytes_size == nullptr || unprotected_bytes == nullptr ||
241  unprotected_bytes_size == nullptr) {
242  gpr_log(GPR_ERROR, "Invalid nullptr arguments to alts_unprotect().");
243  return TSI_INVALID_ARGUMENT;
244  }
245  alts_frame_protector* impl = reinterpret_cast<alts_frame_protector*>(self);
251  if (alts_is_frame_reader_done(impl->reader) &&
252  ((alts_get_output_buffer(impl->reader) == nullptr) ||
255  if (!alts_reset_frame_reader(impl->reader,
256  impl->in_place_unprotect_buffer)) {
257  gpr_log(GPR_ERROR, "Couldn't reset frame reader.");
258  return TSI_INTERNAL_ERROR;
259  }
261  }
268  if (!alts_is_frame_reader_done(impl->reader)) {
269  ensure_buffer_size(impl);
270  *protected_frames_bytes_size =
273  *protected_frames_bytes_size);
274  size_t read_frames_bytes_size = *protected_frames_bytes_size;
275  if (!alts_read_frame_bytes(impl->reader, protected_frames_bytes,
276  &read_frames_bytes_size)) {
277  gpr_log(GPR_ERROR, "Failed to process frame.");
278  return TSI_INTERNAL_ERROR;
279  }
280  *protected_frames_bytes_size = read_frames_bytes_size;
281  } else {
282  *protected_frames_bytes_size = 0;
283  }
288  if (alts_is_frame_reader_done(impl->reader)) {
289  if (impl->in_place_unprotect_bytes_processed == 0) {
290  tsi_result result = unseal(impl);
291  if (result != TSI_OK) {
292  return result;
293  }
294  }
295  size_t bytes_to_write = std::min(
296  *unprotected_bytes_size, alts_get_output_bytes_read(impl->reader) -
298  impl->overhead_length);
299  if (bytes_to_write > 0) {
300  memcpy(unprotected_bytes,
303  bytes_to_write);
304  }
305  *unprotected_bytes_size = bytes_to_write;
306  impl->in_place_unprotect_bytes_processed += bytes_to_write;
307  return TSI_OK;
308  } else {
309  *unprotected_bytes_size = 0;
310  return TSI_OK;
311  }
312 }
313 
314 static void alts_destroy(tsi_frame_protector* self) {
315  alts_frame_protector* impl = reinterpret_cast<alts_frame_protector*>(self);
316  if (impl != nullptr) {
323  gpr_free(impl);
324  }
325 }
326 
329 
331  size_t key_size, bool is_client,
332  bool is_rekey,
333  alts_frame_protector* impl,
334  char** error_details) {
336  gsec_aead_crypter* aead_crypter_seal = nullptr;
337  gsec_aead_crypter* aead_crypter_unseal = nullptr;
339  kAesGcmTagLength, is_rekey,
340  &aead_crypter_seal, error_details);
341  if (status != GRPC_STATUS_OK) {
342  return status;
343  }
345  key, key_size, kAesGcmNonceLength, kAesGcmTagLength, is_rekey,
346  &aead_crypter_unseal, error_details);
347  if (status != GRPC_STATUS_OK) {
348  return status;
349  }
350  size_t overflow_size = is_rekey ? kAltsRecordProtocolRekeyFrameLimit
352  status = alts_seal_crypter_create(aead_crypter_seal, is_client, overflow_size,
353  &impl->seal_crypter, error_details);
354  if (status != GRPC_STATUS_OK) {
355  return status;
356  }
357  status =
358  alts_unseal_crypter_create(aead_crypter_unseal, is_client, overflow_size,
359  &impl->unseal_crypter, error_details);
360  return status;
361 }
362 
364  bool is_client, bool is_rekey,
365  size_t* max_protected_frame_size,
366  tsi_frame_protector** self) {
367  if (key == nullptr || self == nullptr) {
369  "Invalid nullptr arguments to alts_create_frame_protector().");
370  return TSI_INTERNAL_ERROR;
371  }
372  char* error_details = nullptr;
373  alts_frame_protector* impl = grpc_core::Zalloc<alts_frame_protector>();
375  key, key_size, is_client, is_rekey, impl, &error_details);
376  if (status != GRPC_STATUS_OK) {
377  gpr_log(GPR_ERROR, "Failed to create ALTS crypters, %s.", error_details);
378  gpr_free(error_details);
379  return TSI_INTERNAL_ERROR;
380  }
386  size_t max_protected_frame_size_to_set = kDefaultFrameLength;
387  if (max_protected_frame_size != nullptr) {
388  *max_protected_frame_size =
389  std::min(*max_protected_frame_size, kMaxFrameLength);
390  *max_protected_frame_size =
391  std::max(*max_protected_frame_size, kMinFrameLength);
392  max_protected_frame_size_to_set = *max_protected_frame_size;
393  }
394  impl->max_protected_frame_size = max_protected_frame_size_to_set;
395  impl->max_unprotected_frame_size = max_protected_frame_size_to_set;
398  impl->in_place_protect_buffer = static_cast<unsigned char*>(
399  gpr_malloc(sizeof(unsigned char) * max_protected_frame_size_to_set));
400  impl->in_place_unprotect_buffer = static_cast<unsigned char*>(
401  gpr_malloc(sizeof(unsigned char) * max_protected_frame_size_to_set));
406  *self = &impl->base;
407  return TSI_OK;
408 }
alts_reset_frame_reader
bool alts_reset_frame_reader(alts_frame_reader *reader, unsigned char *buffer)
Definition: frame_handler.cc:146
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
alts_create_frame_writer
alts_frame_writer * alts_create_frame_writer()
Definition: frame_handler.cc:51
alts_reset_reader_output_buffer
void alts_reset_reader_output_buffer(alts_frame_reader *reader, unsigned char *buffer)
Definition: frame_handler.cc:141
log.h
alts_unprotect
static tsi_result alts_unprotect(tsi_frame_protector *self, const unsigned char *protected_frames_bytes, size_t *protected_frames_bytes_size, unsigned char *unprotected_bytes, size_t *unprotected_bytes_size)
Definition: alts_frame_protector.cc:234
alts_create_frame_protector
tsi_result alts_create_frame_protector(const uint8_t *key, size_t key_size, bool is_client, bool is_rekey, size_t *max_protected_frame_size, tsi_frame_protector **self)
Definition: alts_frame_protector.cc:363
frame_handler.h
TSI_INTERNAL_ERROR
@ TSI_INTERNAL_ERROR
Definition: transport_security_interface.h:39
alts_crypter_process_in_place
grpc_status_code alts_crypter_process_in_place(alts_crypter *crypter, unsigned char *data, size_t data_allocated_size, size_t data_size, size_t *output_size, char **error_details)
Definition: alts_crypter.cc:34
alts_frame_writer
Definition: frame_handler.h:43
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
useful.h
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
alts_is_frame_writer_done
bool alts_is_frame_writer_done(alts_frame_writer *writer)
Definition: frame_handler.cc:109
alts_frame_protector::writer
alts_frame_writer * writer
Definition: alts_frame_protector.cc:50
status
absl::Status status
Definition: rls.cc:251
alts_get_output_buffer
unsigned char * alts_get_output_buffer(alts_frame_reader *reader)
Definition: frame_handler.cc:215
max_encrypted_payload_bytes
static size_t max_encrypted_payload_bytes(alts_frame_protector *impl)
Definition: alts_frame_protector.cc:78
alts_crypter.h
gsec_aes_gcm_aead_crypter_create
grpc_status_code gsec_aes_gcm_aead_crypter_create(const uint8_t *key, size_t key_length, size_t nonce_length, size_t tag_length, bool rekey, gsec_aead_crypter **crypter, char **error_details)
Definition: aes_gcm.cc:633
alts_crypter_num_overhead_bytes
size_t alts_crypter_num_overhead_bytes(const alts_crypter *crypter)
Definition: alts_crypter.cc:50
kDefaultFrameLength
constexpr size_t kDefaultFrameLength
Definition: alts_frame_protector.cc:38
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
alts_frame_protector::in_place_unprotect_buffer
unsigned char * in_place_unprotect_buffer
Definition: alts_frame_protector.cc:53
alts_destroy_frame_reader
void alts_destroy_frame_reader(alts_frame_reader *reader)
Definition: frame_handler.cc:219
alts_frame_protector_vtable
static const tsi_frame_protector_vtable alts_frame_protector_vtable
Definition: alts_frame_protector.cc:327
alts_read_frame_bytes
bool alts_read_frame_bytes(alts_frame_reader *reader, const unsigned char *bytes, size_t *bytes_size)
Definition: frame_handler.cc:155
TSI_OK
@ TSI_OK
Definition: transport_security_interface.h:32
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
kAesGcmNonceLength
const size_t kAesGcmNonceLength
Definition: gsec.h:47
alts_frame_protector::in_place_unprotect_bytes_processed
size_t in_place_unprotect_bytes_processed
Definition: alts_frame_protector.cc:55
GRPC_STATUS_OK
@ GRPC_STATUS_OK
Definition: include/grpc/impl/codegen/status.h:30
alts_frame_protector::counter_overflow
size_t counter_overflow
Definition: alts_frame_protector.cc:59
alts_frame_protector::base
tsi_frame_protector base
Definition: alts_frame_protector.cc:47
alts_protect_flush
static tsi_result alts_protect_flush(tsi_frame_protector *self, unsigned char *protected_output_frames, size_t *protected_output_frames_size, size_t *still_pending_size)
Definition: alts_frame_protector.cc:82
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
gsec.h
alts_frame_protector::unseal_crypter
alts_crypter * unseal_crypter
Definition: alts_frame_protector.cc:49
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
alts_frame_protector.h
alts_reset_frame_writer
bool alts_reset_frame_writer(alts_frame_writer *writer, const unsigned char *buffer, size_t length)
Definition: frame_handler.cc:55
tsi_result
tsi_result
Definition: transport_security_interface.h:31
alts_get_num_writer_bytes_remaining
size_t alts_get_num_writer_bytes_remaining(alts_frame_writer *writer)
Definition: frame_handler.cc:114
alts_is_frame_reader_done
bool alts_is_frame_reader_done(alts_frame_reader *reader)
Definition: frame_handler.cc:127
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
min
#define min(a, b)
Definition: qsort.h:83
alts_frame_protector::seal_crypter
alts_crypter * seal_crypter
Definition: alts_frame_protector.cc:48
alts_frame_protector::in_place_protect_buffer
unsigned char * in_place_protect_buffer
Definition: alts_frame_protector.cc:52
alts_crypter
Definition: alts_crypter.h:135
alts_write_frame_bytes
bool alts_write_frame_bytes(alts_frame_writer *writer, unsigned char *output, size_t *bytes_size)
Definition: frame_handler.cc:74
kAltsRecordProtocolRekeyFrameLimit
constexpr size_t kAltsRecordProtocolRekeyFrameLimit
Definition: alts_frame_protector.cc:42
alts_frame_protector::max_protected_frame_size
size_t max_protected_frame_size
Definition: alts_frame_protector.cc:56
alts_frame_protector
Definition: alts_frame_protector.cc:46
TSI_DATA_CORRUPTED
@ TSI_DATA_CORRUPTED
Definition: transport_security_interface.h:40
kMaxFrameLength
constexpr size_t kMaxFrameLength
Definition: alts_frame_protector.cc:39
alts_protect
static tsi_result alts_protect(tsi_frame_protector *self, const unsigned char *unprotected_bytes, size_t *unprotected_bytes_size, unsigned char *protected_output_frames, size_t *protected_output_frames_size)
Definition: alts_frame_protector.cc:142
alts_create_frame_reader
alts_frame_reader * alts_create_frame_reader()
Definition: frame_handler.cc:122
seal
static tsi_result seal(alts_frame_protector *impl)
Definition: alts_frame_protector.cc:62
alts_frame_protector::overhead_length
size_t overhead_length
Definition: alts_frame_protector.cc:58
transport_security.h
tsi_frame_protector_vtable
Definition: transport_security.h:34
kMinFrameLength
constexpr size_t kMinFrameLength
Definition: alts_frame_protector.cc:37
create_alts_crypters
static grpc_status_code create_alts_crypters(const uint8_t *key, size_t key_size, bool is_client, bool is_rekey, alts_frame_protector *impl, char **error_details)
Definition: alts_frame_protector.cc:330
key
const char * key
Definition: hpack_parser_table.cc:164
alts_get_reader_bytes_remaining
size_t alts_get_reader_bytes_remaining(alts_frame_reader *reader)
Definition: frame_handler.cc:137
alts_destroy
static void alts_destroy(tsi_frame_protector *self)
Definition: alts_frame_protector.cc:314
ensure_buffer_size
static void ensure_buffer_size(alts_frame_protector *impl)
Definition: alts_frame_protector.cc:210
alts_destroy_frame_writer
void alts_destroy_frame_writer(alts_frame_writer *writer)
Definition: frame_handler.cc:119
alloc.h
alts_seal_crypter_create
grpc_status_code alts_seal_crypter_create(gsec_aead_crypter *gc, bool is_client, size_t overflow_size, alts_crypter **crypter, char **error_details)
Definition: alts_seal_privacy_integrity_crypter.cc:88
alts_frame_protector::reader
alts_frame_reader * reader
Definition: alts_frame_protector.cc:51
alts_frame_protector::max_unprotected_frame_size
size_t max_unprotected_frame_size
Definition: alts_frame_protector.cc:57
TSI_INVALID_ARGUMENT
@ TSI_INVALID_ARGUMENT
Definition: transport_security_interface.h:34
tsi_frame_protector::vtable
const tsi_frame_protector_vtable * vtable
Definition: transport_security.h:52
alts_get_output_bytes_read
size_t alts_get_output_bytes_read(alts_frame_reader *reader)
Definition: frame_handler.cc:211
kFrameHeaderSize
const size_t kFrameHeaderSize
Definition: frame_handler.h:31
alts_unseal_crypter_create
grpc_status_code alts_unseal_crypter_create(gsec_aead_crypter *gc, bool is_client, size_t overflow_size, alts_crypter **crypter, char **error_details)
Definition: alts_unseal_privacy_integrity_crypter.cc:85
gsec_aead_crypter
Definition: gsec.h:178
alts_has_read_frame_length
bool alts_has_read_frame_length(alts_frame_reader *reader)
Definition: frame_handler.cc:133
tsi_frame_protector
Definition: transport_security.h:51
kAltsRecordProtocolFrameLimit
constexpr size_t kAltsRecordProtocolFrameLimit
Definition: alts_frame_protector.cc:43
alts_crypter_destroy
void alts_crypter_destroy(alts_crypter *crypter)
Definition: alts_crypter.cc:59
unseal
static tsi_result unseal(alts_frame_protector *impl)
Definition: alts_frame_protector.cc:195
alts_frame_protector::in_place_protect_bytes_buffered
size_t in_place_protect_bytes_buffered
Definition: alts_frame_protector.cc:54
kAesGcmTagLength
const size_t kAesGcmTagLength
Definition: gsec.h:48
port_platform.h
alts_frame_reader
Definition: frame_handler.h:55


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:40