gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
security_connector.h
1 /*
2  *
3  * Copyright 2015, Google Inc.
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 #ifndef GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONNECTOR_H
35 #define GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONNECTOR_H
36 
37 #include <grpc/grpc_security.h>
38 #include "src/core/iomgr/endpoint.h"
39 #include "src/core/tsi/transport_security_interface.h"
40 
41 /* --- status enum. --- */
42 
43 typedef enum {
44  GRPC_SECURITY_OK = 0,
45  GRPC_SECURITY_PENDING,
46  GRPC_SECURITY_ERROR
47 } grpc_security_status;
48 
49 /* --- URL schemes. --- */
50 
51 #define GRPC_SSL_URL_SCHEME "https"
52 #define GRPC_FAKE_SECURITY_URL_SCHEME "http+fake_security"
53 
54 /* --- security_connector object. ---
55 
56  A security connector object represents away to configure the underlying
57  transport security mechanism and check the resulting trusted peer. */
58 
60 
61 #define GRPC_SECURITY_CONNECTOR_ARG "grpc.security_connector"
62 
63 typedef void (*grpc_security_check_cb)(void *user_data,
64  grpc_security_status status);
65 
66 typedef struct {
67  void (*destroy)(grpc_security_connector *sc);
68  grpc_security_status (*create_handshaker)(grpc_security_connector *sc,
69  tsi_handshaker **handshaker);
70  grpc_security_status (*check_peer)(grpc_security_connector *sc, tsi_peer peer,
71  grpc_security_check_cb cb,
72  void *user_data);
74 
76  const grpc_security_connector_vtable *vtable;
77  gpr_refcount refcount;
78  int is_client_side;
79  const char *url_scheme;
80 };
81 
82 /* Increments the refcount. */
83 grpc_security_connector *grpc_security_connector_ref(
85 
86 /* Decrements the refcount and destroys the object if it reaches 0. */
87 void grpc_security_connector_unref(grpc_security_connector *sc);
88 
89 /* Handshake creation. */
90 grpc_security_status grpc_security_connector_create_handshaker(
91  grpc_security_connector *sc, tsi_handshaker **handshaker);
92 
93 /* Check the peer.
94  Implementations can choose to check the peer either synchronously or
95  asynchronously. In the first case, a successful call will return
96  GRPC_SECURITY_OK. In the asynchronous case, the call will return
97  GRPC_SECURITY_PENDING unless an error is detected early on.
98  Ownership of the peer is transfered.
99 */
100 grpc_security_status grpc_security_connector_check_peer(
101  grpc_security_connector *sc, tsi_peer peer, grpc_security_check_cb cb,
102  void *user_data);
103 
104 /* Util to encapsulate the connector in a channel arg. */
105 grpc_arg grpc_security_connector_to_arg(grpc_security_connector *sc);
106 
107 /* Util to get the connector from a channel arg. */
108 grpc_security_connector *grpc_security_connector_from_arg(const grpc_arg *arg);
109 
110 /* Util to find the connector from channel args. */
111 grpc_security_connector *grpc_find_security_connector_in_args(
112  const grpc_channel_args *args);
113 
114 /* --- channel_security_connector object. ---
115 
116  A channel security connector object represents away to configure the
117  underlying transport security mechanism on the client side. */
118 
120 
122  grpc_security_connector base; /* requires is_client_side to be non 0. */
123  grpc_credentials *request_metadata_creds;
124  grpc_security_status (*check_call_host)(grpc_channel_security_connector *sc,
125  const char *host,
126  grpc_security_check_cb cb,
127  void *user_data);
128 };
129 
130 /* Checks that the host that will be set for a call is acceptable.
131  Implementations can choose do the check either synchronously or
132  asynchronously. In the first case, a successful call will return
133  GRPC_SECURITY_OK. In the asynchronous case, the call will return
134  GRPC_SECURITY_PENDING unless an error is detected early on. */
135 grpc_security_status grpc_channel_security_connector_check_call_host(
136  grpc_channel_security_connector *sc, const char *host,
137  grpc_security_check_cb cb, void *user_data);
138 
139 /* --- Creation security connectors. --- */
140 
141 /* For TESTING ONLY!
142  Creates a fake connector that emulates real channel security. */
143 grpc_channel_security_connector *grpc_fake_channel_security_connector_create(
144  grpc_credentials *request_metadata_creds, int call_host_check_is_async);
145 
146 /* For TESTING ONLY!
147  Creates a fake connector that emulates real server security. */
148 grpc_security_connector *grpc_fake_server_security_connector_create(void);
149 
150 /* Config for ssl clients. */
151 typedef struct {
152  unsigned char *pem_private_key;
153  size_t pem_private_key_size;
154  unsigned char *pem_cert_chain;
155  size_t pem_cert_chain_size;
156  unsigned char *pem_root_certs;
157  size_t pem_root_certs_size;
159 
160 /* Creates an SSL channel_security_connector.
161  - request_metadata_creds is the credentials object which metadata
162  will be sent with each request. This parameter can be NULL.
163  - config is the SSL config to be used for the SSL channel establishment.
164  - is_client should be 0 for a server or a non-0 value for a client.
165  - secure_peer_name is the secure peer name that should be checked in
166  grpc_channel_security_connector_check_peer. This parameter may be NULL in
167  which case the peer name will not be checked. Note that if this parameter
168  is not NULL, then, pem_root_certs should not be NULL either.
169  - sc is a pointer on the connector to be created.
170  This function returns GRPC_SECURITY_OK in case of success or a
171  specific error code otherwise.
172 */
173 grpc_security_status grpc_ssl_channel_security_connector_create(
174  grpc_credentials *request_metadata_creds,
175  const grpc_ssl_config *config, const char *target_name,
176  const char *overridden_target_name, grpc_channel_security_connector **sc);
177 
178 /* Gets the default ssl roots. */
179 size_t grpc_get_default_ssl_roots(const unsigned char **pem_root_certs);
180 
181 /* Config for ssl servers. */
182 typedef struct {
183  unsigned char **pem_private_keys;
184  size_t *pem_private_keys_sizes;
185  unsigned char **pem_cert_chains;
186  size_t *pem_cert_chains_sizes;
187  size_t num_key_cert_pairs;
188  unsigned char *pem_root_certs;
189  size_t pem_root_certs_size;
191 
192 /* Creates an SSL server_security_connector.
193  - config is the SSL config to be used for the SSL channel establishment.
194  - sc is a pointer on the connector to be created.
195  This function returns GRPC_SECURITY_OK in case of success or a
196  specific error code otherwise.
197 */
198 grpc_security_status grpc_ssl_server_security_connector_create(
199  const grpc_ssl_server_config *config, grpc_security_connector **sc);
200 
201 #endif /* GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONNECTOR_H */
Definition: security_connector.h:66
Definition: sync_generic.h:49
Definition: grpc.h:101
Definition: grpc.h:81
Definition: cmdline.c:46
Definition: credentials.h:111
Definition: transport_security_interface.h:192
Definition: security_connector.h:182
Definition: security_connector.h:75
Definition: security_connector.h:121
Definition: security_connector.h:151
Definition: transport_security.h:86