trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
TLSPolicy.h
1#pragma once
2#include <trantor/exports.h>
3
4#include <memory>
5#include <string>
6#include <utility>
7#include <vector>
8
9namespace trantor
10{
11struct TRANTOR_EXPORT TLSPolicy final
12{
24 const std::vector<std::pair<std::string, std::string>> &sslConfCmds)
25 {
26 sslConfCmds_ = sslConfCmds;
27 return *this;
28 }
29
32 TLSPolicy &setHostname(const std::string &hostname)
33 {
34 hostname_ = hostname;
35 return *this;
36 }
37
42 TLSPolicy &setCertPath(const std::string &certPath)
43 {
44 certPath_ = certPath;
45 return *this;
46 }
47
52 TLSPolicy &setKeyPath(const std::string &keyPath)
53 {
54 keyPath_ = keyPath;
55 return *this;
56 }
57
62 TLSPolicy &setCaPath(const std::string &caPath)
63 {
64 caPath_ = caPath;
65 return *this;
66 }
67
72 TLSPolicy &setUseOldTLS(bool useOldTLS)
73 {
74 useOldTLS_ = useOldTLS;
75 return *this;
76 }
77
88 TLSPolicy &setAlpnProtocols(const std::vector<std::string> &alpnProtocols)
89 {
90 alpnProtocols_ = alpnProtocols;
91 return *this;
92 }
93 TLSPolicy &setAlpnProtocols(std::vector<std::string> &&alpnProtocols)
94 {
95 alpnProtocols_ = std::move(alpnProtocols);
96 return *this;
97 }
98
105 TLSPolicy &setUseSystemCertStore(bool useSystemCertStore)
106 {
107 useSystemCertStore_ = useSystemCertStore;
108 return *this;
109 }
110
114 TLSPolicy &setValidate(bool enable)
115 {
116 validate_ = enable;
117 return *this;
118 }
119
132 {
133 allowBrokenChain_ = allow;
134 return *this;
135 }
136
137 // The getters
138 const std::vector<std::pair<std::string, std::string>> &getConfCmds() const
139 {
140 return sslConfCmds_;
141 }
142 const std::string &getHostname() const
143 {
144 return hostname_;
145 }
146 const std::string &getCertPath() const
147 {
148 return certPath_;
149 }
150 const std::string &getKeyPath() const
151 {
152 return keyPath_;
153 }
154 const std::string &getCaPath() const
155 {
156 return caPath_;
157 }
158 bool getUseOldTLS() const
159 {
160 return useOldTLS_;
161 }
162 bool getValidate() const
163 {
164 return validate_;
165 }
166 bool getAllowBrokenChain() const
167 {
168 return allowBrokenChain_;
169 }
170 const std::vector<std::string> &getAlpnProtocols() const
171 {
172 return alpnProtocols_;
173 }
174 const std::vector<std::string> &getAlpnProtocols()
175 {
176 return alpnProtocols_;
177 }
178
179 bool getUseSystemCertStore() const
180 {
181 return useSystemCertStore_;
182 }
183
184 static std::shared_ptr<TLSPolicy> defaultServerPolicy(
185 const std::string &certPath,
186 const std::string &keyPath)
187 {
188 auto policy = std::make_shared<TLSPolicy>();
189 policy->setValidate(false)
190 .setUseOldTLS(false)
191 .setUseSystemCertStore(false)
192 .setCertPath(certPath)
193 .setKeyPath(keyPath);
194 return policy;
195 }
196
197 static std::shared_ptr<TLSPolicy> defaultClientPolicy(
198 const std::string &hostname = "")
199 {
200 auto policy = std::make_shared<TLSPolicy>();
201 policy->setValidate(true)
202 .setUseOldTLS(false)
203 .setUseSystemCertStore(true)
204 .setHostname(hostname);
205 return policy;
206 }
207
208 protected:
209 std::vector<std::pair<std::string, std::string>> sslConfCmds_ = {};
210 std::string hostname_ = "";
211 std::string certPath_ = "";
212 std::string keyPath_ = "";
213 std::string caPath_ = "";
214 std::vector<std::string> alpnProtocols_ = {};
215 bool useOldTLS_ = false; // turn into specific version
216 bool validate_ = true;
217 bool allowBrokenChain_ = false;
218 bool useSystemCertStore_ = true;
219};
220using TLSPolicyPtr = std::shared_ptr<TLSPolicy>;
221} // namespace trantor
Definition EventLoop.h:34
Definition TLSPolicy.h:12
TLSPolicy & setAllowBrokenChain(bool allow)
Allow broken chain (self-signed certificate, root CA not in allowed list, etc..) but still validate t...
Definition TLSPolicy.h:131
TLSPolicy & setHostname(const std::string &hostname)
set the hostname to be used for SNI and certificate validation.
Definition TLSPolicy.h:32
TLSPolicy & setKeyPath(const std::string &keyPath)
set the path to the private key file. The file must be in PEM format.
Definition TLSPolicy.h:52
TLSPolicy & setAlpnProtocols(const std::vector< std::string > &alpnProtocols)
set the list of protocols to be used for ALPN.
Definition TLSPolicy.h:88
TLSPolicy & setUseSystemCertStore(bool useSystemCertStore)
Weather to use the system's certificate store.
Definition TLSPolicy.h:105
TLSPolicy & setConfCmds(const std::vector< std::pair< std::string, std::string > > &sslConfCmds)
set the ssl configuration commands. The commands will be passed to the ssl library....
Definition TLSPolicy.h:23
TLSPolicy & setCertPath(const std::string &certPath)
set the path to the certificate file. The file must be in PEM format.
Definition TLSPolicy.h:42
TLSPolicy & setCaPath(const std::string &caPath)
set the path to the CA file or directory. The file must be in PEM format.
Definition TLSPolicy.h:62
TLSPolicy & setValidate(bool enable)
Enable certificate validation.
Definition TLSPolicy.h:114
TLSPolicy & setUseOldTLS(bool useOldTLS)
enables the use of the old TLS protocol (old meaning < TLS 1.2). TLS providers may not support old pr...
Definition TLSPolicy.h:72