blob: 5d3d1e0f44f53f06cada1cc84d29cbd850ce0dec [file] [log] [blame]
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Boeuf7d1d9ca2015-04-17 14:38:48 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Boeuf7d1d9ca2015-04-17 14:38:48 -070016 *
17 */
18
Yihua Zhang75f0a9f2018-02-20 10:09:47 -080019#ifndef GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_SECURITY_CONNECTOR_H
20#define GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_SECURITY_CONNECTOR_H
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070021
Alexander Polcyndb3e8982018-02-21 16:59:24 -080022#include <grpc/support/port_platform.h>
23
Julien Boeufb71ef652017-04-12 21:44:49 -070024#include <stdbool.h>
25
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070026#include <grpc/grpc_security.h>
Mark D. Roth963be372016-11-16 14:17:06 -080027
28#include "src/core/lib/channel/handshaker.h"
Craig Tiller9533d042016-03-25 17:11:06 -070029#include "src/core/lib/iomgr/endpoint.h"
30#include "src/core/lib/iomgr/tcp_server.h"
Julien Boeufb71ef652017-04-12 21:44:49 -070031#include "src/core/tsi/ssl_transport_security.h"
Craig Tillerb29f1fe2017-03-28 15:49:23 -070032#include "src/core/tsi/transport_security_interface.h"
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070033
ncteisen05294b62017-11-10 16:57:53 -080034extern grpc_core::DebugOnlyTraceFlag grpc_trace_security_connector_refcount;
ncteisen4b584052017-06-08 16:44:38 -070035
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070036/* --- status enum. --- */
37
Craig Tillerbe52c6e2016-01-04 15:35:26 -080038typedef enum { GRPC_SECURITY_OK = 0, GRPC_SECURITY_ERROR } grpc_security_status;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070039
40/* --- URL schemes. --- */
41
42#define GRPC_SSL_URL_SCHEME "https"
43#define GRPC_FAKE_SECURITY_URL_SCHEME "http+fake_security"
44
45/* --- security_connector object. ---
46
47 A security connector object represents away to configure the underlying
48 transport security mechanism and check the resulting trusted peer. */
49
50typedef struct grpc_security_connector grpc_security_connector;
51
David Garcia Quintas01291502017-02-07 13:26:41 -080052#define GRPC_ARG_SECURITY_CONNECTOR "grpc.security_connector"
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070053
Craig Tillera82950e2015-09-22 12:33:20 -070054typedef struct {
Yash Tibrewal8cf14702017-12-06 09:47:54 -080055 void (*destroy)(grpc_security_connector* sc);
56 void (*check_peer)(grpc_security_connector* sc, tsi_peer peer,
57 grpc_auth_context** auth_context,
Craig Tillerbaa14a92017-11-03 09:09:36 -070058 grpc_closure* on_peer_checked);
59 int (*cmp)(grpc_security_connector* sc, grpc_security_connector* other);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070060} grpc_security_connector_vtable;
61
Craig Tillera82950e2015-09-22 12:33:20 -070062struct grpc_security_connector {
Craig Tillerbaa14a92017-11-03 09:09:36 -070063 const grpc_security_connector_vtable* vtable;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070064 gpr_refcount refcount;
Craig Tillerbaa14a92017-11-03 09:09:36 -070065 const char* url_scheme;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070066};
67
Craig Tiller5d44c062015-07-01 08:55:28 -070068/* Refcounting. */
ncteisen4b584052017-06-08 16:44:38 -070069#ifndef NDEBUG
Craig Tiller5d44c062015-07-01 08:55:28 -070070#define GRPC_SECURITY_CONNECTOR_REF(p, r) \
71 grpc_security_connector_ref((p), __FILE__, __LINE__, (r))
Yash Tibrewal8cf14702017-12-06 09:47:54 -080072#define GRPC_SECURITY_CONNECTOR_UNREF(p, r) \
73 grpc_security_connector_unref((p), __FILE__, __LINE__, (r))
Craig Tillerbaa14a92017-11-03 09:09:36 -070074grpc_security_connector* grpc_security_connector_ref(
75 grpc_security_connector* policy, const char* file, int line,
76 const char* reason);
Yash Tibrewal8cf14702017-12-06 09:47:54 -080077void grpc_security_connector_unref(grpc_security_connector* policy,
Craig Tillerbaa14a92017-11-03 09:09:36 -070078 const char* file, int line,
79 const char* reason);
Craig Tiller5d44c062015-07-01 08:55:28 -070080#else
81#define GRPC_SECURITY_CONNECTOR_REF(p, r) grpc_security_connector_ref((p))
Yash Tibrewal8cf14702017-12-06 09:47:54 -080082#define GRPC_SECURITY_CONNECTOR_UNREF(p, r) grpc_security_connector_unref((p))
Craig Tillerbaa14a92017-11-03 09:09:36 -070083grpc_security_connector* grpc_security_connector_ref(
84 grpc_security_connector* policy);
Yash Tibrewal8cf14702017-12-06 09:47:54 -080085void grpc_security_connector_unref(grpc_security_connector* policy);
Craig Tiller5d44c062015-07-01 08:55:28 -070086#endif
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070087
Julien Boeuf1d9ac662015-12-17 21:35:47 -080088/* Check the peer. Callee takes ownership of the peer object.
Mark D. Roth71daef72016-12-06 07:26:52 -080089 When done, sets *auth_context and invokes on_peer_checked. */
Yash Tibrewal8cf14702017-12-06 09:47:54 -080090void grpc_security_connector_check_peer(grpc_security_connector* sc,
Julien Boeuf366f42c2015-12-16 22:05:46 -080091 tsi_peer peer,
Craig Tillerbaa14a92017-11-03 09:09:36 -070092 grpc_auth_context** auth_context,
93 grpc_closure* on_peer_checked);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070094
Mark D. Rothccfdfb32017-10-16 13:26:13 -070095/* Compares two security connectors. */
Craig Tillerbaa14a92017-11-03 09:09:36 -070096int grpc_security_connector_cmp(grpc_security_connector* sc,
97 grpc_security_connector* other);
Mark D. Rothccfdfb32017-10-16 13:26:13 -070098
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070099/* Util to encapsulate the connector in a channel arg. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700100grpc_arg grpc_security_connector_to_arg(grpc_security_connector* sc);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700101
102/* Util to get the connector from a channel arg. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700103grpc_security_connector* grpc_security_connector_from_arg(const grpc_arg* arg);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700104
105/* Util to find the connector from channel args. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700106grpc_security_connector* grpc_security_connector_find_in_args(
107 const grpc_channel_args* args);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700108
109/* --- channel_security_connector object. ---
110
Mark D. Rothccfdfb32017-10-16 13:26:13 -0700111 A channel security connector object represents a way to configure the
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700112 underlying transport security mechanism on the client side. */
113
114typedef struct grpc_channel_security_connector grpc_channel_security_connector;
115
Craig Tillera82950e2015-09-22 12:33:20 -0700116struct grpc_channel_security_connector {
Julien Boeuf4f4d37c2016-02-24 22:07:36 -0800117 grpc_security_connector base;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700118 grpc_channel_credentials* channel_creds;
119 grpc_call_credentials* request_metadata_creds;
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800120 bool (*check_call_host)(grpc_channel_security_connector* sc, const char* host,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700121 grpc_auth_context* auth_context,
122 grpc_closure* on_call_host_checked,
123 grpc_error** error);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800124 void (*cancel_check_call_host)(grpc_channel_security_connector* sc,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700125 grpc_closure* on_call_host_checked,
126 grpc_error* error);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800127 void (*add_handshakers)(grpc_channel_security_connector* sc,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700128 grpc_handshake_manager* handshake_mgr);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700129};
130
Mark D. Rothccfdfb32017-10-16 13:26:13 -0700131/// A helper function for use in grpc_security_connector_cmp() implementations.
Craig Tillerbaa14a92017-11-03 09:09:36 -0700132int grpc_channel_security_connector_cmp(grpc_channel_security_connector* sc1,
133 grpc_channel_security_connector* sc2);
Mark D. Rothccfdfb32017-10-16 13:26:13 -0700134
Mark D. Rothe0778b22017-07-21 15:42:00 -0700135/// Checks that the host that will be set for a call is acceptable.
136/// Returns true if completed synchronously, in which case \a error will
137/// be set to indicate the result. Otherwise, \a on_call_host_checked
138/// will be invoked when complete.
139bool grpc_channel_security_connector_check_call_host(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800140 grpc_channel_security_connector* sc, const char* host,
141 grpc_auth_context* auth_context, grpc_closure* on_call_host_checked,
142 grpc_error** error);
Mark D. Rothe0778b22017-07-21 15:42:00 -0700143
144/// Cancels a pending asychronous call to
145/// grpc_channel_security_connector_check_call_host() with
146/// \a on_call_host_checked as its callback.
147void grpc_channel_security_connector_cancel_check_call_host(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800148 grpc_channel_security_connector* sc, grpc_closure* on_call_host_checked,
149 grpc_error* error);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700150
Mark D. Roth963be372016-11-16 14:17:06 -0800151/* Registers handshakers with \a handshake_mgr. */
Mark D. Roth65b79c82016-12-06 07:20:20 -0800152void grpc_channel_security_connector_add_handshakers(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800153 grpc_channel_security_connector* connector,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700154 grpc_handshake_manager* handshake_mgr);
Julien Boeuf4f4d37c2016-02-24 22:07:36 -0800155
156/* --- server_security_connector object. ---
157
Mark D. Rothccfdfb32017-10-16 13:26:13 -0700158 A server security connector object represents a way to configure the
Julien Boeuf4f4d37c2016-02-24 22:07:36 -0800159 underlying transport security mechanism on the server side. */
160
161typedef struct grpc_server_security_connector grpc_server_security_connector;
162
163struct grpc_server_security_connector {
164 grpc_security_connector base;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700165 grpc_server_credentials* server_creds;
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800166 void (*add_handshakers)(grpc_server_security_connector* sc,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700167 grpc_handshake_manager* handshake_mgr);
Julien Boeuf4f4d37c2016-02-24 22:07:36 -0800168};
169
Mark D. Rothccfdfb32017-10-16 13:26:13 -0700170/// A helper function for use in grpc_security_connector_cmp() implementations.
Craig Tillerbaa14a92017-11-03 09:09:36 -0700171int grpc_server_security_connector_cmp(grpc_server_security_connector* sc1,
172 grpc_server_security_connector* sc2);
Mark D. Rothccfdfb32017-10-16 13:26:13 -0700173
Mark D. Roth65b79c82016-12-06 07:20:20 -0800174void grpc_server_security_connector_add_handshakers(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800175 grpc_server_security_connector* sc, grpc_handshake_manager* handshake_mgr);
Julien Boeuf4f4d37c2016-02-24 22:07:36 -0800176
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700177/* --- Creation security connectors. --- */
178
179/* For TESTING ONLY!
180 Creates a fake connector that emulates real channel security. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700181grpc_channel_security_connector* grpc_fake_channel_security_connector_create(
182 grpc_channel_credentials* channel_creds,
183 grpc_call_credentials* request_metadata_creds, const char* target,
184 const grpc_channel_args* args);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700185
186/* For TESTING ONLY!
187 Creates a fake connector that emulates real server security. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700188grpc_server_security_connector* grpc_fake_server_security_connector_create(
189 grpc_server_credentials* server_creds);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700190
191/* Config for ssl clients. */
Julien Boeufb71ef652017-04-12 21:44:49 -0700192
Craig Tillera82950e2015-09-22 12:33:20 -0700193typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700194 tsi_ssl_pem_key_cert_pair* pem_key_cert_pair;
195 char* pem_root_certs;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700196} grpc_ssl_config;
197
198/* Creates an SSL channel_security_connector.
199 - request_metadata_creds is the credentials object which metadata
200 will be sent with each request. This parameter can be NULL.
201 - config is the SSL config to be used for the SSL channel establishment.
202 - is_client should be 0 for a server or a non-0 value for a client.
203 - secure_peer_name is the secure peer name that should be checked in
204 grpc_channel_security_connector_check_peer. This parameter may be NULL in
205 which case the peer name will not be checked. Note that if this parameter
206 is not NULL, then, pem_root_certs should not be NULL either.
207 - sc is a pointer on the connector to be created.
208 This function returns GRPC_SECURITY_OK in case of success or a
209 specific error code otherwise.
210*/
Craig Tillera82950e2015-09-22 12:33:20 -0700211grpc_security_status grpc_ssl_channel_security_connector_create(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800212 grpc_channel_credentials* channel_creds,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700213 grpc_call_credentials* request_metadata_creds,
214 const grpc_ssl_config* config, const char* target_name,
Ruslan Nigmatullin7ae37332018-02-21 16:44:35 -0800215 const char* overridden_target_name,
216 tsi_ssl_session_cache* ssl_session_cache,
217 grpc_channel_security_connector** sc);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700218
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700219/* Config for ssl servers. */
Craig Tillera82950e2015-09-22 12:33:20 -0700220typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700221 tsi_ssl_pem_key_cert_pair* pem_key_cert_pairs;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700222 size_t num_key_cert_pairs;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700223 char* pem_root_certs;
Deepak Lukosedba4c5f2016-03-25 12:54:25 -0700224 grpc_ssl_client_certificate_request_type client_certificate_request;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700225} grpc_ssl_server_config;
226
227/* Creates an SSL server_security_connector.
228 - config is the SSL config to be used for the SSL channel establishment.
229 - sc is a pointer on the connector to be created.
230 This function returns GRPC_SECURITY_OK in case of success or a
231 specific error code otherwise.
232*/
Craig Tillera82950e2015-09-22 12:33:20 -0700233grpc_security_status grpc_ssl_server_security_connector_create(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800234 grpc_server_credentials* server_credentials,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700235 grpc_server_security_connector** sc);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700236
Julien Boeufcf4124e2015-05-18 15:08:50 -0700237/* Util. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700238const tsi_peer_property* tsi_peer_get_property_by_name(const tsi_peer* peer,
239 const char* name);
Julien Boeufcf4124e2015-05-18 15:08:50 -0700240
Julien Boeufa701ade2015-06-18 15:23:40 +0200241/* Exposed for testing only. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700242grpc_auth_context* tsi_ssl_peer_to_auth_context(const tsi_peer* peer);
Julien Boeufee9d78b2015-12-18 09:50:34 -0800243tsi_peer tsi_shallow_peer_from_ssl_auth_context(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700244 const grpc_auth_context* auth_context);
245void tsi_shallow_peer_destruct(tsi_peer* peer);
Julien Boeufa701ade2015-06-18 15:23:40 +0200246
jiangtaoli2016144f5552018-03-23 11:28:48 -0700247/* --- Default SSL Root Store. --- */
248namespace grpc_core {
249
250// The class implements default SSL root store.
251class DefaultSslRootStore {
252 public:
253 // Gets the default SSL root store. Returns nullptr if not found.
254 static const tsi_ssl_root_certs_store* GetRootStore();
255
256 // Gets the default PEM root certificate.
257 static const char* GetPemRootCerts();
258
259 // Initializes the SSL root store's underlying data structure. It does not
260 // load default SSL root certificates. Should only be called by
261 // grpc_security_init().
262 static void Initialize();
263
264 // Destroys the default SSL root store. Should only be called by
265 // grpc_security_shutdown().
266 static void Destroy();
267
268 protected:
269 // Returns default PEM root certificates in nullptr terminated grpc_slice.
270 // This function is protected instead of private, so that it can be tested.
271 static grpc_slice ComputePemRootCerts();
272
273 private:
274 // Construct me not!
275 DefaultSslRootStore();
276
277 // Initialization of default SSL root store.
278 static void InitRootStore();
279
280 // One-time initialization of default SSL root store.
281 static void InitRootStoreOnce();
282
283 // SSL root store in tsi_ssl_root_certs_store object.
284 static tsi_ssl_root_certs_store* default_root_store_;
285
286 // Default PEM root certificates.
287 static grpc_slice default_pem_root_certs_;
288};
289
290} // namespace grpc_core
291
Yihua Zhang75f0a9f2018-02-20 10:09:47 -0800292#endif /* GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_SECURITY_CONNECTOR_H */