tor/src/test/test_tortls.c

349 lines
8.5 KiB
C
Raw Normal View History

2018-06-20 14:13:28 +02:00
/* Copyright (c) 2010-2018, The Tor Project, Inc. */
2015-09-15 17:09:18 +02:00
/* See LICENSE for licensing information */
#define TORTLS_PRIVATE
#define TOR_X509_PRIVATE
2015-09-15 17:09:18 +02:00
#define LOG_PRIVATE
#include "orconfig.h"
2015-10-02 18:31:43 +02:00
#ifdef _WIN32
#include <winsock2.h>
#endif
#include <math.h>
#include <stddef.h>
2015-10-02 18:31:43 +02:00
2018-06-29 17:42:52 +02:00
#include "lib/cc/compat_compiler.h"
#include "core/or/or.h"
#include "lib/log/log.h"
#include "app/config/config.h"
#include "lib/crypt_ops/compat_openssl.h"
#include "lib/tls/x509.h"
2018-08-13 00:01:14 +02:00
#include "lib/tls/x509_internal.h"
2018-06-21 18:47:11 +02:00
#include "lib/tls/tortls.h"
#include "lib/tls/tortls_st.h"
#include "lib/tls/tortls_internal.h"
#include "app/config/or_state_st.h"
2018-06-20 15:35:05 +02:00
#include "test/test.h"
#include "test/log_test_helpers.h"
#include "tinytest.h"
2015-09-15 17:09:18 +02:00
static void
test_tortls_errno_to_tls_error(void *data)
{
(void) data;
2015-10-02 14:33:54 +02:00
tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ECONNRESET)),OP_EQ,
TOR_TLS_ERROR_CONNRESET);
tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ETIMEDOUT)),OP_EQ,
TOR_TLS_ERROR_TIMEOUT);
tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(EHOSTUNREACH)),OP_EQ,
TOR_TLS_ERROR_NO_ROUTE);
tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ENETUNREACH)),OP_EQ,
TOR_TLS_ERROR_NO_ROUTE);
tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ECONNREFUSED)),OP_EQ,
TOR_TLS_ERROR_CONNREFUSED);
tt_int_op(tor_errno_to_tls_error(0),OP_EQ,TOR_TLS_ERROR_MISC);
2015-09-15 17:09:18 +02:00
done:
(void)1;
}
static void
test_tortls_err_to_string(void *data)
{
(void) data;
2015-10-02 14:33:54 +02:00
tt_str_op(tor_tls_err_to_string(1),OP_EQ,"[Not an error.]");
tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_MISC),OP_EQ,"misc error");
tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_IO),OP_EQ,"unexpected close");
tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_CONNREFUSED),OP_EQ,
"connection refused");
tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_CONNRESET),OP_EQ,
"connection reset");
tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_NO_ROUTE),OP_EQ,
"host unreachable");
tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_TIMEOUT),OP_EQ,
"connection timed out");
tt_str_op(tor_tls_err_to_string(TOR_TLS_CLOSE),OP_EQ,"closed");
tt_str_op(tor_tls_err_to_string(TOR_TLS_WANTREAD),OP_EQ,"want to read");
tt_str_op(tor_tls_err_to_string(TOR_TLS_WANTWRITE),OP_EQ,"want to write");
tt_str_op(tor_tls_err_to_string(-100),OP_EQ,"(unknown error code)");
2015-09-15 17:09:18 +02:00
done:
(void)1;
}
#ifdef ENABLE_OPENSSL
2015-09-15 17:09:18 +02:00
static int
mock_tls_cert_matches_key(const tor_tls_t *tls, const tor_x509_cert_t *cert)
{
(void) tls;
(void) cert; // XXXX look at this.
return 1;
}
static void
test_tortls_tor_tls_get_error(void *data)
{
(void) data;
2015-10-02 14:33:54 +02:00
MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
crypto_pk_t *key1 = NULL, *key2 = NULL;
key1 = pk_generate(2);
key2 = pk_generate(3);
2015-10-21 18:41:00 +02:00
tor_tls_t *tls = NULL;
2015-10-02 14:33:54 +02:00
tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
key1, key2, 86400), OP_EQ, 0);
tls = tor_tls_new(-1, 0);
setup_capture_of_logs(LOG_WARN);
2015-10-02 14:33:54 +02:00
tor_tls_get_error(tls, 0, 0,
(const char *)"in unit test", LOG_WARN, LD_GENERAL);
expect_single_log_msg_containing("unexpected close while in unit test");
2015-09-15 17:09:18 +02:00
done:
UNMOCK(tor_tls_cert_matches_key);
NS_UNMOCK(logv);
crypto_pk_free(key1);
crypto_pk_free(key2);
2015-10-21 18:41:00 +02:00
tor_tls_free(tls);
2015-09-15 17:09:18 +02:00
}
#endif
2015-09-15 17:09:18 +02:00
static void
test_tortls_x509_cert_get_id_digests(void *ignored)
2015-09-15 17:09:18 +02:00
{
(void)ignored;
tor_x509_cert_t *cert;
common_digests_t *d;
const common_digests_t *res;
cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
d = tor_malloc_zero(sizeof(common_digests_t));
d->d[0][0] = 42;
2015-09-15 17:09:18 +02:00
res = tor_x509_cert_get_id_digests(cert);
tt_assert(!res);
2015-09-15 17:09:18 +02:00
cert->pkey_digests_set = 1;
cert->pkey_digests = *d;
res = tor_x509_cert_get_id_digests(cert);
tt_int_op(res->d[0][0], OP_EQ, 42);
2015-09-15 17:09:18 +02:00
done:
tor_free(cert);
tor_free(d);
2015-09-15 17:09:18 +02:00
}
static void
test_tortls_get_my_certs(void *ignored)
2015-09-15 17:09:18 +02:00
{
(void)ignored;
int ret;
tor_tls_context_t *ctx;
const tor_x509_cert_t *link_cert_out = NULL;
const tor_x509_cert_t *id_cert_out = NULL;
2015-09-15 17:09:18 +02:00
ctx = tor_malloc_zero(sizeof(tor_tls_context_t));
2015-09-15 17:09:18 +02:00
client_tls_context = NULL;
ret = tor_tls_get_my_certs(0, NULL, NULL);
tt_int_op(ret, OP_EQ, -1);
2015-09-15 17:09:18 +02:00
server_tls_context = NULL;
ret = tor_tls_get_my_certs(1, NULL, NULL);
tt_int_op(ret, OP_EQ, -1);
2015-09-15 17:09:18 +02:00
client_tls_context = ctx;
ret = tor_tls_get_my_certs(0, NULL, NULL);
tt_int_op(ret, OP_EQ, 0);
2015-09-15 17:09:18 +02:00
client_tls_context = ctx;
ret = tor_tls_get_my_certs(0, &link_cert_out, &id_cert_out);
tt_int_op(ret, OP_EQ, 0);
2015-09-15 17:09:18 +02:00
server_tls_context = ctx;
ret = tor_tls_get_my_certs(1, &link_cert_out, &id_cert_out);
tt_int_op(ret, OP_EQ, 0);
2015-09-15 17:09:18 +02:00
done:
(void)1;
2015-09-15 17:09:18 +02:00
}
#ifdef ENABLE_OPENSSL
2015-09-15 17:09:18 +02:00
static void
test_tortls_get_forced_write_size(void *ignored)
2015-09-15 17:09:18 +02:00
{
(void)ignored;
long ret;
2015-09-15 17:09:18 +02:00
tor_tls_t *tls;
tls = tor_malloc_zero(sizeof(tor_tls_t));
tls->wantwrite_n = 43;
ret = tor_tls_get_forced_write_size(tls);
tt_int_op(ret, OP_EQ, 43);
2015-09-15 17:09:18 +02:00
done:
tor_free(tls);
}
static void
test_tortls_used_v1_handshake(void *ignored)
2015-09-15 17:09:18 +02:00
{
(void)ignored;
int ret;
tor_tls_t *tls;
2015-09-15 17:09:18 +02:00
tls = tor_malloc_zero(sizeof(tor_tls_t));
// These tests assume both V2 handshake server and client are enabled
tls->wasV2Handshake = 0;
ret = tor_tls_used_v1_handshake(tls);
tt_int_op(ret, OP_EQ, 1);
2015-09-15 17:09:18 +02:00
tls->wasV2Handshake = 1;
ret = tor_tls_used_v1_handshake(tls);
2015-09-15 17:09:18 +02:00
tt_int_op(ret, OP_EQ, 0);
done:
tor_free(tls);
}
static void
test_tortls_server_got_renegotiate(void *ignored)
2015-09-15 17:09:18 +02:00
{
(void)ignored;
int ret;
2015-09-15 17:09:18 +02:00
tor_tls_t *tls;
tls = tor_malloc_zero(sizeof(tor_tls_t));
tls->got_renegotiate = 1;
ret = tor_tls_server_got_renegotiate(tls);
tt_int_op(ret, OP_EQ, 1);
2015-09-15 17:09:18 +02:00
done:
tor_free(tls);
}
#endif
2015-09-15 17:09:18 +02:00
static void
test_tortls_evaluate_ecgroup_for_tls(void *ignored)
2015-09-15 17:09:18 +02:00
{
(void)ignored;
int ret;
2015-09-15 17:09:18 +02:00
ret = evaluate_ecgroup_for_tls(NULL);
tt_int_op(ret, OP_EQ, 1);
2015-09-15 17:09:18 +02:00
ret = evaluate_ecgroup_for_tls("foobar");
tt_int_op(ret, OP_EQ, 0);
2015-09-15 17:09:18 +02:00
ret = evaluate_ecgroup_for_tls("P256");
tt_int_op(ret, OP_EQ, 1);
2015-09-15 17:09:18 +02:00
ret = evaluate_ecgroup_for_tls("P224");
// tt_int_op(ret, OP_EQ, 1); This varies between machines
tt_assert(ret == 0 || ret == 1);
2015-09-15 17:09:18 +02:00
done:
(void)0;
2015-09-15 17:09:18 +02:00
}
2018-08-24 00:57:26 +02:00
static void
test_tortls_double_init(void *arg)
{
(void) arg;
/* If we call tor_tls_context_init() a second time, nothing should go
* wrong.
*/
crypto_pk_t *pk1 = NULL, *pk2 = NULL;
pk1 = pk_generate(2);
pk2 = pk_generate(0);
int r = tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
pk1, pk2, 86400);
tt_int_op(r, OP_EQ, 0);
r = tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
pk2, pk1, 86400);
tt_int_op(r, OP_EQ, 0);
done:
crypto_pk_free(pk1);
crypto_pk_free(pk2);
}
static void
test_tortls_address(void *arg)
{
(void)arg;
tor_tls_t *tls = NULL;
crypto_pk_t *pk1=NULL, *pk2=NULL;
pk1 = pk_generate(2);
pk2 = pk_generate(0);
int r = tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
pk1, pk2, 86400);
tt_int_op(r, OP_EQ, 0);
tls = tor_tls_new(-1, 0);
tls->state = TOR_TLS_ST_OPEN;
tor_tls_set_logged_address(tls, "zombo.com");
/* This write should fail, since the fd is -1. */
setup_capture_of_logs(LOG_INFO);
int n = tor_tls_write(tls, "welcome", 7);
tt_int_op(n, OP_LT, 0);
expect_log_msg_containing("with zombo.com");
done:
teardown_capture_of_logs();
tor_tls_free(tls);
crypto_pk_free(pk1);
crypto_pk_free(pk2);
}
static void
test_tortls_is_server(void *arg)
{
(void)arg;
crypto_pk_t *pk1=NULL, *pk2=NULL;
tor_tls_t *tls1=NULL, *tls2=NULL;
pk1 = pk_generate(2);
pk2 = pk_generate(0);
int r = tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
pk1, pk2, 86400);
tt_int_op(r, OP_EQ, 0);
tls1 = tor_tls_new(-1, 0);
tls2 = tor_tls_new(-1, 1);
tt_assert(! tor_tls_is_server(tls1));
tt_assert(tor_tls_is_server(tls2));
done:
tor_tls_free(tls1);
tor_tls_free(tls2);
crypto_pk_free(pk1);
crypto_pk_free(pk2);
}
#define LOCAL_TEST_CASE(name, flags) \
2015-10-21 18:41:00 +02:00
{ #name, test_tortls_##name, (flags|TT_FORK), NULL, NULL }
2015-09-15 17:09:18 +02:00
struct testcase_t tortls_tests[] = {
LOCAL_TEST_CASE(errno_to_tls_error, 0),
LOCAL_TEST_CASE(err_to_string, 0),
LOCAL_TEST_CASE(x509_cert_get_id_digests, 0),
LOCAL_TEST_CASE(get_my_certs, TT_FORK),
#ifdef ENABLE_OPENSSL
LOCAL_TEST_CASE(tor_tls_get_error, 0),
2015-09-15 17:09:18 +02:00
LOCAL_TEST_CASE(get_forced_write_size, 0),
LOCAL_TEST_CASE(used_v1_handshake, TT_FORK),
LOCAL_TEST_CASE(server_got_renegotiate, 0),
#endif
2015-09-15 17:09:18 +02:00
LOCAL_TEST_CASE(evaluate_ecgroup_for_tls, 0),
2018-08-24 00:57:26 +02:00
LOCAL_TEST_CASE(double_init, TT_FORK),
LOCAL_TEST_CASE(address, TT_FORK),
LOCAL_TEST_CASE(is_server, 0),
2015-09-15 17:09:18 +02:00
END_OF_TESTCASES
};