| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 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 "src/cpp/server/secure_server_credentials.h" |
| 20 |
|
| 21 |
#include <algorithm> |
| 22 |
#include <map> |
| 23 |
#include <memory> |
| 24 |
#include <utility> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include <grpc/grpc_security_constants.h> |
| 28 |
#include <grpc/slice.h> |
| 29 |
#include <grpc/status.h> |
| 30 |
#include <grpcpp/security/auth_metadata_processor.h> |
| 31 |
#include <grpcpp/security/tls_credentials_options.h> |
| 32 |
#include <grpcpp/support/slice.h> |
| 33 |
#include <grpcpp/support/status.h> |
| 34 |
#include <grpcpp/support/string_ref.h> |
| 35 |
|
| 36 |
#include "src/cpp/common/secure_auth_context.h" |
| 37 |
|
| 38 |
namespace grpc { |
| 39 |
|
| 40 |
void AuthMetadataProcessorAyncWrapper::Destroy(void* wrapper) { |
| 41 |
auto* w = static_cast<AuthMetadataProcessorAyncWrapper*>(wrapper); |
| 42 |
delete w; |
| 43 |
} |
| 44 |
|
| 45 |
void AuthMetadataProcessorAyncWrapper::Process( |
| 46 |
void* wrapper, grpc_auth_context* context, const grpc_metadata* md, |
| 47 |
size_t num_md, grpc_process_auth_metadata_done_cb cb, void* user_data) { |
| 48 |
auto* w = static_cast<AuthMetadataProcessorAyncWrapper*>(wrapper); |
| 49 |
if (!w->processor_) { |
| 50 |
// Early exit. |
| 51 |
cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_OK, nullptr); |
| 52 |
return; |
| 53 |
} |
| 54 |
if (w->processor_->IsBlocking()) { |
| 55 |
w->thread_pool_->Add([w, context, md, num_md, cb, user_data] { |
| 56 |
w->AuthMetadataProcessorAyncWrapper::InvokeProcessor(context, md, num_md, |
| 57 |
cb, user_data); |
| 58 |
}); |
| 59 |
} else { |
| 60 |
// invoke directly. |
| 61 |
w->InvokeProcessor(context, md, num_md, cb, user_data); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void AuthMetadataProcessorAyncWrapper::InvokeProcessor( |
| 66 |
grpc_auth_context* context, const grpc_metadata* md, size_t num_md, |
| 67 |
grpc_process_auth_metadata_done_cb cb, void* user_data) { |
| 68 |
AuthMetadataProcessor::InputMetadata metadata; |
| 69 |
for (size_t i = 0; i < num_md; i++) { |
| 70 |
metadata.insert(std::make_pair(StringRefFromSlice(&md[i].key), |
| 71 |
StringRefFromSlice(&md[i].value))); |
| 72 |
} |
| 73 |
SecureAuthContext ctx(context); |
| 74 |
AuthMetadataProcessor::OutputMetadata consumed_metadata; |
| 75 |
AuthMetadataProcessor::OutputMetadata response_metadata; |
| 76 |
|
| 77 |
Status status = processor_->Process(metadata, &ctx, &consumed_metadata, |
| 78 |
&response_metadata); |
| 79 |
|
| 80 |
std::vector<grpc_metadata> consumed_md; |
| 81 |
for (const auto& consumed : consumed_metadata) { |
| 82 |
grpc_metadata md_entry; |
| 83 |
md_entry.key = SliceReferencingString(consumed.first); |
| 84 |
md_entry.value = SliceReferencingString(consumed.second); |
| 85 |
consumed_md.push_back(md_entry); |
| 86 |
} |
| 87 |
std::vector<grpc_metadata> response_md; |
| 88 |
for (const auto& response : response_metadata) { |
| 89 |
grpc_metadata md_entry; |
| 90 |
md_entry.key = SliceReferencingString(response.first); |
| 91 |
md_entry.value = SliceReferencingString(response.second); |
| 92 |
response_md.push_back(md_entry); |
| 93 |
} |
| 94 |
auto consumed_md_data = consumed_md.empty() ? nullptr : &consumed_md[0]; |
| 95 |
auto response_md_data = response_md.empty() ? nullptr : &response_md[0]; |
| 96 |
cb(user_data, consumed_md_data, consumed_md.size(), response_md_data, |
| 97 |
response_md.size(), static_cast<grpc_status_code>(status.error_code()), |
| 98 |
status.error_message().c_str()); |
| 99 |
} |
| 100 |
|
| 101 |
int SecureServerCredentials::AddPortToServer(const std::string& addr, |
| 102 |
grpc_server* server) { |
| 103 |
return grpc_server_add_http2_port(server, addr.c_str(), creds_); |
| 104 |
} |
| 105 |
|
| 106 |
void SecureServerCredentials::SetAuthMetadataProcessor( |
| 107 |
const std::shared_ptr<grpc::AuthMetadataProcessor>& processor) { |
| 108 |
auto* wrapper = new grpc::AuthMetadataProcessorAyncWrapper(processor); |
| 109 |
grpc_server_credentials_set_auth_metadata_processor( |
| 110 |
creds_, {grpc::AuthMetadataProcessorAyncWrapper::Process, |
| 111 |
grpc::AuthMetadataProcessorAyncWrapper::Destroy, wrapper}); |
| 112 |
} |
| 113 |
|
| 114 |
std::shared_ptr<ServerCredentials> SslServerCredentials( |
| 115 |
const grpc::SslServerCredentialsOptions& options) { |
| 116 |
std::vector<grpc_ssl_pem_key_cert_pair> pem_key_cert_pairs; |
| 117 |
for (const auto& key_cert_pair : options.pem_key_cert_pairs) { |
| 118 |
grpc_ssl_pem_key_cert_pair p = {key_cert_pair.private_key.c_str(), |
| 119 |
key_cert_pair.cert_chain.c_str()}; |
| 120 |
pem_key_cert_pairs.push_back(p); |
| 121 |
} |
| 122 |
grpc_server_credentials* c_creds = grpc_ssl_server_credentials_create_ex( |
| 123 |
options.pem_root_certs.empty() ? nullptr : options.pem_root_certs.c_str(), |
| 124 |
pem_key_cert_pairs.empty() ? nullptr : &pem_key_cert_pairs[0], |
| 125 |
pem_key_cert_pairs.size(), |
| 126 |
options.force_client_auth |
| 127 |
? GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY |
| 128 |
: options.client_certificate_request, |
| 129 |
nullptr); |
| 130 |
return std::shared_ptr<ServerCredentials>( |
| 131 |
new SecureServerCredentials(c_creds)); |
| 132 |
} |
| 133 |
|
| 134 |
namespace experimental { |
| 135 |
|
| 136 |
std::shared_ptr<ServerCredentials> AltsServerCredentials( |
| 137 |
const AltsServerCredentialsOptions& /* options */) { |
| 138 |
grpc_alts_credentials_options* c_options = |
| 139 |
grpc_alts_credentials_server_options_create(); |
| 140 |
grpc_server_credentials* c_creds = |
| 141 |
grpc_alts_server_credentials_create(c_options); |
| 142 |
grpc_alts_credentials_options_destroy(c_options); |
| 143 |
return std::shared_ptr<ServerCredentials>( |
| 144 |
new SecureServerCredentials(c_creds)); |
| 145 |
} |
| 146 |
|
| 147 |
std::shared_ptr<ServerCredentials> LocalServerCredentials( |
| 148 |
grpc_local_connect_type type) { |
| 149 |
return std::shared_ptr<ServerCredentials>( |
| 150 |
new SecureServerCredentials(grpc_local_server_credentials_create(type))); |
| 151 |
} |
| 152 |
|
| 153 |
std::shared_ptr<ServerCredentials> TlsServerCredentials( |
| 154 |
const grpc::experimental::TlsServerCredentialsOptions& options) { |
| 155 |
return std::shared_ptr<ServerCredentials>(new SecureServerCredentials( |
| 156 |
grpc_tls_server_credentials_create(options.c_credentials_options()))); |
| 157 |
} |
| 158 |
|
| 159 |
} // namespace experimental |
| 160 |
} // namespace grpc |