Debian Codegraph @

src/core/tsi/ssl_transport_security.h

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #ifndef GRPC_CORE_TSI_SSL_TRANSPORT_SECURITY_H
20 #define GRPC_CORE_TSI_SSL_TRANSPORT_SECURITY_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <openssl/x509.h>
25
26 #include "absl/strings/string_view.h"
27
28 #include <grpc/grpc_security_constants.h>
29
30 #include "src/core/tsi/ssl/key_logging/ssl_key_logging.h"
31 #include "src/core/tsi/transport_security_interface.h"
32
33 /* Value for the TSI_CERTIFICATE_TYPE_PEER_PROPERTY property for X509 certs. */
34 #define TSI_X509_CERTIFICATE_TYPE "X509"
35
36 /* This property is of type TSI_PEER_PROPERTY_STRING. */
37 #define TSI_X509_SUBJECT_PEER_PROPERTY "x509_subject"
38 #define TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY "x509_subject_common_name"
39 #define TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY \
40 "x509_subject_alternative_name"
41 #define TSI_SSL_SESSION_REUSED_PEER_PROPERTY "ssl_session_reused"
42 #define TSI_X509_PEM_CERT_PROPERTY "x509_pem_cert"
43 #define TSI_X509_PEM_CERT_CHAIN_PROPERTY "x509_pem_cert_chain"
44 #define TSI_SSL_ALPN_SELECTED_PROTOCOL "ssl_alpn_selected_protocol"
45 #define TSI_X509_DNS_PEER_PROPERTY "x509_dns"
46 #define TSI_X509_URI_PEER_PROPERTY "x509_uri"
47 #define TSI_X509_EMAIL_PEER_PROPERTY "x509_email"
48 #define TSI_X509_IP_PEER_PROPERTY "x509_ip"
49
50 /* --- tsi_ssl_root_certs_store object ---
51
52 This object stores SSL root certificates. It can be shared by multiple SSL
53 context. */
54 typedef struct tsi_ssl_root_certs_store tsi_ssl_root_certs_store;
55
56 /* Given a NULL-terminated string containing the PEM encoding of the root
57 certificates, creates a tsi_ssl_root_certs_store object. */
58 tsi_ssl_root_certs_store* tsi_ssl_root_certs_store_create(
59 const char* pem_roots);
60
61 /* Destroys the tsi_ssl_root_certs_store object. */
62 void tsi_ssl_root_certs_store_destroy(tsi_ssl_root_certs_store* self);
63
64 /* --- tsi_ssl_session_cache object ---
65
66 Cache for SSL sessions for sessions resumption. */
67
68 typedef struct tsi_ssl_session_cache tsi_ssl_session_cache;
69
70 /* Create LRU cache for SSL sessions with \a capacity. */
71 tsi_ssl_session_cache* tsi_ssl_session_cache_create_lru(size_t capacity);
72
73 /* Increment reference counter of \a cache. */
74 void tsi_ssl_session_cache_ref(tsi_ssl_session_cache* cache);
75
76 /* Decrement reference counter of \a cache. */
77 void tsi_ssl_session_cache_unref(tsi_ssl_session_cache* cache);
78
79 /* --- tsi_ssl_key_logger object ---
80
81 Experimental SSL Key logging functionality to enable decryption of
82 packet captures. */
83 static constexpr bool tsi_tls_session_key_logging_supported() {
84 // Supported only for open-ssl versions >= 1.1.1
85 #if OPENSSL_VERSION_NUMBER >= 0x10101000 && !defined(LIBRESSL_VERSION_NUMBER)
86 return true;
87 #else
88 return false;
89 #endif
90 }
91
92 /* --- tsi_ssl_client_handshaker_factory object ---
93
94 This object creates a client tsi_handshaker objects implemented in terms of
95 the TLS 1.2 specificiation. */
96
97 typedef struct tsi_ssl_client_handshaker_factory
98 tsi_ssl_client_handshaker_factory;
99
100 /* Object that holds a private key / certificate chain pair in PEM format. */
101 struct tsi_ssl_pem_key_cert_pair {
102 /* private_key is the NULL-terminated string containing the PEM encoding of
103 the client's private key. */
104 const char* private_key;
105
106 /* cert_chain is the NULL-terminated string containing the PEM encoding of
107 the client's certificate chain. */
108 const char* cert_chain;
109 };
110 /* TO BE DEPRECATED.
111 Creates a client handshaker factory.
112 - pem_key_cert_pair is a pointer to the object containing client's private
113 key and certificate chain. This parameter can be NULL if the client does
114 not have such a key/cert pair.
115 - pem_roots_cert is the NULL-terminated string containing the PEM encoding of
116 the server root certificates.
117 - cipher_suites contains an optional list of the ciphers that the client
118 supports. The format of this string is described in:
119 https://www.openssl.org/docs/apps/ciphers.html.
120 This parameter can be set to NULL to use the default set of ciphers.
121 TODO(jboeuf): Revisit the format of this parameter.
122 - alpn_protocols is an array containing the NULL terminated protocol names
123 that the handshakers created with this factory support. This parameter can
124 be NULL.
125 - num_alpn_protocols is the number of alpn protocols and associated lengths
126 specified. If this parameter is 0, the other alpn parameters must be NULL.
127 - factory is the address of the factory pointer to be created.
128
129 - This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
130 where a parameter is invalid. */
131 tsi_result tsi_create_ssl_client_handshaker_factory(
132 const tsi_ssl_pem_key_cert_pair* pem_key_cert_pair,
133 const char* pem_root_certs, const char* cipher_suites,
134 const char** alpn_protocols, uint16_t num_alpn_protocols,
135 tsi_ssl_client_handshaker_factory** factory);
136
137 struct tsi_ssl_client_handshaker_options {
138 /* pem_key_cert_pair is a pointer to the object containing client's private
139 key and certificate chain. This parameter can be NULL if the client does
140 not have such a key/cert pair. */
141 const tsi_ssl_pem_key_cert_pair* pem_key_cert_pair;
142 /* pem_roots_cert is the NULL-terminated string containing the PEM encoding of
143 the client root certificates. */
144 const char* pem_root_certs;
145 /* root_store is a pointer to the ssl_root_certs_store object. If root_store
146 is not nullptr and SSL implementation permits, root_store will be used as
147 root certificates. Otherwise, pem_roots_cert will be used to load server
148 root certificates. */
149 const tsi_ssl_root_certs_store* root_store;
150 /* cipher_suites contains an optional list of the ciphers that the client
151 supports. The format of this string is described in:
152 https://www.openssl.org/docs/apps/ciphers.html.
153 This parameter can be set to NULL to use the default set of ciphers.
154 TODO(jboeuf): Revisit the format of this parameter. */
155 const char* cipher_suites;
156 /* alpn_protocols is an array containing the NULL terminated protocol names
157 that the handshakers created with this factory support. This parameter can
158 be NULL. */
159 const char** alpn_protocols;
160 /* num_alpn_protocols is the number of alpn protocols and associated lengths
161 specified. If this parameter is 0, the other alpn parameters must be
162 NULL. */
163 size_t num_alpn_protocols;
164 /* ssl_session_cache is a cache for reusable client-side sessions. */
165 tsi_ssl_session_cache* session_cache;
166 /* tsi_ssl_key_logger is an instance used to log SSL keys to a file. */
167 tsi::TlsSessionKeyLoggerCache::TlsSessionKeyLogger* key_logger;
168
169 /* skip server certificate verification. */
170 bool skip_server_certificate_verification;
171
172 /* The min and max TLS versions that will be negotiated by the handshaker. */
173 tsi_tls_version min_tls_version;
174 tsi_tls_version max_tls_version;
175
176 /* The directory where all hashed CRL files enforced by the handshaker are
177 located. If the directory is invalid, CRL checking will fail open and just
178 log. An empty directory will not enable crl checking. Only OpenSSL version
179 > 1.1 is supported for CRL checking*/
180 const char* crl_directory;
181
182 tsi_ssl_client_handshaker_options()
183 : pem_key_cert_pair(nullptr),
184 pem_root_certs(nullptr),
185 root_store(nullptr),
186 cipher_suites(nullptr),
187 alpn_protocols(nullptr),
188 num_alpn_protocols(0),
189 session_cache(nullptr),
190 key_logger(nullptr),
191 skip_server_certificate_verification(false),
192 min_tls_version(tsi_tls_version::TSI_TLS1_2),
193 max_tls_version(tsi_tls_version::TSI_TLS1_3),
194 crl_directory(nullptr) {}
195 };
196
197 /* Creates a client handshaker factory.
198 - options is the options used to create a factory.
199 - factory is the address of the factory pointer to be created.
200
201 - This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
202 where a parameter is invalid. */
203 tsi_result tsi_create_ssl_client_handshaker_factory_with_options(
204 const tsi_ssl_client_handshaker_options* options,
205 tsi_ssl_client_handshaker_factory** factory);
206
207 /* Creates a client handshaker.
208 - factory is the factory from which the handshaker will be created.
209 - server_name_indication indicates the name of the server the client is
210 trying to connect to which will be relayed to the server using the SNI
211 extension.
212 - network_bio_buf_size and ssl_bio_buf_size represent BIO pair buffers used in
213 SSL. The buffer size being 0 translates to 17KB in boringSSL.
214 - handshaker is the address of the handshaker pointer to be created.
215
216 - This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
217 where a parameter is invalid. */
218 tsi_result tsi_ssl_client_handshaker_factory_create_handshaker(
219 tsi_ssl_client_handshaker_factory* factory,
220 const char* server_name_indication, size_t network_bio_buf_size,
221 size_t ssl_bio_buf_size, tsi_handshaker** handshaker);
222
223 /* Decrements reference count of the handshaker factory. Handshaker factory will
224 * be destroyed once no references exist. */
225 void tsi_ssl_client_handshaker_factory_unref(
226 tsi_ssl_client_handshaker_factory* factory);
227
228 /* --- tsi_ssl_server_handshaker_factory object ---
229
230 This object creates a client tsi_handshaker objects implemented in terms of
231 the TLS 1.2 specificiation. */
232
233 typedef struct tsi_ssl_server_handshaker_factory
234 tsi_ssl_server_handshaker_factory;
235
236 /* TO BE DEPRECATED.
237 Creates a server handshaker factory.
238 - pem_key_cert_pairs is an array private key / certificate chains of the
239 server.
240 - num_key_cert_pairs is the number of items in the pem_key_cert_pairs array.
241 - pem_root_certs is the NULL-terminated string containing the PEM encoding
242 of the client root certificates. This parameter may be NULL if the server
243 does not want the client to be authenticated with SSL.
244 - cipher_suites contains an optional list of the ciphers that the server
245 supports. The format of this string is described in:
246 https://www.openssl.org/docs/apps/ciphers.html.
247 This parameter can be set to NULL to use the default set of ciphers.
248 TODO(jboeuf): Revisit the format of this parameter.
249 - alpn_protocols is an array containing the NULL terminated protocol names
250 that the handshakers created with this factory support. This parameter can
251 be NULL.
252 - num_alpn_protocols is the number of alpn protocols and associated lengths
253 specified. If this parameter is 0, the other alpn parameters must be NULL.
254 - factory is the address of the factory pointer to be created.
255
256 - This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
257 where a parameter is invalid. */
258 tsi_result tsi_create_ssl_server_handshaker_factory(
259 const tsi_ssl_pem_key_cert_pair* pem_key_cert_pairs,
260 size_t num_key_cert_pairs, const char* pem_client_root_certs,
261 int force_client_auth, const char* cipher_suites,
262 const char** alpn_protocols, uint16_t num_alpn_protocols,
263 tsi_ssl_server_handshaker_factory** factory);
264
265 /* TO BE DEPRECATED.
266 Same as tsi_create_ssl_server_handshaker_factory method except uses
267 tsi_client_certificate_request_type to support more ways to handle client
268 certificate authentication.
269 - client_certificate_request, if set to non-zero will force the client to
270 authenticate with an SSL cert. Note that this option is ignored if
271 pem_client_root_certs is NULL or pem_client_roots_certs_size is 0 */
272 tsi_result tsi_create_ssl_server_handshaker_factory_ex(
273 const tsi_ssl_pem_key_cert_pair* pem_key_cert_pairs,
274 size_t num_key_cert_pairs, const char* pem_client_root_certs,
275 tsi_client_certificate_request_type client_certificate_request,
276 const char* cipher_suites, const char** alpn_protocols,
277 uint16_t num_alpn_protocols, tsi_ssl_server_handshaker_factory** factory);
278
279 struct tsi_ssl_server_handshaker_options {
280 /* pem_key_cert_pairs is an array private key / certificate chains of the
281 server. */
282 const tsi_ssl_pem_key_cert_pair* pem_key_cert_pairs;
283 /* num_key_cert_pairs is the number of items in the pem_key_cert_pairs
284 array. */
285 size_t num_key_cert_pairs;
286 /* pem_root_certs is the NULL-terminated string containing the PEM encoding
287 of the server root certificates. This parameter may be NULL if the server
288 does not want the client to be authenticated with SSL. */
289 const char* pem_client_root_certs;
290 /* client_certificate_request, if set to non-zero will force the client to
291 authenticate with an SSL cert. Note that this option is ignored if
292 pem_client_root_certs is NULL or pem_client_roots_certs_size is 0. */
293 tsi_client_certificate_request_type client_certificate_request;
294 /* cipher_suites contains an optional list of the ciphers that the server
295 supports. The format of this string is described in:
296 https://www.openssl.org/docs/apps/ciphers.html.
297 This parameter can be set to NULL to use the default set of ciphers.
298 TODO(jboeuf): Revisit the format of this parameter. */
299 const char* cipher_suites;
300 /* alpn_protocols is an array containing the NULL terminated protocol names
301 that the handshakers created with this factory support. This parameter can
302 be NULL. */
303 const char** alpn_protocols;
304 /* num_alpn_protocols is the number of alpn protocols and associated lengths
305 specified. If this parameter is 0, the other alpn parameters must be
306 NULL. */
307 uint16_t num_alpn_protocols;
308 /* session_ticket_key is optional key for encrypting session keys. If
309 parameter is not specified it must be NULL. */
310 const char* session_ticket_key;
311 /* session_ticket_key_size is a size of session ticket encryption key. */
312 size_t session_ticket_key_size;
313 /* The min and max TLS versions that will be negotiated by the handshaker. */
314 tsi_tls_version min_tls_version;
315 tsi_tls_version max_tls_version;
316 /* tsi_ssl_key_logger is an instance used to log SSL keys to a file. */
317 tsi::TlsSessionKeyLoggerCache::TlsSessionKeyLogger* key_logger;
318
319 /* The directory where all hashed CRL files are cached in the x.509 store and
320 * enforced by the handshaker are located. If the directory is invalid, CRL
321 * checking will fail open and just log. An empty directory will not enable
322 * crl checking. Only OpenSSL version > 1.1 is supported for CRL checking */
323 const char* crl_directory;
324
325 tsi_ssl_server_handshaker_options()
326 : pem_key_cert_pairs(nullptr),
327 num_key_cert_pairs(0),
328 pem_client_root_certs(nullptr),
329 client_certificate_request(TSI_DONT_REQUEST_CLIENT_CERTIFICATE),
330 cipher_suites(nullptr),
331 alpn_protocols(nullptr),
332 num_alpn_protocols(0),
333 session_ticket_key(nullptr),
334 session_ticket_key_size(0),
335 min_tls_version(tsi_tls_version::TSI_TLS1_2),
336 max_tls_version(tsi_tls_version::TSI_TLS1_3),
337 key_logger(nullptr),
338 crl_directory(nullptr) {}
339 };
340
341 /* Creates a server handshaker factory.
342 - options is the options used to create a factory.
343 - factory is the address of the factory pointer to be created.
344
345 - This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
346 where a parameter is invalid. */
347 tsi_result tsi_create_ssl_server_handshaker_factory_with_options(
348 const tsi_ssl_server_handshaker_options* options,
349 tsi_ssl_server_handshaker_factory** factory);
350
351 /* Creates a server handshaker.
352 - factory is the factory from which the handshaker will be created.
353 - network_bio_buf_size and ssl_bio_buf_size represent BIO pair buffers used in
354 SSL. The buffer size being 0 translates to 17KB in boringSSL.
355 - handshaker is the address of the handshaker pointer to be created.
356
357 - This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
358 where a parameter is invalid. */
359 tsi_result tsi_ssl_server_handshaker_factory_create_handshaker(
360 tsi_ssl_server_handshaker_factory* factory, size_t network_bio_buf_size,
361 size_t ssl_bio_buf_size, tsi_handshaker** handshaker);
362
363 /* Decrements reference count of the handshaker factory. Handshaker factory will
364 * be destroyed once no references exist. */
365 void tsi_ssl_server_handshaker_factory_unref(
366 tsi_ssl_server_handshaker_factory* factory);
367
368 /* Util that checks that an ssl peer matches a specific name.
369 Still TODO(jboeuf):
370 - handle mixed case.
371 - handle %encoded chars.
372 - handle public suffix wildchar more strictly (e.g. *.co.uk) */
373 int tsi_ssl_peer_matches_name(const tsi_peer* peer, absl::string_view name);
374
375 /* --- Testing support. ---
376
377 These functions and typedefs are not intended to be used outside of testing.
378 */
379
380 /* Base type of client and server handshaker factories. */
381 typedef struct tsi_ssl_handshaker_factory tsi_ssl_handshaker_factory;
382
383 /* Function pointer to handshaker_factory destructor. */
384 typedef void (*tsi_ssl_handshaker_factory_destructor)(
385 tsi_ssl_handshaker_factory* factory);
386
387 /* Virtual table for tsi_ssl_handshaker_factory. */
388 struct tsi_ssl_handshaker_factory_vtable {
389 tsi_ssl_handshaker_factory_destructor destroy;
390 };
391 /* Set destructor of handshaker_factory to new_destructor, returns previous
392 destructor. */
393 const tsi_ssl_handshaker_factory_vtable* tsi_ssl_handshaker_factory_swap_vtable(
394 tsi_ssl_handshaker_factory* factory,
395 tsi_ssl_handshaker_factory_vtable* new_vtable);
396
397 /* Exposed for testing only. */
398 tsi_result tsi_ssl_extract_x509_subject_names_from_pem_cert(
399 const char* pem_cert, tsi_peer* peer);
400
401 /* Exposed for testing only. */
402 tsi_result tsi_ssl_get_cert_chain_contents(STACK_OF(X509) * peer_chain,
403 tsi_peer_property* property);
404
405 #endif /* GRPC_CORE_TSI_SSL_TRANSPORT_SECURITY_H */