README 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. This is intended to be an example of a state-machine driven SSL application. It
  2. acts as an SSL tunneler (functioning as either the server or client half,
  3. depending on command-line arguments). *PLEASE* read the comments in tunala.h
  4. before you treat this stuff as anything more than a curiosity - YOU HAVE BEEN
  5. WARNED!! There, that's the draconian bit out of the way ...
  6. Why "tunala"??
  7. --------------
  8. I thought I asked you to read tunala.h?? :-)
  9. Show me
  10. -------
  11. If you want to simply see it running, skip to the end and see some example
  12. command-line arguments to demonstrate with.
  13. Where to look and what to do?
  14. -----------------------------
  15. The code is split up roughly coinciding with the detaching of an "abstract" SSL
  16. state machine (which is the purpose of all this) and its surrounding application
  17. specifics. This is primarily to make it possible for me to know when I could cut
  18. corners and when I needed to be rigorous (or at least maintain the pretense as
  19. such :-).
  20. Network stuff:
  21. Basically, the network part of all this is what is supposed to be abstracted out
  22. of the way. The intention is to illustrate one way to stick OpenSSL's mechanisms
  23. inside a little memory-driven sandbox and operate it like a pure state-machine.
  24. So, the network code is inside both ip.c (general utility functions and gory
  25. IPv4 details) and tunala.c itself, which takes care of application specifics
  26. like the main select() loop. The connectivity between the specifics of this
  27. application (TCP/IP tunneling and the associated network code) and the
  28. underlying abstract SSL state machine stuff is through the use of the "buffer_t"
  29. type, declared in tunala.h and implemented in buffer.c.
  30. State machine:
  31. Which leaves us, generally speaking, with the abstract "state machine" code left
  32. over and this is sitting inside sm.c, with declarations inside tunala.h. As can
  33. be seen by the definition of the state_machine_t structure and the associated
  34. functions to manipulate it, there are the 3 OpenSSL "handles" plus 4 buffer_t
  35. structures dealing with IO on both the encrypted and unencrypted sides ("dirty"
  36. and "clean" respectively). The "SSL" handle is what facilitates the reading and
  37. writing of the unencrypted (tunneled) data. The two "BIO" handles act as the
  38. read and write channels for encrypted tunnel traffic - in other applications
  39. these are often socket BIOs so that the OpenSSL framework operates with the
  40. network layer directly. In this example, those two BIOs are memory BIOs
  41. (BIO_s_mem()) so that the sending and receiving of the tunnel traffic stays
  42. within the state-machine, and we can handle where this gets send to (or read
  43. from) ourselves.
  44. Why?
  45. ----
  46. If you take a look at the "state_machine_t" section of tunala.h and the code in
  47. sm.c, you will notice that nothing related to the concept of 'transport' is
  48. involved. The binding to TCP/IP networking occurs in tunala.c, specifically
  49. within the "tunala_item_t" structure that associates a state_machine_t object
  50. with 4 file-descriptors. The way to best see where the bridge between the
  51. outside world (TCP/IP reads, writes, select()s, file-descriptors, etc) and the
  52. state machine is, is to examine the "tunala_item_io()" function in tunala.c.
  53. This is currently around lines 641-732 but of course could be subject to change.
  54. And...?
  55. -------
  56. Well, although that function is around 90 lines of code, it could easily have
  57. been a lot less only I was trying to address an easily missed "gotcha" (item (2)
  58. below). The main() code that drives the select/accept/IO loop initialises new
  59. tunala_item_t structures when connections arrive, and works out which
  60. file-descriptors go where depending on whether we're an SSL client or server
  61. (client --> accepted connection is clean and proxied is dirty, server -->
  62. accepted connection is dirty and proxied is clean). What that tunala_item_io()
  63. function is attempting to do is 2 things;
  64. (1) Perform all reads and writes on the network directly into the
  65. state_machine_t's buffers (based on a previous select() result), and only
  66. then allow the abstact state_machine_t to "churn()" using those buffers.
  67. This will cause the SSL machine to consume as much input data from the two
  68. "IN" buffers as possible, and generate as much output data into the two
  69. "OUT" buffers as possible. Back up in the main() function, the next main
  70. loop loop will examine these output buffers and select() for writability
  71. on the corresponding sockets if the buffers are non-empty.
  72. (2) Handle the complicated tunneling-specific issue of cascading "close"s.
  73. This is the reason for most of the complexity in the logic - if one side
  74. of the tunnel is closed, you can't simply close the other side and throw
  75. away the whole thing - (a) there may still be outgoing data on the other
  76. side of the tunnel that hasn't been sent yet, (b) the close (or things
  77. happening during the close) may cause more data to be generated that needs
  78. sending on the other side. Of course, this logic is complicated yet futher
  79. by the fact that it's different depending on which side closes first :-)
  80. state_machine_close_clean() will indicate to the state machine that the
  81. unencrypted side of the tunnel has closed, so any existing outgoing data
  82. needs to be flushed, and the SSL stream needs to be closed down using the
  83. appropriate shutdown sequence. state_machine_close_dirty() is simpler
  84. because it indicates that the SSL stream has been disconnected, so all
  85. that remains before closing the other side is to flush out anything that
  86. remains and wait for it to all be sent.
  87. Anyway, with those things in mind, the code should be a little easier to follow
  88. in terms of "what is *this* bit supposed to achieve??!!".
  89. How might this help?
  90. --------------------
  91. Well, the reason I wrote this is that there seemed to be rather a flood of
  92. questions of late on the openssl-dev and openssl-users lists about getting this
  93. whole IO logic thing sorted out, particularly by those who were trying to either
  94. use non-blocking IO, or wanted SSL in an environment where "something else" was
  95. handling the network already and they needed to operate in memory only. This
  96. code is loosely based on some other stuff I've been working on, although that
  97. stuff is far more complete, far more dependant on a whole slew of other
  98. network/framework code I don't want to incorporate here, and far harder to look
  99. at for 5 minutes and follow where everything is going. I will be trying over
  100. time to suck in a few things from that into this demo in the hopes it might be
  101. more useful, and maybe to even make this demo usable as a utility of its own.
  102. Possible things include:
  103. * controlling multiple processes/threads - this can be used to combat
  104. latencies and get passed file-descriptor limits on some systems, and it uses
  105. a "controller" process/thread that maintains IPC links with the
  106. processes/threads doing the real work.
  107. * cert verification rules - having some say over which certs get in or out :-)
  108. * control over SSL protocols and cipher suites
  109. * A few other things you can already do in s_client and s_server :-)
  110. * Support (and control over) session resuming, particularly when functioning
  111. as an SSL client.
  112. If you have a particular environment where this model might work to let you "do
  113. SSL" without having OpenSSL be aware of the transport, then you should find you
  114. could use the state_machine_t structure (or your own variant thereof) and hook
  115. it up to your transport stuff in much the way tunala.c matches it up with those
  116. 4 file-descriptors. The state_machine_churn(), state_machine_close_clean(), and
  117. state_machine_close_dirty() functions are the main things to understand - after
  118. that's done, you just have to ensure you're feeding and bleeding the 4
  119. state_machine buffers in a logical fashion. This state_machine loop handles not
  120. only handshakes and normal streaming, but also renegotiates - there's no special
  121. handling required beyond keeping an eye on those 4 buffers and keeping them in
  122. sync with your outer "loop" logic. Ie. if one of the OUT buffers is not empty,
  123. you need to find an opportunity to try and forward its data on. If one of the IN
  124. buffers is not full, you should keep an eye out for data arriving that should be
  125. placed there.
  126. This approach could hopefully also allow you to run the SSL protocol in very
  127. different environments. As an example, you could support encrypted event-driven
  128. IPC where threads/processes pass messages to each other inside an SSL layer;
  129. each IPC-message's payload would be in fact the "dirty" content, and the "clean"
  130. payload coming out of the tunnel at each end would be the real intended message.
  131. Likewise, this could *easily* be made to work across unix domain sockets, or
  132. even entirely different network/comms protocols.
  133. This is also a quick and easy way to do VPN if you (and the remote network's
  134. gateway) support virtual network devices that are encapsulted in a single
  135. network connection, perhaps PPP going through an SSL tunnel?
  136. Suggestions
  137. -----------
  138. Please let me know if you find this useful, or if there's anything wrong or
  139. simply too confusing about it. Patches are also welcome, but please attach a
  140. description of what it changes and why, and "diff -urN" format is preferred.
  141. Mail to geoff@openssl.org should do the trick.
  142. Example
  143. -------
  144. Here is an example of how to use "tunala" ...
  145. First, it's assumed that OpenSSL has already built, and that you are building
  146. inside the ./demos/tunala/ directory. If not - please correct the paths and
  147. flags inside the Makefile. Likewise, if you want to tweak the building, it's
  148. best to try and do so in the makefile (eg. removing the debug flags and adding
  149. optimisation flags).
  150. Secondly, this code has mostly only been tested on Linux. However, some
  151. autoconf/etc support has been added and the code has been compiled on openbsd
  152. and solaris using that.
  153. Thirdly, if you are Win32, you probably need to do some *major* rewriting of
  154. ip.c to stand a hope in hell. Good luck, and please mail me the diff if you do
  155. this, otherwise I will take a look at another time. It can certainly be done,
  156. but it's very non-POSIXy.
  157. See the INSTALL document for details on building.
  158. Now, if you don't have an executable "tunala" compiled, go back to "First,...".
  159. Rinse and repeat.
  160. Inside one console, try typing;
  161. (i) ./tunala -listen localhost:8080 -proxy localhost:8081 -cacert CA.pem \
  162. -cert A-client.pem -out_totals -v_peer -v_strict
  163. In another console, type;
  164. (ii) ./tunala -listen localhost:8081 -proxy localhost:23 -cacert CA.pem \
  165. -cert A-server.pem -server 1 -out_totals -v_peer -v_strict
  166. Now if you open another console and "telnet localhost 8080", you should be
  167. tunneled through to the telnet service on your local machine (if it's running -
  168. you could change it to port "22" and tunnel ssh instead if you so desired). When
  169. you logout of the telnet session, the tunnel should cleanly shutdown and show
  170. you some traffic stats in both consoles. Feel free to experiment. :-)
  171. Notes:
  172. - the format for the "-listen" argument can skip the host part (eg. "-listen
  173. 8080" is fine). If you do, the listening socket will listen on all interfaces
  174. so you can connect from other machines for example. Using the "localhost"
  175. form listens only on 127.0.0.1 so you can only connect locally (unless, of
  176. course, you've set up weird stuff with your networking in which case probably
  177. none of the above applies).
  178. - ./tunala -? gives you a list of other command-line options, but tunala.c is
  179. also a good place to look :-)