-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.cpp
More file actions
137 lines (127 loc) · 4 KB
/
server.cpp
File metadata and controls
137 lines (127 loc) · 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
/*
* server.cpp
*
* Created on: Jun 20, 2016
* Author: lancerchao
*/
#include "def.h"
#include "tls.hpp"
#include "server.hpp"
void serve_echo(void *args) {
struct server_args *serve_args = (struct server_args *)args;
SSL *ssl = serve_args->ssl;
server_type type = serve_args->type;
int client = serve_args->client;
char buf[TLS_PAYLOAD_MAX_LEN];
int bytes;
do {
bytes = type == tls_server?SSL_read(ssl, buf, sizeof(buf)):
recv(client, buf, TLS_PAYLOAD_MAX_LEN, 0);
if (bytes < 0) {
break;
}
type == tls_server ? SSL_write(ssl, buf, bytes):
send(client, buf, bytes, 0);
} while (bytes > 0);
}
void serve_delay(void *args) {
struct server_args *serve_args = (struct server_args *)args;
SSL *ssl = serve_args->ssl;
server_type type = serve_args->type;
int client = serve_args->client;
char buf[TLS_PAYLOAD_MAX_LEN];
int bytes;
do {
bytes = type == tls_server?SSL_read(ssl, buf, sizeof(buf)):
recv(client, buf, TLS_PAYLOAD_MAX_LEN, 0);
if (bytes < 0) {
break;
}
sleep(2);
type == tls_server ? SSL_write(ssl, buf, bytes):
send(client, buf, bytes, 0);
} while (bytes > 0);
}
void serve_send_twice(void *args) {
struct server_args *serve_args = (struct server_args *)args;
SSL *ssl = serve_args->ssl;
server_type type = serve_args->type;
int client = serve_args->client;
char buf[TLS_PAYLOAD_MAX_LEN];
int bytes;
do {
bytes = type == tls_server?SSL_read(ssl, buf, sizeof(buf)):
recv(client, buf, TLS_PAYLOAD_MAX_LEN, 0);
if (bytes < 0) {
break;
}
type == tls_server ? SSL_write(ssl, buf, bytes):
send(client, buf, bytes, 0);
type == tls_server ? SSL_write(ssl, buf, bytes):
send(client, buf, bytes, 0);
} while (bytes > 0);
}
/* Does one msg exchange over encrypted, then makes sure that
* send and receive works on original socket
* Server becomes a plaintext serve_echo()
*/
void serve_origfd(void *args) {
struct server_args *serve_args = (struct server_args *)args;
SSL *ssl = serve_args->ssl;
server_type type = serve_args->type;
assert(type == tls_server);
int client = serve_args->client;
char buf[TLS_PAYLOAD_MAX_LEN];
int bytes;
const char *str2 = "encrypted_message1";
const char *str1 = "plain_message1";
bytes = SSL_read(ssl, buf, sizeof(buf));
SSL_write(ssl, buf, bytes);
send(client, "rawr", strlen("rawr")+1, 0);
do {
bytes = recv(client, buf, TLS_PAYLOAD_MAX_LEN, 0);
if (bytes < 0)
break;
send(client, buf, bytes, 0);
} while (bytes > 0);
}
void serve_renegotiate(void *args) {
struct server_args *serve_args = (struct server_args *)args;
SSL *ssl = serve_args->ssl;
server_type type = serve_args->type;
assert(type == tls_server);
int client = serve_args->client;
char buf[TLS_PAYLOAD_MAX_LEN];
int bytes;
const char *str = "renegotiate!";
bytes = SSL_read(ssl, buf, sizeof(buf));
for(int i=0;i<2;i++) {
SSL_write(ssl, buf, bytes);
}
if(SSL_renegotiate(ssl) <= 0){
printf("SSL_renegotiate() failed\n");
}
if(SSL_do_handshake(ssl) <= 0){
printf("SSL_do_handshake1() failed\n");
}
ssl->state = SSL_ST_ACCEPT;
int ret = SSL_do_handshake(ssl);
if (ret <= 0) {
printf("SSL_do_handshake2() failed\n");
}
serve_echo(args);
}
void serve_client_renegotiate(void *args) {
struct server_args *serve_args = (struct server_args *)args;
SSL *ssl = serve_args->ssl;
server_type type = serve_args->type;
assert(type == tls_server);
int client = serve_args->client;
char buf[TLS_PAYLOAD_MAX_LEN];
int bytes;
const char *str = "renegotiate!";
bytes = SSL_read(ssl, buf, sizeof(buf));
SSL_write(ssl, buf, bytes);
SSL_read(ssl, buf, sizeof(buf));
serve_echo(args);
}