/
ssl_common.cpp
295 lines (257 loc) · 7.76 KB
/
ssl_common.cpp
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
#include <ssl_common.h>
std::mutex ssl_common::is_init_ssl_lock;
bool ssl_common::is_init_ssl = false;
#ifdef __FILENAME__
const char *ssl_common::TAG = __FILENAME__;
#else
const char * ssl_common::TAG = "ssl_common";
#endif
ssl_common::ssl_common(const char *cert_file, const char *key_file) {
logger::instance()->d(TAG, __LINE__, "initialising SSL");
#ifdef ENABLE_OPENSSL
std::unique_lock<std::mutex> lock{is_init_ssl_lock};
if (!is_init_ssl) {
is_init_ssl = true;
/* SSL library initialisation */
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
}
lock.unlock();
/* create the SSL server context */
ctx = SSL_CTX_new(SSLv23_method());
if (!ctx) {
logger::instance()->e(TAG, __LINE__, "SSL_CTX_new()");
return;
}
/* Load certificate and private key files, and check consistency */
if (cert_file && key_file) {
if (SSL_CTX_use_certificate_file(ctx, cert_file, SSL_FILETYPE_PEM) != 1) {
logger::instance()->e(TAG, __LINE__, "SSL_CTX_use_certificate_file failed");
}
if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) != 1) {
logger::instance()->e(TAG, __LINE__, "SSL_CTX_use_PrivateKey_file failed");
}
/* Make sure the key and certificate file match. */
if (SSL_CTX_check_private_key(ctx) != 1) {
logger::instance()->e(TAG, __LINE__, "SSL_CTX_check_private_key failed");
} else
logger::instance()->d(TAG, __LINE__, "certificate and private key loaded and verified");
}
/* Recommended to avoid SSLv2 & SSLv3 */
SSL_CTX_set_options(ctx, SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
#else
logger::instance()->e(TAG, __LINE__, "OpenSSL is disable!!!");
#endif
}
bool ssl_common::isReady() {
#ifdef ENABLE_OPENSSL
return ctx != nullptr;
#else
return false;
#endif
}
ssl_common::~ssl_common() {
#ifdef ENABLE_OPENSSL
if (ctx) {
SSL_CTX_free(ctx);
ctx = nullptr;
}
#endif
}
bool ssl_common::init_sub(ssl_common::ssl_common_sub *&sub) {
std::unique_lock<std::mutex> lock(ssl_lock);
if (sub != nullptr) {
delete sub;
sub = nullptr;
}
sub = new ssl_common_sub(this);
return sub != nullptr;
}
void ssl_common::free_sub(ssl_common::ssl_common_sub *&sub) {
std::unique_lock<std::mutex> lock(ssl_lock);
if (sub != nullptr) {
delete sub;
sub = nullptr;
}
}
ssl_common::ssl_common_sub::~ssl_common_sub() {
#ifdef ENABLE_OPENSSL
if (ssl != nullptr) {
SSL_shutdown(ssl);
// SSL_free(ssl);
ssl = nullptr;
}
if (rBIO != nullptr) {
BIO_free(rBIO);
rBIO = nullptr;
} /* SSL reads from, we write to. */
if (wBIO != nullptr) {
BIO_free(wBIO);
wBIO = nullptr;
} /* SSL writes to, we read from. */
#endif
}
ssl_common::ssl_common_sub::ssl_common_sub(ssl_common *parent) {
this->parent = parent;
#ifdef ENABLE_OPENSSL
rBIO = BIO_new(BIO_s_mem());
wBIO = BIO_new(BIO_s_mem());
ssl = SSL_new(parent->ctx);
if (parent->ssl_mode == SSL_MODE_SERVER)
SSL_set_accept_state(ssl); /* ssl server mode */
else if (parent->ssl_mode == SSL_MODE_CLIENT)
SSL_set_connect_state(ssl); /* ssl client mode */
SSL_set_bio(ssl, rBIO, wBIO);
#endif
}
ssl_common::SSL_status ssl_common::ssl_common_sub::get_ssl_status(int n) {
#ifdef ENABLE_OPENSSL
switch (SSL_get_error(ssl, n)) {
case SSL_ERROR_NONE:
return SSL_STATUS_OK;
case SSL_ERROR_WANT_WRITE:
case SSL_ERROR_WANT_READ:
return SSL_STATUS_WANT_IO;
case SSL_ERROR_ZERO_RETURN:
case SSL_ERROR_SYSCALL:
default:
return SSL_STATUS_FAIL;
}
#else
return SSL_STATUS_FAIL;
#endif
}
void ssl_common::ssl_common_sub::send_unencrypted_bytes(unsigned char *buf, size_t len) {
encrypt_buf.insert(encrypt_buf.end(), &buf[0], &buf[len]);
}
void ssl_common::ssl_common_sub::queue_encrypted_bytes(unsigned char *buf, size_t len) {
write_buf.insert(write_buf.end(), &buf[0], &buf[len]);
}
ssl_common::SSL_status ssl_common::ssl_common_sub::do_ssl_handshake() {
#ifdef ENABLE_OPENSSL
enum SSL_status status = SSL_STATUS_OK;
int n = SSL_do_handshake(ssl);
if (n >= 0)return status;
unsigned char* buf = new unsigned char[parent->DEFAULT_BUF_SIZE];
status = get_ssl_status(n);
/* Did SSL request to write bytes? */
if (status == SSL_STATUS_WANT_IO) {
do {
n = BIO_read(wBIO, buf, parent->DEFAULT_BUF_SIZE);
if (n > 0)
queue_encrypted_bytes(buf, n);
else if (!BIO_should_retry(wBIO)) {
delete[] buf;
return SSL_STATUS_FAIL;
}
} while (n > 0);
}
delete[] buf;
return status;
#else
return SSL_status::SSL_STATUS_FAIL;
#endif
}
int ssl_common::ssl_common_sub::on_read_cb(std::vector<unsigned char> &data, unsigned char *src, size_t len) {
write_buf.clear();
enum SSL_status status = SSL_status::SSL_STATUS_OK;
#ifdef ENABLE_OPENSSL
unsigned char* buf = new unsigned char[parent->DEFAULT_BUF_SIZE];
int n = 0;
while (len > 0) {
n = BIO_write(rBIO, src, len);
if (n <= 0) {
delete[] buf;
return -1; /* assume bio write failure is unrecoverable */
}
src += n;
len -= n;
if (!SSL_is_init_finished(ssl)) {
if (do_ssl_handshake() == SSL_STATUS_FAIL) {
delete[] buf;
return -1;
}
if (!SSL_is_init_finished(ssl)) {
delete[] buf;
return 0;
}
}
do {
n = SSL_read(ssl, buf, parent->DEFAULT_BUF_SIZE);
if (n > 0) {
// TODO
// client.io_on_read(buf, (size_t) n);
}
} while (n > 0);
status = get_ssl_status(n);
/* Did SSL request to write bytes? This can happen if peer has requested SSL
* renegotiation. */
if (status == SSL_STATUS_WANT_IO)
do {
n = BIO_read(wBIO, buf, parent->DEFAULT_BUF_SIZE);
if (n > 0)
queue_encrypted_bytes(buf, n);
else if (!BIO_should_retry(wBIO)) {
delete[] buf;
return -1;
}
} while (n > 0);
if (status == SSL_STATUS_FAIL) {
delete[] buf;
return -1;
}
}
data.insert(data.end(), write_buf.begin(), write_buf.end());
write_buf.clear();
delete[] buf;
#else
return -1;
#endif
return 0;
}
int ssl_common::ssl_common_sub::do_encrypt(std::vector<unsigned char> &encrypt_data, std::vector<unsigned char> data) {
enum SSL_status status = SSL_status::SSL_STATUS_OK;
#ifdef ENABLE_OPENSSL
if (!SSL_is_init_finished(ssl))
return 0;
unsigned char* buf = new unsigned char[parent->DEFAULT_BUF_SIZE];
while (!data.empty()) {
int n = SSL_write(ssl, data.data(), data.size());
status = get_ssl_status(n);
if (n > 0) {
/* consume the waiting bytes that have been used by SSL */
data.erase(data.begin(), data.begin() + n);
/* take the output of the SSL object and queue it for socket write */
do {
n = BIO_read(wBIO, buf, parent->DEFAULT_BUF_SIZE);
if (n > 0)
encrypt_data.insert(encrypt_data.end(), &buf[0], &buf[n]);
else if (!BIO_should_retry(wBIO)) {
delete[]buf;
return -1;
}
} while (n > 0);
}
if (status == SSL_STATUS_FAIL) {
delete[] buf;
return -1;
}
if (n == 0)
break;
}
delete[] buf;
#else
return -1;
#endif
return 0;
}
bool ssl_common::ssl_common_sub::SSL_init_finished() {
#ifdef ENABLE_OPENSSL
return SSL_is_init_finished(ssl);
#else
return false;
#endif
}