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
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/tsi/alts/handshaker/alts_tsi_utils.h"
22
23 #include <grpc/byte_buffer_reader.h>
24
25 #include "src/core/lib/slice/slice.h"
26 #include "src/core/lib/slice/slice_internal.h"
27
alts_tsi_utils_convert_to_tsi_result(grpc_status_code code)28 tsi_result alts_tsi_utils_convert_to_tsi_result(grpc_status_code code) {
29 switch (code) {
30 case GRPC_STATUS_OK:
31 return TSI_OK;
32 case GRPC_STATUS_UNKNOWN:
33 return TSI_UNKNOWN_ERROR;
34 case GRPC_STATUS_INVALID_ARGUMENT:
35 return TSI_INVALID_ARGUMENT;
36 case GRPC_STATUS_NOT_FOUND:
37 return TSI_NOT_FOUND;
38 case GRPC_STATUS_INTERNAL:
39 return TSI_INTERNAL_ERROR;
40 default:
41 return TSI_UNKNOWN_ERROR;
42 }
43 }
44
alts_tsi_utils_deserialize_response(grpc_byte_buffer * resp_buffer,upb_Arena * arena)45 grpc_gcp_HandshakerResp* alts_tsi_utils_deserialize_response(
46 grpc_byte_buffer* resp_buffer, upb_Arena* arena) {
47 GPR_ASSERT(resp_buffer != nullptr);
48 GPR_ASSERT(arena != nullptr);
49 grpc_byte_buffer_reader bbr;
50 grpc_byte_buffer_reader_init(&bbr, resp_buffer);
51 grpc_slice slice = grpc_byte_buffer_reader_readall(&bbr);
52 size_t buf_size = GRPC_SLICE_LENGTH(slice);
53 void* buf = upb_Arena_Malloc(arena, buf_size);
54 memcpy(buf, reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(slice)),
55 buf_size);
56 grpc_gcp_HandshakerResp* resp = grpc_gcp_HandshakerResp_parse(
57 reinterpret_cast<char*>(buf), buf_size, arena);
58 grpc_core::CSliceUnref(slice);
59 grpc_byte_buffer_reader_destroy(&bbr);
60 if (resp == nullptr) {
61 gpr_log(GPR_ERROR, "grpc_gcp_handshaker_resp_decode() failed");
62 return nullptr;
63 }
64 return resp;
65 }
66