alts_record_protocol_crypter_common.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 <grpc/support/alloc.h>
24 
25 static void maybe_copy_error_msg(const char* src, char** dst) {
26  if (dst != nullptr && src != nullptr) {
27  *dst = static_cast<char*>(gpr_malloc(strlen(src) + 1));
28  memcpy(*dst, src, strlen(src) + 1);
29  }
30 }
31 
33  const alts_record_protocol_crypter* rp_crypter, const unsigned char* data,
34  size_t* output_size, char** error_details) {
35  if (rp_crypter == nullptr) {
36  maybe_copy_error_msg("alts_crypter instance is nullptr.", error_details);
38  } else if (data == nullptr) {
39  maybe_copy_error_msg("data is nullptr.", error_details);
41  } else if (output_size == nullptr) {
42  maybe_copy_error_msg("output_size is nullptr.", error_details);
44  }
45  return GRPC_STATUS_OK;
46 }
47 
49  char** error_details) {
50  bool is_overflow = false;
52  alts_counter_increment(rp_crypter->ctr, &is_overflow, error_details);
53  if (status != GRPC_STATUS_OK) {
54  return status;
55  }
56  if (is_overflow) {
57  const char error_msg[] =
58  "crypter counter is wrapped. The connection"
59  "should be closed and the key should be deleted.";
60  maybe_copy_error_msg(error_msg, error_details);
61  return GRPC_STATUS_INTERNAL;
62  }
63  return GRPC_STATUS_OK;
64 }
65 
67  if (c != nullptr) {
68  size_t num_overhead_bytes = 0;
69  char* error_details = nullptr;
70  const alts_record_protocol_crypter* rp_crypter =
71  reinterpret_cast<const alts_record_protocol_crypter*>(c);
73  rp_crypter->crypter, &num_overhead_bytes, &error_details);
74  if (status == GRPC_STATUS_OK) {
75  return num_overhead_bytes;
76  }
77  }
78  return 0;
79 }
80 
82  if (c != nullptr) {
83  alts_record_protocol_crypter* rp_crypter =
84  reinterpret_cast<alts_record_protocol_crypter*>(c);
85  alts_counter_destroy(rp_crypter->ctr);
87  }
88 }
89 
91  gsec_aead_crypter* crypter, bool is_client, size_t overflow_size,
92  char** error_details) {
93  if (crypter != nullptr) {
94  auto* rp_crypter = static_cast<alts_record_protocol_crypter*>(
96  size_t counter_size = 0;
98  gsec_aead_crypter_nonce_length(crypter, &counter_size, error_details);
99  if (status != GRPC_STATUS_OK) {
100  return nullptr;
101  }
102  /* Create a counter. */
103  status = alts_counter_create(is_client, counter_size, overflow_size,
104  &rp_crypter->ctr, error_details);
105  if (status != GRPC_STATUS_OK) {
106  return nullptr;
107  }
108  rp_crypter->crypter = crypter;
109  return rp_crypter;
110  }
111  const char error_msg[] = "crypter is nullptr.";
112  maybe_copy_error_msg(error_msg, error_details);
113  return nullptr;
114 }
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
alts_record_protocol_crypter_common.h
maybe_copy_error_msg
static void maybe_copy_error_msg(const char *src, char **dst)
Definition: alts_record_protocol_crypter_common.cc:25
alts_crypter_create_common
alts_record_protocol_crypter * alts_crypter_create_common(gsec_aead_crypter *crypter, bool is_client, size_t overflow_size, char **error_details)
Definition: alts_record_protocol_crypter_common.cc:90
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
alts_record_protocol_crypter
Definition: alts_record_protocol_crypter_common.h:38
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
status
absl::Status status
Definition: rls.cc:251
alts_counter_increment
grpc_status_code alts_counter_increment(alts_counter *crypter_counter, bool *is_overflow, char **error_details)
Definition: alts_counter.cc:66
GRPC_STATUS_INVALID_ARGUMENT
@ GRPC_STATUS_INVALID_ARGUMENT
Definition: include/grpc/impl/codegen/status.h:46
gsec_aead_crypter_tag_length
grpc_status_code gsec_aead_crypter_tag_length(const gsec_aead_crypter *crypter, size_t *tag_length_to_return, char **error_details)
Definition: gsec.cc:170
alts_record_protocol_crypter::crypter
gsec_aead_crypter * crypter
Definition: alts_record_protocol_crypter_common.h:40
increment_counter
grpc_status_code increment_counter(alts_record_protocol_crypter *rp_crypter, char **error_details)
Definition: alts_record_protocol_crypter_common.cc:48
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
GRPC_STATUS_OK
@ GRPC_STATUS_OK
Definition: include/grpc/impl/codegen/status.h:30
alts_record_protocol_crypter_destruct
void alts_record_protocol_crypter_destruct(alts_crypter *c)
Definition: alts_record_protocol_crypter_common.cc:81
alts_record_protocol_crypter_num_overhead_bytes
size_t alts_record_protocol_crypter_num_overhead_bytes(const alts_crypter *c)
Definition: alts_record_protocol_crypter_common.cc:66
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
alts_crypter
Definition: alts_crypter.h:135
gsec_aead_crypter_nonce_length
grpc_status_code gsec_aead_crypter_nonce_length(const gsec_aead_crypter *crypter, size_t *nonce_length_to_return, char **error_details)
Definition: gsec.cc:144
alloc.h
alts_record_protocol_crypter::ctr
alts_counter * ctr
Definition: alts_record_protocol_crypter_common.h:41
gsec_aead_crypter
Definition: gsec.h:178
GRPC_STATUS_INTERNAL
@ GRPC_STATUS_INTERNAL
Definition: include/grpc/impl/codegen/status.h:129
gsec_aead_crypter_destroy
void gsec_aead_crypter_destroy(gsec_aead_crypter *crypter)
Definition: gsec.cc:183
alts_counter_create
grpc_status_code alts_counter_create(bool is_client, size_t counter_size, size_t overflow_size, alts_counter **crypter_counter, char **error_details)
Definition: alts_counter.cc:34
alts_counter_destroy
void alts_counter_destroy(alts_counter *crypter_counter)
Definition: alts_counter.cc:113
port_platform.h
input_sanity_check
grpc_status_code input_sanity_check(const alts_record_protocol_crypter *rp_crypter, const unsigned char *data, size_t *output_size, char **error_details)
Definition: alts_record_protocol_crypter_common.cc:32


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