Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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_SECURITY_H_ |
| 35 | #define GRPC_SECURITY_H_ |
| 36 | |
| 37 | #include "grpc.h" |
| 38 | #include "status.h" |
| 39 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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. */ |
| 55 | grpc_credentials *grpc_default_credentials_create(void); |
| 56 | |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 57 | /* Environment variable that points to the default SSL roots file. This file |
| 58 | must be a PEM encoded file with all the roots such as the one that can be |
| 59 | downloaded from https://pki.google.com/roots.pem. */ |
| 60 | #define GRPC_DEFAULT_SSL_ROOTS_FILE_PATH_ENV_VAR \ |
| 61 | "GRPC_DEFAULT_SSL_ROOTS_FILE_PATH" |
| 62 | |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 63 | /* Object that holds a private key / certificate chain pair in PEM format. */ |
| 64 | typedef struct { |
| 65 | /* private_key is the NULL-terminated string containing the PEM encoding of |
| 66 | the client's private key. */ |
| 67 | const char *private_key; |
| 68 | |
Julien Boeuf | 68ad53e | 2015-01-20 22:37:03 -0800 | [diff] [blame] | 69 | /* cert_chain is the NULL-terminated string containing the PEM encoding of |
| 70 | the client's certificate chain. */ |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 71 | const char *cert_chain; |
| 72 | } grpc_ssl_pem_key_cert_pair; |
| 73 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | /* Creates an SSL credentials object. |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 75 | - pem_roots_cert is the NULL-terminated string containing the PEM encoding |
| 76 | of the server root certificates. If this parameter is NULL, the default |
| 77 | roots will be used. |
| 78 | - pem_key_cert_pair is a pointer on the object containing client's private |
| 79 | key and certificate chain. This parameter can be NULL if the client does |
| 80 | not have such a key/cert pair. */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | grpc_credentials *grpc_ssl_credentials_create( |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 82 | const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pair); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | |
| 84 | /* Creates a composite credentials object. */ |
| 85 | grpc_credentials *grpc_composite_credentials_create(grpc_credentials *creds1, |
| 86 | grpc_credentials *creds2); |
| 87 | |
| 88 | /* Creates a compute engine credentials object. */ |
| 89 | grpc_credentials *grpc_compute_engine_credentials_create(void); |
| 90 | |
jboeuf | ab4f914 | 2014-12-16 16:32:39 -0800 | [diff] [blame] | 91 | extern const gpr_timespec grpc_max_auth_token_lifetime; |
jboeuf | befd265 | 2014-12-12 15:39:47 -0800 | [diff] [blame] | 92 | |
| 93 | /* Creates a service account credentials object. May return NULL if the input is |
jboeuf | ab4f914 | 2014-12-16 16:32:39 -0800 | [diff] [blame] | 94 | invalid. |
| 95 | - json_key is the JSON key string containing the client's private key. |
| 96 | - scope is a space-delimited list of the requested permissions. |
| 97 | - token_lifetime is the lifetime of each token acquired through this service |
| 98 | account credentials. It should not exceed grpc_max_auth_token_lifetime |
| 99 | or will be cropped to this value. */ |
| 100 | grpc_credentials *grpc_service_account_credentials_create( |
| 101 | const char *json_key, const char *scope, gpr_timespec token_lifetime); |
jboeuf | befd265 | 2014-12-12 15:39:47 -0800 | [diff] [blame] | 102 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 103 | /* Creates a fake transport security credentials object for testing. */ |
| 104 | grpc_credentials *grpc_fake_transport_security_credentials_create(void); |
| 105 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 106 | /* Creates an IAM credentials object. */ |
| 107 | grpc_credentials *grpc_iam_credentials_create(const char *authorization_token, |
| 108 | const char *authority_selector); |
| 109 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | /* --- Secure channel creation. --- */ |
| 111 | |
| 112 | /* The caller of the secure_channel_create functions may override the target |
| 113 | name used for SSL host name checking using this channel argument which is of |
| 114 | type GRPC_ARG_STRING. This *should* be used for testing only. |
| 115 | If this argument is not specified, the name used for SSL host name checking |
| 116 | will be the target parameter (assuming that the secure channel is an SSL |
| 117 | channel). If this parameter is specified and the underlying is not an SSL |
| 118 | channel, it will just be ignored. */ |
| 119 | #define GRPC_SSL_TARGET_NAME_OVERRIDE_ARG "grpc.ssl_target_name_override" |
| 120 | |
| 121 | /* Creates a default secure channel using the default credentials object using |
| 122 | the environment. */ |
| 123 | grpc_channel *grpc_default_secure_channel_create(const char *target, |
| 124 | const grpc_channel_args *args); |
| 125 | |
| 126 | /* Creates a secure channel using the passed-in credentials. */ |
| 127 | grpc_channel *grpc_secure_channel_create(grpc_credentials *creds, |
| 128 | const char *target, |
| 129 | const grpc_channel_args *args); |
| 130 | |
| 131 | /* --- grpc_server_credentials object. --- |
| 132 | |
| 133 | A server credentials object represents a way to authenticate a server. */ |
| 134 | |
| 135 | typedef struct grpc_server_credentials grpc_server_credentials; |
| 136 | |
| 137 | /* Releases a server_credentials object. |
| 138 | The creator of the server_credentials object is responsible for its release. |
| 139 | */ |
| 140 | void grpc_server_credentials_release(grpc_server_credentials *creds); |
| 141 | |
| 142 | /* Creates an SSL server_credentials object. |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 143 | - pem_roots_cert is the NULL-terminated string containing the PEM encoding of |
| 144 | the client root certificates. This parameter may be NULL if the server does |
| 145 | not want the client to be authenticated with SSL. |
| 146 | - pem_key_cert_pairs is an array private key / certificate chains of the |
| 147 | server. This parameter cannot be NULL. |
| 148 | - num_key_cert_pairs indicates the number of items in the private_key_files |
| 149 | and cert_chain_files parameters. It should be at least 1. */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 150 | grpc_server_credentials *grpc_ssl_server_credentials_create( |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 151 | const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pairs, |
| 152 | size_t num_key_cert_pairs); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 153 | |
| 154 | /* Creates a fake server transport security credentials object for testing. */ |
| 155 | grpc_server_credentials *grpc_fake_transport_security_server_credentials_create( |
| 156 | void); |
| 157 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 158 | /* --- Secure server creation. --- */ |
| 159 | |
| 160 | /* Creates a secure server using the passed-in server credentials. */ |
| 161 | grpc_server *grpc_secure_server_create(grpc_server_credentials *creds, |
| 162 | grpc_completion_queue *cq, |
| 163 | const grpc_channel_args *args); |
| 164 | |
Craig Tiller | d251ab9 | 2015-02-17 17:22:14 -0800 | [diff] [blame] | 165 | /* Add a HTTP2 over an encrypted link over tcp listener. |
| 166 | Server must have been created with grpc_secure_server_create. |
| 167 | Returns bound port number on success, 0 on failure. |
| 168 | REQUIRES: server not started */ |
| 169 | int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr); |
| 170 | |
| 171 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 172 | #ifdef __cplusplus |
| 173 | } |
| 174 | #endif |
| 175 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame^] | 176 | #endif /* GRPC_SECURITY_H_ */ |