blob: 4af2c67d83ce28567e1f39a53a9dfe54a692402d [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
34#include "src/core/security/auth.h"
35
36#include <string.h>
37
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038#include <grpc/support/alloc.h>
39#include <grpc/support/log.h>
40
Julien Boeuf54b21922015-02-04 16:39:35 -080041#include "src/core/support/string.h"
42#include "src/core/channel/channel_stack.h"
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070043#include "src/core/security/security_connector.h"
Julien Boeuf54b21922015-02-04 16:39:35 -080044#include "src/core/security/credentials.h"
45#include "src/core/surface/call.h"
46
Craig Tiller9c9d4e02015-04-20 09:03:29 -070047#define MAX_CREDENTIALS_METADATA_COUNT 4
Craig Tiller6902ad22015-04-16 08:01:49 -070048
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049/* We can have a per-call credentials. */
50typedef struct {
51 grpc_credentials *creds;
Julien Boeuf54b21922015-02-04 16:39:35 -080052 grpc_mdstr *host;
Julien Boeuff47a5cb2015-02-18 12:24:08 -080053 grpc_mdstr *method;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054 grpc_call_op op;
Craig Tiller9c9d4e02015-04-20 09:03:29 -070055 grpc_linked_mdelem md_links[MAX_CREDENTIALS_METADATA_COUNT];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056} call_data;
57
58/* We can have a per-channel credentials. */
59typedef struct {
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070060 grpc_channel_security_connector *security_connector;
Julien Boeuf54b21922015-02-04 16:39:35 -080061 grpc_mdctx *md_ctx;
62 grpc_mdstr *authority_string;
Julien Boeuff47a5cb2015-02-18 12:24:08 -080063 grpc_mdstr *path_string;
Julien Boeuf54b21922015-02-04 16:39:35 -080064 grpc_mdstr *error_msg_key;
Craig Tillereb131bb2015-03-03 11:00:40 -080065 grpc_mdstr *status_key;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066} channel_data;
67
Julien Boeuf54b21922015-02-04 16:39:35 -080068static void bubbleup_error(grpc_call_element *elem, const char *error_msg) {
Craig Tiller6902ad22015-04-16 08:01:49 -070069 grpc_call_element_recv_status(elem, GRPC_STATUS_UNAUTHENTICATED, error_msg);
Julien Boeuf54b21922015-02-04 16:39:35 -080070 grpc_call_element_send_cancel(elem);
71}
72
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073static void on_credentials_metadata(void *user_data, grpc_mdelem **md_elems,
74 size_t num_md,
75 grpc_credentials_status status) {
76 grpc_call_element *elem = (grpc_call_element *)user_data;
Craig Tiller6902ad22015-04-16 08:01:49 -070077 call_data *calld = elem->call_data;
78 grpc_call_op op = calld->op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079 size_t i;
Craig Tiller9c9d4e02015-04-20 09:03:29 -070080 GPR_ASSERT(num_md <= MAX_CREDENTIALS_METADATA_COUNT);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081 for (i = 0; i < num_md; i++) {
Craig Tiller205aee12015-04-16 14:46:41 -070082 grpc_metadata_batch_add_tail(&op.data.metadata, &calld->md_links[i],
Craig Tiller76f5d462015-04-17 14:58:12 -070083 grpc_mdelem_ref(md_elems[i]));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080084 }
Craig Tiller6902ad22015-04-16 08:01:49 -070085 grpc_call_next_op(elem, &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086}
87
Julien Boeuff47a5cb2015-02-18 12:24:08 -080088static char *build_service_url(const char *url_scheme, call_data *calld) {
89 char *service_url;
90 char *service = gpr_strdup(grpc_mdstr_as_c_string(calld->method));
91 char *last_slash = strrchr(service, '/');
92 if (last_slash == NULL) {
93 gpr_log(GPR_ERROR, "No '/' found in fully qualified method name");
94 service[0] = '\0';
95 } else if (last_slash == service) {
96 /* No service part in fully qualified method name: will just be "/". */
97 service[1] = '\0';
98 } else {
99 *last_slash = '\0';
100 }
101 if (url_scheme == NULL) url_scheme = "";
102 gpr_asprintf(&service_url, "%s://%s%s", url_scheme,
103 grpc_mdstr_as_c_string(calld->host), service);
104 gpr_free(service);
105 return service_url;
106}
107
Julien Boeuf54b21922015-02-04 16:39:35 -0800108static void send_security_metadata(grpc_call_element *elem, grpc_call_op *op) {
109 /* grab pointers to our data from the call element */
110 call_data *calld = elem->call_data;
111 channel_data *channeld = elem->channel_data;
112
113 grpc_credentials *channel_creds =
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700114 channeld->security_connector->request_metadata_creds;
Julien Boeuf54b21922015-02-04 16:39:35 -0800115 /* TODO(jboeuf):
116 Decide on the policy in this case:
117 - populate both channel and call?
118 - the call takes precedence over the channel?
119 - leave this decision up to the channel credentials? */
120 if (calld->creds != NULL) {
121 gpr_log(GPR_ERROR, "Ignoring per call credentials for now.");
122 }
123 if (channel_creds != NULL &&
124 grpc_credentials_has_request_metadata(channel_creds)) {
Julien Boeuff47a5cb2015-02-18 12:24:08 -0800125 char *service_url =
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700126 build_service_url(channeld->security_connector->base.url_scheme, calld);
Julien Boeuf54b21922015-02-04 16:39:35 -0800127 calld->op = *op; /* Copy op (originates from the caller's stack). */
Julien Boeuff47a5cb2015-02-18 12:24:08 -0800128 grpc_credentials_get_request_metadata(channel_creds, service_url,
Julien Boeuf54b21922015-02-04 16:39:35 -0800129 on_credentials_metadata, elem);
Julien Boeuff47a5cb2015-02-18 12:24:08 -0800130 gpr_free(service_url);
Julien Boeuf54b21922015-02-04 16:39:35 -0800131 } else {
132 grpc_call_next_op(elem, op);
133 }
134}
135
136static void on_host_checked(void *user_data, grpc_security_status status) {
137 grpc_call_element *elem = (grpc_call_element *)user_data;
138 call_data *calld = elem->call_data;
139
140 if (status == GRPC_SECURITY_OK) {
141 send_security_metadata(elem, &calld->op);
142 } else {
143 char *error_msg;
144 gpr_asprintf(&error_msg, "Invalid host %s set in :authority metadata.",
145 grpc_mdstr_as_c_string(calld->host));
146 bubbleup_error(elem, error_msg);
Craig Tillereb40a532015-04-17 16:46:20 -0700147 grpc_metadata_batch_destroy(&calld->op.data.metadata);
Julien Boeuf54b21922015-02-04 16:39:35 -0800148 gpr_free(error_msg);
Craig Tillereb131bb2015-03-03 11:00:40 -0800149 calld->op.done_cb(calld->op.user_data, GRPC_OP_ERROR);
Julien Boeuf54b21922015-02-04 16:39:35 -0800150 }
151}
152
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800153/* Called either:
154 - in response to an API call (or similar) from above, to send something
155 - a network event (or similar) from below, to receive something
156 op contains type and call direction information, in addition to the data
157 that is being sent or received. */
ctillerf962f522014-12-10 15:28:27 -0800158static void call_op(grpc_call_element *elem, grpc_call_element *from_elem,
159 grpc_call_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800160 /* grab pointers to our data from the call element */
161 call_data *calld = elem->call_data;
162 channel_data *channeld = elem->channel_data;
Craig Tiller6902ad22015-04-16 08:01:49 -0700163 grpc_linked_mdelem *l;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800164
165 switch (op->type) {
Julien Boeuf54b21922015-02-04 16:39:35 -0800166 case GRPC_SEND_METADATA:
Craig Tillerb4a75a82015-04-21 13:40:09 -0700167 for (l = op->data.metadata.list.head; l != NULL; l = l->next) {
Craig Tiller6902ad22015-04-16 08:01:49 -0700168 grpc_mdelem *md = l->md;
Craig Tiller87d5b192015-04-16 14:37:57 -0700169 /* Pointer comparison is OK for md_elems created from the same context.
170 */
Craig Tiller6902ad22015-04-16 08:01:49 -0700171 if (md->key == channeld->authority_string) {
172 if (calld->host != NULL) grpc_mdstr_unref(calld->host);
173 calld->host = grpc_mdstr_ref(md->value);
174 } else if (md->key == channeld->path_string) {
175 if (calld->method != NULL) grpc_mdstr_unref(calld->method);
176 calld->method = grpc_mdstr_ref(md->value);
177 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800178 }
Julien Boeuf54b21922015-02-04 16:39:35 -0800179 if (calld->host != NULL) {
180 grpc_security_status status;
181 const char *call_host = grpc_mdstr_as_c_string(calld->host);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800182 calld->op = *op; /* Copy op (originates from the caller's stack). */
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700183 status = grpc_channel_security_connector_check_call_host(
184 channeld->security_connector, call_host, on_host_checked, elem);
Julien Boeuf54b21922015-02-04 16:39:35 -0800185 if (status != GRPC_SECURITY_OK) {
186 if (status == GRPC_SECURITY_ERROR) {
187 char *error_msg;
188 gpr_asprintf(&error_msg,
189 "Invalid host %s set in :authority metadata.",
190 call_host);
191 bubbleup_error(elem, error_msg);
Craig Tillereb40a532015-04-17 16:46:20 -0700192 grpc_metadata_batch_destroy(&calld->op.data.metadata);
Julien Boeuf54b21922015-02-04 16:39:35 -0800193 gpr_free(error_msg);
Craig Tillereb131bb2015-03-03 11:00:40 -0800194 op->done_cb(op->user_data, GRPC_OP_ERROR);
Julien Boeuf54b21922015-02-04 16:39:35 -0800195 }
196 break;
197 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800198 }
Julien Boeuf54b21922015-02-04 16:39:35 -0800199 send_security_metadata(elem, op);
200 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800201 default:
202 /* pass control up or down the stack depending on op->dir */
203 grpc_call_next_op(elem, op);
204 break;
205 }
206}
207
208/* Called on special channel events, such as disconnection or new incoming
209 calls on the server */
ctillerf962f522014-12-10 15:28:27 -0800210static void channel_op(grpc_channel_element *elem,
211 grpc_channel_element *from_elem, grpc_channel_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800212 grpc_channel_next_op(elem, op);
213}
214
215/* Constructor for call_data */
216static void init_call_elem(grpc_call_element *elem,
217 const void *server_transport_data) {
218 /* TODO(jboeuf):
219 Find a way to pass-in the credentials from the caller here. */
220 call_data *calld = elem->call_data;
221 calld->creds = NULL;
Julien Boeuf54b21922015-02-04 16:39:35 -0800222 calld->host = NULL;
Julien Boeuff47a5cb2015-02-18 12:24:08 -0800223 calld->method = NULL;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800224}
225
226/* Destructor for call_data */
227static void destroy_call_elem(grpc_call_element *elem) {
228 call_data *calld = elem->call_data;
229 if (calld->creds != NULL) {
230 grpc_credentials_unref(calld->creds);
231 }
Julien Boeuf54b21922015-02-04 16:39:35 -0800232 if (calld->host != NULL) {
233 grpc_mdstr_unref(calld->host);
234 }
Craig Tiller7d0f9ea2015-02-23 13:37:07 -0800235 if (calld->method != NULL) {
236 grpc_mdstr_unref(calld->method);
237 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800238}
239
240/* Constructor for channel_data */
241static void init_channel_elem(grpc_channel_element *elem,
242 const grpc_channel_args *args,
243 grpc_mdctx *metadata_context, int is_first,
244 int is_last) {
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700245 grpc_security_connector *ctx = grpc_find_security_connector_in_args(args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800246 /* grab pointers to our data from the channel element */
247 channel_data *channeld = elem->channel_data;
248
249 /* The first and the last filters tend to be implemented differently to
250 handle the case that there's no 'next' filter to call on the up or down
251 path */
252 GPR_ASSERT(!is_first);
253 GPR_ASSERT(!is_last);
254 GPR_ASSERT(ctx != NULL);
255
256 /* initialize members */
257 GPR_ASSERT(ctx->is_client_side);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700258 channeld->security_connector =
259 (grpc_channel_security_connector *)grpc_security_connector_ref(ctx);
Julien Boeuf54b21922015-02-04 16:39:35 -0800260 channeld->md_ctx = metadata_context;
261 channeld->authority_string =
262 grpc_mdstr_from_string(channeld->md_ctx, ":authority");
Julien Boeuff47a5cb2015-02-18 12:24:08 -0800263 channeld->path_string = grpc_mdstr_from_string(channeld->md_ctx, ":path");
Julien Boeuf54b21922015-02-04 16:39:35 -0800264 channeld->error_msg_key =
265 grpc_mdstr_from_string(channeld->md_ctx, "grpc-message");
Craig Tiller87d5b192015-04-16 14:37:57 -0700266 channeld->status_key =
267 grpc_mdstr_from_string(channeld->md_ctx, "grpc-status");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800268}
269
270/* Destructor for channel data */
271static void destroy_channel_elem(grpc_channel_element *elem) {
272 /* grab pointers to our data from the channel element */
273 channel_data *channeld = elem->channel_data;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700274 grpc_channel_security_connector *ctx = channeld->security_connector;
275 if (ctx != NULL) grpc_security_connector_unref(&ctx->base);
Julien Boeuf54b21922015-02-04 16:39:35 -0800276 if (channeld->authority_string != NULL) {
277 grpc_mdstr_unref(channeld->authority_string);
278 }
279 if (channeld->error_msg_key != NULL) {
280 grpc_mdstr_unref(channeld->error_msg_key);
281 }
Craig Tillereb131bb2015-03-03 11:00:40 -0800282 if (channeld->status_key != NULL) {
283 grpc_mdstr_unref(channeld->status_key);
284 }
Craig Tiller7d0f9ea2015-02-23 13:37:07 -0800285 if (channeld->path_string != NULL) {
286 grpc_mdstr_unref(channeld->path_string);
287 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800288}
289
290const grpc_channel_filter grpc_client_auth_filter = {
Craig Tiller87d5b192015-04-16 14:37:57 -0700291 call_op, channel_op, sizeof(call_data), init_call_elem, destroy_call_elem,
292 sizeof(channel_data), init_channel_elem, destroy_channel_elem, "auth"};