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 
18 class TcpConnection;
19 
20 namespace Ssl
21 {
34 enum class MaxBufferSize {
35  Default = 0,
36  B512,
37  K1,
38  K2,
39  K4,
40  K8,
41  K16,
42 };
43 
45 {
46  return (value == MaxBufferSize::Default) ? 0 : 256U << size_t(value);
47 }
48 
52 struct Options {
53  bool sessionResume : 1;
55  bool verifyLater : 1;
57 
58  Options() : sessionResume(false), clientAuthentication(false), verifyLater(false), freeKeyCertAfterHandshake(false)
59  {
60  }
61 
62  String toString() const;
63 };
64 
66 {
67  return options.toString();
68 }
69 
76 class Session
77 {
78 public:
80 
85 
90 
95 
100 
104  const CipherSuites::Array* cipherSuites = &CipherSuites::basic;
105 
113  int cacheSize = 10;
114 
119 
120 public:
122  {
123  close();
124  delete sessionId;
125  }
126 
131  const SessionId* getSessionId() const
132  {
133  return sessionId;
134  }
135 
142  bool onAccept(TcpConnection* client, tcp_pcb* tcp);
143 
148  void setConnection(Connection* connection)
149  {
150  assert(this->connection == nullptr);
151  this->connection = connection;
152  }
153 
159  {
160  return connection;
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  Context* context = nullptr;
227  Connection* connection = nullptr;
228  SessionId* sessionId = nullptr;
229  CpuFrequency curFreq = CpuFrequency(0);
230 };
231 
232 }; // namespace Ssl
Wraps a pbuf for reading in chunks.
Definition: InputBuffer.h:20
#define __forceinline
Definition: sming_attr.h:13
Definition: Alert.h:15
Performs certificate validation.
Definition: ValidatorList.h:32
Implemented by SSL adapter to handle a connection.
Definition: Connection.h:35
ValidatorList validators
List of certificate validators used by Client.
Definition: Session.h:118
CpuFrequency
CPU Frequency.
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:34
Definition: TcpConnection.h:39
bool freeKeyCertAfterHandshake
Definition: Session.h:56
The String class.
Definition: WString.h:136
String hostName
Used for SNI https://en.wikipedia.org/wiki/Server_Name_Indication.
Definition: Session.h:84
KeyCertPair keyCert
Required for server, optional for client.
Definition: Session.h:89
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:76
bool clientAuthentication
Definition: Session.h:54
Options()
Definition: Session.h:58
~Session()
Definition: Session.h:121
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.
bool sessionResume
Keep a note of session ID for later re-use.
Definition: Session.h:53
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:55
Implemented by SSL adapter to create and manage SSL connections.
Definition: Context.h:28
Manages buffer to store SSL Session ID.
Definition: SessionId.h:21
Let SSL implementation decide.
Configurable options.
Definition: Session.h:52
size_t maxBufferSizeToBytes(MaxBufferSize value)
Definition: Session.h:44
Options options
Various connection options.
Definition: Session.h:94