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