test-https.c 4.6 KB

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