alts_grpc_privacy_integrity_record_protocol.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 #include <grpc/support/log.h>
25 
29 
30 /* Privacy-integrity alts_grpc_record_protocol object uses the same struct
31  * defined in alts_grpc_record_protocol_common.h. */
32 
33 /* --- alts_grpc_record_protocol methods implementation. --- */
34 
36  alts_grpc_record_protocol* rp, grpc_slice_buffer* unprotected_slices,
37  grpc_slice_buffer* protected_slices) {
38  /* Input sanity check. */
39  if (rp == nullptr || unprotected_slices == nullptr ||
40  protected_slices == nullptr) {
42  "Invalid nullptr arguments to alts_grpc_record_protocol protect.");
43  return TSI_INVALID_ARGUMENT;
44  }
45  /* Allocates memory for output frame. In privacy-integrity protect, the
46  * protected frame is stored in a newly allocated buffer. */
47  size_t protected_frame_size =
48  unprotected_slices->length + rp->header_length +
50  grpc_slice protected_slice = GRPC_SLICE_MALLOC(protected_frame_size);
51  iovec_t protected_iovec = {GRPC_SLICE_START_PTR(protected_slice),
52  GRPC_SLICE_LENGTH(protected_slice)};
53  /* Calls alts_iovec_record_protocol protect. */
54  char* error_details = nullptr;
56  unprotected_slices);
59  rp->iovec_rp, rp->iovec_buf, unprotected_slices->count,
60  protected_iovec, &error_details);
61  if (status != GRPC_STATUS_OK) {
62  gpr_log(GPR_ERROR, "Failed to protect, %s", error_details);
63  gpr_free(error_details);
64  grpc_slice_unref_internal(protected_slice);
65  return TSI_INTERNAL_ERROR;
66  }
67  grpc_slice_buffer_add(protected_slices, protected_slice);
69  return TSI_OK;
70 }
71 
73  alts_grpc_record_protocol* rp, grpc_slice_buffer* protected_slices,
74  grpc_slice_buffer* unprotected_slices) {
75  /* Input sanity check. */
76  if (rp == nullptr || protected_slices == nullptr ||
77  unprotected_slices == nullptr) {
78  gpr_log(
79  GPR_ERROR,
80  "Invalid nullptr arguments to alts_grpc_record_protocol unprotect.");
81  return TSI_INVALID_ARGUMENT;
82  }
83  /* Allocates memory for output frame. In privacy-integrity unprotect, the
84  * unprotected data are stored in a newly allocated buffer. */
85  if (protected_slices->length < rp->header_length + rp->tag_length) {
86  gpr_log(GPR_ERROR, "Protected slices do not have sufficient data.");
87  return TSI_INVALID_ARGUMENT;
88  }
89  size_t unprotected_frame_size =
90  protected_slices->length - rp->header_length - rp->tag_length;
91  grpc_slice unprotected_slice = GRPC_SLICE_MALLOC(unprotected_frame_size);
92  iovec_t unprotected_iovec = {GRPC_SLICE_START_PTR(unprotected_slice),
93  GRPC_SLICE_LENGTH(unprotected_slice)};
94  /* Strips frame header from protected slices. */
96  grpc_slice_buffer_move_first(protected_slices, rp->header_length,
97  &rp->header_sb);
99  /* Calls alts_iovec_record_protocol unprotect. */
100  char* error_details = nullptr;
104  rp->iovec_rp, header_iovec, rp->iovec_buf, protected_slices->count,
105  unprotected_iovec, &error_details);
106  if (status != GRPC_STATUS_OK) {
107  gpr_log(GPR_ERROR, "Failed to unprotect, %s", error_details);
108  gpr_free(error_details);
109  grpc_slice_unref_internal(unprotected_slice);
110  return TSI_INTERNAL_ERROR;
111  }
114  grpc_slice_buffer_add(unprotected_slices, unprotected_slice);
115  return TSI_OK;
116 }
117 
122 
124  gsec_aead_crypter* crypter, size_t overflow_size, bool is_client,
125  bool is_protect, alts_grpc_record_protocol** rp) {
126  if (crypter == nullptr || rp == nullptr) {
128  "Invalid nullptr arguments to alts_grpc_record_protocol create.");
129  return TSI_INVALID_ARGUMENT;
130  }
131  auto* impl = static_cast<alts_grpc_record_protocol*>(
133  /* Calls alts_grpc_record_protocol init. */
135  alts_grpc_record_protocol_init(impl, crypter, overflow_size, is_client,
136  /*is_integrity_only=*/false, is_protect);
137  if (result != TSI_OK) {
138  gpr_free(impl);
139  return result;
140  }
142  *rp = impl;
143  return TSI_OK;
144 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
alts_grpc_record_protocol_convert_slice_buffer_to_iovec
void alts_grpc_record_protocol_convert_slice_buffer_to_iovec(alts_grpc_record_protocol *rp, const grpc_slice_buffer *sb)
Definition: alts_grpc_record_protocol_common.cc:50
log.h
alts_grpc_record_protocol_init
tsi_result alts_grpc_record_protocol_init(alts_grpc_record_protocol *rp, gsec_aead_crypter *crypter, size_t overflow_size, bool is_client, bool is_integrity_only, bool is_protect)
Definition: alts_grpc_record_protocol_common.cc:88
TSI_INTERNAL_ERROR
@ TSI_INTERNAL_ERROR
Definition: transport_security_interface.h:39
alts_grpc_record_protocol
Definition: alts_grpc_record_protocol_common.h:47
alts_grpc_record_protocol_vtable
Definition: alts_grpc_record_protocol_common.h:34
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
GRPC_SLICE_MALLOC
#define GRPC_SLICE_MALLOC(len)
Definition: include/grpc/slice.h:70
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
status
absl::Status status
Definition: rls.cc:251
alts_grpc_privacy_integrity_protect
static tsi_result alts_grpc_privacy_integrity_protect(alts_grpc_record_protocol *rp, grpc_slice_buffer *unprotected_slices, grpc_slice_buffer *protected_slices)
Definition: alts_grpc_privacy_integrity_record_protocol.cc:35
alts_iovec_record_protocol.h
alts_grpc_privacy_integrity_record_protocol_vtable
static const alts_grpc_record_protocol_vtable alts_grpc_privacy_integrity_record_protocol_vtable
Definition: alts_grpc_privacy_integrity_record_protocol.cc:119
gpr_zalloc
GPRAPI void * gpr_zalloc(size_t size)
Definition: alloc.cc:40
TSI_OK
@ TSI_OK
Definition: transport_security_interface.h:32
alts_iovec_record_protocol_privacy_integrity_protect
grpc_status_code alts_iovec_record_protocol_privacy_integrity_protect(alts_iovec_record_protocol *rp, const iovec_t *unprotected_vec, size_t unprotected_vec_length, iovec_t protected_frame, char **error_details)
Definition: alts_iovec_record_protocol.cc:291
alts_iovec_record_protocol_privacy_integrity_unprotect
grpc_status_code alts_iovec_record_protocol_privacy_integrity_unprotect(alts_iovec_record_protocol *rp, iovec_t header, const iovec_t *protected_vec, size_t protected_vec_length, iovec_t unprotected_data, char **error_details)
Definition: alts_iovec_record_protocol.cc:357
GRPC_STATUS_OK
@ GRPC_STATUS_OK
Definition: include/grpc/impl/codegen/status.h:30
alts_grpc_privacy_integrity_record_protocol.h
alts_grpc_record_protocol::iovec_buf
iovec_t * iovec_buf
Definition: alts_grpc_record_protocol_common.h:54
grpc_slice_buffer::count
size_t count
Definition: include/grpc/impl/codegen/slice.h:91
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
alts_grpc_privacy_integrity_unprotect
static tsi_result alts_grpc_privacy_integrity_unprotect(alts_grpc_record_protocol *rp, grpc_slice_buffer *protected_slices, grpc_slice_buffer *unprotected_slices)
Definition: alts_grpc_privacy_integrity_record_protocol.cc:72
alts_grpc_record_protocol_common.h
tsi_result
tsi_result
Definition: transport_security_interface.h:31
grpc_slice_buffer::length
size_t length
Definition: include/grpc/impl/codegen/slice.h:96
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
alts_grpc_record_protocol::header_sb
grpc_slice_buffer header_sb
Definition: alts_grpc_record_protocol_common.h:50
alts_grpc_record_protocol::tag_length
size_t tag_length
Definition: alts_grpc_record_protocol_common.h:53
slice_internal.h
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
alts_grpc_privacy_integrity_record_protocol_create
tsi_result alts_grpc_privacy_integrity_record_protocol_create(gsec_aead_crypter *crypter, size_t overflow_size, bool is_client, bool is_protect, alts_grpc_record_protocol **rp)
Definition: alts_grpc_privacy_integrity_record_protocol.cc:123
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
alts_grpc_record_protocol_get_header_iovec
iovec_t alts_grpc_record_protocol_get_header_iovec(alts_grpc_record_protocol *rp)
Definition: alts_grpc_record_protocol_common.cc:70
alts_iovec_record_protocol_get_tag_length
size_t alts_iovec_record_protocol_get_tag_length(const alts_iovec_record_protocol *rp)
Definition: alts_iovec_record_protocol.cc:169
grpc_slice_buffer_add
GPRAPI void grpc_slice_buffer_add(grpc_slice_buffer *sb, grpc_slice slice)
Definition: slice/slice_buffer.cc:170
iovec
Definition: gsec.h:33
alts_grpc_record_protocol::header_length
size_t header_length
Definition: alts_grpc_record_protocol_common.h:52
alloc.h
TSI_INVALID_ARGUMENT
@ TSI_INVALID_ARGUMENT
Definition: transport_security_interface.h:34
grpc_slice_buffer_move_first
GPRAPI void grpc_slice_buffer_move_first(grpc_slice_buffer *src, size_t n, grpc_slice_buffer *dst)
Definition: slice/slice_buffer.cc:348
alts_grpc_record_protocol::iovec_rp
alts_iovec_record_protocol * iovec_rp
Definition: alts_grpc_record_protocol_common.h:49
gsec_aead_crypter
Definition: gsec.h:178
grpc_slice_buffer
Definition: include/grpc/impl/codegen/slice.h:83
grpc_slice_buffer_reset_and_unref_internal
void grpc_slice_buffer_reset_and_unref_internal(grpc_slice_buffer *sb)
Definition: slice/slice_buffer.cc:238
grpc_slice_unref_internal
void grpc_slice_unref_internal(const grpc_slice &slice)
Definition: slice_refcount.h:39
port_platform.h


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