1
#pragma once
2

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

            
6
#include "envoy/ssl/context.h"
7
#include "envoy/ssl/parsed_x509_name.h"
8

            
9
#include "source/common/common/utility.h"
10

            
11
#include "absl/types/optional.h"
12
#include "openssl/ssl.h"
13
#include "openssl/x509v3.h"
14

            
15
namespace Envoy {
16
namespace Extensions {
17
namespace TransportSockets {
18
namespace Tls {
19
namespace Utility {
20

            
21
Envoy::Ssl::CertificateDetailsPtr certificateDetails(X509* cert, const std::string& path,
22
                                                     TimeSource& time_source);
23

            
24
/**
25
 * Determines whether the given name matches 'pattern' which may optionally begin with a wildcard
26
 * or contain a wildcard inside the pattern's first label.
27
 * See: https://www.rfc-editor.org/rfc/rfc6125#section-6.4.3.
28
 * @param dns_name the DNS name to match
29
 * @param pattern the pattern to match against (*.example.com) or (test*.example.com)
30
 * @return true if the san matches pattern
31
 */
32
bool dnsNameMatch(absl::string_view dns_name, absl::string_view pattern);
33

            
34
/**
35
 * Determines whether the given DNS label matches 'pattern' which may contain a wildcard. e.g.,
36
 * patterns "baz*" and "*baz" and "b*z" would match DNS labels "baz1" and "foobaz" and "buzz",
37
 * respectively.
38
 * @param dns_label the DNS name label to match in lower case
39
 * @param pattern the pattern to match against in lower case
40
 * @return true if the dns_label matches pattern
41
 */
42
bool labelWildcardMatch(absl::string_view dns_label, absl::string_view pattern);
43

            
44
/**
45
 * Retrieves the serial number of a certificate.
46
 * @param cert the certificate
47
 * @return std::string the serial number field of the certificate. Returns "" if
48
 *         there is no serial number.
49
 */
50
std::string getSerialNumberFromCertificate(X509& cert);
51

            
52
/**
53
 * Maps a stack of x509 certificates to a vector of strings extracted from the certificates.
54
 * @param stack the stack of certificates
55
 * @param field_extractor the function to extract the field from each certificate.
56
 * @return std::vector<std::string> returns the list of fields extracted from the certificates.
57
 */
58
std::vector<std::string> mapX509Stack(stack_st_X509& stack,
59
                                      std::function<std::string(X509&)> field_extractor);
60

            
61
/**
62
 * Retrieves the subject alternate names of a certificate.
63
 * @param cert the certificate
64
 * @param type type of subject alternate name
65
 * @return std::vector returns the list of subject alternate names.
66
 */
67
std::vector<std::string> getSubjectAltNames(X509& cert, int type);
68

            
69
/**
70
 * Converts the Subject Alternate Name to string.
71
 * @param general_name the subject alternate name
72
 * @return std::string returns the string representation of subject alt names.
73
 */
74
std::string generalNameAsString(const GENERAL_NAME* general_name);
75

            
76
/**
77
 * Retrieves the issuer from certificate.
78
 * @param cert the certificate
79
 * @return std::string the issuer field for the certificate.
80
 */
81
std::string getIssuerFromCertificate(X509& cert);
82

            
83
/**
84
 * Retrieves the subject from certificate.
85
 * @param cert the certificate
86
 * @return std::string the subject field for the certificate.
87
 */
88
std::string getSubjectFromCertificate(X509& cert);
89

            
90
/**
91
 * Parse the well-known attribute values of issuer from certificate.
92
 * @param cert the certificate
93
 * @return Envoy::Ssl::ParsedX509NameConstSharedPtr the struct contains the parsed values.
94
 */
95
Envoy::Ssl::ParsedX509NamePtr parseIssuerFromCertificate(X509& cert);
96

            
97
/**
98
 * Parse the well-known attribute values of subject from certificate.
99
 * @param cert the certificate
100
 * @return Envoy::Ssl::ParsedX509NameConstSharedPtr the struct contains the parsed values.
101
 */
102
Envoy::Ssl::ParsedX509NamePtr parseSubjectFromCertificate(X509& cert);
103

            
104
/**
105
 * Retrieves the extension OIDs from certificate.
106
 * @param cert the certificate
107
 * @return std::vector returns the string list of ASN.1 object identifiers.
108
 */
109
std::vector<std::string> getCertificateExtensionOids(X509& cert);
110

            
111
/**
112
 * Retrieves the value of a specific X509 extension from the cert, if present.
113
 * @param cert the certificate.
114
 * @param extension_name the name of the extension to extract in dotted number format
115
 * @return absl::string_view the DER-encoded value of the extension field or empty if not present.
116
 */
117
absl::string_view getCertificateExtensionValue(X509& cert, absl::string_view extension_name);
118

            
119
/**
120
 * Returns the seconds since unix epoch of the expiration time of this certificate.
121
 * @param cert the certificate
122
 * @return the seconds since unix epoch as a duration, or max duration if cert is null.
123
 */
124
std::chrono::seconds getExpirationUnixTime(const X509* cert);
125

            
126
/**
127
 * Returns the days until this certificate is valid.
128
 * @param cert the certificate
129
 * @param time_source the time source to use for current time calculation.
130
 * @return the number of days till this certificate is valid, the value is set when not expired.
131
 */
132
absl::optional<uint32_t> getDaysUntilExpiration(const X509* cert, TimeSource& time_source);
133

            
134
/**
135
 * Returns the time from when this certificate is valid.
136
 * @param cert the certificate.
137
 * @return time from when this certificate is valid.
138
 */
139
SystemTime getValidFrom(const X509& cert);
140

            
141
/**
142
 * Returns the time when this certificate expires.
143
 * @param cert the certificate.
144
 * @return time after which the certificate expires.
145
 */
146
SystemTime getExpirationTime(const X509& cert);
147

            
148
/**
149
 * Returns the last crypto error from ERR_get_error(), or `absl::nullopt`
150
 * if the error stack is empty.
151
 * @return std::string error message
152
 */
153
absl::optional<std::string> getLastCryptoError();
154

            
155
/**
156
 * Returns error string corresponding error code derived from OpenSSL.
157
 * @param err error code
158
 * @return string message corresponding error code.
159
 */
160
absl::string_view getErrorDescription(int err);
161

            
162
/**
163
 * Extracts the X509 certificate validation error information.
164
 *
165
 * @param ctx the store context
166
 * @return the error details
167
 */
168
std::string getX509VerificationErrorInfo(X509_STORE_CTX* ctx);
169

            
170
/**
171
 * Returns a list of all Subject Alternative Names from the certificate.
172
 * @param cert the certificate
173
 * @return std::vector returns the list of subject alternate names as strings.
174
 */
175
std::vector<std::string> getCertificateSansForLogging(X509* cert);
176

            
177
/**
178
 * Returns a list of all CRL Distribution Points from the certificate.
179
 * @param cert the certificate
180
 * @return std::vector returns the list of CRL distribution points as strings.
181
 */
182
std::vector<std::string> getCertificateCrlDpsForLogging(X509* cert);
183

            
184
} // namespace Utility
185
} // namespace Tls
186
} // namespace TransportSockets
187
} // namespace Extensions
188
} // namespace Envoy