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
|
/* $OpenBSD: ssl_get_shared_ciphers.c,v 1.11 2022/02/05 18:19:39 tb Exp $ */
/*
* Copyright (c) 2021 Theo Buehler <tb@openbsd.org>
*
* Permission to use, copy, modify, and 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.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
struct peer_config {
const char *name;
int server;
uint16_t max_version;
uint16_t min_version;
const char *ciphers;
};
struct ssl_shared_ciphers_test_data {
const char *description;
struct peer_config client_config;
struct peer_config server_config;
const char *shared_ciphers;
const char *shared_ciphers_without_aesni;
};
char *server_cert;
char *server_key;
static const struct ssl_shared_ciphers_test_data ssl_shared_ciphers_tests[] = {
{
.description = "TLSv1.3 defaults",
.client_config = {
.name = "client",
.server = 0,
.max_version = TLS1_3_VERSION,
.min_version = TLS1_3_VERSION,
.ciphers =
"TLS_AES_256_GCM_SHA384:"
"TLS_CHACHA20_POLY1305_SHA256:"
"TLS_AES_128_GCM_SHA256",
},
.server_config = {
.name = "server",
.server = 1,
.max_version = TLS1_3_VERSION,
.min_version = TLS1_3_VERSION,
.ciphers =
"TLS_AES_256_GCM_SHA384:"
"TLS_CHACHA20_POLY1305_SHA256:"
"TLS_AES_128_GCM_SHA256",
},
.shared_ciphers =
"TLS_AES_256_GCM_SHA384:"
"TLS_CHACHA20_POLY1305_SHA256:"
"TLS_AES_128_GCM_SHA256",
},
{
.description = "TLSv1.3, client without ChaCha",
.client_config = {
.name = "client",
.server = 0,
.max_version = TLS1_3_VERSION,
.min_version = TLS1_3_VERSION,
.ciphers =
"TLS_AES_256_GCM_SHA384:"
"TLS_AES_128_GCM_SHA256",
},
.server_config = {
.name = "server",
.server = 1,
.max_version = TLS1_3_VERSION,
.min_version = TLS1_3_VERSION,
.ciphers =
"TLS_AES_256_GCM_SHA384:"
"TLS_CHACHA20_POLY1305_SHA256:"
"TLS_AES_128_GCM_SHA256",
},
.shared_ciphers =
"TLS_AES_256_GCM_SHA384:"
"TLS_AES_128_GCM_SHA256",
},
{
.description = "TLSv1.2",
.client_config = {
.name = "client",
.server = 0,
.max_version = TLS1_2_VERSION,
.min_version = TLS1_2_VERSION,
.ciphers =
"ECDHE-RSA-AES256-GCM-SHA384:"
"ECDHE-ECDSA-AES256-GCM-SHA384:"
"ECDHE-RSA-AES256-SHA384:"
"ECDHE-ECDSA-AES256-SHA384:"
"ECDHE-RSA-AES256-SHA:"
"ECDHE-ECDSA-AES256-SHA",
},
.server_config = {
.name = "server",
.server = 1,
.max_version = TLS1_2_VERSION,
.min_version = TLS1_2_VERSION,
.ciphers =
"ECDHE-RSA-AES256-GCM-SHA384:"
"ECDHE-ECDSA-AES256-GCM-SHA384:"
"ECDHE-RSA-AES256-SHA384:"
"ECDHE-ECDSA-AES256-SHA384:"
"ECDHE-RSA-AES256-SHA:"
"ECDHE-ECDSA-AES256-SHA",
},
.shared_ciphers =
"ECDHE-RSA-AES256-GCM-SHA384:"
"ECDHE-ECDSA-AES256-GCM-SHA384:"
"ECDHE-RSA-AES256-SHA384:"
"ECDHE-ECDSA-AES256-SHA384:"
"ECDHE-RSA-AES256-SHA:"
"ECDHE-ECDSA-AES256-SHA",
},
{
.description = "TLSv1.2, server without ECDSA",
.client_config = {
.name = "client",
.server = 0,
.max_version = TLS1_2_VERSION,
.min_version = TLS1_2_VERSION,
.ciphers =
"ECDHE-RSA-AES256-GCM-SHA384:"
"ECDHE-ECDSA-AES256-GCM-SHA384:"
"ECDHE-RSA-AES256-SHA384:"
"ECDHE-ECDSA-AES256-SHA384:"
"ECDHE-RSA-AES256-SHA:"
"ECDHE-ECDSA-AES256-SHA",
},
.server_config = {
.name = "server",
.server = 1,
.max_version = TLS1_2_VERSION,
.min_version = TLS1_2_VERSION,
.ciphers =
"ECDHE-RSA-AES256-GCM-SHA384:"
"ECDHE-RSA-AES256-SHA384:"
"ECDHE-RSA-AES256-SHA",
},
.shared_ciphers =
"ECDHE-RSA-AES256-GCM-SHA384:"
"ECDHE-RSA-AES256-SHA384:"
"ECDHE-RSA-AES256-SHA",
},
{
.description = "TLSv1.3 ciphers are prepended",
.client_config = {
.name = "client",
.server = 0,
.max_version = TLS1_3_VERSION,
.min_version = TLS1_2_VERSION,
.ciphers =
"ECDHE-RSA-AES256-GCM-SHA384",
},
.server_config = {
.name = "server",
.server = 1,
.max_version = TLS1_3_VERSION,
.min_version = TLS1_2_VERSION,
.ciphers =
"ECDHE-RSA-AES256-GCM-SHA384",
},
.shared_ciphers =
"TLS_AES_256_GCM_SHA384:"
"TLS_CHACHA20_POLY1305_SHA256:"
"TLS_AES_128_GCM_SHA256:"
"ECDHE-RSA-AES256-GCM-SHA384",
.shared_ciphers_without_aesni =
"TLS_CHACHA20_POLY1305_SHA256:"
"TLS_AES_256_GCM_SHA384:"
"TLS_AES_128_GCM_SHA256:"
"ECDHE-RSA-AES256-GCM-SHA384",
},
};
static const size_t N_SHARED_CIPHERS_TESTS =
sizeof(ssl_shared_ciphers_tests) / sizeof(ssl_shared_ciphers_tests[0]);
static SSL_CTX *
peer_config_to_ssl_ctx(const struct peer_config *config)
{
SSL_CTX *ctx;
if ((ctx = SSL_CTX_new(TLS_method())) == NULL) {
fprintf(stderr, "SSL_CTX_new(%s) failed\n", config->name);
goto err;
}
if (!SSL_CTX_set_max_proto_version(ctx, config->max_version)) {
fprintf(stderr, "max_proto_version(%s) failed\n", config->name);
goto err;
}
if (!SSL_CTX_set_min_proto_version(ctx, config->min_version)) {
fprintf(stderr, "min_proto_version(%s) failed\n", config->name);
goto err;
}
if (!SSL_CTX_set_cipher_list(ctx, config->ciphers)) {
fprintf(stderr, "set_cipher_list(%s) failed\n", config->name);
goto err;
}
if (config->server) {
if (!SSL_CTX_use_certificate_file(ctx, server_cert,
SSL_FILETYPE_PEM)) {
fprintf(stderr, "use_certificate_file(%s) failed\n",
config->name);
goto err;
}
if (!SSL_CTX_use_PrivateKey_file(ctx, server_key,
SSL_FILETYPE_PEM)) {
fprintf(stderr, "use_PrivateKey_file(%s) failed\n",
config->name);
goto err;
}
}
return ctx;
err:
SSL_CTX_free(ctx);
return NULL;
}
/* Connect client and server via a pair of "nonblocking" memory BIOs. */
static int
connect_peers(SSL *client_ssl, SSL *server_ssl, const char *description)
{
BIO *client_wbio = NULL, *server_wbio = NULL;
int ret = 0;
if ((client_wbio = BIO_new(BIO_s_mem())) == NULL) {
fprintf(stderr, "%s: failed to create client BIO\n",
description);
goto err;
}
if ((server_wbio = BIO_new(BIO_s_mem())) == NULL) {
fprintf(stderr, "%s: failed to create server BIO\n",
description);
goto err;
}
if (BIO_set_mem_eof_return(client_wbio, -1) <= 0) {
fprintf(stderr, "%s: failed to set client eof return\n",
description);
goto err;
}
if (BIO_set_mem_eof_return(server_wbio, -1) <= 0) {
fprintf(stderr, "%s: failed to set server eof return\n",
description);
goto err;
}
/* Avoid double free. SSL_set_bio() takes ownership of the BIOs. */
BIO_up_ref(client_wbio);
BIO_up_ref(server_wbio);
SSL_set_bio(client_ssl, server_wbio, client_wbio);
SSL_set_bio(server_ssl, client_wbio, server_wbio);
client_wbio = NULL;
server_wbio = NULL;
ret = 1;
err:
BIO_free(client_wbio);
BIO_free(server_wbio);
return ret;
}
static int
push_data_to_peer(SSL *ssl, int *ret, int (*func)(SSL *), const char *func_name,
const char *description)
{
int ssl_err = 0;
if (*ret == 1)
return 1;
/*
* Do SSL_connect/SSL_accept/SSL_shutdown once and loop while hitting
* WANT_WRITE. If done or on WANT_READ hand off to peer.
*/
do {
if ((*ret = func(ssl)) <= 0)
ssl_err = SSL_get_error(ssl, *ret);
} while (*ret <= 0 && ssl_err == SSL_ERROR_WANT_WRITE);
/* Ignore erroneous error - see SSL_shutdown(3)... */
if (func == SSL_shutdown && ssl_err == SSL_ERROR_SYSCALL)
return 1;
if (*ret <= 0 && ssl_err != SSL_ERROR_WANT_READ) {
fprintf(stderr, "%s: %s failed\n", description, func_name);
ERR_print_errors_fp(stderr);
return 0;
}
return 1;
}
/*
* Alternate between loops of SSL_connect() and SSL_accept() as long as only
* WANT_READ and WANT_WRITE situations are encountered. A function is repeated
* until WANT_READ is returned or it succeeds, then it's the other function's
* turn to make progress. Succeeds if SSL_connect() and SSL_accept() return 1.
*/
static int
handshake(SSL *client_ssl, SSL *server_ssl, const char *description)
{
int loops = 0, client_ret = 0, server_ret = 0;
while (loops++ < 10 && (client_ret <= 0 || server_ret <= 0)) {
if (!push_data_to_peer(client_ssl, &client_ret, SSL_connect,
"SSL_connect", description))
return 0;
if (!push_data_to_peer(server_ssl, &server_ret, SSL_accept,
"SSL_accept", description))
return 0;
}
if (client_ret != 1 || server_ret != 1) {
fprintf(stderr, "%s: failed\n", __func__);
return 0;
}
return 1;
}
static int
shutdown_peers(SSL *client_ssl, SSL *server_ssl, const char *description)
{
int loops = 0, client_ret = 0, server_ret = 0;
while (loops++ < 10 && (client_ret <= 0 || server_ret <= 0)) {
if (!push_data_to_peer(client_ssl, &client_ret, SSL_shutdown,
"client shutdown", description))
return 0;
if (!push_data_to_peer(server_ssl, &server_ret, SSL_shutdown,
"server shutdown", description))
return 0;
}
if (client_ret != 1 || server_ret != 1) {
fprintf(stderr, "%s: failed\n", __func__);
return 0;
}
return 1;
}
/* from ssl_ciph.c */
static inline int
ssl_aes_is_accelerated(void)
{
#if defined(__i386__) || defined(__x86_64__)
return ((OPENSSL_cpu_caps() & (1ULL << 57)) != 0);
#else
return (0);
#endif
}
static int
check_shared_ciphers(const struct ssl_shared_ciphers_test_data *test,
const char *got)
{
const char *want = test->shared_ciphers;
int failed;
if (!ssl_aes_is_accelerated() &&
test->shared_ciphers_without_aesni != NULL)
want = test->shared_ciphers_without_aesni;
failed = strcmp(want, got);
if (failed)
fprintf(stderr, "%s: want \"%s\", got \"%s\"\n",
test->description, want, got);
return failed;
}
static int
test_get_shared_ciphers(const struct ssl_shared_ciphers_test_data *test)
{
SSL_CTX *client_ctx = NULL, *server_ctx = NULL;
SSL *client_ssl = NULL, *server_ssl = NULL;
char buf[4096];
int failed = 1;
if ((client_ctx = peer_config_to_ssl_ctx(&test->client_config)) == NULL)
goto err;
if ((server_ctx = peer_config_to_ssl_ctx(&test->server_config)) == NULL)
goto err;
if ((client_ssl = SSL_new(client_ctx)) == NULL) {
fprintf(stderr, "%s: failed to create client SSL\n",
test->description);
goto err;
}
if ((server_ssl = SSL_new(server_ctx)) == NULL) {
fprintf(stderr, "%s: failed to create server SSL\n",
test->description);
goto err;
}
if (!connect_peers(client_ssl, server_ssl, test->description))
goto err;
if (!handshake(client_ssl, server_ssl, test->description))
goto err;
if (SSL_get_shared_ciphers(server_ssl, buf, sizeof(buf)) == NULL) {
fprintf(stderr, "%s: failed to get shared ciphers\n",
test->description);
goto err;
}
if (!shutdown_peers(client_ssl, server_ssl, test->description))
goto err;
failed = check_shared_ciphers(test, buf);
err:
SSL_CTX_free(client_ctx);
SSL_CTX_free(server_ctx);
SSL_free(client_ssl);
SSL_free(server_ssl);
return failed;
}
int
main(int argc, char **argv)
{
size_t i;
int failed = 0;
if (asprintf(&server_cert, "%s/server.pem", CERTSDIR) == -1) {
fprintf(stderr, "asprintf server_cert failed\n");
failed = 1;
goto err;
}
server_key = server_cert;
for (i = 0; i < N_SHARED_CIPHERS_TESTS; i++)
failed |= test_get_shared_ciphers(&ssl_shared_ciphers_tests[i]);
if (failed == 0)
printf("PASS %s\n", __FILE__);
err:
free(server_cert);
return failed;
}
|