test-https.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. wpabuf_free(out);
  116. out = NULL;
  117. wpa_printf(MSG_INFO, "TLS connection established");
  118. if (appl)
  119. wpa_hexdump_buf(MSG_DEBUG, "Received application data", appl);
  120. in = wpabuf_alloc(100 + os_strlen(path));
  121. if (in == NULL)
  122. goto done;
  123. wpabuf_put_str(in, "GET ");
  124. wpabuf_put_str(in, path);
  125. wpabuf_put_str(in, " HTTP/1.0\r\n\r\n");
  126. out = tls_connection_encrypt(tls, conn, in);
  127. wpabuf_free(in);
  128. in = NULL;
  129. if (out == NULL)
  130. goto done;
  131. wpa_printf(MSG_INFO, "Sending HTTP request: %d bytes",
  132. (int) wpabuf_len(out));
  133. if (send(s, wpabuf_head(out), wpabuf_len(out), 0) < 0) {
  134. wpa_printf(MSG_ERROR, "send: %s", strerror(errno));
  135. goto done;
  136. }
  137. wpabuf_free(out);
  138. out = NULL;
  139. wpa_printf(MSG_INFO, "Reading HTTP response");
  140. for (;;) {
  141. int need_more_data;
  142. in = https_recv(s);
  143. if (in == NULL)
  144. goto done;
  145. out = tls_connection_decrypt2(tls, conn, in, &need_more_data);
  146. if (need_more_data)
  147. wpa_printf(MSG_DEBUG, "HTTP: Need more data");
  148. wpabuf_free(in);
  149. in = NULL;
  150. if (out == NULL)
  151. goto done;
  152. wpa_hexdump_ascii(MSG_INFO, "Response", wpabuf_head(out),
  153. wpabuf_len(out));
  154. wpabuf_free(out);
  155. out = NULL;
  156. }
  157. res = 0;
  158. done:
  159. wpabuf_free(out);
  160. wpabuf_free(in);
  161. wpabuf_free(appl);
  162. tls_connection_deinit(tls, conn);
  163. tls_deinit(tls);
  164. return res;
  165. }
  166. int main(int argc, char *argv[])
  167. {
  168. struct addrinfo hints, *result, *rp;
  169. int res, s;
  170. wpa_debug_level = 0;
  171. wpa_debug_show_keys = 1;
  172. if (argc < 4) {
  173. wpa_printf(MSG_INFO, "usage: test-https server port path");
  174. return -1;
  175. }
  176. os_memset(&hints, 0, sizeof(hints));
  177. hints.ai_family = AF_UNSPEC;
  178. hints.ai_socktype = SOCK_STREAM;
  179. res = getaddrinfo(argv[1], argv[2], &hints, &result);
  180. if (res) {
  181. wpa_printf(MSG_ERROR, "getaddrinfo: %s", gai_strerror(res));
  182. return -1;
  183. }
  184. for (rp = result; rp; rp = rp->ai_next) {
  185. s = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  186. if (s < 0)
  187. continue;
  188. if (connect(s, rp->ai_addr, rp->ai_addrlen) == 0)
  189. break;
  190. close(s);
  191. }
  192. freeaddrinfo(result);
  193. if (rp == NULL) {
  194. wpa_printf(MSG_ERROR, "Could not connect");
  195. return -1;
  196. }
  197. https_client(s, argv[3]);
  198. close(s);
  199. return 0;
  200. }