sm.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "tunala.h"
  2. #ifndef NO_TUNALA
  3. void state_machine_init(state_machine_t * machine)
  4. {
  5. machine->ssl = NULL;
  6. machine->bio_intossl = machine->bio_fromssl = NULL;
  7. buffer_init(&machine->clean_in);
  8. buffer_init(&machine->clean_out);
  9. buffer_init(&machine->dirty_in);
  10. buffer_init(&machine->dirty_out);
  11. }
  12. void state_machine_close(state_machine_t * machine)
  13. {
  14. if (machine->ssl)
  15. SSL_free(machine->ssl);
  16. /*
  17. * SSL_free seems to decrement the reference counts already so doing this
  18. * goes kaboom.
  19. */
  20. # if 0
  21. if (machine->bio_intossl)
  22. BIO_free(machine->bio_intossl);
  23. if (machine->bio_fromssl)
  24. BIO_free(machine->bio_fromssl);
  25. # endif
  26. buffer_close(&machine->clean_in);
  27. buffer_close(&machine->clean_out);
  28. buffer_close(&machine->dirty_in);
  29. buffer_close(&machine->dirty_out);
  30. state_machine_init(machine);
  31. }
  32. buffer_t *state_machine_get_buffer(state_machine_t * machine,
  33. sm_buffer_t type)
  34. {
  35. switch (type) {
  36. case SM_CLEAN_IN:
  37. return &machine->clean_in;
  38. case SM_CLEAN_OUT:
  39. return &machine->clean_out;
  40. case SM_DIRTY_IN:
  41. return &machine->dirty_in;
  42. case SM_DIRTY_OUT:
  43. return &machine->dirty_out;
  44. default:
  45. break;
  46. }
  47. /* Should never get here */
  48. abort();
  49. return NULL;
  50. }
  51. SSL *state_machine_get_SSL(state_machine_t * machine)
  52. {
  53. return machine->ssl;
  54. }
  55. int state_machine_set_SSL(state_machine_t * machine, SSL *ssl, int is_server)
  56. {
  57. if (machine->ssl)
  58. /* Shouldn't ever be set twice */
  59. abort();
  60. machine->ssl = ssl;
  61. /* Create the BIOs to handle the dirty side of the SSL */
  62. if ((machine->bio_intossl = BIO_new(BIO_s_mem())) == NULL)
  63. abort();
  64. if ((machine->bio_fromssl = BIO_new(BIO_s_mem())) == NULL)
  65. abort();
  66. /* Hook up the BIOs on the dirty side of the SSL */
  67. SSL_set_bio(machine->ssl, machine->bio_intossl, machine->bio_fromssl);
  68. if (is_server)
  69. SSL_set_accept_state(machine->ssl);
  70. else
  71. SSL_set_connect_state(machine->ssl);
  72. /*
  73. * If we're the first one to generate traffic - do it now otherwise we go
  74. * into the next select empty-handed and our peer will not send data but
  75. * will similarly wait for us.
  76. */
  77. return state_machine_churn(machine);
  78. }
  79. /* Performs the data-IO loop and returns zero if the machine should close */
  80. int state_machine_churn(state_machine_t * machine)
  81. {
  82. unsigned int loop;
  83. if (machine->ssl == NULL) {
  84. if (buffer_empty(&machine->clean_out))
  85. /* Time to close this state-machine altogether */
  86. return 0;
  87. else
  88. /* Still buffered data on the clean side to go out */
  89. return 1;
  90. }
  91. /*
  92. * Do this loop twice to cover any dependencies about which precise order
  93. * of reads and writes is required.
  94. */
  95. for (loop = 0; loop < 2; loop++) {
  96. buffer_to_SSL(&machine->clean_in, machine->ssl);
  97. buffer_to_BIO(&machine->dirty_in, machine->bio_intossl);
  98. buffer_from_SSL(&machine->clean_out, machine->ssl);
  99. buffer_from_BIO(&machine->dirty_out, machine->bio_fromssl);
  100. }
  101. /*
  102. * We close on the SSL side if the info callback noticed some problems or
  103. * an SSL shutdown was underway and shutdown traffic had all been sent.
  104. */
  105. if (SSL_get_app_data(machine->ssl) || (SSL_get_shutdown(machine->ssl) &&
  106. buffer_empty(&machine->dirty_out)))
  107. {
  108. /* Great, we can seal off the dirty side completely */
  109. if (!state_machine_close_dirty(machine))
  110. return 0;
  111. }
  112. /*
  113. * Either the SSL is alive and well, or the closing process still has
  114. * outgoing data waiting to be sent
  115. */
  116. return 1;
  117. }
  118. /* Called when the clean side of the SSL has lost its connection */
  119. int state_machine_close_clean(state_machine_t * machine)
  120. {
  121. /*
  122. * Well, first thing to do is null out the clean-side buffers - they're
  123. * no use any more.
  124. */
  125. buffer_close(&machine->clean_in);
  126. buffer_close(&machine->clean_out);
  127. /* And start an SSL shutdown */
  128. if (machine->ssl)
  129. SSL_shutdown(machine->ssl);
  130. /* This is an "event", so flush the SSL of any generated traffic */
  131. state_machine_churn(machine);
  132. if (buffer_empty(&machine->dirty_in) && buffer_empty(&machine->dirty_out))
  133. return 0;
  134. return 1;
  135. }
  136. /*
  137. * Called when the dirty side of the SSL has lost its connection. This is
  138. * pretty terminal as all that can be left to do is send any buffered output
  139. * on the clean side - after that, we're done.
  140. */
  141. int state_machine_close_dirty(state_machine_t * machine)
  142. {
  143. buffer_close(&machine->dirty_in);
  144. buffer_close(&machine->dirty_out);
  145. buffer_close(&machine->clean_in);
  146. if (machine->ssl)
  147. SSL_free(machine->ssl);
  148. machine->ssl = NULL;
  149. machine->bio_intossl = machine->bio_fromssl = NULL;
  150. if (buffer_empty(&machine->clean_out))
  151. return 0;
  152. return 1;
  153. }
  154. #endif /* !defined(NO_TUNALA) */