Apache Mesos
tls_config.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License
12 
13 #ifndef __PROCESS_SSL_TLS_CONFIG_HPP__
14 #define __PROCESS_SSL_TLS_CONFIG_HPP__
15 
16 #ifdef USE_SSL_SOCKET
17 
18 #ifdef __WINDOWS__
19 // NOTE: This must be included before the OpenSSL headers as it includes
20 // `WinSock2.h` and `Windows.h` in the correct order.
21 #include <stout/windows.hpp>
22 #endif // __WINDOWS__
23 
24 #include <openssl/ssl.h>
25 
26 #include <stout/option.hpp>
27 
28 namespace process {
29 namespace network {
30 namespace openssl {
31 
32 struct TLSClientConfig {
33  // Callback that will be called before the TLS handshake is started.
34  typedef Try<Nothing> (*ConfigureSocketCallback)(
35  SSL* ssl,
36  const Address& peer,
37  const Option<std::string>& servername);
38 
39  // Callback that will be called after the TLS handshake has been
40  // completed successfully.
41  typedef Try<Nothing> (*VerifyCallback)(
42  const SSL* const ssl,
43  const Option<std::string>& servername,
44  const Option<net::IP>& ip);
45 
46  // The `ConfigureSocketCallback` and `VerifyCallback` arguments can be set
47  // to nullptr, in that case they will not be called.
48  TLSClientConfig(
49  const Option<std::string>& servername,
50  SSL_CTX *ctx,
51  ConfigureSocketCallback,
52  VerifyCallback);
53 
54  // Context from which the `SSL` object for this connection is created.
55  SSL_CTX *ctx;
56 
57  // Server hostname to be used for hostname validation, if any.
58  // This will be passed as the `servername` argument to both
59  // callbacks.
60  //
61  // TODO(bevers): Use this for SNI as well when the linked OpenSSL
62  // supports it.
63  Option<std::string> servername;
64 
65  // User-specified callbacks.
66  VerifyCallback verify;
67  ConfigureSocketCallback configure_socket;
68 };
69 
70 
71 // Returns a `TLSClientConfig` object that is configured with the
72 // provided `servername` and the global libprocess SSL context. The
73 // callbacks `verify` and `configure_socket` are setup with a pair
74 // default functions that implement the SSL behaviour configured
75 // via the `LIBPROCESS_SSL_*` environment variables.
76 //
77 // NOTE: Callers must _NOT_ modify the `ctx` in the returned `TLSClientConfig`.
78 // Doing so would mutate global libprocess state.
79 //
80 // NOTE: The returned `ctx`, `verify` and `configure_socket` values all
81 // implement parts of the libprocess default behaviour and rely on each other
82 // for working correctly. It is not recommended to change one of them while
83 // keeping the others, unless you know *exactly* what you're doing.
84 //
85 // NOTE: The passed `servername` will be ignored and a reverse DNS lookup will
86 // be done instead if `LIBPROCESS_SSL_HOSTNAME_VALIDATION_SCHEME=legacy`.
87 TLSClientConfig create_tls_client_config(const Option<std::string>& servername);
88 
89 } // namespace openssl {
90 } // namespace network {
91 } // namespace process {
92 
93 #endif // USE_SSL_SOCKET
94 
95 #endif // __PROCESS_SSL_TLS_CONFIG_HPP__
Definition: check.hpp:33
Try< Nothing > verify(const SSL *const ssl, Mode mode, const Option< std::string > &hostname=None(), const Option< net::IP > &ip=None())
Try< Nothing > configure_socket(SSL *ssl, Mode mode, const Address &peer, const Option< std::string > &peer_hostname)
Definition: executor.hpp:48
Try< Address > peer(int_fd s)
Returns the peer&#39;s Address for the accepted or connected socket.
Definition: network.hpp:98