tunala.h 7.9 KB

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