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