-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSSLSocket.h
183 lines (125 loc) · 4.42 KB
/
SSLSocket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* SSLSocket 16/03/2015 - Secure Socket Layer Socket
$$$$$$$$$$$$$$$$$$$
$ SSLSocket.h $
$$$$$$$$$$$$$$$$$$$
Copyright (C) 2015 W.B. Yates
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/
History:
Very simple secure socket class
We use the OpenSSL interface, see https://wiki.openssl.org/index.php/Main_Page
and the TLS protocol, see https://tools.ietf.org/html/rfc5246
We support session resumption on server and client sides
*** Updated 24/10/23 ***
The implementation of OpenSSL used here is OpenSSL 3.1.3 19 Sep 2023
We recommend LibreSSL see https://www.libressl.org
See also:
https://en.wikipedia.org/wiki/X.509
https://en.wikipedia.org/wiki/Transport_Layer_Security
Compile with the library flags: -lssl -lcrypto
*/
#ifndef __SSLSOCKET_H__
#define __SSLSOCKET_H__
#ifndef __SOCKET_H__
#include "Socket.h"
#endif
#ifndef __SSLCONTEXT_H__
#include "SSLContext.h"
#endif
#include <map>
extern "C"
{
#ifndef HEADER_SSL_H
#include <openssl/ssl.h>
#endif
}
class SSLSocket : public Socket
{
public:
SSLSocket( void ): Socket(), m_sslSocket(0), m_sslContext(0), m_sslSession(0), m_check(1) {}
SSLSocket( const std::string& context );
virtual ~SSLSocket( void );
virtual void
close( void );
// you must set a context before calls to accept or calls to connect
// usually (though not always) set once for the sockets lifetime
void
setContext( const std::string& context );
virtual bool
accept( SSLSocket& client ) const;
virtual bool
connect( const std::string& IP, const int port );
// timeout after seconds, microseconds - 1000000 microseconds equals 1 second
virtual bool
connect( const std::string& IP, const int port, const int seconds, const int microseconds = 0 );
// after close() reconnect to same server; resume session if possible
virtual bool
reconnect( void );
virtual bool
reconnect( const int seconds, const int microseconds = 0 );
// data transimission
// text and persistent data defined in Socket.h
using Socket::send;
using Socket::receive;
// binary data
virtual bool
send( const std::vector<char>& msg ) const;
virtual bool
receive( std::vector<char>& msg ) const;
// end of data transmission
//
// static stuff
//
static bool
initialised( void ) { return m_initialised; }
// register context(s) before creating any sockets
// will overwite any existing context if contexts have same name
static bool
registerContext( SSLContext *context );
static void
clearRegister( void );
protected:
// super class accept disabled
virtual bool
accept( Socket& client ) const { return true; }
// copy/assignment disabled
SSLSocket( const SSLSocket& )=delete;
SSLSocket&
operator=( const SSLSocket& )=delete;
//
bool
certificateCheck( const std::string& host ) const;
SSL *m_sslSocket;
SSL_CTX *m_sslContext;
SSL_SESSION *m_sslSession;
int m_check;
//
// statics
//
static void
print_cn_name(const char* label, X509_NAME* const name);
static void
print_san_name(const char* label, X509* const cert);
static int
verify_callback(int preverify, X509_STORE_CTX* x509_ctx);
static int
pem_password_cb(char *buf, int num, int rwflag, void *userdata);
static SSL_CTX*
sslContext( SSLContext *info );
static DH*
setDHParams( const std::string& dhFile );
static int
sslInitialiseLibrary( void );
static int m_initialised;
static std::map<std::string, SSL_CTX*> m_contextFactory;
};
#endif
//