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