tortls.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_TORTLS_H
  6. #define TOR_TORTLS_H
  7. /**
  8. * \file tortls.h
  9. * \brief Headers for tortls.c
  10. **/
  11. #include <openssl/ssl.h>
  12. #include <openssl/ssl3.h>
  13. #include "crypto.h"
  14. #include "compat.h"
  15. #include "testsupport.h"
  16. /* Opaque structure to hold a TLS connection. */
  17. typedef struct tor_tls_t tor_tls_t;
  18. /* Opaque structure to hold an X509 certificate. */
  19. typedef struct tor_x509_cert_t tor_x509_cert_t;
  20. /* Possible return values for most tor_tls_* functions. */
  21. #define MIN_TOR_TLS_ERROR_VAL_ -9
  22. #define TOR_TLS_ERROR_MISC -9
  23. /* Rename to unexpected close or something. XXXX */
  24. #define TOR_TLS_ERROR_IO -8
  25. #define TOR_TLS_ERROR_CONNREFUSED -7
  26. #define TOR_TLS_ERROR_CONNRESET -6
  27. #define TOR_TLS_ERROR_NO_ROUTE -5
  28. #define TOR_TLS_ERROR_TIMEOUT -4
  29. #define TOR_TLS_CLOSE -3
  30. #define TOR_TLS_WANTREAD -2
  31. #define TOR_TLS_WANTWRITE -1
  32. #define TOR_TLS_DONE 0
  33. /** Collection of case statements for all TLS errors that are not due to
  34. * underlying IO failure. */
  35. #define CASE_TOR_TLS_ERROR_ANY_NONIO \
  36. case TOR_TLS_ERROR_MISC: \
  37. case TOR_TLS_ERROR_CONNREFUSED: \
  38. case TOR_TLS_ERROR_CONNRESET: \
  39. case TOR_TLS_ERROR_NO_ROUTE: \
  40. case TOR_TLS_ERROR_TIMEOUT
  41. /** Use this macro in a switch statement to catch _any_ TLS error. That way,
  42. * if more errors are added, your switches will still work. */
  43. #define CASE_TOR_TLS_ERROR_ANY \
  44. CASE_TOR_TLS_ERROR_ANY_NONIO: \
  45. case TOR_TLS_ERROR_IO
  46. #define TOR_TLS_IS_ERROR(rv) ((rv) < TOR_TLS_CLOSE)
  47. #ifdef TORTLS_PRIVATE
  48. #define TOR_TLS_MAGIC 0x71571571
  49. typedef enum {
  50. TOR_TLS_ST_HANDSHAKE, TOR_TLS_ST_OPEN, TOR_TLS_ST_GOTCLOSE,
  51. TOR_TLS_ST_SENTCLOSE, TOR_TLS_ST_CLOSED, TOR_TLS_ST_RENEGOTIATE,
  52. TOR_TLS_ST_BUFFEREVENT
  53. } tor_tls_state_t;
  54. #define tor_tls_state_bitfield_t ENUM_BF(tor_tls_state_t)
  55. /** Holds a SSL_CTX object and related state used to configure TLS
  56. * connections.
  57. */
  58. typedef struct tor_tls_context_t {
  59. int refcnt;
  60. SSL_CTX *ctx;
  61. tor_x509_cert_t *my_link_cert;
  62. tor_x509_cert_t *my_id_cert;
  63. tor_x509_cert_t *my_auth_cert;
  64. crypto_pk_t *link_key;
  65. crypto_pk_t *auth_key;
  66. } tor_tls_context_t;
  67. /** Structure that we use for a single certificate. */
  68. struct tor_x509_cert_t {
  69. X509 *cert;
  70. uint8_t *encoded;
  71. size_t encoded_len;
  72. unsigned pkey_digests_set : 1;
  73. digests_t cert_digests;
  74. digests_t pkey_digests;
  75. };
  76. /** Holds a SSL object and its associated data. Members are only
  77. * accessed from within tortls.c.
  78. */
  79. struct tor_tls_t {
  80. uint32_t magic;
  81. tor_tls_context_t *context; /** A link to the context object for this tls. */
  82. SSL *ssl; /**< An OpenSSL SSL object. */
  83. int socket; /**< The underlying file descriptor for this TLS connection. */
  84. char *address; /**< An address to log when describing this connection. */
  85. tor_tls_state_bitfield_t state : 3; /**< The current SSL state,
  86. * depending on which operations
  87. * have completed successfully. */
  88. unsigned int isServer:1; /**< True iff this is a server-side connection */
  89. unsigned int wasV2Handshake:1; /**< True iff the original handshake for
  90. * this connection used the updated version
  91. * of the connection protocol (client sends
  92. * different cipher list, server sends only
  93. * one certificate). */
  94. /** True iff we should call negotiated_callback when we're done reading. */
  95. unsigned int got_renegotiate:1;
  96. /** Return value from tor_tls_classify_client_ciphers, or 0 if we haven't
  97. * called that function yet. */
  98. int8_t client_cipher_list_type;
  99. /** Incremented every time we start the server side of a handshake. */
  100. uint8_t server_handshake_count;
  101. size_t wantwrite_n; /**< 0 normally, >0 if we returned wantwrite last
  102. * time. */
  103. /** Last values retrieved from BIO_number_read()/write(); see
  104. * tor_tls_get_n_raw_bytes() for usage.
  105. */
  106. unsigned long last_write_count;
  107. unsigned long last_read_count;
  108. /** If set, a callback to invoke whenever the client tries to renegotiate
  109. * the handshake. */
  110. void (*negotiated_callback)(tor_tls_t *tls, void *arg);
  111. /** Argument to pass to negotiated_callback. */
  112. void *callback_arg;
  113. };
  114. STATIC int tor_errno_to_tls_error(int e);
  115. STATIC int tor_tls_get_error(tor_tls_t *tls, int r, int extra,
  116. const char *doing, int severity, int domain);
  117. STATIC tor_tls_t *tor_tls_get_by_ssl(const SSL *ssl);
  118. STATIC void tor_tls_allocate_tor_tls_object_ex_data_index(void);
  119. STATIC int always_accept_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx);
  120. STATIC int tor_tls_classify_client_ciphers(const SSL *ssl,
  121. STACK_OF(SSL_CIPHER) *peer_ciphers);
  122. STATIC int tor_tls_client_is_using_v2_ciphers(const SSL *ssl);
  123. MOCK_DECL(STATIC void, try_to_extract_certs_from_tls,
  124. (int severity, tor_tls_t *tls, X509 **cert_out, X509 **id_cert_out));
  125. STATIC int dn_indicates_v3_cert(X509_NAME *name);
  126. STATIC size_t SSL_SESSION_get_master_key(SSL_SESSION *s, uint8_t *out,
  127. size_t len);
  128. STATIC void tor_tls_debug_state_callback(const SSL *ssl, int type, int val);
  129. STATIC void tor_tls_server_info_callback(const SSL *ssl, int type, int val);
  130. STATIC int tor_tls_session_secret_cb(SSL *ssl, void *secret,
  131. int *secret_len,
  132. STACK_OF(SSL_CIPHER) *peer_ciphers,
  133. SSL_CIPHER **cipher, void *arg);
  134. STATIC int find_cipher_by_id(const SSL *ssl, const SSL_METHOD *m,
  135. uint16_t cipher);
  136. MOCK_DECL(STATIC X509*, tor_tls_create_certificate,(crypto_pk_t *rsa,
  137. crypto_pk_t *rsa_sign,
  138. const char *cname,
  139. const char *cname_sign,
  140. unsigned int cert_lifetime));
  141. STATIC tor_tls_context_t *tor_tls_context_new(crypto_pk_t *identity,
  142. unsigned int key_lifetime, unsigned flags, int is_client);
  143. MOCK_DECL(STATIC tor_x509_cert_t *, tor_x509_cert_new,(X509 *x509_cert));
  144. STATIC int tor_tls_context_init_one(tor_tls_context_t **ppcontext,
  145. crypto_pk_t *identity,
  146. unsigned int key_lifetime,
  147. unsigned int flags,
  148. int is_client);
  149. STATIC void tls_log_errors(tor_tls_t *tls, int severity, int domain,
  150. const char *doing);
  151. #endif
  152. const char *tor_tls_err_to_string(int err);
  153. void tor_tls_get_state_description(tor_tls_t *tls, char *buf, size_t sz);
  154. void tor_tls_free_all(void);
  155. #define TOR_TLS_CTX_IS_PUBLIC_SERVER (1u<<0)
  156. #define TOR_TLS_CTX_USE_ECDHE_P256 (1u<<1)
  157. #define TOR_TLS_CTX_USE_ECDHE_P224 (1u<<2)
  158. int tor_tls_context_init(unsigned flags,
  159. crypto_pk_t *client_identity,
  160. crypto_pk_t *server_identity,
  161. unsigned int key_lifetime);
  162. tor_tls_t *tor_tls_new(int sock, int is_server);
  163. void tor_tls_set_logged_address(tor_tls_t *tls, const char *address);
  164. void tor_tls_set_renegotiate_callback(tor_tls_t *tls,
  165. void (*cb)(tor_tls_t *, void *arg),
  166. void *arg);
  167. int tor_tls_is_server(tor_tls_t *tls);
  168. void tor_tls_free(tor_tls_t *tls);
  169. int tor_tls_peer_has_cert(tor_tls_t *tls);
  170. MOCK_DECL(tor_x509_cert_t *,tor_tls_get_peer_cert,(tor_tls_t *tls));
  171. int tor_tls_verify(int severity, tor_tls_t *tls, crypto_pk_t **identity);
  172. int tor_tls_check_lifetime(int severity,
  173. tor_tls_t *tls, int past_tolerance,
  174. int future_tolerance);
  175. MOCK_DECL(int, tor_tls_read, (tor_tls_t *tls, char *cp, size_t len));
  176. int tor_tls_write(tor_tls_t *tls, const char *cp, size_t n);
  177. int tor_tls_handshake(tor_tls_t *tls);
  178. int tor_tls_finish_handshake(tor_tls_t *tls);
  179. int tor_tls_renegotiate(tor_tls_t *tls);
  180. void tor_tls_unblock_renegotiation(tor_tls_t *tls);
  181. void tor_tls_block_renegotiation(tor_tls_t *tls);
  182. void tor_tls_assert_renegotiation_unblocked(tor_tls_t *tls);
  183. int tor_tls_shutdown(tor_tls_t *tls);
  184. int tor_tls_get_pending_bytes(tor_tls_t *tls);
  185. size_t tor_tls_get_forced_write_size(tor_tls_t *tls);
  186. void tor_tls_get_n_raw_bytes(tor_tls_t *tls,
  187. size_t *n_read, size_t *n_written);
  188. int tor_tls_get_buffer_sizes(tor_tls_t *tls,
  189. size_t *rbuf_capacity, size_t *rbuf_bytes,
  190. size_t *wbuf_capacity, size_t *wbuf_bytes);
  191. MOCK_DECL(double, tls_get_write_overhead_ratio, (void));
  192. int tor_tls_used_v1_handshake(tor_tls_t *tls);
  193. int tor_tls_received_v3_certificate(tor_tls_t *tls);
  194. int tor_tls_get_num_server_handshakes(tor_tls_t *tls);
  195. int tor_tls_server_got_renegotiate(tor_tls_t *tls);
  196. MOCK_DECL(int,tor_tls_get_tlssecrets,(tor_tls_t *tls, uint8_t *secrets_out));
  197. /* Log and abort if there are unhandled TLS errors in OpenSSL's error stack.
  198. */
  199. #define check_no_tls_errors() check_no_tls_errors_(__FILE__,__LINE__)
  200. void check_no_tls_errors_(const char *fname, int line);
  201. void tor_tls_log_one_error(tor_tls_t *tls, unsigned long err,
  202. int severity, int domain, const char *doing);
  203. #ifdef USE_BUFFEREVENTS
  204. int tor_tls_start_renegotiating(tor_tls_t *tls);
  205. struct bufferevent *tor_tls_init_bufferevent(tor_tls_t *tls,
  206. struct bufferevent *bufev_in,
  207. evutil_socket_t socket, int receiving,
  208. int filter);
  209. #endif
  210. void tor_x509_cert_free(tor_x509_cert_t *cert);
  211. tor_x509_cert_t *tor_x509_cert_decode(const uint8_t *certificate,
  212. size_t certificate_len);
  213. void tor_x509_cert_get_der(const tor_x509_cert_t *cert,
  214. const uint8_t **encoded_out, size_t *size_out);
  215. const digests_t *tor_x509_cert_get_id_digests(const tor_x509_cert_t *cert);
  216. const digests_t *tor_x509_cert_get_cert_digests(const tor_x509_cert_t *cert);
  217. int tor_tls_get_my_certs(int server,
  218. const tor_x509_cert_t **link_cert_out,
  219. const tor_x509_cert_t **id_cert_out);
  220. crypto_pk_t *tor_tls_get_my_client_auth_key(void);
  221. crypto_pk_t *tor_tls_cert_get_key(tor_x509_cert_t *cert);
  222. MOCK_DECL(int,tor_tls_cert_matches_key,(const tor_tls_t *tls,
  223. const tor_x509_cert_t *cert));
  224. int tor_tls_cert_is_valid(int severity,
  225. const tor_x509_cert_t *cert,
  226. const tor_x509_cert_t *signing_cert,
  227. int check_rsa_1024);
  228. const char *tor_tls_get_ciphersuite_name(tor_tls_t *tls);
  229. int evaluate_ecgroup_for_tls(const char *ecgroup);
  230. #endif