SSLClient  v1.5.0
SSLClient.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 "Client.h"
22 #include "SSLSession.h"
23 #include "SSLClientParameters.h"
24 #include <vector>
25 
26 #ifndef SSLClient_H_
27 #define SSLClient_H_
28 
34 class SSLClient : public Client {
35 public:
44  enum Error {
45  SSL_OK = 0,
58  };
59 
66  enum DebugLevel {
68  SSL_NONE = 0,
70  SSL_ERROR = 1,
72  SSL_WARN = 2,
74  SSL_INFO = 3,
75  };
76 
94  explicit SSLClient( Client& client,
95  const br_x509_trust_anchor *trust_anchors,
96  const size_t trust_anchors_num,
97  const int analog_pin,
98  const size_t max_sessions = 1,
99  const DebugLevel debug = SSL_WARN);
100 
101  //========================================
102  //= Functions implemented in SSLClient.cpp
103  //========================================
104 
144  int connect(IPAddress ip, uint16_t port) override;
145 
182  int connect(const char *host, uint16_t port) override;
183 
207  size_t write(const uint8_t *buf, size_t size) override;
209  size_t write(uint8_t b) override { return write(&b, 1); }
210 
229  int available() override;
230 
252  int read(uint8_t *buf, size_t size) override;
257  int read() override { uint8_t read_val; return read(&read_val, 1) > 0 ? read_val : -1; };
258 
267  int peek() override;
268 
276  void flush() override;
277 
286  void stop() override;
287 
301  uint8_t connected() override;
302 
303  //========================================
304  //= Functions Not in the Client Interface
305  //========================================
306 
315  void setMutualAuthParams(const SSLClientParameters& params);
316 
331  SSLSession* getSession(const char* host);
332 
341  void removeSession(const char* host);
342 
348  size_t getSessionCount() const { return m_sessions.size(); }
349 
355  operator bool() { return connected() > 0; }
356 
358  Client& getClient() { return m_client; }
359 
364  void setTimeout(unsigned int t) { m_timeout = t; }
365 
370  unsigned int getTimeout() const { return m_timeout; }
371 
372 private:
374  Client& get_arduino_client() { return m_client; }
375  const Client& get_arduino_client() const { return m_client; }
376 
378  bool m_soft_connected(const char* func_name);
380  int m_start_ssl(const char* host = nullptr, SSLSession* ssl_ses = nullptr);
382  int m_run_until(const unsigned target);
384  unsigned m_update_engine();
386  int m_get_session_index(const char* host) const;
387 
389  void m_print_prefix(const char* func_name, const DebugLevel level) const;
390 
392  void m_print_ssl_error(const int ssl_error, const DebugLevel level) const;
393 
395  void m_print_br_error(const unsigned br_error_code, const DebugLevel level) const;
396 
398  template<typename T>
399  void m_print(const T str, const char* func_name, const DebugLevel level) const {
400  // check the current debug level and serial status
401  if (level > m_debug || !Serial) return;
402  // print prefix
403  m_print_prefix(func_name, level);
404  // print the message
405  Serial.println(str);
406  }
407 
409  template<typename T>
410  void m_info(const T str, const char* func_name) const { m_print(str, func_name, SSL_INFO); }
411 
412  template<typename T>
413  void m_warn(const T str, const char* func_name) const { m_print(str, func_name, SSL_WARN); }
414 
415  template<typename T>
416  void m_error(const T str, const char* func_name) const { m_print(str, func_name, SSL_ERROR); }
417 
418  //============================================
419  //= Data Members
420  //============================================
421  // create a reference the client
422  Client& m_client;
423  // also store an array of SSLSessions, so we can resume communication with multiple websites
424  std::vector<SSLSession> m_sessions;
425  // as well as the maximmum number of sessions we can store
426  const size_t m_max_sessions;
427  // store the pin to fetch an RNG see from
428  const int m_analog_pin;
429  // store whether to enable debug logging
430  const DebugLevel m_debug;
431  // store if we are connected in bearssl or not
432  bool m_is_connected;
433  // store the timeout for SSL internals
434  unsigned int m_timeout;
435  // store the context values required for SSL
436  br_ssl_client_context m_sslctx;
437  br_x509_minimal_context m_x509ctx;
438  // use a mono-directional buffer by default to cut memory in half
439  // can expand to a bi-directional buffer with maximum of BR_SSL_BUFSIZE_BIDI
440  // or shrink to below BR_SSL_BUFSIZE_MONO, and bearSSL will adapt automatically
441  // simply edit this value to change the buffer size to the desired value
442  // additionally, we need to correct buffer size based off of how many sessions we decide to cache
443  // since SSL takes so much memory if we don't it will cause the stack and heap to collide
451  unsigned char m_iobuf[2048];
452  // store the index of where we are writing in the buffer
453  // so we can send our records all at once to prevent
454  // weird timing issues
455  size_t m_write_idx;
456 };
457 
458 #endif
uint8_t connected() override
Check if the device is connected.
Definition: SSLClient.cpp:220
Definition: SSLClient.h:57
This class stores values which allow SSLClient to save and resume SSL sessions.
Definition: SSLSession.h:51
void setTimeout(unsigned int t)
Set the timeout when waiting for an SSL response.
Definition: SSLClient.h:364
Definition: SSLClient.h:47
Definition: SSLClient.h:74
Definition: SSLClient.h:53
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)
Initialize SSLClient with all of the prerequisites needed.
Definition: SSLClient.cpp:24
void flush() override
Force writing the buffered bytes from SSLClient::write to the network.
Definition: SSLClient.cpp:187
SSLSession * getSession(const char *host)
Gets a session reference corresponding to a host and IP, or a reference to a empty session if none ex...
Definition: SSLClient.cpp:251
int available() override
Returns the number of bytes available to read from the data that has been received and decrypted.
Definition: SSLClient.cpp:139
The main SSLClient class. Check out README.md for more info.
Definition: SSLClient.h:34
Definition: SSLClient.h:72
void stop() override
Close the connection.
Definition: SSLClient.cpp:193
Definition: SSLClient.h:70
int connect(IPAddress ip, uint16_t port) override
Connect over SSL to a host specified by an IP address.
Definition: SSLClient.cpp:51
size_t write(const uint8_t *buf, size_t size) override
Write some bytes to the SSL connection.
Definition: SSLClient.cpp:96
int read() override
Read a single byte, or -1 if none is available.
Definition: SSLClient.h:257
void setMutualAuthParams(const SSLClientParameters &params)
Add a client certificate and enable support for mutual auth.
Definition: SSLClient.cpp:275
Error
Static constants defining the possible errors encountered.
Definition: SSLClient.h:44
Definition: SSLClient.h:51
DebugLevel
Level of verbosity used in logging for SSLClient.
Definition: SSLClient.h:66
size_t getSessionCount() const
Get the maximum number of SSL sessions that can be stored at once.
Definition: SSLClient.h:348
int peek() override
View the first byte of the buffer, without removing it from the SSLClient Buffer.
Definition: SSLClient.cpp:175
Definition: SSLClient.h:49
size_t write(uint8_t b) override
Definition: SSLClient.h:209
Client & getClient()
Returns a reference to the client object stored in this class. Take care not to break it.
Definition: SSLClient.h:358
void removeSession(const char *host)
Clear the session corresponding to a host and IP.
Definition: SSLClient.cpp:264
unsigned int getTimeout() const
Get the timeout when waiting for an SSL response.
Definition: SSLClient.h:370
This class stores data required for SSLClient to use mutual authentication.
Definition: SSLClientParameters.h:52
Definition: SSLClient.h:68
Definition: SSLClient.h:45
Definition: SSLClient.h:55