Session.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Session.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include "Context.h"
14 #include "KeyCertPair.h"
15 #include "ValidatorList.h"
16 #include <Platform/System.h>
17 #include <memory>
18 
19 class TcpConnection;
20 
21 namespace Ssl
22 {
35 enum class MaxBufferSize {
36  Default = 0,
37  B512,
38  K1,
39  K2,
40  K4,
41  K8,
42  K16,
43 };
44 
45 __forceinline size_t maxBufferSizeToBytes(MaxBufferSize value)
46 {
47  return (value == MaxBufferSize::Default) ? 0 : 256U << size_t(value);
48 }
49 
53 struct Options {
54  bool sessionResume : 1;
56  bool verifyLater : 1;
58 
60  {
61  }
62 
63  String toString() const;
64 };
65 
66 __forceinline String toString(const Options& options)
67 {
68  return options.toString();
69 }
70 
77 class Session
78 {
79 public:
80  using InitDelegate = Delegate<void(Session& session)>;
81 
86 
91 
96 
101 
105  const CipherSuites::Array* cipherSuites = &CipherSuites::basic;
106 
114  int cacheSize = 10;
115 
120 
121 public:
123  {
124  close();
125  }
126 
131  const SessionId* getSessionId() const
132  {
133  return sessionId.get();
134  }
135 
142  bool onAccept(TcpConnection* client, tcp_pcb* tcp);
143 
148  void setConnection(Connection* connection)
149  {
150  assert(!this->connection);
151  this->connection.reset(connection);
152  }
153 
159  {
160  return connection.get();
161  }
162 
168  bool onConnect(tcp_pcb* tcp);
169 
174  bool isConnected() const
175  {
176  return connection ? connection->isHandshakeDone() : false;
177  }
178 
184  void close();
185 
192  int read(InputBuffer& input, uint8_t*& output);
193 
200  int write(const uint8_t* data, size_t length);
201 
207  bool validateCertificate();
208 
214  void handshakeComplete(bool success);
215 
219  size_t printTo(Print& p) const;
220 
221 private:
222  void beginHandshake();
223  void endHandshake();
224 
225 private:
226  std::unique_ptr<Context> context;
227  std::unique_ptr<Connection> connection;
228  std::unique_ptr<SessionId> sessionId;
229  CpuFrequency curFreq = CpuFrequency(0);
230 };
231 
232 }; // namespace Ssl
size_t printTo(Print &p) const
For debugging.
const CipherSuites::Array * cipherSuites
Definition: Session.h:105
void close()
End the session.
size_t maxBufferSizeToBytes(MaxBufferSize value)
Definition: Session.h:45
ValidatorList validators
List of certificate validators used by Client.
Definition: Session.h:119
bool onAccept(TcpConnection *client, tcp_pcb *tcp)
Called when a client connection is made via server TCP socket.
String hostName
Used for SNI https://en.wikipedia.org/wiki/Server_Name_Indication.
Definition: Session.h:85
bool validateCertificate()
Called by SSL adapter when certificate validation is required.
The String class.
Definition: WString.h:136
String toString() const
MaxBufferSize
Indicate to SSL how much memory (approximately) to commit for buffers.
Definition: Session.h:35
MaxBufferSize maxBufferSize
Controls SSL RAM usage.
Definition: Session.h:100
bool sessionResume
Keep a note of session ID for later re-use.
Definition: Session.h:54
@ Default
Let SSL implementation decide.
bool isConnected() const
Determine if an SSL connection has been fully established.
Definition: Session.h:174
Implemented by SSL adapter to handle a connection.
Definition: Connection.h:46
Class to access an array of integral values stored in flash.
Definition: Array.hpp:113
const SessionId * getSessionId() const
If available, return the current SSL Session ID.
Definition: Session.h:131
String toString(Certificate::RDN rdn)
Obtain a string describing the given name component.
Class to manage an SSL key certificate with optional password.
Definition: KeyCertPair.h:28
Connection * getConnection()
Get the currently active SSL connection object.
Definition: Session.h:158
bool onConnect(tcp_pcb *tcp)
Handle connection event.
int read(InputBuffer &input, uint8_t *&output)
Read data from SSL connection.
Handles all SSL activity for a TCP connection.
Definition: Session.h:77
bool verifyLater
Allow handshake to complete before verifying certificate.
Definition: Session.h:56
Provides formatted output to stream.
Definition: Print.h:36
Performs certificate validation.
Definition: ValidatorList.h:42
int cacheSize
Set session caching.
Definition: Session.h:114
Definition: TcpConnection.h:39
Options()
Definition: Session.h:59
bool clientAuthentication
Definition: Session.h:55
CpuFrequency
Common CPU frequencies.
Definition: System.h:70
void setConnection(Connection *connection)
Called by TcpConnection to set the established SSL connection.
Definition: Session.h:148
bool freeKeyCertAfterHandshake
Definition: Session.h:57
~Session()
Definition: Session.h:122
KeyCertPair keyCert
Required for server, optional for client.
Definition: Session.h:90
void handshakeComplete(bool success)
Called by SSL adapter when handshake has been completed.
Manages buffer to store SSL Session ID.
Definition: SessionId.h:29
Options options
Various connection options.
Definition: Session.h:95
Wraps a pbuf for reading in chunks.
Definition: InputBuffer.h:30
int write(const uint8_t *data, size_t length)
Write data to SSL connection.
Definition: Alert.h:15
Configurable options.
Definition: Session.h:53