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
2 
7 #include <aws/crt/Types.h>
9 #include <aws/io/tls_channel_handler.h>
10 
11 #include <functional>
12 #include <memory>
13 
14 struct aws_tls_ctx_options;
15 
16 namespace Aws
17 {
18  namespace Crt
19  {
20  namespace Io
21  {
22  class Pkcs11Lib;
23  class TlsContextPkcs11Options;
24  struct TlsConnectionInfo;
25  enum class CertificateSource;
26 
27  enum class TlsMode
28  {
29  CLIENT,
30  SERVER,
31  };
32 
38  {
39  friend class TlsContext;
40 
41  public:
42  TlsContextOptions() noexcept;
43  virtual ~TlsContextOptions();
44  TlsContextOptions(const TlsContextOptions &) noexcept = delete;
45  TlsContextOptions &operator=(const TlsContextOptions &) noexcept = delete;
46  TlsContextOptions(TlsContextOptions &&) noexcept;
47  TlsContextOptions &operator=(TlsContextOptions &&) noexcept;
48 
52  explicit operator bool() const noexcept { return m_isInit; }
53 
57  int LastError() const noexcept;
58 
63  static TlsContextOptions InitDefaultClient(Allocator *allocator = ApiAllocator()) noexcept;
64 
76  static TlsContextOptions InitClientWithMtls(
77  const char *cert_path,
78  const char *pkey_path,
79  Allocator *allocator = ApiAllocator()) noexcept;
80 
92  static TlsContextOptions InitClientWithMtls(
93  const ByteCursor &cert,
94  const ByteCursor &pkey,
95  Allocator *allocator = ApiAllocator()) noexcept;
96 
106  static TlsContextOptions InitClientWithMtlsPkcs11(
107  const TlsContextPkcs11Options &pkcs11Options,
108  Allocator *allocator = ApiAllocator()) noexcept;
109 
122  static TlsContextOptions InitClientWithMtlsPkcs12(
123  const char *pkcs12_path,
124  const char *pkcs12_pwd,
125  Allocator *allocator = ApiAllocator()) noexcept;
126 
137  bool SetKeychainPath(ByteCursor &keychain_path) noexcept;
138 
150  static TlsContextOptions InitClientWithMtlsSystemPath(
151  const char *windowsCertStorePath,
152  Allocator *allocator = ApiAllocator()) noexcept;
153 
158  static bool IsAlpnSupported() noexcept;
159 
165  bool SetAlpnList(const char *alpnList) noexcept;
166 
175  void SetVerifyPeer(bool verifyPeer) noexcept;
176 
190  void SetNoCertificateRevocation(bool noCertificateRevocation) noexcept;
191 
196  void SetMinimumTlsVersion(aws_tls_versions minimumTlsVersion);
197 
202  void SetTlsCipherPreference(aws_tls_cipher_pref cipher_pref);
203 
212  bool OverrideDefaultTrustStore(const char *caPath, const char *caFile) noexcept;
213 
218  bool OverrideDefaultTrustStore(const ByteCursor &ca) noexcept;
219 
221  const aws_tls_ctx_options *GetUnderlyingHandle() const noexcept
222  {
223  return m_isInit ? &m_options : nullptr;
224  }
225 
226  private:
227  aws_tls_ctx_options m_options;
228  aws_tls_versions m_metricsTlsVersion;
229  aws_tls_cipher_pref m_metricsCipherPref;
230  // Internal metrics tracking fields, track the certificate source
231  CertificateSource m_metricsCertificateSource;
232 
233  bool m_isInit = false;
234  };
235 
242  {
243  public:
249  const std::shared_ptr<Pkcs11Lib> &pkcs11Lib,
250  Allocator *allocator = ApiAllocator()) noexcept;
251 
258  void SetUserPin(const String &pin) noexcept;
259 
266  void SetSlotId(const uint64_t id) noexcept;
267 
274  void SetTokenLabel(const String &label) noexcept;
275 
283  void SetPrivateKeyObjectLabel(const String &label) noexcept;
284 
291  void SetCertificateFilePath(const String &path) noexcept;
292 
299  void SetCertificateFileContents(const String &contents) noexcept;
300 
302  aws_tls_ctx_pkcs11_options GetUnderlyingHandle() const noexcept;
303 
304  private:
305  std::shared_ptr<Pkcs11Lib> m_pkcs11Lib;
306  Optional<uint64_t> m_slotId;
307  Optional<String> m_userPin;
308  Optional<String> m_tokenLabel;
309  Optional<String> m_privateKeyObjectLabel;
310  Optional<String> m_certificateFilePath;
311  Optional<String> m_certificateFileContents;
312  };
313 
318  {
319  public:
320  TlsConnectionOptions() noexcept;
322  TlsConnectionOptions(const TlsConnectionOptions &) noexcept;
323  TlsConnectionOptions &operator=(const TlsConnectionOptions &) noexcept;
324  TlsConnectionOptions(TlsConnectionOptions &&options) noexcept;
325  TlsConnectionOptions &operator=(TlsConnectionOptions &&options) noexcept;
326 
332  bool SetServerName(ByteCursor &serverName) noexcept;
333 
340  bool SetAlpnList(const char *alpnList) noexcept;
341 
345  explicit operator bool() const noexcept { return isValid(); }
346 
350  int LastError() const noexcept { return m_lastError; }
351 
353  const aws_tls_connection_options *GetUnderlyingHandle() const noexcept
354  {
355  return m_isInit ? &m_tls_connection_options : nullptr;
356  }
357 
359  TlsConnectionInfo GetTlsConnectionInfo() const noexcept;
360 
361  private:
362  bool isValid() const noexcept { return m_isInit; }
363 
364  // Create TlsConnectionOptions from aws_tls_ctx. The metricsCertificateSource, metricsTlsVersion, and
365  // metricsCipherPref are used for metrics tracking. It might not directly reflect the real options.
366  TlsConnectionOptions(
367  aws_tls_ctx *ctx,
368  Allocator *allocator,
369  CertificateSource metricsCertificateSource,
370  aws_tls_versions metricsTlsVersion = AWS_IO_TLS_VER_SYS_DEFAULTS,
371  aws_tls_cipher_pref metricsCipherPref = AWS_IO_TLS_CIPHER_PREF_SYSTEM_DEFAULT) noexcept;
372  aws_tls_connection_options m_tls_connection_options;
373  aws_allocator *m_allocator = nullptr;
374  int m_lastError = AWS_ERROR_SUCCESS;
375  bool m_isInit = false;
376 
377  // Internal metrics tracking fields
378  CertificateSource m_metricsCertificateSource;
379  aws_tls_versions m_metricsTlsVersion;
380  aws_tls_cipher_pref m_metricsCipherPref;
381 
382  friend class TlsContext;
383  };
384 
389  class AWS_CRT_CPP_API TlsContext final
390  {
391  public:
392  TlsContext() noexcept;
393  TlsContext(TlsContextOptions &options, TlsMode mode, Allocator *allocator = ApiAllocator()) noexcept;
394  ~TlsContext() = default;
395  TlsContext(const TlsContext &) noexcept = default;
396  TlsContext &operator=(const TlsContext &) noexcept = default;
397  TlsContext(TlsContext &&) noexcept = default;
398  TlsContext &operator=(TlsContext &&) noexcept = default;
399 
404  TlsConnectionOptions NewConnectionOptions() const noexcept;
405 
409  explicit operator bool() const noexcept { return isValid(); }
410 
414  int GetInitializationError() const noexcept { return m_initializationError; }
415 
417  aws_tls_ctx *GetUnderlyingHandle() const noexcept { return m_ctx.get(); }
418 
419  private:
420  bool isValid() const noexcept { return m_ctx && m_initializationError == AWS_ERROR_SUCCESS; }
421 
422  std::shared_ptr<aws_tls_ctx> m_ctx;
423  int m_initializationError;
424  // Internal metrics tracking fields, track the certificate source
425  CertificateSource m_metricsCertificateSource;
426  aws_tls_versions m_metricsTlsVersion;
427  aws_tls_cipher_pref m_metricsCipherPref;
428  };
429 
430  using NewTlsContextImplCallback = std::function<void *(TlsContextOptions &, TlsMode, Allocator *)>;
431  using DeleteTlsContextImplCallback = std::function<void(void *)>;
432  using IsTlsAlpnSupportedCallback = std::function<bool()>;
433 
438  {
439  public:
440  virtual ~TlsChannelHandler();
441 
445  virtual String GetProtocol() const = 0;
446 
447  protected:
449  struct aws_channel_slot *slot,
450  const struct aws_tls_connection_options &options,
451  Allocator *allocator = ApiAllocator());
452 
458  void CompleteTlsNegotiation(int errorCode);
459 
460  private:
461  aws_tls_on_negotiation_result_fn *m_OnNegotiationResult;
462  void *m_userData;
463 
464  aws_byte_buf m_protocolByteBuf;
465  friend aws_byte_buf(::aws_tls_handler_protocol)(aws_channel_handler *);
466  };
467 
475  {
476  public:
481  virtual void StartNegotiation() = 0;
482 
483  protected:
485  struct aws_channel_slot *slot,
486  const struct aws_tls_connection_options &options,
487  Allocator *allocator = ApiAllocator());
488  };
489 
490  using NewClientTlsHandlerCallback = std::function<std::shared_ptr<ClientTlsChannelHandler>(
491  struct aws_channel_slot *slot,
492  const struct aws_tls_connection_options &options,
493  Allocator *allocator)>;
494 
495  } // namespace Io
496  } // namespace Crt
497 } // namespace Aws
Definition: TlsOptions.h:474
Definition: TlsOptions.h:389
Definition: ChannelHandler.h:41
#define AWS_CRT_CPP_API
Definition: Exports.h:36
int LastError() const noexcept
Definition: TlsOptions.h:350
std::basic_string< char, std::char_traits< char >, StlAllocator< char >> String
Definition: Types.h:45
aws_allocator Allocator
Definition: Allocator.h:14
aws_byte_cursor ByteCursor
Definition: Types.h:31
std::function< bool()> IsTlsAlpnSupportedCallback
Definition: TlsOptions.h:432
Definition: TlsOptions.h:37
TlsMode
Definition: TlsOptions.h:27
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
std::function< void(void *)> DeleteTlsContextImplCallback
Definition: TlsOptions.h:431
std::function< std::shared_ptr< ClientTlsChannelHandler >(struct aws_channel_slot *slot, const struct aws_tls_connection_options &options, Allocator *allocator)> NewClientTlsHandlerCallback
Definition: TlsOptions.h:493
AWS_CRT_CPP_API int LastError() noexcept
Definition: Api.cpp:469
std::function< void *(TlsContextOptions &, TlsMode, Allocator *)> NewTlsContextImplCallback
Definition: TlsOptions.h:430
Definition: TlsOptions.h:241
Definition: TlsOptions.h:317
int GetInitializationError() const noexcept
Definition: TlsOptions.h:414
Definition: Allocator.h:10
Definition: TlsOptions.h:437