test-https.c 4.8 KB

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