b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Testing tool for TLSv1 client routines using HTTPS |
| 3 | * Copyright (c) 2011, Jouni Malinen <j@w1.fi> |
| 4 | * |
| 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
| 7 | */ |
| 8 | |
| 9 | #include "includes.h" |
| 10 | #include <netdb.h> |
| 11 | |
| 12 | #include "common.h" |
| 13 | #include "crypto/tls.h" |
| 14 | |
| 15 | |
| 16 | static void https_tls_event_cb(void *ctx, enum tls_event ev, |
| 17 | union tls_event_data *data) |
| 18 | { |
| 19 | wpa_printf(MSG_DEBUG, "HTTPS: TLS event %d", ev); |
| 20 | } |
| 21 | |
| 22 | |
| 23 | static struct wpabuf * https_recv(int s) |
| 24 | { |
| 25 | struct wpabuf *in; |
| 26 | int len, ret; |
| 27 | fd_set rfds; |
| 28 | struct timeval tv; |
| 29 | |
| 30 | in = wpabuf_alloc(20000); |
| 31 | if (in == NULL) |
| 32 | return NULL; |
| 33 | |
| 34 | FD_ZERO(&rfds); |
| 35 | FD_SET(s, &rfds); |
| 36 | tv.tv_sec = 5; |
| 37 | tv.tv_usec = 0; |
| 38 | |
| 39 | wpa_printf(MSG_DEBUG, "Waiting for more data"); |
| 40 | ret = select(s + 1, &rfds, NULL, NULL, &tv); |
| 41 | if (ret < 0) { |
| 42 | wpa_printf(MSG_ERROR, "select: %s", strerror(errno)); |
| 43 | wpabuf_free(in); |
| 44 | return NULL; |
| 45 | } |
| 46 | if (ret == 0) { |
| 47 | /* timeout */ |
| 48 | wpa_printf(MSG_INFO, "Timeout on waiting for data"); |
| 49 | wpabuf_free(in); |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | len = recv(s, wpabuf_put(in, 0), wpabuf_tailroom(in), 0); |
| 54 | if (len < 0) { |
| 55 | wpa_printf(MSG_ERROR, "recv: %s", strerror(errno)); |
| 56 | wpabuf_free(in); |
| 57 | return NULL; |
| 58 | } |
| 59 | if (len == 0) { |
| 60 | wpa_printf(MSG_DEBUG, "No more data available"); |
| 61 | wpabuf_free(in); |
| 62 | return NULL; |
| 63 | } |
| 64 | wpa_printf(MSG_DEBUG, "Received %d bytes", len); |
| 65 | wpabuf_put(in, len); |
| 66 | |
| 67 | return in; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | static int https_client(int s, const char *path) |
| 72 | { |
| 73 | struct tls_config conf; |
| 74 | void *tls; |
| 75 | struct tls_connection *conn; |
| 76 | struct wpabuf *in, *out, *appl; |
| 77 | int res = -1; |
| 78 | int need_more_data = 0; |
| 79 | |
| 80 | os_memset(&conf, 0, sizeof(conf)); |
| 81 | conf.event_cb = https_tls_event_cb; |
| 82 | tls = tls_init(&conf); |
| 83 | if (tls == NULL) |
| 84 | return -1; |
| 85 | |
| 86 | conn = tls_connection_init(tls); |
| 87 | if (conn == NULL) { |
| 88 | tls_deinit(tls); |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | in = NULL; |
| 93 | |
| 94 | for (;;) { |
| 95 | appl = NULL; |
| 96 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
| 97 | out = tls_connection_handshake2(tls, conn, in, &appl, |
| 98 | &need_more_data); |
| 99 | #else |
| 100 | out = tls_connection_handshake(tls, conn, in, &appl); |
| 101 | #endif |
| 102 | wpabuf_free(in); |
| 103 | in = NULL; |
| 104 | if (out == NULL) { |
| 105 | if (need_more_data) |
| 106 | goto read_more; |
| 107 | goto done; |
| 108 | } |
| 109 | if (tls_connection_get_failed(tls, conn)) { |
| 110 | wpa_printf(MSG_ERROR, "TLS handshake failed"); |
| 111 | goto done; |
| 112 | } |
| 113 | if (tls_connection_established(tls, conn)) |
| 114 | break; |
| 115 | wpa_printf(MSG_DEBUG, "Sending %d bytes", |
| 116 | (int) wpabuf_len(out)); |
| 117 | if (send(s, wpabuf_head(out), wpabuf_len(out), 0) < 0) { |
| 118 | wpa_printf(MSG_ERROR, "send: %s", strerror(errno)); |
| 119 | goto done; |
| 120 | } |
| 121 | wpabuf_free(out); |
| 122 | out = NULL; |
| 123 | |
| 124 | read_more: |
| 125 | in = https_recv(s); |
| 126 | if (in == NULL) |
| 127 | goto done; |
| 128 | } |
| 129 | wpabuf_free(out); |
| 130 | out = NULL; |
| 131 | |
| 132 | wpa_printf(MSG_INFO, "TLS connection established"); |
| 133 | if (appl) |
| 134 | wpa_hexdump_buf(MSG_DEBUG, "Received application data", appl); |
| 135 | |
| 136 | in = wpabuf_alloc(100 + os_strlen(path)); |
| 137 | if (in == NULL) |
| 138 | goto done; |
| 139 | wpabuf_put_str(in, "GET "); |
| 140 | wpabuf_put_str(in, path); |
| 141 | wpabuf_put_str(in, " HTTP/1.0\r\n\r\n"); |
| 142 | out = tls_connection_encrypt(tls, conn, in); |
| 143 | wpabuf_free(in); |
| 144 | in = NULL; |
| 145 | if (out == NULL) |
| 146 | goto done; |
| 147 | |
| 148 | wpa_printf(MSG_INFO, "Sending HTTP request: %d bytes", |
| 149 | (int) wpabuf_len(out)); |
| 150 | if (send(s, wpabuf_head(out), wpabuf_len(out), 0) < 0) { |
| 151 | wpa_printf(MSG_ERROR, "send: %s", strerror(errno)); |
| 152 | goto done; |
| 153 | } |
| 154 | wpabuf_free(out); |
| 155 | out = NULL; |
| 156 | |
| 157 | wpa_printf(MSG_INFO, "Reading HTTP response"); |
| 158 | for (;;) { |
| 159 | int need_more_data = 0; |
| 160 | in = https_recv(s); |
| 161 | if (in == NULL) |
| 162 | goto done; |
| 163 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
| 164 | out = tls_connection_decrypt2(tls, conn, in, &need_more_data); |
| 165 | #else |
| 166 | out = tls_connection_decrypt(tls, conn, in); |
| 167 | #endif |
| 168 | if (need_more_data) |
| 169 | wpa_printf(MSG_DEBUG, "HTTP: Need more data"); |
| 170 | wpabuf_free(in); |
| 171 | in = NULL; |
| 172 | if (out == NULL) |
| 173 | goto done; |
| 174 | wpa_hexdump_ascii(MSG_INFO, "Response", wpabuf_head(out), |
| 175 | wpabuf_len(out)); |
| 176 | wpabuf_free(out); |
| 177 | out = NULL; |
| 178 | } |
| 179 | |
| 180 | res = 0; |
| 181 | done: |
| 182 | wpabuf_free(out); |
| 183 | wpabuf_free(in); |
| 184 | wpabuf_free(appl); |
| 185 | tls_connection_deinit(tls, conn); |
| 186 | tls_deinit(tls); |
| 187 | |
| 188 | return res; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | int main(int argc, char *argv[]) |
| 193 | { |
| 194 | struct addrinfo hints, *result, *rp; |
| 195 | int res, s; |
| 196 | |
| 197 | wpa_debug_level = 0; |
| 198 | wpa_debug_show_keys = 1; |
| 199 | |
| 200 | if (argc < 4) { |
| 201 | wpa_printf(MSG_INFO, "usage: test-https server port path"); |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | os_memset(&hints, 0, sizeof(hints)); |
| 206 | hints.ai_family = AF_UNSPEC; |
| 207 | hints.ai_socktype = SOCK_STREAM; |
| 208 | res = getaddrinfo(argv[1], argv[2], &hints, &result); |
| 209 | if (res) { |
| 210 | wpa_printf(MSG_ERROR, "getaddrinfo: %s", gai_strerror(res)); |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | for (rp = result; rp; rp = rp->ai_next) { |
| 215 | s = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
| 216 | if (s < 0) |
| 217 | continue; |
| 218 | if (connect(s, rp->ai_addr, rp->ai_addrlen) == 0) |
| 219 | break; |
| 220 | close(s); |
| 221 | } |
| 222 | freeaddrinfo(result); |
| 223 | |
| 224 | if (rp == NULL) { |
| 225 | wpa_printf(MSG_ERROR, "Could not connect"); |
| 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | https_client(s, argv[3]); |
| 230 | close(s); |
| 231 | |
| 232 | return 0; |
| 233 | } |