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)44 grpc_gcp_handshaker_resp* alts_tsi_utils_deserialize_response(
45     grpc_byte_buffer* resp_buffer) {
46   GPR_ASSERT(resp_buffer != nullptr);
47   grpc_byte_buffer_reader bbr;
48   grpc_byte_buffer_reader_init(&bbr, resp_buffer);
49   grpc_slice slice = grpc_byte_buffer_reader_readall(&bbr);
50   grpc_gcp_handshaker_resp* resp = grpc_gcp_handshaker_resp_create();
51   bool ok = grpc_gcp_handshaker_resp_decode(slice, resp);
52   grpc_slice_unref_internal(slice);
53   grpc_byte_buffer_reader_destroy(&bbr);
54   if (!ok) {
55     grpc_gcp_handshaker_resp_destroy(resp);
56     gpr_log(GPR_ERROR, "grpc_gcp_handshaker_resp_decode() failed");
57     return nullptr;
58   }
59   return resp;
60 }
61