• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #pragma once
16 
17 #include "pw_preprocessor/compiler.h"
18 
19 PW_MODIFY_DIAGNOSTICS_PUSH();
20 PW_MODIFY_DIAGNOSTIC(ignored, "-Wswitch-enum");
21 #include "mbedtls/certs.h"
22 #include "mbedtls/ctr_drbg.h"
23 #include "mbedtls/entropy.h"
24 #include "mbedtls/error.h"
25 #include "mbedtls/ssl.h"
26 PW_MODIFY_DIAGNOSTICS_POP();
27 
28 #include "pw_status/status.h"
29 #include "pw_tls_client/options.h"
30 
31 namespace pw::tls_client::backend {
32 class SessionImplementation {
33  public:
34   SessionImplementation(SessionOptions options);
35   ~SessionImplementation();
36   Status Setup();
SetTlsStatus(TLSStatus status)37   void SetTlsStatus(TLSStatus status) { tls_status_ = status; }
GetTlsStatus()38   TLSStatus GetTlsStatus() { return tls_status_; }
39 
40   // The method is for test only. When given a non-Ok status, it will override
41   // the status returned by entropy source pw::tls_client::GetRandomBytes();
42   static void SetEntropySourceStatus(Status status);
43 
44  private:
45   // mbedtls entropy
46   mbedtls_entropy_context entropy_ctx_;
47   mbedtls_ctr_drbg_context drbg_ctx_;
48 
49   // SSL data structure
50   mbedtls_ssl_context ssl_ctx_;
51 
52   // Configuration data structure
53   mbedtls_ssl_config ssl_config_;
54 
55   // A copy of the option when creating the client.
56   SessionOptions session_options_;
57 
58   TLSStatus tls_status_ = TLSStatus::kOk;
59 
60   static int MbedTlsWrite(void* ctx, const uint8_t* buf, size_t len);
61   static int MbedTlsRead(void* ctx, unsigned char* buf, size_t len);
62   static int MbedTlsEntropySource(void* ctx,
63                                   unsigned char* out,
64                                   size_t len,
65                                   size_t* output_length);
66 
67   static Status entropy_source_status_;
68 };
69 
70 }  // namespace pw::tls_client::backend
71