Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | c506e24 | 2016-01-04 15:59:29 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [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 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_H |
| 35 | #define GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_H |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 36 | |
| 37 | #include <grpc/grpc_security.h> |
| 38 | #include "src/core/iomgr/endpoint.h" |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 39 | #include "src/core/iomgr/tcp_server.h" |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 40 | #include "src/core/tsi/transport_security_interface.h" |
| 41 | |
| 42 | /* --- status enum. --- */ |
| 43 | |
Craig Tiller | be52c6e | 2016-01-04 15:35:26 -0800 | [diff] [blame] | 44 | typedef enum { GRPC_SECURITY_OK = 0, GRPC_SECURITY_ERROR } grpc_security_status; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 45 | |
| 46 | /* --- URL schemes. --- */ |
| 47 | |
| 48 | #define GRPC_SSL_URL_SCHEME "https" |
| 49 | #define GRPC_FAKE_SECURITY_URL_SCHEME "http+fake_security" |
| 50 | |
| 51 | /* --- security_connector object. --- |
| 52 | |
| 53 | A security connector object represents away to configure the underlying |
| 54 | transport security mechanism and check the resulting trusted peer. */ |
| 55 | |
| 56 | typedef struct grpc_security_connector grpc_security_connector; |
| 57 | |
| 58 | #define GRPC_SECURITY_CONNECTOR_ARG "grpc.security_connector" |
| 59 | |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 60 | typedef void (*grpc_security_peer_check_cb)(grpc_exec_ctx *exec_ctx, |
| 61 | void *user_data, |
| 62 | grpc_security_status status, |
| 63 | grpc_auth_context *auth_context); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 64 | |
Julien Boeuf | 87047d7 | 2015-08-21 14:30:33 -0700 | [diff] [blame] | 65 | /* Ownership of the secure_endpoint is transfered. */ |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 66 | typedef void (*grpc_security_handshake_done_cb)( |
| 67 | grpc_exec_ctx *exec_ctx, void *user_data, grpc_security_status status, |
| 68 | grpc_endpoint *secure_endpoint, grpc_auth_context *auth_context); |
Julien Boeuf | 87047d7 | 2015-08-21 14:30:33 -0700 | [diff] [blame] | 69 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 70 | typedef struct { |
| 71 | void (*destroy)(grpc_security_connector *sc); |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 72 | void (*check_peer)(grpc_exec_ctx *exec_ctx, grpc_security_connector *sc, |
| 73 | tsi_peer peer, grpc_security_peer_check_cb cb, |
| 74 | void *user_data); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 75 | } grpc_security_connector_vtable; |
| 76 | |
yang-g | 5e72a35 | 2015-11-20 09:49:36 -0800 | [diff] [blame] | 77 | typedef struct grpc_security_connector_handshake_list { |
| 78 | void *handshake; |
| 79 | struct grpc_security_connector_handshake_list *next; |
| 80 | } grpc_security_connector_handshake_list; |
| 81 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | struct grpc_security_connector { |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 83 | const grpc_security_connector_vtable *vtable; |
| 84 | gpr_refcount refcount; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 85 | const char *url_scheme; |
| 86 | }; |
| 87 | |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 88 | /* Refcounting. */ |
| 89 | #ifdef GRPC_SECURITY_CONNECTOR_REFCOUNT_DEBUG |
| 90 | #define GRPC_SECURITY_CONNECTOR_REF(p, r) \ |
| 91 | grpc_security_connector_ref((p), __FILE__, __LINE__, (r)) |
| 92 | #define GRPC_SECURITY_CONNECTOR_UNREF(p, r) \ |
| 93 | grpc_security_connector_unref((p), __FILE__, __LINE__, (r)) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 94 | grpc_security_connector *grpc_security_connector_ref( |
| 95 | grpc_security_connector *policy, const char *file, int line, |
| 96 | const char *reason); |
| 97 | void grpc_security_connector_unref(grpc_security_connector *policy, |
| 98 | const char *file, int line, |
| 99 | const char *reason); |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 100 | #else |
| 101 | #define GRPC_SECURITY_CONNECTOR_REF(p, r) grpc_security_connector_ref((p)) |
| 102 | #define GRPC_SECURITY_CONNECTOR_UNREF(p, r) grpc_security_connector_unref((p)) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 103 | grpc_security_connector *grpc_security_connector_ref( |
| 104 | grpc_security_connector *policy); |
| 105 | void grpc_security_connector_unref(grpc_security_connector *policy); |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 106 | #endif |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 107 | |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 108 | /* Check the peer. Callee takes ownership of the peer object. |
| 109 | The callback will include the resulting auth_context. */ |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 110 | void grpc_security_connector_check_peer(grpc_exec_ctx *exec_ctx, |
| 111 | grpc_security_connector *sc, |
| 112 | tsi_peer peer, |
| 113 | grpc_security_peer_check_cb cb, |
| 114 | void *user_data); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 115 | |
| 116 | /* Util to encapsulate the connector in a channel arg. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 117 | grpc_arg grpc_security_connector_to_arg(grpc_security_connector *sc); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 118 | |
| 119 | /* Util to get the connector from a channel arg. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 120 | grpc_security_connector *grpc_security_connector_from_arg(const grpc_arg *arg); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 121 | |
| 122 | /* Util to find the connector from channel args. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | grpc_security_connector *grpc_find_security_connector_in_args( |
| 124 | const grpc_channel_args *args); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 125 | |
| 126 | /* --- channel_security_connector object. --- |
| 127 | |
| 128 | A channel security connector object represents away to configure the |
| 129 | underlying transport security mechanism on the client side. */ |
| 130 | |
| 131 | typedef struct grpc_channel_security_connector grpc_channel_security_connector; |
| 132 | |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 133 | typedef void (*grpc_security_call_host_check_cb)(grpc_exec_ctx *exec_ctx, |
| 134 | void *user_data, |
| 135 | grpc_security_status status); |
| 136 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 137 | struct grpc_channel_security_connector { |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 138 | grpc_security_connector base; |
Julien Boeuf | 441176d | 2015-10-09 21:14:07 -0700 | [diff] [blame] | 139 | grpc_call_credentials *request_metadata_creds; |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 140 | void (*check_call_host)(grpc_exec_ctx *exec_ctx, |
| 141 | grpc_channel_security_connector *sc, const char *host, |
| 142 | grpc_auth_context *auth_context, |
| 143 | grpc_security_call_host_check_cb cb, void *user_data); |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 144 | void (*do_handshake)(grpc_exec_ctx *exec_ctx, |
| 145 | grpc_channel_security_connector *sc, |
| 146 | grpc_endpoint *nonsecure_endpoint, |
| 147 | grpc_security_handshake_done_cb cb, void *user_data); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 150 | /* Checks that the host that will be set for a call is acceptable. */ |
| 151 | void grpc_channel_security_connector_check_call_host( |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 152 | grpc_exec_ctx *exec_ctx, grpc_channel_security_connector *sc, |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 153 | const char *host, grpc_auth_context *auth_context, |
| 154 | grpc_security_call_host_check_cb cb, void *user_data); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 155 | |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 156 | /* Handshake. */ |
| 157 | void grpc_channel_security_connector_do_handshake( |
| 158 | grpc_exec_ctx *exec_ctx, grpc_channel_security_connector *connector, |
| 159 | grpc_endpoint *nonsecure_endpoint, grpc_security_handshake_done_cb cb, |
| 160 | void *user_data); |
| 161 | |
| 162 | /* --- server_security_connector object. --- |
| 163 | |
| 164 | A server security connector object represents away to configure the |
| 165 | underlying transport security mechanism on the server side. */ |
| 166 | |
| 167 | typedef struct grpc_server_security_connector grpc_server_security_connector; |
| 168 | |
| 169 | struct grpc_server_security_connector { |
| 170 | grpc_security_connector base; |
| 171 | gpr_mu mu; |
| 172 | grpc_security_connector_handshake_list *handshaking_handshakes; |
| 173 | const grpc_channel_args *channel_args; |
| 174 | void (*do_handshake)(grpc_exec_ctx *exec_ctx, |
| 175 | grpc_server_security_connector *sc, |
| 176 | grpc_tcp_server_acceptor *acceptor, |
| 177 | grpc_endpoint *nonsecure_endpoint, |
| 178 | grpc_security_handshake_done_cb cb, void *user_data); |
| 179 | }; |
| 180 | |
| 181 | void grpc_server_security_connector_do_handshake( |
| 182 | grpc_exec_ctx *exec_ctx, grpc_server_security_connector *sc, |
| 183 | grpc_tcp_server_acceptor *acceptor, grpc_endpoint *nonsecure_endpoint, |
| 184 | grpc_security_handshake_done_cb cb, void *user_data); |
| 185 | |
| 186 | void grpc_server_security_connector_shutdown( |
| 187 | grpc_exec_ctx *exec_ctx, grpc_server_security_connector *connector); |
| 188 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 189 | /* --- Creation security connectors. --- */ |
| 190 | |
| 191 | /* For TESTING ONLY! |
| 192 | Creates a fake connector that emulates real channel security. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | grpc_channel_security_connector *grpc_fake_channel_security_connector_create( |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 194 | grpc_call_credentials *request_metadata_creds); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 195 | |
| 196 | /* For TESTING ONLY! |
| 197 | Creates a fake connector that emulates real server security. */ |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 198 | grpc_server_security_connector *grpc_fake_server_security_connector_create( |
| 199 | void); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 200 | |
| 201 | /* Config for ssl clients. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 202 | typedef struct { |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 203 | unsigned char *pem_private_key; |
| 204 | size_t pem_private_key_size; |
| 205 | unsigned char *pem_cert_chain; |
| 206 | size_t pem_cert_chain_size; |
| 207 | unsigned char *pem_root_certs; |
| 208 | size_t pem_root_certs_size; |
| 209 | } grpc_ssl_config; |
| 210 | |
| 211 | /* Creates an SSL channel_security_connector. |
| 212 | - request_metadata_creds is the credentials object which metadata |
| 213 | will be sent with each request. This parameter can be NULL. |
| 214 | - config is the SSL config to be used for the SSL channel establishment. |
| 215 | - is_client should be 0 for a server or a non-0 value for a client. |
| 216 | - secure_peer_name is the secure peer name that should be checked in |
| 217 | grpc_channel_security_connector_check_peer. This parameter may be NULL in |
| 218 | which case the peer name will not be checked. Note that if this parameter |
| 219 | is not NULL, then, pem_root_certs should not be NULL either. |
| 220 | - sc is a pointer on the connector to be created. |
| 221 | This function returns GRPC_SECURITY_OK in case of success or a |
| 222 | specific error code otherwise. |
| 223 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 224 | grpc_security_status grpc_ssl_channel_security_connector_create( |
Julien Boeuf | 441176d | 2015-10-09 21:14:07 -0700 | [diff] [blame] | 225 | grpc_call_credentials *request_metadata_creds, |
| 226 | const grpc_ssl_config *config, const char *target_name, |
| 227 | const char *overridden_target_name, grpc_channel_security_connector **sc); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 228 | |
| 229 | /* Gets the default ssl roots. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 230 | size_t grpc_get_default_ssl_roots(const unsigned char **pem_root_certs); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 231 | |
Julien Boeuf | 373debd | 2016-01-27 15:41:12 -0800 | [diff] [blame] | 232 | /* Exposed for TESTING ONLY!. */ |
| 233 | gpr_slice grpc_get_default_ssl_roots_for_testing(void); |
| 234 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 235 | /* Config for ssl servers. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 236 | typedef struct { |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 237 | unsigned char **pem_private_keys; |
| 238 | size_t *pem_private_keys_sizes; |
| 239 | unsigned char **pem_cert_chains; |
| 240 | size_t *pem_cert_chains_sizes; |
| 241 | size_t num_key_cert_pairs; |
| 242 | unsigned char *pem_root_certs; |
| 243 | size_t pem_root_certs_size; |
Julien Boeuf | 5029b30 | 2015-07-21 23:02:16 -0700 | [diff] [blame] | 244 | int force_client_auth; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 245 | } grpc_ssl_server_config; |
| 246 | |
| 247 | /* Creates an SSL server_security_connector. |
| 248 | - config is the SSL config to be used for the SSL channel establishment. |
| 249 | - sc is a pointer on the connector to be created. |
| 250 | This function returns GRPC_SECURITY_OK in case of success or a |
| 251 | specific error code otherwise. |
| 252 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 253 | grpc_security_status grpc_ssl_server_security_connector_create( |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 254 | const grpc_ssl_server_config *config, grpc_server_security_connector **sc); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 255 | |
Julien Boeuf | cf4124e | 2015-05-18 15:08:50 -0700 | [diff] [blame] | 256 | /* Util. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 257 | const tsi_peer_property *tsi_peer_get_property_by_name(const tsi_peer *peer, |
| 258 | const char *name); |
Julien Boeuf | cf4124e | 2015-05-18 15:08:50 -0700 | [diff] [blame] | 259 | |
Julien Boeuf | a701ade | 2015-06-18 15:23:40 +0200 | [diff] [blame] | 260 | /* Exposed for testing only. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 261 | grpc_auth_context *tsi_ssl_peer_to_auth_context(const tsi_peer *peer); |
Julien Boeuf | ee9d78b | 2015-12-18 09:50:34 -0800 | [diff] [blame] | 262 | tsi_peer tsi_shallow_peer_from_ssl_auth_context( |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 263 | const grpc_auth_context *auth_context); |
| 264 | void tsi_shallow_peer_destruct(tsi_peer *peer); |
Julien Boeuf | a701ade | 2015-06-18 15:23:40 +0200 | [diff] [blame] | 265 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 266 | #endif /* GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_H */ |