1*c43e99fdSEd Maste /*
2*c43e99fdSEd Maste   This is an example of how to hook up evhttp with bufferevent_ssl
3*c43e99fdSEd Maste 
4*c43e99fdSEd Maste   It just GETs an https URL given on the command-line and prints the response
5*c43e99fdSEd Maste   body to stdout.
6*c43e99fdSEd Maste 
7*c43e99fdSEd Maste   Actually, it also accepts plain http URLs to make it easy to compare http vs
8*c43e99fdSEd Maste   https code paths.
9*c43e99fdSEd Maste 
10*c43e99fdSEd Maste   Loosely based on le-proxy.c.
11*c43e99fdSEd Maste  */
12*c43e99fdSEd Maste 
13*c43e99fdSEd Maste // Get rid of OSX 10.7 and greater deprecation warnings.
14*c43e99fdSEd Maste #if defined(__APPLE__) && defined(__clang__)
15*c43e99fdSEd Maste #pragma clang diagnostic ignored "-Wdeprecated-declarations"
16*c43e99fdSEd Maste #endif
17*c43e99fdSEd Maste 
18*c43e99fdSEd Maste #include <stdio.h>
19*c43e99fdSEd Maste #include <assert.h>
20*c43e99fdSEd Maste #include <stdlib.h>
21*c43e99fdSEd Maste #include <string.h>
22*c43e99fdSEd Maste #include <errno.h>
23*c43e99fdSEd Maste 
24*c43e99fdSEd Maste #ifdef _WIN32
25*c43e99fdSEd Maste #include <winsock2.h>
26*c43e99fdSEd Maste #include <ws2tcpip.h>
27*c43e99fdSEd Maste 
28*c43e99fdSEd Maste #define snprintf _snprintf
29*c43e99fdSEd Maste #define strcasecmp _stricmp
30*c43e99fdSEd Maste #else
31*c43e99fdSEd Maste #include <sys/socket.h>
32*c43e99fdSEd Maste #include <netinet/in.h>
33*c43e99fdSEd Maste #endif
34*c43e99fdSEd Maste 
35*c43e99fdSEd Maste #include <event2/bufferevent_ssl.h>
36*c43e99fdSEd Maste #include <event2/bufferevent.h>
37*c43e99fdSEd Maste #include <event2/buffer.h>
38*c43e99fdSEd Maste #include <event2/listener.h>
39*c43e99fdSEd Maste #include <event2/util.h>
40*c43e99fdSEd Maste #include <event2/http.h>
41*c43e99fdSEd Maste 
42*c43e99fdSEd Maste #include <openssl/ssl.h>
43*c43e99fdSEd Maste #include <openssl/err.h>
44*c43e99fdSEd Maste #include <openssl/rand.h>
45*c43e99fdSEd Maste 
46*c43e99fdSEd Maste #include "openssl_hostname_validation.h"
47*c43e99fdSEd Maste 
48*c43e99fdSEd Maste static struct event_base *base;
49*c43e99fdSEd Maste static int ignore_cert = 0;
50*c43e99fdSEd Maste 
51*c43e99fdSEd Maste static void
http_request_done(struct evhttp_request * req,void * ctx)52*c43e99fdSEd Maste http_request_done(struct evhttp_request *req, void *ctx)
53*c43e99fdSEd Maste {
54*c43e99fdSEd Maste 	char buffer[256];
55*c43e99fdSEd Maste 	int nread;
56*c43e99fdSEd Maste 
57*c43e99fdSEd Maste 	if (req == NULL) {
58*c43e99fdSEd Maste 		/* If req is NULL, it means an error occurred, but
59*c43e99fdSEd Maste 		 * sadly we are mostly left guessing what the error
60*c43e99fdSEd Maste 		 * might have been.  We'll do our best... */
61*c43e99fdSEd Maste 		struct bufferevent *bev = (struct bufferevent *) ctx;
62*c43e99fdSEd Maste 		unsigned long oslerr;
63*c43e99fdSEd Maste 		int printed_err = 0;
64*c43e99fdSEd Maste 		int errcode = EVUTIL_SOCKET_ERROR();
65*c43e99fdSEd Maste 		fprintf(stderr, "some request failed - no idea which one though!\n");
66*c43e99fdSEd Maste 		/* Print out the OpenSSL error queue that libevent
67*c43e99fdSEd Maste 		 * squirreled away for us, if any. */
68*c43e99fdSEd Maste 		while ((oslerr = bufferevent_get_openssl_error(bev))) {
69*c43e99fdSEd Maste 			ERR_error_string_n(oslerr, buffer, sizeof(buffer));
70*c43e99fdSEd Maste 			fprintf(stderr, "%s\n", buffer);
71*c43e99fdSEd Maste 			printed_err = 1;
72*c43e99fdSEd Maste 		}
73*c43e99fdSEd Maste 		/* If the OpenSSL error queue was empty, maybe it was a
74*c43e99fdSEd Maste 		 * socket error; let's try printing that. */
75*c43e99fdSEd Maste 		if (! printed_err)
76*c43e99fdSEd Maste 			fprintf(stderr, "socket error = %s (%d)\n",
77*c43e99fdSEd Maste 				evutil_socket_error_to_string(errcode),
78*c43e99fdSEd Maste 				errcode);
79*c43e99fdSEd Maste 		return;
80*c43e99fdSEd Maste 	}
81*c43e99fdSEd Maste 
82*c43e99fdSEd Maste 	fprintf(stderr, "Response line: %d %s\n",
83*c43e99fdSEd Maste 	    evhttp_request_get_response_code(req),
84*c43e99fdSEd Maste 	    evhttp_request_get_response_code_line(req));
85*c43e99fdSEd Maste 
86*c43e99fdSEd Maste 	while ((nread = evbuffer_remove(evhttp_request_get_input_buffer(req),
87*c43e99fdSEd Maste 		    buffer, sizeof(buffer)))
88*c43e99fdSEd Maste 	       > 0) {
89*c43e99fdSEd Maste 		/* These are just arbitrary chunks of 256 bytes.
90*c43e99fdSEd Maste 		 * They are not lines, so we can't treat them as such. */
91*c43e99fdSEd Maste 		fwrite(buffer, nread, 1, stdout);
92*c43e99fdSEd Maste 	}
93*c43e99fdSEd Maste }
94*c43e99fdSEd Maste 
95*c43e99fdSEd Maste static void
syntax(void)96*c43e99fdSEd Maste syntax(void)
97*c43e99fdSEd Maste {
98*c43e99fdSEd Maste 	fputs("Syntax:\n", stderr);
99*c43e99fdSEd Maste 	fputs("   https-client -url <https-url> [-data data-file.bin] [-ignore-cert] [-retries num] [-timeout sec] [-crt crt]\n", stderr);
100*c43e99fdSEd Maste 	fputs("Example:\n", stderr);
101*c43e99fdSEd Maste 	fputs("   https-client -url https://ip.appspot.com/\n", stderr);
102*c43e99fdSEd Maste }
103*c43e99fdSEd Maste 
104*c43e99fdSEd Maste static void
err(const char * msg)105*c43e99fdSEd Maste err(const char *msg)
106*c43e99fdSEd Maste {
107*c43e99fdSEd Maste 	fputs(msg, stderr);
108*c43e99fdSEd Maste }
109*c43e99fdSEd Maste 
110*c43e99fdSEd Maste static void
err_openssl(const char * func)111*c43e99fdSEd Maste err_openssl(const char *func)
112*c43e99fdSEd Maste {
113*c43e99fdSEd Maste 	fprintf (stderr, "%s failed:\n", func);
114*c43e99fdSEd Maste 
115*c43e99fdSEd Maste 	/* This is the OpenSSL function that prints the contents of the
116*c43e99fdSEd Maste 	 * error stack to the specified file handle. */
117*c43e99fdSEd Maste 	ERR_print_errors_fp (stderr);
118*c43e99fdSEd Maste 
119*c43e99fdSEd Maste 	exit(1);
120*c43e99fdSEd Maste }
121*c43e99fdSEd Maste 
122*c43e99fdSEd Maste #ifndef _WIN32
123*c43e99fdSEd Maste /* See http://archives.seul.org/libevent/users/Jan-2013/msg00039.html */
cert_verify_callback(X509_STORE_CTX * x509_ctx,void * arg)124*c43e99fdSEd Maste static int cert_verify_callback(X509_STORE_CTX *x509_ctx, void *arg)
125*c43e99fdSEd Maste {
126*c43e99fdSEd Maste 	char cert_str[256];
127*c43e99fdSEd Maste 	const char *host = (const char *) arg;
128*c43e99fdSEd Maste 	const char *res_str = "X509_verify_cert failed";
129*c43e99fdSEd Maste 	HostnameValidationResult res = Error;
130*c43e99fdSEd Maste 
131*c43e99fdSEd Maste 	/* This is the function that OpenSSL would call if we hadn't called
132*c43e99fdSEd Maste 	 * SSL_CTX_set_cert_verify_callback().  Therefore, we are "wrapping"
133*c43e99fdSEd Maste 	 * the default functionality, rather than replacing it. */
134*c43e99fdSEd Maste 	int ok_so_far = 0;
135*c43e99fdSEd Maste 
136*c43e99fdSEd Maste 	X509 *server_cert = NULL;
137*c43e99fdSEd Maste 
138*c43e99fdSEd Maste 	if (ignore_cert) {
139*c43e99fdSEd Maste 		return 1;
140*c43e99fdSEd Maste 	}
141*c43e99fdSEd Maste 
142*c43e99fdSEd Maste 	ok_so_far = X509_verify_cert(x509_ctx);
143*c43e99fdSEd Maste 
144*c43e99fdSEd Maste 	server_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
145*c43e99fdSEd Maste 
146*c43e99fdSEd Maste 	if (ok_so_far) {
147*c43e99fdSEd Maste 		res = validate_hostname(host, server_cert);
148*c43e99fdSEd Maste 
149*c43e99fdSEd Maste 		switch (res) {
150*c43e99fdSEd Maste 		case MatchFound:
151*c43e99fdSEd Maste 			res_str = "MatchFound";
152*c43e99fdSEd Maste 			break;
153*c43e99fdSEd Maste 		case MatchNotFound:
154*c43e99fdSEd Maste 			res_str = "MatchNotFound";
155*c43e99fdSEd Maste 			break;
156*c43e99fdSEd Maste 		case NoSANPresent:
157*c43e99fdSEd Maste 			res_str = "NoSANPresent";
158*c43e99fdSEd Maste 			break;
159*c43e99fdSEd Maste 		case MalformedCertificate:
160*c43e99fdSEd Maste 			res_str = "MalformedCertificate";
161*c43e99fdSEd Maste 			break;
162*c43e99fdSEd Maste 		case Error:
163*c43e99fdSEd Maste 			res_str = "Error";
164*c43e99fdSEd Maste 			break;
165*c43e99fdSEd Maste 		default:
166*c43e99fdSEd Maste 			res_str = "WTF!";
167*c43e99fdSEd Maste 			break;
168*c43e99fdSEd Maste 		}
169*c43e99fdSEd Maste 	}
170*c43e99fdSEd Maste 
171*c43e99fdSEd Maste 	X509_NAME_oneline(X509_get_subject_name (server_cert),
172*c43e99fdSEd Maste 			  cert_str, sizeof (cert_str));
173*c43e99fdSEd Maste 
174*c43e99fdSEd Maste 	if (res == MatchFound) {
175*c43e99fdSEd Maste 		printf("https server '%s' has this certificate, "
176*c43e99fdSEd Maste 		       "which looks good to me:\n%s\n",
177*c43e99fdSEd Maste 		       host, cert_str);
178*c43e99fdSEd Maste 		return 1;
179*c43e99fdSEd Maste 	} else {
180*c43e99fdSEd Maste 		printf("Got '%s' for hostname '%s' and certificate:\n%s\n",
181*c43e99fdSEd Maste 		       res_str, host, cert_str);
182*c43e99fdSEd Maste 		return 0;
183*c43e99fdSEd Maste 	}
184*c43e99fdSEd Maste }
185*c43e99fdSEd Maste #endif
186*c43e99fdSEd Maste 
187*c43e99fdSEd Maste int
main(int argc,char ** argv)188*c43e99fdSEd Maste main(int argc, char **argv)
189*c43e99fdSEd Maste {
190*c43e99fdSEd Maste 	int r;
191*c43e99fdSEd Maste 
192*c43e99fdSEd Maste 	struct evhttp_uri *http_uri = NULL;
193*c43e99fdSEd Maste 	const char *url = NULL, *data_file = NULL;
194*c43e99fdSEd Maste 	const char *crt = "/etc/ssl/certs/ca-certificates.crt";
195*c43e99fdSEd Maste 	const char *scheme, *host, *path, *query;
196*c43e99fdSEd Maste 	char uri[256];
197*c43e99fdSEd Maste 	int port;
198*c43e99fdSEd Maste 	int retries = 0;
199*c43e99fdSEd Maste 	int timeout = -1;
200*c43e99fdSEd Maste 
201*c43e99fdSEd Maste 	SSL_CTX *ssl_ctx = NULL;
202*c43e99fdSEd Maste 	SSL *ssl = NULL;
203*c43e99fdSEd Maste 	struct bufferevent *bev;
204*c43e99fdSEd Maste 	struct evhttp_connection *evcon = NULL;
205*c43e99fdSEd Maste 	struct evhttp_request *req;
206*c43e99fdSEd Maste 	struct evkeyvalq *output_headers;
207*c43e99fdSEd Maste 	struct evbuffer *output_buffer;
208*c43e99fdSEd Maste 
209*c43e99fdSEd Maste 	int i;
210*c43e99fdSEd Maste 	int ret = 0;
211*c43e99fdSEd Maste 	enum { HTTP, HTTPS } type = HTTP;
212*c43e99fdSEd Maste 
213*c43e99fdSEd Maste 	for (i = 1; i < argc; i++) {
214*c43e99fdSEd Maste 		if (!strcmp("-url", argv[i])) {
215*c43e99fdSEd Maste 			if (i < argc - 1) {
216*c43e99fdSEd Maste 				url = argv[i + 1];
217*c43e99fdSEd Maste 			} else {
218*c43e99fdSEd Maste 				syntax();
219*c43e99fdSEd Maste 				goto error;
220*c43e99fdSEd Maste 			}
221*c43e99fdSEd Maste 		} else if (!strcmp("-crt", argv[i])) {
222*c43e99fdSEd Maste 			if (i < argc - 1) {
223*c43e99fdSEd Maste 				crt = argv[i + 1];
224*c43e99fdSEd Maste 			} else {
225*c43e99fdSEd Maste 				syntax();
226*c43e99fdSEd Maste 				goto error;
227*c43e99fdSEd Maste 			}
228*c43e99fdSEd Maste 		} else if (!strcmp("-ignore-cert", argv[i])) {
229*c43e99fdSEd Maste 			ignore_cert = 1;
230*c43e99fdSEd Maste 		} else if (!strcmp("-data", argv[i])) {
231*c43e99fdSEd Maste 			if (i < argc - 1) {
232*c43e99fdSEd Maste 				data_file = argv[i + 1];
233*c43e99fdSEd Maste 			} else {
234*c43e99fdSEd Maste 				syntax();
235*c43e99fdSEd Maste 				goto error;
236*c43e99fdSEd Maste 			}
237*c43e99fdSEd Maste 		} else if (!strcmp("-retries", argv[i])) {
238*c43e99fdSEd Maste 			if (i < argc - 1) {
239*c43e99fdSEd Maste 				retries = atoi(argv[i + 1]);
240*c43e99fdSEd Maste 			} else {
241*c43e99fdSEd Maste 				syntax();
242*c43e99fdSEd Maste 				goto error;
243*c43e99fdSEd Maste 			}
244*c43e99fdSEd Maste 		} else if (!strcmp("-timeout", argv[i])) {
245*c43e99fdSEd Maste 			if (i < argc - 1) {
246*c43e99fdSEd Maste 				timeout = atoi(argv[i + 1]);
247*c43e99fdSEd Maste 			} else {
248*c43e99fdSEd Maste 				syntax();
249*c43e99fdSEd Maste 				goto error;
250*c43e99fdSEd Maste 			}
251*c43e99fdSEd Maste 		} else if (!strcmp("-help", argv[i])) {
252*c43e99fdSEd Maste 			syntax();
253*c43e99fdSEd Maste 			goto error;
254*c43e99fdSEd Maste 		}
255*c43e99fdSEd Maste 	}
256*c43e99fdSEd Maste 
257*c43e99fdSEd Maste 	if (!url) {
258*c43e99fdSEd Maste 		syntax();
259*c43e99fdSEd Maste 		goto error;
260*c43e99fdSEd Maste 	}
261*c43e99fdSEd Maste 
262*c43e99fdSEd Maste #ifdef _WIN32
263*c43e99fdSEd Maste 	{
264*c43e99fdSEd Maste 		WORD wVersionRequested;
265*c43e99fdSEd Maste 		WSADATA wsaData;
266*c43e99fdSEd Maste 		int err;
267*c43e99fdSEd Maste 
268*c43e99fdSEd Maste 		wVersionRequested = MAKEWORD(2, 2);
269*c43e99fdSEd Maste 
270*c43e99fdSEd Maste 		err = WSAStartup(wVersionRequested, &wsaData);
271*c43e99fdSEd Maste 		if (err != 0) {
272*c43e99fdSEd Maste 			printf("WSAStartup failed with error: %d\n", err);
273*c43e99fdSEd Maste 			goto error;
274*c43e99fdSEd Maste 		}
275*c43e99fdSEd Maste 	}
276*c43e99fdSEd Maste #endif // _WIN32
277*c43e99fdSEd Maste 
278*c43e99fdSEd Maste 	http_uri = evhttp_uri_parse(url);
279*c43e99fdSEd Maste 	if (http_uri == NULL) {
280*c43e99fdSEd Maste 		err("malformed url");
281*c43e99fdSEd Maste 		goto error;
282*c43e99fdSEd Maste 	}
283*c43e99fdSEd Maste 
284*c43e99fdSEd Maste 	scheme = evhttp_uri_get_scheme(http_uri);
285*c43e99fdSEd Maste 	if (scheme == NULL || (strcasecmp(scheme, "https") != 0 &&
286*c43e99fdSEd Maste 	                       strcasecmp(scheme, "http") != 0)) {
287*c43e99fdSEd Maste 		err("url must be http or https");
288*c43e99fdSEd Maste 		goto error;
289*c43e99fdSEd Maste 	}
290*c43e99fdSEd Maste 
291*c43e99fdSEd Maste 	host = evhttp_uri_get_host(http_uri);
292*c43e99fdSEd Maste 	if (host == NULL) {
293*c43e99fdSEd Maste 		err("url must have a host");
294*c43e99fdSEd Maste 		goto error;
295*c43e99fdSEd Maste 	}
296*c43e99fdSEd Maste 
297*c43e99fdSEd Maste 	port = evhttp_uri_get_port(http_uri);
298*c43e99fdSEd Maste 	if (port == -1) {
299*c43e99fdSEd Maste 		port = (strcasecmp(scheme, "http") == 0) ? 80 : 443;
300*c43e99fdSEd Maste 	}
301*c43e99fdSEd Maste 
302*c43e99fdSEd Maste 	path = evhttp_uri_get_path(http_uri);
303*c43e99fdSEd Maste 	if (strlen(path) == 0) {
304*c43e99fdSEd Maste 		path = "/";
305*c43e99fdSEd Maste 	}
306*c43e99fdSEd Maste 
307*c43e99fdSEd Maste 	query = evhttp_uri_get_query(http_uri);
308*c43e99fdSEd Maste 	if (query == NULL) {
309*c43e99fdSEd Maste 		snprintf(uri, sizeof(uri) - 1, "%s", path);
310*c43e99fdSEd Maste 	} else {
311*c43e99fdSEd Maste 		snprintf(uri, sizeof(uri) - 1, "%s?%s", path, query);
312*c43e99fdSEd Maste 	}
313*c43e99fdSEd Maste 	uri[sizeof(uri) - 1] = '\0';
314*c43e99fdSEd Maste 
315*c43e99fdSEd Maste #if OPENSSL_VERSION_NUMBER < 0x10100000L
316*c43e99fdSEd Maste 	// Initialize OpenSSL
317*c43e99fdSEd Maste 	SSL_library_init();
318*c43e99fdSEd Maste 	ERR_load_crypto_strings();
319*c43e99fdSEd Maste 	SSL_load_error_strings();
320*c43e99fdSEd Maste 	OpenSSL_add_all_algorithms();
321*c43e99fdSEd Maste #endif
322*c43e99fdSEd Maste 
323*c43e99fdSEd Maste 	/* This isn't strictly necessary... OpenSSL performs RAND_poll
324*c43e99fdSEd Maste 	 * automatically on first use of random number generator. */
325*c43e99fdSEd Maste 	r = RAND_poll();
326*c43e99fdSEd Maste 	if (r == 0) {
327*c43e99fdSEd Maste 		err_openssl("RAND_poll");
328*c43e99fdSEd Maste 		goto error;
329*c43e99fdSEd Maste 	}
330*c43e99fdSEd Maste 
331*c43e99fdSEd Maste 	/* Create a new OpenSSL context */
332*c43e99fdSEd Maste 	ssl_ctx = SSL_CTX_new(SSLv23_method());
333*c43e99fdSEd Maste 	if (!ssl_ctx) {
334*c43e99fdSEd Maste 		err_openssl("SSL_CTX_new");
335*c43e99fdSEd Maste 		goto error;
336*c43e99fdSEd Maste 	}
337*c43e99fdSEd Maste 
338*c43e99fdSEd Maste #ifndef _WIN32
339*c43e99fdSEd Maste 	/* TODO: Add certificate loading on Windows as well */
340*c43e99fdSEd Maste 
341*c43e99fdSEd Maste 	/* Attempt to use the system's trusted root certificates.
342*c43e99fdSEd Maste 	 * (This path is only valid for Debian-based systems.) */
343*c43e99fdSEd Maste 	if (1 != SSL_CTX_load_verify_locations(ssl_ctx, crt, NULL)) {
344*c43e99fdSEd Maste 		err_openssl("SSL_CTX_load_verify_locations");
345*c43e99fdSEd Maste 		goto error;
346*c43e99fdSEd Maste 	}
347*c43e99fdSEd Maste 	/* Ask OpenSSL to verify the server certificate.  Note that this
348*c43e99fdSEd Maste 	 * does NOT include verifying that the hostname is correct.
349*c43e99fdSEd Maste 	 * So, by itself, this means anyone with any legitimate
350*c43e99fdSEd Maste 	 * CA-issued certificate for any website, can impersonate any
351*c43e99fdSEd Maste 	 * other website in the world.  This is not good.  See "The
352*c43e99fdSEd Maste 	 * Most Dangerous Code in the World" article at
353*c43e99fdSEd Maste 	 * https://crypto.stanford.edu/~dabo/pubs/abstracts/ssl-client-bugs.html
354*c43e99fdSEd Maste 	 */
355*c43e99fdSEd Maste 	SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
356*c43e99fdSEd Maste 	/* This is how we solve the problem mentioned in the previous
357*c43e99fdSEd Maste 	 * comment.  We "wrap" OpenSSL's validation routine in our
358*c43e99fdSEd Maste 	 * own routine, which also validates the hostname by calling
359*c43e99fdSEd Maste 	 * the code provided by iSECPartners.  Note that even though
360*c43e99fdSEd Maste 	 * the "Everything You've Always Wanted to Know About
361*c43e99fdSEd Maste 	 * Certificate Validation With OpenSSL (But Were Afraid to
362*c43e99fdSEd Maste 	 * Ask)" paper from iSECPartners says very explicitly not to
363*c43e99fdSEd Maste 	 * call SSL_CTX_set_cert_verify_callback (at the bottom of
364*c43e99fdSEd Maste 	 * page 2), what we're doing here is safe because our
365*c43e99fdSEd Maste 	 * cert_verify_callback() calls X509_verify_cert(), which is
366*c43e99fdSEd Maste 	 * OpenSSL's built-in routine which would have been called if
367*c43e99fdSEd Maste 	 * we hadn't set the callback.  Therefore, we're just
368*c43e99fdSEd Maste 	 * "wrapping" OpenSSL's routine, not replacing it. */
369*c43e99fdSEd Maste 	SSL_CTX_set_cert_verify_callback(ssl_ctx, cert_verify_callback,
370*c43e99fdSEd Maste 					  (void *) host);
371*c43e99fdSEd Maste #else // _WIN32
372*c43e99fdSEd Maste 	(void)crt;
373*c43e99fdSEd Maste #endif // _WIN32
374*c43e99fdSEd Maste 
375*c43e99fdSEd Maste 	// Create event base
376*c43e99fdSEd Maste 	base = event_base_new();
377*c43e99fdSEd Maste 	if (!base) {
378*c43e99fdSEd Maste 		perror("event_base_new()");
379*c43e99fdSEd Maste 		goto error;
380*c43e99fdSEd Maste 	}
381*c43e99fdSEd Maste 
382*c43e99fdSEd Maste 	// Create OpenSSL bufferevent and stack evhttp on top of it
383*c43e99fdSEd Maste 	ssl = SSL_new(ssl_ctx);
384*c43e99fdSEd Maste 	if (ssl == NULL) {
385*c43e99fdSEd Maste 		err_openssl("SSL_new()");
386*c43e99fdSEd Maste 		goto error;
387*c43e99fdSEd Maste 	}
388*c43e99fdSEd Maste 
389*c43e99fdSEd Maste 	#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
390*c43e99fdSEd Maste 	// Set hostname for SNI extension
391*c43e99fdSEd Maste 	SSL_set_tlsext_host_name(ssl, host);
392*c43e99fdSEd Maste 	#endif
393*c43e99fdSEd Maste 
394*c43e99fdSEd Maste 	if (strcasecmp(scheme, "http") == 0) {
395*c43e99fdSEd Maste 		bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
396*c43e99fdSEd Maste 	} else {
397*c43e99fdSEd Maste 		type = HTTPS;
398*c43e99fdSEd Maste 		bev = bufferevent_openssl_socket_new(base, -1, ssl,
399*c43e99fdSEd Maste 			BUFFEREVENT_SSL_CONNECTING,
400*c43e99fdSEd Maste 			BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
401*c43e99fdSEd Maste 	}
402*c43e99fdSEd Maste 
403*c43e99fdSEd Maste 	if (bev == NULL) {
404*c43e99fdSEd Maste 		fprintf(stderr, "bufferevent_openssl_socket_new() failed\n");
405*c43e99fdSEd Maste 		goto error;
406*c43e99fdSEd Maste 	}
407*c43e99fdSEd Maste 
408*c43e99fdSEd Maste 	bufferevent_openssl_set_allow_dirty_shutdown(bev, 1);
409*c43e99fdSEd Maste 
410*c43e99fdSEd Maste 	// For simplicity, we let DNS resolution block. Everything else should be
411*c43e99fdSEd Maste 	// asynchronous though.
412*c43e99fdSEd Maste 	evcon = evhttp_connection_base_bufferevent_new(base, NULL, bev,
413*c43e99fdSEd Maste 		host, port);
414*c43e99fdSEd Maste 	if (evcon == NULL) {
415*c43e99fdSEd Maste 		fprintf(stderr, "evhttp_connection_base_bufferevent_new() failed\n");
416*c43e99fdSEd Maste 		goto error;
417*c43e99fdSEd Maste 	}
418*c43e99fdSEd Maste 
419*c43e99fdSEd Maste 	if (retries > 0) {
420*c43e99fdSEd Maste 		evhttp_connection_set_retries(evcon, retries);
421*c43e99fdSEd Maste 	}
422*c43e99fdSEd Maste 	if (timeout >= 0) {
423*c43e99fdSEd Maste 		evhttp_connection_set_timeout(evcon, timeout);
424*c43e99fdSEd Maste 	}
425*c43e99fdSEd Maste 
426*c43e99fdSEd Maste 	// Fire off the request
427*c43e99fdSEd Maste 	req = evhttp_request_new(http_request_done, bev);
428*c43e99fdSEd Maste 	if (req == NULL) {
429*c43e99fdSEd Maste 		fprintf(stderr, "evhttp_request_new() failed\n");
430*c43e99fdSEd Maste 		goto error;
431*c43e99fdSEd Maste 	}
432*c43e99fdSEd Maste 
433*c43e99fdSEd Maste 	output_headers = evhttp_request_get_output_headers(req);
434*c43e99fdSEd Maste 	evhttp_add_header(output_headers, "Host", host);
435*c43e99fdSEd Maste 	evhttp_add_header(output_headers, "Connection", "close");
436*c43e99fdSEd Maste 
437*c43e99fdSEd Maste 	if (data_file) {
438*c43e99fdSEd Maste 		/* NOTE: In production code, you'd probably want to use
439*c43e99fdSEd Maste 		 * evbuffer_add_file() or evbuffer_add_file_segment(), to
440*c43e99fdSEd Maste 		 * avoid needless copying. */
441*c43e99fdSEd Maste 		FILE * f = fopen(data_file, "rb");
442*c43e99fdSEd Maste 		char buf[1024];
443*c43e99fdSEd Maste 		size_t s;
444*c43e99fdSEd Maste 		size_t bytes = 0;
445*c43e99fdSEd Maste 
446*c43e99fdSEd Maste 		if (!f) {
447*c43e99fdSEd Maste 			syntax();
448*c43e99fdSEd Maste 			goto error;
449*c43e99fdSEd Maste 		}
450*c43e99fdSEd Maste 
451*c43e99fdSEd Maste 		output_buffer = evhttp_request_get_output_buffer(req);
452*c43e99fdSEd Maste 		while ((s = fread(buf, 1, sizeof(buf), f)) > 0) {
453*c43e99fdSEd Maste 			evbuffer_add(output_buffer, buf, s);
454*c43e99fdSEd Maste 			bytes += s;
455*c43e99fdSEd Maste 		}
456*c43e99fdSEd Maste 		evutil_snprintf(buf, sizeof(buf)-1, "%lu", (unsigned long)bytes);
457*c43e99fdSEd Maste 		evhttp_add_header(output_headers, "Content-Length", buf);
458*c43e99fdSEd Maste 		fclose(f);
459*c43e99fdSEd Maste 	}
460*c43e99fdSEd Maste 
461*c43e99fdSEd Maste 	r = evhttp_make_request(evcon, req, data_file ? EVHTTP_REQ_POST : EVHTTP_REQ_GET, uri);
462*c43e99fdSEd Maste 	if (r != 0) {
463*c43e99fdSEd Maste 		fprintf(stderr, "evhttp_make_request() failed\n");
464*c43e99fdSEd Maste 		goto error;
465*c43e99fdSEd Maste 	}
466*c43e99fdSEd Maste 
467*c43e99fdSEd Maste 	event_base_dispatch(base);
468*c43e99fdSEd Maste 	goto cleanup;
469*c43e99fdSEd Maste 
470*c43e99fdSEd Maste error:
471*c43e99fdSEd Maste 	ret = 1;
472*c43e99fdSEd Maste cleanup:
473*c43e99fdSEd Maste 	if (evcon)
474*c43e99fdSEd Maste 		evhttp_connection_free(evcon);
475*c43e99fdSEd Maste 	if (http_uri)
476*c43e99fdSEd Maste 		evhttp_uri_free(http_uri);
477*c43e99fdSEd Maste 	event_base_free(base);
478*c43e99fdSEd Maste 
479*c43e99fdSEd Maste 	if (ssl_ctx)
480*c43e99fdSEd Maste 		SSL_CTX_free(ssl_ctx);
481*c43e99fdSEd Maste 	if (type == HTTP && ssl)
482*c43e99fdSEd Maste 		SSL_free(ssl);
483*c43e99fdSEd Maste #if OPENSSL_VERSION_NUMBER < 0x10100000L
484*c43e99fdSEd Maste 	EVP_cleanup();
485*c43e99fdSEd Maste 	ERR_free_strings();
486*c43e99fdSEd Maste 
487*c43e99fdSEd Maste #ifdef EVENT__HAVE_ERR_REMOVE_THREAD_STATE
488*c43e99fdSEd Maste 	ERR_remove_thread_state(NULL);
489*c43e99fdSEd Maste #else
490*c43e99fdSEd Maste 	ERR_remove_state(0);
491*c43e99fdSEd Maste #endif
492*c43e99fdSEd Maste 	CRYPTO_cleanup_all_ex_data();
493*c43e99fdSEd Maste 
494*c43e99fdSEd Maste 	sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
495*c43e99fdSEd Maste #endif /*OPENSSL_VERSION_NUMBER < 0x10100000L */
496*c43e99fdSEd Maste 
497*c43e99fdSEd Maste #ifdef _WIN32
498*c43e99fdSEd Maste 	WSACleanup();
499*c43e99fdSEd Maste #endif
500*c43e99fdSEd Maste 
501*c43e99fdSEd Maste 	return ret;
502*c43e99fdSEd Maste }
503