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