1
#pragma once
2

            
3
#include <openssl/safestack.h>
4

            
5
#include <array>
6
#include <deque>
7
#include <functional>
8
#include <memory>
9
#include <string>
10
#include <vector>
11

            
12
#include "envoy/network/transport_socket.h"
13
#include "envoy/ssl/context.h"
14
#include "envoy/ssl/context_config.h"
15
#include "envoy/ssl/handshaker.h"
16
#include "envoy/ssl/private_key/private_key.h"
17
#include "envoy/ssl/ssl_socket_extended_info.h"
18
#include "envoy/stats/scope.h"
19
#include "envoy/stats/stats_macros.h"
20

            
21
#include "source/common/common/matchers.h"
22
#include "source/common/stats/symbol_table.h"
23
#include "source/common/tls/cert_validator/cert_validator.h"
24
#include "source/common/tls/context_impl.h"
25
#include "source/common/tls/context_manager_impl.h"
26
#include "source/common/tls/default_tls_certificate_selector.h"
27
#include "source/common/tls/ocsp/ocsp.h"
28
#include "source/common/tls/stats.h"
29

            
30
#include "absl/synchronization/mutex.h"
31
#include "openssl/ssl.h"
32
#include "openssl/x509v3.h"
33

            
34
#ifdef ENVOY_ENABLE_QUIC
35
#include "quiche/quic/core/crypto/proof_source.h"
36
#endif
37

            
38
namespace Envoy {
39

            
40
namespace Extensions {
41
namespace TransportSockets {
42
namespace Tls {
43

            
44
Ssl::CurveNIDVector getClientCurveNIDSupported(CBS& cbs);
45
bool isClientOcspCapable(const SSL_CLIENT_HELLO& ssl_client_hello);
46

            
47
class ServerContextImpl : public ContextImpl,
48
                          public Envoy::Ssl::ServerContext,
49
                          public Envoy::Ssl::TlsCertificateSelectorContext {
50
public:
51
  static absl::StatusOr<std::unique_ptr<ServerContextImpl>>
52
  create(Stats::Scope& scope, const Envoy::Ssl::ServerContextConfig& config,
53
         Server::Configuration::CommonFactoryContext& factory_context,
54
         Ssl::ContextAdditionalInitFunc additional_init);
55

            
56
  // Ssl::TlsCertificateSelectorContext
57
  // The returned vector has the same life-time as the Ssl::TlsCertificateSelectorContext.
58
3459
  const std::vector<Ssl::TlsContext>& getTlsContexts() const override { return tls_contexts_; };
59

            
60
  // Select the TLS certificate context in SSL_CTX_set_select_certificate_cb() callback with
61
  // ClientHello details. This is made public for use by custom TLS extensions who want to
62
  // manually create and use this as a client hello callback.
63
  enum ssl_select_cert_result_t selectTlsContext(const SSL_CLIENT_HELLO* ssl_client_hello);
64

            
65
  // Finds the best matching context. The returned context will have the same lifetime as
66
  // this ``ServerContextImpl``.
67
  std::pair<const Ssl::TlsContext&, Ssl::OcspStapleAction>
68
  findTlsContext(absl::string_view sni, const Ssl::CurveNIDVector& client_ecdsa_capable,
69
                 bool client_ocsp_capable, bool* cert_matched_sni);
70

            
71
  Ssl::CurveNIDVector getClientEcdsaCapabilities(const SSL_CLIENT_HELLO& ssl_client_hello) const;
72

            
73
protected:
74
  ServerContextImpl(
75
      Stats::Scope& scope, const Envoy::Ssl::ServerContextConfig& config,
76
      const std::vector<std::reference_wrapper<const Ssl::TlsCertificateConfig>>& tls_certificates,
77
      bool add_selector, Server::Configuration::CommonFactoryContext& factory_context,
78
      Ssl::ContextAdditionalInitFunc additional_init, absl::Status& creation_status);
79

            
80
private:
81
  using SessionContextID = std::array<uint8_t, SSL_MAX_SSL_SESSION_ID_LENGTH>;
82

            
83
  int alpnSelectCallback(const unsigned char** out, unsigned char* outlen, const unsigned char* in,
84
                         unsigned int inlen);
85
  int sessionTicketProcess(SSL* ssl, uint8_t* key_name, uint8_t* iv, EVP_CIPHER_CTX* ctx,
86
                           HMAC_CTX* hmac_ctx, int encrypt);
87

            
88
  absl::StatusOr<SessionContextID>
89
  generateHashForSessionContextId(const std::vector<std::string>& server_names);
90

            
91
  Ssl::TlsCertificateSelectorPtr tls_certificate_selector_;
92
  const std::vector<Envoy::Ssl::ServerContextConfig::SessionTicketKey> session_ticket_keys_;
93

            
94
protected:
95
  const Ssl::ServerContextConfig::OcspStaplePolicy ocsp_staple_policy_;
96
};
97

            
98
class ServerContextFactoryImpl : public ServerContextFactory {
99
public:
100
533
  std::string name() const override { return "envoy.ssl.server_context_factory.default"; }
101
  absl::StatusOr<Ssl::ServerContextSharedPtr>
102
  createServerContext(Stats::Scope& scope, const Envoy::Ssl::ServerContextConfig& config,
103
                      Server::Configuration::CommonFactoryContext& factory_context,
104
                      Ssl::ContextAdditionalInitFunc additional_init) override;
105
};
106

            
107
DECLARE_FACTORY(ServerContextFactoryImpl);
108

            
109
} // namespace Tls
110
} // namespace TransportSockets
111
} // namespace Extensions
112
} // namespace Envoy