2018-02-27 19:20:58 +00:00
|
|
|
/*-
|
2015-02-24 18:19:20 +00:00
|
|
|
* SSLsplit - transparent SSL/TLS interception
|
2018-02-27 19:20:58 +00:00
|
|
|
* https://www.roe.ch/SSLsplit
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009-2018, Daniel Roethlisberger <daniel@roe.ch>.
|
2012-04-13 12:47:30 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2018-02-27 19:20:58 +00:00
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
2012-04-13 12:47:30 +00:00
|
|
|
*
|
2018-02-27 19:20:58 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2012-04-13 12:47:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PXYCONN_H
|
|
|
|
#define PXYCONN_H
|
|
|
|
|
2017-07-25 13:07:39 +00:00
|
|
|
#include "proxy.h"
|
2012-04-13 12:47:30 +00:00
|
|
|
#include "opts.h"
|
|
|
|
#include "attrib.h"
|
|
|
|
#include "pxythrmgr.h"
|
2017-05-29 09:22:23 +00:00
|
|
|
#include "log.h"
|
2012-04-13 12:47:30 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <event2/event.h>
|
|
|
|
#include <event2/util.h>
|
|
|
|
|
2017-10-26 15:57:46 +00:00
|
|
|
/*
|
|
|
|
* Print helper for logging code.
|
|
|
|
*/
|
|
|
|
#define STRORDASH(x) (((x)&&*(x))?(x):"-")
|
|
|
|
#define STRORNONE(x) (((x)&&*(x))?(x):"")
|
|
|
|
|
2017-07-15 01:07:42 +00:00
|
|
|
typedef struct pxy_conn_child_ctx pxy_conn_child_ctx_t;
|
|
|
|
|
2017-07-07 14:18:01 +00:00
|
|
|
/* single socket bufferevent descriptor */
|
2017-05-29 09:22:23 +00:00
|
|
|
typedef struct pxy_conn_desc {
|
|
|
|
struct bufferevent *bev;
|
|
|
|
SSL *ssl;
|
|
|
|
unsigned int closed : 1;
|
|
|
|
} pxy_conn_desc_t;
|
|
|
|
|
2017-07-12 19:37:36 +00:00
|
|
|
/* parent connection state consisting of three connection descriptors,
|
2017-05-29 09:22:23 +00:00
|
|
|
* connection-wide state and the specs and options */
|
2017-07-15 01:07:42 +00:00
|
|
|
struct pxy_conn_ctx {
|
2017-07-18 19:07:29 +00:00
|
|
|
// Common properties
|
2017-07-29 21:34:46 +00:00
|
|
|
// @attention The order of these common vars should match with their order in children
|
2017-05-29 09:22:23 +00:00
|
|
|
/* per-connection state */
|
|
|
|
struct pxy_conn_desc src;
|
|
|
|
struct pxy_conn_desc dst;
|
2017-07-18 19:07:29 +00:00
|
|
|
|
2017-05-29 09:22:23 +00:00
|
|
|
/* status flags */
|
|
|
|
unsigned int connected : 1; /* 0 until both ends are connected */
|
2017-07-25 13:07:39 +00:00
|
|
|
unsigned int enomem : 1; /* 1 if out of memory */
|
|
|
|
/* http */
|
2017-05-29 09:22:23 +00:00
|
|
|
unsigned int seen_req_header : 1; /* 0 until request header complete */
|
|
|
|
unsigned int seen_resp_header : 1; /* 0 until response hdr complete */
|
|
|
|
unsigned int sent_http_conn_close : 1; /* 0 until Conn: close sent */
|
|
|
|
unsigned int ocsp_denied : 1; /* 1 if OCSP was denied */
|
|
|
|
|
|
|
|
/* log strings from socket */
|
|
|
|
char *srchost_str;
|
|
|
|
char *srcport_str;
|
|
|
|
char *dsthost_str;
|
|
|
|
char *dstport_str;
|
|
|
|
|
|
|
|
/* log strings from HTTP request */
|
|
|
|
char *http_method;
|
|
|
|
char *http_uri;
|
|
|
|
char *http_host;
|
|
|
|
char *http_content_type;
|
|
|
|
|
|
|
|
/* log strings from HTTP response */
|
|
|
|
char *http_status_code;
|
|
|
|
char *http_status_text;
|
|
|
|
char *http_content_length;
|
|
|
|
|
|
|
|
/* log strings related to SSL */
|
|
|
|
char *ssl_names;
|
|
|
|
char *origcrtfpr;
|
|
|
|
char *usedcrtfpr;
|
|
|
|
|
2017-07-18 19:07:29 +00:00
|
|
|
/* store fd and fd event while connected is 0 */
|
|
|
|
evutil_socket_t fd;
|
2017-07-25 13:07:39 +00:00
|
|
|
// End of common properties
|
2017-05-29 09:22:23 +00:00
|
|
|
|
|
|
|
/* content log context */
|
|
|
|
log_content_ctx_t *logctx;
|
2017-07-18 19:07:29 +00:00
|
|
|
|
|
|
|
unsigned int srv_dst_connected : 1; /* 0 until server is connected */
|
|
|
|
unsigned int dst_connected : 1; /* 0 until dst is connected */
|
2017-07-25 13:07:39 +00:00
|
|
|
|
|
|
|
/* ssl */
|
2017-07-18 19:07:29 +00:00
|
|
|
unsigned int sni_peek_retries : 6; /* max 64 SNI parse retries */
|
2017-07-25 13:07:39 +00:00
|
|
|
unsigned int immutable_cert : 1; /* 1 if the cert cannot be changed */
|
|
|
|
unsigned int generated_cert : 1; /* 1 if we generated a new cert */
|
|
|
|
unsigned int passthrough : 1; /* 1 if SSL passthrough is active */
|
|
|
|
/* autossl */
|
2017-07-18 19:07:29 +00:00
|
|
|
unsigned int clienthello_search : 1; /* 1 if waiting for hello */
|
|
|
|
unsigned int clienthello_found : 1; /* 1 if conn upgrade to SSL */
|
|
|
|
|
|
|
|
struct pxy_conn_desc srv_dst;
|
2017-07-22 22:15:59 +00:00
|
|
|
char *srv_dst_ssl_version;
|
|
|
|
char *srv_dst_ssl_cipher;
|
2017-05-29 09:22:23 +00:00
|
|
|
|
|
|
|
struct event *ev;
|
|
|
|
|
|
|
|
/* original destination address, family and certificate */
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
socklen_t addrlen;
|
|
|
|
int af;
|
|
|
|
X509 *origcrt;
|
|
|
|
|
2017-07-15 01:07:42 +00:00
|
|
|
// Thread that the conn is attached to
|
|
|
|
pxy_thr_ctx_t *thr;
|
|
|
|
|
|
|
|
// Unique id of the conn
|
2017-10-26 17:10:36 +00:00
|
|
|
long long unsigned int id;
|
2017-07-15 01:07:42 +00:00
|
|
|
|
|
|
|
pxy_thrmgr_ctx_t *thrmgr;
|
|
|
|
proxyspec_t *spec;
|
|
|
|
opts_t *opts;
|
|
|
|
|
|
|
|
struct event_base *evbase;
|
|
|
|
struct evdns_base *dnsbase;
|
|
|
|
|
|
|
|
evutil_socket_t dst_fd;
|
|
|
|
evutil_socket_t srv_dst_fd;
|
|
|
|
|
|
|
|
// Priv sep socket to obtain a socket for children
|
|
|
|
evutil_socket_t clisock;
|
|
|
|
|
|
|
|
// Fd of the listener event for the children
|
|
|
|
evutil_socket_t child_fd;
|
|
|
|
struct evconnlistener *child_evcl;
|
2017-10-14 22:39:30 +00:00
|
|
|
// SSL proxy specific info: The IP:port address the children are listening on, orig client addr, and orig target addr
|
2017-08-11 12:01:51 +00:00
|
|
|
char *header_str;
|
|
|
|
int sent_header;
|
2017-07-15 01:07:42 +00:00
|
|
|
|
|
|
|
// Child list of the conn
|
|
|
|
pxy_conn_child_ctx_t *children;
|
|
|
|
|
|
|
|
// Number of children, active or closed
|
|
|
|
unsigned int child_count;
|
|
|
|
|
|
|
|
evutil_socket_t child_src_fd;
|
|
|
|
evutil_socket_t child_dst_fd;
|
|
|
|
|
|
|
|
/* server name indicated by client in SNI TLS extension */
|
|
|
|
char *sni;
|
|
|
|
|
2017-07-15 10:04:13 +00:00
|
|
|
// Conn create time
|
|
|
|
time_t ctime;
|
2017-07-15 01:07:42 +00:00
|
|
|
|
2017-07-15 10:04:13 +00:00
|
|
|
// Conn last access time, to determine expired conns
|
|
|
|
// Updated on entry to callback functions, parent or child
|
|
|
|
time_t atime;
|
|
|
|
|
2017-07-15 01:07:42 +00:00
|
|
|
// Per-thread conn list
|
|
|
|
pxy_conn_ctx_t *next;
|
|
|
|
|
|
|
|
// Expired conns are link-listed using this pointer
|
|
|
|
pxy_conn_ctx_t *next_expired;
|
2017-07-18 19:07:29 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_LOCAL_PROCINFO
|
|
|
|
/* local process information */
|
|
|
|
pxy_conn_lproc_desc_t lproc;
|
|
|
|
#endif /* HAVE_LOCAL_PROCINFO */
|
2017-07-15 01:07:42 +00:00
|
|
|
};
|
2017-05-29 09:22:23 +00:00
|
|
|
|
2017-07-12 19:37:36 +00:00
|
|
|
/* child connection state consisting of two connection descriptors,
|
|
|
|
* connection-wide state */
|
2017-07-15 01:07:42 +00:00
|
|
|
struct pxy_conn_child_ctx {
|
2017-07-18 19:07:29 +00:00
|
|
|
// Common properties
|
2017-07-29 21:34:46 +00:00
|
|
|
// @attention The order of these common vars should match with their order in parent
|
2017-07-11 22:45:15 +00:00
|
|
|
/* per-connection state */
|
|
|
|
struct pxy_conn_desc src;
|
|
|
|
struct pxy_conn_desc dst;
|
2017-07-12 19:37:36 +00:00
|
|
|
|
2017-07-11 22:45:15 +00:00
|
|
|
/* status flags */
|
|
|
|
unsigned int connected : 1; /* 0 until both ends are connected */
|
2017-07-25 13:07:39 +00:00
|
|
|
unsigned int enomem : 1; /* 1 if out of memory */
|
|
|
|
/* http */
|
2017-07-18 19:07:29 +00:00
|
|
|
unsigned int seen_req_header : 1; /* 0 until request header complete */
|
|
|
|
unsigned int seen_resp_header : 1; /* 0 until response hdr complete */
|
|
|
|
unsigned int sent_http_conn_close : 1; /* 0 until Conn: close sent */
|
|
|
|
unsigned int ocsp_denied : 1; /* 1 if OCSP was denied */
|
2017-07-11 22:45:15 +00:00
|
|
|
|
|
|
|
/* log strings from socket */
|
|
|
|
char *srchost_str;
|
|
|
|
char *srcport_str;
|
|
|
|
char *dsthost_str;
|
|
|
|
char *dstport_str;
|
|
|
|
|
2017-07-18 19:07:29 +00:00
|
|
|
/* log strings from HTTP request */
|
|
|
|
char *http_method;
|
|
|
|
char *http_uri;
|
|
|
|
char *http_host;
|
|
|
|
char *http_content_type;
|
|
|
|
|
|
|
|
/* log strings from HTTP response */
|
|
|
|
char *http_status_code;
|
|
|
|
char *http_status_text;
|
|
|
|
char *http_content_length;
|
|
|
|
|
2017-07-11 22:45:15 +00:00
|
|
|
/* log strings related to SSL */
|
|
|
|
char *ssl_names;
|
|
|
|
char *origcrtfpr;
|
|
|
|
char *usedcrtfpr;
|
|
|
|
|
2017-07-18 19:07:29 +00:00
|
|
|
/* store fd and fd event while connected is 0 */
|
|
|
|
evutil_socket_t fd;
|
|
|
|
// End of common properties
|
|
|
|
|
|
|
|
evutil_socket_t src_fd;
|
|
|
|
evutil_socket_t dst_fd;
|
2017-07-11 22:45:15 +00:00
|
|
|
|
2017-07-15 01:07:42 +00:00
|
|
|
pxy_conn_ctx_t *parent;
|
2017-07-12 19:37:36 +00:00
|
|
|
|
|
|
|
// Child index
|
|
|
|
unsigned int idx;
|
|
|
|
|
|
|
|
// Children of the conn are link-listed using this pointer
|
|
|
|
pxy_conn_child_ctx_t *next;
|
2017-07-15 01:07:42 +00:00
|
|
|
};
|
2017-07-11 22:45:15 +00:00
|
|
|
|
|
|
|
void pxy_conn_setup(evutil_socket_t, struct sockaddr *, int,
|
|
|
|
pxy_thrmgr_ctx_t *, proxyspec_t *, opts_t *,
|
|
|
|
evutil_socket_t)
|
|
|
|
NONNULL(2,4,5,6);
|
2017-07-25 13:07:39 +00:00
|
|
|
void pxy_conn_free(pxy_conn_ctx_t *ctx, int) NONNULL(1);
|
2017-07-11 22:45:15 +00:00
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
#endif /* !PXYCONN_H */
|
|
|
|
|
|
|
|
/* vim: set noet ft=c: */
|