blob: 3117571d9f7d8ec2f5e6d944480176b80885c422 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * 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
Craig Tiller730ddc22017-03-29 08:38:47 -070034#ifndef GRPC_CORE_TSI_SSL_TRANSPORT_SECURITY_H
35#define GRPC_CORE_TSI_SSL_TRANSPORT_SECURITY_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
Craig Tillerb29f1fe2017-03-28 15:49:23 -070037#include "src/core/tsi/transport_security_interface.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038
39#ifdef __cplusplus
Craig Tillera82950e2015-09-22 12:33:20 -070040extern "C" {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#endif
42
43/* Value for the TSI_CERTIFICATE_TYPE_PEER_PROPERTY property for X509 certs. */
44#define TSI_X509_CERTIFICATE_TYPE "X509"
45
jboeufc2125852015-01-12 16:42:28 -080046/* This property is of type TSI_PEER_PROPERTY_STRING. */
47#define TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY "x509_subject_common_name"
Julien Boeuf77e8c1c2015-05-13 13:50:59 -070048#define TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY \
49 "x509_subject_alternative_name"
jboeufc2125852015-01-12 16:42:28 -080050
Deepak Lukosee61cbe32016-03-14 14:10:44 -070051#define TSI_X509_PEM_CERT_PROPERTY "x509_pem_cert"
52
jboeufc2125852015-01-12 16:42:28 -080053#define TSI_SSL_ALPN_SELECTED_PROTOCOL "ssl_alpn_selected_protocol"
54
Julien Boeuf935d02e2017-04-09 00:07:09 -070055/* --- tsi_ssl_client_handshaker_factory object ---
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056
Julien Boeuf935d02e2017-04-09 00:07:09 -070057 This object creates a client tsi_handshaker objects implemented in terms of
58 the TLS 1.2 specificiation. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059
Julien Boeuf935d02e2017-04-09 00:07:09 -070060typedef struct tsi_ssl_client_handshaker_factory
61 tsi_ssl_client_handshaker_factory;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062
Julien Boeufb71ef652017-04-12 21:44:49 -070063/* Object that holds a private key / certificate chain pair in PEM format. */
64typedef 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
69 /* cert_chain is the NULL-terminated string containing the PEM encoding of
70 the client's certificate chain. */
71 const char *cert_chain;
72} tsi_ssl_pem_key_cert_pair;
73
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074/* Creates a client handshaker factory.
Julien Boeufb71ef652017-04-12 21:44:49 -070075 - pem_key_cert_pair is a pointer to the object containing client's private
76 key and certificate chain. This parameter can be NULL if the client does
77 not have such a key/cert pair.
78 - pem_roots_cert is the NULL-terminated string containing the PEM encoding of
79 the client root certificates. This parameter may be NULL if the server does
80 not want the client to be authenticated with SSL.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081 - cipher_suites contains an optional list of the ciphers that the client
82 supports. The format of this string is described in:
83 https://www.openssl.org/docs/apps/ciphers.html.
84 This parameter can be set to NULL to use the default set of ciphers.
85 TODO(jboeuf): Revisit the format of this parameter.
Julien Boeufb71ef652017-04-12 21:44:49 -070086 - alpn_protocols is an array containing the NULL terminated protocol names
87 that the handshakers created with this factory support. This parameter can
88 be NULL.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080089 - num_alpn_protocols is the number of alpn protocols and associated lengths
90 specified. If this parameter is 0, the other alpn parameters must be NULL.
91 - factory is the address of the factory pointer to be created.
92
93 - This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
94 where a parameter is invalid. */
Craig Tillera82950e2015-09-22 12:33:20 -070095tsi_result tsi_create_ssl_client_handshaker_factory(
Julien Boeufb71ef652017-04-12 21:44:49 -070096 const tsi_ssl_pem_key_cert_pair *pem_key_cert_pair,
97 const char *pem_root_certs, const char *cipher_suites,
98 const char **alpn_protocols, uint16_t num_alpn_protocols,
Julien Boeuf935d02e2017-04-09 00:07:09 -070099 tsi_ssl_client_handshaker_factory **factory);
100
101/* Creates a client handshaker.
102 - self is the factory from which the handshaker will be created.
103 - server_name_indication indicates the name of the server the client is
104 trying to connect to which will be relayed to the server using the SNI
105 extension.
106 - handshaker is the address of the handshaker pointer to be created.
107
108 - This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
109 where a parameter is invalid. */
110tsi_result tsi_ssl_client_handshaker_factory_create_handshaker(
111 tsi_ssl_client_handshaker_factory *self, const char *server_name_indication,
112 tsi_handshaker **handshaker);
113
114/* Destroys the handshaker factory. WARNING: it is unsafe to destroy a factory
115 while handshakers created with this factory are still in use. */
116void tsi_ssl_client_handshaker_factory_destroy(
117 tsi_ssl_client_handshaker_factory *self);
118
119/* --- tsi_ssl_server_handshaker_factory object ---
120
121 This object creates a client tsi_handshaker objects implemented in terms of
122 the TLS 1.2 specificiation. */
123
124typedef struct tsi_ssl_server_handshaker_factory
125 tsi_ssl_server_handshaker_factory;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126
127/* Creates a server handshaker factory.
Julien Boeufb71ef652017-04-12 21:44:49 -0700128 - pem_key_cert_pairs is an array private key / certificate chains of the
129 server.
130 - num_key_cert_pairs is the number of items in the pem_key_cert_pairs array.
131 - pem_root_certs is the NULL-terminated string containing the PEM encoding
132 of the server root certificates.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800133 - cipher_suites contains an optional list of the ciphers that the server
134 supports. The format of this string is described in:
135 https://www.openssl.org/docs/apps/ciphers.html.
136 This parameter can be set to NULL to use the default set of ciphers.
137 TODO(jboeuf): Revisit the format of this parameter.
Julien Boeufb71ef652017-04-12 21:44:49 -0700138 - alpn_protocols is an array containing the NULL terminated protocol names
139 that the handshakers created with this factory support. This parameter can
140 be NULL.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141 - num_alpn_protocols is the number of alpn protocols and associated lengths
142 specified. If this parameter is 0, the other alpn parameters must be NULL.
143 - factory is the address of the factory pointer to be created.
144
145 - This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
146 where a parameter is invalid. */
Craig Tillera82950e2015-09-22 12:33:20 -0700147tsi_result tsi_create_ssl_server_handshaker_factory(
Julien Boeufb71ef652017-04-12 21:44:49 -0700148 const tsi_ssl_pem_key_cert_pair *pem_key_cert_pairs,
149 size_t num_key_cert_pairs, const char *pem_client_root_certs,
150 int force_client_auth, const char *cipher_suites,
151 const char **alpn_protocols, uint16_t num_alpn_protocols,
Julien Boeuf935d02e2017-04-09 00:07:09 -0700152 tsi_ssl_server_handshaker_factory **factory);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800153
Deepak Lukosedba4c5f2016-03-25 12:54:25 -0700154/* Same as tsi_create_ssl_server_handshaker_factory method except uses
155 tsi_client_certificate_request_type to support more ways to handle client
156 certificate authentication.
157 - client_certificate_request, if set to non-zero will force the client to
158 authenticate with an SSL cert. Note that this option is ignored if
159 pem_client_root_certs is NULL or pem_client_roots_certs_size is 0 */
160tsi_result tsi_create_ssl_server_handshaker_factory_ex(
Julien Boeufb71ef652017-04-12 21:44:49 -0700161 const tsi_ssl_pem_key_cert_pair *pem_key_cert_pairs,
162 size_t num_key_cert_pairs, const char *pem_client_root_certs,
Deepak Lukosedba4c5f2016-03-25 12:54:25 -0700163 tsi_client_certificate_request_type client_certificate_request,
Julien Boeufb71ef652017-04-12 21:44:49 -0700164 const char *cipher_suites, const char **alpn_protocols,
165 uint16_t num_alpn_protocols, tsi_ssl_server_handshaker_factory **factory);
Deepak Lukosedba4c5f2016-03-25 12:54:25 -0700166
Julien Boeuf935d02e2017-04-09 00:07:09 -0700167/* Creates a server handshaker.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800168 - self is the factory from which the handshaker will be created.
Julien Boeuf935d02e2017-04-09 00:07:09 -0700169 - handshaker is the address of the handshaker pointer to be created.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800170
171 - This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
172 where a parameter is invalid. */
Julien Boeuf935d02e2017-04-09 00:07:09 -0700173tsi_result tsi_ssl_server_handshaker_factory_create_handshaker(
174 tsi_ssl_server_handshaker_factory *self, tsi_handshaker **handshaker);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800175
176/* Destroys the handshaker factory. WARNING: it is unsafe to destroy a factory
177 while handshakers created with this factory are still in use. */
Julien Boeuf935d02e2017-04-09 00:07:09 -0700178void tsi_ssl_server_handshaker_factory_destroy(
179 tsi_ssl_server_handshaker_factory *self);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180
Julien Boeuf9fff77e2015-02-24 16:50:35 -0800181/* Util that checks that an ssl peer matches a specific name.
182 Still TODO(jboeuf):
183 - handle mixed case.
184 - handle %encoded chars.
Paul Querna47d841d2016-03-10 11:19:17 -0800185 - handle public suffix wildchar more strictly (e.g. *.co.uk) */
Craig Tillera82950e2015-09-22 12:33:20 -0700186int tsi_ssl_peer_matches_name(const tsi_peer *peer, const char *name);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800188#ifdef __cplusplus
189}
190#endif
191
Craig Tiller730ddc22017-03-29 08:38:47 -0700192#endif /* GRPC_CORE_TSI_SSL_TRANSPORT_SECURITY_H */