gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
grpc_security.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_GRPC_SECURITY_H
35 #define GRPC_GRPC_SECURITY_H
36 
37 #include "grpc.h"
38 #include "status.h"
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /* --- grpc_credentials object. ---
45 
46  A credentials object represents a way to authenticate a client. */
47 
48 typedef struct grpc_credentials grpc_credentials;
49 
50 /* Releases a credentials object.
51  The creator of the credentials object is responsible for its release. */
52 void grpc_credentials_release(grpc_credentials *creds);
53 
54 /* Creates default credentials to connect to a google gRPC service.
55  WARNING: Do NOT use this credentials to connect to a non-google service as
56  this could result in an oauth2 token leak. */
57 grpc_credentials *grpc_google_default_credentials_create(void);
58 
59 /* Environment variable that points to the default SSL roots file. This file
60  must be a PEM encoded file with all the roots such as the one that can be
61  downloaded from https://pki.google.com/roots.pem. */
62 #define GRPC_DEFAULT_SSL_ROOTS_FILE_PATH_ENV_VAR \
63  "GRPC_DEFAULT_SSL_ROOTS_FILE_PATH"
64 
65 /* Object that holds a private key / certificate chain pair in PEM format. */
66 typedef struct {
67  /* private_key is the NULL-terminated string containing the PEM encoding of
68  the client's private key. */
69  const char *private_key;
70 
71  /* cert_chain is the NULL-terminated string containing the PEM encoding of
72  the client's certificate chain. */
73  const char *cert_chain;
75 
76 /* Creates an SSL credentials object.
77  - pem_roots_cert is the NULL-terminated string containing the PEM encoding
78  of the server root certificates. If this parameter is NULL, the
79  implementation will first try to dereference the file pointed by the
80  GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable, and if that fails,
81  get the roots from a well-known place on disk (in the grpc install
82  directory).
83  - pem_key_cert_pair is a pointer on the object containing client's private
84  key and certificate chain. This parameter can be NULL if the client does
85  not have such a key/cert pair. */
86 grpc_credentials *grpc_ssl_credentials_create(
87  const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pair);
88 
89 /* Creates a composite credentials object. */
90 grpc_credentials *grpc_composite_credentials_create(grpc_credentials *creds1,
91  grpc_credentials *creds2);
92 
93 /* Creates a compute engine credentials object.
94  WARNING: Do NOT use this credentials to connect to a non-google service as
95  this could result in an oauth2 token leak. */
96 grpc_credentials *grpc_compute_engine_credentials_create(void);
97 
98 extern const gpr_timespec grpc_max_auth_token_lifetime;
99 
100 /* Creates a service account credentials object. May return NULL if the input is
101  invalid.
102  WARNING: Do NOT use this credentials to connect to a non-google service as
103  this could result in an oauth2 token leak.
104  - json_key is the JSON key string containing the client's private key.
105  - scope is a space-delimited list of the requested permissions.
106  - token_lifetime is the lifetime of each token acquired through this service
107  account credentials. It should not exceed grpc_max_auth_token_lifetime
108  or will be cropped to this value. */
109 grpc_credentials *grpc_service_account_credentials_create(
110  const char *json_key, const char *scope, gpr_timespec token_lifetime);
111 
112 /* Creates a JWT credentials object. May return NULL if the input is invalid.
113  - json_key is the JSON key string containing the client's private key.
114  - token_lifetime is the lifetime of each Json Web Token (JWT) created with
115  this credentials. It should not exceed grpc_max_auth_token_lifetime or
116  will be cropped to this value. */
117 grpc_credentials *grpc_jwt_credentials_create(const char *json_key,
118  gpr_timespec token_lifetime);
119 
120 /* Creates an Oauth2 Refresh Token crednetials object. May return NULL if the
121  input is invalid.
122  WARNING: Do NOT use this credentials to connect to a non-google service as
123  this could result in an oauth2 token leak.
124  - json_refresh_token is the JSON string containing the refresh token itself
125  along with a client_id and client_secret. */
126 grpc_credentials *grpc_refresh_token_credentials_create(
127  const char *json_refresh_token);
128 
129 /* Creates a fake transport security credentials object for testing. */
130 grpc_credentials *grpc_fake_transport_security_credentials_create(void);
131 
132 /* Creates an IAM credentials object. */
133 grpc_credentials *grpc_iam_credentials_create(const char *authorization_token,
134  const char *authority_selector);
135 
136 /* --- Secure channel creation. --- */
137 
138 /* The caller of the secure_channel_create functions may override the target
139  name used for SSL host name checking using this channel argument which is of
140  type GRPC_ARG_STRING. This *should* be used for testing only.
141  If this argument is not specified, the name used for SSL host name checking
142  will be the target parameter (assuming that the secure channel is an SSL
143  channel). If this parameter is specified and the underlying is not an SSL
144  channel, it will just be ignored. */
145 #define GRPC_SSL_TARGET_NAME_OVERRIDE_ARG "grpc.ssl_target_name_override"
146 
147 /* Creates a secure channel using the passed-in credentials. */
148 grpc_channel *grpc_secure_channel_create(grpc_credentials *creds,
149  const char *target,
150  const grpc_channel_args *args);
151 
152 /* --- grpc_server_credentials object. ---
153 
154  A server credentials object represents a way to authenticate a server. */
155 
157 
158 /* Releases a server_credentials object.
159  The creator of the server_credentials object is responsible for its release.
160  */
161 void grpc_server_credentials_release(grpc_server_credentials *creds);
162 
163 /* Creates an SSL server_credentials object.
164  - pem_roots_cert is the NULL-terminated string containing the PEM encoding of
165  the client root certificates. This parameter may be NULL if the server does
166  not want the client to be authenticated with SSL.
167  - pem_key_cert_pairs is an array private key / certificate chains of the
168  server. This parameter cannot be NULL.
169  - num_key_cert_pairs indicates the number of items in the private_key_files
170  and cert_chain_files parameters. It should be at least 1. */
171 grpc_server_credentials *grpc_ssl_server_credentials_create(
172  const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pairs,
173  size_t num_key_cert_pairs);
174 
175 /* Creates a fake server transport security credentials object for testing. */
176 grpc_server_credentials *grpc_fake_transport_security_server_credentials_create(
177  void);
178 
179 /* --- Server-side secure ports. --- */
180 
181 /* Add a HTTP2 over an encrypted link over tcp listener.
182  Returns bound port number on success, 0 on failure.
183  REQUIRES: server not started */
184 int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr,
185  grpc_server_credentials *creds);
186 
187 /* --- Call specific credentials. --- */
188 
189 /* Sets a credentials to a call. Can only be called on the client side before
190  grpc_call_start_batch. */
191 grpc_call_error grpc_call_set_credentials(grpc_call *call,
192  grpc_credentials *creds);
193 
194 #ifdef __cplusplus
195 }
196 #endif
197 
198 #endif /* GRPC_GRPC_SECURITY_H */
Definition: grpc.h:101
Definition: channel.c:52
Definition: credentials.h:174
Definition: credentials.h:111
Definition: grpc_security.h:66
Definition: time.h:48
Definition: call.c:128
Definition: server.c:127