qa_sock.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #define _POSIX_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <strings.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <error.h>
  8. #include <errno.h>
  9. #include <time.h>
  10. #include <sys/types.h>
  11. #include <sys/select.h>
  12. #include <sys/socket.h>
  13. #include <netdb.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/ssl.h>
  16. #include "qa/qa.h"
  17. #include "qa/qa_sock.h"
  18. #define TIMEOUT_SEC 1
  19. #define TIMEOUT_USEC 0
  20. #define SOCKET_PROTOCOL 0
  21. #define INVALID_SOCKET (-1)
  22. /** BIO wrapper around stdout */
  23. BIO* bio_out;
  24. /** BIO wrapper around srderr */
  25. BIO* bio_err;
  26. /**
  27. * \brief Converts a uri into a tuple {host, service}.
  28. *
  29. * Parses an input string containing a host and (maybe) a service/port.
  30. * Valid options are:
  31. * - service://hostname
  32. * - hostname
  33. * - hostname:port
  34. * The resulting tuple will be stored in params \ref host and \ref service.
  35. * \note \ref uri might be modified during parsing.
  36. *
  37. * \param[in] uri The input uri.
  38. * \param[out] host Place where to store parsed host.
  39. * \param[out] service Place where to store parsed service.
  40. *
  41. * \return 0 if the string was not parsable, 1 otherwise.
  42. */
  43. int host_port(char *uri, char **host, char **service)
  44. {
  45. char* c;
  46. if (!(c = strchr(uri, ':'))) {
  47. *host = uri;
  48. *service = NULL;
  49. } else {
  50. *c = '\0';
  51. if (c[1] == '/' && c[2] == '/') {
  52. *service = uri;
  53. *host = c+3;
  54. } else {
  55. *service = c+1;
  56. *host = uri;
  57. }
  58. }
  59. return 1;
  60. }
  61. /**
  62. * \brief Instantiate a new TCP connection.
  63. *
  64. * Attempt to create tcp connection with with the remote host `host`
  65. * - eventually resolved - over port `port`.
  66. * \return -1 on failure, the socket file descriptor otherwise.
  67. *
  68. */
  69. int init_client(const char *host, const char *port)
  70. {
  71. int s, i;
  72. fd_set socket_fds;
  73. struct addrinfo *result, *rp;
  74. struct timeval timeout;
  75. if ((i=getaddrinfo(host, port, NULL, &result))) {
  76. BIO_printf(bio_err, "Error: %s\n", gai_strerror(i));
  77. return -1;
  78. }
  79. for (rp=result; rp; rp = rp->ai_next) {
  80. s = socket(rp->ai_family,
  81. rp->ai_socktype,
  82. rp->ai_protocol);
  83. if (s == INVALID_SOCKET) continue;
  84. if (rp->ai_protocol == SOCK_STREAM) {
  85. i = 0;
  86. i = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char*) &i, sizeof(i));
  87. if (i < 0) return -1;
  88. }
  89. //Set the socket to non-blocking
  90. int flags = fcntl(s, F_GETFL, 0);
  91. fcntl(s, F_SETFL, flags | O_NONBLOCK);
  92. connect(s, rp->ai_addr, rp->ai_addrlen);
  93. if (errno != EINPROGRESS) {
  94. close(s);
  95. continue;
  96. }
  97. FD_ZERO(&socket_fds);
  98. FD_SET(s, &socket_fds);
  99. timeout.tv_sec = TIMEOUT_SEC;
  100. timeout.tv_usec = TIMEOUT_USEC;
  101. i = select(s+1, NULL, &socket_fds, NULL, &timeout);
  102. if (i > 0) break;
  103. close(s);
  104. }
  105. if (!rp) return -1;
  106. return s;
  107. }
  108. /**
  109. * \brief Connection informations.
  110. */
  111. typedef struct qa_connection {
  112. int socket; /**< socket file descriptor. */
  113. SSL *ssl; /**< ssl handler for this connection. */
  114. BIO *sbio;
  115. SSL_CTX *ctx; /**< ssl context used in this connection. */
  116. } qa_connection_t;
  117. /**
  118. * \brief Destructor for a \ref qa_connection.
  119. *
  120. * Closes the socket, shuts down the connection, and frees all memory used for
  121. * holding the connection.
  122. * \note Input might be partial (ex. a socket exists, but not no ssl session).
  123. *
  124. * \param c The connection to be freed.
  125. */
  126. static void qa_connection_free(struct qa_connection* c)
  127. {
  128. if (!c) return;
  129. if (c->sbio)
  130. BIO_free(c->sbio);
  131. if (c->socket != -1)
  132. close(c->socket);
  133. if (c->ssl) {
  134. SSL_shutdown(c->ssl);
  135. SSL_free(c->ssl);
  136. }
  137. if (c->ctx)
  138. SSL_CTX_free(c->ctx);
  139. free(c);
  140. }
  141. static int verify_callback(int ok, X509_STORE_CTX* ctx)
  142. {
  143. return ok;
  144. }
  145. /**
  146. * \brief Set up a new ssl connection.
  147. *
  148. * Create a new \ref qa_connection, turning on OpenSSL (if not yet started), and
  149. * opening a socket with the target server over ssl.
  150. *
  151. * \param[in] conf Configuration holding informations about the target.
  152. * \return The new connection.
  153. */
  154. struct qa_connection* qa_connection_new(char* address)
  155. {
  156. struct qa_connection* c = NULL;
  157. char *host, *port;
  158. int attempts;
  159. int err;
  160. fd_set socket_fds;
  161. struct timeval timeout;
  162. /* parse input address */
  163. if (!host_port(address, &host, &port)) goto error;
  164. if (!port) port = "https";
  165. c = calloc(1, sizeof(struct qa_connection));
  166. if (!c) goto error;
  167. /* set up context, and protocol versions */
  168. c->ctx = SSL_CTX_new(TLSv1_client_method());
  169. if (!c->ctx) goto error;
  170. /* create the ssl session, disabling certificate verification */
  171. SSL_CTX_set_verify(c->ctx, SSL_VERIFY_NONE, verify_callback);
  172. c->ssl = SSL_new(c->ctx);
  173. SSL_set_connect_state(c->ssl);
  174. if (!c->ssl) goto error;
  175. /* open the socket over ssl */
  176. c->socket = init_client(host, port);
  177. if (c->socket == -1) goto error;
  178. if (!SSL_set_fd(c->ssl, c->socket)) goto error;
  179. FD_ZERO(&socket_fds);
  180. FD_SET(c->socket, &socket_fds);
  181. for(attempts = 10; attempts; attempts--) {
  182. err = SSL_do_handshake(c->ssl);
  183. // err = SSL_connect(c->ssl);
  184. if (err == 1) return c;
  185. err = SSL_get_error(c->ssl, err);
  186. timeout.tv_sec = TIMEOUT_SEC;
  187. timeout.tv_usec = TIMEOUT_USEC;
  188. if (err == SSL_ERROR_WANT_WRITE)
  189. select(c->socket+1, NULL, &socket_fds, NULL, &timeout);
  190. else if (err == SSL_ERROR_WANT_READ)
  191. select(c->socket+1, &socket_fds, NULL, NULL, &timeout);
  192. else
  193. goto error;
  194. }
  195. error:
  196. /* XXX. add checks for errno, and the ssl error stack (ssl_get_error) */
  197. qa_connection_free(c);
  198. return NULL;
  199. }
  200. /**
  201. * \brief Fetches the certificate opening a tcp connection to the given address.
  202. *
  203. * Attempts to open a new tcp connection to the address `address`, and return
  204. * the X509 certificate presented by the server.
  205. *
  206. * \param address[in] the uri to which handshake a ssl connection.
  207. * \return a valid pointer to the X509 certificate if the handshake succeeded,
  208. * NULL otherwise.
  209. */
  210. X509* get_remote_cert(char *address)
  211. {
  212. X509 *crt = NULL;
  213. qa_connection_t *c;
  214. fprintf(stderr, "[-] connecting to %s\n", address);
  215. c = qa_connection_new(address);
  216. if (!c) return NULL;
  217. crt = SSL_get_peer_certificate(c->ssl);
  218. qa_connection_free(c);
  219. return crt;
  220. }