GRPC Core  0.13.1-pre1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grpc_security.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016, 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_GRPC_SECURITY_H
35 #define GRPC_GRPC_SECURITY_H
36 
37 #include <grpc/grpc.h>
38 #include <grpc/status.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /* --- Authentication Context. --- */
45 
46 #define GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME "transport_security_type"
47 #define GRPC_SSL_TRANSPORT_SECURITY_TYPE "ssl"
48 
49 #define GRPC_X509_CN_PROPERTY_NAME "x509_common_name"
50 #define GRPC_X509_SAN_PROPERTY_NAME "x509_subject_alternative_name"
51 
53 
56  size_t index;
57  const char *name;
59 
60 /* value, if not NULL, is guaranteed to be NULL terminated. */
61 typedef struct grpc_auth_property {
62  char *name;
63  char *value;
64  size_t value_length;
66 
67 /* Returns NULL when the iterator is at the end. */
70 
71 /* Iterates over the auth context. */
74 
75 /* Gets the peer identity. Returns an empty iterator (first _next will return
76  NULL) if the peer is not authenticated. */
79 
80 /* Finds a property in the context. May return an empty iterator (first _next
81  will return NULL) if no property with this name was found in the context. */
84  const char *name);
85 
86 /* Gets the name of the property that indicates the peer identity. Will return
87  NULL if the peer is not authenticated. */
89  const grpc_auth_context *ctx);
90 
91 /* Returns 1 if the peer is authenticated, 0 otherwise. */
93  const grpc_auth_context *ctx);
94 
95 /* Gets the auth context from the call. Caller needs to call
96  grpc_auth_context_release on the returned context. */
98 
99 /* Releases the auth context returned from grpc_call_auth_context. */
101 
102 /* --
103  The following auth context methods should only be called by a server metadata
104  processor to set properties extracted from auth metadata.
105  -- */
106 
107 /* Add a property. */
109  const char *name, const char *value,
110  size_t value_length);
111 
112 /* Add a C string property. */
114  const char *name,
115  const char *value);
116 
117 /* Sets the property name. Returns 1 if successful or 0 in case of failure
118  (which means that no property with this name exists). */
120  grpc_auth_context *ctx, const char *name);
121 
122 /* --- grpc_channel_credentials object. ---
123 
124  A channel credentials object represents a way to authenticate a client on a
125  channel. */
126 
128 
129 /* Releases a channel credentials object.
130  The creator of the credentials object is responsible for its release. */
132 
133 /* Environment variable that points to the google default application
134  credentials json key or refresh token. Used in the
135  grpc_google_default_credentials_create function. */
136 #define GRPC_GOOGLE_CREDENTIALS_ENV_VAR "GOOGLE_APPLICATION_CREDENTIALS"
137 
138 /* Creates default credentials to connect to a google gRPC service.
139  WARNING: Do NOT use this credentials to connect to a non-google service as
140  this could result in an oauth2 token leak. */
142 
143 /* Environment variable that points to the default SSL roots file. This file
144  must be a PEM encoded file with all the roots such as the one that can be
145  downloaded from https://pki.google.com/roots.pem. */
146 #define GRPC_DEFAULT_SSL_ROOTS_FILE_PATH_ENV_VAR \
147  "GRPC_DEFAULT_SSL_ROOTS_FILE_PATH"
148 
149 /* Results for the SSL roots override callback. */
150 typedef enum {
152  GRPC_SSL_ROOTS_OVERRIDE_FAIL_PERMANENTLY, /* Do not try fallback options. */
155 
156 /* Callback for getting the SSL roots override from the application.
157  In case of success, *pem_roots_certs must be set to a NULL terminated string
158  containing the list of PEM encoded root certificates. The ownership is passed
159  to the core and freed (laster by the core) with gpr_free.
160  If this function fails and GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment is
161  set to a valid path, it will override the roots specified this func */
163  char **pem_root_certs);
164 
165 /* Setup a callback to override the default TLS/SSL roots.
166  This function is not thread-safe and must be called at initialization time
167  before any ssl credentials are created to have the desired side effect.
168  If GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment is set to a valid path, the
169  callback will not be called. */
172 
173 /* Object that holds a private key / certificate chain pair in PEM format. */
174 typedef struct {
175  /* private_key is the NULL-terminated string containing the PEM encoding of
176  the client's private key. */
177  const char *private_key;
178 
179  /* cert_chain is the NULL-terminated string containing the PEM encoding of
180  the client's certificate chain. */
181  const char *cert_chain;
183 
184 /* Creates an SSL credentials object.
185  - pem_roots_cert is the NULL-terminated string containing the PEM encoding
186  of the server root certificates. If this parameter is NULL, the
187  implementation will first try to dereference the file pointed by the
188  GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable, and if that fails,
189  try to get the roots set by grpc_override_ssl_default_roots. Eventually,
190  if all these fail, it will try to get the roots from a well-known place on
191  disk (in the grpc install directory).
192  - pem_key_cert_pair is a pointer on the object containing client's private
193  key and certificate chain. This parameter can be NULL if the client does
194  not have such a key/cert pair. */
196  const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pair,
197  void *reserved);
198 
199 /* --- grpc_call_credentials object.
200 
201  A call credentials object represents a way to authenticate on a particular
202  call. These credentials can be composed with a channel credentials object
203  so that they are sent with every call on this channel. */
204 
206 
207 /* Releases a call credentials object.
208  The creator of the credentials object is responsible for its release. */
210 
211 /* Creates a composite channel credentials object. */
213  grpc_channel_credentials *channel_creds, grpc_call_credentials *call_creds,
214  void *reserved);
215 
216 /* Creates a composite call credentials object. */
219  void *reserved);
220 
221 /* Creates a compute engine credentials object for connecting to Google.
222  WARNING: Do NOT use this credentials to connect to a non-google service as
223  this could result in an oauth2 token leak. */
225  void *reserved);
226 
228 
229 /* Creates a JWT credentials object. May return NULL if the input is invalid.
230  - json_key is the JSON key string containing the client's private key.
231  - token_lifetime is the lifetime of each Json Web Token (JWT) created with
232  this credentials. It should not exceed grpc_max_auth_token_lifetime or
233  will be cropped to this value. */
236  gpr_timespec token_lifetime,
237  void *reserved);
238 
239 /* Creates an Oauth2 Refresh Token credentials object for connecting to Google.
240  May return NULL if the input is invalid.
241  WARNING: Do NOT use this credentials to connect to a non-google service as
242  this could result in an oauth2 token leak.
243  - json_refresh_token is the JSON string containing the refresh token itself
244  along with a client_id and client_secret. */
246  const char *json_refresh_token, void *reserved);
247 
248 /* Creates an Oauth2 Access Token credentials with an access token that was
249  aquired by an out of band mechanism. */
251  const char *access_token, void *reserved);
252 
253 /* Creates an IAM credentials object for connecting to Google. */
255  const char *authorization_token, const char *authority_selector,
256  void *reserved);
257 
258 /* Callback function to be called by the metadata credentials plugin
259  implementation when the metadata is ready.
260  - user_data is the opaque pointer that was passed in the get_metadata method
261  of the grpc_metadata_credentials_plugin (see below).
262  - creds_md is an array of credentials metadata produced by the plugin. It
263  may be set to NULL in case of an error.
264  - num_creds_md is the number of items in the creds_md array.
265  - status must be GRPC_STATUS_OK in case of success or another specific error
266  code otherwise.
267  - error_details contains details about the error if any. In case of success
268  it should be NULL and will be otherwise ignored. */
270  void *user_data, const grpc_metadata *creds_md, size_t num_creds_md,
271  grpc_status_code status, const char *error_details);
272 
273 /* Context that can be used by metadata credentials plugin in order to create
274  auth related metadata. */
275 typedef struct {
276  /* The fully qualifed service url. */
277  const char *service_url;
278 
279  /* The method name of the RPC being called (not fully qualified).
280  The fully qualified method name can be built from the service_url:
281  full_qualified_method_name = ctx->service_url + '/' + ctx->method_name. */
282  const char *method_name;
283 
284  /* The auth_context of the channel which gives the server's identity. */
286 
287  /* Reserved for future use. */
288  void *reserved;
290 
291 /* grpc_metadata_credentials plugin is an API user provided structure used to
292  create grpc_credentials objects that can be set on a channel (composed) or
293  a call. See grpc_credentials_metadata_create_from_plugin below.
294  The grpc client stack will call the get_metadata method of the plugin for
295  every call in scope for the credentials created from it. */
296 typedef struct {
297  /* The implementation of this method has to be non-blocking.
298  - context is the information that can be used by the plugin to create auth
299  metadata.
300  - cb is the callback that needs to be called when the metadata is ready.
301  - user_data needs to be passed as the first parameter of the callback. */
302  void (*get_metadata)(void *state, grpc_auth_metadata_context context,
303  grpc_credentials_plugin_metadata_cb cb, void *user_data);
304 
305  /* Destroys the plugin state. */
306  void (*destroy)(void *state);
307 
308  /* State that will be set as the first parameter of the methods above. */
309  void *state;
310 
311  /* Type of credentials that this plugin is implementing. */
312  const char *type;
314 
315 /* Creates a credentials object from a plugin. */
317  grpc_metadata_credentials_plugin plugin, void *reserved);
318 
319 /* --- Secure channel creation. --- */
320 
321 /* Creates a secure channel using the passed-in credentials. */
323  grpc_channel_credentials *creds, const char *target,
324  const grpc_channel_args *args, void *reserved);
325 
326 /* --- grpc_server_credentials object. ---
327 
328  A server credentials object represents a way to authenticate a server. */
329 
331 
332 /* Releases a server_credentials object.
333  The creator of the server_credentials object is responsible for its release.
334  */
336 
337 /* Creates an SSL server_credentials object.
338  - pem_roots_cert is the NULL-terminated string containing the PEM encoding of
339  the client root certificates. This parameter may be NULL if the server does
340  not want the client to be authenticated with SSL.
341  - pem_key_cert_pairs is an array private key / certificate chains of the
342  server. This parameter cannot be NULL.
343  - num_key_cert_pairs indicates the number of items in the private_key_files
344  and cert_chain_files parameters. It should be at least 1.
345  - force_client_auth, if set to non-zero will force the client to authenticate
346  with an SSL cert. Note that this option is ignored if pem_root_certs is
347  NULL. */
349  const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pairs,
350  size_t num_key_cert_pairs, int force_client_auth, void *reserved);
351 
352 /* --- Server-side secure ports. --- */
353 
354 /* Add a HTTP2 over an encrypted link over tcp listener.
355  Returns bound port number on success, 0 on failure.
356  REQUIRES: server not started */
358  const char *addr,
359  grpc_server_credentials *creds);
360 
361 /* --- Call specific credentials. --- */
362 
363 /* Sets a credentials to a call. Can only be called on the client side before
364  grpc_call_start_batch. */
367 
368 /* --- Auth Metadata Processing --- */
369 
370 /* Callback function that is called when the metadata processing is done.
371  - Consumed metadata will be removed from the set of metadata available on the
372  call. consumed_md may be NULL if no metadata has been consumed.
373  - Response metadata will be set on the response. response_md may be NULL.
374  - status is GRPC_STATUS_OK for success or a specific status for an error.
375  Common error status for auth metadata processing is either
376  GRPC_STATUS_UNAUTHENTICATED in case of an authentication failure or
377  GRPC_STATUS PERMISSION_DENIED in case of an authorization failure.
378  - error_details gives details about the error. May be NULL. */
380  void *user_data, const grpc_metadata *consumed_md, size_t num_consumed_md,
381  const grpc_metadata *response_md, size_t num_response_md,
382  grpc_status_code status, const char *error_details);
383 
384 /* Pluggable server-side metadata processor object. */
385 typedef struct {
386  /* The context object is read/write: it contains the properties of the
387  channel peer and it is the job of the process function to augment it with
388  properties derived from the passed-in metadata.
389  The lifetime of these objects is guaranteed until cb is invoked. */
390  void (*process)(void *state, grpc_auth_context *context,
391  const grpc_metadata *md, size_t num_md,
392  grpc_process_auth_metadata_done_cb cb, void *user_data);
393  void (*destroy)(void *state);
394  void *state;
396 
399 
400 #ifdef __cplusplus
401 }
402 #endif
403 
404 #endif /* GRPC_GRPC_SECURITY_H */
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:62
GRPCAPI grpc_auth_property_iterator grpc_auth_context_find_properties_by_name(const grpc_auth_context *ctx, const char *name)
GRPCAPI void grpc_auth_context_add_cstring_property(grpc_auth_context *ctx, const char *name, const char *value)
void(* grpc_process_auth_metadata_done_cb)(void *user_data, const grpc_metadata *consumed_md, size_t num_consumed_md, const grpc_metadata *response_md, size_t num_response_md, grpc_status_code status, const char *error_details)
Definition: grpc_security.h:379
GRPCAPI grpc_auth_property_iterator grpc_auth_context_property_iterator(const grpc_auth_context *ctx)
size_t value_length
Definition: grpc_security.h:64
struct grpc_server_credentials grpc_server_credentials
Definition: grpc_security.h:330
GRPCAPI grpc_call_error grpc_call_set_credentials(grpc_call *call, grpc_call_credentials *creds)
An array of arguments that can be passed around.
Definition: grpc_types.h:110
char * value
Definition: grpc_security.h:63
GRPCAPI grpc_channel_credentials * grpc_composite_channel_credentials_create(grpc_channel_credentials *channel_creds, grpc_call_credentials *call_creds, void *reserved)
struct grpc_channel grpc_channel
The Channel interface allows creation of Call objects.
Definition: grpc_types.h:54
GRPCAPI grpc_call_credentials * grpc_service_account_jwt_access_credentials_create(const char *json_key, gpr_timespec token_lifetime, void *reserved)
GRPCAPI void grpc_server_credentials_set_auth_metadata_processor(grpc_server_credentials *creds, grpc_auth_metadata_processor processor)
Definition: grpc_security.h:296
const char * method_name
Definition: grpc_security.h:282
const char * type
Definition: grpc_security.h:312
const char * private_key
Definition: grpc_security.h:177
GRPCAPI grpc_call_credentials * grpc_google_refresh_token_credentials_create(const char *json_refresh_token, void *reserved)
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc_types.h:57
GRPCAPI grpc_channel_credentials * grpc_ssl_credentials_create(const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pair, void *reserved)
void * state
Definition: grpc_security.h:394
Definition: grpc_security.h:153
char * name
Definition: grpc_security.h:62
void * reserved
Definition: grpc_security.h:288
GRPCAPI grpc_channel * grpc_secure_channel_create(grpc_channel_credentials *creds, const char *target, const grpc_channel_args *args, void *reserved)
GRPCAPI gpr_timespec grpc_max_auth_token_lifetime()
GRPCAPI void grpc_auth_context_add_property(grpc_auth_context *ctx, const char *name, const char *value, size_t value_length)
GRPCAPI grpc_channel_credentials * grpc_google_default_credentials_create(void)
GRPCAPI void grpc_set_ssl_roots_override_callback(grpc_ssl_roots_override_callback cb)
grpc_call_error
Result of a grpc call.
Definition: grpc_types.h:158
GRPCAPI grpc_auth_context * grpc_call_auth_context(grpc_call *call)
GRPCAPI int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, grpc_server_credentials *creds)
const grpc_auth_context * ctx
Definition: grpc_security.h:55
GRPCAPI const grpc_auth_property * grpc_auth_property_iterator_next(grpc_auth_property_iterator *it)
const char * service_url
Definition: grpc_security.h:277
const char * name
Definition: grpc_security.h:57
GRPCAPI void grpc_call_credentials_release(grpc_call_credentials *creds)
size_t index
Definition: grpc_security.h:56
Definition: grpc_security.h:151
Definition: grpc_security.h:54
struct grpc_call_credentials grpc_call_credentials
Definition: grpc_security.h:205
Definition: grpc_security.h:174
const grpc_auth_context * channel_auth_context
Definition: grpc_security.h:285
A single metadata element.
Definition: grpc_types.h:203
struct grpc_auth_context grpc_auth_context
Definition: grpc_security.h:52
struct grpc_auth_property_iterator grpc_auth_property_iterator
GRPCAPI grpc_auth_property_iterator grpc_auth_context_peer_identity(const grpc_auth_context *ctx)
const char * cert_chain
Definition: grpc_security.h:181
GRPCAPI grpc_server_credentials * grpc_ssl_server_credentials_create(const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pairs, size_t num_key_cert_pairs, int force_client_auth, void *reserved)
Definition: grpc_security.h:385
#define GRPCAPI
Definition: port_platform.h:363
struct grpc_channel_credentials grpc_channel_credentials
Definition: grpc_security.h:127
void * state
Definition: grpc_security.h:309
GRPCAPI void grpc_server_credentials_release(grpc_server_credentials *creds)
GRPCAPI grpc_call_credentials * grpc_google_compute_engine_credentials_create(void *reserved)
Definition: grpc_security.h:61
Definition: grpc_security.h:152
GRPCAPI grpc_call_credentials * grpc_composite_call_credentials_create(grpc_call_credentials *creds1, grpc_call_credentials *creds2, void *reserved)
GRPCAPI grpc_call_credentials * grpc_metadata_credentials_create_from_plugin(grpc_metadata_credentials_plugin plugin, void *reserved)
GRPCAPI grpc_call_credentials * grpc_google_iam_credentials_create(const char *authorization_token, const char *authority_selector, void *reserved)
struct grpc_auth_property grpc_auth_property
GRPCAPI void grpc_auth_context_release(grpc_auth_context *context)
GRPCAPI int grpc_auth_context_set_peer_identity_property_name(grpc_auth_context *ctx, const char *name)
GRPCAPI grpc_call_credentials * grpc_access_token_credentials_create(const char *access_token, void *reserved)
GRPCAPI const char * grpc_auth_context_peer_identity_property_name(const grpc_auth_context *ctx)
Definition: time.h:63
void(* grpc_credentials_plugin_metadata_cb)(void *user_data, const grpc_metadata *creds_md, size_t num_creds_md, grpc_status_code status, const char *error_details)
Definition: grpc_security.h:269
grpc_status_code
Definition: status.h:41
GRPCAPI int grpc_auth_context_peer_is_authenticated(const grpc_auth_context *ctx)
grpc_ssl_roots_override_result
Definition: grpc_security.h:150
Definition: grpc_security.h:275
grpc_ssl_roots_override_result(* grpc_ssl_roots_override_callback)(char **pem_root_certs)
Definition: grpc_security.h:162
GRPCAPI void grpc_channel_credentials_release(grpc_channel_credentials *creds)