2 * Testing tool for TLSv1 server routines using HTTPS
3 * Copyright (c) 2011-2019, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/tls.h"
15 static void https_tls_event_cb(void *ctx
, enum tls_event ev
,
16 union tls_event_data
*data
)
18 wpa_printf(MSG_DEBUG
, "HTTPS: TLS event %d", ev
);
22 static struct wpabuf
* https_recv(int s
, int timeout_ms
)
29 in
= wpabuf_alloc(20000);
35 tv
.tv_sec
= timeout_ms
/ 1000;
36 tv
.tv_usec
= timeout_ms
% 1000;
38 wpa_printf(MSG_DEBUG
, "Waiting for more data");
39 ret
= select(s
+ 1, &rfds
, NULL
, NULL
, &tv
);
41 wpa_printf(MSG_ERROR
, "select: %s", strerror(errno
));
47 wpa_printf(MSG_INFO
, "Timeout on waiting for data");
52 len
= recv(s
, wpabuf_put(in
, 0), wpabuf_tailroom(in
), 0);
54 wpa_printf(MSG_ERROR
, "recv: %s", strerror(errno
));
59 wpa_printf(MSG_DEBUG
, "No more data available");
63 wpa_printf(MSG_DEBUG
, "Received %d bytes", len
);
70 static void https_tls_log_cb(void *ctx
, const char *msg
)
72 wpa_printf(MSG_DEBUG
, "TLS: %s", msg
);
76 static int https_server(int s
)
78 struct tls_config conf
;
80 struct tls_connection_params params
;
81 struct tls_connection
*conn
;
82 struct wpabuf
*in
, *out
, *appl
;
85 os_memset(&conf
, 0, sizeof(conf
));
86 conf
.event_cb
= https_tls_event_cb
;
87 tls
= tls_init(&conf
);
91 os_memset(¶ms
, 0, sizeof(params
));
92 params
.ca_cert
= "hwsim/auth_serv/ca.pem";
93 params
.client_cert
= "hwsim/auth_serv/server.pem";
94 params
.private_key
= "hwsim/auth_serv/server.key";
95 params
.dh_file
= "hwsim/auth_serv/dh.conf";
97 if (tls_global_set_params(tls
, ¶ms
)) {
98 wpa_printf(MSG_ERROR
, "Failed to set TLS parameters");
103 conn
= tls_connection_init(tls
);
109 tls_connection_set_log_cb(conn
, https_tls_log_cb
, NULL
);
112 in
= https_recv(s
, 5000);
117 out
= tls_connection_server_handshake(tls
, conn
, in
, &appl
);
121 if (!tls_connection_get_failed(tls
, conn
) &&
122 !tls_connection_established(tls
, conn
))
126 wpa_printf(MSG_DEBUG
, "Sending %d bytes",
127 (int) wpabuf_len(out
));
128 if (send(s
, wpabuf_head(out
), wpabuf_len(out
), 0) < 0) {
129 wpa_printf(MSG_ERROR
, "send: %s", strerror(errno
));
134 if (tls_connection_get_failed(tls
, conn
)) {
135 wpa_printf(MSG_ERROR
, "TLS handshake failed");
138 if (tls_connection_established(tls
, conn
))
144 wpa_printf(MSG_INFO
, "TLS connection established");
146 wpa_hexdump_buf(MSG_DEBUG
, "Received application data", appl
);
148 wpa_printf(MSG_INFO
, "Reading HTTP request");
152 in
= https_recv(s
, 5000);
155 out
= tls_connection_decrypt2(tls
, conn
, in
, &need_more_data
);
158 if (need_more_data
) {
159 wpa_printf(MSG_DEBUG
, "HTTP: Need more data");
164 wpa_hexdump_ascii(MSG_INFO
, "Request",
165 wpabuf_head(out
), wpabuf_len(out
));
171 in
= wpabuf_alloc(1000);
174 wpabuf_put_str(in
, "HTTP/1.1 200 OK\r\n"
175 "Server: test-https_server\r\n"
177 "<HTML><BODY>HELLO</BODY></HTML>\n");
178 wpa_hexdump_ascii(MSG_DEBUG
, "Response",
179 wpabuf_head(in
), wpabuf_len(in
));
180 out
= tls_connection_encrypt(tls
, conn
, in
);
183 wpa_hexdump_buf(MSG_DEBUG
, "Encrypted response", out
);
187 wpa_printf(MSG_INFO
, "Sending HTTP response: %d bytes",
188 (int) wpabuf_len(out
));
189 if (send(s
, wpabuf_head(out
), wpabuf_len(out
), 0) < 0) {
190 wpa_printf(MSG_ERROR
, "send: %s", strerror(errno
));
201 tls_connection_deinit(tls
, conn
);
209 int main(int argc
, char *argv
[])
211 struct sockaddr_in sin
;
216 wpa_debug_show_keys
= 1;
219 wpa_printf(MSG_INFO
, "usage: test-https_server port");
223 port
= atoi(argv
[1]);
225 s
= socket(AF_INET
, SOCK_STREAM
, 0);
231 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
)) < 0) {
232 wpa_printf(MSG_DEBUG
,
233 "HTTP: setsockopt(SO_REUSEADDR) failed: %s",
235 /* try to continue anyway */
238 os_memset(&sin
, 0, sizeof(sin
));
239 sin
.sin_family
= AF_INET
;
240 sin
.sin_port
= htons(port
);
241 if (bind(s
, (struct sockaddr
*) &sin
, sizeof(sin
)) < 0) {
247 if (listen(s
, 10) < 0) {
254 struct sockaddr_in addr
;
255 socklen_t addr_len
= sizeof(addr
);
257 conn
= accept(s
, (struct sockaddr
*) &addr
, &addr_len
);
263 wpa_printf(MSG_DEBUG
, "-------------------------------------");
264 wpa_printf(MSG_DEBUG
, "Connection from %s:%d",
265 inet_ntoa(addr
.sin_addr
), ntohs(addr
.sin_port
));
268 wpa_printf(MSG_DEBUG
, "Done with the connection");
269 wpa_printf(MSG_DEBUG
, "-------------------------------------");