tls_credentials_options_util.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "src/cpp/common/tls_credentials_options_util.h"
  19. #include <grpcpp/security/tls_credentials_options.h>
  20. namespace grpc_impl {
  21. namespace experimental {
  22. /** Converts the Cpp key materials to C key materials; this allocates memory for
  23. * the C key materials. Note that the user must free
  24. * the underlying pointer to private key and cert chain duplicates; they are not
  25. * freed when the grpc_core::UniquePtr<char> member variables of PemKeyCertPair
  26. * are unused. Similarly, the user must free the underlying pointer to
  27. * c_pem_root_certs. **/
  28. grpc_tls_key_materials_config* ConvertToCKeyMaterialsConfig(
  29. const std::shared_ptr<TlsKeyMaterialsConfig>& config) {
  30. if (config == nullptr) {
  31. return nullptr;
  32. }
  33. grpc_tls_key_materials_config* c_config =
  34. grpc_tls_key_materials_config_create();
  35. ::grpc_core::InlinedVector<::grpc_core::PemKeyCertPair, 1>
  36. c_pem_key_cert_pair_list;
  37. for (const auto& key_cert_pair : config->pem_key_cert_pair_list()) {
  38. grpc_ssl_pem_key_cert_pair* ssl_pair =
  39. (grpc_ssl_pem_key_cert_pair*)gpr_malloc(
  40. sizeof(grpc_ssl_pem_key_cert_pair));
  41. ssl_pair->private_key = gpr_strdup(key_cert_pair.private_key.c_str());
  42. ssl_pair->cert_chain = gpr_strdup(key_cert_pair.cert_chain.c_str());
  43. ::grpc_core::PemKeyCertPair c_pem_key_cert_pair =
  44. ::grpc_core::PemKeyCertPair(ssl_pair);
  45. c_pem_key_cert_pair_list.push_back(::std::move(c_pem_key_cert_pair));
  46. }
  47. ::grpc_core::UniquePtr<char> c_pem_root_certs(
  48. gpr_strdup(config->pem_root_certs().c_str()));
  49. c_config->set_key_materials(std::move(c_pem_root_certs),
  50. std::move(c_pem_key_cert_pair_list));
  51. c_config->set_version(config->version());
  52. return c_config;
  53. }
  54. /** The C schedule and cancel functions for the credential reload config.
  55. * They populate a C credential reload arg with the result of a C++ credential
  56. * reload schedule/cancel API. **/
  57. int TlsCredentialReloadConfigCSchedule(void* /*config_user_data*/,
  58. grpc_tls_credential_reload_arg* arg) {
  59. if (arg == nullptr || arg->config == nullptr ||
  60. arg->config->context() == nullptr) {
  61. gpr_log(GPR_ERROR, "credential reload arg was not properly initialized");
  62. return 1;
  63. }
  64. TlsCredentialReloadConfig* cpp_config =
  65. static_cast<TlsCredentialReloadConfig*>(arg->config->context());
  66. TlsCredentialReloadArg* cpp_arg = new TlsCredentialReloadArg(arg);
  67. int schedule_result = cpp_config->Schedule(cpp_arg);
  68. return schedule_result;
  69. }
  70. void TlsCredentialReloadConfigCCancel(void* /*config_user_data*/,
  71. grpc_tls_credential_reload_arg* arg) {
  72. if (arg == nullptr || arg->config == nullptr ||
  73. arg->config->context() == nullptr) {
  74. gpr_log(GPR_ERROR, "credential reload arg was not properly initialized");
  75. return;
  76. }
  77. if (arg->context == nullptr) {
  78. gpr_log(GPR_ERROR, "credential reload arg schedule has already completed");
  79. return;
  80. }
  81. TlsCredentialReloadConfig* cpp_config =
  82. static_cast<TlsCredentialReloadConfig*>(arg->config->context());
  83. TlsCredentialReloadArg* cpp_arg =
  84. static_cast<TlsCredentialReloadArg*>(arg->context);
  85. cpp_config->Cancel(cpp_arg);
  86. }
  87. void TlsCredentialReloadArgDestroyContext(void* context) {
  88. if (context != nullptr) {
  89. TlsCredentialReloadArg* cpp_arg =
  90. static_cast<TlsCredentialReloadArg*>(context);
  91. delete cpp_arg;
  92. }
  93. }
  94. /** The C schedule and cancel functions for the server authorization check
  95. * config. They populate a C server authorization check arg with the result
  96. * of a C++ server authorization check schedule/cancel API. **/
  97. int TlsServerAuthorizationCheckConfigCSchedule(
  98. void* /*config_user_data*/, grpc_tls_server_authorization_check_arg* arg) {
  99. if (arg == nullptr || arg->config == nullptr ||
  100. arg->config->context() == nullptr) {
  101. gpr_log(GPR_ERROR,
  102. "server authorization check arg was not properly initialized");
  103. return 1;
  104. }
  105. TlsServerAuthorizationCheckConfig* cpp_config =
  106. static_cast<TlsServerAuthorizationCheckConfig*>(arg->config->context());
  107. TlsServerAuthorizationCheckArg* cpp_arg =
  108. new TlsServerAuthorizationCheckArg(arg);
  109. int schedule_result = cpp_config->Schedule(cpp_arg);
  110. return schedule_result;
  111. }
  112. void TlsServerAuthorizationCheckConfigCCancel(
  113. void* /*config_user_data*/, grpc_tls_server_authorization_check_arg* arg) {
  114. if (arg == nullptr || arg->config == nullptr ||
  115. arg->config->context() == nullptr) {
  116. gpr_log(GPR_ERROR,
  117. "server authorization check arg was not properly initialized");
  118. return;
  119. }
  120. if (arg->context == nullptr) {
  121. gpr_log(GPR_ERROR,
  122. "server authorization check arg schedule has already completed");
  123. return;
  124. }
  125. TlsServerAuthorizationCheckConfig* cpp_config =
  126. static_cast<TlsServerAuthorizationCheckConfig*>(arg->config->context());
  127. TlsServerAuthorizationCheckArg* cpp_arg =
  128. static_cast<TlsServerAuthorizationCheckArg*>(arg->context);
  129. cpp_config->Cancel(cpp_arg);
  130. }
  131. void TlsServerAuthorizationCheckArgDestroyContext(void* context) {
  132. if (context != nullptr) {
  133. TlsServerAuthorizationCheckArg* cpp_arg =
  134. static_cast<TlsServerAuthorizationCheckArg*>(context);
  135. delete cpp_arg;
  136. }
  137. }
  138. } // namespace experimental
  139. } // namespace grpc_impl