Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/SSLClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,39 @@ SSLClient::SSLClient( Client& client,
const size_t trust_anchors_num,
const int analog_pin,
const size_t max_sessions,
const size_t buffer_size,
const DebugLevel debug)
: m_client(client)
, m_sessions()
, m_max_sessions(max_sessions)
, m_iobuf(nullptr)
, m_iobuf_size(buffer_size)
, m_analog_pin(analog_pin)
, m_debug(debug)
, m_is_connected(false)
, m_write_idx(0)
, m_br_last_state(0) {

setTimeout(30*1000);
// Allocate buffer dynamically
m_iobuf = new unsigned char[m_iobuf_size];
// zero the iobuf just in case it's still garbage
memset(m_iobuf, 0, sizeof m_iobuf);
memset(m_iobuf, 0, m_iobuf_size);
// initlalize the various bearssl libraries so they're ready to go when we connect
br_client_init_TLS12_only(&m_sslctx, &m_x509ctx, trust_anchors, trust_anchors_num);
// comment the above line and uncomment the line below if you're having trouble connecting over SSL
// br_ssl_client_init_full(&m_sslctx, &m_x509ctx, m_trust_anchors, m_trust_anchors_num);
// check if the buffer size is half or full duplex
constexpr auto duplex = sizeof m_iobuf <= BR_SSL_BUFSIZE_MONO ? 0 : 1;
br_ssl_engine_set_buffer(&m_sslctx.eng, m_iobuf, sizeof m_iobuf, duplex);
const auto duplex = m_iobuf_size <= BR_SSL_BUFSIZE_MONO ? 0 : 1;
br_ssl_engine_set_buffer(&m_sslctx.eng, m_iobuf, m_iobuf_size, duplex);
}

/**
* @brief Destructor
* Frees the dynamically allocated m_iobuf.
*/
SSLClient::~SSLClient() {
delete[] m_iobuf; // Added destructor body
}

/* see SSLClient.h*/
Expand Down Expand Up @@ -719,4 +732,4 @@ void SSLClient::m_print_br_state(const unsigned state, const DebugLevel level) c
if (state & BR_SSL_SENDAPP) Serial.println(" SENDAPP");
if (state & BR_SSL_RECVAPP) Serial.println(" RECVAPP");
}
}
}
31 changes: 29 additions & 2 deletions src/SSLClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,41 @@ class SSLClient : public Client {
* @param trust_anchors_num The number of objects in the trust_anchors array.
* @param analog_pin An analog pin to pull random bytes from, used in seeding the RNG.
* @param max_sessions The maximum number of SSL sessions to store connection information from.
* @param buffer_size The size of the buffer used for SSL communication.
* @param debug The level of debug logging (use the ::DebugLevel enum).
*/
explicit SSLClient( Client& client,
const br_x509_trust_anchor *trust_anchors,
const size_t trust_anchors_num,
const int analog_pin,
const size_t max_sessions = 1,
const size_t buffer_size = 2048,
const DebugLevel debug = SSL_WARN);


/**
* @brief ORIGINAL (6-argument) Constructor for backward compatibility.
* This calls the new 7-argument constructor, passing a default buffer size of 2048.
*/
explicit SSLClient( Client& client,
const br_x509_trust_anchor *trust_anchors,
const size_t trust_anchors_num,
const int analog_pin,
const size_t max_sessions = 1,
const DebugLevel debug = SSL_WARN)
// This is a "delegating constructor"
// It calls the main 7-argument constructor with the default buffer size
: SSLClient(client, trust_anchors, trust_anchors_num, analog_pin, max_sessions, 2048, debug)
{
// Body is empty, all work is done by the main constructor
}

/**
* @brief Destructor
* Frees the dynamically allocated m_iobuf.
*/
~SSLClient();

//========================================
//= Functions implemented in SSLClient.cpp
//========================================
Expand Down Expand Up @@ -466,7 +492,8 @@ class SSLClient : public Client {
* As a rule of thumb SSLClient will fail if it does not have at least 8000 bytes when starting a
* connection.
*/
unsigned char m_iobuf[2048];
unsigned char* m_iobuf;
size_t m_iobuf_size;
// store the index of where we are writing in the buffer
// so we can send our records all at once to prevent
// weird timing issues
Expand All @@ -475,4 +502,4 @@ class SSLClient : public Client {
unsigned m_br_last_state;
};

#endif /** SSLClient_H_ */
#endif /** SSLClient_H_ */