1
#pragma once
2

            
3
#include <array>
4
#include <chrono>
5
#include <functional>
6
#include <string>
7
#include <vector>
8

            
9
#include "envoy/common/pure.h"
10
#include "envoy/extensions/transport_sockets/tls/v3/common.pb.h"
11
#include "envoy/ssl/certificate_validation_context_config.h"
12
#include "envoy/ssl/handshaker.h"
13
#include "envoy/ssl/tls_certificate_config.h"
14

            
15
#include "source/common/network/cidr_range.h"
16

            
17
#include "absl/types/optional.h"
18

            
19
namespace Envoy {
20
namespace Ssl {
21

            
22
/**
23
 * Supplies the configuration for an SSL context.
24
 */
25
class ContextConfig {
26
public:
27
7171
  virtual ~ContextConfig() = default;
28

            
29
  /**
30
   * The list of supported protocols exposed via ALPN. Client connections will send these
31
   * protocols to the server. Server connections will use these protocols to select the next
32
   * protocol if the client supports ALPN.
33
   */
34
  virtual const std::string& alpnProtocols() const PURE;
35

            
36
  /**
37
   * The ':' delimited list of supported cipher suites
38
   */
39
  virtual const std::string& cipherSuites() const PURE;
40

            
41
  /**
42
   * The ':' delimited list of supported ECDH curves.
43
   */
44
  virtual const std::string& ecdhCurves() const PURE;
45

            
46
  /**
47
   * The ':' delimited list of supported signature algorithms.
48
   * See https://www.rfc-editor.org/rfc/rfc8446#page-41 for the names.
49
   */
50
  virtual const std::string& signatureAlgorithms() const PURE;
51

            
52
  /**
53
   * @return std::vector<std::reference_wrapper<const TlsCertificateConfig>> TLS
54
   * certificate configs.
55
   */
56
  virtual std::vector<std::reference_wrapper<const TlsCertificateConfig>>
57
  tlsCertificates() const PURE;
58

            
59
  /**
60
   * @return CertificateValidationContextConfig the certificate validation context config.
61
   */
62
  virtual const CertificateValidationContextConfig* certificateValidationContext() const PURE;
63

            
64
  /**
65
   * @return The minimum TLS protocol version to negotiate.
66
   */
67
  virtual unsigned minProtocolVersion() const PURE;
68

            
69
  /**
70
   * @return The maximum TLS protocol version to negotiate.
71
   */
72
  virtual unsigned maxProtocolVersion() const PURE;
73

            
74
  /**
75
   * @return true if the ContextConfig is able to provide secrets to create SSL context,
76
   * and false if dynamic secrets are expected but are not downloaded from SDS server yet.
77
   */
78
  virtual bool isReady() const PURE;
79

            
80
  /**
81
   * Add secret callback into context config. When dynamic secrets are in use and new secrets
82
   * are downloaded from SDS server, this callback is invoked to update SSL context.
83
   * @param callback callback that is executed by context config.
84
   */
85
  virtual void setSecretUpdateCallback(std::function<absl::Status()> callback) PURE;
86

            
87
  /**
88
   * @return a callback which can be used to create Handshaker instances.
89
   */
90
  virtual HandshakerFactoryCb createHandshaker() const PURE;
91

            
92
  /**
93
   * @return the set of capabilities for handshaker instances created by this context.
94
   */
95
  virtual HandshakerCapabilities capabilities() const PURE;
96

            
97
  /**
98
   * @return a callback for configuring an SSL_CTX before use.
99
   */
100
  virtual SslCtxCb sslctxCb() const PURE;
101

            
102
  /**
103
   * @return the TLS key log local filter.
104
   */
105
  virtual const Network::Address::IpList& tlsKeyLogLocal() const PURE;
106

            
107
  /**
108
   * @return the TLS key log remote filter.
109
   */
110
  virtual const Network::Address::IpList& tlsKeyLogRemote() const PURE;
111

            
112
  /**
113
   * @return the TLS key log path
114
   */
115
  virtual const std::string& tlsKeyLogPath() const PURE;
116

            
117
  /**
118
   * @return the access log manager object reference
119
   */
120
  virtual AccessLog::AccessLogManager& accessLogManager() const PURE;
121

            
122
  /**
123
   * @return the compiance policy for the TLS context.
124
   */
125
  virtual absl::optional<
126
      envoy::extensions::transport_sockets::tls::v3::TlsParameters::CompliancePolicy>
127
  compliancePolicy() const PURE;
128
};
129

            
130
class ClientContextConfig : public virtual ContextConfig {
131
public:
132
  /**
133
   * @return The server name indication if it's set and ssl enabled
134
   * Otherwise, ""
135
   */
136
  virtual const std::string& serverNameIndication() const PURE;
137

            
138
  /**
139
   * If true, replaces the SNI for the connection with the hostname of the upstream host, if
140
   * the hostname is known.
141
   */
142
  virtual bool autoHostServerNameIndication() const PURE;
143

            
144
  /**
145
   * If true, replace any Subject Alternative Name validations with a validation for a DNS SAN
146
   * matching the SNI value sent. Note that the validation will be against the actual requested SNI,
147
   * regardless of how it is configured.
148
   */
149
  virtual bool autoSniSanMatch() const PURE;
150

            
151
  /**
152
   * @return true if server-initiated TLS renegotiation will be allowed.
153
   */
154
  virtual bool allowRenegotiation() const PURE;
155

            
156
  /**
157
   * @return The maximum number of session keys to store.
158
   */
159
  virtual size_t maxSessionKeys() const PURE;
160

            
161
  /**
162
   * @return true if the enforcement that handshake will fail if the keyUsage extension is present
163
   * and incompatible with the TLS usage is enabled.
164
   */
165
  virtual bool enforceRsaKeyUsage() const PURE;
166

            
167
  /**
168
   * @return an optional factory which can be used to create TLS context provider instances.
169
   */
170
  virtual OptRef<UpstreamTlsCertificateSelectorFactory> tlsCertificateSelectorFactory() const PURE;
171
};
172

            
173
using ClientContextConfigPtr = std::unique_ptr<ClientContextConfig>;
174

            
175
class ServerContextConfig : public virtual ContextConfig {
176
public:
177
  struct SessionTicketKey {
178
    std::array<uint8_t, 16> name_;         // 16 == SSL_TICKET_KEY_NAME_LEN
179
    std::array<uint8_t, 32> hmac_key_;     // 32 == SHA256_DIGEST_LENGTH
180
    std::array<uint8_t, 256 / 8> aes_key_; // AES256 key size, in bytes
181
  };
182

            
183
  enum class OcspStaplePolicy {
184
    LenientStapling,
185
    StrictStapling,
186
    MustStaple,
187
  };
188

            
189
  /**
190
   * @return True if client certificate is required, false otherwise.
191
   */
192
  virtual bool requireClientCertificate() const PURE;
193

            
194
  /**
195
   * @return OcspStaplePolicy The rule for determining whether to staple OCSP
196
   * responses on new connections.
197
   */
198
  virtual OcspStaplePolicy ocspStaplePolicy() const PURE;
199

            
200
  /**
201
   * @return The keys to use for encrypting and decrypting session tickets.
202
   * The first element is used for encrypting new tickets, and all elements
203
   * are candidates for decrypting received tickets.
204
   */
205
  virtual const std::vector<SessionTicketKey>& sessionTicketKeys() const PURE;
206

            
207
  /**
208
   * @return timeout in seconds for the session.
209
   * Session timeout is used to specify lifetime hint of tls tickets.
210
   */
211
  virtual absl::optional<std::chrono::seconds> sessionTimeout() const PURE;
212

            
213
  /**
214
   * @return True if stateless TLS session resumption is disabled, false otherwise.
215
   */
216
  virtual bool disableStatelessSessionResumption() const PURE;
217

            
218
  /**
219
   * @return True if stateful TLS session resumption is disabled, false otherwise.
220
   */
221
  virtual bool disableStatefulSessionResumption() const PURE;
222

            
223
  /**
224
   * @return True if we allow full scan certificates when there is no cert matching SNI during
225
   * downstream TLS handshake, false otherwise.
226
   */
227
  virtual bool fullScanCertsOnSNIMismatch() const PURE;
228

            
229
  /**
230
   * @return true if the client cipher preference is enabled, false otherwise.
231
   */
232
  virtual bool preferClientCiphers() const PURE;
233

            
234
  /**
235
   * @return a factory which can be used to create TLS context provider instances.
236
   */
237
  virtual TlsCertificateSelectorFactory& tlsCertificateSelectorFactory() const PURE;
238

            
239
  /**
240
   * @return reference to the server names configured on the socket factory.
241
   */
242
  virtual const std::vector<std::string>& serverNames() const PURE;
243
};
244

            
245
using ServerContextConfigPtr = std::unique_ptr<ServerContextConfig>;
246

            
247
} // namespace Ssl
248
} // namespace Envoy