-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdtls_server.cpp
613 lines (471 loc) · 14.4 KB
/
dtls_server.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <assert.h>
#include <signal.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <unistd.h>
#include <openssl/ssl.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include "my_cbio.h"
#define COOKIE_SECRET_LENGTH 16
#define EPOLL_TIMEOUT 1000 // miliseconds
#define SOCKET_IDLE_TIMEOUT 600 // seconds
#define SHUTDOWN_TIMEOUT 5 // seconds
#define MAX(x, y) ((x) >= (y) ? (x) : (y))
#define MIN(x, y) ((x) <= (y) ? (x) : (y))
struct UdpConnectInfo;
typedef int (*EventHandler)(UdpConnectInfo* pstConnInfo);
#if (__cplusplus >= 201103L) // c++11
#include <unordered_map>
typedef std::unordered_map<CustomBuffer*, UdpConnectInfo*, HashCustomBuffer, CompareCustomBuffer> ConnectMap;
typedef std::unordered_map<CustomBuffer*, UdpConnectInfo*, HashCustomBuffer, CompareCustomBuffer>::iterator ConnectMapIterator;
#else // c++98
#include <tr1/unordered_map>
typedef std::tr1::unordered_map<CustomBuffer*, UdpConnectInfo*, HashCustomBuffer, CompareCustomBuffer> ConnectMap;
typedef std::tr1::unordered_map<CustomBuffer*, UdpConnectInfo*, HashCustomBuffer, CompareCustomBuffer>::iterator ConnectMapIterator;
#endif
typedef enum{
DTLS_STATUS_INIT=0, // wait connect
DTLS_STATUS_CONNECTED=1, // connected
DTLS_STATUS_SHUTDOWN=2, // shutdown & wait close
}ConnStatusType;
struct UdpConnectInfo{
CustomBioData m_stBioData;
SSL* m_pstSSL;
int m_iStatus;
time_t m_tLastAccess;
ConnectMap* m_pstConnMap;
EventHandler m_fEvHandle;
UdpConnectInfo()
{
m_pstSSL = NULL;
m_iStatus = DTLS_STATUS_INIT;
m_tLastAccess = 0;
m_pstConnMap = NULL;
m_fEvHandle = NULL;
}
~UdpConnectInfo()
{
if(m_pstConnMap != NULL)
m_pstConnMap->erase(&(m_stBioData.m_stAddrBuf));
if(m_pstSSL != NULL)
SSL_free(m_pstSSL);
m_pstSSL = NULL;
m_iStatus = DTLS_STATUS_INIT;
m_tLastAccess = 0;
m_pstConnMap = NULL;
m_fEvHandle = NULL;
}
};
unsigned char cookie_secret[COOKIE_SECRET_LENGTH];
int listen_fd;
SSL_CTX *ctx;
ConnectMap stConnMap;
UdpConnectInfo* pstListenConnInfo = NULL;
UdpConnectInfo* new_udpconnect_info();
void signal_handler(int sig)
{
if (sig==SIGINT)
fprintf(stderr, "signal SIGINT\n");
else
fprintf(stderr, "get signal[%d]\n", sig);
}
int init_cookie_secret()
{
if(!RAND_bytes(cookie_secret, COOKIE_SECRET_LENGTH)){
printf("error setting random cookie secret\n");
return -1;
}
return 0;
}
int generate_cookie(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len)
{
unsigned char *buffer, result[EVP_MAX_MD_SIZE];
unsigned int length = 0, resultlength;
BIO_ADDR* pstPeerAddr = BIO_ADDR_new();
struct sockaddr_storage stAddr;
struct sockaddr_in* ipv4_addr = (struct sockaddr_in*)&stAddr;
struct sockaddr_in6* ipv6_addr = (struct sockaddr_in6*)&stAddr;
/* Read peer information */
(void) BIO_dgram_get_peer(SSL_get_rbio(ssl), pstPeerAddr);
stAddr.ss_family = BIO_ADDR_family(pstPeerAddr);
/* Create buffer with peer's address and port */
length = 0;
switch (stAddr.ss_family) {
case AF_INET:
length += sizeof(struct in_addr);
ipv4_addr->sin_port = BIO_ADDR_rawport(pstPeerAddr);
BIO_ADDR_rawaddress(pstPeerAddr, &ipv4_addr->sin_addr, NULL);
break;
case AF_INET6:
length += sizeof(struct in6_addr);
ipv6_addr->sin6_port = BIO_ADDR_rawport(pstPeerAddr);
BIO_ADDR_rawaddress(pstPeerAddr, &ipv6_addr->sin6_addr, NULL);
break;
default:
printf("unknow family:%d. AF_UNSPEC=%d\n", BIO_ADDR_family(pstPeerAddr), AF_UNSPEC);
BIO_ADDR_free(pstPeerAddr);
OPENSSL_assert(0);
break;
}
length += sizeof(in_port_t);
BIO_ADDR_free(pstPeerAddr);
buffer = (unsigned char*) OPENSSL_malloc(length);
if(buffer == NULL){
printf("out of memory\n");
return 0;
}
switch (stAddr.ss_family) {
case AF_INET:
memcpy(buffer, &ipv4_addr->sin_port, sizeof(in_port_t));
memcpy(buffer + sizeof(ipv4_addr->sin_port), &ipv4_addr->sin_addr, sizeof(struct in_addr));
break;
case AF_INET6:
memcpy(buffer, &ipv6_addr->sin6_port, sizeof(in_port_t));
memcpy(buffer + sizeof(in_port_t), &ipv6_addr->sin6_addr, sizeof(struct in6_addr));
break;
default:
OPENSSL_assert(0);
break;
}
/* Calculate HMAC of buffer using the secret */
HMAC(EVP_sha1(), (const void*) cookie_secret, COOKIE_SECRET_LENGTH, (const unsigned char*) buffer, length, result, &resultlength);
OPENSSL_free(buffer);
memcpy(cookie, result, resultlength);
*cookie_len = resultlength;
return 1;
}
int verify_cookie(SSL *ssl, const unsigned char *cookie, unsigned int cookie_len)
{
int ret;
unsigned char result[EVP_MAX_MD_SIZE];
unsigned int result_len;
ret = generate_cookie(ssl, result, &result_len);
if(ret != 1)
return ret;
if(cookie_len == result_len && memcmp(result, cookie, result_len) == 0)
return 1;
return 0;
}
int dtls_verify_callback (int ok, X509_STORE_CTX *ctx)
{
/* This function should ask the user
* if he trusts the received certificate.
* Here we always trust.
*/
return 1;
}
int on_message(UdpConnectInfo* pstInfo)
{
int len;
char buf[200];
printf("socket[%d] %s ...\n", pstInfo->m_stBioData.m_iFd, __FUNCTION__);
len = SSL_read(pstInfo->m_pstSSL, buf, sizeof(buf) - 1);
if(len == 0){
SSL_shutdown(pstInfo->m_pstSSL);
dump_addr((struct sockaddr *)&pstInfo->m_stBioData.m_stClientAddr, "client close: ");
return(1);
}
else if(len <= 0){
switch (SSL_get_error(pstInfo->m_pstSSL, len)) {
case SSL_ERROR_NONE:
case SSL_ERROR_WANT_READ:
case SSL_ERROR_ZERO_RETURN:
return(0);
case SSL_ERROR_SYSCALL:
printf("ssl error syscall\n");
return(-1);
case SSL_ERROR_SSL:
printf("ssl error ssl\n");
return(-1);
default:
printf("unknow error when ssl accept\n");
return(-1);
}
}
buf[len] = 0;
printf("recv: %d bytes:%s\n", len, buf);
len = SSL_write(pstInfo->m_pstSSL, buf, len);
if(len <= 0){
switch (SSL_get_error(pstInfo->m_pstSSL, len)) {
case SSL_ERROR_NONE:
case SSL_ERROR_WANT_WRITE:
case SSL_ERROR_ZERO_RETURN:
return(0);
case SSL_ERROR_SYSCALL:
printf("ssl error syscall\n");
return(-1);
case SSL_ERROR_SSL:
printf("ssl error ssl\n");
return(-1);
default:
printf("unknow error when ssl accept\n");
return(-1);
}
}
printf("send %d bytes\n", len);
return(0);
}
int on_connect(UdpConnectInfo* pstInfo)
{
int iRet;
int tmp;
iRet = SSL_accept(pstInfo->m_pstSSL);
printf("SSL_accept():%d\n", iRet);
if(iRet == 1){
dump_addr((struct sockaddr *)&pstInfo->m_stBioData.m_stClientAddr, "new connection: ");
pstInfo->m_fEvHandle = on_message;
pstInfo->m_iStatus = DTLS_STATUS_CONNECTED;
}
else{
tmp = SSL_get_error(pstInfo->m_pstSSL, iRet);
switch (tmp) {
case SSL_ERROR_NONE:
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
printf("SSL_accept delay\n");
return(0);
case SSL_ERROR_SYSCALL:
printf("SSL_accept error syscall\n");
return(0);
case SSL_ERROR_SSL:
printf("SSL_accept error ssl\n");
return(-1);
default:
printf("unknow error when ssl accept:%d\n", tmp);
return(-1);
}
}
return(0);
}
int do_new_connection(UdpConnectInfo*& pstInfo)
{
int iRet;
BIO_ADDR* pstPeerAddr = BIO_ADDR_new();
iRet = DTLSv1_listen(pstInfo->m_pstSSL, pstPeerAddr);
printf("DTLSv1_listen():%d\n", iRet);
BIO_ADDR_free(pstPeerAddr);
if(iRet != 1)
return(0);
(*pstInfo->m_pstConnMap)[&pstInfo->m_stBioData.m_stAddrBuf] = pstInfo;
pstInfo->m_fEvHandle(pstInfo);
pstInfo = new_udpconnect_info();
return(0);
}
UdpConnectInfo* new_udpconnect_info()
{
UdpConnectInfo* pstInfo;
pstInfo = new UdpConnectInfo;
if(pstInfo == NULL){
printf("new UdpConnectInfo fail: %m\n");
return(NULL);
}
pstInfo->m_pstSSL = SSL_new(ctx);
if(pstInfo->m_pstSSL == NULL){
printf("SSL_new fail:%m\n");
delete pstInfo;
return(NULL);
}
pstInfo->m_stBioData.m_stAddrBuf.m_iCap = sizeof(struct sockaddr_storage);
pstInfo->m_stBioData.m_stAddrBuf.m_iLen = sizeof(struct sockaddr_storage);
memset(&pstInfo->m_stBioData.m_stClientAddr, 0, sizeof(struct sockaddr_storage));
pstInfo->m_stBioData.m_iPeekMode = 0;
pstInfo->m_pstConnMap = &stConnMap; // connect map
pstInfo->m_fEvHandle = on_connect;
BIO *bio = BIO_new(BIO_s_custom());
BIO_set_data(bio, (void *)&pstInfo->m_stBioData);
BIO_set_init(bio, 1);
SSL_set_bio(pstInfo->m_pstSSL, bio, bio);
return pstInfo;
}
void check_idle_socket(time_t time_now)
{
for(ConnectMapIterator it=stConnMap.begin(); it != stConnMap.end(); ){
UdpConnectInfo* pstInfo = it->second;
if(time_now - pstInfo->m_tLastAccess < SOCKET_IDLE_TIMEOUT){
++it;
continue;
}
if(pstInfo->m_iStatus == DTLS_STATUS_SHUTDOWN){
if(time_now - pstInfo->m_tLastAccess < SOCKET_IDLE_TIMEOUT + SHUTDOWN_TIMEOUT){
++it;
continue;
}
printf("client socket[%d] shutdown timeout, release it now\n", pstInfo->m_stBioData.m_iFd);
stConnMap.erase(it++);
delete pstInfo;
}
else{
printf("client socket[%d] idle timeout, shutdown it now\n", pstInfo->m_stBioData.m_iFd);
SSL_shutdown(pstInfo->m_pstSSL);
++it;
}
}
return;
}
void show_usage(const char* name)
{
printf("usage: %s ip:port\n", name);
}
int main(int argc, char* argv[])
{
int ret;
char* p;
int port;
int listen_fd;
sockaddr_in listen_addr;
unsigned int listen_addr_len = sizeof(struct sockaddr_in);
int on = 1, off = 0;
int epfd;
if(argc < 2){
show_usage(argv[0]);
exit(1);
}
p = strchr(argv[1], ':');
if(p == NULL){
show_usage(argv[0]);
exit(1);
}
port = atoi(p+1);
if(port <= 0 || port > 65535){
printf("invalid port:%d\n", port);
exit(1);
}
char ip[32];
memset(ip, 0, sizeof(ip));
strncpy(ip, argv[1], MIN(sizeof(ip) - 1, p - argv[1]));
memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
listen_addr.sin_port = htons(port);
ret = inet_pton(AF_INET, ip, &listen_addr.sin_addr);
if(ret == 0){
printf("listen address invalid:%s\n", ip);
exit(1);
}
init_cookie_secret();
// ssl init
SSL_load_error_strings();
SSL_library_init();
const SSL_METHOD *mtd = DTLS_server_method();
ctx = SSL_CTX_new(mtd);
SSL_CTX_set_min_proto_version(ctx, DTLS1_2_VERSION);
if(SSL_CTX_use_certificate_chain_file(ctx, "server-cert.pem") != 1){
printf("load cert chain file fail\n");
exit(1);
}
if(SSL_CTX_use_PrivateKey_file(ctx, "server-key.pem", SSL_FILETYPE_PEM) != 1){
printf("load private key file fail\n");
exit(1);
}
if(SSL_CTX_check_private_key (ctx) != 1){
printf("invalid private key!\n");
exit(1);
}
/* Client has to authenticate */
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, dtls_verify_callback);
ret = SSL_CTX_load_verify_locations(ctx, "root-ca.pem", NULL);
printf("SSL_CTX_load_verify_location(): %d\n", ret);
ret = SSL_CTX_set_default_verify_file(ctx);
printf("SSL_CTX_set_default_verify_file(): %d\n", ret);
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
SSL_CTX_set_read_ahead(ctx, 1);
SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie);
SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie);
epfd = epoll_create1(EPOLL_CLOEXEC);
struct epoll_event epe = {0};
listen_fd = socket(listen_addr.sin_family, SOCK_DGRAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
if(listen_fd < 0){
printf("listen socket fail:%m\n");
exit(3);
}
else{
printf("listen fd:%d\n", listen_fd);
}
UdpConnectInfo* pstListenConnInfo = new_udpconnect_info();
if(pstListenConnInfo == NULL){
printf("new udpconnect info error:%m\n");
exit(4);
}
pstListenConnInfo->m_fEvHandle = on_connect;
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (const void*) &on, (socklen_t) sizeof(on));
#ifdef SO_REUSEPORT
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEPORT, (const void*) &on, (socklen_t) sizeof(on));
#endif
ret = bind(listen_fd, (struct sockaddr *)&listen_addr, (socklen_t)listen_addr_len);
if(ret != 0){
printf("bind addr[%s] error:%m\n", argv[0]);
exit(2);
}
epe.events = EPOLLIN|EPOLLET;
epe.data.fd = listen_fd;
epoll_ctl(epfd, EPOLL_CTL_ADD, listen_fd, &epe);
signal(SIGINT, signal_handler);
int ev_num;
struct epoll_event evs[10];
time_t last_time=0, time_now=0;
CustomBuffer* pstPkg = CustomBuffer::NewBuf(2000); // buffer must large than MTU
while(1){
ev_num = epoll_wait(epfd, evs, sizeof(evs)/sizeof(evs[0]), EPOLL_TIMEOUT);
if(ev_num == -1){
printf("epoll_wait error: %m\n");
break;
}
time_now = time(NULL);
for(int i=0; i < ev_num; i++){
if(evs[i].data.fd < 0){
printf("invalid epoll event. fd:%d\n", evs[i].data.fd);
continue;
}
while((pstPkg->m_iLen = recvfrom(evs[i].data.fd, pstPkg->BufBegin(), pstPkg->m_iCap, 0, (struct sockaddr *)&pstListenConnInfo->m_stBioData.m_stClientAddr, (socklen_t*)&pstListenConnInfo->m_stBioData.m_stAddrBuf.m_iLen)) > 0){
dump_addr((struct sockaddr *)&pstListenConnInfo->m_stBioData.m_stClientAddr, "<< ");
ConnectMapIterator it = stConnMap.find(&(pstListenConnInfo->m_stBioData.m_stAddrBuf));
if(it != stConnMap.end()){
dump_addr((struct sockaddr *)&pstListenConnInfo->m_stBioData.m_stClientAddr, "recv data from client: ");
UdpConnectInfo* pstConn = it->second;
pstConn->m_tLastAccess = time_now;
pstConn->m_stBioData.m_stQueue.push_back((void*)pstPkg);
ret = pstConn->m_fEvHandle(pstConn);
if(ret != 0){
if(ret > 0)
dump_addr((struct sockaddr *)&pstConn->m_stBioData.m_stClientAddr, "client shutdown socket: ");
else
dump_addr((struct sockaddr *)&pstConn->m_stBioData.m_stClientAddr, "event process fail! ");
stConnMap.erase(it);
delete pstConn;
}
}
else{
dump_addr((struct sockaddr *)&pstListenConnInfo->m_stBioData.m_stClientAddr, "new connection: ");
pstListenConnInfo->m_stBioData.m_iFd = evs[i].data.fd;
pstListenConnInfo->m_stBioData.m_stQueue.push_back((void*)pstPkg);
ret = do_new_connection(pstListenConnInfo);
}
pstPkg = CustomBuffer::NewBuf(2000);
}
}
if(time_now - last_time >= (SOCKET_IDLE_TIMEOUT / 2)){
check_idle_socket(time_now);
last_time = time_now;
}
}
for(ConnectMapIterator it=stConnMap.begin(); it != stConnMap.end(); ){
UdpConnectInfo* pstConn = it->second;
stConnMap.erase(it++);
delete pstConn;
}
free(pstPkg);
pstPkg = NULL;
delete pstListenConnInfo;
pstListenConnInfo = NULL;
close(listen_fd);
SSL_CTX_free(ctx);
BIO_s_custom_meth_free();
return(0);
}