blob: d472667a7eec32971936d6d15da54630ce1bbfe4 [file] [log] [blame]
Craig Tiller42bc87c2015-02-23 08:50:19 -08001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Julien Boeufc2274e72015-08-10 12:45:17 -070034#include <functional>
35#include <map>
36#include <memory>
37
Julien Boeufc2274e72015-08-10 12:45:17 -070038#include "src/cpp/common/secure_auth_context.h"
Julien Boeuf1d2240c2015-04-09 21:07:56 -070039#include "src/cpp/server/secure_server_credentials.h"
Craig Tiller42bc87c2015-02-23 08:50:19 -080040
Julien Boeuf5be92a32015-08-28 16:28:18 -070041#include <grpc++/security/auth_metadata_processor.h>
Julien Boeufc2274e72015-08-10 12:45:17 -070042
Craig Tiller42bc87c2015-02-23 08:50:19 -080043namespace grpc {
44
Craig Tiller71a0f9d2015-09-28 17:22:01 -070045void AuthMetadataProcessorAyncWrapper::Destroy(void* wrapper) {
Julien Boeuf0c711ad2015-08-28 14:10:58 -070046 auto* w = reinterpret_cast<AuthMetadataProcessorAyncWrapper*>(wrapper);
47 delete w;
48}
49
Julien Boeufc2274e72015-08-10 12:45:17 -070050void AuthMetadataProcessorAyncWrapper::Process(
Julien Boeufbf25bb02015-08-14 12:36:11 -070051 void* wrapper, grpc_auth_context* context, const grpc_metadata* md,
52 size_t num_md, grpc_process_auth_metadata_done_cb cb, void* user_data) {
53 auto* w = reinterpret_cast<AuthMetadataProcessorAyncWrapper*>(wrapper);
Vijay Paie57abcf2015-09-29 22:55:34 +000054 if (!w->processor_) {
Julien Boeufbf25bb02015-08-14 12:36:11 -070055 // Early exit.
Julien Boeuf5b3516e2015-08-26 11:19:10 -070056 cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_OK, nullptr);
Julien Boeufbf25bb02015-08-14 12:36:11 -070057 return;
Julien Boeuf59413352015-08-13 22:03:56 -070058 }
Julien Boeufbf25bb02015-08-14 12:36:11 -070059 if (w->processor_->IsBlocking()) {
60 w->thread_pool_->Add(
61 std::bind(&AuthMetadataProcessorAyncWrapper::InvokeProcessor, w,
62 context, md, num_md, cb, user_data));
63 } else {
64 // invoke directly.
65 w->InvokeProcessor(context, md, num_md, cb, user_data);
66 }
Julien Boeufc2274e72015-08-10 12:45:17 -070067}
68
Julien Boeufbf25bb02015-08-14 12:36:11 -070069void AuthMetadataProcessorAyncWrapper::InvokeProcessor(
Craig Tiller71a0f9d2015-09-28 17:22:01 -070070 grpc_auth_context* ctx, const grpc_metadata* md, size_t num_md,
Julien Boeufc2274e72015-08-10 12:45:17 -070071 grpc_process_auth_metadata_done_cb cb, void* user_data) {
Julien Boeuf35b559f2015-08-26 23:17:35 -070072 AuthMetadataProcessor::InputMetadata metadata;
Julien Boeufbf25bb02015-08-14 12:36:11 -070073 for (size_t i = 0; i < num_md; i++) {
74 metadata.insert(std::make_pair(
Julien Boeuf35b559f2015-08-26 23:17:35 -070075 md[i].key, grpc::string_ref(md[i].value, md[i].value_length)));
Julien Boeufbf25bb02015-08-14 12:36:11 -070076 }
Julien Boeuf0c711ad2015-08-28 14:10:58 -070077 SecureAuthContext context(ctx, false);
Julien Boeuf35b559f2015-08-26 23:17:35 -070078 AuthMetadataProcessor::OutputMetadata consumed_metadata;
79 AuthMetadataProcessor::OutputMetadata response_metadata;
80
81 Status status = processor_->Process(metadata, &context, &consumed_metadata,
82 &response_metadata);
83
Julien Boeuf0c711ad2015-08-28 14:10:58 -070084 std::vector<grpc_metadata> consumed_md;
Julien Boeuf35b559f2015-08-26 23:17:35 -070085 for (auto it = consumed_metadata.begin(); it != consumed_metadata.end();
86 ++it) {
Vijay Paie57abcf2015-09-29 22:55:34 +000087 grpc_metadata md_entry;
88 md_entry.key = it->first.c_str();
89 md_entry.value = it->second.data();
90 md_entry.value_length = it->second.size();
91 md_entry.flags = 0;
92 consumed_md.push_back(md_entry);
Julien Boeufc2274e72015-08-10 12:45:17 -070093 }
Julien Boeuf0c711ad2015-08-28 14:10:58 -070094 std::vector<grpc_metadata> response_md;
Julien Boeuf35b559f2015-08-26 23:17:35 -070095 for (auto it = response_metadata.begin(); it != response_metadata.end();
96 ++it) {
Vijay Paie57abcf2015-09-29 22:55:34 +000097 grpc_metadata md_entry;
98 md_entry.key = it->first.c_str();
99 md_entry.value = it->second.data();
100 md_entry.value_length = it->second.size();
101 md_entry.flags = 0;
102 response_md.push_back(md_entry);
Julien Boeuf35b559f2015-08-26 23:17:35 -0700103 }
yang-g61e461e2015-09-03 13:08:59 -0700104 auto consumed_md_data = consumed_md.empty() ? nullptr : &consumed_md[0];
105 auto response_md_data = response_md.empty() ? nullptr : &response_md[0];
106 cb(user_data, consumed_md_data, consumed_md.size(), response_md_data,
Julien Boeuf35b559f2015-08-26 23:17:35 -0700107 response_md.size(), static_cast<grpc_status_code>(status.error_code()),
108 status.error_message().c_str());
Julien Boeufc2274e72015-08-10 12:45:17 -0700109}
110
Craig Tillerd6c98df2015-08-18 09:33:44 -0700111int SecureServerCredentials::AddPortToServer(const grpc::string& addr,
112 grpc_server* server) {
Julien Boeuf1d2240c2015-04-09 21:07:56 -0700113 return grpc_server_add_secure_http2_port(server, addr.c_str(), creds_);
114}
Craig Tiller42bc87c2015-02-23 08:50:19 -0800115
Julien Boeufc2274e72015-08-10 12:45:17 -0700116void SecureServerCredentials::SetAuthMetadataProcessor(
117 const std::shared_ptr<AuthMetadataProcessor>& processor) {
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700118 auto* wrapper = new AuthMetadataProcessorAyncWrapper(processor);
Julien Boeufc2274e72015-08-10 12:45:17 -0700119 grpc_server_credentials_set_auth_metadata_processor(
Julien Boeuf0c711ad2015-08-28 14:10:58 -0700120 creds_, {AuthMetadataProcessorAyncWrapper::Process,
121 AuthMetadataProcessorAyncWrapper::Destroy, wrapper});
Julien Boeufc2274e72015-08-10 12:45:17 -0700122}
123
Craig Tiller42bc87c2015-02-23 08:50:19 -0800124std::shared_ptr<ServerCredentials> SslServerCredentials(
Yang Gao6baa9b62015-03-17 10:49:39 -0700125 const SslServerCredentialsOptions& options) {
Craig Tiller42bc87c2015-02-23 08:50:19 -0800126 std::vector<grpc_ssl_pem_key_cert_pair> pem_key_cert_pairs;
Vijay Pai82dd80a2015-03-24 10:36:08 -0700127 for (auto key_cert_pair = options.pem_key_cert_pairs.begin();
Julien Boeuf1d2240c2015-04-09 21:07:56 -0700128 key_cert_pair != options.pem_key_cert_pairs.end(); key_cert_pair++) {
Vijay Pai82dd80a2015-03-24 10:36:08 -0700129 grpc_ssl_pem_key_cert_pair p = {key_cert_pair->private_key.c_str(),
Julien Boeuf1d2240c2015-04-09 21:07:56 -0700130 key_cert_pair->cert_chain.c_str()};
Vijay Pai82dd80a2015-03-24 10:36:08 -0700131 pem_key_cert_pairs.push_back(p);
Craig Tiller42bc87c2015-02-23 08:50:19 -0800132 }
Yang Gao6baa9b62015-03-17 10:49:39 -0700133 grpc_server_credentials* c_creds = grpc_ssl_server_credentials_create(
Craig Tiller42bc87c2015-02-23 08:50:19 -0800134 options.pem_root_certs.empty() ? nullptr : options.pem_root_certs.c_str(),
yang-g61e461e2015-09-03 13:08:59 -0700135 pem_key_cert_pairs.empty() ? nullptr : &pem_key_cert_pairs[0],
136 pem_key_cert_pairs.size(), options.force_client_auth, nullptr);
Yang Gao6baa9b62015-03-17 10:49:39 -0700137 return std::shared_ptr<ServerCredentials>(
138 new SecureServerCredentials(c_creds));
Craig Tiller42bc87c2015-02-23 08:50:19 -0800139}
140
141} // namespace grpc