SSLClientImpl.h
Go to the documentation of this file.
/* Copyright 2019 OSU OPEnS Lab
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "bearssl.h"
#include "Arduino.h"
#include "Client.h"
#include "SSLSession.h"
#include "SSLClientParameters.h"
#ifndef SSLClientImpl_H_
#define SSLClientImpl_H_
enum Error {
SSL_OK = 0,
SSL_CLIENT_CONNECT_FAIL,
SSL_BR_CONNECT_FAIL,
SSL_CLIENT_WRTIE_ERROR,
SSL_BR_WRITE_ERROR,
SSL_INTERNAL_ERROR,
SSL_OUT_OF_MEMORY
};
enum DebugLevel {
SSL_NONE = 0,
SSL_ERROR = 1,
SSL_WARN = 2,
SSL_INFO = 3,
};
class SSLClientImpl : public Client {
public:
explicit SSLClientImpl(const br_x509_trust_anchor *trust_anchors,
const size_t trust_anchors_num, const int analog_pin,
const DebugLevel debug);
explicit SSLClientImpl(const br_x509_trust_anchor *trust_anchors,
const size_t trust_anchors_num, const int analog_pin,
const DebugLevel debug, const SSLClientParameters* mutual_auth_params);
//============================================
//= Functions implemented in SSLClientImpl.cpp
//============================================
int connect_impl(IPAddress ip, uint16_t port);
int connect_impl(const char *host, uint16_t port);
size_t write_impl(const uint8_t *buf, size_t size);
int available_impl();
int read_impl(uint8_t *buf, size_t size);
int peek_impl();
void flush_impl();
void stop_impl();
uint8_t connected_impl();
SSLSession& get_session_impl(const char* host, const IPAddress& addr);
void remove_session_impl(const char* host, const IPAddress& addr);
void set_mutual_impl(const SSLClientParameters* params);
//============================================
//= Functions implemented in SSLClient.h
//============================================
virtual uint16_t localPort() = 0;
virtual IPAddress remoteIP() = 0;
virtual uint16_t remotePort() = 0;
virtual size_t getSessionCount() const = 0;
protected:
virtual Client& get_arduino_client() = 0;
virtual const Client& get_arduino_client() const = 0;
virtual SSLSession* get_session_array() = 0;
virtual const SSLSession* get_session_array() const = 0;
//============================================
//= Functions implemented in SSLClientImpl.cpp
//============================================
void m_print_prefix(const char* func_name, const DebugLevel level) const;
void m_print_ssl_error(const int ssl_error, const DebugLevel level) const;
void m_print_br_error(const unsigned br_error_code, const DebugLevel level) const;
template<typename T>
void m_print(const T str, const char* func_name, const DebugLevel level) const {
// check the current debug level and serial status
if (level > m_debug || !Serial) return;
// print prefix
m_print_prefix(func_name, level);
// print the message
Serial.println(str);
}
template<typename T>
void m_info(const T str, const char* func_name) const { m_print(str, func_name, SSL_INFO); }
template<typename T>
void m_warn(const T str, const char* func_name) const { m_print(str, func_name, SSL_WARN); }
template<typename T>
void m_error(const T str, const char* func_name) const { m_print(str, func_name, SSL_ERROR); }
private:
bool m_soft_connected(const char* func_name);
int m_start_ssl(const char* host, SSLSession& ssl_ses);
int m_run_until(const unsigned target);
unsigned m_update_engine();
int m_get_session_index(const char* host, const IPAddress& addr) const;
//============================================
//= Data Members
//============================================
// store the pin to fetch an RNG see from
const int m_analog_pin;
// store an index of where a new session can be placed if we don't have any corresponding sessions
size_t m_session_index;
// store whether to enable debug logging
const DebugLevel m_debug;
// store if we are connected in bearssl or not
bool m_is_connected;
// store the context values required for SSL
br_ssl_client_context m_sslctx;
br_x509_minimal_context m_x509ctx;
// use a mono-directional buffer by default to cut memory in half
// can expand to a bi-directional buffer with maximum of BR_SSL_BUFSIZE_BIDI
// or shrink to below BR_SSL_BUFSIZE_MONO, and bearSSL will adapt automatically
// simply edit this value to change the buffer size to the desired value
// additionally, we need to correct buffer size based off of how many sessions we decide to cache
// since SSL takes so much memory if we don't it will cause the stack and heap to collide
unsigned char m_iobuf[2048];
// 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
size_t m_write_idx;
};
#endif /* SSLClientImpl_H_ */
SSLClientImpl::write_impl
size_t write_impl(const uint8_t *buf, size_t size)
Definition: SSLClientImpl.cpp:130
SSLClientImpl::remotePort
virtual uint16_t remotePort()=0
SSLClientImpl::m_print
void m_print(const T str, const char *func_name, const DebugLevel level) const
debugging print function, only prints if m_debug is true
Definition: SSLClientImpl.h:147
SSL_WARN
Definition: SSLClientImpl.h:66
SSLClientImpl::remoteIP
virtual IPAddress remoteIP()=0
SSLClientImpl::get_session_impl
SSLSession & get_session_impl(const char *host, const IPAddress &addr)
Definition: SSLClientImpl.cpp:286
SSLSession
This class stores values which allow SSLClient to save and resume SSL sessions.
Definition: SSLSession.h:52
SSLClientImpl::m_info
void m_info(const T str, const char *func_name) const
Prints a info message to serial, if info messages are enabled.
Definition: SSLClientImpl.h:158
SSLClientImpl::SSLClientImpl
SSLClientImpl(const br_x509_trust_anchor *trust_anchors, const size_t trust_anchors_num, const int analog_pin, const DebugLevel debug)
Definition: SSLClientImpl.cpp:53
SSLClientImpl::m_error
void m_error(const T str, const char *func_name) const
Definition: SSLClientImpl.h:164
SSLClientImpl::peek_impl
int peek_impl()
Definition: SSLClientImpl.cpp:209
SSL_INFO
Definition: SSLClientImpl.h:68
SSL_ERROR
Definition: SSLClientImpl.h:64
SSLClientParameters
This struct stores data required for SSLClient to use mutual authentication.
Definition: SSLClientParameters.h:52
SSL_INTERNAL_ERROR
Definition: SSLClientImpl.h:49
SSLClientImpl::getSessionCount
virtual size_t getSessionCount() const =0
SSLClientImpl::get_session_array
virtual SSLSession * get_session_array()=0
SSL_BR_WRITE_ERROR
Definition: SSLClientImpl.h:47
SSL_OK
Definition: SSLClientImpl.h:39
SSLClientImpl::m_print_ssl_error
void m_print_ssl_error(const int ssl_error, const DebugLevel level) const
Prints the string associated with a write error.
Definition: SSLClientImpl.cpp:668
SSLClientImpl::available_impl
int available_impl()
Definition: SSLClientImpl.cpp:173
Error
Error
Static constants defining the possible errors encountered.
Definition: SSLClientImpl.h:38
SSL_BR_CONNECT_FAIL
Definition: SSLClientImpl.h:43
SSLClientImpl::read_impl
int read_impl(uint8_t *buf, size_t size)
Definition: SSLClientImpl.cpp:194
SSLClientImpl::remove_session_impl
void remove_session_impl(const char *host, const IPAddress &addr)
Definition: SSLClientImpl.cpp:305
SSL_CLIENT_WRTIE_ERROR
Definition: SSLClientImpl.h:45
SSLClientImpl::get_arduino_client
virtual Client & get_arduino_client()=0
SSL_CLIENT_CONNECT_FAIL
Definition: SSLClientImpl.h:41
SSLClientImpl::m_print_prefix
void m_print_prefix(const char *func_name, const DebugLevel level) const
Prints a debugging prefix to all logs, so we can attatch them to useful information.
Definition: SSLClientImpl.cpp:650
SSLClientImpl::set_mutual_impl
void set_mutual_impl(const SSLClientParameters *params)
Definition: SSLClientImpl.cpp:316
SSL_NONE
Definition: SSLClientImpl.h:62
SSLClientImpl::m_print_br_error
void m_print_br_error(const unsigned br_error_code, const DebugLevel level) const
Print the text string associated with a BearSSL error code.
Definition: SSLClientImpl.cpp:683
SSLClientImpl::m_warn
void m_warn(const T str, const char *func_name) const
Definition: SSLClientImpl.h:161
SSLClientParameters.h
SSLSession.h
SSLClientImpl::connect_impl
int connect_impl(IPAddress ip, uint16_t port)
Definition: SSLClientImpl.cpp:73
SSL_OUT_OF_MEMORY
Definition: SSLClientImpl.h:51
SSLClientImpl::stop_impl
void stop_impl()
Definition: SSLClientImpl.cpp:227
SSLClientImpl::flush_impl
void flush_impl()
Definition: SSLClientImpl.cpp:221
SSLClientImpl
Implementation code to be inherited by SSLClient.
Definition: SSLClientImpl.h:72
SSLClientImpl::localPort
virtual uint16_t localPort()=0
SSLClientImpl::connected_impl
uint8_t connected_impl()
Definition: SSLClientImpl.cpp:255
DebugLevel
DebugLevel
Level of verbosity used in logging for SSLClient.
Definition: SSLClientImpl.h:60