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_internal.h"
26
alts_tsi_utils_convert_to_tsi_result(grpc_status_code code)27 tsi_result alts_tsi_utils_convert_to_tsi_result(grpc_status_code code) {
28 switch (code) {
29 case GRPC_STATUS_OK:
30 return TSI_OK;
31 case GRPC_STATUS_UNKNOWN:
32 return TSI_UNKNOWN_ERROR;
33 case GRPC_STATUS_INVALID_ARGUMENT:
34 return TSI_INVALID_ARGUMENT;
35 case GRPC_STATUS_NOT_FOUND:
36 return TSI_NOT_FOUND;
37 case GRPC_STATUS_INTERNAL:
38 return TSI_INTERNAL_ERROR;
39 default:
40 return TSI_UNKNOWN_ERROR;
41 }
42 }
43
alts_tsi_utils_deserialize_response(grpc_byte_buffer * resp_buffer,upb_arena * arena)44 grpc_gcp_HandshakerResp* alts_tsi_utils_deserialize_response(
45 grpc_byte_buffer* resp_buffer, upb_arena* arena) {
46 GPR_ASSERT(resp_buffer != nullptr);
47 GPR_ASSERT(arena != nullptr);
48 grpc_byte_buffer_reader bbr;
49 grpc_byte_buffer_reader_init(&bbr, resp_buffer);
50 grpc_slice slice = grpc_byte_buffer_reader_readall(&bbr);
51 size_t buf_size = GPR_SLICE_LENGTH(slice);
52 void* buf = upb_arena_malloc(arena, buf_size);
53 memcpy(buf, reinterpret_cast<const char*>(GPR_SLICE_START_PTR(slice)),
54 buf_size);
55 grpc_gcp_HandshakerResp* resp = grpc_gcp_HandshakerResp_parse(
56 reinterpret_cast<char*>(buf), buf_size, arena);
57 grpc_slice_unref_internal(slice);
58 grpc_byte_buffer_reader_destroy(&bbr);
59 if (resp == nullptr) {
60 gpr_log(GPR_ERROR, "grpc_gcp_handshaker_resp_decode() failed");
61 return nullptr;
62 }
63 return resp;
64 }
65