test-https.c 4.5 KB

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