1
#pragma once
2

            
3
#include <openssl/ssl.h>
4

            
5
#include "envoy/ssl/handshaker.h"
6

            
7
#include "source/common/tls/context_impl.h"
8
#include "source/common/tls/server_context_impl.h"
9
#include "source/common/tls/stats.h"
10

            
11
namespace Envoy {
12
namespace Extensions {
13
namespace TransportSockets {
14
namespace Tls {
15

            
16
// Defined in server_context_impl.h
17
class ServerContextImpl;
18

            
19
Ssl::OcspStapleAction
20
ocspStapleAction(const Ssl::TlsContext& ctx, bool client_ocsp_capable,
21
                 Ssl::ServerContextConfig::OcspStaplePolicy ocsp_staple_policy);
22

            
23
/**
24
 * The default TLS context provider, selecting certificate based on SNI.
25
 */
26
class DefaultTlsCertificateSelector : public Ssl::TlsCertificateSelector,
27
                                      protected Logger::Loggable<Logger::Id::connection> {
28
public:
29
  DefaultTlsCertificateSelector(const Ssl::ServerContextConfig& config,
30
                                Ssl::TlsCertificateSelectorContext& selector_ctx);
31

            
32
  Ssl::SelectionResult selectTlsContext(const SSL_CLIENT_HELLO& ssl_client_hello,
33
                                        Ssl::CertificateSelectionCallbackPtr cb) override;
34

            
35
  // Finds the best matching context. The returned context will have the same lifetime as
36
  // ``ServerContextImpl``.
37
  std::pair<const Ssl::TlsContext&, Ssl::OcspStapleAction>
38
  findTlsContext(absl::string_view sni, const Ssl::CurveNIDVector& client_ecdsa_capabilities,
39
                 bool client_ocsp_capable, bool* cert_matched_sni) override;
40

            
41
private:
42
  // Currently, at most one certificate of a given key type may be specified for each exact
43
  // server name or wildcard domain name.
44
  using PkeyTypesMap = absl::flat_hash_map<int, std::reference_wrapper<const Ssl::TlsContext>>;
45
  // Both exact server names and wildcard domains are part of the same map, in which wildcard
46
  // domains are prefixed with "." (i.e. ".example.com" for "*.example.com") to differentiate
47
  // between exact and wildcard entries.
48
  using ServerNamesMap = absl::flat_hash_map<std::string, PkeyTypesMap>;
49

            
50
  void populateServerNamesMap(const Ssl::TlsContext& ctx, const int pkey_id);
51

            
52
  // ServerContext own this selector, it's safe to use itself here.
53
  ServerContextImpl& server_ctx_;
54
  const std::vector<Ssl::TlsContext>& tls_contexts_;
55

            
56
  ServerNamesMap server_names_map_;
57
  bool has_rsa_{false};
58

            
59
  const Ssl::ServerContextConfig::OcspStaplePolicy ocsp_staple_policy_;
60
  bool full_scan_certs_on_sni_mismatch_;
61
};
62

            
63
class DefaultTlsCertificateSelectorFactory : public Ssl::TlsCertificateSelectorFactory {
64
public:
65
  explicit DefaultTlsCertificateSelectorFactory(const Ssl::ServerContextConfig& config)
66
3489
      : config_(config) {}
67
3452
  Ssl::TlsCertificateSelectorPtr create(Ssl::TlsCertificateSelectorContext& selector_ctx) override {
68
3452
    return std::make_unique<DefaultTlsCertificateSelector>(config_, selector_ctx);
69
3452
  };
70
58
  absl::Status onConfigUpdate() override { return absl::OkStatus(); }
71

            
72
private:
73
  // TLS context config owns this factory instance.
74
  const Ssl::ServerContextConfig& config_;
75
};
76

            
77
class TlsCertificateSelectorConfigFactoryImpl : public Ssl::TlsCertificateSelectorConfigFactory {
78
public:
79
3
  std::string name() const override { return "envoy.tls.certificate_selectors.default"; }
80
  absl::StatusOr<Ssl::TlsCertificateSelectorFactoryPtr>
81
  createTlsCertificateSelectorFactory(const Protobuf::Message&,
82
                                      Server::Configuration::GenericFactoryContext&,
83
3489
                                      const Ssl::ServerContextConfig& config, bool) override {
84
3489
    return std::make_unique<DefaultTlsCertificateSelectorFactory>(config);
85
3489
  }
86

            
87
  ProtobufTypes::MessagePtr createEmptyConfigProto() override {
88
    return std::make_unique<Protobuf::Struct>();
89
  }
90

            
91
3489
  static Ssl::TlsCertificateSelectorConfigFactory* getDefaultTlsCertificateSelectorConfigFactory() {
92
3489
    static TlsCertificateSelectorConfigFactoryImpl default_tls_certificate_selector_config_factory;
93
3489
    return &default_tls_certificate_selector_config_factory;
94
3489
  }
95
};
96

            
97
} // namespace Tls
98
} // namespace TransportSockets
99
} // namespace Extensions
100
} // namespace Envoy