1 /*
2  *
3  * Copyright 2019 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/grpc_security.h>
20 #include <grpcpp/security/alts_context.h>
21 
22 #include "src/core/tsi/alts/handshaker/alts_tsi_handshaker.h"
23 #include "src/proto/grpc/gcp/altscontext.upb.h"
24 
25 namespace grpc {
26 namespace experimental {
27 
28 // A upb-generated grpc_gcp_AltsContext is passed in to construct an
29 // AltsContext. Normal users should use GetAltsContextFromAuthContext to get
30 // AltsContext, instead of constructing their own.
AltsContext(const grpc_gcp_AltsContext * ctx)31 AltsContext::AltsContext(const grpc_gcp_AltsContext* ctx) {
32   upb_strview application_protocol =
33       grpc_gcp_AltsContext_application_protocol(ctx);
34   if (application_protocol.data != nullptr && application_protocol.size > 0) {
35     application_protocol_ =
36         std::string(application_protocol.data, application_protocol.size);
37   }
38   upb_strview record_protocol = grpc_gcp_AltsContext_record_protocol(ctx);
39   if (record_protocol.data != nullptr && record_protocol.size > 0) {
40     record_protocol_ = std::string(record_protocol.data, record_protocol.size);
41   }
42   upb_strview peer_service_account =
43       grpc_gcp_AltsContext_peer_service_account(ctx);
44   if (peer_service_account.data != nullptr && peer_service_account.size > 0) {
45     peer_service_account_ =
46         std::string(peer_service_account.data, peer_service_account.size);
47   }
48   upb_strview local_service_account =
49       grpc_gcp_AltsContext_local_service_account(ctx);
50   if (local_service_account.data != nullptr && local_service_account.size > 0) {
51     local_service_account_ =
52         std::string(local_service_account.data, local_service_account.size);
53   }
54   const grpc_gcp_RpcProtocolVersions* versions =
55       grpc_gcp_AltsContext_peer_rpc_versions(ctx);
56   if (versions != nullptr) {
57     const grpc_gcp_RpcProtocolVersions_Version* max_version =
58         grpc_gcp_RpcProtocolVersions_max_rpc_version(versions);
59     if (max_version != nullptr) {
60       int max_version_major =
61           grpc_gcp_RpcProtocolVersions_Version_major(max_version);
62       int max_version_minor =
63           grpc_gcp_RpcProtocolVersions_Version_minor(max_version);
64       peer_rpc_versions_.max_rpc_version.major_version = max_version_major;
65       peer_rpc_versions_.max_rpc_version.minor_version = max_version_minor;
66     }
67     const grpc_gcp_RpcProtocolVersions_Version* min_version =
68         grpc_gcp_RpcProtocolVersions_min_rpc_version(versions);
69     if (min_version != nullptr) {
70       int min_version_major =
71           grpc_gcp_RpcProtocolVersions_Version_major(min_version);
72       int min_version_minor =
73           grpc_gcp_RpcProtocolVersions_Version_minor(min_version);
74       peer_rpc_versions_.min_rpc_version.major_version = min_version_major;
75       peer_rpc_versions_.min_rpc_version.minor_version = min_version_minor;
76     }
77   }
78   if (grpc_gcp_AltsContext_security_level(ctx) >= GRPC_SECURITY_MIN ||
79       grpc_gcp_AltsContext_security_level(ctx) <= GRPC_SECURITY_MAX) {
80     security_level_ = static_cast<grpc_security_level>(
81         grpc_gcp_AltsContext_security_level(ctx));
82   }
83   if (grpc_gcp_AltsContext_has_peer_attributes(ctx)) {
84     size_t iter = UPB_MAP_BEGIN;
85     const grpc_gcp_AltsContext_PeerAttributesEntry* peer_attributes_entry =
86         grpc_gcp_AltsContext_peer_attributes_next(ctx, &iter);
87     while (peer_attributes_entry != nullptr) {
88       upb_strview key =
89           grpc_gcp_AltsContext_PeerAttributesEntry_key(peer_attributes_entry);
90       upb_strview val =
91           grpc_gcp_AltsContext_PeerAttributesEntry_value(peer_attributes_entry);
92       peer_attributes_map_[std::string(key.data, key.size)] =
93           std::string(val.data, val.size);
94       peer_attributes_entry =
95           grpc_gcp_AltsContext_peer_attributes_next(ctx, &iter);
96     }
97   }
98 }
99 
application_protocol() const100 std::string AltsContext::application_protocol() const {
101   return application_protocol_;
102 }
103 
record_protocol() const104 std::string AltsContext::record_protocol() const { return record_protocol_; }
105 
peer_service_account() const106 std::string AltsContext::peer_service_account() const {
107   return peer_service_account_;
108 }
109 
local_service_account() const110 std::string AltsContext::local_service_account() const {
111   return local_service_account_;
112 }
113 
security_level() const114 grpc_security_level AltsContext::security_level() const {
115   return security_level_;
116 }
117 
peer_rpc_versions() const118 AltsContext::RpcProtocolVersions AltsContext::peer_rpc_versions() const {
119   return peer_rpc_versions_;
120 }
121 
peer_attributes() const122 const std::map<std::string, std::string>& AltsContext::peer_attributes() const {
123   return peer_attributes_map_;
124 }
125 
126 }  // namespace experimental
127 }  // namespace grpc
128