Skip to content

Commit 79d38e5

Browse files
committed
fix(modem): TLS example: Added restore session support in mbedtls-wrap
Reusable component in modem_tcp_client example implements a simple mbedtls wrapper. This update add support for mbedtls deinit() and for saving and restoring TLS session.
1 parent 7faa974 commit 79d38e5

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

components/esp_modem/examples/modem_tcp_client/components/extra_tcp_transports/include/mbedtls_wrap.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#pragma once
77

88
#include <utility>
9+
#include <memory>
910
#include <span>
1011
#include "mbedtls/ssl.h"
1112
#include "mbedtls/entropy.h"
@@ -22,6 +23,7 @@ class Tls {
2223
Tls();
2324
virtual ~Tls();
2425
bool init(is_server server, do_verify verify);
26+
bool deinit();
2527
int handshake();
2628
int write(const unsigned char *buf, size_t len);
2729
int read(unsigned char *buf, size_t len);
@@ -41,12 +43,33 @@ class Tls {
4143
mbedtls_entropy_context entropy_{};
4244
virtual void delay() {}
4345

46+
bool set_session();
47+
bool get_session();
48+
void reset_session();
49+
bool is_session_loaded();
50+
4451
private:
4552
static void print_error(const char *function, int error_code);
4653
static int bio_write(void *ctx, const unsigned char *buf, size_t len);
4754
static int bio_read(void *ctx, unsigned char *buf, size_t len);
4855
int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
4956
const unsigned char *key, size_t keylen,
5057
const unsigned char *pwd, size_t pwdlen);
58+
struct unique_session {
59+
unique_session()
60+
{
61+
::mbedtls_ssl_session_init(&s);
62+
}
63+
~unique_session()
64+
{
65+
::mbedtls_ssl_session_free(&s);
66+
}
67+
mbedtls_ssl_session *ptr()
68+
{
69+
return &s;
70+
}
71+
mbedtls_ssl_session s;
72+
};
73+
std::unique_ptr<unique_session> session_;
5174

5275
};

components/esp_modem/examples/modem_tcp_client/components/extra_tcp_transports/mbedtls_wrap.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ bool Tls::init(is_server server, do_verify verify)
3535
return true;
3636
}
3737

38+
bool Tls::deinit()
39+
{
40+
::mbedtls_ssl_config_free(&conf_);
41+
::mbedtls_ssl_free(&ssl_);
42+
::mbedtls_pk_free(&pk_key_);
43+
::mbedtls_x509_crt_free(&public_cert_);
44+
::mbedtls_x509_crt_free(&ca_cert_);
45+
return true;
46+
}
47+
3848
void Tls::print_error(const char *function, int error_code)
3949
{
4050
static char error_buf[100];
@@ -132,3 +142,39 @@ Tls::~Tls()
132142
::mbedtls_x509_crt_free(&public_cert_);
133143
::mbedtls_x509_crt_free(&ca_cert_);
134144
}
145+
146+
bool Tls::get_session()
147+
{
148+
if (session_ == nullptr) {
149+
session_ = std::make_unique<unique_session>();
150+
}
151+
int ret = ::mbedtls_ssl_get_session(&ssl_, session_->ptr());
152+
if (ret != 0) {
153+
print_error("mbedtls_ssl_get_session() failed", ret);
154+
return false;
155+
}
156+
return true;
157+
}
158+
159+
bool Tls::set_session()
160+
{
161+
if (session_ == nullptr) {
162+
printf("session hasn't been initialized");
163+
return false;
164+
}
165+
int ret = mbedtls_ssl_set_session(&ssl_, session_->ptr());
166+
if (ret != 0) {
167+
print_error("mbedtls_ssl_set_session() failed", ret);
168+
return false;
169+
}
170+
return true;
171+
}
172+
173+
void Tls::reset_session()
174+
{
175+
session_.reset(nullptr);
176+
}
177+
bool Tls::is_session_loaded()
178+
{
179+
return session_ != nullptr;
180+
}

0 commit comments

Comments
 (0)