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