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