| 1 |
// |
| 2 |
// Copyright 2021 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include <stddef.h> |
| 18 |
|
| 19 |
#include <algorithm> |
| 20 |
#include <functional> |
| 21 |
#include <map> |
| 22 |
#include <string> |
| 23 |
#include <utility> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
#include <grpc/grpc_security.h> |
| 27 |
#include <grpc/status.h> |
| 28 |
#include <grpc/support/alloc.h> |
| 29 |
#include <grpc/support/log.h> |
| 30 |
#include <grpc/support/string_util.h> |
| 31 |
#include <grpcpp/impl/codegen/sync.h> |
| 32 |
#include <grpcpp/impl/grpc_library.h> |
| 33 |
#include <grpcpp/security/tls_certificate_verifier.h> |
| 34 |
#include <grpcpp/support/status.h> |
| 35 |
#include <grpcpp/support/string_ref.h> |
| 36 |
|
| 37 |
namespace grpc { |
| 38 |
namespace experimental { |
| 39 |
|
| 40 |
static internal::GrpcLibraryInitializer g_gli_initializer; |
| 41 |
|
| 42 |
TlsCustomVerificationCheckRequest::TlsCustomVerificationCheckRequest( |
| 43 |
grpc_tls_custom_verification_check_request* request) |
| 44 |
: c_request_(request) { |
| 45 |
GPR_ASSERT(c_request_ != nullptr); |
| 46 |
} |
| 47 |
|
| 48 |
grpc::string_ref TlsCustomVerificationCheckRequest::target_name() const { |
| 49 |
return c_request_->target_name != nullptr ? c_request_->target_name : ""; |
| 50 |
} |
| 51 |
|
| 52 |
grpc::string_ref TlsCustomVerificationCheckRequest::peer_cert() const { |
| 53 |
return c_request_->peer_info.peer_cert != nullptr |
| 54 |
? c_request_->peer_info.peer_cert |
| 55 |
: ""; |
| 56 |
} |
| 57 |
|
| 58 |
grpc::string_ref TlsCustomVerificationCheckRequest::peer_cert_full_chain() |
| 59 |
const { |
| 60 |
return c_request_->peer_info.peer_cert_full_chain != nullptr |
| 61 |
? c_request_->peer_info.peer_cert_full_chain |
| 62 |
: ""; |
| 63 |
} |
| 64 |
|
| 65 |
grpc::string_ref TlsCustomVerificationCheckRequest::common_name() const { |
| 66 |
return c_request_->peer_info.common_name != nullptr |
| 67 |
? c_request_->peer_info.common_name |
| 68 |
: ""; |
| 69 |
} |
| 70 |
|
| 71 |
std::vector<grpc::string_ref> TlsCustomVerificationCheckRequest::uri_names() |
| 72 |
const { |
| 73 |
std::vector<grpc::string_ref> uri_names; |
| 74 |
for (size_t i = 0; i < c_request_->peer_info.san_names.uri_names_size; ++i) { |
| 75 |
uri_names.emplace_back(c_request_->peer_info.san_names.uri_names[i]); |
| 76 |
} |
| 77 |
return uri_names; |
| 78 |
} |
| 79 |
|
| 80 |
std::vector<grpc::string_ref> TlsCustomVerificationCheckRequest::dns_names() |
| 81 |
const { |
| 82 |
std::vector<grpc::string_ref> dns_names; |
| 83 |
for (size_t i = 0; i < c_request_->peer_info.san_names.dns_names_size; ++i) { |
| 84 |
dns_names.emplace_back(c_request_->peer_info.san_names.dns_names[i]); |
| 85 |
} |
| 86 |
return dns_names; |
| 87 |
} |
| 88 |
|
| 89 |
std::vector<grpc::string_ref> TlsCustomVerificationCheckRequest::email_names() |
| 90 |
const { |
| 91 |
std::vector<grpc::string_ref> email_names; |
| 92 |
for (size_t i = 0; i < c_request_->peer_info.san_names.email_names_size; |
| 93 |
++i) { |
| 94 |
email_names.emplace_back(c_request_->peer_info.san_names.email_names[i]); |
| 95 |
} |
| 96 |
return email_names; |
| 97 |
} |
| 98 |
|
| 99 |
std::vector<grpc::string_ref> TlsCustomVerificationCheckRequest::ip_names() |
| 100 |
const { |
| 101 |
std::vector<grpc::string_ref> ip_names; |
| 102 |
for (size_t i = 0; i < c_request_->peer_info.san_names.ip_names_size; ++i) { |
| 103 |
ip_names.emplace_back(c_request_->peer_info.san_names.ip_names[i]); |
| 104 |
} |
| 105 |
return ip_names; |
| 106 |
} |
| 107 |
|
| 108 |
CertificateVerifier::CertificateVerifier(grpc_tls_certificate_verifier* v) |
| 109 |
: verifier_(v) { |
| 110 |
g_gli_initializer.summon(); |
| 111 |
} |
| 112 |
|
| 113 |
CertificateVerifier::~CertificateVerifier() { |
| 114 |
grpc_tls_certificate_verifier_release(verifier_); |
| 115 |
} |
| 116 |
|
| 117 |
bool CertificateVerifier::Verify(TlsCustomVerificationCheckRequest* request, |
| 118 |
std::function<void(grpc::Status)> callback, |
| 119 |
grpc::Status* sync_status) { |
| 120 |
GPR_ASSERT(request != nullptr); |
| 121 |
GPR_ASSERT(request->c_request() != nullptr); |
| 122 |
{ |
| 123 |
internal::MutexLock lock(&mu_); |
| 124 |
request_map_.emplace(request->c_request(), std::move(callback)); |
| 125 |
} |
| 126 |
grpc_status_code status_code = GRPC_STATUS_OK; |
| 127 |
char* error_details = nullptr; |
| 128 |
bool is_done = grpc_tls_certificate_verifier_verify( |
| 129 |
verifier_, request->c_request(), &AsyncCheckDone, this, &status_code, |
| 130 |
&error_details); |
| 131 |
if (is_done) { |
| 132 |
if (status_code != GRPC_STATUS_OK) { |
| 133 |
*sync_status = grpc::Status(static_cast<grpc::StatusCode>(status_code), |
| 134 |
error_details); |
| 135 |
} |
| 136 |
internal::MutexLock lock(&mu_); |
| 137 |
request_map_.erase(request->c_request()); |
| 138 |
} |
| 139 |
gpr_free(error_details); |
| 140 |
return is_done; |
| 141 |
} |
| 142 |
|
| 143 |
void CertificateVerifier::Cancel(TlsCustomVerificationCheckRequest* request) { |
| 144 |
GPR_ASSERT(request != nullptr); |
| 145 |
GPR_ASSERT(request->c_request() != nullptr); |
| 146 |
grpc_tls_certificate_verifier_cancel(verifier_, request->c_request()); |
| 147 |
} |
| 148 |
|
| 149 |
void CertificateVerifier::AsyncCheckDone( |
| 150 |
grpc_tls_custom_verification_check_request* request, void* callback_arg, |
| 151 |
grpc_status_code status, const char* error_details) { |
| 152 |
auto* self = static_cast<CertificateVerifier*>(callback_arg); |
| 153 |
std::function<void(grpc::Status)> callback; |
| 154 |
{ |
| 155 |
internal::MutexLock lock(&self->mu_); |
| 156 |
auto it = self->request_map_.find(request); |
| 157 |
if (it != self->request_map_.end()) { |
| 158 |
callback = std::move(it->second); |
| 159 |
self->request_map_.erase(it); |
| 160 |
} |
| 161 |
} |
| 162 |
if (callback != nullptr) { |
| 163 |
grpc::Status return_status; |
| 164 |
if (status != GRPC_STATUS_OK) { |
| 165 |
return_status = |
| 166 |
grpc::Status(static_cast<grpc::StatusCode>(status), error_details); |
| 167 |
} |
| 168 |
callback(return_status); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
ExternalCertificateVerifier::ExternalCertificateVerifier() { |
| 173 |
base_ = new grpc_tls_certificate_verifier_external(); |
| 174 |
base_->user_data = this; |
| 175 |
base_->verify = VerifyInCoreExternalVerifier; |
| 176 |
base_->cancel = CancelInCoreExternalVerifier; |
| 177 |
base_->destruct = DestructInCoreExternalVerifier; |
| 178 |
} |
| 179 |
|
| 180 |
ExternalCertificateVerifier::~ExternalCertificateVerifier() { delete base_; } |
| 181 |
|
| 182 |
int ExternalCertificateVerifier::VerifyInCoreExternalVerifier( |
| 183 |
void* user_data, grpc_tls_custom_verification_check_request* request, |
| 184 |
grpc_tls_on_custom_verification_check_done_cb callback, void* callback_arg, |
| 185 |
grpc_status_code* sync_status, char** sync_error_details) { |
| 186 |
auto* self = static_cast<ExternalCertificateVerifier*>(user_data); |
| 187 |
TlsCustomVerificationCheckRequest* cpp_request = nullptr; |
| 188 |
{ |
| 189 |
internal::MutexLock lock(&self->mu_); |
| 190 |
auto pair = self->request_map_.emplace( |
| 191 |
request, AsyncRequestState(callback, callback_arg, request)); |
| 192 |
GPR_ASSERT(pair.second); |
| 193 |
cpp_request = &pair.first->second.cpp_request; |
| 194 |
} |
| 195 |
grpc::Status sync_current_verifier_status; |
| 196 |
bool is_done = self->Verify( |
| 197 |
cpp_request, |
| 198 |
[self, request](grpc::Status status) { |
| 199 |
grpc_tls_on_custom_verification_check_done_cb callback = nullptr; |
| 200 |
void* callback_arg = nullptr; |
| 201 |
{ |
| 202 |
internal::MutexLock lock(&self->mu_); |
| 203 |
auto it = self->request_map_.find(request); |
| 204 |
if (it != self->request_map_.end()) { |
| 205 |
callback = it->second.callback; |
| 206 |
callback_arg = it->second.callback_arg; |
| 207 |
self->request_map_.erase(it); |
| 208 |
} |
| 209 |
} |
| 210 |
if (callback != nullptr) { |
| 211 |
callback(request, callback_arg, |
| 212 |
static_cast<grpc_status_code>(status.error_code()), |
| 213 |
status.error_message().c_str()); |
| 214 |
} |
| 215 |
}, |
| 216 |
&sync_current_verifier_status); |
| 217 |
if (is_done) { |
| 218 |
if (!sync_current_verifier_status.ok()) { |
| 219 |
*sync_status = static_cast<grpc_status_code>( |
| 220 |
sync_current_verifier_status.error_code()); |
| 221 |
*sync_error_details = |
| 222 |
gpr_strdup(sync_current_verifier_status.error_message().c_str()); |
| 223 |
} |
| 224 |
internal::MutexLock lock(&self->mu_); |
| 225 |
self->request_map_.erase(request); |
| 226 |
} |
| 227 |
return is_done; |
| 228 |
} |
| 229 |
|
| 230 |
void ExternalCertificateVerifier::CancelInCoreExternalVerifier( |
| 231 |
void* user_data, grpc_tls_custom_verification_check_request* request) { |
| 232 |
auto* self = static_cast<ExternalCertificateVerifier*>(user_data); |
| 233 |
TlsCustomVerificationCheckRequest* cpp_request = nullptr; |
| 234 |
{ |
| 235 |
internal::MutexLock lock(&self->mu_); |
| 236 |
auto it = self->request_map_.find(request); |
| 237 |
if (it != self->request_map_.end()) { |
| 238 |
cpp_request = &it->second.cpp_request; |
| 239 |
} |
| 240 |
} |
| 241 |
if (cpp_request != nullptr) { |
| 242 |
self->Cancel(cpp_request); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
void ExternalCertificateVerifier::DestructInCoreExternalVerifier( |
| 247 |
void* user_data) { |
| 248 |
auto* self = static_cast<ExternalCertificateVerifier*>(user_data); |
| 249 |
delete self; |
| 250 |
} |
| 251 |
|
| 252 |
NoOpCertificateVerifier::NoOpCertificateVerifier() |
| 253 |
: CertificateVerifier(grpc_tls_certificate_verifier_no_op_create()) {} |
| 254 |
|
| 255 |
HostNameCertificateVerifier::HostNameCertificateVerifier() |
| 256 |
: CertificateVerifier(grpc_tls_certificate_verifier_host_name_create()) {} |
| 257 |
|
| 258 |
} // namespace experimental |
| 259 |
} // namespace grpc |