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