2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2003, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2019-01-16 18:33:22 +01:00
|
|
|
* Copyright (c) 2007-2019, The Tor Project, Inc. */
|
2003-09-04 18:05:08 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2012-10-12 18:13:10 +02:00
|
|
|
#ifndef TOR_TORTLS_H
|
|
|
|
#define TOR_TORTLS_H
|
2003-09-04 18:05:08 +02:00
|
|
|
|
2004-05-10 09:54:13 +02:00
|
|
|
/**
|
|
|
|
* \file tortls.h
|
|
|
|
* \brief Headers for tortls.c
|
|
|
|
**/
|
|
|
|
|
2018-06-21 18:47:11 +02:00
|
|
|
#include "lib/crypt_ops/crypto_rsa.h"
|
2018-06-21 19:12:23 +02:00
|
|
|
#include "lib/testsupport/testsupport.h"
|
2018-08-17 17:24:50 +02:00
|
|
|
#include "lib/net/nettypes.h"
|
2003-09-04 18:05:08 +02:00
|
|
|
|
2004-05-01 22:46:28 +02:00
|
|
|
/* Opaque structure to hold a TLS connection. */
|
2005-10-06 06:33:40 +02:00
|
|
|
typedef struct tor_tls_t tor_tls_t;
|
2003-09-04 18:05:08 +02:00
|
|
|
|
2018-08-17 17:24:50 +02:00
|
|
|
#ifdef TORTLS_PRIVATE
|
|
|
|
#ifdef ENABLE_OPENSSL
|
|
|
|
struct ssl_st;
|
|
|
|
struct ssl_ctx_st;
|
|
|
|
struct ssl_session_st;
|
|
|
|
typedef struct ssl_ctx_st tor_tls_context_impl_t;
|
|
|
|
typedef struct ssl_st tor_tls_impl_t;
|
2019-06-05 15:33:35 +02:00
|
|
|
#else /* !(defined(ENABLE_OPENSSL)) */
|
2018-08-17 17:24:50 +02:00
|
|
|
struct PRFileDesc;
|
|
|
|
typedef struct PRFileDesc tor_tls_context_impl_t;
|
|
|
|
typedef struct PRFileDesc tor_tls_impl_t;
|
2019-06-05 15:33:35 +02:00
|
|
|
#endif /* defined(ENABLE_OPENSSL) */
|
|
|
|
#endif /* defined(TORTLS_PRIVATE) */
|
2018-08-17 17:24:50 +02:00
|
|
|
|
2018-08-12 00:16:04 +02:00
|
|
|
struct tor_x509_cert_t;
|
2011-09-13 17:37:15 +02:00
|
|
|
|
2004-05-01 22:46:28 +02:00
|
|
|
/* Possible return values for most tor_tls_* functions. */
|
2012-10-12 18:22:13 +02:00
|
|
|
#define MIN_TOR_TLS_ERROR_VAL_ -9
|
2007-01-15 22:13:37 +01:00
|
|
|
#define TOR_TLS_ERROR_MISC -9
|
2008-12-18 17:11:24 +01:00
|
|
|
/* Rename to unexpected close or something. XXXX */
|
2007-01-15 22:13:37 +01:00
|
|
|
#define TOR_TLS_ERROR_IO -8
|
|
|
|
#define TOR_TLS_ERROR_CONNREFUSED -7
|
|
|
|
#define TOR_TLS_ERROR_CONNRESET -6
|
|
|
|
#define TOR_TLS_ERROR_NO_ROUTE -5
|
|
|
|
#define TOR_TLS_ERROR_TIMEOUT -4
|
|
|
|
#define TOR_TLS_CLOSE -3
|
|
|
|
#define TOR_TLS_WANTREAD -2
|
|
|
|
#define TOR_TLS_WANTWRITE -1
|
|
|
|
#define TOR_TLS_DONE 0
|
2003-09-04 18:05:08 +02:00
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** Collection of case statements for all TLS errors that are not due to
|
|
|
|
* underlying IO failure. */
|
2008-02-21 17:11:58 +01:00
|
|
|
#define CASE_TOR_TLS_ERROR_ANY_NONIO \
|
2007-01-15 22:21:05 +01:00
|
|
|
case TOR_TLS_ERROR_MISC: \
|
|
|
|
case TOR_TLS_ERROR_CONNREFUSED: \
|
|
|
|
case TOR_TLS_ERROR_CONNRESET: \
|
|
|
|
case TOR_TLS_ERROR_NO_ROUTE: \
|
|
|
|
case TOR_TLS_ERROR_TIMEOUT
|
|
|
|
|
2008-02-21 17:11:58 +01:00
|
|
|
/** Use this macro in a switch statement to catch _any_ TLS error. That way,
|
|
|
|
* if more errors are added, your switches will still work. */
|
|
|
|
#define CASE_TOR_TLS_ERROR_ANY \
|
|
|
|
CASE_TOR_TLS_ERROR_ANY_NONIO: \
|
|
|
|
case TOR_TLS_ERROR_IO
|
|
|
|
|
2007-01-15 22:21:05 +01:00
|
|
|
#define TOR_TLS_IS_ERROR(rv) ((rv) < TOR_TLS_CLOSE)
|
2015-09-15 17:09:18 +02:00
|
|
|
|
|
|
|
/** Holds a SSL_CTX object and related state used to configure TLS
|
|
|
|
* connections.
|
|
|
|
*/
|
2018-08-12 01:38:07 +02:00
|
|
|
typedef struct tor_tls_context_t tor_tls_context_t;
|
2015-09-15 17:09:18 +02:00
|
|
|
|
2007-11-07 17:37:08 +01:00
|
|
|
const char *tor_tls_err_to_string(int err);
|
2011-06-22 21:29:30 +02:00
|
|
|
void tor_tls_get_state_description(tor_tls_t *tls, char *buf, size_t sz);
|
2005-02-11 02:41:19 +01:00
|
|
|
void tor_tls_free_all(void);
|
2012-12-26 02:04:54 +01:00
|
|
|
|
|
|
|
#define TOR_TLS_CTX_IS_PUBLIC_SERVER (1u<<0)
|
|
|
|
#define TOR_TLS_CTX_USE_ECDHE_P256 (1u<<1)
|
|
|
|
#define TOR_TLS_CTX_USE_ECDHE_P224 (1u<<2)
|
|
|
|
|
2018-08-12 00:16:04 +02:00
|
|
|
void tor_tls_init(void);
|
|
|
|
void tls_log_errors(tor_tls_t *tls, int severity, int domain,
|
|
|
|
const char *doing);
|
2012-12-26 02:04:54 +01:00
|
|
|
int tor_tls_context_init(unsigned flags,
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *client_identity,
|
|
|
|
crypto_pk_t *server_identity,
|
2010-10-01 23:06:57 +02:00
|
|
|
unsigned int key_lifetime);
|
2018-08-12 22:04:12 +02:00
|
|
|
void tor_tls_context_incref(tor_tls_context_t *ctx);
|
|
|
|
void tor_tls_context_decref(tor_tls_context_t *ctx);
|
|
|
|
tor_tls_context_t *tor_tls_context_get(int is_server);
|
2018-08-17 17:24:50 +02:00
|
|
|
tor_tls_t *tor_tls_new(tor_socket_t sock, int is_server);
|
2008-02-19 23:27:44 +01:00
|
|
|
void tor_tls_set_logged_address(tor_tls_t *tls, const char *address);
|
2011-12-07 01:49:20 +01:00
|
|
|
void tor_tls_set_renegotiate_callback(tor_tls_t *tls,
|
2007-12-01 09:09:48 +01:00
|
|
|
void (*cb)(tor_tls_t *, void *arg),
|
|
|
|
void *arg);
|
2005-10-06 06:33:40 +02:00
|
|
|
int tor_tls_is_server(tor_tls_t *tls);
|
2018-09-12 17:02:59 +02:00
|
|
|
void tor_tls_release_socket(tor_tls_t *tls);
|
2017-11-17 17:55:52 +01:00
|
|
|
void tor_tls_free_(tor_tls_t *tls);
|
2017-12-07 16:44:04 +01:00
|
|
|
#define tor_tls_free(tls) FREE_AND_NULL(tor_tls_t, tor_tls_free_, (tls))
|
2005-10-06 06:33:40 +02:00
|
|
|
int tor_tls_peer_has_cert(tor_tls_t *tls);
|
2018-08-12 00:16:04 +02:00
|
|
|
MOCK_DECL(struct tor_x509_cert_t *,tor_tls_get_peer_cert,(tor_tls_t *tls));
|
|
|
|
MOCK_DECL(struct tor_x509_cert_t *,tor_tls_get_own_cert,(tor_tls_t *tls));
|
2012-01-18 21:53:30 +01:00
|
|
|
int tor_tls_verify(int severity, tor_tls_t *tls, crypto_pk_t **identity);
|
2014-09-18 20:03:49 +02:00
|
|
|
MOCK_DECL(int, tor_tls_read, (tor_tls_t *tls, char *cp, size_t len));
|
2007-01-30 23:19:41 +01:00
|
|
|
int tor_tls_write(tor_tls_t *tls, const char *cp, size_t n);
|
2005-10-06 06:33:40 +02:00
|
|
|
int tor_tls_handshake(tor_tls_t *tls);
|
2009-08-14 20:34:16 +02:00
|
|
|
int tor_tls_finish_handshake(tor_tls_t *tls);
|
2010-09-27 22:07:14 +02:00
|
|
|
void tor_tls_unblock_renegotiation(tor_tls_t *tls);
|
2011-12-07 01:49:21 +01:00
|
|
|
void tor_tls_block_renegotiation(tor_tls_t *tls);
|
2010-10-12 20:45:15 +02:00
|
|
|
void tor_tls_assert_renegotiation_unblocked(tor_tls_t *tls);
|
2005-10-06 06:33:40 +02:00
|
|
|
int tor_tls_get_pending_bytes(tor_tls_t *tls);
|
2006-12-13 23:46:42 +01:00
|
|
|
size_t tor_tls_get_forced_write_size(tor_tls_t *tls);
|
2005-10-06 06:33:40 +02:00
|
|
|
|
2006-12-29 04:42:46 +01:00
|
|
|
void tor_tls_get_n_raw_bytes(tor_tls_t *tls,
|
|
|
|
size_t *n_read, size_t *n_written);
|
2004-01-13 02:19:02 +01:00
|
|
|
|
2015-05-13 18:12:53 +02:00
|
|
|
int tor_tls_get_buffer_sizes(tor_tls_t *tls,
|
2009-09-01 05:23:47 +02:00
|
|
|
size_t *rbuf_capacity, size_t *rbuf_bytes,
|
|
|
|
size_t *wbuf_capacity, size_t *wbuf_bytes);
|
2008-04-10 17:12:24 +02:00
|
|
|
|
2014-04-15 14:20:34 +02:00
|
|
|
MOCK_DECL(double, tls_get_write_overhead_ratio, (void));
|
2013-03-12 03:06:07 +01:00
|
|
|
|
2007-10-30 22:46:02 +01:00
|
|
|
int tor_tls_used_v1_handshake(tor_tls_t *tls);
|
2010-10-11 19:45:27 +02:00
|
|
|
int tor_tls_get_num_server_handshakes(tor_tls_t *tls);
|
|
|
|
int tor_tls_server_got_renegotiate(tor_tls_t *tls);
|
2018-08-12 00:16:04 +02:00
|
|
|
MOCK_DECL(int,tor_tls_cert_matches_key,(const tor_tls_t *tls,
|
|
|
|
const struct tor_x509_cert_t *cert));
|
2014-10-16 15:07:50 +02:00
|
|
|
MOCK_DECL(int,tor_tls_get_tlssecrets,(tor_tls_t *tls, uint8_t *secrets_out));
|
2018-08-23 17:30:18 +02:00
|
|
|
#ifdef ENABLE_OPENSSL
|
|
|
|
/* OpenSSL lets us see these master secrets; NSS sensibly does not. */
|
|
|
|
#define HAVE_WORKING_TOR_TLS_GET_TLSSECRETS
|
|
|
|
#endif
|
2016-05-10 22:47:52 +02:00
|
|
|
MOCK_DECL(int,tor_tls_export_key_material,(
|
|
|
|
tor_tls_t *tls, uint8_t *secrets_out,
|
|
|
|
const uint8_t *context,
|
|
|
|
size_t context_len,
|
|
|
|
const char *label));
|
2007-10-30 22:46:02 +01:00
|
|
|
|
2018-08-17 17:24:50 +02:00
|
|
|
#ifdef ENABLE_OPENSSL
|
2004-05-01 22:46:28 +02:00
|
|
|
/* Log and abort if there are unhandled TLS errors in OpenSSL's error stack.
|
|
|
|
*/
|
2012-10-12 18:22:13 +02:00
|
|
|
#define check_no_tls_errors() check_no_tls_errors_(__FILE__,__LINE__)
|
|
|
|
void check_no_tls_errors_(const char *fname, int line);
|
2018-08-17 17:24:50 +02:00
|
|
|
|
2010-10-11 19:26:57 +02:00
|
|
|
void tor_tls_log_one_error(tor_tls_t *tls, unsigned long err,
|
|
|
|
int severity, int domain, const char *doing);
|
2019-06-05 15:33:35 +02:00
|
|
|
#else /* !(defined(ENABLE_OPENSSL)) */
|
2018-08-17 17:24:50 +02:00
|
|
|
#define check_no_tls_errors() STMT_NIL
|
2019-06-05 15:33:35 +02:00
|
|
|
#endif /* defined(ENABLE_OPENSSL) */
|
2004-04-27 01:00:07 +02:00
|
|
|
|
2011-09-13 17:37:15 +02:00
|
|
|
int tor_tls_get_my_certs(int server,
|
2018-08-12 00:16:04 +02:00
|
|
|
const struct tor_x509_cert_t **link_cert_out,
|
|
|
|
const struct tor_x509_cert_t **id_cert_out);
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *tor_tls_get_my_client_auth_key(void);
|
2018-08-12 00:16:04 +02:00
|
|
|
|
2012-11-28 19:31:17 +01:00
|
|
|
const char *tor_tls_get_ciphersuite_name(tor_tls_t *tls);
|
2011-09-13 17:37:15 +02:00
|
|
|
|
2015-05-21 19:07:30 +02:00
|
|
|
int evaluate_ecgroup_for_tls(const char *ecgroup);
|
|
|
|
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(TOR_TORTLS_H) */
|