qa_sock.c 4.7 KB

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