xref: /aosp_15_r20/external/pigweed/pw_tls_client_mbedtls/public/pw_tls_client_mbedtls/backend_types.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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/ctr_drbg.h"
22 #include "mbedtls/entropy.h"
23 #include "mbedtls/error.h"
24 #include "mbedtls/ssl.h"
25 PW_MODIFY_DIAGNOSTICS_POP();
26 
27 #include "pw_status/status.h"
28 #include "pw_tls_client/options.h"
29 
30 namespace pw::tls_client::backend {
31 class SessionImplementation {
32  public:
33   SessionImplementation(SessionOptions options);
34   ~SessionImplementation();
35   Status Setup();
SetTlsStatus(TLSStatus status)36   void SetTlsStatus(TLSStatus status) { tls_status_ = status; }
GetTlsStatus()37   TLSStatus GetTlsStatus() { return tls_status_; }
38 
39   // The method is for test only. When given a non-Ok status, it will override
40   // the status returned by entropy source pw::tls_client::GetRandomBytes();
41   static void SetEntropySourceStatus(Status status);
42 
43  private:
44   // mbedtls entropy
45   mbedtls_entropy_context entropy_ctx_;
46   mbedtls_ctr_drbg_context drbg_ctx_;
47 
48   // SSL data structure
49   mbedtls_ssl_context ssl_ctx_;
50 
51   // Configuration data structure
52   mbedtls_ssl_config ssl_config_;
53 
54   // A copy of the option when creating the client.
55   SessionOptions session_options_;
56 
57   TLSStatus tls_status_ = TLSStatus::kOk;
58 
59   static int MbedTlsWrite(void* ctx, const uint8_t* buf, size_t len);
60   static int MbedTlsRead(void* ctx, unsigned char* buf, size_t len);
61   static int MbedTlsEntropySource(void* ctx,
62                                   unsigned char* out,
63                                   size_t len,
64                                   size_t* output_length);
65 
66   static Status entropy_source_status_;
67 };
68 
69 }  // namespace pw::tls_client::backend
70