1 /* Copyright (c) 2010-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
5 #define TORTLS_OPENSSL_PRIVATE
6 #define TOR_X509_PRIVATE
15 #include "lib/cc/compat_compiler.h"
17 /* Some versions of OpenSSL declare SSL_get_selected_srtp_profile twice in
18 * srtp.h. Suppress the GCC warning so we can build with -Wredundant-decl. */
19 DISABLE_GCC_WARNING("-Wredundant-decls")
21 #include <openssl/opensslv.h>
23 #include <openssl/ssl.h>
24 #include <openssl/ssl3.h>
25 #include <openssl/err.h>
26 #include <openssl/asn1t.h>
27 #include <openssl/x509.h>
28 #include <openssl/rsa.h>
29 #include <openssl/evp.h>
30 #include <openssl/bn.h>
32 ENABLE_GCC_WARNING("-Wredundant-decls")
34 #include "core/or/or.h"
35 #include "lib/log/log.h"
36 #include "app/config/config.h"
37 #include "lib/crypt_ops/compat_openssl.h"
38 #include "lib/tls/x509.h"
39 #include "lib/tls/x509_internal.h"
40 #include "lib/tls/tortls.h"
41 #include "lib/tls/tortls_st.h"
42 #include "lib/tls/tortls_internal.h"
43 #include "app/config/or_state_st.h"
45 #include "test/test.h"
46 #include "test/log_test_helpers.h"
47 #include "test/test_tortls.h"
49 #ifndef HAVE_SSL_STATE
50 #define OPENSSL_OPAQUE
53 #if defined(OPENSSL_OPAQUE) && !defined(LIBRESSL_VERSION_NUMBER)
54 #define SSL_STATE_STR "before SSL initialization"
56 #define SSL_STATE_STR "before/accept initialization"
59 #ifndef OPENSSL_OPAQUE
61 give_me_a_test_method(void)
63 SSL_METHOD
*method
= tor_malloc_zero(sizeof(SSL_METHOD
));
64 memcpy(method
, TLSv1_method(), sizeof(SSL_METHOD
));
69 fake_num_ciphers(void)
73 #endif /* !defined(OPENSSL_OPAQUE) */
76 mock_tls_cert_matches_key(const tor_tls_t
*tls
, const tor_x509_cert_t
*cert
)
79 (void) cert
; // XXXX look at this.
84 test_tortls_tor_tls_new(void *data
)
87 MOCK(tor_tls_cert_matches_key
, mock_tls_cert_matches_key
);
88 crypto_pk_t
*key1
= NULL
, *key2
= NULL
;
89 SSL_METHOD
*method
= NULL
;
91 key1
= pk_generate(2);
92 key2
= pk_generate(3);
94 tor_tls_t
*tls
= NULL
;
95 tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER
,
96 key1
, key2
, 86400), OP_EQ
, 0);
97 tls
= tor_tls_new(-1, 0);
99 tor_tls_free(tls
); tls
= NULL
;
101 SSL_CTX_free(client_tls_context
->ctx
);
102 client_tls_context
->ctx
= NULL
;
103 tls
= tor_tls_new(-1, 0);
104 tt_ptr_op(tls
, OP_EQ
, NULL
);
106 #ifndef OPENSSL_OPAQUE
107 method
= give_me_a_test_method();
108 SSL_CTX
*ctx
= SSL_CTX_new(method
);
109 method
->num_ciphers
= fake_num_ciphers
;
110 client_tls_context
->ctx
= ctx
;
111 tls
= tor_tls_new(-1, 0);
112 tt_ptr_op(tls
, OP_EQ
, NULL
);
113 #endif /* !defined(OPENSSL_OPAQUE) */
116 UNMOCK(tor_tls_cert_matches_key
);
117 crypto_pk_free(key1
);
118 crypto_pk_free(key2
);
127 #ifdef OPENSSL_1_1_API
128 OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS
, NULL
);
131 SSL_load_error_strings();
132 #endif /* defined(OPENSSL_1_1_API) */
136 test_tortls_get_state_description(void *ignored
)
144 ctx
= SSL_CTX_new(SSLv23_method());
146 buf
= tor_malloc_zero(1000);
147 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
149 tor_tls_get_state_description(NULL
, buf
, 20);
150 tt_str_op(buf
, OP_EQ
, "(No SSL object)");
154 tor_tls_get_state_description(tls
, buf
, 20);
155 tt_str_op(buf
, OP_EQ
, "(No SSL object)");
157 tls
->ssl
= SSL_new(ctx
);
158 tor_tls_get_state_description(tls
, buf
, 200);
159 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in HANDSHAKE");
161 tls
->state
= TOR_TLS_ST_OPEN
;
162 tor_tls_get_state_description(tls
, buf
, 200);
163 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in OPEN");
165 tls
->state
= TOR_TLS_ST_GOTCLOSE
;
166 tor_tls_get_state_description(tls
, buf
, 200);
167 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in GOTCLOSE");
169 tls
->state
= TOR_TLS_ST_SENTCLOSE
;
170 tor_tls_get_state_description(tls
, buf
, 200);
171 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in SENTCLOSE");
173 tls
->state
= TOR_TLS_ST_CLOSED
;
174 tor_tls_get_state_description(tls
, buf
, 200);
175 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in CLOSED");
177 tls
->state
= TOR_TLS_ST_RENEGOTIATE
;
178 tor_tls_get_state_description(tls
, buf
, 200);
179 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in RENEGOTIATE");
181 tls
->state
= TOR_TLS_ST_BUFFEREVENT
;
182 tor_tls_get_state_description(tls
, buf
, 200);
183 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
);
186 tor_tls_get_state_description(tls
, buf
, 200);
187 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in unknown TLS state");
197 test_tortls_get_by_ssl(void *ignored
)
206 tor_tls_allocate_tor_tls_object_ex_data_index();
208 ctx
= SSL_CTX_new(SSLv23_method());
209 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
210 tls
->magic
= TOR_TLS_MAGIC
;
214 res
= tor_tls_get_by_ssl(ssl
);
217 SSL_set_ex_data(ssl
, tor_tls_object_ex_data_index
, tls
);
219 res
= tor_tls_get_by_ssl(ssl
);
220 tt_assert(res
== tls
);
229 test_tortls_allocate_tor_tls_object_ex_data_index(void *ignored
)
234 tor_tls_allocate_tor_tls_object_ex_data_index();
236 first
= tor_tls_object_ex_data_index
;
237 tor_tls_allocate_tor_tls_object_ex_data_index();
238 tt_int_op(first
, OP_EQ
, tor_tls_object_ex_data_index
);
245 test_tortls_log_one_error(void *ignored
)
254 ctx
= SSL_CTX_new(SSLv23_method());
255 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
256 setup_capture_of_logs(LOG_INFO
);
258 tor_tls_log_one_error(NULL
, 0, LOG_WARN
, 0, "something");
259 expect_log_msg("TLS error while something: "
260 "(null) (in (null):(null):---)\n");
262 mock_clean_saved_logs();
263 tor_tls_log_one_error(tls
, 0, LOG_WARN
, 0, NULL
);
264 expect_log_msg("TLS error: (null) "
265 "(in (null):(null):---)\n");
267 mock_clean_saved_logs();
268 tls
->address
= tor_strdup("127.hello");
269 tor_tls_log_one_error(tls
, 0, LOG_WARN
, 0, NULL
);
270 expect_log_msg("TLS error with 127.hello: "
271 "(null) (in (null):(null):---)\n");
272 tor_free(tls
->address
);
274 mock_clean_saved_logs();
275 tls
->address
= tor_strdup("127.hello");
276 tor_tls_log_one_error(tls
, 0, LOG_WARN
, 0, "blarg");
277 expect_log_msg("TLS error while blarg with "
278 "127.hello: (null) (in (null):(null):---)\n");
280 mock_clean_saved_logs();
281 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, 3), LOG_WARN
, 0, NULL
);
282 expect_log_msg_containing("TLS error with 127.hello");
284 mock_clean_saved_logs();
285 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, SSL_R_HTTP_REQUEST
),
287 expect_log_severity(LOG_INFO
);
289 mock_clean_saved_logs();
290 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, SSL_R_HTTPS_PROXY_REQUEST
),
292 expect_log_severity(LOG_INFO
);
294 mock_clean_saved_logs();
295 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, SSL_R_RECORD_LENGTH_MISMATCH
),
297 expect_log_severity(LOG_INFO
);
299 #ifndef OPENSSL_1_1_API
300 mock_clean_saved_logs();
301 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, SSL_R_RECORD_TOO_LARGE
),
303 expect_log_severity(LOG_INFO
);
304 #endif /* !defined(OPENSSL_1_1_API) */
306 mock_clean_saved_logs();
307 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, SSL_R_UNKNOWN_PROTOCOL
),
309 expect_log_severity(LOG_INFO
);
311 mock_clean_saved_logs();
312 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, SSL_R_UNSUPPORTED_PROTOCOL
),
314 expect_log_severity(LOG_INFO
);
316 tls
->ssl
= SSL_new(ctx
);
318 mock_clean_saved_logs();
319 tor_tls_log_one_error(tls
, 0, LOG_WARN
, 0, NULL
);
320 expect_log_msg("TLS error with 127.hello: (null)"
321 " (in (null):(null):" SSL_STATE_STR
")\n");
324 teardown_capture_of_logs();
330 tor_free(tls
->address
);
334 #ifndef OPENSSL_OPAQUE
336 test_tortls_get_error(void *ignored
)
345 ctx
= SSL_CTX_new(SSLv23_method());
346 setup_capture_of_logs(LOG_INFO
);
347 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
348 tls
->ssl
= SSL_new(ctx
);
349 SSL_set_bio(tls
->ssl
, BIO_new(BIO_s_mem()), NULL
);
351 ret
= tor_tls_get_error(tls
, 0, 0, "something", LOG_WARN
, 0);
352 tt_int_op(ret
, OP_EQ
, TOR_TLS_ERROR_IO
);
353 expect_log_msg("TLS error: unexpected close while"
354 " something (before/accept initialization)\n");
356 mock_clean_saved_logs();
357 ret
= tor_tls_get_error(tls
, 2, 0, "something", LOG_WARN
, 0);
358 tt_int_op(ret
, OP_EQ
, 0);
359 expect_no_log_entry();
361 mock_clean_saved_logs();
362 ret
= tor_tls_get_error(tls
, 0, 1, "something", LOG_WARN
, 0);
363 tt_int_op(ret
, OP_EQ
, -11);
364 expect_no_log_entry();
366 mock_clean_saved_logs();
368 ERR_put_error(ERR_LIB_BN
, 2, -1, "somewhere.c", 99);
369 ret
= tor_tls_get_error(tls
, 0, 0, "something", LOG_WARN
, 0);
370 tt_int_op(ret
, OP_EQ
, TOR_TLS_ERROR_MISC
);
371 expect_log_msg("TLS error while something: (null)"
372 " (in bignum routines:(null):before/accept initialization)\n");
374 mock_clean_saved_logs();
376 tls
->ssl
->rwstate
= SSL_READING
;
377 SSL_get_rbio(tls
->ssl
)->flags
= BIO_FLAGS_READ
;
378 ret
= tor_tls_get_error(tls
, -1, 0, "something", LOG_WARN
, 0);
379 tt_int_op(ret
, OP_EQ
, TOR_TLS_WANTREAD
);
380 expect_no_log_entry();
382 mock_clean_saved_logs();
384 tls
->ssl
->rwstate
= SSL_READING
;
385 SSL_get_rbio(tls
->ssl
)->flags
= BIO_FLAGS_WRITE
;
386 ret
= tor_tls_get_error(tls
, -1, 0, "something", LOG_WARN
, 0);
387 tt_int_op(ret
, OP_EQ
, TOR_TLS_WANTWRITE
);
388 expect_no_log_entry();
390 mock_clean_saved_logs();
392 tls
->ssl
->rwstate
= 0;
393 tls
->ssl
->shutdown
= SSL_RECEIVED_SHUTDOWN
;
394 tls
->ssl
->s3
->warn_alert
=SSL_AD_CLOSE_NOTIFY
;
395 ret
= tor_tls_get_error(tls
, 0, 0, "something", LOG_WARN
, 0);
396 tt_int_op(ret
, OP_EQ
, TOR_TLS_CLOSE
);
399 mock_clean_saved_logs();
400 ret
= tor_tls_get_error(tls
, 0, 2, "something", LOG_WARN
, 0);
401 tt_int_op(ret
, OP_EQ
, -10);
402 expect_no_log_entry();
404 mock_clean_saved_logs();
405 ERR_put_error(ERR_LIB_SYS
, 2, -1, "somewhere.c", 99);
406 ret
= tor_tls_get_error(tls
, -1, 0, "something", LOG_WARN
, 0);
407 tt_int_op(ret
, OP_EQ
, -9);
408 expect_log_msg("TLS error while something: (null) (in system library:"
409 "connect:before/accept initialization)\n");
412 teardown_capture_of_logs();
417 #endif /* !defined(OPENSSL_OPAQUE) */
420 test_tortls_always_accept_verify_cb(void *ignored
)
425 ret
= always_accept_verify_cb(0, NULL
);
426 tt_int_op(ret
, OP_EQ
, 1);
432 #ifndef OPENSSL_OPAQUE
434 test_tortls_x509_cert_free(void *ignored
)
437 tor_x509_cert_t
*cert
;
439 cert
= tor_malloc_zero(sizeof(tor_x509_cert_t
));
440 tor_x509_cert_free(cert
);
442 cert
= tor_malloc_zero(sizeof(tor_x509_cert_t
));
443 cert
->cert
= X509_new();
444 cert
->encoded
= tor_malloc_zero(1);
445 tor_x509_cert_free(cert
);
447 #endif /* !defined(OPENSSL_OPAQUE) */
449 #ifndef OPENSSL_OPAQUE
451 * Use only for the matching fake_x509_free() call
454 fake_x509_malloc(void)
456 return tor_malloc_zero(sizeof(X509
));
460 fake_x509_free(X509
*cert
)
463 if (cert
->cert_info
) {
464 if (cert
->cert_info
->key
) {
465 if (cert
->cert_info
->key
->pkey
) {
466 tor_free(cert
->cert_info
->key
->pkey
);
468 tor_free(cert
->cert_info
->key
);
470 tor_free(cert
->cert_info
);
475 #endif /* !defined(OPENSSL_OPAQUE) */
477 #ifndef OPENSSL_OPAQUE
479 test_tortls_cert_get_key(void *ignored
)
482 tor_x509_cert_t
*cert
= NULL
;
483 crypto_pk_t
*res
= NULL
;
484 cert
= tor_malloc_zero(sizeof(tor_x509_cert_t
));
486 key
= fake_x509_malloc();
489 res
= tor_tls_cert_get_key(cert
);
493 key
->cert_info
= tor_malloc_zero(sizeof(X509_CINF
));
494 key
->cert_info
->key
= tor_malloc_zero(sizeof(X509_PUBKEY
));
495 key
->cert_info
->key
->pkey
= tor_malloc_zero(sizeof(EVP_PKEY
));
496 key
->cert_info
->key
->pkey
->references
= 1;
497 key
->cert_info
->key
->pkey
->type
= 2;
498 res
= tor_tls_cert_get_key(cert
);
506 #endif /* !defined(OPENSSL_OPAQUE) */
509 test_tortls_get_my_client_auth_key(void *ignored
)
513 crypto_pk_t
*expected
;
514 tor_tls_context_t
*ctx
;
517 ctx
= tor_malloc_zero(sizeof(tor_tls_context_t
));
518 expected
= crypto_new_pk_from_openssl_rsa_(k
);
519 ctx
->auth_key
= expected
;
521 client_tls_context
= NULL
;
522 ret
= tor_tls_get_my_client_auth_key();
525 client_tls_context
= ctx
;
526 ret
= tor_tls_get_my_client_auth_key();
527 tt_assert(ret
== expected
);
530 crypto_pk_free(expected
);
534 #ifndef HAVE_SSL_GET_CLIENT_CIPHERS
536 get_cipher_by_name(const char *name
)
539 const SSL_METHOD
*method
= SSLv23_method();
540 int num
= method
->num_ciphers();
542 for (i
= 0; i
< num
; ++i
) {
543 const SSL_CIPHER
*cipher
= method
->get_cipher(i
);
544 const char *ciphername
= SSL_CIPHER_get_name(cipher
);
545 if (!strcmp(ciphername
, name
)) {
546 return (SSL_CIPHER
*)cipher
;
552 #endif /* !defined(HAVE_SSL_GET_CLIENT_CIPHERS) */
554 #ifndef OPENSSL_OPAQUE
556 test_tortls_get_ciphersuite_name(void *ignored
)
561 ctx
= tor_malloc_zero(sizeof(tor_tls_t
));
562 ctx
->ssl
= tor_malloc_zero(sizeof(SSL
));
564 ret
= tor_tls_get_ciphersuite_name(ctx
);
565 tt_str_op(ret
, OP_EQ
, "(NONE)");
573 get_cipher_by_id(uint16_t id
)
576 const SSL_METHOD
*method
= SSLv23_method();
577 int num
= method
->num_ciphers();
578 for (i
= 0; i
< num
; ++i
) {
579 const SSL_CIPHER
*cipher
= method
->get_cipher(i
);
580 if (id
== (SSL_CIPHER_get_id(cipher
) & 0xffff)) {
581 return (SSL_CIPHER
*)cipher
;
589 test_tortls_classify_client_ciphers(void *ignored
)
597 STACK_OF(SSL_CIPHER
) *ciphers
;
598 SSL_CIPHER
*tmp_cipher
;
602 tor_tls_allocate_tor_tls_object_ex_data_index();
604 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
605 tls
->magic
= TOR_TLS_MAGIC
;
607 ctx
= SSL_CTX_new(TLSv1_method());
611 ciphers
= sk_SSL_CIPHER_new_null();
613 ret
= tor_tls_classify_client_ciphers(ssl
, NULL
);
614 tt_int_op(ret
, OP_EQ
, -1);
616 SSL_set_ex_data(ssl
, tor_tls_object_ex_data_index
, tls
);
617 tls
->client_cipher_list_type
= 42;
619 ret
= tor_tls_classify_client_ciphers(ssl
, NULL
);
620 tt_int_op(ret
, OP_EQ
, 42);
622 tls
->client_cipher_list_type
= 0;
623 ret
= tor_tls_classify_client_ciphers(ssl
, ciphers
);
624 tt_int_op(ret
, OP_EQ
, 1);
625 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 1);
627 tls
->client_cipher_list_type
= 0;
628 ret
= tor_tls_classify_client_ciphers(ssl
, SSL_get_ciphers(ssl
));
629 tt_int_op(ret
, OP_EQ
, 3);
630 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 3);
632 SSL_CIPHER
*one
= get_cipher_by_name(TLS1_TXT_DHE_RSA_WITH_AES_128_SHA
),
633 *two
= get_cipher_by_name(TLS1_TXT_DHE_RSA_WITH_AES_256_SHA
),
634 *three
= get_cipher_by_name(SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA
),
636 sk_SSL_CIPHER_push(ciphers
, one
);
637 sk_SSL_CIPHER_push(ciphers
, two
);
638 sk_SSL_CIPHER_push(ciphers
, three
);
639 sk_SSL_CIPHER_push(ciphers
, four
);
641 tls
->client_cipher_list_type
= 0;
642 ret
= tor_tls_classify_client_ciphers(ssl
, ciphers
);
643 tt_int_op(ret
, OP_EQ
, 1);
644 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 1);
646 sk_SSL_CIPHER_zero(ciphers
);
648 one
= get_cipher_by_name("ECDHE-RSA-AES256-GCM-SHA384");
651 two
= get_cipher_by_name("ECDHE-RSA-AES128-GCM-SHA256");
654 sk_SSL_CIPHER_push(ciphers
, one
);
655 tls
->client_cipher_list_type
= 0;
656 ret
= tor_tls_classify_client_ciphers(ssl
, ciphers
);
657 tt_int_op(ret
, OP_EQ
, 3);
658 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 3);
660 sk_SSL_CIPHER_push(ciphers
, two
);
661 tls
->client_cipher_list_type
= 0;
662 ret
= tor_tls_classify_client_ciphers(ssl
, ciphers
);
663 tt_int_op(ret
, OP_EQ
, 3);
664 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 3);
667 tls
->client_cipher_list_type
= 0;
668 ret
= tor_tls_classify_client_ciphers(ssl
, ciphers
);
669 tt_int_op(ret
, OP_EQ
, 3);
670 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 3);
672 sk_SSL_CIPHER_zero(ciphers
);
673 for (i
=0; v2_cipher_list
[i
]; i
++) {
674 tmp_cipher
= get_cipher_by_id(v2_cipher_list
[i
]);
675 tt_assert(tmp_cipher
);
676 sk_SSL_CIPHER_push(ciphers
, tmp_cipher
);
678 tls
->client_cipher_list_type
= 0;
679 ret
= tor_tls_classify_client_ciphers(ssl
, ciphers
);
680 tt_int_op(ret
, OP_EQ
, 2);
681 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 2);
684 sk_SSL_CIPHER_free(ciphers
);
689 #endif /* !defined(OPENSSL_OPAQUE) */
692 test_tortls_client_is_using_v2_ciphers(void *ignored
)
696 #ifdef HAVE_SSL_GET_CLIENT_CIPHERS
705 STACK_OF(SSL_CIPHER
) *ciphers
;
709 ctx
= SSL_CTX_new(TLSv1_method());
711 sess
= SSL_SESSION_new();
713 ret
= tor_tls_client_is_using_v2_ciphers(ssl
);
714 tt_int_op(ret
, OP_EQ
, -1);
717 ret
= tor_tls_client_is_using_v2_ciphers(ssl
);
718 tt_int_op(ret
, OP_EQ
, 0);
720 ciphers
= sk_SSL_CIPHER_new_null();
721 SSL_CIPHER
*one
= get_cipher_by_name("ECDHE-RSA-AES256-GCM-SHA384");
724 sk_SSL_CIPHER_push(ciphers
, one
);
725 sess
->ciphers
= ciphers
;
726 ret
= tor_tls_client_is_using_v2_ciphers(ssl
);
727 tt_int_op(ret
, OP_EQ
, 1);
731 #endif /* defined(HAVE_SSL_GET_CLIENT_CIPHERS) */
734 #ifndef OPENSSL_OPAQUE
735 static int fixed_ssl_pending_result
= 0;
738 fixed_ssl_pending(const SSL
*ignored
)
741 return fixed_ssl_pending_result
;
745 test_tortls_get_pending_bytes(void *ignored
)
752 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
753 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
754 method
= tor_malloc_zero(sizeof(SSL_METHOD
));
755 method
->ssl_pending
= fixed_ssl_pending
;
756 tls
->ssl
->method
= method
;
758 fixed_ssl_pending_result
= 42;
759 ret
= tor_tls_get_pending_bytes(tls
);
760 tt_int_op(ret
, OP_EQ
, 42);
767 #endif /* !defined(OPENSSL_OPAQUE) */
769 #ifndef OPENSSL_OPAQUE
771 test_tortls_SSL_SESSION_get_master_key(void *ignored
)
777 out
= tor_malloc_zero(1);
778 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
779 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
780 tls
->ssl
->session
= tor_malloc_zero(sizeof(SSL_SESSION
));
781 tls
->ssl
->session
->master_key_length
= 1;
783 #ifndef HAVE_SSL_SESSION_GET_MASTER_KEY
784 tls
->ssl
->session
->master_key
[0] = 43;
785 ret
= SSL_SESSION_get_master_key(tls
->ssl
->session
, out
, 0);
786 tt_int_op(ret
, OP_EQ
, 1);
787 tt_int_op(out
[0], OP_EQ
, 0);
789 ret
= SSL_SESSION_get_master_key(tls
->ssl
->session
, out
, 1);
790 tt_int_op(ret
, OP_EQ
, 1);
791 tt_int_op(out
[0], OP_EQ
, 43);
794 #endif /* !defined(HAVE_SSL_SESSION_GET_MASTER_KEY) */
795 tor_free(tls
->ssl
->session
);
800 #endif /* !defined(OPENSSL_OPAQUE) */
802 #ifndef OPENSSL_OPAQUE
804 test_tortls_get_tlssecrets(void *ignored
)
808 uint8_t *secret_out
= tor_malloc_zero(DIGEST256_LEN
);
810 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
811 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
812 tls
->ssl
->session
= tor_malloc_zero(sizeof(SSL_SESSION
));
813 tls
->ssl
->session
->master_key_length
= 1;
814 tls
->ssl
->s3
= tor_malloc_zero(sizeof(SSL3_STATE
));
816 ret
= tor_tls_get_tlssecrets(tls
, secret_out
);
817 tt_int_op(ret
, OP_EQ
, 0);
820 tor_free(secret_out
);
821 tor_free(tls
->ssl
->s3
);
822 tor_free(tls
->ssl
->session
);
826 #endif /* !defined(OPENSSL_OPAQUE) */
828 #ifndef OPENSSL_OPAQUE
830 test_tortls_get_buffer_sizes(void *ignored
)
835 size_t rbuf_c
=-1, rbuf_b
=-1, wbuf_c
=-1, wbuf_b
=-1;
837 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
838 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
839 tls
->ssl
->s3
= tor_malloc_zero(sizeof(SSL3_STATE
));
841 tls
->ssl
->s3
->rbuf
.buf
= NULL
;
842 tls
->ssl
->s3
->rbuf
.len
= 1;
843 tls
->ssl
->s3
->rbuf
.offset
= 0;
844 tls
->ssl
->s3
->rbuf
.left
= 42;
846 tls
->ssl
->s3
->wbuf
.buf
= NULL
;
847 tls
->ssl
->s3
->wbuf
.len
= 2;
848 tls
->ssl
->s3
->wbuf
.offset
= 0;
849 tls
->ssl
->s3
->wbuf
.left
= 43;
851 ret
= tor_tls_get_buffer_sizes(tls
, &rbuf_c
, &rbuf_b
, &wbuf_c
, &wbuf_b
);
852 #if OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(1,1,0)
853 tt_int_op(ret
, OP_EQ
, -1);
855 tt_int_op(ret
, OP_EQ
, 0);
856 tt_int_op(rbuf_c
, OP_EQ
, 0);
857 tt_int_op(wbuf_c
, OP_EQ
, 0);
858 tt_int_op(rbuf_b
, OP_EQ
, 42);
859 tt_int_op(wbuf_b
, OP_EQ
, 43);
861 tls
->ssl
->s3
->rbuf
.buf
= tor_malloc_zero(1);
862 tls
->ssl
->s3
->wbuf
.buf
= tor_malloc_zero(1);
863 ret
= tor_tls_get_buffer_sizes(tls
, &rbuf_c
, &rbuf_b
, &wbuf_c
, &wbuf_b
);
864 tt_int_op(ret
, OP_EQ
, 0);
865 tt_int_op(rbuf_c
, OP_EQ
, 1);
866 tt_int_op(wbuf_c
, OP_EQ
, 2);
868 #endif /* OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(1,1,0) */
871 tor_free(tls
->ssl
->s3
->rbuf
.buf
);
872 tor_free(tls
->ssl
->s3
->wbuf
.buf
);
873 tor_free(tls
->ssl
->s3
);
877 #endif /* !defined(OPENSSL_OPAQUE) */
879 #ifndef OPENSSL_OPAQUE
880 typedef struct cert_pkey_st_local
883 EVP_PKEY
*privatekey
;
884 const EVP_MD
*digest
;
887 typedef struct sess_cert_st_local
889 STACK_OF(X509
) *cert_chain
;
891 CERT_PKEY_local
*peer_key
;
892 CERT_PKEY_local peer_pkeys
[8];
897 test_tortls_try_to_extract_certs_from_tls(void *ignored
)
901 X509
*cert
= NULL
, *id_cert
= NULL
, *c1
= NULL
, *c2
= NULL
;
902 SESS_CERT_local
*sess
= NULL
;
904 c1
= read_cert_from(validCertString
);
905 c2
= read_cert_from(caCertString
);
907 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
908 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
909 tls
->ssl
->session
= tor_malloc_zero(sizeof(SSL_SESSION
));
910 sess
= tor_malloc_zero(sizeof(SESS_CERT_local
));
911 tls
->ssl
->session
->sess_cert
= (void *)sess
;
913 try_to_extract_certs_from_tls(LOG_WARN
, tls
, &cert
, &id_cert
);
917 tls
->ssl
->session
->peer
= c1
;
918 try_to_extract_certs_from_tls(LOG_WARN
, tls
, &cert
, &id_cert
);
919 tt_assert(cert
== c1
);
921 X509_free(cert
); /* decrease refcnt */
923 sess
->cert_chain
= sk_X509_new_null();
924 try_to_extract_certs_from_tls(LOG_WARN
, tls
, &cert
, &id_cert
);
925 tt_assert(cert
== c1
);
927 X509_free(cert
); /* decrease refcnt */
929 sk_X509_push(sess
->cert_chain
, c1
);
930 sk_X509_push(sess
->cert_chain
, c2
);
932 try_to_extract_certs_from_tls(LOG_WARN
, tls
, &cert
, &id_cert
);
933 tt_assert(cert
== c1
);
935 X509_free(cert
); /* decrease refcnt */
936 X509_free(id_cert
); /* decrease refcnt */
939 sk_X509_free(sess
->cert_chain
);
941 tor_free(tls
->ssl
->session
);
947 #endif /* !defined(OPENSSL_OPAQUE) */
949 #ifndef OPENSSL_OPAQUE
951 test_tortls_get_peer_cert(void *ignored
)
954 tor_x509_cert_t
*ret
;
958 cert
= read_cert_from(validCertString
);
960 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
961 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
962 tls
->ssl
->session
= tor_malloc_zero(sizeof(SSL_SESSION
));
964 ret
= tor_tls_get_peer_cert(tls
);
967 tls
->ssl
->session
->peer
= cert
;
968 ret
= tor_tls_get_peer_cert(tls
);
970 tt_assert(ret
->cert
== cert
);
973 tor_x509_cert_free(ret
);
974 tor_free(tls
->ssl
->session
);
979 #endif /* !defined(OPENSSL_OPAQUE) */
981 #ifndef OPENSSL_OPAQUE
983 test_tortls_peer_has_cert(void *ignored
)
990 cert
= read_cert_from(validCertString
);
992 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
993 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
994 tls
->ssl
->session
= tor_malloc_zero(sizeof(SSL_SESSION
));
996 ret
= tor_tls_peer_has_cert(tls
);
999 tls
->ssl
->session
->peer
= cert
;
1000 ret
= tor_tls_peer_has_cert(tls
);
1004 tor_free(tls
->ssl
->session
);
1009 #endif /* !defined(OPENSSL_OPAQUE) */
1012 test_tortls_get_write_overhead_ratio(void *ignored
)
1017 total_bytes_written_over_tls
= 0;
1018 ret
= tls_get_write_overhead_ratio();
1019 tt_double_op(fabs(ret
- 1.0), OP_LT
, 1E-12);
1021 total_bytes_written_by_tls
= 10;
1022 total_bytes_written_over_tls
= 1;
1023 ret
= tls_get_write_overhead_ratio();
1024 tt_double_op(fabs(ret
- 10.0), OP_LT
, 1E-12);
1026 total_bytes_written_by_tls
= 10;
1027 total_bytes_written_over_tls
= 2;
1028 ret
= tls_get_write_overhead_ratio();
1029 tt_double_op(fabs(ret
- 5.0), OP_LT
, 1E-12);
1036 test_tortls_is_server(void *ignored
)
1042 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1044 ret
= tor_tls_is_server(tls
);
1045 tt_int_op(ret
, OP_EQ
, 1);
1051 #ifndef OPENSSL_OPAQUE
1053 test_tortls_session_secret_cb(void *ignored
)
1058 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
1063 tor_tls_allocate_tor_tls_object_ex_data_index();
1065 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1067 tls
->magic
= TOR_TLS_MAGIC
;
1069 ctx
= SSL_CTX_new(TLSv1_method());
1070 tls
->ssl
= SSL_new(ctx
);
1071 SSL_set_ex_data(tls
->ssl
, tor_tls_object_ex_data_index
, tls
);
1073 SSL_set_session_secret_cb(tls
->ssl
, tor_tls_session_secret_cb
, NULL
);
1075 tor_tls_session_secret_cb(tls
->ssl
, NULL
, NULL
, NULL
, NULL
, NULL
);
1076 tt_assert(!tls
->ssl
->tls_session_secret_cb
);
1078 one
= get_cipher_by_name("ECDHE-RSA-AES256-GCM-SHA384");
1080 ciphers
= sk_SSL_CIPHER_new_null();
1081 sk_SSL_CIPHER_push(ciphers
, one
);
1083 tls
->client_cipher_list_type
= 0;
1084 tor_tls_session_secret_cb(tls
->ssl
, NULL
, NULL
, ciphers
, NULL
, NULL
);
1085 tt_assert(!tls
->ssl
->tls_session_secret_cb
);
1088 sk_SSL_CIPHER_free(ciphers
);
1093 #endif /* !defined(OPENSSL_OPAQUE) */
1095 #ifndef OPENSSL_OPAQUE
1096 /* TODO: It seems block_renegotiation and unblock_renegotiation and
1097 * using different blags. This might not be correct */
1099 test_tortls_block_renegotiation(void *ignored
)
1104 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1105 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1106 tls
->ssl
->s3
= tor_malloc_zero(sizeof(SSL3_STATE
));
1107 #ifndef SUPPORT_UNSAFE_RENEGOTIATION_FLAG
1108 #define SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION 0
1111 tls
->ssl
->s3
->flags
= SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
;
1113 tor_tls_block_renegotiation(tls
);
1115 #ifndef OPENSSL_1_1_API
1116 tt_assert(!(tls
->ssl
->s3
->flags
&
1117 SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
));
1121 tor_free(tls
->ssl
->s3
);
1127 test_tortls_unblock_renegotiation(void *ignored
)
1132 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1133 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1134 tor_tls_unblock_renegotiation(tls
);
1136 tt_uint_op(SSL_get_options(tls
->ssl
) &
1137 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
, OP_EQ
,
1138 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
);
1144 #endif /* !defined(OPENSSL_OPAQUE) */
1147 test_tortls_set_logged_address(void *ignored
)
1152 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1154 tor_tls_set_logged_address(tls
, "foo bar");
1156 tt_str_op(tls
->address
, OP_EQ
, "foo bar");
1158 tor_tls_set_logged_address(tls
, "foo bar 2");
1159 tt_str_op(tls
->address
, OP_EQ
, "foo bar 2");
1162 tor_free(tls
->address
);
1166 #ifndef OPENSSL_OPAQUE
1168 example_cb(tor_tls_t
*t
, void *arg
)
1175 test_tortls_set_renegotiate_callback(void *ignored
)
1179 const char *arg
= "hello";
1181 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1182 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1184 tor_tls_set_renegotiate_callback(tls
, example_cb
, (void*)arg
);
1185 tt_assert(tls
->negotiated_callback
== example_cb
);
1186 tt_assert(tls
->callback_arg
== arg
);
1187 tt_assert(!tls
->got_renegotiate
);
1189 /* Assumes V2_HANDSHAKE_SERVER */
1190 tt_assert(tls
->ssl
->info_callback
== tor_tls_server_info_callback
);
1192 tor_tls_set_renegotiate_callback(tls
, NULL
, (void*)arg
);
1193 tt_assert(tls
->ssl
->info_callback
== tor_tls_debug_state_callback
);
1199 #endif /* !defined(OPENSSL_OPAQUE) */
1201 #ifndef OPENSSL_OPAQUE
1202 static SSL_CIPHER
*fixed_cipher1
= NULL
;
1203 static SSL_CIPHER
*fixed_cipher2
= NULL
;
1204 static const SSL_CIPHER
*
1205 fake_get_cipher(unsigned ncipher
)
1210 return fixed_cipher1
;
1212 return fixed_cipher2
;
1217 #endif /* !defined(OPENSSL_OPAQUE) */
1219 #ifndef OPENSSL_OPAQUE
1221 test_tortls_find_cipher_by_id(void *ignored
)
1227 const SSL_METHOD
*m
= TLSv1_method();
1228 SSL_METHOD
*empty_method
= tor_malloc_zero(sizeof(SSL_METHOD
));
1230 fixed_cipher1
= tor_malloc_zero(sizeof(SSL_CIPHER
));
1231 fixed_cipher2
= tor_malloc_zero(sizeof(SSL_CIPHER
));
1232 fixed_cipher2
->id
= 0xC00A;
1236 ctx
= SSL_CTX_new(m
);
1239 ret
= find_cipher_by_id(ssl
, NULL
, 0xC00A);
1240 tt_int_op(ret
, OP_EQ
, 1);
1242 ret
= find_cipher_by_id(ssl
, m
, 0xC00A);
1243 tt_int_op(ret
, OP_EQ
, 1);
1245 ret
= find_cipher_by_id(ssl
, m
, 0xFFFF);
1246 tt_int_op(ret
, OP_EQ
, 0);
1248 ret
= find_cipher_by_id(ssl
, empty_method
, 0xC00A);
1249 tt_int_op(ret
, OP_EQ
, 1);
1251 ret
= find_cipher_by_id(ssl
, empty_method
, 0xFFFF);
1252 #ifdef HAVE_SSL_CIPHER_FIND
1253 tt_int_op(ret
, OP_EQ
, 0);
1255 tt_int_op(ret
, OP_EQ
, 1);
1258 empty_method
->get_cipher
= fake_get_cipher
;
1259 ret
= find_cipher_by_id(ssl
, empty_method
, 0xC00A);
1260 tt_int_op(ret
, OP_EQ
, 1);
1262 empty_method
->get_cipher
= m
->get_cipher
;
1263 empty_method
->num_ciphers
= m
->num_ciphers
;
1264 ret
= find_cipher_by_id(ssl
, empty_method
, 0xC00A);
1265 tt_int_op(ret
, OP_EQ
, 1);
1267 empty_method
->get_cipher
= fake_get_cipher
;
1268 empty_method
->num_ciphers
= m
->num_ciphers
;
1269 ret
= find_cipher_by_id(ssl
, empty_method
, 0xC00A);
1270 tt_int_op(ret
, OP_EQ
, 1);
1272 empty_method
->num_ciphers
= fake_num_ciphers
;
1273 ret
= find_cipher_by_id(ssl
, empty_method
, 0xC00A);
1274 #ifdef HAVE_SSL_CIPHER_FIND
1275 tt_int_op(ret
, OP_EQ
, 1);
1277 tt_int_op(ret
, OP_EQ
, 0);
1281 tor_free(empty_method
);
1284 tor_free(fixed_cipher1
);
1286 #endif /* !defined(OPENSSL_OPAQUE) */
1288 #ifndef OPENSSL_OPAQUE
1290 test_tortls_debug_state_callback(void *ignored
)
1294 char *buf
= tor_malloc_zero(1000);
1297 setup_capture_of_logs(LOG_DEBUG
);
1299 ssl
= tor_malloc_zero(sizeof(SSL
));
1301 tor_tls_debug_state_callback(ssl
, 32, 45);
1303 n
= tor_snprintf(buf
, 1000, "SSL %p is now in state unknown"
1304 " state [type=32,val=45].\n", ssl
);
1305 /* tor's snprintf returns -1 on error */
1306 tt_int_op(n
, OP_NE
, -1);
1307 expect_log_msg(buf
);
1310 teardown_capture_of_logs();
1314 #endif /* !defined(OPENSSL_OPAQUE) */
1316 #ifndef OPENSSL_OPAQUE
1318 test_tortls_server_info_callback(void *ignored
)
1327 ctx
= SSL_CTX_new(TLSv1_method());
1330 tor_tls_allocate_tor_tls_object_ex_data_index();
1332 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1333 tls
->magic
= TOR_TLS_MAGIC
;
1336 setup_full_capture_of_logs(LOG_WARN
);
1337 SSL_set_state(ssl
, SSL3_ST_SW_SRVR_HELLO_A
);
1338 mock_clean_saved_logs();
1339 tor_tls_server_info_callback(ssl
, SSL_CB_ACCEPT_LOOP
, 0);
1340 expect_single_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
1342 SSL_set_state(ssl
, SSL3_ST_SW_SRVR_HELLO_B
);
1343 mock_clean_saved_logs();
1344 tor_tls_server_info_callback(ssl
, SSL_CB_ACCEPT_LOOP
, 0);
1345 expect_single_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
1347 SSL_set_state(ssl
, 99);
1348 mock_clean_saved_logs();
1349 tor_tls_server_info_callback(ssl
, SSL_CB_ACCEPT_LOOP
, 0);
1350 expect_no_log_entry();
1351 teardown_capture_of_logs();
1353 SSL_set_ex_data(tls
->ssl
, tor_tls_object_ex_data_index
, tls
);
1354 SSL_set_state(ssl
, SSL3_ST_SW_SRVR_HELLO_B
);
1355 tls
->negotiated_callback
= 0;
1356 //tls->server_handshake_count = 120;
1357 tor_tls_server_info_callback(ssl
, SSL_CB_ACCEPT_LOOP
, 0);
1358 //tt_int_op(tls->server_handshake_count, OP_EQ, 121);
1360 //tls->server_handshake_count = 127;
1361 tls
->negotiated_callback
= (void *)1;
1362 tor_tls_server_info_callback(ssl
, SSL_CB_ACCEPT_LOOP
, 0);
1363 //tt_int_op(tls->server_handshake_count, OP_EQ, 127);
1364 tt_int_op(tls
->got_renegotiate
, OP_EQ
, 1);
1366 tls
->ssl
->session
= SSL_SESSION_new();
1367 tls
->wasV2Handshake
= 0;
1368 tor_tls_server_info_callback(ssl
, SSL_CB_ACCEPT_LOOP
, 0);
1369 tt_int_op(tls
->wasV2Handshake
, OP_EQ
, 0);
1372 teardown_capture_of_logs();
1377 #endif /* !defined(OPENSSL_OPAQUE) */
1379 #ifndef OPENSSL_OPAQUE
1380 static int fixed_ssl_read_result_index
;
1381 static int fixed_ssl_read_result
[5];
1384 fixed_ssl_read(SSL
*s
, void *buf
, int len
)
1389 return fixed_ssl_read_result
[fixed_ssl_read_result_index
++];
1393 dummy_handshake_func(SSL
*s
)
1399 static int negotiated_callback_called
;
1402 negotiated_callback_setter(tor_tls_t
*t
, void *arg
)
1406 negotiated_callback_called
++;
1410 test_tortls_read(void *ignored
)
1416 SSL_METHOD
*method
= give_me_a_test_method();
1417 setup_capture_of_logs(LOG_WARN
);
1419 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1420 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1421 tls
->state
= TOR_TLS_ST_OPEN
;
1423 ret
= tor_tls_read(tls
, buf
, 10);
1424 tt_int_op(ret
, OP_EQ
, -9);
1426 /* These tests assume that V2_HANDSHAKE_SERVER is set */
1427 tls
->ssl
->handshake_func
= dummy_handshake_func
;
1428 tls
->ssl
->method
= method
;
1429 method
->ssl_read
= fixed_ssl_read
;
1430 fixed_ssl_read_result_index
= 0;
1431 fixed_ssl_read_result
[0] = 42;
1432 tls
->state
= TOR_TLS_ST_OPEN
;
1434 ret
= tor_tls_read(tls
, buf
, 10);
1435 tt_int_op(ret
, OP_EQ
, 42);
1437 tls
->state
= TOR_TLS_ST_OPEN
;
1438 tls
->got_renegotiate
= 1;
1439 fixed_ssl_read_result_index
= 0;
1441 ret
= tor_tls_read(tls
, buf
, 10);
1442 tt_int_op(tls
->got_renegotiate
, OP_EQ
, 0);
1444 tls
->state
= TOR_TLS_ST_OPEN
;
1445 tls
->got_renegotiate
= 1;
1446 negotiated_callback_called
= 0;
1447 tls
->negotiated_callback
= negotiated_callback_setter
;
1448 fixed_ssl_read_result_index
= 0;
1450 ret
= tor_tls_read(tls
, buf
, 10);
1451 tt_int_op(negotiated_callback_called
, OP_EQ
, 1);
1453 #ifndef LIBRESSL_VERSION_NUMBER
1454 fixed_ssl_read_result_index
= 0;
1455 fixed_ssl_read_result
[0] = 0;
1456 tls
->ssl
->version
= SSL2_VERSION
;
1458 ret
= tor_tls_read(tls
, buf
, 10);
1459 tt_int_op(ret
, OP_EQ
, TOR_TLS_CLOSE
);
1460 tt_int_op(tls
->state
, OP_EQ
, TOR_TLS_ST_CLOSED
);
1461 #endif /* !defined(LIBRESSL_VERSION_NUMBER) */
1465 teardown_capture_of_logs();
1471 static int fixed_ssl_write_result
;
1474 fixed_ssl_write(SSL
*s
, const void *buf
, int len
)
1479 return fixed_ssl_write_result
;
1483 test_tortls_write(void *ignored
)
1488 SSL_METHOD
*method
= give_me_a_test_method();
1490 setup_capture_of_logs(LOG_WARN
);
1492 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1493 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1494 tls
->state
= TOR_TLS_ST_OPEN
;
1496 ret
= tor_tls_write(tls
, buf
, 0);
1497 tt_int_op(ret
, OP_EQ
, 0);
1499 ret
= tor_tls_write(tls
, buf
, 10);
1500 tt_int_op(ret
, OP_EQ
, -9);
1502 tls
->ssl
->method
= method
;
1503 tls
->wantwrite_n
= 1;
1504 ret
= tor_tls_write(tls
, buf
, 10);
1505 tt_int_op(tls
->wantwrite_n
, OP_EQ
, 0);
1507 method
->ssl_write
= fixed_ssl_write
;
1508 tls
->ssl
->handshake_func
= dummy_handshake_func
;
1509 fixed_ssl_write_result
= 1;
1511 ret
= tor_tls_write(tls
, buf
, 10);
1512 tt_int_op(ret
, OP_EQ
, 1);
1514 fixed_ssl_write_result
= -1;
1516 tls
->ssl
->rwstate
= SSL_READING
;
1517 SSL_set_bio(tls
->ssl
, BIO_new(BIO_s_mem()), NULL
);
1518 SSL_get_rbio(tls
->ssl
)->flags
= BIO_FLAGS_READ
;
1519 ret
= tor_tls_write(tls
, buf
, 10);
1520 tt_int_op(ret
, OP_EQ
, TOR_TLS_WANTREAD
);
1523 tls
->ssl
->rwstate
= SSL_READING
;
1524 SSL_set_bio(tls
->ssl
, BIO_new(BIO_s_mem()), NULL
);
1525 SSL_get_rbio(tls
->ssl
)->flags
= BIO_FLAGS_WRITE
;
1526 ret
= tor_tls_write(tls
, buf
, 10);
1527 tt_int_op(ret
, OP_EQ
, TOR_TLS_WANTWRITE
);
1530 teardown_capture_of_logs();
1531 BIO_free(tls
->ssl
->rbio
);
1536 #endif /* !defined(OPENSSL_OPAQUE) */
1538 #ifndef OPENSSL_OPAQUE
1539 static int fixed_ssl_accept_result
;
1540 static int fixed_ssl_connect_result
;
1543 setting_error_ssl_accept(SSL
*ssl
)
1546 ERR_put_error(ERR_LIB_BN
, 2, -1, "somewhere.c", 99);
1547 ERR_put_error(ERR_LIB_SYS
, 2, -1, "somewhere.c", 99);
1548 return fixed_ssl_accept_result
;
1552 setting_error_ssl_connect(SSL
*ssl
)
1555 ERR_put_error(ERR_LIB_BN
, 2, -1, "somewhere.c", 99);
1556 ERR_put_error(ERR_LIB_SYS
, 2, -1, "somewhere.c", 99);
1557 return fixed_ssl_connect_result
;
1561 fixed_ssl_accept(SSL
*ssl
)
1564 return fixed_ssl_accept_result
;
1568 test_tortls_handshake(void *ignored
)
1574 SSL_METHOD
*method
= give_me_a_test_method();
1575 setup_capture_of_logs(LOG_INFO
);
1578 SSL_load_error_strings();
1580 ctx
= SSL_CTX_new(TLSv1_method());
1582 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1583 tls
->ssl
= SSL_new(ctx
);
1584 tls
->state
= TOR_TLS_ST_HANDSHAKE
;
1586 ret
= tor_tls_handshake(tls
);
1587 tt_int_op(ret
, OP_EQ
, -9);
1590 tls
->state
= TOR_TLS_ST_HANDSHAKE
;
1591 ret
= tor_tls_handshake(tls
);
1592 tt_int_op(ret
, OP_EQ
, -9);
1594 tls
->ssl
->method
= method
;
1595 method
->ssl_accept
= fixed_ssl_accept
;
1596 fixed_ssl_accept_result
= 2;
1598 tls
->state
= TOR_TLS_ST_HANDSHAKE
;
1599 ret
= tor_tls_handshake(tls
);
1600 tt_int_op(tls
->state
, OP_EQ
, TOR_TLS_ST_OPEN
);
1602 method
->ssl_accept
= setting_error_ssl_accept
;
1603 fixed_ssl_accept_result
= 1;
1605 mock_clean_saved_logs();
1606 tls
->state
= TOR_TLS_ST_HANDSHAKE
;
1607 ret
= tor_tls_handshake(tls
);
1608 tt_int_op(ret
, OP_EQ
, TOR_TLS_ERROR_MISC
);
1610 /* This fails on jessie. Investigate why! */
1612 expect_log_msg("TLS error while handshaking: (null) (in bignum routines:"
1613 "(null):SSLv3 write client hello B)\n");
1614 expect_log_msg("TLS error while handshaking: (null) (in system library:"
1615 "connect:SSLv3 write client hello B)\n");
1617 expect_log_severity(LOG_INFO
);
1620 method
->ssl_connect
= setting_error_ssl_connect
;
1621 fixed_ssl_connect_result
= 1;
1623 mock_clean_saved_logs();
1624 tls
->state
= TOR_TLS_ST_HANDSHAKE
;
1625 ret
= tor_tls_handshake(tls
);
1626 tt_int_op(ret
, OP_EQ
, TOR_TLS_ERROR_MISC
);
1630 expect_log_msg("TLS error while handshaking: "
1631 "(null) (in bignum routines:(null):SSLv3 write client hello B)\n");
1632 expect_log_msg("TLS error while handshaking: "
1633 "(null) (in system library:connect:SSLv3 write client hello B)\n");
1635 expect_log_severity(LOG_WARN
);
1638 teardown_capture_of_logs();
1644 #endif /* !defined(OPENSSL_OPAQUE) */
1646 #ifndef OPENSSL_OPAQUE
1648 test_tortls_finish_handshake(void *ignored
)
1654 SSL_METHOD
*method
= give_me_a_test_method();
1656 SSL_load_error_strings();
1658 X509
*c1
= read_cert_from(validCertString
);
1659 SESS_CERT_local
*sess
= NULL
;
1661 ctx
= SSL_CTX_new(method
);
1663 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1664 tls
->ssl
= SSL_new(ctx
);
1665 tls
->state
= TOR_TLS_ST_OPEN
;
1667 ret
= tor_tls_finish_handshake(tls
);
1668 tt_int_op(ret
, OP_EQ
, 0);
1671 tls
->wasV2Handshake
= 0;
1672 setup_full_capture_of_logs(LOG_WARN
);
1673 ret
= tor_tls_finish_handshake(tls
);
1674 tt_int_op(ret
, OP_EQ
, 0);
1675 tt_int_op(tls
->wasV2Handshake
, OP_EQ
, 1);
1676 expect_single_log_msg_containing("For some reason, wasV2Handshake didn't "
1678 teardown_capture_of_logs();
1680 tls
->wasV2Handshake
= 1;
1681 ret
= tor_tls_finish_handshake(tls
);
1682 tt_int_op(ret
, OP_EQ
, 0);
1683 tt_int_op(tls
->wasV2Handshake
, OP_EQ
, 1);
1685 tls
->wasV2Handshake
= 1;
1686 tls
->ssl
->session
= SSL_SESSION_new();
1687 ret
= tor_tls_finish_handshake(tls
);
1688 tt_int_op(ret
, OP_EQ
, 0);
1689 tt_int_op(tls
->wasV2Handshake
, OP_EQ
, 0);
1693 sess
= tor_malloc_zero(sizeof(SESS_CERT_local
));
1694 tls
->ssl
->session
->sess_cert
= (void *)sess
;
1695 sess
->cert_chain
= sk_X509_new_null();
1696 sk_X509_push(sess
->cert_chain
, c1
);
1697 tls
->ssl
->session
->peer
= c1
;
1698 tls
->wasV2Handshake
= 0;
1699 ret
= tor_tls_finish_handshake(tls
);
1700 tt_int_op(ret
, OP_EQ
, 0);
1701 tt_int_op(tls
->wasV2Handshake
, OP_EQ
, 1);
1703 method
->num_ciphers
= fake_num_ciphers
;
1704 ret
= tor_tls_finish_handshake(tls
);
1705 tt_int_op(ret
, OP_EQ
, -9);
1709 sk_X509_free(sess
->cert_chain
);
1710 if (tls
->ssl
&& tls
->ssl
->session
) {
1711 tor_free(tls
->ssl
->session
->sess_cert
);
1717 teardown_capture_of_logs();
1719 #endif /* !defined(OPENSSL_OPAQUE) */
1721 static int fixed_crypto_pk_new_result_index
;
1722 static crypto_pk_t
*fixed_crypto_pk_new_result
[5];
1724 static crypto_pk_t
*
1725 fixed_crypto_pk_new(void)
1727 return fixed_crypto_pk_new_result
[fixed_crypto_pk_new_result_index
++];
1730 #ifndef OPENSSL_OPAQUE
1731 static int fixed_crypto_pk_generate_key_with_bits_result_index
;
1732 static int fixed_crypto_pk_generate_key_with_bits_result
[5];
1733 static int fixed_tor_tls_create_certificate_result_index
;
1734 static X509
*fixed_tor_tls_create_certificate_result
[5];
1735 static int fixed_tor_x509_cert_new_result_index
;
1736 static tor_x509_cert_t
*fixed_tor_x509_cert_new_result
[5];
1739 fixed_crypto_pk_generate_key_with_bits(crypto_pk_t
*env
, int bits
)
1743 return fixed_crypto_pk_generate_key_with_bits_result
[
1744 fixed_crypto_pk_generate_key_with_bits_result_index
++];
1748 fixed_tor_tls_create_certificate(crypto_pk_t
*rsa
,
1749 crypto_pk_t
*rsa_sign
,
1751 const char *cname_sign
,
1752 unsigned int cert_lifetime
)
1758 (void)cert_lifetime
;
1759 X509
*result
= fixed_tor_tls_create_certificate_result
[
1760 fixed_tor_tls_create_certificate_result_index
++];
1762 return X509_dup(result
);
1768 fixed_tor_tls_create_certificate_results_free(void)
1771 for (i
= 0; i
< ARRAY_LENGTH(fixed_tor_tls_create_certificate_result
); ++i
) {
1772 X509
*cert
= fixed_tor_tls_create_certificate_result
[i
];
1775 fixed_tor_tls_create_certificate_result
[i
] = NULL
;
1780 fixed_tor_x509_cert_new_results_free(void)
1783 for (i
= 0; i
< ARRAY_LENGTH(fixed_tor_x509_cert_new_result
); ++i
) {
1784 tor_x509_cert_free(fixed_tor_x509_cert_new_result
[i
]);
1788 static tor_x509_cert_t
*
1789 fixed_tor_x509_cert_new(tor_x509_cert_impl_t
*x509_cert
)
1792 tor_x509_cert_t
**certp
=
1793 &fixed_tor_x509_cert_new_result
[fixed_tor_x509_cert_new_result_index
++];
1794 tor_x509_cert_t
*cert
= *certp
;
1800 test_tortls_context_new(void *ignored
)
1803 tor_tls_context_t
*ret
;
1804 crypto_pk_t
*pk1
, *pk2
, *pk3
, *pk4
, *pk5
, *pk6
, *pk7
, *pk8
, *pk9
, *pk10
,
1805 *pk11
, *pk12
, *pk13
, *pk14
, *pk15
, *pk16
, *pk17
, *pk18
;
1807 pk1
= crypto_pk_new();
1808 pk2
= crypto_pk_new();
1809 pk3
= crypto_pk_new();
1810 pk4
= crypto_pk_new();
1811 pk5
= crypto_pk_new();
1812 pk6
= crypto_pk_new();
1813 pk7
= crypto_pk_new();
1814 pk8
= crypto_pk_new();
1815 pk9
= crypto_pk_new();
1816 pk10
= crypto_pk_new();
1817 pk11
= crypto_pk_new();
1818 pk12
= crypto_pk_new();
1819 pk13
= crypto_pk_new();
1820 pk14
= crypto_pk_new();
1821 pk15
= crypto_pk_new();
1822 pk16
= crypto_pk_new();
1823 pk17
= crypto_pk_new();
1824 pk18
= crypto_pk_new();
1826 fixed_crypto_pk_new_result_index
= 0;
1827 fixed_crypto_pk_new_result
[0] = NULL
;
1828 MOCK(crypto_pk_new
, fixed_crypto_pk_new
);
1829 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
1832 /* note: we already override this in testing_common.c, so we
1833 * run this unit test in a subprocess. */
1834 MOCK(crypto_pk_generate_key_with_bits
,
1835 fixed_crypto_pk_generate_key_with_bits
);
1836 fixed_crypto_pk_new_result_index
= 0;
1837 fixed_crypto_pk_new_result
[0] = pk1
;
1838 fixed_crypto_pk_new_result
[1] = NULL
;
1839 fixed_crypto_pk_generate_key_with_bits_result
[0] = -1;
1840 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
1841 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
1844 fixed_crypto_pk_new_result_index
= 0;
1845 fixed_crypto_pk_new_result
[0] = pk2
;
1846 fixed_crypto_pk_new_result
[1] = NULL
;
1847 fixed_crypto_pk_generate_key_with_bits_result
[0] = 0;
1848 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
1849 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
1852 fixed_crypto_pk_new_result_index
= 0;
1853 fixed_crypto_pk_new_result
[0] = pk3
;
1854 fixed_crypto_pk_new_result
[1] = pk4
;
1855 fixed_crypto_pk_new_result
[2] = NULL
;
1856 fixed_crypto_pk_generate_key_with_bits_result
[0] = 0;
1857 fixed_crypto_pk_generate_key_with_bits_result
[1] = -1;
1858 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
1859 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
1862 MOCK(tor_tls_create_certificate
, fixed_tor_tls_create_certificate
);
1864 fixed_crypto_pk_new_result_index
= 0;
1865 fixed_crypto_pk_new_result
[0] = pk5
;
1866 fixed_crypto_pk_new_result
[1] = pk6
;
1867 fixed_crypto_pk_new_result
[2] = NULL
;
1868 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
1869 fixed_crypto_pk_generate_key_with_bits_result
[1] = 0;
1870 fixed_tor_tls_create_certificate_result_index
= 0;
1871 fixed_tor_tls_create_certificate_result
[0] = NULL
;
1872 fixed_tor_tls_create_certificate_result
[1] = X509_new();
1873 fixed_tor_tls_create_certificate_result
[2] = X509_new();
1874 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
1876 fixed_tor_tls_create_certificate_results_free();
1878 fixed_crypto_pk_new_result_index
= 0;
1879 fixed_crypto_pk_new_result
[0] = pk7
;
1880 fixed_crypto_pk_new_result
[1] = pk8
;
1881 fixed_crypto_pk_new_result
[2] = NULL
;
1882 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
1883 fixed_tor_tls_create_certificate_result_index
= 0;
1884 fixed_tor_tls_create_certificate_result
[0] = X509_new();
1885 fixed_tor_tls_create_certificate_result
[1] = NULL
;
1886 fixed_tor_tls_create_certificate_result
[2] = X509_new();
1887 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
1889 fixed_tor_tls_create_certificate_results_free();
1891 fixed_crypto_pk_new_result_index
= 0;
1892 fixed_crypto_pk_new_result
[0] = pk9
;
1893 fixed_crypto_pk_new_result
[1] = pk10
;
1894 fixed_crypto_pk_new_result
[2] = NULL
;
1895 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
1896 fixed_tor_tls_create_certificate_result_index
= 0;
1897 fixed_tor_tls_create_certificate_result
[0] = X509_new();
1898 fixed_tor_tls_create_certificate_result
[1] = X509_new();
1899 fixed_tor_tls_create_certificate_result
[2] = NULL
;
1900 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
1902 fixed_tor_tls_create_certificate_results_free();
1904 MOCK(tor_x509_cert_new
, fixed_tor_x509_cert_new
);
1905 fixed_crypto_pk_new_result_index
= 0;
1906 fixed_crypto_pk_new_result
[0] = pk11
;
1907 fixed_crypto_pk_new_result
[1] = pk12
;
1908 fixed_crypto_pk_new_result
[2] = NULL
;
1909 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
1910 fixed_tor_tls_create_certificate_result_index
= 0;
1911 fixed_tor_tls_create_certificate_result
[0] = X509_new();
1912 fixed_tor_tls_create_certificate_result
[1] = X509_new();
1913 fixed_tor_tls_create_certificate_result
[2] = X509_new();
1914 fixed_tor_x509_cert_new_result_index
= 0;
1915 fixed_tor_x509_cert_new_result
[0] = NULL
;
1916 fixed_tor_x509_cert_new_result
[1] = NULL
;
1917 fixed_tor_x509_cert_new_result
[2] = NULL
;
1918 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
1920 fixed_tor_tls_create_certificate_results_free();
1922 fixed_crypto_pk_new_result_index
= 0;
1923 fixed_crypto_pk_new_result
[0] = pk13
;
1924 fixed_crypto_pk_new_result
[1] = pk14
;
1925 fixed_crypto_pk_new_result
[2] = NULL
;
1926 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
1927 fixed_tor_tls_create_certificate_result_index
= 0;
1928 fixed_tor_tls_create_certificate_result
[0] = X509_new();
1929 fixed_tor_tls_create_certificate_result
[1] = X509_new();
1930 fixed_tor_tls_create_certificate_result
[2] = X509_new();
1931 fixed_tor_x509_cert_new_result_index
= 0;
1932 fixed_tor_x509_cert_new_result
[0] = tor_malloc_zero(sizeof(tor_x509_cert_t
));
1933 fixed_tor_x509_cert_new_result
[1] = NULL
;
1934 fixed_tor_x509_cert_new_result
[2] = NULL
;
1935 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
1937 fixed_tor_tls_create_certificate_results_free();
1938 fixed_tor_x509_cert_new_results_free();
1940 fixed_crypto_pk_new_result_index
= 0;
1941 fixed_crypto_pk_new_result
[0] = pk15
;
1942 fixed_crypto_pk_new_result
[1] = pk16
;
1943 fixed_crypto_pk_new_result
[2] = NULL
;
1944 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
1945 fixed_tor_tls_create_certificate_result_index
= 0;
1946 fixed_tor_tls_create_certificate_result
[0] = X509_new();
1947 fixed_tor_tls_create_certificate_result
[1] = X509_new();
1948 fixed_tor_tls_create_certificate_result
[2] = X509_new();
1949 fixed_tor_x509_cert_new_result_index
= 0;
1950 fixed_tor_x509_cert_new_result
[0] = tor_malloc_zero(sizeof(tor_x509_cert_t
));
1951 fixed_tor_x509_cert_new_result
[1] = tor_malloc_zero(sizeof(tor_x509_cert_t
));
1952 fixed_tor_x509_cert_new_result
[2] = NULL
;
1953 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
1955 fixed_tor_tls_create_certificate_results_free();
1956 fixed_tor_x509_cert_new_results_free();
1958 fixed_crypto_pk_new_result_index
= 0;
1959 fixed_crypto_pk_new_result
[0] = pk17
;
1960 fixed_crypto_pk_new_result
[1] = pk18
;
1961 fixed_crypto_pk_new_result
[2] = NULL
;
1962 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
1963 fixed_tor_tls_create_certificate_result_index
= 0;
1964 fixed_tor_tls_create_certificate_result
[0] = X509_new();
1965 fixed_tor_tls_create_certificate_result
[1] = X509_new();
1966 fixed_tor_tls_create_certificate_result
[2] = X509_new();
1967 fixed_tor_x509_cert_new_result_index
= 0;
1968 fixed_tor_x509_cert_new_result
[0] = tor_malloc_zero(sizeof(tor_x509_cert_t
));
1969 fixed_tor_x509_cert_new_result
[1] = tor_malloc_zero(sizeof(tor_x509_cert_t
));
1970 fixed_tor_x509_cert_new_result
[2] = tor_malloc_zero(sizeof(tor_x509_cert_t
));
1971 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
1975 fixed_tor_tls_create_certificate_results_free();
1976 fixed_tor_x509_cert_new_results_free();
1977 UNMOCK(tor_x509_cert_new
);
1978 UNMOCK(tor_tls_create_certificate
);
1979 UNMOCK(crypto_pk_generate_key_with_bits
);
1980 UNMOCK(crypto_pk_new
);
1982 #endif /* !defined(OPENSSL_OPAQUE) */
1984 static int fixed_crypto_pk_get_evp_pkey_result_index
= 0;
1985 static EVP_PKEY
*fixed_crypto_pk_get_evp_pkey_result
[5];
1988 fixed_crypto_pk_get_evp_pkey_(crypto_pk_t
*env
, int private)
1992 return fixed_crypto_pk_get_evp_pkey_result
[
1993 fixed_crypto_pk_get_evp_pkey_result_index
++];
1997 test_tortls_create_certificate(void *ignored
)
2001 crypto_pk_t
*pk1
, *pk2
;
2003 pk1
= crypto_pk_new();
2004 pk2
= crypto_pk_new();
2006 MOCK(crypto_pk_get_openssl_evp_pkey_
, fixed_crypto_pk_get_evp_pkey_
);
2007 fixed_crypto_pk_get_evp_pkey_result_index
= 0;
2008 fixed_crypto_pk_get_evp_pkey_result
[0] = NULL
;
2009 ret
= tor_tls_create_certificate(pk1
, pk2
, "hello", "hello2", 1);
2012 fixed_crypto_pk_get_evp_pkey_result_index
= 0;
2013 fixed_crypto_pk_get_evp_pkey_result
[0] = EVP_PKEY_new();
2014 fixed_crypto_pk_get_evp_pkey_result
[1] = NULL
;
2015 ret
= tor_tls_create_certificate(pk1
, pk2
, "hello", "hello2", 1);
2018 fixed_crypto_pk_get_evp_pkey_result_index
= 0;
2019 fixed_crypto_pk_get_evp_pkey_result
[0] = EVP_PKEY_new();
2020 fixed_crypto_pk_get_evp_pkey_result
[1] = EVP_PKEY_new();
2021 ret
= tor_tls_create_certificate(pk1
, pk2
, "hello", "hello2", 1);
2025 UNMOCK(crypto_pk_get_openssl_evp_pkey_
);
2026 crypto_pk_free(pk1
);
2027 crypto_pk_free(pk2
);
2031 test_tortls_cert_new(void *ignored
)
2034 tor_x509_cert_t
*ret
;
2035 X509
*cert
= read_cert_from(validCertString
);
2037 ret
= tor_x509_cert_new(NULL
);
2040 ret
= tor_x509_cert_new(cert
);
2042 tor_x509_cert_free(ret
);
2046 cert
= read_cert_from(validCertString
);
2047 /* XXX this doesn't do what you think: it alters a copy of the pubkey. */
2048 X509_get_pubkey(cert
)->type
= EVP_PKEY_DSA
;
2049 ret
= tor_x509_cert_new(cert
);
2053 #ifndef OPENSSL_OPAQUE
2054 cert
= read_cert_from(validCertString
);
2055 X509_CINF_free(cert
->cert_info
);
2056 cert
->cert_info
= NULL
;
2057 ret
= tor_x509_cert_new(cert
);
2059 #endif /* !defined(OPENSSL_OPAQUE) */
2062 tor_x509_cert_free(ret
);
2066 test_tortls_cert_is_valid(void *ignored
)
2070 tor_x509_cert_t
*cert
= NULL
, *scert
= NULL
;
2072 scert
= tor_malloc_zero(sizeof(tor_x509_cert_t
));
2073 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 0);
2074 tt_int_op(ret
, OP_EQ
, 0);
2076 cert
= tor_malloc_zero(sizeof(tor_x509_cert_t
));
2077 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 0);
2078 tt_int_op(ret
, OP_EQ
, 0);
2082 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2083 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2084 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 0);
2085 tt_int_op(ret
, OP_EQ
, 1);
2087 #ifndef OPENSSL_OPAQUE
2088 tor_x509_cert_free(cert
);
2089 tor_x509_cert_free(scert
);
2090 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2091 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2092 ASN1_TIME_free(cert
->cert
->cert_info
->validity
->notAfter
);
2093 cert
->cert
->cert_info
->validity
->notAfter
=
2094 ASN1_TIME_set(NULL
, time(NULL
)-1000000);
2095 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 0);
2096 tt_int_op(ret
, OP_EQ
, 0);
2098 tor_x509_cert_free(cert
);
2099 tor_x509_cert_free(scert
);
2100 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2101 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2102 X509_PUBKEY_free(cert
->cert
->cert_info
->key
);
2103 cert
->cert
->cert_info
->key
= NULL
;
2104 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 1);
2105 tt_int_op(ret
, OP_EQ
, 0);
2106 #endif /* !defined(OPENSSL_OPAQUE) */
2109 tor_x509_cert_free(cert
);
2110 tor_x509_cert_free(scert
);
2111 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2112 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2113 /* This doesn't actually change the key in the cert. XXXXXX */
2114 BN_one(EVP_PKEY_get1_RSA(X509_get_pubkey(cert
->cert
))->n
);
2115 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 1);
2116 tt_int_op(ret
, OP_EQ
, 0);
2118 tor_x509_cert_free(cert
);
2119 tor_x509_cert_free(scert
);
2120 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2121 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2122 /* This doesn't actually change the key in the cert. XXXXXX */
2123 X509_get_pubkey(cert
->cert
)->type
= EVP_PKEY_EC
;
2124 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 1);
2125 tt_int_op(ret
, OP_EQ
, 0);
2127 tor_x509_cert_free(cert
);
2128 tor_x509_cert_free(scert
);
2129 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2130 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2131 /* This doesn't actually change the key in the cert. XXXXXX */
2132 X509_get_pubkey(cert
->cert
)->type
= EVP_PKEY_EC
;
2133 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 0);
2134 tt_int_op(ret
, OP_EQ
, 1);
2136 tor_x509_cert_free(cert
);
2137 tor_x509_cert_free(scert
);
2138 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2139 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2140 /* This doesn't actually change the key in the cert. XXXXXX */
2141 X509_get_pubkey(cert
->cert
)->type
= EVP_PKEY_EC
;
2142 X509_get_pubkey(cert
->cert
)->ameth
= NULL
;
2143 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 0);
2144 tt_int_op(ret
, OP_EQ
, 0);
2148 tor_x509_cert_free(cert
);
2149 tor_x509_cert_free(scert
);
2153 test_tortls_context_init_one(void *ignored
)
2157 tor_tls_context_t
*old
= NULL
;
2159 MOCK(crypto_pk_new
, fixed_crypto_pk_new
);
2161 fixed_crypto_pk_new_result_index
= 0;
2162 fixed_crypto_pk_new_result
[0] = NULL
;
2163 ret
= tor_tls_context_init_one(&old
, NULL
, 0, 0, 0);
2164 tt_int_op(ret
, OP_EQ
, -1);
2167 UNMOCK(crypto_pk_new
);
2170 #define LOCAL_TEST_CASE(name, flags) \
2171 { #name, test_tortls_##name, (flags|TT_FORK), NULL, NULL }
2173 #ifdef OPENSSL_OPAQUE
2174 #define INTRUSIVE_TEST_CASE(name, flags) \
2175 { #name, NULL, TT_SKIP, NULL, NULL }
2177 #define INTRUSIVE_TEST_CASE(name, flags) LOCAL_TEST_CASE(name, flags)
2178 #endif /* defined(OPENSSL_OPAQUE) */
2180 struct testcase_t tortls_openssl_tests
[] = {
2181 LOCAL_TEST_CASE(tor_tls_new
, TT_FORK
),
2182 LOCAL_TEST_CASE(get_state_description
, TT_FORK
),
2183 LOCAL_TEST_CASE(get_by_ssl
, TT_FORK
),
2184 LOCAL_TEST_CASE(allocate_tor_tls_object_ex_data_index
, TT_FORK
),
2185 LOCAL_TEST_CASE(log_one_error
, TT_FORK
),
2186 INTRUSIVE_TEST_CASE(get_error
, TT_FORK
),
2187 LOCAL_TEST_CASE(always_accept_verify_cb
, 0),
2188 INTRUSIVE_TEST_CASE(x509_cert_free
, 0),
2189 INTRUSIVE_TEST_CASE(cert_get_key
, 0),
2190 LOCAL_TEST_CASE(get_my_client_auth_key
, TT_FORK
),
2191 INTRUSIVE_TEST_CASE(get_ciphersuite_name
, 0),
2192 INTRUSIVE_TEST_CASE(classify_client_ciphers
, 0),
2193 LOCAL_TEST_CASE(client_is_using_v2_ciphers
, 0),
2194 INTRUSIVE_TEST_CASE(get_pending_bytes
, 0),
2195 INTRUSIVE_TEST_CASE(SSL_SESSION_get_master_key
, 0),
2196 INTRUSIVE_TEST_CASE(get_tlssecrets
, 0),
2197 INTRUSIVE_TEST_CASE(get_buffer_sizes
, 0),
2198 INTRUSIVE_TEST_CASE(try_to_extract_certs_from_tls
, 0),
2199 INTRUSIVE_TEST_CASE(get_peer_cert
, 0),
2200 INTRUSIVE_TEST_CASE(peer_has_cert
, 0),
2201 INTRUSIVE_TEST_CASE(finish_handshake
, 0),
2202 INTRUSIVE_TEST_CASE(handshake
, 0),
2203 INTRUSIVE_TEST_CASE(write
, 0),
2204 INTRUSIVE_TEST_CASE(read
, 0),
2205 INTRUSIVE_TEST_CASE(server_info_callback
, 0),
2206 LOCAL_TEST_CASE(get_write_overhead_ratio
, TT_FORK
),
2207 LOCAL_TEST_CASE(is_server
, 0),
2208 INTRUSIVE_TEST_CASE(block_renegotiation
, 0),
2209 INTRUSIVE_TEST_CASE(unblock_renegotiation
, 0),
2210 INTRUSIVE_TEST_CASE(set_renegotiate_callback
, 0),
2211 LOCAL_TEST_CASE(set_logged_address
, 0),
2212 INTRUSIVE_TEST_CASE(find_cipher_by_id
, 0),
2213 INTRUSIVE_TEST_CASE(session_secret_cb
, 0),
2214 INTRUSIVE_TEST_CASE(debug_state_callback
, 0),
2215 INTRUSIVE_TEST_CASE(context_new
, TT_FORK
/* redundant */),
2216 LOCAL_TEST_CASE(create_certificate
, 0),
2217 LOCAL_TEST_CASE(cert_new
, 0),
2218 LOCAL_TEST_CASE(cert_is_valid
, 0),
2219 LOCAL_TEST_CASE(context_init_one
, 0),