blob: 1f91e6527861cf3f6e773d89ea27fa3867f657cd [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPC_GRPC_SECURITY_H
35#define GRPC_GRPC_SECURITY_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
Nicolas "Pixel" Noble1ed15e22015-06-09 02:24:35 +020037#include <grpc/grpc.h>
38#include <grpc/status.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039
nnoble0c475f02014-12-05 15:37:39 -080040#ifdef __cplusplus
41extern "C" {
42#endif
43
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044/* --- grpc_credentials object. ---
45
46 A credentials object represents a way to authenticate a client. */
47
48typedef struct grpc_credentials grpc_credentials;
49
50/* Releases a credentials object.
51 The creator of the credentials object is responsible for its release. */
52void grpc_credentials_release(grpc_credentials *creds);
53
Julien Boeufc66f2a82015-02-23 13:00:36 -080054/* 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. */
57grpc_credentials *grpc_google_default_credentials_create(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058
Julien Boeuf026a4172015-02-02 18:36:37 -080059/* 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
Julien Boeuf8fbcc432015-01-15 16:44:13 -080065/* Object that holds a private key / certificate chain pair in PEM format. */
66typedef 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
Julien Boeuf68ad53e2015-01-20 22:37:03 -080071 /* cert_chain is the NULL-terminated string containing the PEM encoding of
72 the client's certificate chain. */
Julien Boeuf8fbcc432015-01-15 16:44:13 -080073 const char *cert_chain;
74} grpc_ssl_pem_key_cert_pair;
75
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076/* Creates an SSL credentials object.
Julien Boeuf8fbcc432015-01-15 16:44:13 -080077 - pem_roots_cert is the NULL-terminated string containing the PEM encoding
Julien Boeuf3e001792015-02-20 15:02:36 -080078 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).
Julien Boeuf8fbcc432015-01-15 16:44:13 -080083 - 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. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086grpc_credentials *grpc_ssl_credentials_create(
Julien Boeuf8fbcc432015-01-15 16:44:13 -080087 const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pair);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088
89/* Creates a composite credentials object. */
90grpc_credentials *grpc_composite_credentials_create(grpc_credentials *creds1,
91 grpc_credentials *creds2);
92
Julien Boeufc66f2a82015-02-23 13:00:36 -080093/* 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. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080096grpc_credentials *grpc_compute_engine_credentials_create(void);
97
jboeufab4f9142014-12-16 16:32:39 -080098extern const gpr_timespec grpc_max_auth_token_lifetime;
jboeufbefd2652014-12-12 15:39:47 -080099
100/* Creates a service account credentials object. May return NULL if the input is
jboeufab4f9142014-12-16 16:32:39 -0800101 invalid.
Julien Boeufc66f2a82015-02-23 13:00:36 -0800102 WARNING: Do NOT use this credentials to connect to a non-google service as
103 this could result in an oauth2 token leak.
jboeufab4f9142014-12-16 16:32:39 -0800104 - 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. */
109grpc_credentials *grpc_service_account_credentials_create(
110 const char *json_key, const char *scope, gpr_timespec token_lifetime);
jboeufbefd2652014-12-12 15:39:47 -0800111
Julien Boeuff47a5cb2015-02-18 12:24:08 -0800112/* 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. */
117grpc_credentials *grpc_jwt_credentials_create(const char *json_key,
118 gpr_timespec token_lifetime);
119
Eric Dobsona6124ec2015-06-08 11:17:42 -0700120/* Creates an Oauth2 Refresh Token credentials object. May return NULL if the
Julien Boeuf9835cf02015-03-09 16:56:44 -0700121 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. */
126grpc_credentials *grpc_refresh_token_credentials_create(
127 const char *json_refresh_token);
128
Julien Boeuf2805be12015-07-01 02:47:18 -0700129/* Creates an Oauth2 Access Token credentials with an access token that was
130 aquired by an out of band mechanism. */
131grpc_credentials *grpc_access_token_credentials_create(
132 const char *access_token);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800133
nnoble0c475f02014-12-05 15:37:39 -0800134/* Creates an IAM credentials object. */
135grpc_credentials *grpc_iam_credentials_create(const char *authorization_token,
136 const char *authority_selector);
137
Julien Boeuf2805be12015-07-01 02:47:18 -0700138/* Creates a fake transport security credentials object for testing. */
139grpc_credentials *grpc_fake_transport_security_credentials_create(void);
140
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141/* --- Secure channel creation. --- */
142
143/* The caller of the secure_channel_create functions may override the target
144 name used for SSL host name checking using this channel argument which is of
145 type GRPC_ARG_STRING. This *should* be used for testing only.
146 If this argument is not specified, the name used for SSL host name checking
147 will be the target parameter (assuming that the secure channel is an SSL
148 channel). If this parameter is specified and the underlying is not an SSL
149 channel, it will just be ignored. */
150#define GRPC_SSL_TARGET_NAME_OVERRIDE_ARG "grpc.ssl_target_name_override"
151
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800152/* Creates a secure channel using the passed-in credentials. */
153grpc_channel *grpc_secure_channel_create(grpc_credentials *creds,
154 const char *target,
155 const grpc_channel_args *args);
156
157/* --- grpc_server_credentials object. ---
158
159 A server credentials object represents a way to authenticate a server. */
160
161typedef struct grpc_server_credentials grpc_server_credentials;
162
163/* Releases a server_credentials object.
164 The creator of the server_credentials object is responsible for its release.
165 */
166void grpc_server_credentials_release(grpc_server_credentials *creds);
167
168/* Creates an SSL server_credentials object.
Julien Boeuf8fbcc432015-01-15 16:44:13 -0800169 - pem_roots_cert is the NULL-terminated string containing the PEM encoding of
170 the client root certificates. This parameter may be NULL if the server does
171 not want the client to be authenticated with SSL.
172 - pem_key_cert_pairs is an array private key / certificate chains of the
173 server. This parameter cannot be NULL.
174 - num_key_cert_pairs indicates the number of items in the private_key_files
175 and cert_chain_files parameters. It should be at least 1. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800176grpc_server_credentials *grpc_ssl_server_credentials_create(
Julien Boeuf8fbcc432015-01-15 16:44:13 -0800177 const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pairs,
178 size_t num_key_cert_pairs);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800179
180/* Creates a fake server transport security credentials object for testing. */
181grpc_server_credentials *grpc_fake_transport_security_server_credentials_create(
182 void);
183
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800184/* --- Server-side secure ports. --- */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800185
Craig Tillerd251ab92015-02-17 17:22:14 -0800186/* Add a HTTP2 over an encrypted link over tcp listener.
Craig Tillerd251ab92015-02-17 17:22:14 -0800187 Returns bound port number on success, 0 on failure.
188 REQUIRES: server not started */
Craig Tiller759026c2015-02-22 23:09:45 -0800189int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr,
190 grpc_server_credentials *creds);
Craig Tillerd251ab92015-02-17 17:22:14 -0800191
Julien Boeuf9f218dd2015-04-23 10:24:02 -0700192/* --- Call specific credentials. --- */
193
194/* Sets a credentials to a call. Can only be called on the client side before
195 grpc_call_start_batch. */
196grpc_call_error grpc_call_set_credentials(grpc_call *call,
197 grpc_credentials *creds);
198
Julien Boeuf84d964a2015-04-29 11:31:06 -0700199/* --- Authentication Context. --- */
200
201/* TODO(jboeuf): Define some well-known property names. */
202
Craig Tiller9a576332015-06-17 10:21:49 -0700203#define GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME "transport_security_type"
Julien Boeuf84d964a2015-04-29 11:31:06 -0700204#define GRPC_FAKE_TRANSPORT_SECURITY_TYPE "fake"
205#define GRPC_SSL_TRANSPORT_SECURITY_TYPE "ssl"
206
207#define GRPC_X509_CN_PROPERTY_NAME "x509_common_name"
208#define GRPC_X509_SAN_PROPERTY_NAME "x509_subject_alternative_name"
209
210typedef struct grpc_auth_context grpc_auth_context;
Julien Boeuf83b02972015-05-20 22:50:34 -0700211
212typedef struct grpc_auth_property_iterator {
213 const grpc_auth_context *ctx;
214 size_t index;
215 const char *name;
216} grpc_auth_property_iterator;
Julien Boeuf84d964a2015-04-29 11:31:06 -0700217
218/* value, if not NULL, is guaranteed to be NULL terminated. */
219typedef struct grpc_auth_property {
220 char *name;
221 char *value;
222 size_t value_length;
223} grpc_auth_property;
224
225/* Returns NULL when the iterator is at the end. */
226const grpc_auth_property *grpc_auth_property_iterator_next(
227 grpc_auth_property_iterator *it);
Julien Boeuf84d964a2015-04-29 11:31:06 -0700228
Julien Boeuf83b02972015-05-20 22:50:34 -0700229/* Iterates over the auth context. */
230grpc_auth_property_iterator grpc_auth_context_property_iterator(
Julien Boeuf84d964a2015-04-29 11:31:06 -0700231 const grpc_auth_context *ctx);
232
Julien Boeuf83b02972015-05-20 22:50:34 -0700233/* Gets the peer identity. Returns an empty iterator (first _next will return
234 NULL) if the peer is not authenticated. */
235grpc_auth_property_iterator grpc_auth_context_peer_identity(
Julien Boeuf84d964a2015-04-29 11:31:06 -0700236 const grpc_auth_context *ctx);
237
Julien Boeuf83b02972015-05-20 22:50:34 -0700238/* Finds a property in the context. May return an empty iterator (first _next
239 will return NULL) if no property with this name was found in the context. */
240grpc_auth_property_iterator grpc_auth_context_find_properties_by_name(
Julien Boeuf84d964a2015-04-29 11:31:06 -0700241 const grpc_auth_context *ctx, const char *name);
242
243/* Gets the name of the property that indicates the peer identity. Will return
244 NULL if the peer is not authenticated. */
245const char *grpc_auth_context_peer_identity_property_name(
246 const grpc_auth_context *ctx);
247
Julien Boeuf83b02972015-05-20 22:50:34 -0700248/* Returns 1 if the peer is authenticated, 0 otherwise. */
249int grpc_auth_context_peer_is_authenticated(const grpc_auth_context *ctx);
250
Julien Boeuf84d964a2015-04-29 11:31:06 -0700251/* Gets the auth context from the call. */
252const grpc_auth_context *grpc_call_auth_context(grpc_call *call);
253
nnoble0c475f02014-12-05 15:37:39 -0800254#ifdef __cplusplus
255}
256#endif
257
Craig Tiller9a576332015-06-17 10:21:49 -0700258#endif /* GRPC_GRPC_SECURITY_H */