SSLClient  v1.1.1
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 "SSLClientImpl.h"
23 #include "SSLSession.h"
24 #include "SSLClientParameters.h"
25 
26 #ifndef SSLClient_H_
27 #define SSLClient_H_
28 
34 template <class C, size_t SessionCache = 1>
35 class SSLClient : public SSLClientImpl {
36 /*
37  * static checks
38  * I'm a java developer, so I want to ensure that my inheritance is safe.
39  * These checks ensure that all the functions we use on class C are
40  * actually present on class C. It does this by checking that the
41  * class inherits from Client.
42  *
43  * Additionally, I ran into a lot of memory issues with large sessions caches.
44  * Since each session contains at max 352 bytes of memory, they eat of the
45  * stack quite quickly and can cause overflows. As a result, I have added a
46  * warning here to discourage the use of more than 3 sessions at a time. Any
47  * amount past that will require special modification of this library, and
48  * assumes you know what you are doing.
49  */
50 static_assert(SessionCache > 0 && SessionCache < 255, "There can be no less than one and no more than 255 sessions in the cache!");
51 static_assert(SessionCache <= 3, "You need to decrease the size of m_iobuf in order to have more than 3 sessions at once, otherwise memory issues will occur.");
52 
53 public:
71  explicit SSLClient( const C& client,
72  const br_x509_trust_anchor *trust_anchors,
73  const size_t trust_anchors_num,
74  const int analog_pin,
75  const DebugLevel debug = SSL_WARN)
76  : SSLClientImpl(trust_anchors, trust_anchors_num, analog_pin, debug)
77  , m_client(client)
78  , m_sessions{}
79  {
80  // set the timeout to a reasonable number (it can always be changes later)
81  // SSL Connections take a really long time so we don't want to time out a legitimate thing
82  setTimeout(30 * 1000);
83  }
84 
89  explicit SSLClient( const C& client,
90  const br_x509_trust_anchor *trust_anchors,
91  const size_t trust_anchors_num,
92  const int analog_pin,
93  const DebugLevel debug,
94  const SSLClientParameters* mutual_auth_params)
95  : SSLClientImpl(trust_anchors, trust_anchors_num, analog_pin, debug, mutual_auth_params)
96  , m_client(client)
97  , m_sessions{}
98  {
99  // set the timeout to a reasonable number (it can always be changes later)
100  // SSL Connections take a really long time so we don't want to time out a legitimate thing
101  setTimeout(30 * 1000);
102  }
103 
104  //========================================
105  //= Functions implemented in SSLClientImpl
106  //========================================
107 
147  int connect(IPAddress ip, uint16_t port) override { return connect_impl(ip, port); }
148 
185  int connect(const char *host, uint16_t port) override { return connect_impl(host, port); }
186 
188  size_t write(uint8_t b) override { return write_impl(&b, 1); }
212  size_t write(const uint8_t *buf, size_t size) override { return write_impl(buf, size); }
213 
232  int available() override { return available_impl(); }
233 
238  int read() override { uint8_t read_val; return read(&read_val, 1) > 0 ? read_val : -1; };
260  int read(uint8_t *buf, size_t size) override { return read_impl(buf, size); }
261 
270  int peek() override { return peek_impl(); }
271 
279  void flush() override { return flush_impl(); }
280 
289  void stop() override { return stop_impl(); }
290 
304  uint8_t connected() override { return connected_impl(); }
305 
306  //========================================
307  //= Functions Not in the Client Interface
308  //========================================
309 
324  SSLSession& getSession(const char* host, const IPAddress& addr) { return get_session_impl(host, addr); }
325 
334  void removeSession(const char* host, const IPAddress& addr) { return remove_session_impl(host, addr); }
335 
341  size_t getSessionCount() const override { return SessionCache; }
342 
348  operator bool() { return connected() > 0; }
350  bool operator==(const bool value) { return bool() == value; }
352  bool operator!=(const bool value) { return bool() != value; }
354  bool operator==(const C& rhs) { return m_client == rhs; }
356  bool operator!=(const C& rhs) { return m_client != rhs; }
358  uint16_t localPort() override { return m_client.localPort(); }
360  IPAddress remoteIP() override { return m_client.remoteIP(); }
362  uint16_t remotePort() override { return m_client.remotePort(); }
363 
365  C& getClient() { return m_client; }
366 
367 protected:
369  Client& get_arduino_client() override { return m_client; }
370  const Client& get_arduino_client() const override { return m_client; }
372  SSLSession* get_session_array() override { return m_sessions; }
373  const SSLSession* get_session_array() const override { return m_sessions; }
374 
375 private:
376  // create a copy of the client
377  C m_client;
378  // also store an array of SSLSessions, so we can resume communication with multiple websites
379  SSLSession m_sessions[SessionCache];
380 };
381 
382 #endif
size_t write_impl(const uint8_t *buf, size_t size)
Definition: SSLClientImpl.cpp:147
const SSLSession * get_session_array() const override
Definition: SSLClient.h:373
IPAddress remoteIP() override
Returns the remote IP, if C::remoteIP exists.
Definition: SSLClient.h:360
size_t write(uint8_t b) override
Definition: SSLClient.h:188
Definition: SSLClientImpl.h:66
SSLSession & get_session_impl(const char *host, const IPAddress &addr)
Definition: SSLClientImpl.cpp:305
This class stores values which allow SSLClient to save and resume SSL sessions.
Definition: SSLSession.h:52
SSLClient(const C &client, 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)
Definition: SSLClient.h:89
bool operator!=(const C &rhs)
Returns whether or not two SSLClient objects do not have the same underlying client object.
Definition: SSLClient.h:356
int available() override
Returns the number of bytes available to read from the data that has been received and decrypted.
Definition: SSLClient.h:232
C & getClient()
Returns a reference to the client object stored in this class. Take care not to break it.
Definition: SSLClient.h:365
int peek_impl()
Definition: SSLClientImpl.cpp:226
This struct stores data required for SSLClient to use mutual authentication.
Definition: SSLClientParameters.h:52
void flush() override
Force writing the buffered bytes from SSLClient::write to the network.
Definition: SSLClient.h:279
The main SSLClient class. Check out README.md for more info.
Definition: SSLClient.h:35
bool operator!=(const bool value)
Definition: SSLClient.h:352
void stop() override
Close the connection.
Definition: SSLClient.h:289
size_t write(const uint8_t *buf, size_t size) override
Write some bytes to the SSL connection.
Definition: SSLClient.h:212
SSLClient(const C &client, const br_x509_trust_anchor *trust_anchors, const size_t trust_anchors_num, const int analog_pin, const DebugLevel debug=SSL_WARN)
Initialize SSLClient with all of the prerequisites needed.
Definition: SSLClient.h:71
int peek() override
View the first byte of the buffer, without removing it from the SSLClient Buffer.
Definition: SSLClient.h:270
int available_impl()
Definition: SSLClientImpl.cpp:190
bool operator==(const C &rhs)
Returns whether or not two SSLClient objects have the same underlying client object.
Definition: SSLClient.h:354
int read_impl(uint8_t *buf, size_t size)
Definition: SSLClientImpl.cpp:211
SSLSession * get_session_array() override
Returns an instance of the session array that is on the stack.
Definition: SSLClient.h:372
void remove_session_impl(const char *host, const IPAddress &addr)
Definition: SSLClientImpl.cpp:324
Client & get_arduino_client() override
Returns an instance of m_client that is polymorphic and can be used by SSLClientImpl.
Definition: SSLClient.h:369
uint16_t localPort() override
Returns the local port, if C::localPort exists.
Definition: SSLClient.h:358
int read() override
Read a single byte, or -1 if none is available.
Definition: SSLClient.h:238
uint8_t connected() override
Check if the device is connected.
Definition: SSLClient.h:304
const Client & get_arduino_client() const override
Definition: SSLClient.h:370
int connect(const char *host, uint16_t port) override
Connect over SSL to a host specified by a hostname.
Definition: SSLClient.h:185
bool operator==(const bool value)
Definition: SSLClient.h:350
uint16_t remotePort() override
Returns the remote port, if C::remotePort exists. Else return 0.
Definition: SSLClient.h:362
int connect_impl(IPAddress ip, uint16_t port)
Definition: SSLClientImpl.cpp:90
size_t getSessionCount() const override
Get the maximum number of SSL sessions that can be stored at once.
Definition: SSLClient.h:341
void stop_impl()
Definition: SSLClientImpl.cpp:246
void flush_impl()
Definition: SSLClientImpl.cpp:238
Implementation code to be inherited by SSLClient.
Definition: SSLClientImpl.h:72
void removeSession(const char *host, const IPAddress &addr)
Clear the session corresponding to a host and IP.
Definition: SSLClient.h:334
uint8_t connected_impl()
Definition: SSLClientImpl.cpp:274
SSLSession & getSession(const char *host, const IPAddress &addr)
Gets a session reference corresponding to a host and IP, or a reference to a empty session if none ex...
Definition: SSLClient.h:324
DebugLevel
Level of verbosity used in logging for SSLClient.
Definition: SSLClientImpl.h:60
int read(uint8_t *buf, size_t size) override
Read size bytes from the SSL client buffer, copying them into *buf, and return the number of bytes re...
Definition: SSLClient.h:260
int connect(IPAddress ip, uint16_t port) override
Connect over SSL to a host specified by an IP address.
Definition: SSLClient.h:147