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 
59  Options() : sessionResume(false), clientAuthentication(false), verifyLater(false), freeKeyCertAfterHandshake(false)
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:
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
Wraps a pbuf for reading in chunks.
Definition: InputBuffer.h:22
Definition: Alert.h:15
Performs certificate validation.
Definition: ValidatorList.h:32
Implemented by SSL adapter to handle a connection.
Definition: Connection.h:36
ValidatorList validators
List of certificate validators used by Client.
Definition: Session.h:119
CpuFrequency
Common CPU frequencies.
Definition: System.h:70
const SessionId * getSessionId() const
If available, return the current SSL Session ID.
Definition: Session.h:131
MaxBufferSize
Indicate to SSL how much memory (approximately) to commit for buffers.
Definition: Session.h:35
Definition: TcpConnection.h:39
bool freeKeyCertAfterHandshake
Definition: Session.h:57
The String class.
Definition: WString.h:136
String hostName
Used for SNI https://en.wikipedia.org/wiki/Server_Name_Indication.
Definition: Session.h:85
KeyCertPair keyCert
Required for server, optional for client.
Definition: Session.h:90
void setConnection(Connection *connection)
Called by TcpConnection to set the established SSL connection.
Definition: Session.h:148
Provides formatted output to stream.
Definition: Print.h:36
Class to manage an SSL key certificate with optional password.
Definition: KeyCertPair.h:20
Handles all SSL activity for a TCP connection.
Definition: Session.h:77
bool clientAuthentication
Definition: Session.h:55
Options()
Definition: Session.h:59
~Session()
Definition: Session.h:122
String toString() const
bool isConnected() const
Determine if an SSL connection has been fully established.
Definition: Session.h:174
Connection * getConnection()
Get the currently active SSL connection object.
Definition: Session.h:158
String toString(Certificate::RDN rdn)
Obtain a string describing the given name component.
Timer2Clock::Ticks< uint32_t > read()
Get elapsed watchdog time since last reset.
bool sessionResume
Keep a note of session ID for later re-use.
Definition: Session.h:54
Class to access an array of integral values stored in flash.
Definition: Array.hpp:113
bool verifyLater
Allow handshake to complete before verifying certificate.
Definition: Session.h:56
Manages buffer to store SSL Session ID.
Definition: SessionId.h:21
Let SSL implementation decide.
Configurable options.
Definition: Session.h:53
size_t maxBufferSizeToBytes(MaxBufferSize value)
Definition: Session.h:45
Options options
Various connection options.
Definition: Session.h:95