1 /* Copyright (c) 2010-2018, 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(redundant
-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(redundant
-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/tortls.h"
40 #include "lib/tls/tortls_st.h"
41 #include "lib/tls/tortls_internal.h"
42 #include "app/config/or_state_st.h"
44 #include "test/test.h"
45 #include "test/log_test_helpers.h"
46 #define NS_MODULE tortls
48 #ifndef HAVE_SSL_STATE
49 #define OPENSSL_OPAQUE
52 #if defined(OPENSSL_OPAQUE) && !defined(LIBRESSL_VERSION_NUMBER)
53 #define SSL_STATE_STR "before SSL initialization"
55 #define SSL_STATE_STR "before/accept initialization"
58 #ifndef OPENSSL_OPAQUE
60 give_me_a_test_method(void)
62 SSL_METHOD
*method
= tor_malloc_zero(sizeof(SSL_METHOD
));
63 memcpy(method
, TLSv1_method(), sizeof(SSL_METHOD
));
68 fake_num_ciphers(void)
72 #endif /* !defined(OPENSSL_OPAQUE) */
75 mock_tls_cert_matches_key(const tor_tls_t
*tls
, const tor_x509_cert_t
*cert
)
78 (void) cert
; // XXXX look at this.
83 test_tortls_tor_tls_new(void *data
)
86 MOCK(tor_tls_cert_matches_key
, mock_tls_cert_matches_key
);
87 crypto_pk_t
*key1
= NULL
, *key2
= NULL
;
88 SSL_METHOD
*method
= NULL
;
90 key1
= pk_generate(2);
91 key2
= pk_generate(3);
93 tor_tls_t
*tls
= NULL
;
94 tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER
,
95 key1
, key2
, 86400), OP_EQ
, 0);
96 tls
= tor_tls_new(-1, 0);
98 tor_tls_free(tls
); tls
= NULL
;
100 SSL_CTX_free(client_tls_context
->ctx
);
101 client_tls_context
->ctx
= NULL
;
102 tls
= tor_tls_new(-1, 0);
103 tt_ptr_op(tls
, OP_EQ
, NULL
);
105 #ifndef OPENSSL_OPAQUE
106 method
= give_me_a_test_method();
107 SSL_CTX
*ctx
= SSL_CTX_new(method
);
108 method
->num_ciphers
= fake_num_ciphers
;
109 client_tls_context
->ctx
= ctx
;
110 tls
= tor_tls_new(-1, 0);
111 tt_ptr_op(tls
, OP_EQ
, NULL
);
112 #endif /* !defined(OPENSSL_OPAQUE) */
115 UNMOCK(tor_tls_cert_matches_key
);
116 crypto_pk_free(key1
);
117 crypto_pk_free(key2
);
123 #define NS_MODULE tortls
128 #ifdef OPENSSL_1_1_API
129 OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS
, NULL
);
132 SSL_load_error_strings();
137 test_tortls_get_state_description(void *ignored
)
145 ctx
= SSL_CTX_new(SSLv23_method());
147 buf
= tor_malloc_zero(1000);
148 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
150 tor_tls_get_state_description(NULL
, buf
, 20);
151 tt_str_op(buf
, OP_EQ
, "(No SSL object)");
155 tor_tls_get_state_description(tls
, buf
, 20);
156 tt_str_op(buf
, OP_EQ
, "(No SSL object)");
158 tls
->ssl
= SSL_new(ctx
);
159 tor_tls_get_state_description(tls
, buf
, 200);
160 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in HANDSHAKE");
162 tls
->state
= TOR_TLS_ST_OPEN
;
163 tor_tls_get_state_description(tls
, buf
, 200);
164 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in OPEN");
166 tls
->state
= TOR_TLS_ST_GOTCLOSE
;
167 tor_tls_get_state_description(tls
, buf
, 200);
168 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in GOTCLOSE");
170 tls
->state
= TOR_TLS_ST_SENTCLOSE
;
171 tor_tls_get_state_description(tls
, buf
, 200);
172 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in SENTCLOSE");
174 tls
->state
= TOR_TLS_ST_CLOSED
;
175 tor_tls_get_state_description(tls
, buf
, 200);
176 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in CLOSED");
178 tls
->state
= TOR_TLS_ST_RENEGOTIATE
;
179 tor_tls_get_state_description(tls
, buf
, 200);
180 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in RENEGOTIATE");
182 tls
->state
= TOR_TLS_ST_BUFFEREVENT
;
183 tor_tls_get_state_description(tls
, buf
, 200);
184 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
);
187 tor_tls_get_state_description(tls
, buf
, 200);
188 tt_str_op(buf
, OP_EQ
, SSL_STATE_STR
" in unknown TLS state");
198 test_tortls_get_by_ssl(void *ignored
)
207 tor_tls_allocate_tor_tls_object_ex_data_index();
209 ctx
= SSL_CTX_new(SSLv23_method());
210 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
211 tls
->magic
= TOR_TLS_MAGIC
;
215 res
= tor_tls_get_by_ssl(ssl
);
218 SSL_set_ex_data(ssl
, tor_tls_object_ex_data_index
, tls
);
220 res
= tor_tls_get_by_ssl(ssl
);
221 tt_assert(res
== tls
);
230 test_tortls_allocate_tor_tls_object_ex_data_index(void *ignored
)
235 tor_tls_allocate_tor_tls_object_ex_data_index();
237 first
= tor_tls_object_ex_data_index
;
238 tor_tls_allocate_tor_tls_object_ex_data_index();
239 tt_int_op(first
, OP_EQ
, tor_tls_object_ex_data_index
);
246 test_tortls_log_one_error(void *ignored
)
255 ctx
= SSL_CTX_new(SSLv23_method());
256 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
257 setup_capture_of_logs(LOG_INFO
);
259 tor_tls_log_one_error(NULL
, 0, LOG_WARN
, 0, "something");
260 expect_log_msg("TLS error while something: "
261 "(null) (in (null):(null):---)\n");
263 mock_clean_saved_logs();
264 tor_tls_log_one_error(tls
, 0, LOG_WARN
, 0, NULL
);
265 expect_log_msg("TLS error: (null) "
266 "(in (null):(null):---)\n");
268 mock_clean_saved_logs();
269 tls
->address
= tor_strdup("127.hello");
270 tor_tls_log_one_error(tls
, 0, LOG_WARN
, 0, NULL
);
271 expect_log_msg("TLS error with 127.hello: "
272 "(null) (in (null):(null):---)\n");
273 tor_free(tls
->address
);
275 mock_clean_saved_logs();
276 tls
->address
= tor_strdup("127.hello");
277 tor_tls_log_one_error(tls
, 0, LOG_WARN
, 0, "blarg");
278 expect_log_msg("TLS error while blarg with "
279 "127.hello: (null) (in (null):(null):---)\n");
281 mock_clean_saved_logs();
282 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, 3), LOG_WARN
, 0, NULL
);
283 expect_log_msg("TLS error with 127.hello: "
284 "BN lib (in unknown library:(null):---)\n");
286 mock_clean_saved_logs();
287 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, SSL_R_HTTP_REQUEST
),
289 expect_log_severity(LOG_INFO
);
291 mock_clean_saved_logs();
292 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, SSL_R_HTTPS_PROXY_REQUEST
),
294 expect_log_severity(LOG_INFO
);
296 mock_clean_saved_logs();
297 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, SSL_R_RECORD_LENGTH_MISMATCH
),
299 expect_log_severity(LOG_INFO
);
301 #ifndef OPENSSL_1_1_API
302 mock_clean_saved_logs();
303 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, SSL_R_RECORD_TOO_LARGE
),
305 expect_log_severity(LOG_INFO
);
306 #endif /* !defined(OPENSSL_1_1_API) */
308 mock_clean_saved_logs();
309 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, SSL_R_UNKNOWN_PROTOCOL
),
311 expect_log_severity(LOG_INFO
);
313 mock_clean_saved_logs();
314 tor_tls_log_one_error(tls
, ERR_PACK(1, 2, SSL_R_UNSUPPORTED_PROTOCOL
),
316 expect_log_severity(LOG_INFO
);
318 tls
->ssl
= SSL_new(ctx
);
320 mock_clean_saved_logs();
321 tor_tls_log_one_error(tls
, 0, LOG_WARN
, 0, NULL
);
322 expect_log_msg("TLS error with 127.hello: (null)"
323 " (in (null):(null):" SSL_STATE_STR
")\n");
326 teardown_capture_of_logs();
332 tor_free(tls
->address
);
336 #ifndef OPENSSL_OPAQUE
338 test_tortls_get_error(void *ignored
)
347 ctx
= SSL_CTX_new(SSLv23_method());
348 setup_capture_of_logs(LOG_INFO
);
349 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
350 tls
->ssl
= SSL_new(ctx
);
351 SSL_set_bio(tls
->ssl
, BIO_new(BIO_s_mem()), NULL
);
353 ret
= tor_tls_get_error(tls
, 0, 0, "something", LOG_WARN
, 0);
354 tt_int_op(ret
, OP_EQ
, TOR_TLS_ERROR_IO
);
355 expect_log_msg("TLS error: unexpected close while"
356 " something (before/accept initialization)\n");
358 mock_clean_saved_logs();
359 ret
= tor_tls_get_error(tls
, 2, 0, "something", LOG_WARN
, 0);
360 tt_int_op(ret
, OP_EQ
, 0);
361 expect_no_log_entry();
363 mock_clean_saved_logs();
364 ret
= tor_tls_get_error(tls
, 0, 1, "something", LOG_WARN
, 0);
365 tt_int_op(ret
, OP_EQ
, -11);
366 expect_no_log_entry();
368 mock_clean_saved_logs();
370 ERR_put_error(ERR_LIB_BN
, 2, -1, "somewhere.c", 99);
371 ret
= tor_tls_get_error(tls
, 0, 0, "something", LOG_WARN
, 0);
372 tt_int_op(ret
, OP_EQ
, TOR_TLS_ERROR_MISC
);
373 expect_log_msg("TLS error while something: (null)"
374 " (in bignum routines:(null):before/accept initialization)\n");
376 mock_clean_saved_logs();
378 tls
->ssl
->rwstate
= SSL_READING
;
379 SSL_get_rbio(tls
->ssl
)->flags
= BIO_FLAGS_READ
;
380 ret
= tor_tls_get_error(tls
, -1, 0, "something", LOG_WARN
, 0);
381 tt_int_op(ret
, OP_EQ
, TOR_TLS_WANTREAD
);
382 expect_no_log_entry();
384 mock_clean_saved_logs();
386 tls
->ssl
->rwstate
= SSL_READING
;
387 SSL_get_rbio(tls
->ssl
)->flags
= BIO_FLAGS_WRITE
;
388 ret
= tor_tls_get_error(tls
, -1, 0, "something", LOG_WARN
, 0);
389 tt_int_op(ret
, OP_EQ
, TOR_TLS_WANTWRITE
);
390 expect_no_log_entry();
392 mock_clean_saved_logs();
394 tls
->ssl
->rwstate
= 0;
395 tls
->ssl
->shutdown
= SSL_RECEIVED_SHUTDOWN
;
396 tls
->ssl
->s3
->warn_alert
=SSL_AD_CLOSE_NOTIFY
;
397 ret
= tor_tls_get_error(tls
, 0, 0, "something", LOG_WARN
, 0);
398 tt_int_op(ret
, OP_EQ
, TOR_TLS_CLOSE
);
401 mock_clean_saved_logs();
402 ret
= tor_tls_get_error(tls
, 0, 2, "something", LOG_WARN
, 0);
403 tt_int_op(ret
, OP_EQ
, -10);
404 expect_no_log_entry();
406 mock_clean_saved_logs();
407 ERR_put_error(ERR_LIB_SYS
, 2, -1, "somewhere.c", 99);
408 ret
= tor_tls_get_error(tls
, -1, 0, "something", LOG_WARN
, 0);
409 tt_int_op(ret
, OP_EQ
, -9);
410 expect_log_msg("TLS error while something: (null) (in system library:"
411 "connect:before/accept initialization)\n");
414 teardown_capture_of_logs();
419 #endif /* !defined(OPENSSL_OPAQUE) */
422 test_tortls_always_accept_verify_cb(void *ignored
)
427 ret
= always_accept_verify_cb(0, NULL
);
428 tt_int_op(ret
, OP_EQ
, 1);
434 #ifndef OPENSSL_OPAQUE
436 test_tortls_x509_cert_free(void *ignored
)
439 tor_x509_cert_t
*cert
;
441 cert
= tor_malloc_zero(sizeof(tor_x509_cert_t
));
442 tor_x509_cert_free(cert
);
444 cert
= tor_malloc_zero(sizeof(tor_x509_cert_t
));
445 cert
->cert
= X509_new();
446 cert
->encoded
= tor_malloc_zero(1);
447 tor_x509_cert_free(cert
);
449 #endif /* !defined(OPENSSL_OPAQUE) */
451 #ifndef OPENSSL_OPAQUE
453 fixed_pub_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
460 * Use only for the matching fake_x509_free() call
463 fake_x509_malloc(void)
465 return tor_malloc_zero(sizeof(X509
));
469 fake_x509_free(X509
*cert
)
472 if (cert
->cert_info
) {
473 if (cert
->cert_info
->key
) {
474 if (cert
->cert_info
->key
->pkey
) {
475 tor_free(cert
->cert_info
->key
->pkey
);
477 tor_free(cert
->cert_info
->key
);
479 tor_free(cert
->cert_info
);
486 test_tortls_cert_matches_key(void *ignored
)
491 tor_x509_cert_t
*cert
;
492 X509
*one
= NULL
, *two
= NULL
;
493 EVP_PKEY_ASN1_METHOD
*meth
= EVP_PKEY_asn1_new(999, 0, NULL
, NULL
);
494 EVP_PKEY_asn1_set_public(meth
, NULL
, NULL
, fixed_pub_cmp
, NULL
, NULL
, NULL
);
496 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
497 cert
= tor_malloc_zero(sizeof(tor_x509_cert_t
));
498 one
= fake_x509_malloc();
500 two
= fake_x509_malloc();
503 res
= tor_tls_cert_matches_key(tls
, cert
);
504 tt_int_op(res
, OP_EQ
, 0);
506 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
507 tls
->ssl
->session
= tor_malloc_zero(sizeof(SSL_SESSION
));
508 tls
->ssl
->session
->peer
= one
;
509 res
= tor_tls_cert_matches_key(tls
, cert
);
510 tt_int_op(res
, OP_EQ
, 0);
513 res
= tor_tls_cert_matches_key(tls
, cert
);
514 tt_int_op(res
, OP_EQ
, 0);
516 one
->cert_info
= tor_malloc_zero(sizeof(X509_CINF
));
517 one
->cert_info
->key
= tor_malloc_zero(sizeof(X509_PUBKEY
));
518 one
->cert_info
->key
->pkey
= tor_malloc_zero(sizeof(EVP_PKEY
));
519 one
->cert_info
->key
->pkey
->references
= 1;
520 one
->cert_info
->key
->pkey
->ameth
= meth
;
521 one
->cert_info
->key
->pkey
->type
= 1;
523 two
->cert_info
= tor_malloc_zero(sizeof(X509_CINF
));
524 two
->cert_info
->key
= tor_malloc_zero(sizeof(X509_PUBKEY
));
525 two
->cert_info
->key
->pkey
= tor_malloc_zero(sizeof(EVP_PKEY
));
526 two
->cert_info
->key
->pkey
->references
= 1;
527 two
->cert_info
->key
->pkey
->ameth
= meth
;
528 two
->cert_info
->key
->pkey
->type
= 2;
530 res
= tor_tls_cert_matches_key(tls
, cert
);
531 tt_int_op(res
, OP_EQ
, 0);
533 one
->cert_info
->key
->pkey
->type
= 1;
534 two
->cert_info
->key
->pkey
->type
= 1;
535 res
= tor_tls_cert_matches_key(tls
, cert
);
536 tt_int_op(res
, OP_EQ
, 1);
539 EVP_PKEY_asn1_free(meth
);
540 tor_free(tls
->ssl
->session
);
549 test_tortls_cert_get_key(void *ignored
)
552 tor_x509_cert_t
*cert
= NULL
;
553 crypto_pk_t
*res
= NULL
;
554 cert
= tor_malloc_zero(sizeof(tor_x509_cert_t
));
556 key
= fake_x509_malloc();
559 res
= tor_tls_cert_get_key(cert
);
563 key
->cert_info
= tor_malloc_zero(sizeof(X509_CINF
));
564 key
->cert_info
->key
= tor_malloc_zero(sizeof(X509_PUBKEY
));
565 key
->cert_info
->key
->pkey
= tor_malloc_zero(sizeof(EVP_PKEY
));
566 key
->cert_info
->key
->pkey
->references
= 1;
567 key
->cert_info
->key
->pkey
->type
= 2;
568 res
= tor_tls_cert_get_key(cert
);
576 #endif /* !defined(OPENSSL_OPAQUE) */
579 test_tortls_get_my_client_auth_key(void *ignored
)
583 crypto_pk_t
*expected
;
584 tor_tls_context_t
*ctx
;
587 ctx
= tor_malloc_zero(sizeof(tor_tls_context_t
));
588 expected
= crypto_new_pk_from_openssl_rsa_(k
);
589 ctx
->auth_key
= expected
;
591 client_tls_context
= NULL
;
592 ret
= tor_tls_get_my_client_auth_key();
595 client_tls_context
= ctx
;
596 ret
= tor_tls_get_my_client_auth_key();
597 tt_assert(ret
== expected
);
604 #ifndef HAVE_SSL_GET_CLIENT_CIPHERS
606 get_cipher_by_name(const char *name
)
609 const SSL_METHOD
*method
= SSLv23_method();
610 int num
= method
->num_ciphers();
612 for (i
= 0; i
< num
; ++i
) {
613 const SSL_CIPHER
*cipher
= method
->get_cipher(i
);
614 const char *ciphername
= SSL_CIPHER_get_name(cipher
);
615 if (!strcmp(ciphername
, name
)) {
616 return (SSL_CIPHER
*)cipher
;
622 #endif /* !defined(HAVE_SSL_GET_CLIENT_CIPHERS) */
624 #ifndef OPENSSL_OPAQUE
626 test_tortls_get_ciphersuite_name(void *ignored
)
631 ctx
= tor_malloc_zero(sizeof(tor_tls_t
));
632 ctx
->ssl
= tor_malloc_zero(sizeof(SSL
));
634 ret
= tor_tls_get_ciphersuite_name(ctx
);
635 tt_str_op(ret
, OP_EQ
, "(NONE)");
643 get_cipher_by_id(uint16_t id
)
646 const SSL_METHOD
*method
= SSLv23_method();
647 int num
= method
->num_ciphers();
648 for (i
= 0; i
< num
; ++i
) {
649 const SSL_CIPHER
*cipher
= method
->get_cipher(i
);
650 if (id
== (SSL_CIPHER_get_id(cipher
) & 0xffff)) {
651 return (SSL_CIPHER
*)cipher
;
659 test_tortls_classify_client_ciphers(void *ignored
)
667 STACK_OF(SSL_CIPHER
) *ciphers
;
668 SSL_CIPHER
*tmp_cipher
;
672 tor_tls_allocate_tor_tls_object_ex_data_index();
674 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
675 tls
->magic
= TOR_TLS_MAGIC
;
677 ctx
= SSL_CTX_new(TLSv1_method());
681 ciphers
= sk_SSL_CIPHER_new_null();
683 ret
= tor_tls_classify_client_ciphers(ssl
, NULL
);
684 tt_int_op(ret
, OP_EQ
, -1);
686 SSL_set_ex_data(ssl
, tor_tls_object_ex_data_index
, tls
);
687 tls
->client_cipher_list_type
= 42;
689 ret
= tor_tls_classify_client_ciphers(ssl
, NULL
);
690 tt_int_op(ret
, OP_EQ
, 42);
692 tls
->client_cipher_list_type
= 0;
693 ret
= tor_tls_classify_client_ciphers(ssl
, ciphers
);
694 tt_int_op(ret
, OP_EQ
, 1);
695 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 1);
697 tls
->client_cipher_list_type
= 0;
698 ret
= tor_tls_classify_client_ciphers(ssl
, SSL_get_ciphers(ssl
));
699 tt_int_op(ret
, OP_EQ
, 3);
700 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 3);
702 SSL_CIPHER
*one
= get_cipher_by_name(TLS1_TXT_DHE_RSA_WITH_AES_128_SHA
),
703 *two
= get_cipher_by_name(TLS1_TXT_DHE_RSA_WITH_AES_256_SHA
),
704 *three
= get_cipher_by_name(SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA
),
706 sk_SSL_CIPHER_push(ciphers
, one
);
707 sk_SSL_CIPHER_push(ciphers
, two
);
708 sk_SSL_CIPHER_push(ciphers
, three
);
709 sk_SSL_CIPHER_push(ciphers
, four
);
711 tls
->client_cipher_list_type
= 0;
712 ret
= tor_tls_classify_client_ciphers(ssl
, ciphers
);
713 tt_int_op(ret
, OP_EQ
, 1);
714 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 1);
716 sk_SSL_CIPHER_zero(ciphers
);
718 one
= get_cipher_by_name("ECDHE-RSA-AES256-GCM-SHA384");
721 two
= get_cipher_by_name("ECDHE-RSA-AES128-GCM-SHA256");
724 sk_SSL_CIPHER_push(ciphers
, one
);
725 tls
->client_cipher_list_type
= 0;
726 ret
= tor_tls_classify_client_ciphers(ssl
, ciphers
);
727 tt_int_op(ret
, OP_EQ
, 3);
728 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 3);
730 sk_SSL_CIPHER_push(ciphers
, two
);
731 tls
->client_cipher_list_type
= 0;
732 ret
= tor_tls_classify_client_ciphers(ssl
, ciphers
);
733 tt_int_op(ret
, OP_EQ
, 3);
734 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 3);
737 tls
->client_cipher_list_type
= 0;
738 ret
= tor_tls_classify_client_ciphers(ssl
, ciphers
);
739 tt_int_op(ret
, OP_EQ
, 3);
740 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 3);
742 sk_SSL_CIPHER_zero(ciphers
);
743 for (i
=0; v2_cipher_list
[i
]; i
++) {
744 tmp_cipher
= get_cipher_by_id(v2_cipher_list
[i
]);
745 tt_assert(tmp_cipher
);
746 sk_SSL_CIPHER_push(ciphers
, tmp_cipher
);
748 tls
->client_cipher_list_type
= 0;
749 ret
= tor_tls_classify_client_ciphers(ssl
, ciphers
);
750 tt_int_op(ret
, OP_EQ
, 2);
751 tt_int_op(tls
->client_cipher_list_type
, OP_EQ
, 2);
754 sk_SSL_CIPHER_free(ciphers
);
759 #endif /* !defined(OPENSSL_OPAQUE) */
762 test_tortls_client_is_using_v2_ciphers(void *ignored
)
766 #ifdef HAVE_SSL_GET_CLIENT_CIPHERS
775 STACK_OF(SSL_CIPHER
) *ciphers
;
779 ctx
= SSL_CTX_new(TLSv1_method());
781 sess
= SSL_SESSION_new();
783 ret
= tor_tls_client_is_using_v2_ciphers(ssl
);
784 tt_int_op(ret
, OP_EQ
, -1);
787 ret
= tor_tls_client_is_using_v2_ciphers(ssl
);
788 tt_int_op(ret
, OP_EQ
, 0);
790 ciphers
= sk_SSL_CIPHER_new_null();
791 SSL_CIPHER
*one
= get_cipher_by_name("ECDHE-RSA-AES256-GCM-SHA384");
794 sk_SSL_CIPHER_push(ciphers
, one
);
795 sess
->ciphers
= ciphers
;
796 ret
= tor_tls_client_is_using_v2_ciphers(ssl
);
797 tt_int_op(ret
, OP_EQ
, 1);
801 #endif /* defined(HAVE_SSL_GET_CLIENT_CIPHERS) */
804 #ifndef OPENSSL_OPAQUE
805 static X509
*fixed_try_to_extract_certs_from_tls_cert_out_result
= NULL
;
806 static X509
*fixed_try_to_extract_certs_from_tls_id_cert_out_result
= NULL
;
809 fixed_try_to_extract_certs_from_tls(int severity
, tor_tls_t
*tls
,
810 tor_x509_cert_impl_t
**cert_out
,
811 tor_x509_cert_impl_t
**id_cert_out
)
815 *cert_out
= fixed_try_to_extract_certs_from_tls_cert_out_result
;
816 *id_cert_out
= fixed_try_to_extract_certs_from_tls_id_cert_out_result
;
818 #endif /* !defined(OPENSSL_OPAQUE) */
820 #ifndef OPENSSL_OPAQUE
821 static const char* notCompletelyValidCertString
=
822 "-----BEGIN CERTIFICATE-----\n"
823 "MIICVjCCAb8CAg37MA0GCSqGSIb3DQEBBQUAMIGbMQswCQYDVQQGEwJKUDEOMAwG\n"
824 "A1UECBMFVG9reW8xEDAOBgNVBAcTB0NodW8ta3UxETAPBgNVBAoTCEZyYW5rNERE\n"
825 "MRgwFgYDVQQLEw9XZWJDZXJ0IFN1cHBvcnQxGDAWBgNVBAMTD0ZyYW5rNEREIFdl\n"
826 "YiBDQTEjMCEGCSqGSIb3DQEJARYUc3VwcG9ydEBmcmFuazRkZC5jb20wHhcNMTIw\n"
827 "ODIyMDUyNzIzWhcNMTcwODIxMDUyNzIzWjBKMQswCQYDVQQGEwJKUDEOMAwGA1UE\n"
828 "CAwFVG9reW8xETAPBgNVBAoMCEZyYW5rNEREMRgwFgYDVQQDDA93d3cuZXhhbXBs\n"
829 "ZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMYBBrx5PlP0WNI/ZdzD\n"
830 "+6Pktmurn+F2kQYbtc7XQh8/LTBvCo+P6iZoLEmUA9e7EXLRxgU1CVqeAi7QcAn9\n"
831 "MwBlc8ksFJHB0rtf9pmf8Oza9E0Bynlq/4/Kb1x+d+AyhL7oK9tQwB24uHOueHi1\n"
832 "C/iVv8CSWKiYe6hzN1txYe8rAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAASPdjigJ\n"
833 "kXCqKWpnZ/Oc75EUcMi6HztaW8abUMlYXPIgkV2F7YanHOB7K4f7OOLjiz8DTPFf\n"
834 "jC9UeuErhaA/zzWi8ewMTFZW/WshOrm3fNvcMrMLKtH534JKvcdMg6qIdjTFINIr\n"
835 "evnAhf0cwULaebn+lMs8Pdl7y37+sfluVok=\n"
836 "-----END CERTIFICATE-----\n";
837 #endif /* !defined(OPENSSL_OPAQUE) */
839 static const char* validCertString
= "-----BEGIN CERTIFICATE-----\n"
840 "MIIDpTCCAY0CAg3+MA0GCSqGSIb3DQEBBQUAMF4xCzAJBgNVBAYTAlVTMREwDwYD\n"
841 "VQQIDAhJbGxpbm9pczEQMA4GA1UEBwwHQ2hpY2FnbzEUMBIGA1UECgwLVG9yIFRl\n"
842 "c3RpbmcxFDASBgNVBAMMC1RvciBUZXN0aW5nMB4XDTE1MDkwNjEzMzk1OVoXDTQz\n"
843 "MDEyMjEzMzk1OVowVjELMAkGA1UEBhMCVVMxEDAOBgNVBAcMB0NoaWNhZ28xFDAS\n"
844 "BgNVBAoMC1RvciBUZXN0aW5nMR8wHQYDVQQDDBZ0ZXN0aW5nLnRvcnByb2plY3Qu\n"
845 "b3JnMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDoT6uyVVhWyOF3wkHjjYbd\n"
846 "nKaykyRv4JVtKQdZ4OpEErmX1zw4MmyzpQNV6iR4bQnWiyLfzyVJMZDIC/WILBfX\n"
847 "w2Pza/yuLgUvDc3twMuhOACzOQVO8PrEF/aVv2+hbCCy2udXvKhnYn+CCXl3ozc8\n"
848 "XcKYvujTXDyvGWY3xwAjlQIDAQABMA0GCSqGSIb3DQEBBQUAA4ICAQCUvnhzQWuQ\n"
849 "MrN+pERkE+zcTI/9dGS90rUMMLgu8VDNqTa0TUQh8uO0EQ6uDvI8Js6e8tgwS0BR\n"
850 "UBahqb7ZHv+rejGCBr5OudqD+x4STiiuPNJVs86JTLN8SpM9CHjIBH5WCCN2KOy3\n"
851 "mevNoRcRRyYJzSFULCunIK6FGulszigMYGscrO4oiTkZiHPh9KvWT40IMiHfL+Lw\n"
852 "EtEWiLex6064LcA2YQ1AMuSZyCexks63lcfaFmQbkYOKqXa1oLkIRuDsOaSVjTfe\n"
853 "vec+X6jvf12cFTKS5WIeqkKF2Irt+dJoiHEGTe5RscUMN/f+gqHPzfFz5dR23sxo\n"
854 "g+HC6MZHlFkLAOx3wW6epPS8A/m1mw3zMPoTnb2U2YYt8T0dJMMlUn/7Y1sEAa+a\n"
855 "dSTMaeUf6VnJ//11m454EZl1to9Z7oJOgqmFffSrdD4BGIWe8f7hhW6L1Enmqe/J\n"
856 "BKL3wbzZh80O1W0bndAwhnEEhlzneFY84cbBo9pmVxpODHkUcStpr5Z7pBDrcL21\n"
857 "Ss/aB/1YrsVXhdvJdOGxl3Mnl9dUY57CympLGlT8f0pPS6GAKOelECOhFMHmJd8L\n"
858 "dj3XQSmKtYHevZ6IvuMXSlB/fJvSjSlkCuLo5+kJoaqPuRu+i/S1qxeRy3CBwmnE\n"
859 "LdSNdcX4N79GQJ996PA8+mUCQG7YRtK+WA==\n"
860 "-----END CERTIFICATE-----\n";
862 static const char* caCertString
= "-----BEGIN CERTIFICATE-----\n"
863 "MIIFjzCCA3egAwIBAgIJAKd5WgyfPMYRMA0GCSqGSIb3DQEBCwUAMF4xCzAJBgNV\n"
864 "BAYTAlVTMREwDwYDVQQIDAhJbGxpbm9pczEQMA4GA1UEBwwHQ2hpY2FnbzEUMBIG\n"
865 "A1UECgwLVG9yIFRlc3RpbmcxFDASBgNVBAMMC1RvciBUZXN0aW5nMB4XDTE1MDkw\n"
866 "NjEzMzc0MVoXDTQzMDEyMjEzMzc0MVowXjELMAkGA1UEBhMCVVMxETAPBgNVBAgM\n"
867 "CElsbGlub2lzMRAwDgYDVQQHDAdDaGljYWdvMRQwEgYDVQQKDAtUb3IgVGVzdGlu\n"
868 "ZzEUMBIGA1UEAwwLVG9yIFRlc3RpbmcwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAw\n"
869 "ggIKAoICAQCpLMUEiLW5leUgBZoEJms2V7lZRhIAjnJBhVMHD0e3UubNknmaQoxf\n"
870 "ARz3rvqOaRd0JlV+qM9qE0DjiYcCVP1cAfqAo9d83uS1vwY3YMVJzADlaIiHfyVW\n"
871 "uEgBy0vvkeUBqaua24dYlcwsemOiXYLu41yM1wkcGHW1AhBNHppY6cznb8TyLgNM\n"
872 "2x3SGUdzc5XMyAFx51faKGBA3wjs+Hg1PLY7d30nmCgEOBavpm5I1disM/0k+Mcy\n"
873 "YmAKEo/iHJX/rQzO4b9znP69juLlR8PDBUJEVIG/CYb6+uw8MjjUyiWXYoqfVmN2\n"
874 "hm/lH8b6rXw1a2Aa3VTeD0DxaWeacMYHY/i01fd5n7hCoDTRNdSw5KJ0L3Z0SKTu\n"
875 "0lzffKzDaIfyZGlpW5qdouACkWYzsaitQOePVE01PIdO30vUfzNTFDfy42ccx3Di\n"
876 "59UCu+IXB+eMtrBfsok0Qc63vtF1linJgjHW1z/8ujk8F7/qkOfODhk4l7wngc2A\n"
877 "EmwWFIFoGaiTEZHB9qteXr4unbXZ0AHpM02uGGwZEGohjFyebEb73M+J57WKKAFb\n"
878 "PqbLcGUksL1SHNBNAJcVLttX55sO4nbidOS/kA3m+F1R04MBTyQF9qA6YDDHqdI3\n"
879 "h/3pw0Z4fxVouTYT4/NfRnX4JTP4u+7Mpcoof28VME0qWqD1LnRhFQIDAQABo1Aw\n"
880 "TjAdBgNVHQ4EFgQUMoAgIXH7pZ3QMRwTjT+DM9Yo/v0wHwYDVR0jBBgwFoAUMoAg\n"
881 "IXH7pZ3QMRwTjT+DM9Yo/v0wDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOC\n"
882 "AgEAUJxacjXR9sT+Xs6ISFiUsyd0T6WVKMnV46xrYJHirGfx+krWHrjxMY+ZtxYD\n"
883 "DBDGlo11Qc4v6QrclNf5QUBfIiGQsP9Cm6hHcQ+Tpg9HHCgSqG1YNPwCPReCR4br\n"
884 "BLvLfrfkcBL2IWM0PdQdCze+59DBfipsULD2mEn9fjYRXQEwb2QWtQ9qRc20Yb/x\n"
885 "Q4b/+CvUodLkaq7B8MHz0BV8HHcBoph6DYaRmO/N+hPauIuSp6XyaGYcEefGKVKj\n"
886 "G2+fcsdyXsoijNdL8vNKwm4j2gVwCBnw16J00yfFoV46YcbfqEdJB2je0XSvwXqt\n"
887 "14AOTngxso2h9k9HLtrfpO1ZG/B5AcCMs1lzbZ2fp5DPHtjvvmvA2RJqgo3yjw4W\n"
888 "4DHAuTglYFlC3mDHNfNtcGP20JvepcQNzNP2UzwcpOc94hfKikOFw+gf9Vf1qd0y\n"
889 "h/Sk6OZHn2+JVUPiWHIQV98Vtoh4RmUZDJD+b55ia3fQGTGzt4z1XFzQYSva5sfs\n"
890 "wocS/papthqWldQU7x+3wofNd5CNU1x6WKXG/yw30IT/4F8ADJD6GeygNT8QJYvt\n"
891 "u/8lAkbOy6B9xGmSvr0Kk1oq9P2NshA6kalxp1Oz/DTNDdL4AeBXV3JmM6WWCjGn\n"
892 "Yy1RT69d0rwYc5u/vnqODz1IjvT90smsrkBumGt791FAFeg=\n"
893 "-----END CERTIFICATE-----\n";
896 read_cert_from(const char *str
)
898 BIO
*bio
= BIO_new(BIO_s_mem());
899 BIO_write(bio
, str
, (int) strlen(str
));
900 X509
*res
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
905 #ifndef OPENSSL_OPAQUE
907 test_tortls_verify(void *ignored
)
912 crypto_pk_t
*k
= NULL
;
913 X509
*cert1
= NULL
, *cert2
= NULL
, *invalidCert
= NULL
,
914 *validCert
= NULL
, *caCert
= NULL
;
916 cert1
= tor_malloc_zero(sizeof(X509
));
917 cert1
->references
= 10;
919 cert2
= tor_malloc_zero(sizeof(X509
));
920 cert2
->references
= 10;
922 validCert
= read_cert_from(validCertString
);
923 caCert
= read_cert_from(caCertString
);
924 invalidCert
= read_cert_from(notCompletelyValidCertString
);
926 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
927 ret
= tor_tls_verify(LOG_WARN
, tls
, &k
);
928 tt_int_op(ret
, OP_EQ
, -1);
930 MOCK(try_to_extract_certs_from_tls
, fixed_try_to_extract_certs_from_tls
);
932 fixed_try_to_extract_certs_from_tls_cert_out_result
= cert1
;
933 ret
= tor_tls_verify(LOG_WARN
, tls
, &k
);
934 tt_int_op(ret
, OP_EQ
, -1);
936 fixed_try_to_extract_certs_from_tls_id_cert_out_result
= cert2
;
937 ret
= tor_tls_verify(LOG_WARN
, tls
, &k
);
938 tt_int_op(ret
, OP_EQ
, -1);
940 fixed_try_to_extract_certs_from_tls_cert_out_result
= invalidCert
;
941 fixed_try_to_extract_certs_from_tls_id_cert_out_result
= invalidCert
;
943 ret
= tor_tls_verify(LOG_WARN
, tls
, &k
);
944 tt_int_op(ret
, OP_EQ
, -1);
946 fixed_try_to_extract_certs_from_tls_cert_out_result
= validCert
;
947 fixed_try_to_extract_certs_from_tls_id_cert_out_result
= caCert
;
949 ret
= tor_tls_verify(LOG_WARN
, tls
, &k
);
950 tt_int_op(ret
, OP_EQ
, 0);
954 UNMOCK(try_to_extract_certs_from_tls
);
960 #endif /* !defined(OPENSSL_OPAQUE) */
962 #ifndef OPENSSL_OPAQUE
964 test_tortls_check_lifetime(void *ignored
)
969 X509
*validCert
= read_cert_from(validCertString
);
970 time_t now
= time(NULL
);
972 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
973 ret
= tor_tls_check_lifetime(LOG_WARN
, tls
, time(NULL
), 0, 0);
974 tt_int_op(ret
, OP_EQ
, -1);
976 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
977 tls
->ssl
->session
= tor_malloc_zero(sizeof(SSL_SESSION
));
978 tls
->ssl
->session
->peer
= validCert
;
979 ret
= tor_tls_check_lifetime(LOG_WARN
, tls
, time(NULL
), 0, 0);
980 tt_int_op(ret
, OP_EQ
, 0);
982 ASN1_STRING_free(validCert
->cert_info
->validity
->notBefore
);
983 validCert
->cert_info
->validity
->notBefore
= ASN1_TIME_set(NULL
, now
-10);
984 ASN1_STRING_free(validCert
->cert_info
->validity
->notAfter
);
985 validCert
->cert_info
->validity
->notAfter
= ASN1_TIME_set(NULL
, now
+60);
987 ret
= tor_tls_check_lifetime(LOG_WARN
, tls
, time(NULL
), 0, -1000);
988 tt_int_op(ret
, OP_EQ
, -1);
990 ret
= tor_tls_check_lifetime(LOG_WARN
, tls
, time(NULL
), -1000, 0);
991 tt_int_op(ret
, OP_EQ
, -1);
994 tor_free(tls
->ssl
->session
);
997 X509_free(validCert
);
999 #endif /* !defined(OPENSSL_OPAQUE) */
1001 #ifndef OPENSSL_OPAQUE
1002 static int fixed_ssl_pending_result
= 0;
1005 fixed_ssl_pending(const SSL
*ignored
)
1008 return fixed_ssl_pending_result
;
1012 test_tortls_get_pending_bytes(void *ignored
)
1019 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1020 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1021 method
= tor_malloc_zero(sizeof(SSL_METHOD
));
1022 method
->ssl_pending
= fixed_ssl_pending
;
1023 tls
->ssl
->method
= method
;
1025 fixed_ssl_pending_result
= 42;
1026 ret
= tor_tls_get_pending_bytes(tls
);
1027 tt_int_op(ret
, OP_EQ
, 42);
1034 #endif /* !defined(OPENSSL_OPAQUE) */
1036 #ifndef OPENSSL_OPAQUE
1038 test_tortls_SSL_SESSION_get_master_key(void *ignored
)
1044 out
= tor_malloc_zero(1);
1045 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1046 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1047 tls
->ssl
->session
= tor_malloc_zero(sizeof(SSL_SESSION
));
1048 tls
->ssl
->session
->master_key_length
= 1;
1050 #ifndef HAVE_SSL_SESSION_GET_MASTER_KEY
1051 tls
->ssl
->session
->master_key
[0] = 43;
1052 ret
= SSL_SESSION_get_master_key(tls
->ssl
->session
, out
, 0);
1053 tt_int_op(ret
, OP_EQ
, 1);
1054 tt_int_op(out
[0], OP_EQ
, 0);
1056 ret
= SSL_SESSION_get_master_key(tls
->ssl
->session
, out
, 1);
1057 tt_int_op(ret
, OP_EQ
, 1);
1058 tt_int_op(out
[0], OP_EQ
, 43);
1061 #endif /* !defined(HAVE_SSL_SESSION_GET_MASTER_KEY) */
1062 tor_free(tls
->ssl
->session
);
1067 #endif /* !defined(OPENSSL_OPAQUE) */
1069 #ifndef OPENSSL_OPAQUE
1071 test_tortls_get_tlssecrets(void *ignored
)
1075 uint8_t *secret_out
= tor_malloc_zero(DIGEST256_LEN
);
1077 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1078 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1079 tls
->ssl
->session
= tor_malloc_zero(sizeof(SSL_SESSION
));
1080 tls
->ssl
->session
->master_key_length
= 1;
1081 tls
->ssl
->s3
= tor_malloc_zero(sizeof(SSL3_STATE
));
1083 ret
= tor_tls_get_tlssecrets(tls
, secret_out
);
1084 tt_int_op(ret
, OP_EQ
, 0);
1087 tor_free(secret_out
);
1088 tor_free(tls
->ssl
->s3
);
1089 tor_free(tls
->ssl
->session
);
1093 #endif /* !defined(OPENSSL_OPAQUE) */
1095 #ifndef OPENSSL_OPAQUE
1097 test_tortls_get_buffer_sizes(void *ignored
)
1102 size_t rbuf_c
=-1, rbuf_b
=-1, wbuf_c
=-1, wbuf_b
=-1;
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
));
1108 tls
->ssl
->s3
->rbuf
.buf
= NULL
;
1109 tls
->ssl
->s3
->rbuf
.len
= 1;
1110 tls
->ssl
->s3
->rbuf
.offset
= 0;
1111 tls
->ssl
->s3
->rbuf
.left
= 42;
1113 tls
->ssl
->s3
->wbuf
.buf
= NULL
;
1114 tls
->ssl
->s3
->wbuf
.len
= 2;
1115 tls
->ssl
->s3
->wbuf
.offset
= 0;
1116 tls
->ssl
->s3
->wbuf
.left
= 43;
1118 ret
= tor_tls_get_buffer_sizes(tls
, &rbuf_c
, &rbuf_b
, &wbuf_c
, &wbuf_b
);
1119 #if OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(1,1,0)
1120 tt_int_op(ret
, OP_EQ
, -1);
1122 tt_int_op(ret
, OP_EQ
, 0);
1123 tt_int_op(rbuf_c
, OP_EQ
, 0);
1124 tt_int_op(wbuf_c
, OP_EQ
, 0);
1125 tt_int_op(rbuf_b
, OP_EQ
, 42);
1126 tt_int_op(wbuf_b
, OP_EQ
, 43);
1128 tls
->ssl
->s3
->rbuf
.buf
= tor_malloc_zero(1);
1129 tls
->ssl
->s3
->wbuf
.buf
= tor_malloc_zero(1);
1130 ret
= tor_tls_get_buffer_sizes(tls
, &rbuf_c
, &rbuf_b
, &wbuf_c
, &wbuf_b
);
1131 tt_int_op(ret
, OP_EQ
, 0);
1132 tt_int_op(rbuf_c
, OP_EQ
, 1);
1133 tt_int_op(wbuf_c
, OP_EQ
, 2);
1135 #endif /* OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(1,1,0) */
1138 tor_free(tls
->ssl
->s3
->rbuf
.buf
);
1139 tor_free(tls
->ssl
->s3
->wbuf
.buf
);
1140 tor_free(tls
->ssl
->s3
);
1144 #endif /* !defined(OPENSSL_OPAQUE) */
1146 #ifndef OPENSSL_OPAQUE
1147 typedef struct cert_pkey_st_local
1150 EVP_PKEY
*privatekey
;
1151 const EVP_MD
*digest
;
1154 typedef struct sess_cert_st_local
1156 STACK_OF(X509
) *cert_chain
;
1158 CERT_PKEY_local
*peer_key
;
1159 CERT_PKEY_local peer_pkeys
[8];
1164 test_tortls_try_to_extract_certs_from_tls(void *ignored
)
1168 X509
*cert
= NULL
, *id_cert
= NULL
, *c1
= NULL
, *c2
= NULL
;
1169 SESS_CERT_local
*sess
= NULL
;
1171 c1
= read_cert_from(validCertString
);
1172 c2
= read_cert_from(caCertString
);
1174 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1175 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1176 tls
->ssl
->session
= tor_malloc_zero(sizeof(SSL_SESSION
));
1177 sess
= tor_malloc_zero(sizeof(SESS_CERT_local
));
1178 tls
->ssl
->session
->sess_cert
= (void *)sess
;
1180 try_to_extract_certs_from_tls(LOG_WARN
, tls
, &cert
, &id_cert
);
1182 tt_assert(!id_cert
);
1184 tls
->ssl
->session
->peer
= c1
;
1185 try_to_extract_certs_from_tls(LOG_WARN
, tls
, &cert
, &id_cert
);
1186 tt_assert(cert
== c1
);
1187 tt_assert(!id_cert
);
1188 X509_free(cert
); /* decrease refcnt */
1190 sess
->cert_chain
= sk_X509_new_null();
1191 try_to_extract_certs_from_tls(LOG_WARN
, tls
, &cert
, &id_cert
);
1192 tt_assert(cert
== c1
);
1193 tt_assert(!id_cert
);
1194 X509_free(cert
); /* decrease refcnt */
1196 sk_X509_push(sess
->cert_chain
, c1
);
1197 sk_X509_push(sess
->cert_chain
, c2
);
1199 try_to_extract_certs_from_tls(LOG_WARN
, tls
, &cert
, &id_cert
);
1200 tt_assert(cert
== c1
);
1202 X509_free(cert
); /* decrease refcnt */
1205 sk_X509_free(sess
->cert_chain
);
1207 tor_free(tls
->ssl
->session
);
1213 #endif /* !defined(OPENSSL_OPAQUE) */
1215 #ifndef OPENSSL_OPAQUE
1217 test_tortls_get_peer_cert(void *ignored
)
1220 tor_x509_cert_t
*ret
;
1224 cert
= read_cert_from(validCertString
);
1226 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1227 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1228 tls
->ssl
->session
= tor_malloc_zero(sizeof(SSL_SESSION
));
1230 ret
= tor_tls_get_peer_cert(tls
);
1233 tls
->ssl
->session
->peer
= cert
;
1234 ret
= tor_tls_get_peer_cert(tls
);
1236 tt_assert(ret
->cert
== cert
);
1239 tor_x509_cert_free(ret
);
1240 tor_free(tls
->ssl
->session
);
1245 #endif /* !defined(OPENSSL_OPAQUE) */
1247 #ifndef OPENSSL_OPAQUE
1249 test_tortls_peer_has_cert(void *ignored
)
1256 cert
= read_cert_from(validCertString
);
1258 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1259 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1260 tls
->ssl
->session
= tor_malloc_zero(sizeof(SSL_SESSION
));
1262 ret
= tor_tls_peer_has_cert(tls
);
1265 tls
->ssl
->session
->peer
= cert
;
1266 ret
= tor_tls_peer_has_cert(tls
);
1270 tor_free(tls
->ssl
->session
);
1275 #endif /* !defined(OPENSSL_OPAQUE) */
1278 test_tortls_get_write_overhead_ratio(void *ignored
)
1283 total_bytes_written_over_tls
= 0;
1284 ret
= tls_get_write_overhead_ratio();
1285 tt_double_op(fabs(ret
- 1.0), OP_LT
, 1E-12);
1287 total_bytes_written_by_tls
= 10;
1288 total_bytes_written_over_tls
= 1;
1289 ret
= tls_get_write_overhead_ratio();
1290 tt_double_op(fabs(ret
- 10.0), OP_LT
, 1E-12);
1292 total_bytes_written_by_tls
= 10;
1293 total_bytes_written_over_tls
= 2;
1294 ret
= tls_get_write_overhead_ratio();
1295 tt_double_op(fabs(ret
- 5.0), OP_LT
, 1E-12);
1302 test_tortls_is_server(void *ignored
)
1308 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1310 ret
= tor_tls_is_server(tls
);
1311 tt_int_op(ret
, OP_EQ
, 1);
1317 #ifndef OPENSSL_OPAQUE
1319 test_tortls_session_secret_cb(void *ignored
)
1324 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
1329 tor_tls_allocate_tor_tls_object_ex_data_index();
1331 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1333 tls
->magic
= TOR_TLS_MAGIC
;
1335 ctx
= SSL_CTX_new(TLSv1_method());
1336 tls
->ssl
= SSL_new(ctx
);
1337 SSL_set_ex_data(tls
->ssl
, tor_tls_object_ex_data_index
, tls
);
1339 SSL_set_session_secret_cb(tls
->ssl
, tor_tls_session_secret_cb
, NULL
);
1341 tor_tls_session_secret_cb(tls
->ssl
, NULL
, NULL
, NULL
, NULL
, NULL
);
1342 tt_assert(!tls
->ssl
->tls_session_secret_cb
);
1344 one
= get_cipher_by_name("ECDHE-RSA-AES256-GCM-SHA384");
1346 ciphers
= sk_SSL_CIPHER_new_null();
1347 sk_SSL_CIPHER_push(ciphers
, one
);
1349 tls
->client_cipher_list_type
= 0;
1350 tor_tls_session_secret_cb(tls
->ssl
, NULL
, NULL
, ciphers
, NULL
, NULL
);
1351 tt_assert(!tls
->ssl
->tls_session_secret_cb
);
1354 sk_SSL_CIPHER_free(ciphers
);
1359 #endif /* !defined(OPENSSL_OPAQUE) */
1361 #ifndef OPENSSL_OPAQUE
1362 /* TODO: It seems block_renegotiation and unblock_renegotiation and
1363 * using different blags. This might not be correct */
1365 test_tortls_block_renegotiation(void *ignored
)
1370 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1371 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1372 tls
->ssl
->s3
= tor_malloc_zero(sizeof(SSL3_STATE
));
1373 #ifndef SUPPORT_UNSAFE_RENEGOTIATION_FLAG
1374 #define SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION 0
1377 tls
->ssl
->s3
->flags
= SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
;
1379 tor_tls_block_renegotiation(tls
);
1381 #ifndef OPENSSL_1_1_API
1382 tt_assert(!(tls
->ssl
->s3
->flags
&
1383 SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
));
1387 tor_free(tls
->ssl
->s3
);
1393 test_tortls_unblock_renegotiation(void *ignored
)
1398 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1399 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1400 tor_tls_unblock_renegotiation(tls
);
1402 tt_uint_op(SSL_get_options(tls
->ssl
) &
1403 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
, OP_EQ
,
1404 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
);
1410 #endif /* !defined(OPENSSL_OPAQUE) */
1412 #ifndef OPENSSL_OPAQUE
1414 test_tortls_assert_renegotiation_unblocked(void *ignored
)
1419 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1420 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1421 tor_tls_unblock_renegotiation(tls
);
1422 tor_tls_assert_renegotiation_unblocked(tls
);
1423 /* No assertion here - this test will fail if tor_assert is turned on
1424 * and things are bad. */
1429 #endif /* !defined(OPENSSL_OPAQUE) */
1432 test_tortls_set_logged_address(void *ignored
)
1437 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1439 tor_tls_set_logged_address(tls
, "foo bar");
1441 tt_str_op(tls
->address
, OP_EQ
, "foo bar");
1443 tor_tls_set_logged_address(tls
, "foo bar 2");
1444 tt_str_op(tls
->address
, OP_EQ
, "foo bar 2");
1447 tor_free(tls
->address
);
1451 #ifndef OPENSSL_OPAQUE
1453 example_cb(tor_tls_t
*t
, void *arg
)
1460 test_tortls_set_renegotiate_callback(void *ignored
)
1464 const char *arg
= "hello";
1466 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1467 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1469 tor_tls_set_renegotiate_callback(tls
, example_cb
, (void*)arg
);
1470 tt_assert(tls
->negotiated_callback
== example_cb
);
1471 tt_assert(tls
->callback_arg
== arg
);
1472 tt_assert(!tls
->got_renegotiate
);
1474 /* Assumes V2_HANDSHAKE_SERVER */
1475 tt_assert(tls
->ssl
->info_callback
== tor_tls_server_info_callback
);
1477 tor_tls_set_renegotiate_callback(tls
, NULL
, (void*)arg
);
1478 tt_assert(tls
->ssl
->info_callback
== tor_tls_debug_state_callback
);
1484 #endif /* !defined(OPENSSL_OPAQUE) */
1486 #ifndef OPENSSL_OPAQUE
1487 static SSL_CIPHER
*fixed_cipher1
= NULL
;
1488 static SSL_CIPHER
*fixed_cipher2
= NULL
;
1489 static const SSL_CIPHER
*
1490 fake_get_cipher(unsigned ncipher
)
1495 return fixed_cipher1
;
1497 return fixed_cipher2
;
1502 #endif /* !defined(OPENSSL_OPAQUE) */
1504 #ifndef OPENSSL_OPAQUE
1506 test_tortls_find_cipher_by_id(void *ignored
)
1512 const SSL_METHOD
*m
= TLSv1_method();
1513 SSL_METHOD
*empty_method
= tor_malloc_zero(sizeof(SSL_METHOD
));
1515 fixed_cipher1
= tor_malloc_zero(sizeof(SSL_CIPHER
));
1516 fixed_cipher2
= tor_malloc_zero(sizeof(SSL_CIPHER
));
1517 fixed_cipher2
->id
= 0xC00A;
1521 ctx
= SSL_CTX_new(m
);
1524 ret
= find_cipher_by_id(ssl
, NULL
, 0xC00A);
1525 tt_int_op(ret
, OP_EQ
, 1);
1527 ret
= find_cipher_by_id(ssl
, m
, 0xC00A);
1528 tt_int_op(ret
, OP_EQ
, 1);
1530 ret
= find_cipher_by_id(ssl
, m
, 0xFFFF);
1531 tt_int_op(ret
, OP_EQ
, 0);
1533 ret
= find_cipher_by_id(ssl
, empty_method
, 0xC00A);
1534 tt_int_op(ret
, OP_EQ
, 1);
1536 ret
= find_cipher_by_id(ssl
, empty_method
, 0xFFFF);
1537 #ifdef HAVE_SSL_CIPHER_FIND
1538 tt_int_op(ret
, OP_EQ
, 0);
1540 tt_int_op(ret
, OP_EQ
, 1);
1543 empty_method
->get_cipher
= fake_get_cipher
;
1544 ret
= find_cipher_by_id(ssl
, empty_method
, 0xC00A);
1545 tt_int_op(ret
, OP_EQ
, 1);
1547 empty_method
->get_cipher
= m
->get_cipher
;
1548 empty_method
->num_ciphers
= m
->num_ciphers
;
1549 ret
= find_cipher_by_id(ssl
, empty_method
, 0xC00A);
1550 tt_int_op(ret
, OP_EQ
, 1);
1552 empty_method
->get_cipher
= fake_get_cipher
;
1553 empty_method
->num_ciphers
= m
->num_ciphers
;
1554 ret
= find_cipher_by_id(ssl
, empty_method
, 0xC00A);
1555 tt_int_op(ret
, OP_EQ
, 1);
1557 empty_method
->num_ciphers
= fake_num_ciphers
;
1558 ret
= find_cipher_by_id(ssl
, empty_method
, 0xC00A);
1559 #ifdef HAVE_SSL_CIPHER_FIND
1560 tt_int_op(ret
, OP_EQ
, 1);
1562 tt_int_op(ret
, OP_EQ
, 0);
1566 tor_free(empty_method
);
1569 tor_free(fixed_cipher1
);
1571 #endif /* !defined(OPENSSL_OPAQUE) */
1573 #ifndef OPENSSL_OPAQUE
1575 test_tortls_debug_state_callback(void *ignored
)
1579 char *buf
= tor_malloc_zero(1000);
1582 setup_capture_of_logs(LOG_DEBUG
);
1584 ssl
= tor_malloc_zero(sizeof(SSL
));
1586 tor_tls_debug_state_callback(ssl
, 32, 45);
1588 n
= tor_snprintf(buf
, 1000, "SSL %p is now in state unknown"
1589 " state [type=32,val=45].\n", ssl
);
1590 /* tor's snprintf returns -1 on error */
1591 tt_int_op(n
, OP_NE
, -1);
1592 expect_log_msg(buf
);
1595 teardown_capture_of_logs();
1599 #endif /* !defined(OPENSSL_OPAQUE) */
1601 #ifndef OPENSSL_OPAQUE
1603 test_tortls_server_info_callback(void *ignored
)
1612 ctx
= SSL_CTX_new(TLSv1_method());
1615 tor_tls_allocate_tor_tls_object_ex_data_index();
1617 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1618 tls
->magic
= TOR_TLS_MAGIC
;
1621 setup_full_capture_of_logs(LOG_WARN
);
1622 SSL_set_state(ssl
, SSL3_ST_SW_SRVR_HELLO_A
);
1623 mock_clean_saved_logs();
1624 tor_tls_server_info_callback(ssl
, SSL_CB_ACCEPT_LOOP
, 0);
1625 expect_single_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
1627 SSL_set_state(ssl
, SSL3_ST_SW_SRVR_HELLO_B
);
1628 mock_clean_saved_logs();
1629 tor_tls_server_info_callback(ssl
, SSL_CB_ACCEPT_LOOP
, 0);
1630 expect_single_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
1632 SSL_set_state(ssl
, 99);
1633 mock_clean_saved_logs();
1634 tor_tls_server_info_callback(ssl
, SSL_CB_ACCEPT_LOOP
, 0);
1635 expect_no_log_entry();
1636 teardown_capture_of_logs();
1638 SSL_set_ex_data(tls
->ssl
, tor_tls_object_ex_data_index
, tls
);
1639 SSL_set_state(ssl
, SSL3_ST_SW_SRVR_HELLO_B
);
1640 tls
->negotiated_callback
= 0;
1641 tls
->server_handshake_count
= 120;
1642 tor_tls_server_info_callback(ssl
, SSL_CB_ACCEPT_LOOP
, 0);
1643 tt_int_op(tls
->server_handshake_count
, OP_EQ
, 121);
1645 tls
->server_handshake_count
= 127;
1646 tls
->negotiated_callback
= (void *)1;
1647 tor_tls_server_info_callback(ssl
, SSL_CB_ACCEPT_LOOP
, 0);
1648 tt_int_op(tls
->server_handshake_count
, OP_EQ
, 127);
1649 tt_int_op(tls
->got_renegotiate
, OP_EQ
, 1);
1651 tls
->ssl
->session
= SSL_SESSION_new();
1652 tls
->wasV2Handshake
= 0;
1653 tor_tls_server_info_callback(ssl
, SSL_CB_ACCEPT_LOOP
, 0);
1654 tt_int_op(tls
->wasV2Handshake
, OP_EQ
, 0);
1657 teardown_capture_of_logs();
1662 #endif /* !defined(OPENSSL_OPAQUE) */
1664 #ifndef OPENSSL_OPAQUE
1665 static int fixed_ssl_read_result_index
;
1666 static int fixed_ssl_read_result
[5];
1667 static int fixed_ssl_shutdown_result
;
1670 fixed_ssl_read(SSL
*s
, void *buf
, int len
)
1675 return fixed_ssl_read_result
[fixed_ssl_read_result_index
++];
1679 fixed_ssl_shutdown(SSL
*s
)
1682 return fixed_ssl_shutdown_result
;
1685 #ifndef LIBRESSL_VERSION_NUMBER
1686 static int fixed_ssl_state_to_set
;
1687 static tor_tls_t
*fixed_tls
;
1690 setting_version_ssl_shutdown(SSL
*s
)
1692 s
->version
= SSL2_VERSION
;
1693 return fixed_ssl_shutdown_result
;
1697 setting_version_and_state_ssl_shutdown(SSL
*s
)
1699 fixed_tls
->state
= fixed_ssl_state_to_set
;
1700 s
->version
= SSL2_VERSION
;
1701 return fixed_ssl_shutdown_result
;
1703 #endif /* !defined(LIBRESSL_VERSION_NUMBER) */
1706 dummy_handshake_func(SSL
*s
)
1713 test_tortls_shutdown(void *ignored
)
1718 SSL_METHOD
*method
= give_me_a_test_method();
1719 setup_capture_of_logs(LOG_WARN
);
1721 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1722 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1723 tls
->ssl
->method
= method
;
1724 method
->ssl_read
= fixed_ssl_read
;
1725 method
->ssl_shutdown
= fixed_ssl_shutdown
;
1727 ret
= tor_tls_shutdown(tls
);
1728 tt_int_op(ret
, OP_EQ
, -9);
1730 tls
->state
= TOR_TLS_ST_SENTCLOSE
;
1731 fixed_ssl_read_result_index
= 0;
1732 fixed_ssl_read_result
[0] = 10;
1733 fixed_ssl_read_result
[1] = -1;
1734 ret
= tor_tls_shutdown(tls
);
1735 tt_int_op(ret
, OP_EQ
, -9);
1737 #ifndef LIBRESSL_VERSION_NUMBER
1738 tls
->ssl
->handshake_func
= dummy_handshake_func
;
1740 fixed_ssl_read_result_index
= 0;
1741 fixed_ssl_read_result
[0] = 10;
1742 fixed_ssl_read_result
[1] = 42;
1743 fixed_ssl_read_result
[2] = 0;
1744 fixed_ssl_shutdown_result
= 1;
1746 tls
->ssl
->version
= SSL2_VERSION
;
1747 ret
= tor_tls_shutdown(tls
);
1748 tt_int_op(ret
, OP_EQ
, TOR_TLS_DONE
);
1749 tt_int_op(tls
->state
, OP_EQ
, TOR_TLS_ST_CLOSED
);
1751 fixed_ssl_read_result_index
= 0;
1752 fixed_ssl_read_result
[0] = 10;
1753 fixed_ssl_read_result
[1] = 42;
1754 fixed_ssl_read_result
[2] = 0;
1755 fixed_ssl_shutdown_result
= 0;
1757 tls
->ssl
->version
= 0;
1758 ret
= tor_tls_shutdown(tls
);
1759 tt_int_op(ret
, OP_EQ
, TOR_TLS_DONE
);
1760 tt_int_op(tls
->state
, OP_EQ
, TOR_TLS_ST_CLOSED
);
1762 fixed_ssl_read_result_index
= 0;
1763 fixed_ssl_read_result
[0] = 10;
1764 fixed_ssl_read_result
[1] = 42;
1765 fixed_ssl_read_result
[2] = 0;
1766 fixed_ssl_shutdown_result
= 0;
1768 tls
->ssl
->version
= 0;
1769 method
->ssl_shutdown
= setting_version_ssl_shutdown
;
1770 ret
= tor_tls_shutdown(tls
);
1771 tt_int_op(ret
, OP_EQ
, TOR_TLS_ERROR_MISC
);
1773 fixed_ssl_read_result_index
= 0;
1774 fixed_ssl_read_result
[0] = 10;
1775 fixed_ssl_read_result
[1] = 42;
1776 fixed_ssl_read_result
[2] = 0;
1777 fixed_ssl_shutdown_result
= 0;
1779 fixed_ssl_state_to_set
= TOR_TLS_ST_GOTCLOSE
;
1781 tls
->ssl
->version
= 0;
1782 method
->ssl_shutdown
= setting_version_and_state_ssl_shutdown
;
1783 ret
= tor_tls_shutdown(tls
);
1784 tt_int_op(ret
, OP_EQ
, TOR_TLS_ERROR_MISC
);
1786 fixed_ssl_read_result_index
= 0;
1787 fixed_ssl_read_result
[0] = 10;
1788 fixed_ssl_read_result
[1] = 42;
1789 fixed_ssl_read_result
[2] = 0;
1790 fixed_ssl_read_result
[3] = -1;
1791 fixed_ssl_shutdown_result
= 0;
1793 fixed_ssl_state_to_set
= 0;
1795 tls
->ssl
->version
= 0;
1796 method
->ssl_shutdown
= setting_version_and_state_ssl_shutdown
;
1797 ret
= tor_tls_shutdown(tls
);
1798 tt_int_op(ret
, OP_EQ
, TOR_TLS_ERROR_MISC
);
1799 #endif /* !defined(LIBRESSL_VERSION_NUMBER) */
1802 teardown_capture_of_logs();
1808 static int negotiated_callback_called
;
1811 negotiated_callback_setter(tor_tls_t
*t
, void *arg
)
1815 negotiated_callback_called
++;
1819 test_tortls_read(void *ignored
)
1825 SSL_METHOD
*method
= give_me_a_test_method();
1826 setup_capture_of_logs(LOG_WARN
);
1828 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1829 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1830 tls
->state
= TOR_TLS_ST_OPEN
;
1832 ret
= tor_tls_read(tls
, buf
, 10);
1833 tt_int_op(ret
, OP_EQ
, -9);
1835 /* These tests assume that V2_HANDSHAKE_SERVER is set */
1836 tls
->ssl
->handshake_func
= dummy_handshake_func
;
1837 tls
->ssl
->method
= method
;
1838 method
->ssl_read
= fixed_ssl_read
;
1839 fixed_ssl_read_result_index
= 0;
1840 fixed_ssl_read_result
[0] = 42;
1841 tls
->state
= TOR_TLS_ST_OPEN
;
1843 ret
= tor_tls_read(tls
, buf
, 10);
1844 tt_int_op(ret
, OP_EQ
, 42);
1846 tls
->state
= TOR_TLS_ST_OPEN
;
1847 tls
->got_renegotiate
= 1;
1848 fixed_ssl_read_result_index
= 0;
1850 ret
= tor_tls_read(tls
, buf
, 10);
1851 tt_int_op(tls
->got_renegotiate
, OP_EQ
, 0);
1853 tls
->state
= TOR_TLS_ST_OPEN
;
1854 tls
->got_renegotiate
= 1;
1855 negotiated_callback_called
= 0;
1856 tls
->negotiated_callback
= negotiated_callback_setter
;
1857 fixed_ssl_read_result_index
= 0;
1859 ret
= tor_tls_read(tls
, buf
, 10);
1860 tt_int_op(negotiated_callback_called
, OP_EQ
, 1);
1862 #ifndef LIBRESSL_VERSION_NUMBER
1863 fixed_ssl_read_result_index
= 0;
1864 fixed_ssl_read_result
[0] = 0;
1865 tls
->ssl
->version
= SSL2_VERSION
;
1867 ret
= tor_tls_read(tls
, buf
, 10);
1868 tt_int_op(ret
, OP_EQ
, TOR_TLS_CLOSE
);
1869 tt_int_op(tls
->state
, OP_EQ
, TOR_TLS_ST_CLOSED
);
1870 #endif /* !defined(LIBRESSL_VERSION_NUMBER) */
1874 teardown_capture_of_logs();
1880 static int fixed_ssl_write_result
;
1883 fixed_ssl_write(SSL
*s
, const void *buf
, int len
)
1888 return fixed_ssl_write_result
;
1892 test_tortls_write(void *ignored
)
1897 SSL_METHOD
*method
= give_me_a_test_method();
1899 setup_capture_of_logs(LOG_WARN
);
1901 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1902 tls
->ssl
= tor_malloc_zero(sizeof(SSL
));
1903 tls
->state
= TOR_TLS_ST_OPEN
;
1905 ret
= tor_tls_write(tls
, buf
, 0);
1906 tt_int_op(ret
, OP_EQ
, 0);
1908 ret
= tor_tls_write(tls
, buf
, 10);
1909 tt_int_op(ret
, OP_EQ
, -9);
1911 tls
->ssl
->method
= method
;
1912 tls
->wantwrite_n
= 1;
1913 ret
= tor_tls_write(tls
, buf
, 10);
1914 tt_int_op(tls
->wantwrite_n
, OP_EQ
, 0);
1916 method
->ssl_write
= fixed_ssl_write
;
1917 tls
->ssl
->handshake_func
= dummy_handshake_func
;
1918 fixed_ssl_write_result
= 1;
1920 ret
= tor_tls_write(tls
, buf
, 10);
1921 tt_int_op(ret
, OP_EQ
, 1);
1923 fixed_ssl_write_result
= -1;
1925 tls
->ssl
->rwstate
= SSL_READING
;
1926 SSL_set_bio(tls
->ssl
, BIO_new(BIO_s_mem()), NULL
);
1927 SSL_get_rbio(tls
->ssl
)->flags
= BIO_FLAGS_READ
;
1928 ret
= tor_tls_write(tls
, buf
, 10);
1929 tt_int_op(ret
, OP_EQ
, TOR_TLS_WANTREAD
);
1932 tls
->ssl
->rwstate
= SSL_READING
;
1933 SSL_set_bio(tls
->ssl
, BIO_new(BIO_s_mem()), NULL
);
1934 SSL_get_rbio(tls
->ssl
)->flags
= BIO_FLAGS_WRITE
;
1935 ret
= tor_tls_write(tls
, buf
, 10);
1936 tt_int_op(ret
, OP_EQ
, TOR_TLS_WANTWRITE
);
1939 teardown_capture_of_logs();
1940 BIO_free(tls
->ssl
->rbio
);
1945 #endif /* !defined(OPENSSL_OPAQUE) */
1947 #ifndef OPENSSL_OPAQUE
1948 static int fixed_ssl_accept_result
;
1949 static int fixed_ssl_connect_result
;
1952 setting_error_ssl_accept(SSL
*ssl
)
1955 ERR_put_error(ERR_LIB_BN
, 2, -1, "somewhere.c", 99);
1956 ERR_put_error(ERR_LIB_SYS
, 2, -1, "somewhere.c", 99);
1957 return fixed_ssl_accept_result
;
1961 setting_error_ssl_connect(SSL
*ssl
)
1964 ERR_put_error(ERR_LIB_BN
, 2, -1, "somewhere.c", 99);
1965 ERR_put_error(ERR_LIB_SYS
, 2, -1, "somewhere.c", 99);
1966 return fixed_ssl_connect_result
;
1970 fixed_ssl_accept(SSL
*ssl
)
1973 return fixed_ssl_accept_result
;
1977 test_tortls_handshake(void *ignored
)
1983 SSL_METHOD
*method
= give_me_a_test_method();
1984 setup_capture_of_logs(LOG_INFO
);
1987 SSL_load_error_strings();
1989 ctx
= SSL_CTX_new(TLSv1_method());
1991 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
1992 tls
->ssl
= SSL_new(ctx
);
1993 tls
->state
= TOR_TLS_ST_HANDSHAKE
;
1995 ret
= tor_tls_handshake(tls
);
1996 tt_int_op(ret
, OP_EQ
, -9);
1999 tls
->state
= TOR_TLS_ST_HANDSHAKE
;
2000 ret
= tor_tls_handshake(tls
);
2001 tt_int_op(ret
, OP_EQ
, -9);
2003 tls
->ssl
->method
= method
;
2004 method
->ssl_accept
= fixed_ssl_accept
;
2005 fixed_ssl_accept_result
= 2;
2007 tls
->state
= TOR_TLS_ST_HANDSHAKE
;
2008 ret
= tor_tls_handshake(tls
);
2009 tt_int_op(tls
->state
, OP_EQ
, TOR_TLS_ST_OPEN
);
2011 method
->ssl_accept
= setting_error_ssl_accept
;
2012 fixed_ssl_accept_result
= 1;
2014 mock_clean_saved_logs();
2015 tls
->state
= TOR_TLS_ST_HANDSHAKE
;
2016 ret
= tor_tls_handshake(tls
);
2017 tt_int_op(ret
, OP_EQ
, TOR_TLS_ERROR_MISC
);
2019 /* This fails on jessie. Investigate why! */
2021 expect_log_msg("TLS error while handshaking: (null) (in bignum routines:"
2022 "(null):SSLv3 write client hello B)\n");
2023 expect_log_msg("TLS error while handshaking: (null) (in system library:"
2024 "connect:SSLv3 write client hello B)\n");
2026 expect_log_severity(LOG_INFO
);
2029 method
->ssl_connect
= setting_error_ssl_connect
;
2030 fixed_ssl_connect_result
= 1;
2032 mock_clean_saved_logs();
2033 tls
->state
= TOR_TLS_ST_HANDSHAKE
;
2034 ret
= tor_tls_handshake(tls
);
2035 tt_int_op(ret
, OP_EQ
, TOR_TLS_ERROR_MISC
);
2039 expect_log_msg("TLS error while handshaking: "
2040 "(null) (in bignum routines:(null):SSLv3 write client hello B)\n");
2041 expect_log_msg("TLS error while handshaking: "
2042 "(null) (in system library:connect:SSLv3 write client hello B)\n");
2044 expect_log_severity(LOG_WARN
);
2047 teardown_capture_of_logs();
2053 #endif /* !defined(OPENSSL_OPAQUE) */
2055 #ifndef OPENSSL_OPAQUE
2057 test_tortls_finish_handshake(void *ignored
)
2063 SSL_METHOD
*method
= give_me_a_test_method();
2065 SSL_load_error_strings();
2067 X509
*c1
= read_cert_from(validCertString
);
2068 SESS_CERT_local
*sess
= NULL
;
2070 ctx
= SSL_CTX_new(method
);
2072 tls
= tor_malloc_zero(sizeof(tor_tls_t
));
2073 tls
->ssl
= SSL_new(ctx
);
2074 tls
->state
= TOR_TLS_ST_OPEN
;
2076 ret
= tor_tls_finish_handshake(tls
);
2077 tt_int_op(ret
, OP_EQ
, 0);
2080 tls
->wasV2Handshake
= 0;
2081 setup_full_capture_of_logs(LOG_WARN
);
2082 ret
= tor_tls_finish_handshake(tls
);
2083 tt_int_op(ret
, OP_EQ
, 0);
2084 tt_int_op(tls
->wasV2Handshake
, OP_EQ
, 1);
2085 expect_single_log_msg_containing("For some reason, wasV2Handshake didn't "
2087 teardown_capture_of_logs();
2089 tls
->wasV2Handshake
= 1;
2090 ret
= tor_tls_finish_handshake(tls
);
2091 tt_int_op(ret
, OP_EQ
, 0);
2092 tt_int_op(tls
->wasV2Handshake
, OP_EQ
, 1);
2094 tls
->wasV2Handshake
= 1;
2095 tls
->ssl
->session
= SSL_SESSION_new();
2096 ret
= tor_tls_finish_handshake(tls
);
2097 tt_int_op(ret
, OP_EQ
, 0);
2098 tt_int_op(tls
->wasV2Handshake
, OP_EQ
, 0);
2102 sess
= tor_malloc_zero(sizeof(SESS_CERT_local
));
2103 tls
->ssl
->session
->sess_cert
= (void *)sess
;
2104 sess
->cert_chain
= sk_X509_new_null();
2105 sk_X509_push(sess
->cert_chain
, c1
);
2106 tls
->ssl
->session
->peer
= c1
;
2107 tls
->wasV2Handshake
= 0;
2108 ret
= tor_tls_finish_handshake(tls
);
2109 tt_int_op(ret
, OP_EQ
, 0);
2110 tt_int_op(tls
->wasV2Handshake
, OP_EQ
, 1);
2112 method
->num_ciphers
= fake_num_ciphers
;
2113 ret
= tor_tls_finish_handshake(tls
);
2114 tt_int_op(ret
, OP_EQ
, -9);
2118 sk_X509_free(sess
->cert_chain
);
2119 if (tls
->ssl
&& tls
->ssl
->session
) {
2120 tor_free(tls
->ssl
->session
->sess_cert
);
2126 teardown_capture_of_logs();
2128 #endif /* !defined(OPENSSL_OPAQUE) */
2130 static int fixed_crypto_pk_new_result_index
;
2131 static crypto_pk_t
*fixed_crypto_pk_new_result
[5];
2133 static crypto_pk_t
*
2134 fixed_crypto_pk_new(void)
2136 return fixed_crypto_pk_new_result
[fixed_crypto_pk_new_result_index
++];
2139 #ifndef OPENSSL_OPAQUE
2140 static int fixed_crypto_pk_generate_key_with_bits_result_index
;
2141 static int fixed_crypto_pk_generate_key_with_bits_result
[5];
2142 static int fixed_tor_tls_create_certificate_result_index
;
2143 static X509
*fixed_tor_tls_create_certificate_result
[5];
2144 static int fixed_tor_x509_cert_new_result_index
;
2145 static tor_x509_cert_t
*fixed_tor_x509_cert_new_result
[5];
2148 fixed_crypto_pk_generate_key_with_bits(crypto_pk_t
*env
, int bits
)
2152 return fixed_crypto_pk_generate_key_with_bits_result
[
2153 fixed_crypto_pk_generate_key_with_bits_result_index
++];
2157 fixed_tor_tls_create_certificate(crypto_pk_t
*rsa
,
2158 crypto_pk_t
*rsa_sign
,
2160 const char *cname_sign
,
2161 unsigned int cert_lifetime
)
2167 (void)cert_lifetime
;
2168 return fixed_tor_tls_create_certificate_result
[
2169 fixed_tor_tls_create_certificate_result_index
++];
2172 static tor_x509_cert_t
*
2173 fixed_tor_x509_cert_new(tor_x509_cert_impl_t
*x509_cert
)
2176 return fixed_tor_x509_cert_new_result
[
2177 fixed_tor_x509_cert_new_result_index
++];
2181 test_tortls_context_new(void *ignored
)
2184 tor_tls_context_t
*ret
;
2185 crypto_pk_t
*pk1
, *pk2
, *pk3
, *pk4
, *pk5
, *pk6
, *pk7
, *pk8
, *pk9
, *pk10
,
2186 *pk11
, *pk12
, *pk13
, *pk14
, *pk15
, *pk16
, *pk17
, *pk18
;
2188 pk1
= crypto_pk_new();
2189 pk2
= crypto_pk_new();
2190 pk3
= crypto_pk_new();
2191 pk4
= crypto_pk_new();
2192 pk5
= crypto_pk_new();
2193 pk6
= crypto_pk_new();
2194 pk7
= crypto_pk_new();
2195 pk8
= crypto_pk_new();
2196 pk9
= crypto_pk_new();
2197 pk10
= crypto_pk_new();
2198 pk11
= crypto_pk_new();
2199 pk12
= crypto_pk_new();
2200 pk13
= crypto_pk_new();
2201 pk14
= crypto_pk_new();
2202 pk15
= crypto_pk_new();
2203 pk16
= crypto_pk_new();
2204 pk17
= crypto_pk_new();
2205 pk18
= crypto_pk_new();
2207 fixed_crypto_pk_new_result_index
= 0;
2208 fixed_crypto_pk_new_result
[0] = NULL
;
2209 MOCK(crypto_pk_new
, fixed_crypto_pk_new
);
2210 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
2213 /* note: we already override this in testing_common.c, so we
2214 * run this unit test in a subprocess. */
2215 MOCK(crypto_pk_generate_key_with_bits
,
2216 fixed_crypto_pk_generate_key_with_bits
);
2217 fixed_crypto_pk_new_result_index
= 0;
2218 fixed_crypto_pk_new_result
[0] = pk1
;
2219 fixed_crypto_pk_new_result
[1] = NULL
;
2220 fixed_crypto_pk_generate_key_with_bits_result
[0] = -1;
2221 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
2222 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
2225 fixed_crypto_pk_new_result_index
= 0;
2226 fixed_crypto_pk_new_result
[0] = pk2
;
2227 fixed_crypto_pk_new_result
[1] = NULL
;
2228 fixed_crypto_pk_generate_key_with_bits_result
[0] = 0;
2229 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
2230 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
2233 fixed_crypto_pk_new_result_index
= 0;
2234 fixed_crypto_pk_new_result
[0] = pk3
;
2235 fixed_crypto_pk_new_result
[1] = pk4
;
2236 fixed_crypto_pk_new_result
[2] = NULL
;
2237 fixed_crypto_pk_generate_key_with_bits_result
[0] = 0;
2238 fixed_crypto_pk_generate_key_with_bits_result
[1] = -1;
2239 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
2240 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
2243 MOCK(tor_tls_create_certificate
, fixed_tor_tls_create_certificate
);
2245 fixed_crypto_pk_new_result_index
= 0;
2246 fixed_crypto_pk_new_result
[0] = pk5
;
2247 fixed_crypto_pk_new_result
[1] = pk6
;
2248 fixed_crypto_pk_new_result
[2] = NULL
;
2249 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
2250 fixed_crypto_pk_generate_key_with_bits_result
[1] = 0;
2251 fixed_tor_tls_create_certificate_result_index
= 0;
2252 fixed_tor_tls_create_certificate_result
[0] = NULL
;
2253 fixed_tor_tls_create_certificate_result
[1] = X509_new();
2254 fixed_tor_tls_create_certificate_result
[2] = X509_new();
2255 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
2258 fixed_crypto_pk_new_result_index
= 0;
2259 fixed_crypto_pk_new_result
[0] = pk7
;
2260 fixed_crypto_pk_new_result
[1] = pk8
;
2261 fixed_crypto_pk_new_result
[2] = NULL
;
2262 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
2263 fixed_tor_tls_create_certificate_result_index
= 0;
2264 fixed_tor_tls_create_certificate_result
[0] = X509_new();
2265 fixed_tor_tls_create_certificate_result
[1] = NULL
;
2266 fixed_tor_tls_create_certificate_result
[2] = X509_new();
2267 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
2270 fixed_crypto_pk_new_result_index
= 0;
2271 fixed_crypto_pk_new_result
[0] = pk9
;
2272 fixed_crypto_pk_new_result
[1] = pk10
;
2273 fixed_crypto_pk_new_result
[2] = NULL
;
2274 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
2275 fixed_tor_tls_create_certificate_result_index
= 0;
2276 fixed_tor_tls_create_certificate_result
[0] = X509_new();
2277 fixed_tor_tls_create_certificate_result
[1] = X509_new();
2278 fixed_tor_tls_create_certificate_result
[2] = NULL
;
2279 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
2282 MOCK(tor_x509_cert_new
, fixed_tor_x509_cert_new
);
2283 fixed_crypto_pk_new_result_index
= 0;
2284 fixed_crypto_pk_new_result
[0] = pk11
;
2285 fixed_crypto_pk_new_result
[1] = pk12
;
2286 fixed_crypto_pk_new_result
[2] = NULL
;
2287 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
2288 fixed_tor_tls_create_certificate_result_index
= 0;
2289 fixed_tor_tls_create_certificate_result
[0] = X509_new();
2290 fixed_tor_tls_create_certificate_result
[1] = X509_new();
2291 fixed_tor_tls_create_certificate_result
[2] = X509_new();
2292 fixed_tor_x509_cert_new_result_index
= 0;
2293 fixed_tor_x509_cert_new_result
[0] = NULL
;
2294 fixed_tor_x509_cert_new_result
[1] = NULL
;
2295 fixed_tor_x509_cert_new_result
[2] = NULL
;
2296 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
2299 fixed_crypto_pk_new_result_index
= 0;
2300 fixed_crypto_pk_new_result
[0] = pk13
;
2301 fixed_crypto_pk_new_result
[1] = pk14
;
2302 fixed_crypto_pk_new_result
[2] = NULL
;
2303 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
2304 fixed_tor_tls_create_certificate_result_index
= 0;
2305 fixed_tor_tls_create_certificate_result
[0] = X509_new();
2306 fixed_tor_tls_create_certificate_result
[1] = X509_new();
2307 fixed_tor_tls_create_certificate_result
[2] = X509_new();
2308 fixed_tor_x509_cert_new_result_index
= 0;
2309 fixed_tor_x509_cert_new_result
[0] = tor_malloc_zero(sizeof(tor_x509_cert_t
));
2310 fixed_tor_x509_cert_new_result
[1] = NULL
;
2311 fixed_tor_x509_cert_new_result
[2] = NULL
;
2312 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
2315 fixed_crypto_pk_new_result_index
= 0;
2316 fixed_crypto_pk_new_result
[0] = pk15
;
2317 fixed_crypto_pk_new_result
[1] = pk16
;
2318 fixed_crypto_pk_new_result
[2] = NULL
;
2319 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
2320 fixed_tor_tls_create_certificate_result_index
= 0;
2321 fixed_tor_tls_create_certificate_result
[0] = X509_new();
2322 fixed_tor_tls_create_certificate_result
[1] = X509_new();
2323 fixed_tor_tls_create_certificate_result
[2] = X509_new();
2324 fixed_tor_x509_cert_new_result_index
= 0;
2325 fixed_tor_x509_cert_new_result
[0] = tor_malloc_zero(sizeof(tor_x509_cert_t
));
2326 fixed_tor_x509_cert_new_result
[1] = tor_malloc_zero(sizeof(tor_x509_cert_t
));
2327 fixed_tor_x509_cert_new_result
[2] = NULL
;
2328 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
2331 fixed_crypto_pk_new_result_index
= 0;
2332 fixed_crypto_pk_new_result
[0] = pk17
;
2333 fixed_crypto_pk_new_result
[1] = pk18
;
2334 fixed_crypto_pk_new_result
[2] = NULL
;
2335 fixed_crypto_pk_generate_key_with_bits_result_index
= 0;
2336 fixed_tor_tls_create_certificate_result_index
= 0;
2337 fixed_tor_tls_create_certificate_result
[0] = X509_new();
2338 fixed_tor_tls_create_certificate_result
[1] = X509_new();
2339 fixed_tor_tls_create_certificate_result
[2] = X509_new();
2340 fixed_tor_x509_cert_new_result_index
= 0;
2341 fixed_tor_x509_cert_new_result
[0] = tor_malloc_zero(sizeof(tor_x509_cert_t
));
2342 fixed_tor_x509_cert_new_result
[1] = tor_malloc_zero(sizeof(tor_x509_cert_t
));
2343 fixed_tor_x509_cert_new_result
[2] = tor_malloc_zero(sizeof(tor_x509_cert_t
));
2344 ret
= tor_tls_context_new(NULL
, 0, 0, 0);
2348 UNMOCK(tor_x509_cert_new
);
2349 UNMOCK(tor_tls_create_certificate
);
2350 UNMOCK(crypto_pk_generate_key_with_bits
);
2351 UNMOCK(crypto_pk_new
);
2353 #endif /* !defined(OPENSSL_OPAQUE) */
2355 static int fixed_crypto_pk_get_evp_pkey_result_index
= 0;
2356 static EVP_PKEY
*fixed_crypto_pk_get_evp_pkey_result
[5];
2359 fixed_crypto_pk_get_evp_pkey_(crypto_pk_t
*env
, int private)
2363 return fixed_crypto_pk_get_evp_pkey_result
[
2364 fixed_crypto_pk_get_evp_pkey_result_index
++];
2368 test_tortls_create_certificate(void *ignored
)
2372 crypto_pk_t
*pk1
, *pk2
;
2374 pk1
= crypto_pk_new();
2375 pk2
= crypto_pk_new();
2377 MOCK(crypto_pk_get_openssl_evp_pkey_
, fixed_crypto_pk_get_evp_pkey_
);
2378 fixed_crypto_pk_get_evp_pkey_result_index
= 0;
2379 fixed_crypto_pk_get_evp_pkey_result
[0] = NULL
;
2380 ret
= tor_tls_create_certificate(pk1
, pk2
, "hello", "hello2", 1);
2383 fixed_crypto_pk_get_evp_pkey_result_index
= 0;
2384 fixed_crypto_pk_get_evp_pkey_result
[0] = EVP_PKEY_new();
2385 fixed_crypto_pk_get_evp_pkey_result
[1] = NULL
;
2386 ret
= tor_tls_create_certificate(pk1
, pk2
, "hello", "hello2", 1);
2389 fixed_crypto_pk_get_evp_pkey_result_index
= 0;
2390 fixed_crypto_pk_get_evp_pkey_result
[0] = EVP_PKEY_new();
2391 fixed_crypto_pk_get_evp_pkey_result
[1] = EVP_PKEY_new();
2392 ret
= tor_tls_create_certificate(pk1
, pk2
, "hello", "hello2", 1);
2396 UNMOCK(crypto_pk_get_openssl_evp_pkey_
);
2397 crypto_pk_free(pk1
);
2398 crypto_pk_free(pk2
);
2402 test_tortls_cert_new(void *ignored
)
2405 tor_x509_cert_t
*ret
;
2406 X509
*cert
= read_cert_from(validCertString
);
2408 ret
= tor_x509_cert_new(NULL
);
2411 ret
= tor_x509_cert_new(cert
);
2413 tor_x509_cert_free(ret
);
2417 cert
= read_cert_from(validCertString
);
2418 /* XXX this doesn't do what you think: it alters a copy of the pubkey. */
2419 X509_get_pubkey(cert
)->type
= EVP_PKEY_DSA
;
2420 ret
= tor_x509_cert_new(cert
);
2424 #ifndef OPENSSL_OPAQUE
2425 cert
= read_cert_from(validCertString
);
2426 X509_CINF_free(cert
->cert_info
);
2427 cert
->cert_info
= NULL
;
2428 ret
= tor_x509_cert_new(cert
);
2430 #endif /* !defined(OPENSSL_OPAQUE) */
2433 tor_x509_cert_free(ret
);
2437 test_tortls_cert_is_valid(void *ignored
)
2441 tor_x509_cert_t
*cert
= NULL
, *scert
= NULL
;
2443 scert
= tor_malloc_zero(sizeof(tor_x509_cert_t
));
2444 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 0);
2445 tt_int_op(ret
, OP_EQ
, 0);
2447 cert
= tor_malloc_zero(sizeof(tor_x509_cert_t
));
2448 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 0);
2449 tt_int_op(ret
, OP_EQ
, 0);
2453 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2454 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2455 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 0);
2456 tt_int_op(ret
, OP_EQ
, 1);
2458 #ifndef OPENSSL_OPAQUE
2459 tor_x509_cert_free(cert
);
2460 tor_x509_cert_free(scert
);
2461 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2462 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2463 ASN1_TIME_free(cert
->cert
->cert_info
->validity
->notAfter
);
2464 cert
->cert
->cert_info
->validity
->notAfter
=
2465 ASN1_TIME_set(NULL
, time(NULL
)-1000000);
2466 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 0);
2467 tt_int_op(ret
, OP_EQ
, 0);
2469 tor_x509_cert_free(cert
);
2470 tor_x509_cert_free(scert
);
2471 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2472 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2473 X509_PUBKEY_free(cert
->cert
->cert_info
->key
);
2474 cert
->cert
->cert_info
->key
= NULL
;
2475 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 1);
2476 tt_int_op(ret
, OP_EQ
, 0);
2477 #endif /* !defined(OPENSSL_OPAQUE) */
2480 tor_x509_cert_free(cert
);
2481 tor_x509_cert_free(scert
);
2482 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2483 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2484 /* This doesn't actually change the key in the cert. XXXXXX */
2485 BN_one(EVP_PKEY_get1_RSA(X509_get_pubkey(cert
->cert
))->n
);
2486 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 1);
2487 tt_int_op(ret
, OP_EQ
, 0);
2489 tor_x509_cert_free(cert
);
2490 tor_x509_cert_free(scert
);
2491 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2492 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2493 /* This doesn't actually change the key in the cert. XXXXXX */
2494 X509_get_pubkey(cert
->cert
)->type
= EVP_PKEY_EC
;
2495 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 1);
2496 tt_int_op(ret
, OP_EQ
, 0);
2498 tor_x509_cert_free(cert
);
2499 tor_x509_cert_free(scert
);
2500 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2501 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2502 /* This doesn't actually change the key in the cert. XXXXXX */
2503 X509_get_pubkey(cert
->cert
)->type
= EVP_PKEY_EC
;
2504 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 0);
2505 tt_int_op(ret
, OP_EQ
, 1);
2507 tor_x509_cert_free(cert
);
2508 tor_x509_cert_free(scert
);
2509 cert
= tor_x509_cert_new(read_cert_from(validCertString
));
2510 scert
= tor_x509_cert_new(read_cert_from(caCertString
));
2511 /* This doesn't actually change the key in the cert. XXXXXX */
2512 X509_get_pubkey(cert
->cert
)->type
= EVP_PKEY_EC
;
2513 X509_get_pubkey(cert
->cert
)->ameth
= NULL
;
2514 ret
= tor_tls_cert_is_valid(LOG_WARN
, cert
, scert
, time(NULL
), 0);
2515 tt_int_op(ret
, OP_EQ
, 0);
2519 tor_x509_cert_free(cert
);
2520 tor_x509_cert_free(scert
);
2524 test_tortls_context_init_one(void *ignored
)
2528 tor_tls_context_t
*old
= NULL
;
2530 MOCK(crypto_pk_new
, fixed_crypto_pk_new
);
2532 fixed_crypto_pk_new_result_index
= 0;
2533 fixed_crypto_pk_new_result
[0] = NULL
;
2534 ret
= tor_tls_context_init_one(&old
, NULL
, 0, 0, 0);
2535 tt_int_op(ret
, OP_EQ
, -1);
2538 UNMOCK(crypto_pk_new
);
2541 #define LOCAL_TEST_CASE(name, flags) \
2542 { #name, test_tortls_##name, (flags|TT_FORK), NULL, NULL }
2544 #ifdef OPENSSL_OPAQUE
2545 #define INTRUSIVE_TEST_CASE(name, flags) \
2546 { #name, NULL, TT_SKIP, NULL, NULL }
2548 #define INTRUSIVE_TEST_CASE(name, flags) LOCAL_TEST_CASE(name, flags)
2549 #endif /* defined(OPENSSL_OPAQUE) */
2551 struct testcase_t tortls_openssl_tests
[] = {
2552 LOCAL_TEST_CASE(tor_tls_new
, TT_FORK
),
2553 LOCAL_TEST_CASE(get_state_description
, TT_FORK
),
2554 LOCAL_TEST_CASE(get_by_ssl
, TT_FORK
),
2555 LOCAL_TEST_CASE(allocate_tor_tls_object_ex_data_index
, TT_FORK
),
2556 LOCAL_TEST_CASE(log_one_error
, TT_FORK
),
2557 INTRUSIVE_TEST_CASE(get_error
, TT_FORK
),
2558 LOCAL_TEST_CASE(always_accept_verify_cb
, 0),
2559 INTRUSIVE_TEST_CASE(x509_cert_free
, 0),
2560 INTRUSIVE_TEST_CASE(cert_matches_key
, 0),
2561 INTRUSIVE_TEST_CASE(cert_get_key
, 0),
2562 LOCAL_TEST_CASE(get_my_client_auth_key
, TT_FORK
),
2563 INTRUSIVE_TEST_CASE(get_ciphersuite_name
, 0),
2564 INTRUSIVE_TEST_CASE(classify_client_ciphers
, 0),
2565 LOCAL_TEST_CASE(client_is_using_v2_ciphers
, 0),
2566 INTRUSIVE_TEST_CASE(verify
, 0),
2567 INTRUSIVE_TEST_CASE(check_lifetime
, 0),
2568 INTRUSIVE_TEST_CASE(get_pending_bytes
, 0),
2569 INTRUSIVE_TEST_CASE(SSL_SESSION_get_master_key
, 0),
2570 INTRUSIVE_TEST_CASE(get_tlssecrets
, 0),
2571 INTRUSIVE_TEST_CASE(get_buffer_sizes
, 0),
2572 INTRUSIVE_TEST_CASE(try_to_extract_certs_from_tls
, 0),
2573 INTRUSIVE_TEST_CASE(get_peer_cert
, 0),
2574 INTRUSIVE_TEST_CASE(peer_has_cert
, 0),
2575 INTRUSIVE_TEST_CASE(shutdown
, 0),
2576 INTRUSIVE_TEST_CASE(finish_handshake
, 0),
2577 INTRUSIVE_TEST_CASE(handshake
, 0),
2578 INTRUSIVE_TEST_CASE(write
, 0),
2579 INTRUSIVE_TEST_CASE(read
, 0),
2580 INTRUSIVE_TEST_CASE(server_info_callback
, 0),
2581 LOCAL_TEST_CASE(get_write_overhead_ratio
, TT_FORK
),
2582 LOCAL_TEST_CASE(is_server
, 0),
2583 INTRUSIVE_TEST_CASE(assert_renegotiation_unblocked
, 0),
2584 INTRUSIVE_TEST_CASE(block_renegotiation
, 0),
2585 INTRUSIVE_TEST_CASE(unblock_renegotiation
, 0),
2586 INTRUSIVE_TEST_CASE(set_renegotiate_callback
, 0),
2587 LOCAL_TEST_CASE(set_logged_address
, 0),
2588 INTRUSIVE_TEST_CASE(find_cipher_by_id
, 0),
2589 INTRUSIVE_TEST_CASE(session_secret_cb
, 0),
2590 INTRUSIVE_TEST_CASE(debug_state_callback
, 0),
2591 INTRUSIVE_TEST_CASE(context_new
, TT_FORK
/* redundant */),
2592 LOCAL_TEST_CASE(create_certificate
, 0),
2593 LOCAL_TEST_CASE(cert_new
, 0),
2594 LOCAL_TEST_CASE(cert_is_valid
, 0),
2595 LOCAL_TEST_CASE(context_init_one
, 0),