blob: 2818299235ce4bed4ed66b965881385afcd2c7a3 [file] [log] [blame]
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -07001/*
2 *
Craig Tillerc506e242016-01-04 15:59:29 -08003 * Copyright 2015-2016, Google Inc.
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -07004 * 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 Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_H
35#define GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_H
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070036
37#include <grpc/grpc_security.h>
38#include "src/core/iomgr/endpoint.h"
Julien Boeuf4f4d37c2016-02-24 22:07:36 -080039#include "src/core/iomgr/tcp_server.h"
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070040#include "src/core/tsi/transport_security_interface.h"
41
42/* --- status enum. --- */
43
Craig Tillerbe52c6e2016-01-04 15:35:26 -080044typedef enum { GRPC_SECURITY_OK = 0, GRPC_SECURITY_ERROR } grpc_security_status;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070045
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
56typedef struct grpc_security_connector grpc_security_connector;
57
58#define GRPC_SECURITY_CONNECTOR_ARG "grpc.security_connector"
59
Julien Boeuf366f42c2015-12-16 22:05:46 -080060typedef 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 Boeuf7d1d9ca2015-04-17 14:38:48 -070064
Julien Boeuf87047d72015-08-21 14:30:33 -070065/* Ownership of the secure_endpoint is transfered. */
Julien Boeuf366f42c2015-12-16 22:05:46 -080066typedef 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 Boeuf87047d72015-08-21 14:30:33 -070069
Craig Tillera82950e2015-09-22 12:33:20 -070070typedef struct {
71 void (*destroy)(grpc_security_connector *sc);
Julien Boeuf366f42c2015-12-16 22:05:46 -080072 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 Boeuf7d1d9ca2015-04-17 14:38:48 -070075} grpc_security_connector_vtable;
76
yang-g5e72a352015-11-20 09:49:36 -080077typedef 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 Tillera82950e2015-09-22 12:33:20 -070082struct grpc_security_connector {
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070083 const grpc_security_connector_vtable *vtable;
84 gpr_refcount refcount;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070085 const char *url_scheme;
86};
87
Craig Tiller5d44c062015-07-01 08:55:28 -070088/* 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 Tillera82950e2015-09-22 12:33:20 -070094grpc_security_connector *grpc_security_connector_ref(
95 grpc_security_connector *policy, const char *file, int line,
96 const char *reason);
97void grpc_security_connector_unref(grpc_security_connector *policy,
98 const char *file, int line,
99 const char *reason);
Craig Tiller5d44c062015-07-01 08:55:28 -0700100#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 Tillera82950e2015-09-22 12:33:20 -0700103grpc_security_connector *grpc_security_connector_ref(
104 grpc_security_connector *policy);
105void grpc_security_connector_unref(grpc_security_connector *policy);
Craig Tiller5d44c062015-07-01 08:55:28 -0700106#endif
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700107
Julien Boeuf1d9ac662015-12-17 21:35:47 -0800108/* Check the peer. Callee takes ownership of the peer object.
109 The callback will include the resulting auth_context. */
Julien Boeuf366f42c2015-12-16 22:05:46 -0800110void 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 Boeuf7d1d9ca2015-04-17 14:38:48 -0700115
116/* Util to encapsulate the connector in a channel arg. */
Craig Tillera82950e2015-09-22 12:33:20 -0700117grpc_arg grpc_security_connector_to_arg(grpc_security_connector *sc);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700118
119/* Util to get the connector from a channel arg. */
Craig Tillera82950e2015-09-22 12:33:20 -0700120grpc_security_connector *grpc_security_connector_from_arg(const grpc_arg *arg);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700121
122/* Util to find the connector from channel args. */
Craig Tillera82950e2015-09-22 12:33:20 -0700123grpc_security_connector *grpc_find_security_connector_in_args(
124 const grpc_channel_args *args);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700125
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
131typedef struct grpc_channel_security_connector grpc_channel_security_connector;
132
Julien Boeuf366f42c2015-12-16 22:05:46 -0800133typedef void (*grpc_security_call_host_check_cb)(grpc_exec_ctx *exec_ctx,
134 void *user_data,
135 grpc_security_status status);
136
Craig Tillera82950e2015-09-22 12:33:20 -0700137struct grpc_channel_security_connector {
Julien Boeuf4f4d37c2016-02-24 22:07:36 -0800138 grpc_security_connector base;
Julien Boeuf441176d2015-10-09 21:14:07 -0700139 grpc_call_credentials *request_metadata_creds;
Julien Boeuf1d9ac662015-12-17 21:35:47 -0800140 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 Boeuf4f4d37c2016-02-24 22:07:36 -0800144 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 Boeuf7d1d9ca2015-04-17 14:38:48 -0700148};
149
Julien Boeuf1d9ac662015-12-17 21:35:47 -0800150/* Checks that the host that will be set for a call is acceptable. */
151void grpc_channel_security_connector_check_call_host(
Craig Tillera82950e2015-09-22 12:33:20 -0700152 grpc_exec_ctx *exec_ctx, grpc_channel_security_connector *sc,
Julien Boeuf1d9ac662015-12-17 21:35:47 -0800153 const char *host, grpc_auth_context *auth_context,
154 grpc_security_call_host_check_cb cb, void *user_data);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700155
Julien Boeuf4f4d37c2016-02-24 22:07:36 -0800156/* Handshake. */
157void 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
167typedef struct grpc_server_security_connector grpc_server_security_connector;
168
169struct 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
181void 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
186void grpc_server_security_connector_shutdown(
187 grpc_exec_ctx *exec_ctx, grpc_server_security_connector *connector);
188
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700189/* --- Creation security connectors. --- */
190
191/* For TESTING ONLY!
192 Creates a fake connector that emulates real channel security. */
Craig Tillera82950e2015-09-22 12:33:20 -0700193grpc_channel_security_connector *grpc_fake_channel_security_connector_create(
Julien Boeuf1d9ac662015-12-17 21:35:47 -0800194 grpc_call_credentials *request_metadata_creds);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700195
196/* For TESTING ONLY!
197 Creates a fake connector that emulates real server security. */
Julien Boeuf4f4d37c2016-02-24 22:07:36 -0800198grpc_server_security_connector *grpc_fake_server_security_connector_create(
199 void);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700200
201/* Config for ssl clients. */
Craig Tillera82950e2015-09-22 12:33:20 -0700202typedef struct {
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700203 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 Tillera82950e2015-09-22 12:33:20 -0700224grpc_security_status grpc_ssl_channel_security_connector_create(
Julien Boeuf441176d2015-10-09 21:14:07 -0700225 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 Boeuf7d1d9ca2015-04-17 14:38:48 -0700228
229/* Gets the default ssl roots. */
Craig Tillera82950e2015-09-22 12:33:20 -0700230size_t grpc_get_default_ssl_roots(const unsigned char **pem_root_certs);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700231
Julien Boeuf373debd2016-01-27 15:41:12 -0800232/* Exposed for TESTING ONLY!. */
233gpr_slice grpc_get_default_ssl_roots_for_testing(void);
234
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700235/* Config for ssl servers. */
Craig Tillera82950e2015-09-22 12:33:20 -0700236typedef struct {
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700237 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 Boeuf5029b302015-07-21 23:02:16 -0700244 int force_client_auth;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700245} 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 Tillera82950e2015-09-22 12:33:20 -0700253grpc_security_status grpc_ssl_server_security_connector_create(
Julien Boeuf4f4d37c2016-02-24 22:07:36 -0800254 const grpc_ssl_server_config *config, grpc_server_security_connector **sc);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700255
Julien Boeufcf4124e2015-05-18 15:08:50 -0700256/* Util. */
Craig Tillera82950e2015-09-22 12:33:20 -0700257const tsi_peer_property *tsi_peer_get_property_by_name(const tsi_peer *peer,
258 const char *name);
Julien Boeufcf4124e2015-05-18 15:08:50 -0700259
Julien Boeufa701ade2015-06-18 15:23:40 +0200260/* Exposed for testing only. */
Craig Tillera82950e2015-09-22 12:33:20 -0700261grpc_auth_context *tsi_ssl_peer_to_auth_context(const tsi_peer *peer);
Julien Boeufee9d78b2015-12-18 09:50:34 -0800262tsi_peer tsi_shallow_peer_from_ssl_auth_context(
Julien Boeuf1d9ac662015-12-17 21:35:47 -0800263 const grpc_auth_context *auth_context);
264void tsi_shallow_peer_destruct(tsi_peer *peer);
Julien Boeufa701ade2015-06-18 15:23:40 +0200265
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700266#endif /* GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_H */