qa_sock.c 4.8 KB

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