aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test-https.c
blob: a72e56f9d21c7016a6afc3590519f8f2bdf85a35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Testing tool for TLSv1 client routines using HTTPS
 * Copyright (c) 2011, Jouni Malinen <j@w1.fi>
 *
 * This software may be distributed under the terms of the BSD license.
 * See README for more details.
 */

#include "includes.h"
#include <netdb.h>

#include "common.h"
#include "crypto/tls.h"


static void https_tls_event_cb(void *ctx, enum tls_event ev,
			       union tls_event_data *data)
{
	wpa_printf(MSG_DEBUG, "HTTPS: TLS event %d", ev);
}


static struct wpabuf * https_recv(int s)
{
	struct wpabuf *in;
	int len, ret;
	fd_set rfds;
	struct timeval tv;

	in = wpabuf_alloc(20000);
	if (in == NULL)
		return NULL;

	FD_ZERO(&rfds);
	FD_SET(s, &rfds);
	tv.tv_sec = 5;
	tv.tv_usec = 0;

	wpa_printf(MSG_DEBUG, "Waiting for more data");
	ret = select(s + 1, &rfds, NULL, NULL, &tv);
	if (ret < 0) {
		wpa_printf(MSG_ERROR, "select: %s", strerror(errno));
		wpabuf_free(in);
		return NULL;
	}
	if (ret == 0) {
		/* timeout */
		wpa_printf(MSG_INFO, "Timeout on waiting for data");
		wpabuf_free(in);
		return NULL;
	}

	len = recv(s, wpabuf_put(in, 0), wpabuf_tailroom(in), 0);
	if (len < 0) {
		wpa_printf(MSG_ERROR, "recv: %s", strerror(errno));
		wpabuf_free(in);
		return NULL;
	}
	if (len == 0) {
		wpa_printf(MSG_DEBUG, "No more data available");
		wpabuf_free(in);
		return NULL;
	}
	wpa_printf(MSG_DEBUG, "Received %d bytes", len);
	wpabuf_put(in, len);

	return in;
}


static int https_client(int s, const char *path)
{
	struct tls_config conf;
	void *tls;
	struct tls_connection *conn;
	struct wpabuf *in, *out, *appl;
	int res = -1;
	int need_more_data;

	os_memset(&conf, 0, sizeof(conf));
	conf.event_cb = https_tls_event_cb;
	tls = tls_init(&conf);
	if (tls == NULL)
		return -1;

	conn = tls_connection_init(tls);
	if (conn == NULL) {
		tls_deinit(tls);
		return -1;
	}

	in = NULL;

	for (;;) {
		appl = NULL;
		out = tls_connection_handshake2(tls, conn, in, &appl,
						&need_more_data);
		wpabuf_free(in);
		in = NULL;
		if (out == NULL) {
			if (need_more_data)
				goto read_more;
			goto done;
		}
		if (tls_connection_get_failed(tls, conn)) {
			wpa_printf(MSG_ERROR, "TLS handshake failed");
			goto done;
		}
		if (tls_connection_established(tls, conn))
			break;
		wpa_printf(MSG_DEBUG, "Sending %d bytes",
			   (int) wpabuf_len(out));
		if (send(s, wpabuf_head(out), wpabuf_len(out), 0) < 0) {
			wpa_printf(MSG_ERROR, "send: %s", strerror(errno));
			goto done;
		}
		wpabuf_free(out);
		out = NULL;

	read_more:
		in = https_recv(s);
		if (in == NULL)
			goto done;
	}
	wpabuf_free(out);
	out = NULL;

	wpa_printf(MSG_INFO, "TLS connection established");
	if (appl)
		wpa_hexdump_buf(MSG_DEBUG, "Received application data", appl);

	in = wpabuf_alloc(100 + os_strlen(path));
	if (in == NULL)
		goto done;
	wpabuf_put_str(in, "GET ");
	wpabuf_put_str(in, path);
	wpabuf_put_str(in, " HTTP/1.0\r\n\r\n");
	out = tls_connection_encrypt(tls, conn, in);
	wpabuf_free(in);
	in = NULL;
	if (out == NULL)
		goto done;

	wpa_printf(MSG_INFO, "Sending HTTP request: %d bytes",
		   (int) wpabuf_len(out));
	if (send(s, wpabuf_head(out), wpabuf_len(out), 0) < 0) {
		wpa_printf(MSG_ERROR, "send: %s", strerror(errno));
		goto done;
	}
	wpabuf_free(out);
	out = NULL;

	wpa_printf(MSG_INFO, "Reading HTTP response");
	for (;;) {
		int need_more_data;
		in = https_recv(s);
		if (in == NULL)
			goto done;
		out = tls_connection_decrypt2(tls, conn, in, &need_more_data);
		if (need_more_data)
			wpa_printf(MSG_DEBUG, "HTTP: Need more data");
		wpabuf_free(in);
		in = NULL;
		if (out == NULL)
			goto done;
		wpa_hexdump_ascii(MSG_INFO, "Response", wpabuf_head(out),
				  wpabuf_len(out));
		wpabuf_free(out);
		out = NULL;
	}

	res = 0;
done:
	wpabuf_free(out);
	wpabuf_free(in);
	wpabuf_free(appl);
	tls_connection_deinit(tls, conn);
	tls_deinit(tls);

	return res;
}


int main(int argc, char *argv[])
{
	struct addrinfo hints, *result, *rp;
	int res, s;

	wpa_debug_level = 0;
	wpa_debug_show_keys = 1;

	if (argc < 4) {
		wpa_printf(MSG_INFO, "usage: test-https server port path");
		return -1;
	}

	os_memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	res = getaddrinfo(argv[1], argv[2], &hints, &result);
	if (res) {
		wpa_printf(MSG_ERROR, "getaddrinfo: %s", gai_strerror(res));
		return -1;
	}

	for (rp = result; rp; rp = rp->ai_next) {
		s = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
		if (s < 0)
			continue;
		if (connect(s, rp->ai_addr, rp->ai_addrlen) == 0)
			break;
		close(s);
	}
	freeaddrinfo(result);

	if (rp == NULL) {
		wpa_printf(MSG_ERROR, "Could not connect");
		return -1;
	}

	https_client(s, argv[3]);
	close(s);

	return 0;
}