diff --git a/src/cpp/common/tls_credentials_options.cc b/src/cpp/common/tls_credentials_options.cc index c4bf2dfba6a..162e15d92e7 100644 --- a/src/cpp/common/tls_credentials_options.cc +++ b/src/cpp/common/tls_credentials_options.cc @@ -33,22 +33,26 @@ void TlsKeyMaterialsConfig::set_key_materials( namespace { /** Creates a new C struct for the key materials. **/ -grpc_tls_key_materials_config* c_key_materials(::std::shared_ptr config) { +grpc_tls_key_materials_config* c_key_materials(const ::std::shared_ptr& config) { grpc_tls_key_materials_config* c_config = grpc_tls_key_materials_config_create(); ::grpc_core::InlinedVector<::grpc_core::PemKeyCertPair, 1> c_pem_key_cert_pair_list; for (auto key_cert_pair = config->pem_key_cert_pair_list().begin(); key_cert_pair != config->pem_key_cert_pair_list().end(); key_cert_pair++) { + grpc_ssl_pem_key_cert_pair* ssl_pair = + (grpc_ssl_pem_key_cert_pair*)gpr_malloc( + sizeof(grpc_ssl_pem_key_cert_pair)); + ssl_pair->private_key = gpr_strdup(key_cert_pair->private_key.c_str()); + ssl_pair->cert_chain = gpr_strdup(key_cert_pair->cert_chain.c_str()); ::grpc_core::PemKeyCertPair c_pem_key_cert_pair = - ::grpc_core::PemKeyCertPair(key_cert_pair->private_key.c_str(), - key_cert_pair->cert_chain.c_str()); + ::grpc_core::PemKeyCertPair(ssl_pair); c_pem_key_cert_pair_list.push_back(::std::move(c_pem_key_cert_pair)); } - ::grpc_core::UniquePtr c_pem_root_certs( - gpr_strdup(config->pem_root_certs().c_str())); + ::grpc_core::UniquePtr c_pem_root_certs(gpr_strdup(config->pem_root_certs().c_str())); c_config->set_key_materials(::std::move(c_pem_root_certs), ::std::move(c_pem_key_cert_pair_list)); + c_config->set_version(config->version()); return c_config; } @@ -71,204 +75,232 @@ grpc_tls_key_materials_config* c_key_materials(::std::shared_ptrset_key_materials( ::std::move(gpr_strdup(config->pem_root_certs())), ::std::move(cpp_pem_key_cert_pair_list)); + cpp_config->set_version(config->version()); return cpp_config; } } // namespace /** TLS credential reload arg API implementation **/ +TlsCredentialReloadArg::TlsCredentialReloadArg() {} + +TlsCredentialReloadArg::TlsCredentialReloadArg( + grpc_tls_credential_reload_arg arg) { + c_arg_ = arg; +} + +TlsCredentialReloadArg::~TlsCredentialReloadArg() {} + void* TlsCredentialReloadArg::cb_user_data() const { - return c_arg_->cb_user_data; + return c_arg_.cb_user_data; } /** This function creates a new TlsKeyMaterialsConfig instance whose fields are * not shared with the corresponding key materials config fields of the * TlsCredentialReloadArg instance. **/ ::std::shared_ptr TlsCredentialReloadArg::key_materials_config() const { - return tls_key_materials_c_to_cpp(c_arg_->key_materials_config); + return tls_key_materials_c_to_cpp(c_arg_.key_materials_config); } grpc_ssl_certificate_config_reload_status TlsCredentialReloadArg::status() const { - return c_arg_->status; + return c_arg_.status; } -grpc::string TlsCredentialReloadArg::error_details() const { - return static_cast(c_arg_->error_details); +::std::shared_ptr TlsCredentialReloadArg::error_details() const { + ::std::shared_ptr cpp_error_details(new grpc::string(c_arg_.error_details)); + return cpp_error_details; } void TlsCredentialReloadArg::set_cb_user_data(void* cb_user_data) { - c_arg_->cb_user_data = cb_user_data; + c_arg_.cb_user_data = cb_user_data; } void TlsCredentialReloadArg::set_key_materials_config( ::std::shared_ptr key_materials_config) { - c_arg_->key_materials_config = c_key_materials(key_materials_config); + c_arg_.key_materials_config = c_key_materials(key_materials_config); } void TlsCredentialReloadArg::set_status( grpc_ssl_certificate_config_reload_status status) { - c_arg_->status = status; + c_arg_.status = status; } void TlsCredentialReloadArg::set_error_details(grpc::string error_details) { - c_arg_->error_details = gpr_strdup(error_details.c_str()); + c_arg_.error_details = gpr_strdup(error_details.c_str()); } -namespace { -/** Creates a smart pointer to a C++ version of the credential reload argument, - * with the callback function set to a nullptr. **/ -::std::unique_ptr tls_credential_reload_arg_c_to_cpp(const grpc_tls_credential_reload_arg* arg) { - ::std::unique_ptr cpp_arg(new TlsCredentialReloadArg()); - cpp_arg->set_cb_user_data(arg->cb_user_data); - cpp_arg->set_key_materials_config( - tls_key_materials_c_to_cpp(arg->key_materials_config)); - cpp_arg->set_status(arg->status); - cpp_arg->set_error_details(arg->error_details); - return cpp_arg; +void TlsCredentialReloadArg::callback() { + c_arg_.cb(&c_arg_); } -} // namespace void TlsCredentialReloadArg::callback() { - c_arg_->cb(c_arg_); + c_arg_.cb(c_arg_); } +namespace { +/** The C schedule and cancel functions for the credential reload config. **/ +int tls_credential_reload_config_c_schedule( + void* config_user_data, grpc_tls_credential_reload_arg* arg) { + TlsCredentialReloadConfig* cpp_config = + static_cast(arg->config->context()); + TlsCredentialReloadArg cpp_arg(*arg); + int schedule_output = cpp_config->Schedule(&cpp_arg); + arg->cb_user_data = cpp_arg.cb_user_data(); + arg->key_materials_config = c_key_materials(cpp_arg.key_materials_config()); + arg->status = cpp_arg.status(); + arg->error_details = gpr_strdup(cpp_arg.error_details()->c_str()); + return schedule_output; +} + +void tls_credential_reload_config_c_cancel( + void* config_user_data, grpc_tls_credential_reload_arg* arg) { + TlsCredentialReloadConfig* cpp_config = + static_cast(arg->config->context()); + TlsCredentialReloadArg cpp_arg(*arg); + cpp_config->Cancel(&cpp_arg); + arg->cb_user_data = cpp_arg.cb_user_data(); + arg->key_materials_config = c_key_materials(cpp_arg.key_materials_config()); + arg->status = cpp_arg.status(); + arg->error_details = cpp_arg.error_details()->c_str(); +} +} // namespace + /** gRPC TLS credential reload config API implementation **/ TlsCredentialReloadConfig::TlsCredentialReloadConfig( const void* config_user_data, int (*schedule)(void* config_user_data, TlsCredentialReloadArg* arg), void (*cancel)(void* config_user_data, TlsCredentialReloadArg* arg), - void (*destruct)(void* config_user_data)) - : config_user_data_(const_cast(config_user_data)), - schedule_(schedule), - cancel_(cancel), - destruct_(destruct) {} + void (*destruct)(void* config_user_data)) { + config_user_data_ = const_cast(config_user_data); + schedule_ = schedule; + cancel_ = cancel; + destruct_ = destruct; + c_config_ = grpc_tls_credential_reload_config_create( + config_user_data_, &tls_credential_reload_config_c_schedule, + &tls_credential_reload_config_c_cancel, destruct_); + c_config_->set_context(static_cast(this)); +} TlsCredentialReloadConfig::~TlsCredentialReloadConfig() {} -grpc_tls_credential_reload_config* -TlsCredentialReloadConfig::c_credential_reload() const { - typedef int (*grpcpp_tls_credential_reload_schedule)( - void* config_user_data, TlsCredentialReloadArg* arg); - grpcpp_tls_credential_reload_schedule cpp_schedule = schedule_; - typedef int (*grpc_tls_credential_reload_schedule)( - void* config_user_data, grpc_tls_credential_reload_arg* arg); - std::function c_schedule = - [cpp_schedule](void* config_user_data, - grpc_tls_credential_reload_arg* arg) { - return cpp_schedule(config_user_data, - tls_credential_reload_arg_c_to_cpp(arg).get()); - }; - - typedef void (*grpcpp_tls_credential_reload_cancel)( - void* config_user_data, TlsCredentialReloadArg* arg); - grpcpp_tls_credential_reload_cancel cpp_cancel = cancel_; - typedef void (*grpc_tls_credential_reload_cancel)( - void* config_user_data, grpc_tls_credential_reload_arg* arg); - std::function c_cancel = - [cpp_cancel](void* config_user_data, - grpc_tls_credential_reload_arg* arg) { - return cpp_cancel(config_user_data, - tls_credential_reload_arg_c_to_cpp(arg).get()); - }; - - grpc_tls_credential_reload_config* c_config = - grpc_tls_credential_reload_config_create( - const_cast(config_user_data_), - *(c_schedule.target()), - *(c_cancel.target()), destruct_); - return c_config; +/** gRPC TLS server authorization check arg API implementation **/ +TlsServerAuthorizationCheckArg::TlsServerAuthorizationCheckArg() {} + +TlsServerAuthorizationCheckArg::TlsServerAuthorizationCheckArg( + grpc_tls_server_authorization_check_arg arg) { + c_arg_ = arg; } -/** gRPC TLS server authorization check arg API implementation **/ +TlsServerAuthorizationCheckArg::~TlsServerAuthorizationCheckArg() {} + +void* TlsServerAuthorizationCheckArg::cb_user_data() const { + return c_arg_.cb_user_data; +} + +int TlsServerAuthorizationCheckArg::success() const { return c_arg_.success; } + +::std::shared_ptr TlsServerAuthorizationCheckArg::target_name() + const { + ::std::shared_ptr cpp_target_name( + new grpc::string(c_arg_.target_name)); + return cpp_target_name; +} + +::std::shared_ptr TlsServerAuthorizationCheckArg::peer_cert() + const { + ::std::shared_ptr cpp_peer_cert( + new grpc::string(c_arg_.peer_cert)); + return cpp_peer_cert; +} -::std::unique_ptr -tls_server_authorization_check_arg_c_to_cpp( - const grpc_tls_server_authorization_check_arg* arg) { - ::std::unique_ptr cpp_arg( - new TlsServerAuthorizationCheckArg()); - cpp_arg->set_cb( - static_cast(nullptr)); - cpp_arg->set_cb_user_data(arg->cb_user_data); - cpp_arg->set_success(arg->success); - cpp_arg->set_target_name(arg->target_name); - cpp_arg->set_peer_cert(arg->peer_cert); - cpp_arg->set_status(arg->status); - cpp_arg->set_error_details(arg->error_details); - return cpp_arg; -} - -grpc_tls_on_server_authorization_check_done_cb -TlsServerAuthorizationCheckArg::c_callback() const { - grpcpp_tls_on_server_authorization_check_done_cb cpp_cb = cb_; - std::function c_cb = - [cpp_cb](grpc_tls_server_authorization_check_arg* arg) { - return cpp_cb(tls_server_authorization_check_arg_c_to_cpp(arg).get()); - }; - return *(c_cb.target()); -} - -grpc_tls_server_authorization_check_arg* -TlsServerAuthorizationCheckArg::c_server_authorization_check_arg() const { - grpc_tls_server_authorization_check_arg* c_arg = - new grpc_tls_server_authorization_check_arg(); - c_arg->cb = this->c_callback(); - c_arg->cb_user_data = cb_user_data_; - c_arg->success = success_; - c_arg->target_name = gpr_strdup(target_name_.c_str()); - c_arg->peer_cert = gpr_strdup(peer_cert_.c_str()); - c_arg->status = status_; - c_arg->error_details = gpr_strdup(error_details_.c_str()); - return c_arg; +grpc_status_code TlsServerAuthorizationCheckArg::status() const { + return c_arg_.status; } +::std::shared_ptr TlsServerAuthorizationCheckArg::error_details() + const { + ::std::shared_ptr cpp_error_details( +new grpc::string(c_arg_.error_details)); + return cpp_error_details; +} + +void TlsServerAuthorizationCheckArg::set_cb_user_data(void* cb_user_data) { + c_arg_.cb_user_data = cb_user_data; +} + +void TlsServerAuthorizationCheckArg::set_success(int success) { + c_arg_.success = success; +} + +void TlsServerAuthorizationCheckArg::set_target_name( + const grpc::string& target_name) { + c_arg_.target_name = gpr_strdup(target_name.c_str()); +} + +void TlsServerAuthorizationCheckArg::set_peer_cert( + const grpc::string& peer_cert) { + c_arg_.peer_cert = gpr_strdup(peer_cert.c_str()); +} + +void TlsServerAuthorizationCheckArg::set_status(grpc_status_code status) { + c_arg_.status = status; +} + +void TlsServerAuthorizationCheckArg::set_error_details( + const grpc::string& error_details) { + c_arg_.error_details = gpr_strdup(error_details.c_str()); +} + +namespace { +/** The C schedule and cancel functions for the credential reload config. **/ +int tls_server_authorization_check_config_c_schedule( + void* config_user_data, grpc_tls_server_authorization_check_arg* arg) { + TlsServerAuthorizationCheckConfig* cpp_config = + static_cast(arg->config->context()); + TlsServerAuthorizationCheckArg cpp_arg(*arg); + int schedule_output = cpp_config->Schedule(&cpp_arg); + arg->cb_user_data = cpp_arg.cb_user_data(); + arg->success = cpp_arg.success(); + arg->target_name = gpr_strdup(cpp_arg.target_name()->c_str()); + arg->peer_cert = gpr_strdup(cpp_arg.peer_cert()->c_str()); + arg->status = cpp_arg.status(); + arg->error_details = gpr_strdup(cpp_arg.error_details()->c_str()); + return schedule_output; +} + +void tls_server_authorization_check_config_c_cancel( + void* config_user_data, grpc_tls_server_authorization_check_arg* arg) { + TlsServerAuthorizationCheckConfig* cpp_config = + static_cast(arg->config->context()); + TlsServerAuthorizationCheckArg cpp_arg(*arg); + cpp_config->Cancel(&cpp_arg); + arg->cb_user_data = cpp_arg.cb_user_data(); + arg->success = cpp_arg.success(); + arg->target_name = gpr_strdup(cpp_arg.target_name()->c_str()); + arg->peer_cert = gpr_strdup(cpp_arg.peer_cert()->c_str()); + arg->status = cpp_arg.status(); + arg->error_details = gpr_strdup(cpp_arg.error_details()->c_str()); +} +} // namespace + /** gRPC TLS server authorization check config API implementation **/ TlsServerAuthorizationCheckConfig::TlsServerAuthorizationCheckConfig( const void* config_user_data, int (*schedule)(void* config_user_data, TlsServerAuthorizationCheckArg* arg), void (*cancel)(void* config_user_data, TlsServerAuthorizationCheckArg* arg), - void (*destruct)(void* config_user_data)) - : config_user_data_(const_cast(config_user_data)), - schedule_(schedule), - cancel_(cancel), - destruct_(destruct) {} + void (*destruct)(void* config_user_data)) { + config_user_data_ = const_cast(config_user_data); + schedule_ = schedule; + cancel_ = cancel; +destruct_ = destruct; + c_config_ = grpc_tls_server_authorization_check_config_create( + config_user_data_, &tls_server_authorization_check_config_c_schedule, + &tls_server_authorization_check_config_c_cancel, destruct_); + c_config_->set_context(static_cast(this)); +} -TlsServerAuthorizationCheckConfig::~TlsServerAuthorizationCheckConfig() {} -grpc_tls_server_authorization_check_config* -TlsServerAuthorizationCheckConfig::c_server_authorization_check() const { - typedef int (*grpcpp_tls_server_authorization_check_schedule)( - void* config_user_data, TlsServerAuthorizationCheckArg* arg); - grpcpp_tls_server_authorization_check_schedule cpp_schedule = schedule_; - typedef int (*grpc_tls_server_authorization_check_schedule)( - void* config_user_data, grpc_tls_server_authorization_check_arg* arg); - std::function - c_schedule = - [cpp_schedule](void* config_user_data, - grpc_tls_server_authorization_check_arg* arg) { - return cpp_schedule( - config_user_data, - tls_server_authorization_check_arg_c_to_cpp(arg).get()); - }; - typedef void (*grpcpp_tls_server_authorization_check_cancel)( - void* config_user_data, TlsServerAuthorizationCheckArg* arg); - grpcpp_tls_server_authorization_check_cancel cpp_cancel = cancel_; - typedef void (*grpc_tls_server_authorization_check_cancel)( - void* config_user_data, grpc_tls_server_authorization_check_arg* arg); - std::function - c_cancel = [cpp_cancel](void* config_user_data, - grpc_tls_server_authorization_check_arg* arg) { - return cpp_cancel( - config_user_data, - tls_server_authorization_check_arg_c_to_cpp(arg).get()); - }; - grpc_tls_server_authorization_check_config* c_config = - grpc_tls_server_authorization_check_config_create( - const_cast(config_user_data_), - *(c_schedule.target()), - *(c_cancel.target()), - destruct_); - return c_config; -} +TlsServerAuthorizationCheckConfig::~TlsServerAuthorizationCheckConfig() {} /** gRPC TLS credential options API implementation **/ grpc_tls_credentials_options* TlsCredentialsOptions::c_credentials_options()