-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssl_util.cpp
More file actions
291 lines (265 loc) · 5.42 KB
/
ssl_util.cpp
File metadata and controls
291 lines (265 loc) · 5.42 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
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "ssl_util.h"
static bool ssl_lib_initialed = false;
inline int ssl_err_message_cb(const char *str, size_t len, void *u)
{
std::string* msg = (std::string*)u;
msg->append(str,len);
return len;
}
const int ssl_client_flag = 1;
const int ssl_server_flag = 1 << 2;
SSLFactory::SSLFactory() : ctx_(NULL), flag_(0)
{
if (!ssl_lib_initialed)
{
SSL_library_init();
SSL_load_error_strings();
}
}
SSLFactory::~SSLFactory()
{
if (ctx_)
{
SSL_CTX_free(ctx_);
ctx_ = NULL;
}
}
bool SSLFactory::load()
{
ctx_ = SSL_CTX_new(SSLv23_client_method());
if (!ctx_)
{
ERR_print_errors_cb(ssl_err_message_cb,&err_message_);
return false;
}
flag_ = ssl_client_flag;
return true;
}
bool SSLFactory::load(const std::string& cert,const std::string& priv)
{
ctx_ = SSL_CTX_new(SSLv23_method());
if (!ctx_)
{
ERR_print_errors_cb(ssl_err_message_cb,&err_message_);
ctx_ = NULL;
return false;
}
if (SSL_CTX_use_certificate_file(ctx_, cert.c_str(), SSL_FILETYPE_PEM) != 1)
{
ERR_print_errors_cb(ssl_err_message_cb,&err_message_);
SSL_CTX_free(ctx_);
ctx_ = NULL;
return false;
}
if (SSL_CTX_use_PrivateKey_file(ctx_, priv.c_str(), SSL_FILETYPE_PEM) != 1)
{
ERR_print_errors_cb(ssl_err_message_cb,&err_message_);
SSL_CTX_free(ctx_);
ctx_ = NULL;
return false;
}
if (SSL_CTX_check_private_key(ctx_) != 1)
{
err_message_ = "cert is not match private key";
SSL_CTX_free(ctx_);
ctx_ = NULL;
return false;
}
SSL_CTX_set_options(ctx_, SSL_OP_ALL|SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3);
flag_ = ssl_client_flag | ssl_server_flag;
return true;
}
SSLBase* SSLFactory::create_client()
{
if (!(flag_ & ssl_client_flag))
return NULL;
SSLBase* base = new SSLClient();
if (!base->init(ctx_))
{
err_message_ = base->get_error();
delete base;
base = NULL;
}
return base;
}
SSLBase* SSLFactory::create_server()
{
if (!(flag_ & ssl_server_flag))
return NULL;
SSLBase* base = new SSLServer();
if (!base->init(ctx_))
{
err_message_ = base->get_error();
delete base;
base = NULL;
}
return base;
}
SSLBase::~SSLBase()
{
if (ssl_)
{
SSL_shutdown(ssl_);
SSL_free(ssl_);
}
}
bool SSLBase::is_handshake_ok()
{
return SSL_is_init_finished(ssl_) && is_handshake_;
}
int SSLBase::handshake(unsigned char* buf,int len,const std::function<void (const unsigned char*,int)>& send)
{
int ret;
if (!is_client_ && (buf == NULL || len <=0 ))
return SSL_WRITE_HANDSHAKE_WANTIO;
if (buf != NULL && len > 0)
BIO_write(rbio_,buf,len);
if (is_client_)
ret = SSL_do_handshake(ssl_);
else
ret = SSL_accept(ssl_);
if (ret == 1)
{
is_handshake_ = true;
}
else
{
switch (SSL_get_error(ssl_, ret))
{
case SSL_ERROR_NONE:
is_handshake_ = true;
break;
case SSL_ERROR_WANT_WRITE:
case SSL_ERROR_WANT_READ:
len = SSL_WRITE_HANDSHAKE_WANTIO;
break;
case SSL_ERROR_ZERO_RETURN:
case SSL_ERROR_SYSCALL:
default:
ERR_print_errors_cb(ssl_err_message_cb,&err_message_);
return SSL_WRITE_HANDSHAKE_FAIL;
}
}
char buffer[4096];
int rlen;
do {
rlen = BIO_read(wbio_,buffer,sizeof(buffer));
if (rlen > 0 && send != nullptr)
{
send((unsigned char*)buffer,rlen);
}
} while(rlen > 0);
return len;
}
int SSLBase::write(const unsigned char* buf,int len,const std::function<void (const unsigned char*,int)>& send)
{
int sw_len;
int orign_len = len;
int encode_len;
char buffer[4096];
do {
sw_len = SSL_write(ssl_,buf,len);
if (sw_len < 0)
{
ERR_print_errors_cb(ssl_err_message_cb,&err_message_);
return sw_len;
}
buf += sw_len;
len -= sw_len;
do {
encode_len = BIO_read(wbio_,buffer,sizeof(buffer));
if (encode_len > 0 && send != nullptr)
{
send((unsigned char*)buffer,encode_len);
}
} while(encode_len > 0);
} while(len > 0);
return orign_len;
}
int SSLBase::read(const unsigned char* buf,int len,const std::function<void (const unsigned char*,int)>& rb)
{
int sw_len;
int orign_len = len;
int decode_len;
char buffer[4096];
do {
sw_len = BIO_write(rbio_,buf,len);
if (sw_len < 0)
{
ERR_print_errors_cb(ssl_err_message_cb,&err_message_);
return sw_len;
}
buf += sw_len;
len -= sw_len;
do {
decode_len = SSL_read(ssl_,buffer,sizeof(buffer));
if (decode_len > 0 && rb != nullptr)
{
rb((unsigned char*)buffer,decode_len);
}
} while(decode_len > 0);
} while(len > 0);
return orign_len;
}
const std::string& SSLBase::get_error() const
{
return err_message_;
}
const std::string SSLBase::get_peer_cert()
{
std::string ret;
if (ssl_)
{
X509* cert = SSL_get_peer_certificate(ssl_);
if (cert)
{
char* buffer;
long len;
BIO* pem = BIO_new(BIO_s_mem());
PEM_write_bio_X509(pem, cert);
len = BIO_get_mem_data(pem,&buffer);
ret.append(buffer,len);
BIO_free(pem);
X509_free(cert);
}
}
return std::move(ret);
}
bool SSLClient::init(SSL_CTX* ctx)
{
if ( !ctx )
{
return false;
}
ssl_ = SSL_new(ctx);
if (!ssl_)
{
ERR_print_errors_cb(ssl_err_message_cb,&err_message_);
return false;
}
rbio_ = BIO_new(BIO_s_mem());
wbio_ = BIO_new(BIO_s_mem());
SSL_set_bio(ssl_,rbio_,wbio_);
SSL_set_connect_state(ssl_);
return true;
}
bool SSLServer::init(SSL_CTX* ctx)
{
if ( !ctx )
{
return false;
}
ssl_ = SSL_new(ctx);
if (!ssl_)
{
ERR_print_errors_cb(ssl_err_message_cb,&err_message_);
return false;
}
rbio_ = BIO_new(BIO_s_mem());
wbio_ = BIO_new(BIO_s_mem());
SSL_set_bio(ssl_,rbio_,wbio_);
SSL_set_accept_state(ssl_);
return true;
}