aws-crt-cpp
TlsOptions.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License").
6  * You may not use this file except in compliance with the License.
7  * A copy of the License is located at
8  *
9  * http://aws.amazon.com/apache2.0
10  *
11  * or in the "license" file accompanying this file. This file is distributed
12  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 
17 #include <aws/crt/Types.h>
18 
19 #include <aws/io/tls_channel_handler.h>
20 
21 struct aws_tls_ctx_options;
22 
23 namespace Aws
24 {
25  namespace Crt
26  {
27  namespace Io
28  {
29  enum class TlsMode
30  {
31  CLIENT,
32  SERVER,
33  };
34 
36  {
37  friend class TlsContext;
38 
39  public:
40  TlsContextOptions() noexcept;
42  TlsContextOptions(const TlsContextOptions &) noexcept = delete;
43  TlsContextOptions &operator=(const TlsContextOptions &) noexcept = delete;
45  TlsContextOptions &operator=(TlsContextOptions &&) noexcept;
49  explicit operator bool() const noexcept { return m_isInit; }
53  int LastError() const noexcept { return aws_last_error(); }
54 
59  static TlsContextOptions InitDefaultClient(Allocator *allocator = g_allocator) noexcept;
67  static TlsContextOptions InitClientWithMtls(
68  const char *cert_path,
69  const char *pkey_path,
70  Allocator *allocator = g_allocator) noexcept;
71 
79  static TlsContextOptions InitClientWithMtls(
80  const ByteCursor &cert,
81  const ByteCursor &pkey,
82  Allocator *allocator = g_allocator) noexcept;
83 
84 #ifdef __APPLE__
85 
94  static TlsContextOptions InitClientWithMtlsPkcs12(
95  const char *pkcs12_path,
96  const char *pkcs12_pwd,
97  Allocator *allocator = g_allocator) noexcept;
98 #endif
99 
104  static bool IsAlpnSupported() noexcept;
105 
111  bool SetAlpnList(const char *alpnList) noexcept;
112 
121  void SetVerifyPeer(bool verifyPeer) noexcept;
122 
131  bool OverrideDefaultTrustStore(const char *caPath, const char *caFile) noexcept;
136  bool OverrideDefaultTrustStore(const ByteCursor &ca) noexcept;
137 
138  private:
139  aws_tls_ctx_options m_options;
140  bool m_isInit;
141  };
142 
147  {
148  public:
149  TlsConnectionOptions() noexcept;
151  TlsConnectionOptions(const TlsConnectionOptions &) noexcept;
152  TlsConnectionOptions &operator=(const TlsConnectionOptions &) noexcept;
153  TlsConnectionOptions(TlsConnectionOptions &&options) noexcept;
154  TlsConnectionOptions &operator=(TlsConnectionOptions &&options) noexcept;
155 
161  bool SetServerName(ByteCursor &serverName) noexcept;
162 
169  bool SetAlpnList(const char *alpnList) noexcept;
173  explicit operator bool() const noexcept { return m_isInit; }
177  int LastError() const noexcept { return m_lastError; }
179  const aws_tls_connection_options *GetUnderlyingHandle() const noexcept
180  {
181  return &m_tls_connection_options;
182  }
183 
184  private:
185  TlsConnectionOptions(aws_tls_ctx *ctx, Allocator *allocator) noexcept;
186  aws_tls_connection_options m_tls_connection_options;
187  aws_allocator *m_allocator;
188  int m_lastError;
189  bool m_isInit;
190 
191  friend class TlsContext;
192  };
193 
195  {
196  public:
197  TlsContext() noexcept;
198  TlsContext(TlsContextOptions &options, TlsMode mode, Allocator *allocator = g_allocator) noexcept;
199  ~TlsContext() = default;
200  TlsContext(const TlsContext &) noexcept = default;
201  TlsContext &operator=(const TlsContext &) noexcept = default;
202  TlsContext(TlsContext &&) noexcept = default;
203  TlsContext &operator=(TlsContext &&) noexcept = default;
204 
205  TlsConnectionOptions NewConnectionOptions() const noexcept;
209  explicit operator bool() const noexcept { return m_ctx && m_initializationError == AWS_ERROR_SUCCESS; }
213  int GetInitializationError() const noexcept { return m_initializationError; }
214 
215  private:
216  std::shared_ptr<aws_tls_ctx> m_ctx;
217  int m_initializationError;
218  };
219 
222 
223  } // namespace Io
224  } // namespace Crt
225 } // namespace Aws
Aws::Crt::Allocator
aws_allocator Allocator
Definition: StlAllocator.h:25
Aws::Crt::Io::TlsMode::SERVER
@ SERVER
Types.h
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:34
Aws::Crt::Io::TlsContext::~TlsContext
~TlsContext()=default
Aws::Crt::Io::TlsMode
TlsMode
Definition: TlsOptions.h:30
Aws::Crt::ByteCursor
aws_byte_cursor ByteCursor
Definition: Types.h:44
Aws::Crt::Io::TlsConnectionOptions::LastError
int LastError() const noexcept
Definition: TlsOptions.h:177
Aws::Crt::g_allocator
Allocator * g_allocator
Definition: Api.cpp:28
Aws::Crt::Io::TlsContext::operator=
TlsContext & operator=(const TlsContext &) noexcept=default
Aws::Crt::Io::TlsContext::TlsContext
TlsContext(TlsContext &&) noexcept=default
Aws::Crt::Io::TlsContextOptions::LastError
int LastError() const noexcept
Definition: TlsOptions.h:53
Aws
Definition: Api.h:25
Aws::Crt::Io::TlsContext
Definition: TlsOptions.h:195
Aws::Crt::Io::InitTlsStaticState
AWS_CRT_CPP_API void InitTlsStaticState(Allocator *alloc) noexcept
Aws::Crt::Io::TlsContext::TlsContext
TlsContext(const TlsContext &) noexcept=default
Aws::Crt::Io::TlsConnectionOptions
Definition: TlsOptions.h:147
Aws::Crt::Io::TlsMode::CLIENT
@ CLIENT
Aws::Crt::Io::CleanUpTlsStaticState
AWS_CRT_CPP_API void CleanUpTlsStaticState() noexcept
Aws::Crt::Io::TlsContextOptions
Definition: TlsOptions.h:36
Aws::Crt::Io::TlsContext::GetInitializationError
int GetInitializationError() const noexcept
Definition: TlsOptions.h:213