VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.3/test/dtlstest.c@ 96081

最後變更 在這個檔案從96081是 94320,由 vboxsync 提交於 3 年 前

libs/openssl-3.0.1: Export to OSE and fix copyright headers in Makefiles, bugref:10128

檔案大小: 14.9 KB
 
1/*
2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#include <string.h>
11#include <openssl/bio.h>
12#include <openssl/crypto.h>
13#include <openssl/ssl.h>
14#include <openssl/err.h>
15
16#include "helpers/ssltestlib.h"
17#include "testutil.h"
18
19static char *cert = NULL;
20static char *privkey = NULL;
21static unsigned int timer_cb_count;
22
23#define NUM_TESTS 2
24
25
26#define DUMMY_CERT_STATUS_LEN 12
27
28static unsigned char certstatus[] = {
29 SSL3_RT_HANDSHAKE, /* Content type */
30 0xfe, 0xfd, /* Record version */
31 0, 1, /* Epoch */
32 0, 0, 0, 0, 0, 0x0f, /* Record sequence number */
33 0, DTLS1_HM_HEADER_LENGTH + DUMMY_CERT_STATUS_LEN - 2,
34 SSL3_MT_CERTIFICATE_STATUS, /* Cert Status handshake message type */
35 0, 0, DUMMY_CERT_STATUS_LEN, /* Message len */
36 0, 5, /* Message sequence */
37 0, 0, 0, /* Fragment offset */
38 0, 0, DUMMY_CERT_STATUS_LEN - 2, /* Fragment len */
39 0x80, 0x80, 0x80, 0x80, 0x80,
40 0x80, 0x80, 0x80, 0x80, 0x80 /* Dummy data */
41};
42
43#define RECORD_SEQUENCE 10
44
45static unsigned int timer_cb(SSL *s, unsigned int timer_us)
46{
47 ++timer_cb_count;
48
49 if (timer_us == 0)
50 return 50000;
51 else
52 return 2 * timer_us;
53}
54
55static int test_dtls_unprocessed(int testidx)
56{
57 SSL_CTX *sctx = NULL, *cctx = NULL;
58 SSL *serverssl1 = NULL, *clientssl1 = NULL;
59 BIO *c_to_s_fbio, *c_to_s_mempacket;
60 int testresult = 0;
61
62 timer_cb_count = 0;
63
64 if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
65 DTLS_client_method(),
66 DTLS1_VERSION, 0,
67 &sctx, &cctx, cert, privkey)))
68 return 0;
69
70#ifndef OPENSSL_NO_DTLS1_2
71 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
72 goto end;
73#else
74 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
75 if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA:@SECLEVEL=0"))
76 || !TEST_true(SSL_CTX_set_cipher_list(cctx,
77 "AES128-SHA:@SECLEVEL=0")))
78 goto end;
79#endif
80
81 c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
82 if (!TEST_ptr(c_to_s_fbio))
83 goto end;
84
85 /* BIO is freed by create_ssl_connection on error */
86 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
87 NULL, c_to_s_fbio)))
88 goto end;
89
90 DTLS_set_timer_cb(clientssl1, timer_cb);
91
92 if (testidx == 1)
93 certstatus[RECORD_SEQUENCE] = 0xff;
94
95 /*
96 * Inject a dummy record from the next epoch. In test 0, this should never
97 * get used because the message sequence number is too big. In test 1 we set
98 * the record sequence number to be way off in the future.
99 */
100 c_to_s_mempacket = SSL_get_wbio(clientssl1);
101 c_to_s_mempacket = BIO_next(c_to_s_mempacket);
102 mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
103 sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
104
105 /*
106 * Create the connection. We use "create_bare_ssl_connection" here so that
107 * we can force the connection to not do "SSL_read" once partly connected.
108 * We don't want to accidentally read the dummy records we injected because
109 * they will fail to decrypt.
110 */
111 if (!TEST_true(create_bare_ssl_connection(serverssl1, clientssl1,
112 SSL_ERROR_NONE, 0)))
113 goto end;
114
115 if (timer_cb_count == 0) {
116 printf("timer_callback was not called.\n");
117 goto end;
118 }
119
120 testresult = 1;
121 end:
122 SSL_free(serverssl1);
123 SSL_free(clientssl1);
124 SSL_CTX_free(sctx);
125 SSL_CTX_free(cctx);
126
127 return testresult;
128}
129
130#define CLI_TO_SRV_EPOCH_0_RECS 3
131#define CLI_TO_SRV_EPOCH_1_RECS 1
132#if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
133# define SRV_TO_CLI_EPOCH_0_RECS 10
134#else
135/*
136 * In this case we have no ServerKeyExchange message, because we don't have
137 * ECDHE or DHE. When it is present it gets fragmented into 3 records in this
138 * test.
139 */
140# define SRV_TO_CLI_EPOCH_0_RECS 9
141#endif
142#define SRV_TO_CLI_EPOCH_1_RECS 1
143#define TOTAL_FULL_HAND_RECORDS \
144 (CLI_TO_SRV_EPOCH_0_RECS + CLI_TO_SRV_EPOCH_1_RECS + \
145 SRV_TO_CLI_EPOCH_0_RECS + SRV_TO_CLI_EPOCH_1_RECS)
146
147#define CLI_TO_SRV_RESUME_EPOCH_0_RECS 3
148#define CLI_TO_SRV_RESUME_EPOCH_1_RECS 1
149#define SRV_TO_CLI_RESUME_EPOCH_0_RECS 2
150#define SRV_TO_CLI_RESUME_EPOCH_1_RECS 1
151#define TOTAL_RESUME_HAND_RECORDS \
152 (CLI_TO_SRV_RESUME_EPOCH_0_RECS + CLI_TO_SRV_RESUME_EPOCH_1_RECS + \
153 SRV_TO_CLI_RESUME_EPOCH_0_RECS + SRV_TO_CLI_RESUME_EPOCH_1_RECS)
154
155#define TOTAL_RECORDS (TOTAL_FULL_HAND_RECORDS + TOTAL_RESUME_HAND_RECORDS)
156
157/*
158 * We are assuming a ServerKeyExchange message is sent in this test. If we don't
159 * have either DH or EC, then it won't be
160 */
161#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
162static int test_dtls_drop_records(int idx)
163{
164 SSL_CTX *sctx = NULL, *cctx = NULL;
165 SSL *serverssl = NULL, *clientssl = NULL;
166 BIO *c_to_s_fbio, *mempackbio;
167 int testresult = 0;
168 int epoch = 0;
169 SSL_SESSION *sess = NULL;
170 int cli_to_srv_epoch0, cli_to_srv_epoch1, srv_to_cli_epoch0;
171
172 if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
173 DTLS_client_method(),
174 DTLS1_VERSION, 0,
175 &sctx, &cctx, cert, privkey)))
176 return 0;
177
178#ifdef OPENSSL_NO_DTLS1_2
179 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
180 if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
181 || !TEST_true(SSL_CTX_set_cipher_list(cctx,
182 "DEFAULT:@SECLEVEL=0")))
183 goto end;
184#endif
185
186 if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
187 goto end;
188
189 if (idx >= TOTAL_FULL_HAND_RECORDS) {
190 /* We're going to do a resumption handshake. Get a session first. */
191 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
192 NULL, NULL))
193 || !TEST_true(create_ssl_connection(serverssl, clientssl,
194 SSL_ERROR_NONE))
195 || !TEST_ptr(sess = SSL_get1_session(clientssl)))
196 goto end;
197
198 SSL_shutdown(clientssl);
199 SSL_shutdown(serverssl);
200 SSL_free(serverssl);
201 SSL_free(clientssl);
202 serverssl = clientssl = NULL;
203
204 cli_to_srv_epoch0 = CLI_TO_SRV_RESUME_EPOCH_0_RECS;
205 cli_to_srv_epoch1 = CLI_TO_SRV_RESUME_EPOCH_1_RECS;
206 srv_to_cli_epoch0 = SRV_TO_CLI_RESUME_EPOCH_0_RECS;
207 idx -= TOTAL_FULL_HAND_RECORDS;
208 } else {
209 cli_to_srv_epoch0 = CLI_TO_SRV_EPOCH_0_RECS;
210 cli_to_srv_epoch1 = CLI_TO_SRV_EPOCH_1_RECS;
211 srv_to_cli_epoch0 = SRV_TO_CLI_EPOCH_0_RECS;
212 }
213
214 c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
215 if (!TEST_ptr(c_to_s_fbio))
216 goto end;
217
218 /* BIO is freed by create_ssl_connection on error */
219 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
220 NULL, c_to_s_fbio)))
221 goto end;
222
223 if (sess != NULL) {
224 if (!TEST_true(SSL_set_session(clientssl, sess)))
225 goto end;
226 }
227
228 DTLS_set_timer_cb(clientssl, timer_cb);
229 DTLS_set_timer_cb(serverssl, timer_cb);
230
231 /* Work out which record to drop based on the test number */
232 if (idx >= cli_to_srv_epoch0 + cli_to_srv_epoch1) {
233 mempackbio = SSL_get_wbio(serverssl);
234 idx -= cli_to_srv_epoch0 + cli_to_srv_epoch1;
235 if (idx >= srv_to_cli_epoch0) {
236 epoch = 1;
237 idx -= srv_to_cli_epoch0;
238 }
239 } else {
240 mempackbio = SSL_get_wbio(clientssl);
241 if (idx >= cli_to_srv_epoch0) {
242 epoch = 1;
243 idx -= cli_to_srv_epoch0;
244 }
245 mempackbio = BIO_next(mempackbio);
246 }
247 BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL);
248 BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL);
249
250 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
251 goto end;
252
253 if (sess != NULL && !TEST_true(SSL_session_reused(clientssl)))
254 goto end;
255
256 /* If the test did what we planned then it should have dropped a record */
257 if (!TEST_int_eq((int)BIO_ctrl(mempackbio, MEMPACKET_CTRL_GET_DROP_REC, 0,
258 NULL), -1))
259 goto end;
260
261 testresult = 1;
262 end:
263 SSL_SESSION_free(sess);
264 SSL_free(serverssl);
265 SSL_free(clientssl);
266 SSL_CTX_free(sctx);
267 SSL_CTX_free(cctx);
268
269 return testresult;
270}
271#endif /* !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) */
272
273static const char dummy_cookie[] = "0123456";
274
275static int generate_cookie_cb(SSL *ssl, unsigned char *cookie,
276 unsigned int *cookie_len)
277{
278 memcpy(cookie, dummy_cookie, sizeof(dummy_cookie));
279 *cookie_len = sizeof(dummy_cookie);
280 return 1;
281}
282
283static int verify_cookie_cb(SSL *ssl, const unsigned char *cookie,
284 unsigned int cookie_len)
285{
286 return TEST_mem_eq(cookie, cookie_len, dummy_cookie, sizeof(dummy_cookie));
287}
288
289static int test_cookie(void)
290{
291 SSL_CTX *sctx = NULL, *cctx = NULL;
292 SSL *serverssl = NULL, *clientssl = NULL;
293 int testresult = 0;
294
295 if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
296 DTLS_client_method(),
297 DTLS1_VERSION, 0,
298 &sctx, &cctx, cert, privkey)))
299 return 0;
300
301 SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE);
302 SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
303 SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
304
305#ifdef OPENSSL_NO_DTLS1_2
306 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
307 if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
308 || !TEST_true(SSL_CTX_set_cipher_list(cctx,
309 "DEFAULT:@SECLEVEL=0")))
310 goto end;
311#endif
312
313 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
314 NULL, NULL))
315 || !TEST_true(create_ssl_connection(serverssl, clientssl,
316 SSL_ERROR_NONE)))
317 goto end;
318
319 testresult = 1;
320 end:
321 SSL_free(serverssl);
322 SSL_free(clientssl);
323 SSL_CTX_free(sctx);
324 SSL_CTX_free(cctx);
325
326 return testresult;
327}
328
329static int test_dtls_duplicate_records(void)
330{
331 SSL_CTX *sctx = NULL, *cctx = NULL;
332 SSL *serverssl = NULL, *clientssl = NULL;
333 int testresult = 0;
334
335 if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
336 DTLS_client_method(),
337 DTLS1_VERSION, 0,
338 &sctx, &cctx, cert, privkey)))
339 return 0;
340
341#ifdef OPENSSL_NO_DTLS1_2
342 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
343 if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
344 || !TEST_true(SSL_CTX_set_cipher_list(cctx,
345 "DEFAULT:@SECLEVEL=0")))
346 goto end;
347#endif
348
349 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
350 NULL, NULL)))
351 goto end;
352
353 DTLS_set_timer_cb(clientssl, timer_cb);
354 DTLS_set_timer_cb(serverssl, timer_cb);
355
356 BIO_ctrl(SSL_get_wbio(clientssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
357 BIO_ctrl(SSL_get_wbio(serverssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
358
359 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
360 goto end;
361
362 testresult = 1;
363 end:
364 SSL_free(serverssl);
365 SSL_free(clientssl);
366 SSL_CTX_free(sctx);
367 SSL_CTX_free(cctx);
368
369 return testresult;
370}
371
372/*
373 * Test just sending a Finished message as the first message. Should fail due
374 * to an unexpected message.
375 */
376static int test_just_finished(void)
377{
378 int testresult = 0, ret;
379 SSL_CTX *sctx = NULL;
380 SSL *serverssl = NULL;
381 BIO *rbio = NULL, *wbio = NULL, *sbio = NULL;
382 unsigned char buf[] = {
383 /* Record header */
384 SSL3_RT_HANDSHAKE, /* content type */
385 (DTLS1_2_VERSION >> 8) & 0xff, /* protocol version hi byte */
386 DTLS1_2_VERSION & 0xff, /* protocol version lo byte */
387 0, 0, /* epoch */
388 0, 0, 0, 0, 0, 0, /* record sequence */
389 0, DTLS1_HM_HEADER_LENGTH + SHA_DIGEST_LENGTH, /* record length */
390
391 /* Message header */
392 SSL3_MT_FINISHED, /* message type */
393 0, 0, SHA_DIGEST_LENGTH, /* message length */
394 0, 0, /* message sequence */
395 0, 0, 0, /* fragment offset */
396 0, 0, SHA_DIGEST_LENGTH, /* fragment length */
397
398 /* Message body */
399 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
400 };
401
402
403 if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
404 NULL, 0, 0,
405 &sctx, NULL, cert, privkey)))
406 return 0;
407
408 serverssl = SSL_new(sctx);
409 rbio = BIO_new(BIO_s_mem());
410 wbio = BIO_new(BIO_s_mem());
411
412 if (!TEST_ptr(serverssl) || !TEST_ptr(rbio) || !TEST_ptr(wbio))
413 goto end;
414
415 sbio = rbio;
416 SSL_set0_rbio(serverssl, rbio);
417 SSL_set0_wbio(serverssl, wbio);
418 rbio = wbio = NULL;
419 DTLS_set_timer_cb(serverssl, timer_cb);
420
421 if (!TEST_int_eq(BIO_write(sbio, buf, sizeof(buf)), sizeof(buf)))
422 goto end;
423
424 /* We expect the attempt to process the message to fail */
425 if (!TEST_int_le(ret = SSL_accept(serverssl), 0))
426 goto end;
427
428 /* Check that we got the error we were expecting */
429 if (!TEST_int_eq(SSL_get_error(serverssl, ret), SSL_ERROR_SSL))
430 goto end;
431
432 if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_UNEXPECTED_MESSAGE))
433 goto end;
434
435 testresult = 1;
436 end:
437 BIO_free(rbio);
438 BIO_free(wbio);
439 SSL_free(serverssl);
440 SSL_CTX_free(sctx);
441
442 return testresult;
443}
444
445OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
446
447int setup_tests(void)
448{
449 if (!test_skip_common_options()) {
450 TEST_error("Error parsing test options\n");
451 return 0;
452 }
453
454 if (!TEST_ptr(cert = test_get_argument(0))
455 || !TEST_ptr(privkey = test_get_argument(1)))
456 return 0;
457
458 ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
459#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
460 ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
461#endif
462 ADD_TEST(test_cookie);
463 ADD_TEST(test_dtls_duplicate_records);
464 ADD_TEST(test_just_finished);
465
466 return 1;
467}
468
469void cleanup_tests(void)
470{
471 bio_f_tls_dump_filter_free();
472 bio_s_mempacket_test_free();
473}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette