forked from vincentbernat/ssl-dos
/
server-vs-client.c
234 lines (203 loc) · 7.2 KB
/
server-vs-client.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
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
/*
* Copyright (c) 2011 Vincent Bernat <bernat@luffy.cx>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Tools to assess the difference of computational power between a SSL
client and a SSL server to establish a SSL connection. Test with
various ciphers and various certificates.
This program will just fork a second copy to act as a client and
exchange several SSL handshakes during a short period of time and
measure CPU time of both client and server.
*/
#include "common.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <openssl/ssl.h>
#include <openssl/bio.h>
#include <openssl/err.h>
struct result {
int handshakes; /* Number of handshakes done. */
struct timespec cpu; /* CPU time */
};
int clientserver[2];
/* OpenSSL threading */
static pthread_mutex_t *mutex_buf = NULL;
static void locking_function(int mode, int n,
const char * file,
int line) {
if (mode & CRYPTO_LOCK)
pthread_mutex_lock(&mutex_buf[n]);
else
pthread_mutex_unlock(&mutex_buf[n]);
}
static unsigned long id_function(void) {
return ((unsigned long)pthread_self());
}
/* Dunno why I should declare it */
extern int pthread_getcpuclockid (pthread_t,
clockid_t *);
/* Client part */
static void* client_thread(void *arg) {
SSL_CTX *ctx = arg;
int left = 1000; /* Number of handshakes left */
static struct result result;
result.handshakes = 0;
while (left) {
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, clientserver[0]);
if (SSL_connect(ssl) != 1)
break;
result.handshakes++;
left--;
SSL_set_shutdown(ssl, SSL_SENT_SHUTDOWN);
SSL_free(ssl);
}
clockid_t cid;
pthread_getcpuclockid(pthread_self(), &cid);
clock_gettime(cid, &result.cpu);
close(clientserver[0]);
return &result;
}
static pthread_t start_client(const char *ciphersuite) {
SSL_CTX *ctx;
start("Initializing client");
if ((ctx = SSL_CTX_new(TLSv1_client_method())) == NULL)
fail("Unable to initialize SSL context:\n%s",
ERR_error_string(ERR_get_error(), NULL));
if (SSL_CTX_set_cipher_list(ctx, ciphersuite) != 1)
fail("Unable to set cipher list to %s:\n%s",
ciphersuite,
ERR_error_string(ERR_get_error(), NULL));
pthread_t threadid;
if (pthread_create(&threadid, NULL, &client_thread, ctx))
fail("Unable to create server thread");
return threadid;
}
/* Server part */
static void* server_thread(void *arg) {
SSL_CTX *ctx = arg;
static struct result result;
result.handshakes = 0;
while (1) {
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, clientserver[1]);
if (SSL_accept(ssl) != 1)
break;
result.handshakes++;
SSL_set_shutdown(ssl, SSL_SENT_SHUTDOWN);
SSL_free(ssl);
}
clockid_t cid;
pthread_getcpuclockid(pthread_self(), &cid);
clock_gettime(cid, &result.cpu);
close(clientserver[1]);
return &result;
}
static pthread_t start_server(const char *ciphersuite,
const char *certificate) {
SSL_CTX *ctx;
start("Initializing server");
if ((ctx = SSL_CTX_new(SSLv23_server_method())) == NULL)
fail("Unable to initialize SSL context:\n%s",
ERR_error_string(ERR_get_error(), NULL));
/* Cipher suite */
if (SSL_CTX_set_cipher_list(ctx, ciphersuite) != 1)
fail("Unable to set cipher list to %s:\n%s",
ciphersuite,
ERR_error_string(ERR_get_error(), NULL));
/* Certificate */
if (SSL_CTX_use_certificate_chain_file(ctx, certificate) <= 0)
fail("Unable to use given certificate:\n%s",
ERR_error_string(ERR_get_error(), NULL));
if (SSL_CTX_use_PrivateKey_file(ctx, certificate, SSL_FILETYPE_PEM) <= 0)
fail("Unable to use given key file:\n%s",
ERR_error_string(ERR_get_error(), NULL));
/* DH */
DH *dh;
BIO *bio;
bio = BIO_new_file(certificate, "r");
if (!bio)
fail("Unable to read certificate:\n%s",
ERR_error_string(ERR_get_error(), NULL));
dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
BIO_free(bio);
if (dh) {
SSL_CTX_set_tmp_dh(ctx, dh);
DH_free(dh);
}
/* ECDH */
EC_KEY *ecdh = NULL;
ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
SSL_CTX_set_tmp_ecdh(ctx,ecdh);
EC_KEY_free(ecdh);
pthread_t threadid;
if (pthread_create(&threadid, NULL, &server_thread, ctx))
fail("Unable to create server thread");
return threadid;
}
int
main(int argc, char * const argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: \n");
fprintf(stderr, " %s ciphersuite certificate\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, " - `ciphersuite` is the name of cipher suite to use. Use\n");
fprintf(stderr, " `openssl ciphers` to choose one.\n");
fprintf(stderr, "\n");
fprintf(stderr, " - `certificate` is the name of the file containing\n");
fprintf(stderr, " the certificate, the key and appropriate additional parameters.\n");
return 1;
}
const char *ciphersuite = argv[1];
const char *certificate = argv[2];
start("Initialize OpenSSL library");
SSL_load_error_strings();
SSL_library_init();
if (!(mutex_buf = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t))))
fail("Unable to allocate memory for mutex");
for (int i = 0; i < CRYPTO_num_locks(); i++)
pthread_mutex_init(&mutex_buf[i], NULL);
CRYPTO_set_id_callback(id_function);
CRYPTO_set_locking_callback(locking_function);
pthread_t client, server;
start("Prepare client and server");
if (socketpair(AF_UNIX, SOCK_STREAM, 0, clientserver))
fail("Unable to get a socket pair for client/server communication:\n%m");
server = start_server(ciphersuite, certificate);
client = start_client(ciphersuite);
struct result *client_result, *server_result;
start("Waiting for client to finish");
if (pthread_join(client, (void **)&client_result))
fail("Unable to join client thread");
start("Waiting for server to finish");
if (pthread_join(server, (void **)&server_result))
fail("Unable to join server thread");
end("Got the following results:\n"
"Handshakes from client: %d\n"
"User CPU time in client: %4ld.%03ld\n"
"Handshakes from server: %d\n"
"User CPU time in server: %4ld.%03ld\n"
"Ratio: %.2f %%",
client_result->handshakes,
client_result->cpu.tv_sec, client_result->cpu.tv_nsec / 1000000,
server_result->handshakes,
server_result->cpu.tv_sec, server_result->cpu.tv_nsec / 1000000,
(server_result->cpu.tv_sec * 1000. + server_result->cpu.tv_nsec / 1000000.) * 100. /
(client_result->cpu.tv_sec * 1000. + client_result->cpu.tv_nsec / 1000000.));
}