• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/support/alloc.h>
21 #include <grpcpp/security/tls_credentials_options.h>
22 
23 #include "absl/container/inlined_vector.h"
24 #include "src/cpp/common/tls_credentials_options_util.h"
25 
26 namespace grpc {
27 namespace experimental {
28 
29 /** gRPC TLS server authorization check arg API implementation **/
TlsServerAuthorizationCheckArg(grpc_tls_server_authorization_check_arg * arg)30 TlsServerAuthorizationCheckArg::TlsServerAuthorizationCheckArg(
31     grpc_tls_server_authorization_check_arg* arg)
32     : c_arg_(arg) {
33   GPR_ASSERT(c_arg_ != nullptr);
34   if (c_arg_->context != nullptr) {
35     gpr_log(GPR_ERROR, "c_arg context has already been set");
36   }
37   c_arg_->context = static_cast<void*>(this);
38   c_arg_->destroy_context = &TlsServerAuthorizationCheckArgDestroyContext;
39 }
40 
~TlsServerAuthorizationCheckArg()41 TlsServerAuthorizationCheckArg::~TlsServerAuthorizationCheckArg() {}
42 
cb_user_data() const43 void* TlsServerAuthorizationCheckArg::cb_user_data() const {
44   return c_arg_->cb_user_data;
45 }
46 
success() const47 int TlsServerAuthorizationCheckArg::success() const { return c_arg_->success; }
48 
target_name() const49 std::string TlsServerAuthorizationCheckArg::target_name() const {
50   std::string cpp_target_name(c_arg_->target_name);
51   return cpp_target_name;
52 }
53 
peer_cert() const54 std::string TlsServerAuthorizationCheckArg::peer_cert() const {
55   std::string cpp_peer_cert(c_arg_->peer_cert);
56   return cpp_peer_cert;
57 }
58 
peer_cert_full_chain() const59 std::string TlsServerAuthorizationCheckArg::peer_cert_full_chain() const {
60   std::string cpp_peer_cert_full_chain(c_arg_->peer_cert_full_chain);
61   return cpp_peer_cert_full_chain;
62 }
63 
status() const64 grpc_status_code TlsServerAuthorizationCheckArg::status() const {
65   return c_arg_->status;
66 }
67 
error_details() const68 std::string TlsServerAuthorizationCheckArg::error_details() const {
69   return c_arg_->error_details->error_details();
70 }
71 
set_cb_user_data(void * cb_user_data)72 void TlsServerAuthorizationCheckArg::set_cb_user_data(void* cb_user_data) {
73   c_arg_->cb_user_data = cb_user_data;
74 }
75 
set_success(int success)76 void TlsServerAuthorizationCheckArg::set_success(int success) {
77   c_arg_->success = success;
78 }
79 
set_target_name(const std::string & target_name)80 void TlsServerAuthorizationCheckArg::set_target_name(
81     const std::string& target_name) {
82   c_arg_->target_name = gpr_strdup(target_name.c_str());
83 }
84 
set_peer_cert(const std::string & peer_cert)85 void TlsServerAuthorizationCheckArg::set_peer_cert(
86     const std::string& peer_cert) {
87   c_arg_->peer_cert = gpr_strdup(peer_cert.c_str());
88 }
89 
set_peer_cert_full_chain(const std::string & peer_cert_full_chain)90 void TlsServerAuthorizationCheckArg::set_peer_cert_full_chain(
91     const std::string& peer_cert_full_chain) {
92   c_arg_->peer_cert_full_chain = gpr_strdup(peer_cert_full_chain.c_str());
93 }
94 
set_status(grpc_status_code status)95 void TlsServerAuthorizationCheckArg::set_status(grpc_status_code status) {
96   c_arg_->status = status;
97 }
98 
set_error_details(const std::string & error_details)99 void TlsServerAuthorizationCheckArg::set_error_details(
100     const std::string& error_details) {
101   c_arg_->error_details->set_error_details(error_details.c_str());
102 }
103 
OnServerAuthorizationCheckDoneCallback()104 void TlsServerAuthorizationCheckArg::OnServerAuthorizationCheckDoneCallback() {
105   if (c_arg_->cb == nullptr) {
106     gpr_log(GPR_ERROR, "server authorizaton check arg callback API is nullptr");
107     return;
108   }
109   c_arg_->cb(c_arg_);
110 }
111 
TlsServerAuthorizationCheckConfig(std::shared_ptr<TlsServerAuthorizationCheckInterface> server_authorization_check_interface)112 TlsServerAuthorizationCheckConfig::TlsServerAuthorizationCheckConfig(
113     std::shared_ptr<TlsServerAuthorizationCheckInterface>
114         server_authorization_check_interface)
115     : server_authorization_check_interface_(
116           std::move(server_authorization_check_interface)) {
117   c_config_ = grpc_tls_server_authorization_check_config_create(
118       nullptr, &TlsServerAuthorizationCheckConfigCSchedule,
119       &TlsServerAuthorizationCheckConfigCCancel, nullptr);
120   c_config_->set_context(static_cast<void*>(this));
121 }
122 
~TlsServerAuthorizationCheckConfig()123 TlsServerAuthorizationCheckConfig::~TlsServerAuthorizationCheckConfig() {
124   grpc_tls_server_authorization_check_config_release(c_config_);
125 }
126 
TlsCredentialsOptions(std::shared_ptr<CertificateProviderInterface> certificate_provider)127 TlsCredentialsOptions::TlsCredentialsOptions(
128     std::shared_ptr<CertificateProviderInterface> certificate_provider)
129     : certificate_provider_(std::move(certificate_provider)) {
130   c_credentials_options_ = grpc_tls_credentials_options_create();
131   if (certificate_provider_ != nullptr) {
132     grpc_tls_credentials_options_set_certificate_provider(
133         c_credentials_options_, certificate_provider_->c_provider());
134   }
135 }
136 
watch_root_certs()137 void TlsCredentialsOptions::watch_root_certs() {
138   grpc_tls_credentials_options_watch_root_certs(c_credentials_options_);
139 }
140 
set_root_cert_name(const std::string & root_cert_name)141 void TlsCredentialsOptions::set_root_cert_name(
142     const std::string& root_cert_name) {
143   grpc_tls_credentials_options_set_root_cert_name(c_credentials_options_,
144                                                   root_cert_name.c_str());
145 }
146 
watch_identity_key_cert_pairs()147 void TlsCredentialsOptions::watch_identity_key_cert_pairs() {
148   grpc_tls_credentials_options_watch_identity_key_cert_pairs(
149       c_credentials_options_);
150 }
151 
set_identity_cert_name(const std::string & identity_cert_name)152 void TlsCredentialsOptions::set_identity_cert_name(
153     const std::string& identity_cert_name) {
154   grpc_tls_credentials_options_set_identity_cert_name(
155       c_credentials_options_, identity_cert_name.c_str());
156 }
157 
set_server_verification_option(grpc_tls_server_verification_option server_verification_option)158 void TlsChannelCredentialsOptions::set_server_verification_option(
159     grpc_tls_server_verification_option server_verification_option) {
160   grpc_tls_credentials_options* options = c_credentials_options();
161   GPR_ASSERT(options != nullptr);
162   grpc_tls_credentials_options_set_server_verification_option(
163       options, server_verification_option);
164 }
165 
set_server_authorization_check_config(std::shared_ptr<TlsServerAuthorizationCheckConfig> config)166 void TlsChannelCredentialsOptions::set_server_authorization_check_config(
167     std::shared_ptr<TlsServerAuthorizationCheckConfig> config) {
168   grpc_tls_credentials_options* options = c_credentials_options();
169   GPR_ASSERT(options != nullptr);
170   if (config != nullptr) {
171     grpc_tls_credentials_options_set_server_authorization_check_config(
172         options, config->c_config());
173   }
174 }
175 
set_cert_request_type(grpc_ssl_client_certificate_request_type cert_request_type)176 void TlsServerCredentialsOptions::set_cert_request_type(
177     grpc_ssl_client_certificate_request_type cert_request_type) {
178   grpc_tls_credentials_options* options = c_credentials_options();
179   GPR_ASSERT(options != nullptr);
180   grpc_tls_credentials_options_set_cert_request_type(options,
181                                                      cert_request_type);
182 }
183 
184 }  // namespace experimental
185 }  // namespace grpc
186