tls_credentials_options_util.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. c_config->set_key_materials(config->pem_root_certs().c_str(),
  48. c_pem_key_cert_pair_list);
  49. c_config->set_version(config->version());
  50. return c_config;
  51. }
  52. /** The C schedule and cancel functions for the credential reload config.
  53. * They populate a C credential reload arg with the result of a C++ credential
  54. * reload schedule/cancel API. **/
  55. int TlsCredentialReloadConfigCSchedule(void* /*config_user_data*/,
  56. grpc_tls_credential_reload_arg* arg) {
  57. if (arg == nullptr || arg->config == nullptr ||
  58. arg->config->context() == nullptr) {
  59. gpr_log(GPR_ERROR, "credential reload arg was not properly initialized");
  60. return 1;
  61. }
  62. TlsCredentialReloadConfig* cpp_config =
  63. static_cast<TlsCredentialReloadConfig*>(arg->config->context());
  64. TlsCredentialReloadArg* cpp_arg = new TlsCredentialReloadArg(arg);
  65. int schedule_result = cpp_config->Schedule(cpp_arg);
  66. return schedule_result;
  67. }
  68. void TlsCredentialReloadConfigCCancel(void* /*config_user_data*/,
  69. grpc_tls_credential_reload_arg* arg) {
  70. if (arg == nullptr || arg->config == nullptr ||
  71. arg->config->context() == nullptr) {
  72. gpr_log(GPR_ERROR, "credential reload arg was not properly initialized");
  73. return;
  74. }
  75. if (arg->context == nullptr) {
  76. gpr_log(GPR_ERROR, "credential reload arg schedule has already completed");
  77. return;
  78. }
  79. TlsCredentialReloadConfig* cpp_config =
  80. static_cast<TlsCredentialReloadConfig*>(arg->config->context());
  81. TlsCredentialReloadArg* cpp_arg =
  82. static_cast<TlsCredentialReloadArg*>(arg->context);
  83. cpp_config->Cancel(cpp_arg);
  84. }
  85. void TlsCredentialReloadArgDestroyContext(void* context) {
  86. if (context != nullptr) {
  87. TlsCredentialReloadArg* cpp_arg =
  88. static_cast<TlsCredentialReloadArg*>(context);
  89. delete cpp_arg;
  90. }
  91. }
  92. /** The C schedule and cancel functions for the server authorization check
  93. * config. They populate a C server authorization check arg with the result
  94. * of a C++ server authorization check schedule/cancel API. **/
  95. int TlsServerAuthorizationCheckConfigCSchedule(
  96. void* /*config_user_data*/, grpc_tls_server_authorization_check_arg* arg) {
  97. if (arg == nullptr || arg->config == nullptr ||
  98. arg->config->context() == nullptr) {
  99. gpr_log(GPR_ERROR,
  100. "server authorization check arg was not properly initialized");
  101. return 1;
  102. }
  103. TlsServerAuthorizationCheckConfig* cpp_config =
  104. static_cast<TlsServerAuthorizationCheckConfig*>(arg->config->context());
  105. TlsServerAuthorizationCheckArg* cpp_arg =
  106. new TlsServerAuthorizationCheckArg(arg);
  107. int schedule_result = cpp_config->Schedule(cpp_arg);
  108. return schedule_result;
  109. }
  110. void TlsServerAuthorizationCheckConfigCCancel(
  111. void* /*config_user_data*/, grpc_tls_server_authorization_check_arg* arg) {
  112. if (arg == nullptr || arg->config == nullptr ||
  113. arg->config->context() == nullptr) {
  114. gpr_log(GPR_ERROR,
  115. "server authorization check arg was not properly initialized");
  116. return;
  117. }
  118. if (arg->context == nullptr) {
  119. gpr_log(GPR_ERROR,
  120. "server authorization check arg schedule has already completed");
  121. return;
  122. }
  123. TlsServerAuthorizationCheckConfig* cpp_config =
  124. static_cast<TlsServerAuthorizationCheckConfig*>(arg->config->context());
  125. TlsServerAuthorizationCheckArg* cpp_arg =
  126. static_cast<TlsServerAuthorizationCheckArg*>(arg->context);
  127. cpp_config->Cancel(cpp_arg);
  128. }
  129. void TlsServerAuthorizationCheckArgDestroyContext(void* context) {
  130. if (context != nullptr) {
  131. TlsServerAuthorizationCheckArg* cpp_arg =
  132. static_cast<TlsServerAuthorizationCheckArg*>(context);
  133. delete cpp_arg;
  134. }
  135. }
  136. } // namespace experimental
  137. } // namespace grpc_impl