1
#pragma once
2

            
3
#include <memory>
4
#include <string>
5

            
6
#include "envoy/common/pure.h"
7
#include "envoy/common/time.h"
8
#include "envoy/ssl/parsed_x509_name.h"
9

            
10
#include "absl/strings/string_view.h"
11
#include "absl/types/optional.h"
12
#include "absl/types/span.h"
13

            
14
namespace Envoy {
15
namespace Ssl {
16

            
17
// This is forward declared to avoid needing to forward declare `GENERAL_NAME` from BoringSSL.
18
class SanMatcher;
19

            
20
/**
21
 * Base connection interface for all SSL connections.
22
 */
23
class ConnectionInfo {
24
public:
25
9247
  virtual ~ConnectionInfo() = default;
26

            
27
  /**
28
   * @return bool whether the peer certificate is presented.
29
   **/
30
  virtual bool peerCertificatePresented() const PURE;
31

            
32
  /**
33
   * @return bool whether the peer certificate was validated.
34
   **/
35
  virtual bool peerCertificateValidated() const PURE;
36

            
37
  /**
38
   * @return absl::Span<const std::string>the URIs in the SAN field of the local certificate.
39
   *         Returns {} if there is no local certificate, or no SAN field, or no URI.
40
   **/
41
  virtual absl::Span<const std::string> uriSanLocalCertificate() const PURE;
42

            
43
  /**
44
   * @return std::string the subject field of the local certificate in RFC 2253 format. Returns ""
45
   *         if there is no local certificate, or no subject.
46
   **/
47
  virtual const std::string& subjectLocalCertificate() const PURE;
48

            
49
  /**
50
   * @return std::string the SHA256 digest of the peer certificate. Returns "" if there is no peer
51
   *         certificate which can happen in TLS (non mTLS) connections.
52
   */
53
  virtual const std::string& sha256PeerCertificateDigest() const PURE;
54

            
55
  /**
56
   * @return std::string the SHA1 digest of the peer certificate. Returns "" if there is no peer
57
   *         certificate which can happen in TLS (non mTLS) connections.
58
   */
59
  virtual const std::string& sha1PeerCertificateDigest() const PURE;
60

            
61
  /**
62
   * @return std::string the serial number field of the peer certificate. Returns "" if
63
   *         there is no peer certificate, or no serial number.
64
   **/
65
  virtual const std::string& serialNumberPeerCertificate() const PURE;
66

            
67
  /**
68
   * @return absl::Span<const std::string> the SHA256 digests of all peer certificates.
69
   *         Returns an empty vector if there is no peer certificate which can happen in
70
   *         TLS (non mTLS) connections.
71
   */
72
  virtual absl::Span<const std::string> sha256PeerCertificateChainDigests() const PURE;
73

            
74
  /**
75
   * @return absl::Span<const std::string> the SHA1 digest of all peer certificates.
76
   *         Returns an empty vector if there is no peer certificate which can happen in
77
   *         TLS (non mTLS) connections.
78
   */
79
  virtual absl::Span<const std::string> sha1PeerCertificateChainDigests() const PURE;
80

            
81
  /**
82
   * @return absl::Span<const std::string> the serial numbers of all peer certificates.
83
   *         An empty vector indicates that there were no peer certificates which can happen
84
   *         in TLS (non mTLS) connections.
85
   *         A vector element with a "" value indicates that the certificate at that index in
86
   *         the cert chain did not have a serial number.
87
   **/
88
  virtual absl::Span<const std::string> serialNumbersPeerCertificates() const PURE;
89

            
90
  /**
91
   * @return std::string the issuer field of the peer certificate in RFC 2253 format. Returns "" if
92
   *         there is no peer certificate, or no issuer.
93
   **/
94
  virtual const std::string& issuerPeerCertificate() const PURE;
95

            
96
  /**
97
   * @return std::string the subject field of the peer certificate in RFC 2253 format. Returns "" if
98
   *         there is no peer certificate, or no subject.
99
   **/
100
  virtual const std::string& subjectPeerCertificate() const PURE;
101

            
102
  /**
103
   * @return the well-known attribute values parsed from subject field of the peer certificate.
104
   *         Returns absl::nullopt if there is no peer certificate.
105
   **/
106
  virtual ParsedX509NameOptConstRef parsedSubjectPeerCertificate() const PURE;
107

            
108
  /**
109
   * @return absl::Span<const std::string> the URIs in the SAN field of the peer certificate.
110
   *         Returns {} if there is no peer certificate, or no SAN field, or no URI.
111
   **/
112
  virtual absl::Span<const std::string> uriSanPeerCertificate() const PURE;
113

            
114
  /**
115
   * @return std::string the URL-encoded PEM-encoded representation of the peer certificate. Returns
116
   *         "" if there is no peer certificate or encoding fails.
117
   **/
118
  virtual const std::string& urlEncodedPemEncodedPeerCertificate() const PURE;
119

            
120
  /**
121
   * @return std::string the URL-encoded PEM-encoded representation of the full peer certificate
122
   *         chain including the leaf certificate. Returns "" if there is no peer certificate or
123
   *         encoding fails.
124
   *
125
   * @note This is the peer-provided certificate chain, not the validated certificate chain. This
126
   *       may include certificates that are not part of the validated chain.
127
   **/
128
  virtual const std::string& urlEncodedPemEncodedPeerCertificateChain() const PURE;
129

            
130
  /**
131
   * @return bool whether the provided matcher matches a SAN in the peer certificate.
132
   * @note This method takes a matcher, instead of returning the SANs, to avoid putting
133
   *       BoringSSL types into interfaces.
134
   */
135
  virtual bool peerCertificateSanMatches(const SanMatcher& matcher) const PURE;
136

            
137
  /**
138
   * @return absl::Span<const std::string> the DNS entries in the SAN field of the peer certificate.
139
   *         Returns {} if there is no peer certificate, or no SAN field, or no DNS.
140
   **/
141
  virtual absl::Span<const std::string> dnsSansPeerCertificate() const PURE;
142

            
143
  /**
144
   * @return absl::Span<const std::string> the DNS entries in the SAN field of the local
145
   *certificate. Returns {} if there is no local certificate, or no SAN field, or no DNS.
146
   **/
147
  virtual absl::Span<const std::string> dnsSansLocalCertificate() const PURE;
148

            
149
  /**
150
   * @return absl::Span<const std::string> the IP entries in the SAN field of the peer certificate.
151
   *         Returns {} if there is no peer certificate, or no SAN field, or no IPs.
152
   **/
153
  virtual absl::Span<const std::string> ipSansPeerCertificate() const PURE;
154

            
155
  /**
156
   * @return absl::Span<const std::string> the IP entries in the SAN field of the local
157
   *certificate. Returns {} if there is no local certificate, or no SAN field, or no IPs.
158
   **/
159
  virtual absl::Span<const std::string> ipSansLocalCertificate() const PURE;
160

            
161
  /**
162
   * @return absl::Span<const std::string> the Email entries in the SAN field of the peer
163
   *certificate. Returns {} if there is no peer certificate, or no SAN field, or no Emails.
164
   **/
165
  virtual absl::Span<const std::string> emailSansPeerCertificate() const PURE;
166

            
167
  /**
168
   * @return absl::Span<const std::string> the Email entries in the SAN field of the local
169
   *certificate. Returns {} if there is no local certificate, or no SAN field, or no Emails.
170
   **/
171
  virtual absl::Span<const std::string> emailSansLocalCertificate() const PURE;
172

            
173
  /**
174
   * @return absl::Span<const std::string> the OtherName entries in the SAN field of the peer
175
   *certificate. Returns {} if there is no peer certificate, or no SAN field, or no OtherNames.
176
   **/
177
  virtual absl::Span<const std::string> othernameSansPeerCertificate() const PURE;
178

            
179
  /**
180
   * @return absl::Span<const std::string> the OtherName entries in the SAN field of the local
181
   *certificate. Returns {} if there is no local certificate, or no SAN field, or no OtherNames.
182
   **/
183
  virtual absl::Span<const std::string> othernameSansLocalCertificate() const PURE;
184

            
185
  /**
186
   * @return absl::Span<const std::string> the OID entries of the peer certificate extensions.
187
   *         Returns {} if there is no peer certificate, or no extensions.
188
   **/
189
  virtual absl::Span<const std::string> oidsPeerCertificate() const PURE;
190

            
191
  /**
192
   * @return absl::Span<const std::string> the OID entries of the local certificate extensions.
193
   *         Returns {} if there is no local certificate, or no extensions.
194
   **/
195
  virtual absl::Span<const std::string> oidsLocalCertificate() const PURE;
196

            
197
  /**
198
   * @return absl::optional<SystemTime> the time that the peer certificate was issued and should be
199
   *         considered valid from. Returns empty absl::optional if there is no peer certificate.
200
   **/
201
  virtual absl::optional<SystemTime> validFromPeerCertificate() const PURE;
202

            
203
  /**
204
   * @return absl::optional<SystemTime> the time that the peer certificate expires and should not be
205
   *         considered valid after. Returns empty absl::optional if there is no peer certificate.
206
   **/
207
  virtual absl::optional<SystemTime> expirationPeerCertificate() const PURE;
208

            
209
  /**
210
   * @return std::string the hex-encoded TLS session ID as defined in rfc5246.
211
   **/
212
  virtual const std::string& sessionId() const PURE;
213

            
214
  /**
215
   * @return uint16_t the standard ID for the ciphers used in the established TLS connection.
216
   *         Returns 0xffff if there is no current negotiated ciphersuite.
217
   **/
218
  virtual uint16_t ciphersuiteId() const PURE;
219

            
220
  /**
221
   * @return std::string the OpenSSL name for the set of ciphers used in the established TLS
222
   *         connection. Returns "" if there is no current negotiated ciphersuite.
223
   **/
224
  virtual std::string ciphersuiteString() const PURE;
225

            
226
  /**
227
   * @return std::string the TLS version (e.g., TLSv1.2, TLSv1.3) used in the established TLS
228
   *         connection.
229
   **/
230
  virtual const std::string& tlsVersion() const PURE;
231

            
232
  /**
233
   * @return std::string the protocol negotiated via ALPN.
234
   **/
235
  virtual const std::string& alpn() const PURE;
236

            
237
  /**
238
   * @return std::string the SNI used to establish the connection.
239
   **/
240
  virtual const std::string& sni() const PURE;
241
};
242

            
243
using ConnectionInfoConstSharedPtr = std::shared_ptr<const ConnectionInfo>;
244

            
245
} // namespace Ssl
246
} // namespace Envoy