2 * Testing tool for TLSv1 client routines using HTTPS
3 * Copyright (c) 2011, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #include "crypto/tls.h"
16 static void https_tls_event_cb(void *ctx, enum tls_event ev,
17 union tls_event_data *data)
19 wpa_printf(MSG_DEBUG, "HTTPS: TLS event %d", ev);
23 static struct wpabuf * https_recv(int s)
30 in = wpabuf_alloc(20000);
39 wpa_printf(MSG_DEBUG, "Waiting for more data");
40 ret = select(s + 1, &rfds, NULL, NULL, &tv);
42 wpa_printf(MSG_ERROR, "select: %s", strerror(errno));
48 wpa_printf(MSG_INFO, "Timeout on waiting for data");
53 len = recv(s, wpabuf_put(in, 0), wpabuf_tailroom(in), 0);
55 wpa_printf(MSG_ERROR, "recv: %s", strerror(errno));
60 wpa_printf(MSG_DEBUG, "No more data available");
64 wpa_printf(MSG_DEBUG, "Received %d bytes", len);
71 static int https_client(int s, const char *path)
73 struct tls_config conf;
75 struct tls_connection *conn;
76 struct wpabuf *in, *out, *appl;
80 os_memset(&conf, 0, sizeof(conf));
81 conf.event_cb = https_tls_event_cb;
82 tls = tls_init(&conf);
86 conn = tls_connection_init(tls);
96 out = tls_connection_handshake2(tls, conn, in, &appl,
105 if (tls_connection_get_failed(tls, conn)) {
106 wpa_printf(MSG_ERROR, "TLS handshake failed");
109 if (tls_connection_established(tls, conn))
111 wpa_printf(MSG_DEBUG, "Sending %d bytes",
112 (int) wpabuf_len(out));
113 if (send(s, wpabuf_head(out), wpabuf_len(out), 0) < 0) {
114 wpa_printf(MSG_ERROR, "send: %s", strerror(errno));
128 wpa_printf(MSG_INFO, "TLS connection established");
130 wpa_hexdump_buf(MSG_DEBUG, "Received application data", appl);
132 in = wpabuf_alloc(100 + os_strlen(path));
135 wpabuf_put_str(in, "GET ");
136 wpabuf_put_str(in, path);
137 wpabuf_put_str(in, " HTTP/1.0\r\n\r\n");
138 out = tls_connection_encrypt(tls, conn, in);
144 wpa_printf(MSG_INFO, "Sending HTTP request: %d bytes",
145 (int) wpabuf_len(out));
146 if (send(s, wpabuf_head(out), wpabuf_len(out), 0) < 0) {
147 wpa_printf(MSG_ERROR, "send: %s", strerror(errno));
153 wpa_printf(MSG_INFO, "Reading HTTP response");
159 out = tls_connection_decrypt2(tls, conn, in, &need_more_data);
161 wpa_printf(MSG_DEBUG, "HTTP: Need more data");
166 wpa_hexdump_ascii(MSG_INFO, "Response", wpabuf_head(out),
177 tls_connection_deinit(tls, conn);
184 int main(int argc, char *argv[])
186 struct addrinfo hints, *result, *rp;
190 wpa_debug_show_keys = 1;
193 wpa_printf(MSG_INFO, "usage: test-https server port path");
197 os_memset(&hints, 0, sizeof(hints));
198 hints.ai_family = AF_UNSPEC;
199 hints.ai_socktype = SOCK_STREAM;
200 res = getaddrinfo(argv[1], argv[2], &hints, &result);
202 wpa_printf(MSG_ERROR, "getaddrinfo: %s", gai_strerror(res));
206 for (rp = result; rp; rp = rp->ai_next) {
207 s = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
210 if (connect(s, rp->ai_addr, rp->ai_addrlen) == 0)
214 freeaddrinfo(result);
217 wpa_printf(MSG_ERROR, "Could not connect");
221 https_client(s, argv[3]);