SSLClient  v1.3.0
Add TLS 1.2 functionality to any network library.
SSLClientImpl.h
Go to the documentation of this file.
1 /* Copyright 2019 OSU OPEnS Lab
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
4  * software and associated documentation files (the "Software"), to deal in the Software
5  * without restriction, including without limitation the rights to use, copy, modify,
6  * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7  * permit persons to whom the Software is furnished to do so, subject to the following
8  * conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in all
11  * copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
15  * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
16  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include "bearssl.h"
22 #include "Arduino.h"
23 #include "Client.h"
24 #include "SSLSession.h"
25 #include "SSLClientParameters.h"
26 
27 #ifndef SSLClientImpl_H_
28 #define SSLClientImpl_H_
29 
38 enum Error {
39  SSL_OK = 0,
52 };
53 
60 enum DebugLevel {
62  SSL_NONE = 0,
64  SSL_ERROR = 1,
66  SSL_WARN = 2,
68  SSL_INFO = 3,
69 };
70 
72 class SSLClientImpl : public Client {
73 public:
75  explicit SSLClientImpl(const br_x509_trust_anchor *trust_anchors,
76  const size_t trust_anchors_num, const int analog_pin,
77  const DebugLevel debug);
78 
80  explicit SSLClientImpl(const br_x509_trust_anchor *trust_anchors,
81  const size_t trust_anchors_num, const int analog_pin,
82  const DebugLevel debug, const SSLClientParameters* mutual_auth_params);
83 
84  //============================================
85  //= Functions implemented in SSLClientImpl.cpp
86  //============================================
87 
89  int connect_impl(IPAddress ip, uint16_t port);
91  int connect_impl(const char *host, uint16_t port);
93  size_t write_impl(const uint8_t *buf, size_t size);
95  int available_impl();
97  int read_impl(uint8_t *buf, size_t size);
99  int peek_impl();
101  void flush_impl();
103  void stop_impl();
105  uint8_t connected_impl();
107  SSLSession& get_session_impl(const char* host, const IPAddress& addr);
109  void remove_session_impl(const char* host, const IPAddress& addr);
111  void set_mutual_impl(const SSLClientParameters* params);
112  //============================================
113  //= Functions implemented in SSLClient.h
114  //============================================
116  virtual uint16_t localPort() = 0;
118  virtual IPAddress remoteIP() = 0;
120  virtual uint16_t remotePort() = 0;
122  virtual size_t getSessionCount() const = 0;
123 
124 protected:
126  virtual Client& get_arduino_client() = 0;
127  virtual const Client& get_arduino_client() const = 0;
129  virtual SSLSession* get_session_array() = 0;
130  virtual const SSLSession* get_session_array() const = 0;
131 
132  //============================================
133  //= Functions implemented in SSLClientImpl.cpp
134  //============================================
135 
137  void m_print_prefix(const char* func_name, const DebugLevel level) const;
138 
140  void m_print_ssl_error(const int ssl_error, const DebugLevel level) const;
141 
143  void m_print_br_error(const unsigned br_error_code, const DebugLevel level) const;
144 
146  template<typename T>
147  void m_print(const T str, const char* func_name, const DebugLevel level) const {
148  // check the current debug level and serial status
149  if (level > m_debug || !Serial) return;
150  // print prefix
151  m_print_prefix(func_name, level);
152  // print the message
153  Serial.println(str);
154  }
155 
157  template<typename T>
158  void m_info(const T str, const char* func_name) const { m_print(str, func_name, SSL_INFO); }
159 
160  template<typename T>
161  void m_warn(const T str, const char* func_name) const { m_print(str, func_name, SSL_WARN); }
162 
163  template<typename T>
164  void m_error(const T str, const char* func_name) const { m_print(str, func_name, SSL_ERROR); }
165 
166 private:
168  bool m_soft_connected(const char* func_name);
170  int m_start_ssl(const char* host, SSLSession& ssl_ses);
172  int m_run_until(const unsigned target);
174  unsigned m_update_engine();
176  int m_get_session_index(const char* host, const IPAddress& addr) const;
177 
178  //============================================
179  //= Data Members
180  //============================================
181 
182  // store the pin to fetch an RNG see from
183  const int m_analog_pin;
184  // store an index of where a new session can be placed if we don't have any corresponding sessions
185  size_t m_session_index;
186  // store whether to enable debug logging
187  const DebugLevel m_debug;
188  // store if we are connected in bearssl or not
189  bool m_is_connected;
190  // store the context values required for SSL
191  br_ssl_client_context m_sslctx;
192  br_x509_minimal_context m_x509ctx;
193  // use a mono-directional buffer by default to cut memory in half
194  // can expand to a bi-directional buffer with maximum of BR_SSL_BUFSIZE_BIDI
195  // or shrink to below BR_SSL_BUFSIZE_MONO, and bearSSL will adapt automatically
196  // simply edit this value to change the buffer size to the desired value
197  // additionally, we need to correct buffer size based off of how many sessions we decide to cache
198  // since SSL takes so much memory if we don't it will cause the stack and heap to collide
206  unsigned char m_iobuf[2048];
207  // store the index of where we are writing in the buffer
208  // so we can send our records all at once to prevent
209  // weird timing issues
210  size_t m_write_idx;
211 };
212 
213 #endif /* SSLClientImpl_H_ */
size_t write_impl(const uint8_t *buf, size_t size)
Definition: SSLClientImpl.cpp:130
virtual uint16_t remotePort()=0
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
Definition: SSLClientImpl.h:66
virtual IPAddress remoteIP()=0
SSLSession & get_session_impl(const char *host, const IPAddress &addr)
Definition: SSLClientImpl.cpp:286
This class stores values which allow SSLClient to save and resume SSL sessions.
Definition: SSLSession.h:52
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(const br_x509_trust_anchor *trust_anchors, const size_t trust_anchors_num, const int analog_pin, const DebugLevel debug)
Definition: SSLClientImpl.cpp:53
void m_error(const T str, const char *func_name) const
Definition: SSLClientImpl.h:164
int peek_impl()
Definition: SSLClientImpl.cpp:209
Definition: SSLClientImpl.h:68
Definition: SSLClientImpl.h:64
This struct stores data required for SSLClient to use mutual authentication.
Definition: SSLClientParameters.h:52
Definition: SSLClientImpl.h:49
virtual size_t getSessionCount() const =0
virtual SSLSession * get_session_array()=0
Definition: SSLClientImpl.h:47
Definition: SSLClientImpl.h:39
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
int available_impl()
Definition: SSLClientImpl.cpp:173
Error
Static constants defining the possible errors encountered.
Definition: SSLClientImpl.h:38
Definition: SSLClientImpl.h:43
int read_impl(uint8_t *buf, size_t size)
Definition: SSLClientImpl.cpp:194
void remove_session_impl(const char *host, const IPAddress &addr)
Definition: SSLClientImpl.cpp:305
Definition: SSLClientImpl.h:45
virtual Client & get_arduino_client()=0
Definition: SSLClientImpl.h:41
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
void set_mutual_impl(const SSLClientParameters *params)
Definition: SSLClientImpl.cpp:316
Definition: SSLClientImpl.h:62
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
void m_warn(const T str, const char *func_name) const
Definition: SSLClientImpl.h:161
int connect_impl(IPAddress ip, uint16_t port)
Definition: SSLClientImpl.cpp:73
Definition: SSLClientImpl.h:51
void stop_impl()
Definition: SSLClientImpl.cpp:227
void flush_impl()
Definition: SSLClientImpl.cpp:221
Implementation code to be inherited by SSLClient.
Definition: SSLClientImpl.h:72
virtual uint16_t localPort()=0
uint8_t connected_impl()
Definition: SSLClientImpl.cpp:255
DebugLevel
Level of verbosity used in logging for SSLClient.
Definition: SSLClientImpl.h:60