forked from embeddedmz/socket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPSSLServer.cpp
More file actions
305 lines (259 loc) · 10.4 KB
/
TCPSSLServer.cpp
File metadata and controls
305 lines (259 loc) · 10.4 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* @file TCPSSLServer.cpp
* @brief implementation of the TCP SSL server class
* @author Mohamed Amine Mzoughi <[email protected]>
*/
#ifdef OPENSSL
#include "TCPSSLServer.h"
CTCPSSLServer::CTCPSSLServer(const LogFnCallback oLogger,
const std::string& strPort,
const OpenSSLProtocol eSSLVersion,
const SettingsFlag eSettings /*= ALL_FLAGS*/) /*throw (EResolveError)*/
: ASecureSocket(oLogger, eSSLVersion, eSettings)
, m_TCPServer(oLogger, strPort, eSettings)
{
}
CTCPSSLServer::~CTCPSSLServer()
{
//SocketClose(m_TCPServer.m_ListenSocket);
}
bool CTCPSSLServer::SetRcvTimeout(SSLSocket& ClientSocket, unsigned int msec_timeout)
{
return m_TCPServer.SetRcvTimeout(ClientSocket.m_SockFd, msec_timeout);
}
#ifndef _WIN32
bool CTCPSSLServer::SetRcvTimeout(SSLSocket& ClientSocket, struct timeval timeout)
{
return m_TCPServer.SetRcvTimeout(ClientSocket.m_SockFd, timeout);
}
#endif
bool CTCPSSLServer::SetSndTimeout(SSLSocket& ClientSocket, unsigned int msec_timeout)
{
return m_TCPServer.SetSndTimeout(ClientSocket.m_SockFd, msec_timeout);
}
#ifndef _WIN32
bool CTCPSSLServer::SetSndTimeout(SSLSocket& ClientSocket, struct timeval timeout)
{
return m_TCPServer.SetSndTimeout(ClientSocket.m_SockFd, timeout);
}
#endif
// returns the socket of the accepted client
int CTCPSSLServer::Listen(SSLSocket& ClientSocket, size_t msec /*= ACCEPT_WAIT_INF_DELAY*/)
{
int ret_val = m_TCPServer.Listen(ClientSocket.m_SockFd, msec);
if (ret_val < 0)
{
SocketLog("[ERROR]TCPSSLServer, m_TCPServer Listen failed[:Unable to accept an incoming TCP connection with a client.][:%s]", m_TCPServer.m_strPort.c_str());
return -1;
}
if (ret_val == 0)
{
return 0;
}
ret_val = 0;
do
{
if (!SetUpCtxServer(ClientSocket))
{
SocketLog("[ERROR]TCPSSLServer, SSL_CTX_new failed[:%s][%d]", m_TCPServer.m_strPort.c_str(), ClientSocket.m_SockFd);
//ERR_print_errors_fp(stdout);
break;
}
//SSL_CTX_set_options(ClientSocket.m_pCTXSSL, SSL_OP_SINGLE_DH_USE);
//SSL_CTX_set_cert_verify_callback(ClientSocket.m_pCTXSSL, AlwaysTrueCallback, nullptr);
/* Load server certificate into the SSL context. */
if (!m_strSSLCertFile.empty())
{
if (SSL_CTX_use_certificate_file(ClientSocket.m_pCTXSSL, m_strSSLCertFile.c_str(), SSL_FILETYPE_PEM) != 1)
{
SocketLog("[ERROR]TCPSSLServer, SSL_CTX_use_certificate_file failed[Loading cert file failed.][%lu:%s][%d][%s]", ERR_get_error(), ERR_error_string(ERR_get_error(), nullptr), ClientSocket.m_SockFd, m_strSSLCertFile.c_str());
//ERR_print_errors_fp(stdout);
break;
}
}
/* Load trusted CA file. */
if (!m_strCAFile.empty())
{
if (SSL_CTX_load_verify_locations(ClientSocket.m_pCTXSSL, m_strCAFile.c_str(), nullptr) != 1)
{
SocketLog("[ERROR]TCPSSLServer, SSL_CTX_load_verify_locations failed[Loading CA file failed.][%lu:%s][%d][%s]", ERR_get_error(), ERR_error_string(ERR_get_error(), nullptr), ClientSocket.m_SockFd, m_strCAFile.c_str());
break;
}
/* Set to require peer (client) certificate verification. */
//SSL_CTX_set_verify(m_SSLConnectSocket.m_pCTXSSL, SSL_VERIFY_PEER, VerifyCallback);
/* Set the verification depth to 1 */
SSL_CTX_set_verify_depth(ClientSocket.m_pCTXSSL, 1);
}
/* Load the server private-key into the SSL context. */
if (!m_strSSLKeyFile.empty())
{
if (SSL_CTX_use_PrivateKey_file(ClientSocket.m_pCTXSSL, m_strSSLKeyFile.c_str(), SSL_FILETYPE_PEM) != 1)
{
SocketLog("[ERROR]TCPSSLServer, SSL_CTX_use_PrivateKey_file failed[Loading key file failed.][%lu:%s][%d][%s]", ERR_get_error(), ERR_error_string(ERR_get_error(), nullptr), ClientSocket.m_SockFd, m_strSSLKeyFile.c_str());
//ERR_print_errors_fp(stdout);
break;
}
#if 0
// verify private key
if (SSL_CTX_check_private_key(ClientSocket.m_pCTXSSL) != 1)
{
SocketLog("[ERROR]TCPSSLServer, SSL_CTX_check_private_key failed[Private key does not match the public certificate.][%lu:%s][%d]", ERR_get_error(), ERR_error_string(ERR_get_error(), nullptr), ClientSocket.m_SockFd);
break;
}
#endif
}
ClientSocket.m_pSSL = SSL_new(ClientSocket.m_pCTXSSL);
if (ClientSocket.m_pSSL == nullptr)
{
SocketLog("[ERROR]TCPSSLServer, SSL_new failed[%lu:%s][%d]", ERR_get_error(), ERR_error_string(ERR_get_error(), nullptr), ClientSocket.m_SockFd);
break;
}
// set the socket directly into the SSL structure or we can use a BIO structure
if (SSL_set_fd(ClientSocket.m_pSSL, (int)ClientSocket.m_SockFd) != 1)
{
SocketLog("[ERROR]TCPSSLServer, SSL_set_fd failed[%lu:%s][%d]", ERR_get_error(), ERR_error_string(ERR_get_error(), nullptr), ClientSocket.m_SockFd);
break;
}
bool acceptOK = false;
do
{
/* wait for a TLS/SSL client to initiate a TLS/SSL handshake */
int iSSLErr = SSL_accept(ClientSocket.m_pSSL);
if (iSSLErr <= 0)
{
//Error occurred, log and close down ssl
int iErrCode = SSL_get_error(ClientSocket.m_pSSL, iSSLErr);
if (iErrCode == SSL_ERROR_WANT_ACCEPT || iErrCode == SSL_ERROR_WANT_READ || iErrCode == SSL_ERROR_WANT_WRITE)
{
continue;
}
SocketLog("[ERROR]TCPSSLServer, SSL_accept failed[%d:%s][%d]", iErrCode, GetSSLErrorString(iErrCode), ClientSocket.m_SockFd);
// under Windows it creates problems
#ifndef _WIN32
//ERR_print_errors_fp(stdout);
#endif
break;
}
acceptOK = true;
break;
} while (1);
if (!acceptOK)
{
break;
}
/* The TLS/SSL handshake is successfully completed and a TLS/SSL connection
* has been established. Now all reads and writes must use SSL. */
// peer_cert = SSL_get_peer_certificate(ClientSocket.m_pSSL);
SocketLog("[ERROR]TCPSSLServer, SSL_accept accepted[%d]", ClientSocket.m_SockFd);
return 1;
} while (0);
Disconnect(ClientSocket);
return ret_val;
}
/**
* When an SSL_read() operation has to be repeated because of SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE,
* it must be repeated with the same arguments.
*/
int CTCPSSLServer::Receive(const SSLSocket& ClientSocket, char* pData, size_t uSize, bool bReadFully /*= true*/) const
{
if (ClientSocket.m_SockFd == INVALID_SOCKET)
{
SocketLog("[ERROR]TCPSSLServer, SSL_read failed[not a connection to SSL server.]");
return -1;
}
if (pData == nullptr && uSize != 0)
{
SocketLog("[ERROR]TCPSSLServer, SSL_read failed[%d][%p:%zu]", ClientSocket.m_SockFd, pData, uSize);
return -2;
}
int total = 0;
do
{
int nRecvd = SSL_read(ClientSocket.m_pSSL, pData + total, (int)uSize - total);
if (nRecvd <= 0)
{
int iErrCode = SSL_get_error(ClientSocket.m_pSSL, nRecvd);
if (iErrCode == SSL_ERROR_WANT_READ || iErrCode == SSL_ERROR_WANT_WRITE)
{
continue;
}
if (total == 0)
{
//ERR_print_errors_fp(stdout);
if (iErrCode == SSL_ERROR_ZERO_RETURN)
{
SocketLog("[INFO ]TCPSSLServer, SSL_read failed(peer shut down)[%d:%s][%d]", iErrCode, GetSSLErrorString(iErrCode), ClientSocket.m_SockFd);
return 0;
}
SocketLog("[ERROR]TCPSSLServer, SSL_read failed[%d:%s][%d]", iErrCode, GetSSLErrorString(iErrCode), ClientSocket.m_SockFd);
return -1;
}
break;
}
total += nRecvd;
} while (bReadFully && (total < (int)uSize));
return total;
}
/* When an SSL_write() operation has to be repeated because of SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE,
* it must be repeated with the same arguments.
* When calling SSL_write() with uSize=0 bytes to be sent the behaviour is undefined. */
int CTCPSSLServer::Send(const SSLSocket& ClientSocket, const char* pData, size_t uSize) const
{
if (ClientSocket.m_pSSL == nullptr || ClientSocket.m_SockFd == INVALID_SOCKET)
{
SocketLog("[ERROR]TCPSSLServer, SSL_write failed[not a connection to SSL server.]");
return -1;
}
//OpenSSL 1.1.1
if (pData == nullptr && uSize != 0)
{
SocketLog("[ERROR]TCPSSLServer, SSL_write failed[%d][%p:%zu]", ClientSocket.m_SockFd, pData, uSize);
return -2;
}
int total = 0;
do
{
int nSent = SSL_write(ClientSocket.m_pSSL, pData + total, (int)uSize - total);
if (nSent <= 0)
{
int iErrCode = SSL_get_error(ClientSocket.m_pSSL, nSent);
if (iErrCode == SSL_ERROR_WANT_WRITE || iErrCode == SSL_ERROR_WANT_READ)
{
continue;
}
if (iErrCode == SSL_ERROR_ZERO_RETURN)
{
SocketLog("[WARN ]TCPSSLServer, SSL_write SSL_shutdowned[%d:%s][%d]", iErrCode, GetSSLErrorString(iErrCode), ClientSocket.m_SockFd);
break;
}
SocketLog("[ERROR]TCPSSLServer, SSL_write failed[%d:%s][%d]", iErrCode, GetSSLErrorString(iErrCode), ClientSocket.m_SockFd);
return -1;
}
total += nSent;
} while (total < (int)uSize);
return total;
}
int CTCPSSLServer::Send(const SSLSocket& ClientSocket, const std::string& strData) const
{
return Send(ClientSocket, strData.c_str(), strData.length());
}
int CTCPSSLServer::Send(const SSLSocket& ClientSocket, const std::vector<char>& Data) const
{
return Send(ClientSocket, Data.data(), Data.size());
}
bool CTCPSSLServer::HasPending(const SSLSocket& ClientSocket)
{
return ClientSocket.HasPending();
}
int CTCPSSLServer::PendingBytes(const SSLSocket& ClientSocket)
{
return ClientSocket.PendingBytes();
}
void CTCPSSLServer::Disconnect(SSLSocket& ClientSocket) const
{
// send close_notify message to notify peer of the SSL closure.
ClientSocket.Disconnect();
//ShutdownSSL(ClientSocket);
//m_TCPServer.Disconnect(ClientSocket.m_SockFd);
}
#endif