Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
| 34 | #include "src/core/security/auth.h" |
| 35 | |
| 36 | #include <string.h> |
| 37 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
| 39 | #include <grpc/support/log.h> |
| 40 | |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 41 | #include "src/core/support/string.h" |
| 42 | #include "src/core/channel/channel_stack.h" |
| 43 | #include "src/core/security/security_context.h" |
| 44 | #include "src/core/security/credentials.h" |
| 45 | #include "src/core/surface/call.h" |
| 46 | |
Craig Tiller | 9c9d4e0 | 2015-04-20 09:03:29 -0700 | [diff] [blame^] | 47 | #define MAX_CREDENTIALS_METADATA_COUNT 4 |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 48 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | /* We can have a per-call credentials. */ |
| 50 | typedef struct { |
| 51 | grpc_credentials *creds; |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 52 | grpc_mdstr *host; |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 53 | grpc_mdstr *method; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | grpc_call_op op; |
Craig Tiller | 9c9d4e0 | 2015-04-20 09:03:29 -0700 | [diff] [blame^] | 55 | grpc_linked_mdelem md_links[MAX_CREDENTIALS_METADATA_COUNT]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | } call_data; |
| 57 | |
| 58 | /* We can have a per-channel credentials. */ |
| 59 | typedef struct { |
| 60 | grpc_channel_security_context *security_context; |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 61 | grpc_mdctx *md_ctx; |
| 62 | grpc_mdstr *authority_string; |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 63 | grpc_mdstr *path_string; |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 64 | grpc_mdstr *error_msg_key; |
Craig Tiller | eb131bb | 2015-03-03 11:00:40 -0800 | [diff] [blame] | 65 | grpc_mdstr *status_key; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | } channel_data; |
| 67 | |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 68 | static void bubbleup_error(grpc_call_element *elem, const char *error_msg) { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 69 | grpc_call_element_recv_status(elem, GRPC_STATUS_UNAUTHENTICATED, error_msg); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 70 | grpc_call_element_send_cancel(elem); |
| 71 | } |
| 72 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 73 | static 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 Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 77 | call_data *calld = elem->call_data; |
| 78 | grpc_call_op op = calld->op; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 79 | size_t i; |
Craig Tiller | 9c9d4e0 | 2015-04-20 09:03:29 -0700 | [diff] [blame^] | 80 | GPR_ASSERT(num_md <= MAX_CREDENTIALS_METADATA_COUNT); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | for (i = 0; i < num_md; i++) { |
Craig Tiller | 205aee1 | 2015-04-16 14:46:41 -0700 | [diff] [blame] | 82 | grpc_metadata_batch_add_tail(&op.data.metadata, &calld->md_links[i], |
Craig Tiller | 76f5d46 | 2015-04-17 14:58:12 -0700 | [diff] [blame] | 83 | grpc_mdelem_ref(md_elems[i])); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | } |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 85 | grpc_call_next_op(elem, &op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 88 | static 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 Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 108 | static 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 = |
| 114 | channeld->security_context->request_metadata_creds; |
| 115 | /* 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 Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 125 | char *service_url = |
| 126 | build_service_url(channeld->security_context->base.url_scheme, calld); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 127 | calld->op = *op; /* Copy op (originates from the caller's stack). */ |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 128 | grpc_credentials_get_request_metadata(channel_creds, service_url, |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 129 | on_credentials_metadata, elem); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 130 | gpr_free(service_url); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 131 | } else { |
| 132 | grpc_call_next_op(elem, op); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | static 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 Tiller | eb40a53 | 2015-04-17 16:46:20 -0700 | [diff] [blame] | 147 | grpc_metadata_batch_destroy(&calld->op.data.metadata); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 148 | gpr_free(error_msg); |
Craig Tiller | eb131bb | 2015-03-03 11:00:40 -0800 | [diff] [blame] | 149 | calld->op.done_cb(calld->op.user_data, GRPC_OP_ERROR); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 153 | /* 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. */ |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 158 | static void call_op(grpc_call_element *elem, grpc_call_element *from_elem, |
| 159 | grpc_call_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 160 | /* grab pointers to our data from the call element */ |
| 161 | call_data *calld = elem->call_data; |
| 162 | channel_data *channeld = elem->channel_data; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 163 | grpc_linked_mdelem *l; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 164 | |
| 165 | switch (op->type) { |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 166 | case GRPC_SEND_METADATA: |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 167 | for (l = op->data.metadata.list.head; l; l = l->next) { |
| 168 | grpc_mdelem *md = l->md; |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 169 | /* Pointer comparison is OK for md_elems created from the same context. |
| 170 | */ |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 171 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 178 | } |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 179 | if (calld->host != NULL) { |
| 180 | grpc_security_status status; |
| 181 | const char *call_host = grpc_mdstr_as_c_string(calld->host); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 182 | calld->op = *op; /* Copy op (originates from the caller's stack). */ |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 183 | status = grpc_channel_security_context_check_call_host( |
| 184 | channeld->security_context, call_host, on_host_checked, elem); |
| 185 | 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 Tiller | eb40a53 | 2015-04-17 16:46:20 -0700 | [diff] [blame] | 192 | grpc_metadata_batch_destroy(&calld->op.data.metadata); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 193 | gpr_free(error_msg); |
Craig Tiller | eb131bb | 2015-03-03 11:00:40 -0800 | [diff] [blame] | 194 | op->done_cb(op->user_data, GRPC_OP_ERROR); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 195 | } |
| 196 | break; |
| 197 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 198 | } |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 199 | send_security_metadata(elem, op); |
| 200 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 201 | 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 */ |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 210 | static void channel_op(grpc_channel_element *elem, |
| 211 | grpc_channel_element *from_elem, grpc_channel_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 212 | grpc_channel_next_op(elem, op); |
| 213 | } |
| 214 | |
| 215 | /* Constructor for call_data */ |
| 216 | static 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 Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 222 | calld->host = NULL; |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 223 | calld->method = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* Destructor for call_data */ |
| 227 | static 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 Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 232 | if (calld->host != NULL) { |
| 233 | grpc_mdstr_unref(calld->host); |
| 234 | } |
Craig Tiller | 7d0f9ea | 2015-02-23 13:37:07 -0800 | [diff] [blame] | 235 | if (calld->method != NULL) { |
| 236 | grpc_mdstr_unref(calld->method); |
| 237 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* Constructor for channel_data */ |
| 241 | static 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) { |
| 245 | grpc_security_context *ctx = grpc_find_security_context_in_args(args); |
| 246 | /* 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); |
| 258 | channeld->security_context = |
| 259 | (grpc_channel_security_context *)grpc_security_context_ref(ctx); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 260 | channeld->md_ctx = metadata_context; |
| 261 | channeld->authority_string = |
| 262 | grpc_mdstr_from_string(channeld->md_ctx, ":authority"); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 263 | channeld->path_string = grpc_mdstr_from_string(channeld->md_ctx, ":path"); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 264 | channeld->error_msg_key = |
| 265 | grpc_mdstr_from_string(channeld->md_ctx, "grpc-message"); |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 266 | channeld->status_key = |
| 267 | grpc_mdstr_from_string(channeld->md_ctx, "grpc-status"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* Destructor for channel data */ |
| 271 | static 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; |
| 274 | grpc_channel_security_context *ctx = channeld->security_context; |
| 275 | if (ctx != NULL) grpc_security_context_unref(&ctx->base); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 276 | 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 Tiller | eb131bb | 2015-03-03 11:00:40 -0800 | [diff] [blame] | 282 | if (channeld->status_key != NULL) { |
| 283 | grpc_mdstr_unref(channeld->status_key); |
| 284 | } |
Craig Tiller | 7d0f9ea | 2015-02-23 13:37:07 -0800 | [diff] [blame] | 285 | if (channeld->path_string != NULL) { |
| 286 | grpc_mdstr_unref(channeld->path_string); |
| 287 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | const grpc_channel_filter grpc_client_auth_filter = { |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 291 | call_op, channel_op, sizeof(call_data), init_call_elem, destroy_call_elem, |
| 292 | sizeof(channel_data), init_channel_elem, destroy_channel_elem, "auth"}; |