tunala.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Tunala ("Tunneler with a New Zealand accent") Written by Geoff Thorpe,
  3. * but endorsed/supported by noone. Please use this is if it's useful or
  4. * informative to you, but it's only here as a scratchpad for ideas about how
  5. * you might (or might not) program with OpenSSL. If you deploy this is in a
  6. * mission-critical environment, and have not read, understood, audited, and
  7. * modified this code to your satisfaction, and the result is that all hell
  8. * breaks loose and you are looking for a new employer, then it proves
  9. * nothing except perhaps that Darwinism is alive and well. Let's just say,
  10. * *I* don't use this in a mission-critical environment, so it would be
  11. * stupid for anyone to assume that it is solid and/or tested enough when
  12. * even its author doesn't place that much trust in it. You have been warned.
  13. * With thanks to Cryptographic Appliances, Inc.
  14. */
  15. #ifndef _TUNALA_H
  16. # define _TUNALA_H
  17. /* pull in autoconf fluff */
  18. # ifndef NO_CONFIG_H
  19. # include "config.h"
  20. # else
  21. /*
  22. * We don't have autoconf, we have to set all of these unless a tweaked
  23. * Makefile tells us not to ...
  24. */
  25. /* headers */
  26. # ifndef NO_HAVE_SELECT
  27. # define HAVE_SELECT
  28. # endif
  29. # ifndef NO_HAVE_SOCKET
  30. # define HAVE_SOCKET
  31. # endif
  32. # ifndef NO_HAVE_UNISTD_H
  33. # define HAVE_UNISTD_H
  34. # endif
  35. # ifndef NO_HAVE_FCNTL_H
  36. # define HAVE_FCNTL_H
  37. # endif
  38. # ifndef NO_HAVE_LIMITS_H
  39. # define HAVE_LIMITS_H
  40. # endif
  41. /* features */
  42. # ifndef NO_HAVE_STRSTR
  43. # define HAVE_STRSTR
  44. # endif
  45. # ifndef NO_HAVE_STRTOUL
  46. # define HAVE_STRTOUL
  47. # endif
  48. # endif
  49. # if !defined(HAVE_SELECT) || !defined(HAVE_SOCKET)
  50. # error "can't build without some network basics like select() and socket()"
  51. # endif
  52. # include <stdlib.h>
  53. # ifndef NO_SYSTEM_H
  54. # include <string.h>
  55. # ifdef HAVE_UNISTD_H
  56. # include <unistd.h>
  57. # endif
  58. # ifdef HAVE_FCNTL_H
  59. # include <fcntl.h>
  60. # endif
  61. # ifdef HAVE_LIMITS_H
  62. # include <limits.h>
  63. # endif
  64. # include <netdb.h>
  65. # include <signal.h>
  66. # include <sys/socket.h>
  67. # include <sys/types.h>
  68. # include <netinet/in.h>
  69. # endif /* !defined(NO_SYSTEM_H) */
  70. # ifndef NO_OPENSSL
  71. # include <openssl/err.h>
  72. # include <openssl/engine.h>
  73. # include <openssl/ssl.h>
  74. # endif /* !defined(NO_OPENSSL) */
  75. # ifndef OPENSSL_NO_BUFFER
  76. /*
  77. * This is the generic "buffer" type that is used when feeding the
  78. * state-machine. It's basically a FIFO with respect to the "adddata" &
  79. * "takedata" type functions that operate on it.
  80. */
  81. # define MAX_DATA_SIZE 16384
  82. typedef struct _buffer_t {
  83. unsigned char data[MAX_DATA_SIZE];
  84. unsigned int used;
  85. /*
  86. * Statistical values - counts the total number of bytes read in and read
  87. * out (respectively) since "buffer_init()"
  88. */
  89. unsigned long total_in, total_out;
  90. } buffer_t;
  91. /* Initialise a buffer structure before use */
  92. void buffer_init(buffer_t * buf);
  93. /*
  94. * Cleanup a buffer structure - presently not needed, but if buffer_t is
  95. * converted to using dynamic allocation, this would be required - so should
  96. * be called to protect against an explosion of memory leaks later if the
  97. * change is made.
  98. */
  99. void buffer_close(buffer_t * buf);
  100. /* Basic functions to manipulate buffers */
  101. unsigned int buffer_used(buffer_t * buf); /* How much data in the buffer */
  102. unsigned int buffer_unused(buffer_t * buf); /* How much space in the buffer */
  103. int buffer_full(buffer_t * buf); /* Boolean, is it full? */
  104. int buffer_notfull(buffer_t * buf); /* Boolean, is it not full? */
  105. int buffer_empty(buffer_t * buf); /* Boolean, is it empty? */
  106. int buffer_notempty(buffer_t * buf); /* Boolean, is it not empty? */
  107. unsigned long buffer_total_in(buffer_t * buf); /* Total bytes written to
  108. * buffer */
  109. unsigned long buffer_total_out(buffer_t * buf); /* Total bytes read from
  110. * buffer */
  111. # if 0 /* Currently used only within buffer.c -
  112. * better to expose only higher-level
  113. * functions anyway */
  114. /*
  115. * Add data to the tail of the buffer, returns the amount that was actually
  116. * added (so, you need to check if return value is less than size)
  117. */
  118. unsigned int buffer_adddata(buffer_t * buf, const unsigned char *ptr,
  119. unsigned int size);
  120. /*
  121. * Take data from the front of the buffer (and scroll the rest forward). If
  122. * "ptr" is NULL, this just removes data off the front of the buffer. Return
  123. * value is the amount actually removed (can be less than size if the buffer
  124. * has too little data).
  125. */
  126. unsigned int buffer_takedata(buffer_t * buf, unsigned char *ptr,
  127. unsigned int size);
  128. /*
  129. * Flushes as much data as possible out of the "from" buffer into the "to"
  130. * buffer. Return value is the amount moved. The amount moved can be
  131. * restricted to a maximum by specifying "cap" - setting it to -1 means no
  132. * limit.
  133. */
  134. unsigned int buffer_tobuffer(buffer_t * to, buffer_t * from, int cap);
  135. # endif
  136. # ifndef NO_IP
  137. /* Read or write between a file-descriptor and a buffer */
  138. int buffer_from_fd(buffer_t * buf, int fd);
  139. int buffer_to_fd(buffer_t * buf, int fd);
  140. # endif /* !defined(NO_IP) */
  141. # ifndef NO_OPENSSL
  142. /* Read or write between an SSL or BIO and a buffer */
  143. void buffer_from_SSL(buffer_t * buf, SSL *ssl);
  144. void buffer_to_SSL(buffer_t * buf, SSL *ssl);
  145. void buffer_from_BIO(buffer_t * buf, BIO *bio);
  146. void buffer_to_BIO(buffer_t * buf, BIO *bio);
  147. /* Callbacks */
  148. void cb_ssl_info(const SSL *s, int where, int ret);
  149. /* Called if output should be sent too */
  150. void cb_ssl_info_set_output(FILE *fp);
  151. int cb_ssl_verify(int ok, X509_STORE_CTX *ctx);
  152. void cb_ssl_verify_set_output(FILE *fp);
  153. void cb_ssl_verify_set_depth(unsigned int verify_depth);
  154. void cb_ssl_verify_set_level(unsigned int level);
  155. RSA *cb_generate_tmp_rsa(SSL *s, int is_export, int keylength);
  156. # endif /* !defined(NO_OPENSSL) */
  157. # endif /* !defined(OPENSSL_NO_BUFFER) */
  158. # ifndef NO_TUNALA
  159. # ifdef OPENSSL_NO_BUFFER
  160. # error "TUNALA section of tunala.h requires BUFFER support"
  161. # endif
  162. typedef struct _state_machine_t {
  163. SSL *ssl;
  164. BIO *bio_intossl;
  165. BIO *bio_fromssl;
  166. buffer_t clean_in, clean_out;
  167. buffer_t dirty_in, dirty_out;
  168. } state_machine_t;
  169. typedef enum {
  170. SM_CLEAN_IN, SM_CLEAN_OUT,
  171. SM_DIRTY_IN, SM_DIRTY_OUT
  172. } sm_buffer_t;
  173. void state_machine_init(state_machine_t * machine);
  174. void state_machine_close(state_machine_t * machine);
  175. buffer_t *state_machine_get_buffer(state_machine_t * machine,
  176. sm_buffer_t type);
  177. SSL *state_machine_get_SSL(state_machine_t * machine);
  178. int state_machine_set_SSL(state_machine_t * machine, SSL *ssl, int is_server);
  179. /* Performs the data-IO loop and returns zero if the machine should close */
  180. int state_machine_churn(state_machine_t * machine);
  181. /*
  182. * Is used to handle closing conditions - namely when one side of the tunnel
  183. * has closed but the other should finish flushing.
  184. */
  185. int state_machine_close_clean(state_machine_t * machine);
  186. int state_machine_close_dirty(state_machine_t * machine);
  187. # endif /* !defined(NO_TUNALA) */
  188. # ifndef NO_IP
  189. /*
  190. * Initialise anything related to the networking. This includes blocking
  191. * pesky SIGPIPE signals.
  192. */
  193. int ip_initialise(void);
  194. /*
  195. * ip is the 4-byte ip address (eg. 127.0.0.1 is {0x7F,0x00,0x00,0x01}), port
  196. * is the port to listen on (host byte order), and the return value is the
  197. * file-descriptor or -1 on error.
  198. */
  199. int ip_create_listener_split(const char *ip, unsigned short port);
  200. /* Same semantics as above. */
  201. int ip_create_connection_split(const char *ip, unsigned short port);
  202. /* Converts a string into the ip/port before calling the above */
  203. int ip_create_listener(const char *address);
  204. int ip_create_connection(const char *address);
  205. /*
  206. * Just does a string conversion on its own. NB: If accept_all_ip is
  207. * non-zero, then the address string could be just a port. Ie. it's suitable
  208. * for a listening address but not a connecting address.
  209. */
  210. int ip_parse_address(const char *address, const char **parsed_ip,
  211. unsigned short *port, int accept_all_ip);
  212. /*
  213. * Accepts an incoming connection through the listener. Assumes selects and
  214. * what-not have deemed it an appropriate thing to do.
  215. */
  216. int ip_accept_connection(int listen_fd);
  217. # endif /* !defined(NO_IP) */
  218. /* These functions wrap up things that can be portability hassles. */
  219. int int_strtoul(const char *str, unsigned long *val);
  220. # ifdef HAVE_STRSTR
  221. # define int_strstr strstr
  222. # else
  223. char *int_strstr(const char *haystack, const char *needle);
  224. # endif
  225. #endif /* !defined(_TUNALA_H) */