aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
TlsOptions.h
Go to the documentation of this file.
1#pragma once
7#include <aws/crt/Types.h>
9#include <aws/io/tls_channel_handler.h>
10
11#include <functional>
12#include <memory>
13
14struct aws_tls_ctx_options;
15
16namespace Aws
17{
18 namespace Crt
19 {
20 namespace Io
21 {
22 enum class TlsMode
23 {
24 CLIENT,
25 SERVER,
26 };
27
29 {
30 friend class TlsContext;
31
32 public:
33 TlsContextOptions() noexcept;
34 virtual ~TlsContextOptions();
35 TlsContextOptions(const TlsContextOptions &) noexcept = delete;
36 TlsContextOptions &operator=(const TlsContextOptions &) noexcept = delete;
38 TlsContextOptions &operator=(TlsContextOptions &&) noexcept;
42 explicit operator bool() const noexcept { return m_isInit; }
46 int LastError() const noexcept;
47
52 static TlsContextOptions InitDefaultClient(Allocator *allocator = g_allocator) noexcept;
60 static TlsContextOptions InitClientWithMtls(
61 const char *cert_path,
62 const char *pkey_path,
63 Allocator *allocator = g_allocator) noexcept;
64
72 static TlsContextOptions InitClientWithMtls(
73 const ByteCursor &cert,
74 const ByteCursor &pkey,
75 Allocator *allocator = g_allocator) noexcept;
76
77#ifdef __APPLE__
87 static TlsContextOptions InitClientWithMtlsPkcs12(
88 const char *pkcs12_path,
89 const char *pkcs12_pwd,
90 Allocator *allocator = g_allocator) noexcept;
91
98 bool SetKeychainPath(ByteCursor &keychain_path) noexcept;
99#endif
100
101#ifdef _WIN32
108 static TlsContextOptions InitClientWithMtlsSystemPath(
109 const char *registryPath,
110 Allocator *allocator = g_allocator) noexcept;
111#endif /* _WIN32 */
112
117 static bool IsAlpnSupported() noexcept;
118
124 bool SetAlpnList(const char *alpnList) noexcept;
125
134 void SetVerifyPeer(bool verifyPeer) noexcept;
135
140 void SetMinimumTlsVersion(aws_tls_versions minimumTlsVersion);
141
150 bool OverrideDefaultTrustStore(const char *caPath, const char *caFile) noexcept;
155 bool OverrideDefaultTrustStore(const ByteCursor &ca) noexcept;
156
158 const aws_tls_ctx_options *GetUnderlyingHandle() const noexcept { return &m_options; }
159
160 private:
161 aws_tls_ctx_options m_options;
162 bool m_isInit;
163 };
164
169 {
170 public:
171 TlsConnectionOptions() noexcept;
174 TlsConnectionOptions &operator=(const TlsConnectionOptions &) noexcept;
175 TlsConnectionOptions(TlsConnectionOptions &&options) noexcept;
176 TlsConnectionOptions &operator=(TlsConnectionOptions &&options) noexcept;
177
183 bool SetServerName(ByteCursor &serverName) noexcept;
184
191 bool SetAlpnList(const char *alpnList) noexcept;
195 explicit operator bool() const noexcept { return isValid(); }
199 int LastError() const noexcept { return m_lastError; }
201 const aws_tls_connection_options *GetUnderlyingHandle() const noexcept
202 {
203 return &m_tls_connection_options;
204 }
205
206 private:
207 bool isValid() const noexcept { return m_isInit; }
208
209 TlsConnectionOptions(aws_tls_ctx *ctx, Allocator *allocator) noexcept;
210 aws_tls_connection_options m_tls_connection_options;
211 aws_allocator *m_allocator;
212 int m_lastError;
213 bool m_isInit;
214
215 friend class TlsContext;
216 };
217
219 {
220 public:
221 TlsContext() noexcept;
222 TlsContext(TlsContextOptions &options, TlsMode mode, Allocator *allocator = g_allocator) noexcept;
223 ~TlsContext() = default;
224 TlsContext(const TlsContext &) noexcept = default;
225 TlsContext &operator=(const TlsContext &) noexcept = default;
226 TlsContext(TlsContext &&) noexcept = default;
227 TlsContext &operator=(TlsContext &&) noexcept = default;
228
229 TlsConnectionOptions NewConnectionOptions() const noexcept;
233 explicit operator bool() const noexcept { return isValid(); }
237 int GetInitializationError() const noexcept { return m_initializationError; }
238
239 aws_tls_ctx *GetUnderlyingHandle() noexcept { return m_ctx.get(); }
240
241 private:
242 bool isValid() const noexcept { return m_ctx && m_initializationError == AWS_ERROR_SUCCESS; }
243
244 std::shared_ptr<aws_tls_ctx> m_ctx;
245 int m_initializationError;
246 };
247
248 using NewTlsContextImplCallback = std::function<void *(TlsContextOptions &, TlsMode, Allocator *)>;
249 using DeleteTlsContextImplCallback = std::function<void(void *)>;
250 using IsTlsAlpnSupportedCallback = std::function<bool()>;
251
256 {
257 public:
258 virtual ~TlsChannelHandler();
259
263 virtual String GetProtocol() const = 0;
264
265 protected:
267 struct aws_channel_slot *slot,
268 const struct aws_tls_connection_options &options,
269 Allocator *allocator = g_allocator);
270
276 void CompleteTlsNegotiation(int errorCode);
277
278 private:
279 aws_tls_on_negotiation_result_fn *m_OnNegotiationResult;
280 void *m_userData;
281
282 aws_byte_buf m_protocolByteBuf;
283 friend aws_byte_buf(::aws_tls_handler_protocol)(aws_channel_handler *);
284 };
285
293 {
294 public:
299 virtual void StartNegotiation() = 0;
300
301 protected:
303 struct aws_channel_slot *slot,
304 const struct aws_tls_connection_options &options,
305 Allocator *allocator = g_allocator);
306 };
307
308 using NewClientTlsHandlerCallback = std::function<std::shared_ptr<ClientTlsChannelHandler>(
309 struct aws_channel_slot *slot,
310 const struct aws_tls_connection_options &options,
311 Allocator *allocator)>;
312
313 } // namespace Io
314 } // namespace Crt
315} // namespace Aws
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Definition: ChannelHandler.h:47
Definition: TlsOptions.h:293
Definition: TlsOptions.h:256
virtual String GetProtocol() const =0
Definition: TlsOptions.h:169
int LastError() const noexcept
Definition: TlsOptions.h:199
Definition: TlsOptions.h:219
TlsContext(TlsContext &&) noexcept=default
TlsContext & operator=(const TlsContext &) noexcept=default
int GetInitializationError() const noexcept
Definition: TlsOptions.h:237
aws_tls_ctx * GetUnderlyingHandle() noexcept
Definition: TlsOptions.h:239
TlsContext(const TlsContext &) noexcept=default
Definition: TlsOptions.h:29
std::function< void *(TlsContextOptions &, TlsMode, Allocator *)> NewTlsContextImplCallback
Definition: TlsOptions.h:248
TlsMode
Definition: TlsOptions.h:23
std::function< bool()> IsTlsAlpnSupportedCallback
Definition: TlsOptions.h:250
std::function< std::shared_ptr< ClientTlsChannelHandler >(struct aws_channel_slot *slot, const struct aws_tls_connection_options &options, Allocator *allocator)> NewClientTlsHandlerCallback
Definition: TlsOptions.h:311
std::function< void(void *)> DeleteTlsContextImplCallback
Definition: TlsOptions.h:249
aws_byte_cursor ByteCursor
Definition: Types.h:33
aws_allocator Allocator
Definition: StlAllocator.h:17
AWS_CRT_CPP_API Allocator * g_allocator
Definition: Api.cpp:21
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:47
AWS_CRT_CPP_API int LastError() noexcept
Definition: Api.cpp:315
Definition: Api.h:17