1
#pragma once
2

            
3
#include <string>
4

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

            
7
#include "source/common/common/logger.h"
8
#include "source/common/tls/utility.h"
9

            
10
#include "absl/types/optional.h"
11
#include "openssl/ssl.h"
12

            
13
namespace Envoy {
14
namespace Extensions {
15
namespace TransportSockets {
16
namespace Tls {
17

            
18
// An implementation wraps struct SSL in BoringSSL.
19
class ConnectionInfoImplBase : public Ssl::ConnectionInfo {
20
public:
21
  // Ssl::ConnectionInfo
22
  bool peerCertificatePresented() const override;
23
  const std::string& sha256PeerCertificateDigest() const override;
24
  absl::Span<const std::string> sha256PeerCertificateChainDigests() const override;
25
  const std::string& sha1PeerCertificateDigest() const override;
26
  absl::Span<const std::string> sha1PeerCertificateChainDigests() const override;
27
  const std::string& serialNumberPeerCertificate() const override;
28
  absl::Span<const std::string> serialNumbersPeerCertificates() const override;
29
  const std::string& issuerPeerCertificate() const override;
30
  const std::string& subjectPeerCertificate() const override;
31
  Ssl::ParsedX509NameOptConstRef parsedSubjectPeerCertificate() const override;
32
  const std::string& subjectLocalCertificate() const override;
33
  const std::string& urlEncodedPemEncodedPeerCertificate() const override;
34
  const std::string& urlEncodedPemEncodedPeerCertificateChain() const override;
35
  bool peerCertificateSanMatches(const Ssl::SanMatcher& matcher) const override;
36
  absl::Span<const std::string> uriSanPeerCertificate() const override;
37
  absl::Span<const std::string> uriSanLocalCertificate() const override;
38
  absl::Span<const std::string> dnsSansPeerCertificate() const override;
39
  absl::Span<const std::string> dnsSansLocalCertificate() const override;
40
  absl::Span<const std::string> ipSansPeerCertificate() const override;
41
  absl::Span<const std::string> ipSansLocalCertificate() const override;
42
  absl::Span<const std::string> emailSansPeerCertificate() const override;
43
  absl::Span<const std::string> emailSansLocalCertificate() const override;
44
  absl::Span<const std::string> othernameSansPeerCertificate() const override;
45
  absl::Span<const std::string> othernameSansLocalCertificate() const override;
46
  absl::Span<const std::string> oidsPeerCertificate() const override;
47
  absl::Span<const std::string> oidsLocalCertificate() const override;
48
  absl::optional<SystemTime> validFromPeerCertificate() const override;
49
  absl::optional<SystemTime> expirationPeerCertificate() const override;
50
  const std::string& sessionId() const override;
51
  uint16_t ciphersuiteId() const override;
52
  std::string ciphersuiteString() const override;
53
  const std::string& tlsVersion() const override;
54
  const std::string& alpn() const override;
55
  const std::string& sni() const override;
56

            
57
  virtual SSL* ssl() const PURE;
58

            
59
private:
60
  // Enum values should be the name of the calling function, but capitalized.
61
  enum class CachedValueTag : uint8_t {
62
    Alpn,
63
    SessionId,
64
    Sni,
65
    TlsVersion,
66
    UriSanLocalCertificate,
67
    DnsSansLocalCertificate,
68
    IpSansLocalCertificate,
69
    Sha256PeerCertificateDigest,
70
    Sha256PeerCertificateChainDigests,
71
    Sha1PeerCertificateDigest,
72
    Sha1PeerCertificateChainDigests,
73
    SerialNumberPeerCertificate,
74
    SerialNumbersPeerCertificates,
75
    IssuerPeerCertificate,
76
    SubjectPeerCertificate,
77
    ParsedSubjectPeerCertificate,
78
    SubjectLocalCertificate,
79
    EmailSansLocalCertificate,
80
    OthernameSansLocalCertificate,
81
    UriSanPeerCertificate,
82
    EmailSansPeerCertificate,
83
    OthernameSansPeerCertificate,
84
    UrlEncodedPemEncodedPeerCertificate,
85
    UrlEncodedPemEncodedPeerCertificateChain,
86
    PeerCertificateSanMatches,
87
    DnsSansPeerCertificate,
88
    IpSansPeerCertificate,
89
    OidsPeerCertificate,
90
    OidsLocalCertificate,
91
  };
92

            
93
  // Retrieve the given tag from the set of cached values, or create the value via the supplied
94
  // create function and cache it. The returned reference is valid for the lifetime of this object.
95
  template <typename ValueType>
96
  const ValueType& getCachedValueOrCreate(CachedValueTag tag,
97
                                          std::function<ValueType(SSL* ssl)> create) const;
98

            
99
  // For any given instance of this class, most of the accessors are never called, so
100
  // having fixed space for cached data that isn't used is a waste. Instead, create a lookup
101
  // table of cached values that are created on demand. Use a node_hash_map so that returned
102
  // references are not invalidated when additional items are added.
103
  using CachedValue = absl::variant<std::string, std::vector<std::string>, Ssl::ParsedX509NamePtr,
104
                                    bssl::UniquePtr<GENERAL_NAMES>>;
105
  mutable absl::node_hash_map<CachedValueTag, CachedValue> cached_values_;
106
};
107

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