SSLClient  v1.3.0
Add TLS 1.2 functionality to any network library.
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 "SSLObj.h"
25 #include <vector>
26 
27 #ifndef SSLClient_H_
28 #define SSLClient_H_
29 
35 class SSLClient : public Client {
36 public:
45  enum Error {
46  SSL_OK = 0,
59  };
60 
67  enum DebugLevel {
69  SSL_NONE = 0,
71  SSL_ERROR = 1,
73  SSL_WARN = 2,
75  SSL_INFO = 3,
76  };
77 
95  explicit SSLClient( Client& client,
96  const br_x509_trust_anchor *trust_anchors,
97  const size_t trust_anchors_num,
98  const int analog_pin,
99  const size_t max_sessions = 1,
100  const DebugLevel debug = SSL_WARN);
101 
102  //========================================
103  //= Functions implemented in SSLClient.cpp
104  //========================================
105 
145  int connect(IPAddress ip, uint16_t port) override;
146 
183  int connect(const char *host, uint16_t port) override;
184 
208  size_t write(const uint8_t *buf, size_t size) override;
210  size_t write(uint8_t b) override { return write(&b, 1); }
211 
230  int available() override;
231 
253  int read(uint8_t *buf, size_t size) override;
258  int read() override { uint8_t read_val; return read(&read_val, 1) > 0 ? read_val : -1; };
259 
268  int peek() override;
269 
277  void flush() override;
278 
287  void stop() override;
288 
302  uint8_t connected() override;
303 
304  //========================================
305  //= Functions Not in the Client Interface
306  //========================================
307 
316  void setMutualAuthParams(const SSLClientParameters* params);
317 
332  SSLSession* getSession(const char* host);
333 
342  void removeSession(const char* host);
343 
349  size_t getSessionCount() const { return m_sessions.size(); }
350 
356  operator bool() { return connected() > 0; }
357 
359  Client& getClient() { return m_client; }
360 
361 private:
363  Client& get_arduino_client() { return m_client; }
364  const Client& get_arduino_client() const { return m_client; }
365 
367  bool m_soft_connected(const char* func_name);
369  int m_start_ssl(const char* host = nullptr, SSLSession* ssl_ses = nullptr);
371  int m_run_until(const unsigned target);
373  unsigned m_update_engine();
375  int m_get_session_index(const char* host) const;
376 
378  void m_print_prefix(const char* func_name, const DebugLevel level) const;
379 
381  void m_print_ssl_error(const int ssl_error, const DebugLevel level) const;
382 
384  void m_print_br_error(const unsigned br_error_code, const DebugLevel level) const;
385 
387  template<typename T>
388  void m_print(const T str, const char* func_name, const DebugLevel level) const {
389  // check the current debug level and serial status
390  if (level > m_debug || !Serial) return;
391  // print prefix
392  m_print_prefix(func_name, level);
393  // print the message
394  Serial.println(str);
395  }
396 
398  template<typename T>
399  void m_info(const T str, const char* func_name) const { m_print(str, func_name, SSL_INFO); }
400 
401  template<typename T>
402  void m_warn(const T str, const char* func_name) const { m_print(str, func_name, SSL_WARN); }
403 
404  template<typename T>
405  void m_error(const T str, const char* func_name) const { m_print(str, func_name, SSL_ERROR); }
406 
407  //============================================
408  //= Data Members
409  //============================================
410  // create a copy of the client
411  Client& m_client;
412  // also store an array of SSLSessions, so we can resume communication with multiple websites
413  std::vector<SSLSession> m_sessions;
414  // as well as the maximmum number of sessions we can store
415  const size_t m_max_sessions;
416  // store the pin to fetch an RNG see from
417  const int m_analog_pin;
418  // store whether to enable debug logging
419  const DebugLevel m_debug;
420  // store if we are connected in bearssl or not
421  bool m_is_connected;
422  // store the context values required for SSL
423  br_ssl_client_context m_sslctx;
424  br_x509_minimal_context m_x509ctx;
425  // use a mono-directional buffer by default to cut memory in half
426  // can expand to a bi-directional buffer with maximum of BR_SSL_BUFSIZE_BIDI
427  // or shrink to below BR_SSL_BUFSIZE_MONO, and bearSSL will adapt automatically
428  // simply edit this value to change the buffer size to the desired value
429  // additionally, we need to correct buffer size based off of how many sessions we decide to cache
430  // since SSL takes so much memory if we don't it will cause the stack and heap to collide
438  unsigned char m_iobuf[2048];
439  // store the index of where we are writing in the buffer
440  // so we can send our records all at once to prevent
441  // weird timing issues
442  size_t m_write_idx;
443 };
444 
445 #endif
uint8_t connected() override
Check if the device is connected.
Definition: SSLClient.cpp:255
Definition: SSLClient.h:58
This class stores values which allow SSLClient to save and resume SSL sessions.
Definition: SSLSession.h:51
Definition: SSLClient.h:48
Definition: SSLClient.h:75
Definition: SSLClient.h:54
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:55
void flush() override
Force writing the buffered bytes from SSLClient::write to the network.
Definition: SSLClient.cpp:222
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:286
This struct stores data required for SSLClient to use mutual authentication.
Definition: SSLClientParameters.h:52
void setMutualAuthParams(const SSLClientParameters *params)
Add a client certificate and enable support for mutual auth.
Definition: SSLClient.cpp:310
int available() override
Returns the number of bytes available to read from the data that has been received and decrypted.
Definition: SSLClient.cpp:174
The main SSLClient class. Check out README.md for more info.
Definition: SSLClient.h:35
Definition: SSLClient.h:73
void stop() override
Close the connection.
Definition: SSLClient.cpp:228
Definition: SSLClient.h:71
int connect(IPAddress ip, uint16_t port) override
Connect over SSL to a host specified by an IP address.
Definition: SSLClient.cpp:82
size_t write(const uint8_t *buf, size_t size) override
Write some bytes to the SSL connection.
Definition: SSLClient.cpp:131
int read() override
Read a single byte, or -1 if none is available.
Definition: SSLClient.h:258
Error
Static constants defining the possible errors encountered.
Definition: SSLClient.h:45
Definition: SSLClient.h:52
DebugLevel
Level of verbosity used in logging for SSLClient.
Definition: SSLClient.h:67
size_t getSessionCount() const
Get the maximum number of SSL sessions that can be stored at once.
Definition: SSLClient.h:349
int peek() override
View the first byte of the buffer, without removing it from the SSLClient Buffer.
Definition: SSLClient.cpp:210
Definition: SSLClient.h:50
size_t write(uint8_t b) override
Definition: SSLClient.h:210
Client & getClient()
Returns a reference to the client object stored in this class. Take care not to break it.
Definition: SSLClient.h:359
void removeSession(const char *host)
Clear the session corresponding to a host and IP.
Definition: SSLClient.cpp:299
Definition: SSLClient.h:69
Definition: SSLClient.h:46
Definition: SSLClient.h:56