2 This is an example of how to hook up evhttp with bufferevent_ssl
4 It just GETs an https URL given on the command-line and prints the response
7 Actually, it also accepts plain http URLs to make it easy to compare http vs
10 Loosely based on le-proxy.c.
13 // Get rid of OSX 10.7 and greater deprecation warnings.
14 #if defined(__APPLE__) && defined(__clang__)
15 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
28 #define snprintf _snprintf
29 #define strcasecmp _stricmp
31 #include <sys/socket.h>
32 #include <netinet/in.h>
35 #include <event2/bufferevent_ssl.h>
36 #include <event2/bufferevent.h>
37 #include <event2/buffer.h>
38 #include <event2/listener.h>
39 #include <event2/util.h>
40 #include <event2/http.h>
42 #include <openssl/ssl.h>
43 #include <openssl/err.h>
44 #include <openssl/rand.h>
46 #include "openssl_hostname_validation.h"
48 static int ignore_cert = 0;
51 http_request_done(struct evhttp_request *req, void *ctx)
57 /* If req is NULL, it means an error occurred, but
58 * sadly we are mostly left guessing what the error
59 * might have been. We'll do our best... */
60 struct bufferevent *bev = (struct bufferevent *) ctx;
63 int errcode = EVUTIL_SOCKET_ERROR();
64 fprintf(stderr, "some request failed - no idea which one though!\n");
65 /* Print out the OpenSSL error queue that libevent
66 * squirreled away for us, if any. */
67 while ((oslerr = bufferevent_get_openssl_error(bev))) {
68 ERR_error_string_n(oslerr, buffer, sizeof(buffer));
69 fprintf(stderr, "%s\n", buffer);
72 /* If the OpenSSL error queue was empty, maybe it was a
73 * socket error; let's try printing that. */
75 fprintf(stderr, "socket error = %s (%d)\n",
76 evutil_socket_error_to_string(errcode),
81 fprintf(stderr, "Response line: %d %s\n",
82 evhttp_request_get_response_code(req),
83 evhttp_request_get_response_code_line(req));
85 while ((nread = evbuffer_remove(evhttp_request_get_input_buffer(req),
86 buffer, sizeof(buffer)))
88 /* These are just arbitrary chunks of 256 bytes.
89 * They are not lines, so we can't treat them as such. */
90 fwrite(buffer, nread, 1, stdout);
97 fputs("Syntax:\n", stderr);
98 fputs(" https-client -url <https-url> [-data data-file.bin] [-ignore-cert] [-retries num] [-timeout sec] [-crt crt]\n", stderr);
99 fputs("Example:\n", stderr);
100 fputs(" https-client -url https://ip.appspot.com/\n", stderr);
110 err_openssl(const char *func)
112 fprintf (stderr, "%s failed:\n", func);
114 /* This is the OpenSSL function that prints the contents of the
115 * error stack to the specified file handle. */
116 ERR_print_errors_fp (stderr);
122 /* See http://archives.seul.org/libevent/users/Jan-2013/msg00039.html */
123 static int cert_verify_callback(X509_STORE_CTX *x509_ctx, void *arg)
126 const char *host = (const char *) arg;
127 const char *res_str = "X509_verify_cert failed";
128 HostnameValidationResult res = Error;
130 /* This is the function that OpenSSL would call if we hadn't called
131 * SSL_CTX_set_cert_verify_callback(). Therefore, we are "wrapping"
132 * the default functionality, rather than replacing it. */
135 X509 *server_cert = NULL;
141 ok_so_far = X509_verify_cert(x509_ctx);
143 server_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
146 res = validate_hostname(host, server_cert);
150 res_str = "MatchFound";
153 res_str = "MatchNotFound";
156 res_str = "NoSANPresent";
158 case MalformedCertificate:
159 res_str = "MalformedCertificate";
170 X509_NAME_oneline(X509_get_subject_name (server_cert),
171 cert_str, sizeof (cert_str));
173 if (res == MatchFound) {
174 printf("https server '%s' has this certificate, "
175 "which looks good to me:\n%s\n",
179 printf("Got '%s' for hostname '%s' and certificate:\n%s\n",
180 res_str, host, cert_str);
187 main(int argc, char **argv)
190 struct event_base *base = NULL;
191 struct evhttp_uri *http_uri = NULL;
192 const char *url = NULL, *data_file = NULL;
193 const char *crt = NULL;
194 const char *scheme, *host, *path, *query;
200 SSL_CTX *ssl_ctx = NULL;
202 struct bufferevent *bev;
203 struct evhttp_connection *evcon = NULL;
204 struct evhttp_request *req;
205 struct evkeyvalq *output_headers;
206 struct evbuffer *output_buffer;
210 enum { HTTP, HTTPS } type = HTTP;
212 for (i = 1; i < argc; i++) {
213 if (!strcmp("-url", argv[i])) {
220 } else if (!strcmp("-crt", argv[i])) {
227 } else if (!strcmp("-ignore-cert", argv[i])) {
229 } else if (!strcmp("-data", argv[i])) {
231 data_file = argv[i + 1];
236 } else if (!strcmp("-retries", argv[i])) {
238 retries = atoi(argv[i + 1]);
243 } else if (!strcmp("-timeout", argv[i])) {
245 timeout = atoi(argv[i + 1]);
250 } else if (!strcmp("-help", argv[i])) {
263 WORD wVersionRequested;
267 wVersionRequested = MAKEWORD(2, 2);
269 err = WSAStartup(wVersionRequested, &wsaData);
271 printf("WSAStartup failed with error: %d\n", err);
277 http_uri = evhttp_uri_parse(url);
278 if (http_uri == NULL) {
279 err("malformed url");
283 scheme = evhttp_uri_get_scheme(http_uri);
284 if (scheme == NULL || (strcasecmp(scheme, "https") != 0 &&
285 strcasecmp(scheme, "http") != 0)) {
286 err("url must be http or https");
290 host = evhttp_uri_get_host(http_uri);
292 err("url must have a host");
296 port = evhttp_uri_get_port(http_uri);
298 port = (strcasecmp(scheme, "http") == 0) ? 80 : 443;
301 path = evhttp_uri_get_path(http_uri);
302 if (strlen(path) == 0) {
306 query = evhttp_uri_get_query(http_uri);
308 snprintf(uri, sizeof(uri) - 1, "%s", path);
310 snprintf(uri, sizeof(uri) - 1, "%s?%s", path, query);
312 uri[sizeof(uri) - 1] = '\0';
314 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
315 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
316 // Initialize OpenSSL
318 ERR_load_crypto_strings();
319 SSL_load_error_strings();
320 OpenSSL_add_all_algorithms();
323 /* This isn't strictly necessary... OpenSSL performs RAND_poll
324 * automatically on first use of random number generator. */
327 err_openssl("RAND_poll");
331 /* Create a new OpenSSL context */
332 ssl_ctx = SSL_CTX_new(SSLv23_method());
334 err_openssl("SSL_CTX_new");
339 /* TODO: Add certificate loading on Windows as well */
343 /* Attempt to use the system's trusted root certificates. */
344 store = SSL_CTX_get_cert_store(ssl_ctx);
345 if (X509_STORE_set_default_paths(store) != 1) {
346 err_openssl("X509_STORE_set_default_paths");
350 if (SSL_CTX_load_verify_locations(ssl_ctx, crt, NULL) != 1) {
351 err_openssl("SSL_CTX_load_verify_locations");
355 /* Ask OpenSSL to verify the server certificate. Note that this
356 * does NOT include verifying that the hostname is correct.
357 * So, by itself, this means anyone with any legitimate
358 * CA-issued certificate for any website, can impersonate any
359 * other website in the world. This is not good. See "The
360 * Most Dangerous Code in the World" article at
361 * https://crypto.stanford.edu/~dabo/pubs/abstracts/ssl-client-bugs.html
363 SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
364 /* This is how we solve the problem mentioned in the previous
365 * comment. We "wrap" OpenSSL's validation routine in our
366 * own routine, which also validates the hostname by calling
367 * the code provided by iSECPartners. Note that even though
368 * the "Everything You've Always Wanted to Know About
369 * Certificate Validation With OpenSSL (But Were Afraid to
370 * Ask)" paper from iSECPartners says very explicitly not to
371 * call SSL_CTX_set_cert_verify_callback (at the bottom of
372 * page 2), what we're doing here is safe because our
373 * cert_verify_callback() calls X509_verify_cert(), which is
374 * OpenSSL's built-in routine which would have been called if
375 * we hadn't set the callback. Therefore, we're just
376 * "wrapping" OpenSSL's routine, not replacing it. */
377 SSL_CTX_set_cert_verify_callback(ssl_ctx, cert_verify_callback,
384 base = event_base_new();
386 perror("event_base_new()");
390 // Create OpenSSL bufferevent and stack evhttp on top of it
391 ssl = SSL_new(ssl_ctx);
393 err_openssl("SSL_new()");
397 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
398 // Set hostname for SNI extension
399 SSL_set_tlsext_host_name(ssl, host);
402 if (strcasecmp(scheme, "http") == 0) {
403 bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
406 bev = bufferevent_openssl_socket_new(base, -1, ssl,
407 BUFFEREVENT_SSL_CONNECTING,
408 BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
412 fprintf(stderr, "bufferevent_openssl_socket_new() failed\n");
416 bufferevent_openssl_set_allow_dirty_shutdown(bev, 1);
418 // For simplicity, we let DNS resolution block. Everything else should be
419 // asynchronous though.
420 evcon = evhttp_connection_base_bufferevent_new(base, NULL, bev,
423 fprintf(stderr, "evhttp_connection_base_bufferevent_new() failed\n");
428 evhttp_connection_set_retries(evcon, retries);
431 evhttp_connection_set_timeout(evcon, timeout);
434 // Fire off the request
435 req = evhttp_request_new(http_request_done, bev);
437 fprintf(stderr, "evhttp_request_new() failed\n");
441 output_headers = evhttp_request_get_output_headers(req);
442 evhttp_add_header(output_headers, "Host", host);
443 evhttp_add_header(output_headers, "Connection", "close");
446 /* NOTE: In production code, you'd probably want to use
447 * evbuffer_add_file() or evbuffer_add_file_segment(), to
448 * avoid needless copying. */
449 FILE * f = fopen(data_file, "rb");
459 output_buffer = evhttp_request_get_output_buffer(req);
460 while ((s = fread(buf, 1, sizeof(buf), f)) > 0) {
461 evbuffer_add(output_buffer, buf, s);
464 evutil_snprintf(buf, sizeof(buf)-1, "%lu", (unsigned long)bytes);
465 evhttp_add_header(output_headers, "Content-Length", buf);
469 r = evhttp_make_request(evcon, req, data_file ? EVHTTP_REQ_POST : EVHTTP_REQ_GET, uri);
471 fprintf(stderr, "evhttp_make_request() failed\n");
475 event_base_dispatch(base);
482 evhttp_connection_free(evcon);
484 evhttp_uri_free(http_uri);
486 event_base_free(base);
489 SSL_CTX_free(ssl_ctx);
490 if (type == HTTP && ssl)
492 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
493 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
497 #if OPENSSL_VERSION_NUMBER < 0x10000000L
500 ERR_remove_thread_state(NULL);
503 CRYPTO_cleanup_all_ex_data();
505 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
506 #endif /* (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
507 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L) */