Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015-2016 gRPC authors. |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "src/core/lib/security/credentials/composite/composite_credentials.h" |
| 20 | |
| 21 | #include <string.h> |
| 22 | |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 23 | #include "src/core/lib/iomgr/polling_entity.h" |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 24 | #include "src/core/lib/surface/api_trace.h" |
| 25 | |
| 26 | #include <grpc/support/alloc.h> |
| 27 | #include <grpc/support/log.h> |
| 28 | #include <grpc/support/string_util.h> |
| 29 | |
| 30 | /* -- Composite call credentials. -- */ |
| 31 | |
| 32 | typedef struct { |
| 33 | grpc_composite_call_credentials *composite_creds; |
| 34 | size_t creds_index; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 35 | grpc_polling_entity *pollent; |
Mark D. Roth | e0778b2 | 2017-07-21 15:42:00 -0700 | [diff] [blame] | 36 | grpc_auth_metadata_context auth_md_context; |
| 37 | grpc_credentials_mdelem_array *md_array; |
| 38 | grpc_closure *on_request_metadata; |
| 39 | grpc_closure internal_on_request_metadata; |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 40 | } grpc_composite_call_credentials_metadata_context; |
| 41 | |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 42 | static void composite_call_destruct(grpc_exec_ctx *exec_ctx, |
| 43 | grpc_call_credentials *creds) { |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 44 | grpc_composite_call_credentials *c = (grpc_composite_call_credentials *)creds; |
Mark D. Roth | e0778b2 | 2017-07-21 15:42:00 -0700 | [diff] [blame] | 45 | for (size_t i = 0; i < c->inner.num_creds; i++) { |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 46 | grpc_call_credentials_unref(exec_ctx, c->inner.creds_array[i]); |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 47 | } |
| 48 | gpr_free(c->inner.creds_array); |
| 49 | } |
| 50 | |
Mark D. Roth | e0778b2 | 2017-07-21 15:42:00 -0700 | [diff] [blame] | 51 | static void composite_call_metadata_cb(grpc_exec_ctx *exec_ctx, void *arg, |
| 52 | grpc_error *error) { |
| 53 | grpc_composite_call_credentials_metadata_context *ctx = |
| 54 | (grpc_composite_call_credentials_metadata_context *)arg; |
| 55 | if (error == GRPC_ERROR_NONE) { |
| 56 | /* See if we need to get some more metadata. */ |
| 57 | if (ctx->creds_index < ctx->composite_creds->inner.num_creds) { |
| 58 | grpc_call_credentials *inner_creds = |
| 59 | ctx->composite_creds->inner.creds_array[ctx->creds_index++]; |
| 60 | if (grpc_call_credentials_get_request_metadata( |
| 61 | exec_ctx, inner_creds, ctx->pollent, ctx->auth_md_context, |
| 62 | ctx->md_array, &ctx->internal_on_request_metadata, &error)) { |
| 63 | // Synchronous response, so call ourselves recursively. |
| 64 | composite_call_metadata_cb(exec_ctx, arg, error); |
| 65 | GRPC_ERROR_UNREF(error); |
| 66 | } |
| 67 | return; |
| 68 | } |
| 69 | // We're done! |
| 70 | } |
| 71 | GRPC_CLOSURE_SCHED(exec_ctx, ctx->on_request_metadata, GRPC_ERROR_REF(error)); |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 72 | gpr_free(ctx); |
| 73 | } |
| 74 | |
Mark D. Roth | e0778b2 | 2017-07-21 15:42:00 -0700 | [diff] [blame] | 75 | static bool composite_call_get_request_metadata( |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 76 | grpc_exec_ctx *exec_ctx, grpc_call_credentials *creds, |
| 77 | grpc_polling_entity *pollent, grpc_auth_metadata_context auth_md_context, |
Mark D. Roth | e0778b2 | 2017-07-21 15:42:00 -0700 | [diff] [blame] | 78 | grpc_credentials_mdelem_array *md_array, grpc_closure *on_request_metadata, |
| 79 | grpc_error **error) { |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 80 | grpc_composite_call_credentials *c = (grpc_composite_call_credentials *)creds; |
| 81 | grpc_composite_call_credentials_metadata_context *ctx; |
Yash Tibrewal | acd46e5 | 2017-09-20 11:28:25 -0700 | [diff] [blame^] | 82 | ctx = (grpc_composite_call_credentials_metadata_context *)gpr_zalloc( |
| 83 | sizeof(grpc_composite_call_credentials_metadata_context)); |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 84 | ctx->composite_creds = c; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 85 | ctx->pollent = pollent; |
Mark D. Roth | e0778b2 | 2017-07-21 15:42:00 -0700 | [diff] [blame] | 86 | ctx->auth_md_context = auth_md_context; |
| 87 | ctx->md_array = md_array; |
| 88 | ctx->on_request_metadata = on_request_metadata; |
| 89 | GRPC_CLOSURE_INIT(&ctx->internal_on_request_metadata, |
| 90 | composite_call_metadata_cb, ctx, grpc_schedule_on_exec_ctx); |
Mark D. Roth | 55a807a | 2017-09-28 08:33:51 -0700 | [diff] [blame] | 91 | bool synchronous = true; |
Mark D. Roth | e0778b2 | 2017-07-21 15:42:00 -0700 | [diff] [blame] | 92 | while (ctx->creds_index < ctx->composite_creds->inner.num_creds) { |
| 93 | grpc_call_credentials *inner_creds = |
| 94 | ctx->composite_creds->inner.creds_array[ctx->creds_index++]; |
| 95 | if (grpc_call_credentials_get_request_metadata( |
| 96 | exec_ctx, inner_creds, ctx->pollent, ctx->auth_md_context, |
| 97 | ctx->md_array, &ctx->internal_on_request_metadata, error)) { |
| 98 | if (*error != GRPC_ERROR_NONE) break; |
| 99 | } else { |
Mark D. Roth | 55a807a | 2017-09-28 08:33:51 -0700 | [diff] [blame] | 100 | synchronous = false; // Async return. |
Mark D. Roth | e0778b2 | 2017-07-21 15:42:00 -0700 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | } |
Mark D. Roth | 55a807a | 2017-09-28 08:33:51 -0700 | [diff] [blame] | 104 | if (synchronous) gpr_free(ctx); |
| 105 | return synchronous; |
Mark D. Roth | e0778b2 | 2017-07-21 15:42:00 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void composite_call_cancel_get_request_metadata( |
| 109 | grpc_exec_ctx *exec_ctx, grpc_call_credentials *creds, |
| 110 | grpc_credentials_mdelem_array *md_array, grpc_error *error) { |
| 111 | grpc_composite_call_credentials *c = (grpc_composite_call_credentials *)creds; |
| 112 | for (size_t i = 0; i < c->inner.num_creds; ++i) { |
| 113 | grpc_call_credentials_cancel_get_request_metadata( |
| 114 | exec_ctx, c->inner.creds_array[i], md_array, GRPC_ERROR_REF(error)); |
| 115 | } |
| 116 | GRPC_ERROR_UNREF(error); |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static grpc_call_credentials_vtable composite_call_credentials_vtable = { |
Mark D. Roth | e0778b2 | 2017-07-21 15:42:00 -0700 | [diff] [blame] | 120 | composite_call_destruct, composite_call_get_request_metadata, |
| 121 | composite_call_cancel_get_request_metadata}; |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 122 | |
| 123 | static grpc_call_credentials_array get_creds_array( |
| 124 | grpc_call_credentials **creds_addr) { |
| 125 | grpc_call_credentials_array result; |
| 126 | grpc_call_credentials *creds = *creds_addr; |
| 127 | result.creds_array = creds_addr; |
| 128 | result.num_creds = 1; |
| 129 | if (strcmp(creds->type, GRPC_CALL_CREDENTIALS_TYPE_COMPOSITE) == 0) { |
| 130 | result = *grpc_composite_call_credentials_get_credentials(creds); |
| 131 | } |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | grpc_call_credentials *grpc_composite_call_credentials_create( |
| 136 | grpc_call_credentials *creds1, grpc_call_credentials *creds2, |
| 137 | void *reserved) { |
| 138 | size_t i; |
| 139 | size_t creds_array_byte_size; |
| 140 | grpc_call_credentials_array creds1_array; |
| 141 | grpc_call_credentials_array creds2_array; |
| 142 | grpc_composite_call_credentials *c; |
| 143 | GRPC_API_TRACE( |
| 144 | "grpc_composite_call_credentials_create(creds1=%p, creds2=%p, " |
| 145 | "reserved=%p)", |
| 146 | 3, (creds1, creds2, reserved)); |
| 147 | GPR_ASSERT(reserved == NULL); |
| 148 | GPR_ASSERT(creds1 != NULL); |
| 149 | GPR_ASSERT(creds2 != NULL); |
Yash Tibrewal | acd46e5 | 2017-09-20 11:28:25 -0700 | [diff] [blame^] | 150 | c = (grpc_composite_call_credentials *)gpr_zalloc( |
| 151 | sizeof(grpc_composite_call_credentials)); |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 152 | c->base.type = GRPC_CALL_CREDENTIALS_TYPE_COMPOSITE; |
| 153 | c->base.vtable = &composite_call_credentials_vtable; |
| 154 | gpr_ref_init(&c->base.refcount, 1); |
| 155 | creds1_array = get_creds_array(&creds1); |
| 156 | creds2_array = get_creds_array(&creds2); |
| 157 | c->inner.num_creds = creds1_array.num_creds + creds2_array.num_creds; |
| 158 | creds_array_byte_size = c->inner.num_creds * sizeof(grpc_call_credentials *); |
Yash Tibrewal | acd46e5 | 2017-09-20 11:28:25 -0700 | [diff] [blame^] | 159 | c->inner.creds_array = |
| 160 | (grpc_call_credentials **)gpr_zalloc(creds_array_byte_size); |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 161 | for (i = 0; i < creds1_array.num_creds; i++) { |
| 162 | grpc_call_credentials *cur_creds = creds1_array.creds_array[i]; |
| 163 | c->inner.creds_array[i] = grpc_call_credentials_ref(cur_creds); |
| 164 | } |
| 165 | for (i = 0; i < creds2_array.num_creds; i++) { |
| 166 | grpc_call_credentials *cur_creds = creds2_array.creds_array[i]; |
| 167 | c->inner.creds_array[i + creds1_array.num_creds] = |
| 168 | grpc_call_credentials_ref(cur_creds); |
| 169 | } |
| 170 | return &c->base; |
| 171 | } |
| 172 | |
| 173 | const grpc_call_credentials_array * |
| 174 | grpc_composite_call_credentials_get_credentials(grpc_call_credentials *creds) { |
| 175 | const grpc_composite_call_credentials *c = |
| 176 | (const grpc_composite_call_credentials *)creds; |
| 177 | GPR_ASSERT(strcmp(creds->type, GRPC_CALL_CREDENTIALS_TYPE_COMPOSITE) == 0); |
| 178 | return &c->inner; |
| 179 | } |
| 180 | |
| 181 | grpc_call_credentials *grpc_credentials_contains_type( |
| 182 | grpc_call_credentials *creds, const char *type, |
| 183 | grpc_call_credentials **composite_creds) { |
| 184 | size_t i; |
| 185 | if (strcmp(creds->type, type) == 0) { |
| 186 | if (composite_creds != NULL) *composite_creds = NULL; |
| 187 | return creds; |
| 188 | } else if (strcmp(creds->type, GRPC_CALL_CREDENTIALS_TYPE_COMPOSITE) == 0) { |
| 189 | const grpc_call_credentials_array *inner_creds_array = |
| 190 | grpc_composite_call_credentials_get_credentials(creds); |
| 191 | for (i = 0; i < inner_creds_array->num_creds; i++) { |
| 192 | if (strcmp(type, inner_creds_array->creds_array[i]->type) == 0) { |
| 193 | if (composite_creds != NULL) *composite_creds = creds; |
| 194 | return inner_creds_array->creds_array[i]; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | /* -- Composite channel credentials. -- */ |
| 202 | |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 203 | static void composite_channel_destruct(grpc_exec_ctx *exec_ctx, |
| 204 | grpc_channel_credentials *creds) { |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 205 | grpc_composite_channel_credentials *c = |
| 206 | (grpc_composite_channel_credentials *)creds; |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 207 | grpc_channel_credentials_unref(exec_ctx, c->inner_creds); |
| 208 | grpc_call_credentials_unref(exec_ctx, c->call_creds); |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static grpc_security_status composite_channel_create_security_connector( |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 212 | grpc_exec_ctx *exec_ctx, grpc_channel_credentials *creds, |
| 213 | grpc_call_credentials *call_creds, const char *target, |
| 214 | const grpc_channel_args *args, grpc_channel_security_connector **sc, |
| 215 | grpc_channel_args **new_args) { |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 216 | grpc_composite_channel_credentials *c = |
| 217 | (grpc_composite_channel_credentials *)creds; |
| 218 | grpc_security_status status = GRPC_SECURITY_ERROR; |
| 219 | |
| 220 | GPR_ASSERT(c->inner_creds != NULL && c->call_creds != NULL && |
| 221 | c->inner_creds->vtable != NULL && |
| 222 | c->inner_creds->vtable->create_security_connector != NULL); |
| 223 | /* If we are passed a call_creds, create a call composite to pass it |
| 224 | downstream. */ |
| 225 | if (call_creds != NULL) { |
| 226 | grpc_call_credentials *composite_call_creds = |
| 227 | grpc_composite_call_credentials_create(c->call_creds, call_creds, NULL); |
| 228 | status = c->inner_creds->vtable->create_security_connector( |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 229 | exec_ctx, c->inner_creds, composite_call_creds, target, args, sc, |
| 230 | new_args); |
| 231 | grpc_call_credentials_unref(exec_ctx, composite_call_creds); |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 232 | } else { |
| 233 | status = c->inner_creds->vtable->create_security_connector( |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 234 | exec_ctx, c->inner_creds, c->call_creds, target, args, sc, new_args); |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 235 | } |
| 236 | return status; |
| 237 | } |
| 238 | |
Julien Boeuf | e26ab6c | 2016-09-22 15:13:07 -0700 | [diff] [blame] | 239 | static grpc_channel_credentials * |
| 240 | composite_channel_duplicate_without_call_credentials( |
| 241 | grpc_channel_credentials *creds) { |
| 242 | grpc_composite_channel_credentials *c = |
| 243 | (grpc_composite_channel_credentials *)creds; |
| 244 | return grpc_channel_credentials_ref(c->inner_creds); |
| 245 | } |
| 246 | |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 247 | static grpc_channel_credentials_vtable composite_channel_credentials_vtable = { |
Julien Boeuf | e26ab6c | 2016-09-22 15:13:07 -0700 | [diff] [blame] | 248 | composite_channel_destruct, composite_channel_create_security_connector, |
| 249 | composite_channel_duplicate_without_call_credentials}; |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 250 | |
| 251 | grpc_channel_credentials *grpc_composite_channel_credentials_create( |
| 252 | grpc_channel_credentials *channel_creds, grpc_call_credentials *call_creds, |
| 253 | void *reserved) { |
Yash Tibrewal | acd46e5 | 2017-09-20 11:28:25 -0700 | [diff] [blame^] | 254 | grpc_composite_channel_credentials *c = |
| 255 | (grpc_composite_channel_credentials *)gpr_zalloc(sizeof(*c)); |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 256 | GPR_ASSERT(channel_creds != NULL && call_creds != NULL && reserved == NULL); |
| 257 | GRPC_API_TRACE( |
| 258 | "grpc_composite_channel_credentials_create(channel_creds=%p, " |
| 259 | "call_creds=%p, reserved=%p)", |
| 260 | 3, (channel_creds, call_creds, reserved)); |
| 261 | c->base.type = channel_creds->type; |
| 262 | c->base.vtable = &composite_channel_credentials_vtable; |
| 263 | gpr_ref_init(&c->base.refcount, 1); |
| 264 | c->inner_creds = grpc_channel_credentials_ref(channel_creds); |
| 265 | c->call_creds = grpc_call_credentials_ref(call_creds); |
| 266 | return &c->base; |
| 267 | } |