-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathktls_server.c
96 lines (77 loc) · 1.75 KB
/
ktls_server.c
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
#include <ktls_server.h>
#define PORT 4433
static void serverlet(int client, SSL* ssl)/* Serve the connection -- threadable */
{
char buf[16384];
int bytes = 0;
if ( SSL_accept(ssl) == -1 ) {
ERR_print_errors_fp(stderr);
goto end;
}
if (setup_ktls(client, ssl) < 0) {
perror("setup_ktls failed");
goto end;
}
/* recv request */
bytes = SSL_read(ssl, buf, sizeof(buf));
if (bytes < 0) {
perror("SSL_read failed");
ERR_print_errors_fp(stderr);
goto end;
}
/*send response */
bytes = send(client, buf, sizeof(buf), 0);
if (bytes < 0) {
perror("KTLS send failed");
ERR_print_errors_fp(stderr);
goto end;
}
end:
return;
}
#define CRT_PEM "cert.pem"
#define KEY_PEM "key.pem"
static void ssl_main_server(int port)
{
SSL_CTX *ctx = NULL;
SSL *ssl = NULL;
/* init openssl */
init_openssl();
/* initialize SSL */
ctx = init_server_ctx();
if (!ctx) goto end;
/* load certs */
if (load_certificates(ctx, CRT_PEM, KEY_PEM) < 0) goto end;
/* set cipher list */
if (SSL_CTX_set_cipher_list(ctx, "ECDH-ECDSA-AES128-GCM-SHA256") == 0) goto end;
/* create server socket */
int server = create_ktls_server(port);
if (server < 0) goto end;
while (1) {
struct sockaddr_in addr;
unsigned int len = sizeof(addr);
/* accept connection as usual */
int client = accept(server, (struct sockaddr*) &addr, &len);
/* accept connection as usual */
ssl = SSL_new(ctx);
/* set connection socket to SSL state */
SSL_set_fd(ssl, client);
/* service connection */
serverlet(client, ssl);
SSL_free(ssl);
/* close connection */
close(client);
}
/* close server socket */
close(server);
/* release context */
SSL_CTX_free(ctx);
clean_openssl();
end:
return;
}
int main(int argv, char* argc[])
{
ssl_main_server(PORT);
return 0;
}