xref: /minix3/crypto/external/bsd/openssl/dist/ssl/ssl_task.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* ssl/ssl_task.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc /* VMS */
60*0a6a1f1dSLionel Sambuc /*-
61ebfedea0SLionel Sambuc  * DECnet object for servicing SSL.  We accept the inbound and speak a
62ebfedea0SLionel Sambuc  * simple protocol for multiplexing the 2 data streams (application and
63ebfedea0SLionel Sambuc  * ssl data) over this logical link.
64ebfedea0SLionel Sambuc  *
65ebfedea0SLionel Sambuc  * Logical names:
66ebfedea0SLionel Sambuc  *    SSL_CIPHER        Defines a list of cipher specifications the server
67ebfedea0SLionel Sambuc  *                      will support in order of preference.
68ebfedea0SLionel Sambuc  *    SSL_SERVER_CERTIFICATE
69ebfedea0SLionel Sambuc  *                      Points to PEM (privacy enhanced mail) file that
70ebfedea0SLionel Sambuc  *                      contains the server certificate and private password.
71ebfedea0SLionel Sambuc  *    SYS$NET           Logical created by netserver.exe as hook for completing
72ebfedea0SLionel Sambuc  *                      DECnet logical link.
73ebfedea0SLionel Sambuc  *
74ebfedea0SLionel Sambuc  * Each NSP message sent over the DECnet link has the following structure:
75ebfedea0SLionel Sambuc  *    struct rpc_msg {
76ebfedea0SLionel Sambuc  *      char channel;
77ebfedea0SLionel Sambuc  *      char function;
78ebfedea0SLionel Sambuc  *      short length;
79ebfedea0SLionel Sambuc  *      char data[MAX_DATA];
80ebfedea0SLionel Sambuc  *    } msg;
81ebfedea0SLionel Sambuc  *
82ebfedea0SLionel Sambuc  * The channel field designates the virtual data stream this message applies
83ebfedea0SLionel Sambuc  * to and is one of:
84ebfedea0SLionel Sambuc  *   A - Application data (payload).
85ebfedea0SLionel Sambuc  *   R - Remote client connection that initiated the SSL connection.  Encrypted
86ebfedea0SLionel Sambuc  *       data is sent over this connection.
87ebfedea0SLionel Sambuc  *   G - General data, reserved for future use.
88ebfedea0SLionel Sambuc  *
89ebfedea0SLionel Sambuc  * The data streams are half-duplex read/write and have following functions:
90ebfedea0SLionel Sambuc  *   G - Get, requests that up to msg.length bytes of data be returned.  The
91ebfedea0SLionel Sambuc  *       data is returned in the next 'C' function response that matches the
92ebfedea0SLionel Sambuc  *       requesting channel.
93ebfedea0SLionel Sambuc  *   P - Put, requests that the first msg.length bytes of msg.data be appended
94ebfedea0SLionel Sambuc  *       to the designated stream.
95ebfedea0SLionel Sambuc  *   C - Confirms a get or put.  Every get and put will get a confirm response,
96ebfedea0SLionel Sambuc  *       you cannot initiate another function on a channel until the previous
97ebfedea0SLionel Sambuc  *       operation has been confirmed.
98ebfedea0SLionel Sambuc  *
99ebfedea0SLionel Sambuc  *  The 2 channels may interleave their operations, for example:
100ebfedea0SLionel Sambuc  *        Server msg           Client msg
101ebfedea0SLionel Sambuc  *         A, Get, 4092          ---->
102ebfedea0SLionel Sambuc  *                               <----  R, get, 4092
103ebfedea0SLionel Sambuc  *         R, Confirm, {hello}   ---->
104ebfedea0SLionel Sambuc  *                               <----  R, put, {srv hello}
105ebfedea0SLionel Sambuc  *         R, Confirm, 0         ---->
106ebfedea0SLionel Sambuc  *                               .              (SSL handshake completed)
107ebfedea0SLionel Sambuc  *                               .              (read first app data).
108ebfedea0SLionel Sambuc  *                               <----  A, confirm, {http data}
109ebfedea0SLionel Sambuc  *         A, Put, {http data}   ---->
110ebfedea0SLionel Sambuc  *                               <----  A, confirm, 0
111ebfedea0SLionel Sambuc  *
112ebfedea0SLionel Sambuc  *  The length field is not permitted to be larger that 4092 bytes.
113ebfedea0SLionel Sambuc  *
114ebfedea0SLionel Sambuc  * Author: Dave Jones
115ebfedea0SLionel Sambuc  * Date:   22-JUL-1996
116ebfedea0SLionel Sambuc  */
117ebfedea0SLionel Sambuc #include <stdlib.h>
118ebfedea0SLionel Sambuc #include <stdio.h>
119ebfedea0SLionel Sambuc #include <iodef.h>              /* VMS IO$_ definitions */
120ebfedea0SLionel Sambuc #include <descrip.h>            /* VMS string descriptors */
121ebfedea0SLionel Sambuc extern int SYS$QIOW(), SYS$ASSIGN();
122ebfedea0SLionel Sambuc int LIB$INIT_TIMER(), LIB$SHOW_TIMER();
123ebfedea0SLionel Sambuc 
124ebfedea0SLionel Sambuc #include <string.h>             /* from ssltest.c */
125ebfedea0SLionel Sambuc #include <errno.h>
126ebfedea0SLionel Sambuc 
127ebfedea0SLionel Sambuc #include "e_os.h"
128ebfedea0SLionel Sambuc 
129ebfedea0SLionel Sambuc #include <openssl/buffer.h>
130ebfedea0SLionel Sambuc #include <openssl/x509.h>
131ebfedea0SLionel Sambuc #include <openssl/ssl.h>
132ebfedea0SLionel Sambuc #include <openssl/err.h>
133ebfedea0SLionel Sambuc 
134ebfedea0SLionel Sambuc int MS_CALLBACK verify_callback(int ok, X509 *xs, X509 *xi, int depth,
135ebfedea0SLionel Sambuc                                 int error);
136ebfedea0SLionel Sambuc BIO *bio_err = NULL;
137ebfedea0SLionel Sambuc BIO *bio_stdout = NULL;
138ebfedea0SLionel Sambuc BIO_METHOD *BIO_s_rtcp();
139ebfedea0SLionel Sambuc 
140ebfedea0SLionel Sambuc static char *cipher = NULL;
141ebfedea0SLionel Sambuc int verbose = 1;
142ebfedea0SLionel Sambuc #ifdef FIONBIO
143ebfedea0SLionel Sambuc static int s_nbio = 0;
144ebfedea0SLionel Sambuc #endif
145ebfedea0SLionel Sambuc #define TEST_SERVER_CERT "SSL_SERVER_CERTIFICATE"
146ebfedea0SLionel Sambuc /*************************************************************************/
147*0a6a1f1dSLionel Sambuc /* Should have member alignment inhibited */
148*0a6a1f1dSLionel Sambuc struct rpc_msg {
149*0a6a1f1dSLionel Sambuc     /* 'A'-app data. 'R'-remote client 'G'-global */
150*0a6a1f1dSLionel Sambuc     char channel;
151*0a6a1f1dSLionel Sambuc     /* 'G'-get, 'P'-put, 'C'-confirm, 'X'-close */
152*0a6a1f1dSLionel Sambuc     char function;
153*0a6a1f1dSLionel Sambuc     /* Amount of data returned or max to return */
154*0a6a1f1dSLionel Sambuc     unsigned short int length;
155*0a6a1f1dSLionel Sambuc     /* variable data */
156*0a6a1f1dSLionel Sambuc     char data[4092];
157ebfedea0SLionel Sambuc };
158ebfedea0SLionel Sambuc #define RPC_HDR_SIZE (sizeof(struct rpc_msg) - 4092)
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc static $DESCRIPTOR(sysnet, "SYS$NET");
161ebfedea0SLionel Sambuc typedef unsigned short io_channel;
162ebfedea0SLionel Sambuc 
163ebfedea0SLionel Sambuc struct io_status {
164ebfedea0SLionel Sambuc     unsigned short status;
165ebfedea0SLionel Sambuc     unsigned short count;
166ebfedea0SLionel Sambuc     unsigned long stsval;
167ebfedea0SLionel Sambuc };
168ebfedea0SLionel Sambuc int doit(io_channel chan, SSL_CTX *s_ctx);
169ebfedea0SLionel Sambuc /*****************************************************************************/
170*0a6a1f1dSLionel Sambuc /*
171*0a6a1f1dSLionel Sambuc  * Decnet I/O routines.
172ebfedea0SLionel Sambuc  */
get(io_channel chan,char * buffer,int maxlen,int * length)173ebfedea0SLionel Sambuc static int get(io_channel chan, char *buffer, int maxlen, int *length)
174ebfedea0SLionel Sambuc {
175ebfedea0SLionel Sambuc     int status;
176ebfedea0SLionel Sambuc     struct io_status iosb;
177ebfedea0SLionel Sambuc     status = SYS$QIOW(0, chan, IO$_READVBLK, &iosb, 0, 0,
178ebfedea0SLionel Sambuc                       buffer, maxlen, 0, 0, 0, 0);
179*0a6a1f1dSLionel Sambuc     if ((status & 1) == 1)
180*0a6a1f1dSLionel Sambuc         status = iosb.status;
181*0a6a1f1dSLionel Sambuc     if ((status & 1) == 1)
182*0a6a1f1dSLionel Sambuc         *length = iosb.count;
183ebfedea0SLionel Sambuc     return status;
184ebfedea0SLionel Sambuc }
185ebfedea0SLionel Sambuc 
put(io_channel chan,char * buffer,int length)186ebfedea0SLionel Sambuc static int put(io_channel chan, char *buffer, int length)
187ebfedea0SLionel Sambuc {
188ebfedea0SLionel Sambuc     int status;
189ebfedea0SLionel Sambuc     struct io_status iosb;
190ebfedea0SLionel Sambuc     status = SYS$QIOW(0, chan, IO$_WRITEVBLK, &iosb, 0, 0,
191ebfedea0SLionel Sambuc                       buffer, length, 0, 0, 0, 0);
192*0a6a1f1dSLionel Sambuc     if ((status & 1) == 1)
193*0a6a1f1dSLionel Sambuc         status = iosb.status;
194ebfedea0SLionel Sambuc     return status;
195ebfedea0SLionel Sambuc }
196*0a6a1f1dSLionel Sambuc 
197ebfedea0SLionel Sambuc /***************************************************************************/
198*0a6a1f1dSLionel Sambuc /*
199*0a6a1f1dSLionel Sambuc  * Handle operations on the 'G' channel.
200ebfedea0SLionel Sambuc  */
general_request(io_channel chan,struct rpc_msg * msg,int length)201ebfedea0SLionel Sambuc static int general_request(io_channel chan, struct rpc_msg *msg, int length)
202ebfedea0SLionel Sambuc {
203ebfedea0SLionel Sambuc     return 48;
204ebfedea0SLionel Sambuc }
205*0a6a1f1dSLionel Sambuc 
206ebfedea0SLionel Sambuc /***************************************************************************/
main(int argc,char ** argv)207ebfedea0SLionel Sambuc int main(int argc, char **argv)
208ebfedea0SLionel Sambuc {
209ebfedea0SLionel Sambuc     int status, length;
210ebfedea0SLionel Sambuc     io_channel chan;
211ebfedea0SLionel Sambuc     struct rpc_msg msg;
212ebfedea0SLionel Sambuc 
213ebfedea0SLionel Sambuc     char *CApath = NULL, *CAfile = NULL;
214ebfedea0SLionel Sambuc     int badop = 0;
215ebfedea0SLionel Sambuc     int ret = 1;
216ebfedea0SLionel Sambuc     int client_auth = 0;
217ebfedea0SLionel Sambuc     int server_auth = 0;
218ebfedea0SLionel Sambuc     SSL_CTX *s_ctx = NULL;
219ebfedea0SLionel Sambuc     /*
220ebfedea0SLionel Sambuc      * Confirm logical link with initiating client.
221ebfedea0SLionel Sambuc      */
222ebfedea0SLionel Sambuc     LIB$INIT_TIMER();
223ebfedea0SLionel Sambuc     status = SYS$ASSIGN(&sysnet, &chan, 0, 0, 0);
224ebfedea0SLionel Sambuc     printf("status of assign to SYS$NET: %d\n", status);
225ebfedea0SLionel Sambuc     /*
226ebfedea0SLionel Sambuc      * Initialize standard out and error files.
227ebfedea0SLionel Sambuc      */
228ebfedea0SLionel Sambuc     if (bio_err == NULL)
229ebfedea0SLionel Sambuc         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
230ebfedea0SLionel Sambuc             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE);
231ebfedea0SLionel Sambuc     if (bio_stdout == NULL)
232ebfedea0SLionel Sambuc         if ((bio_stdout = BIO_new(BIO_s_file())) != NULL)
233ebfedea0SLionel Sambuc             BIO_set_fp(bio_stdout, stdout, BIO_NOCLOSE);
234ebfedea0SLionel Sambuc     /*
235ebfedea0SLionel Sambuc      * get the preferred cipher list and other initialization
236ebfedea0SLionel Sambuc      */
237*0a6a1f1dSLionel Sambuc     if (cipher == NULL)
238*0a6a1f1dSLionel Sambuc         cipher = getenv("SSL_CIPHER");
239ebfedea0SLionel Sambuc     printf("cipher list: %s\n", cipher ? cipher : "{undefined}");
240ebfedea0SLionel Sambuc 
241ebfedea0SLionel Sambuc     SSL_load_error_strings();
242ebfedea0SLionel Sambuc     OpenSSL_add_all_algorithms();
243ebfedea0SLionel Sambuc 
244*0a6a1f1dSLionel Sambuc     /*
245*0a6a1f1dSLionel Sambuc      * DRM, this was the original, but there is no such thing as SSLv2()
246*0a6a1f1dSLionel Sambuc      * s_ctx=SSL_CTX_new(SSLv2());
247ebfedea0SLionel Sambuc      */
248ebfedea0SLionel Sambuc     s_ctx = SSL_CTX_new(SSLv2_server_method());
249ebfedea0SLionel Sambuc 
250*0a6a1f1dSLionel Sambuc     if (s_ctx == NULL)
251*0a6a1f1dSLionel Sambuc         goto end;
252ebfedea0SLionel Sambuc 
253ebfedea0SLionel Sambuc     SSL_CTX_use_certificate_file(s_ctx, TEST_SERVER_CERT, SSL_FILETYPE_PEM);
254ebfedea0SLionel Sambuc     SSL_CTX_use_RSAPrivateKey_file(s_ctx, TEST_SERVER_CERT, SSL_FILETYPE_PEM);
255ebfedea0SLionel Sambuc     printf("Loaded server certificate: '%s'\n", TEST_SERVER_CERT);
256ebfedea0SLionel Sambuc 
257ebfedea0SLionel Sambuc     /*
258ebfedea0SLionel Sambuc      * Take commands from client until bad status.
259ebfedea0SLionel Sambuc      */
260ebfedea0SLionel Sambuc     LIB$SHOW_TIMER();
261ebfedea0SLionel Sambuc     status = doit(chan, s_ctx);
262ebfedea0SLionel Sambuc     LIB$SHOW_TIMER();
263ebfedea0SLionel Sambuc     /*
264ebfedea0SLionel Sambuc      * do final cleanup and exit.
265ebfedea0SLionel Sambuc      */
266ebfedea0SLionel Sambuc  end:
267*0a6a1f1dSLionel Sambuc     if (s_ctx != NULL)
268*0a6a1f1dSLionel Sambuc         SSL_CTX_free(s_ctx);
269ebfedea0SLionel Sambuc     LIB$SHOW_TIMER();
270ebfedea0SLionel Sambuc     return 1;
271ebfedea0SLionel Sambuc }
272ebfedea0SLionel Sambuc 
doit(io_channel chan,SSL_CTX * s_ctx)273ebfedea0SLionel Sambuc int doit(io_channel chan, SSL_CTX *s_ctx)
274ebfedea0SLionel Sambuc {
275ebfedea0SLionel Sambuc     int status, length, link_state;
276ebfedea0SLionel Sambuc     struct rpc_msg msg;
277ebfedea0SLionel Sambuc 
278ebfedea0SLionel Sambuc     SSL *s_ssl = NULL;
279ebfedea0SLionel Sambuc     BIO *c_to_s = NULL;
280ebfedea0SLionel Sambuc     BIO *s_to_c = NULL;
281ebfedea0SLionel Sambuc     BIO *c_bio = NULL;
282ebfedea0SLionel Sambuc     BIO *s_bio = NULL;
283ebfedea0SLionel Sambuc     int i;
284ebfedea0SLionel Sambuc     int done = 0;
285ebfedea0SLionel Sambuc 
286ebfedea0SLionel Sambuc     s_ssl = SSL_new(s_ctx);
287*0a6a1f1dSLionel Sambuc     if (s_ssl == NULL)
288*0a6a1f1dSLionel Sambuc         goto err;
289ebfedea0SLionel Sambuc 
290ebfedea0SLionel Sambuc     c_to_s = BIO_new(BIO_s_rtcp());
291ebfedea0SLionel Sambuc     s_to_c = BIO_new(BIO_s_rtcp());
292*0a6a1f1dSLionel Sambuc     if ((s_to_c == NULL) || (c_to_s == NULL))
293*0a6a1f1dSLionel Sambuc         goto err;
294*0a6a1f1dSLionel Sambuc /*- original, DRM 24-SEP-1997
295ebfedea0SLionel Sambuc     BIO_set_fd ( c_to_s, "", chan );
296ebfedea0SLionel Sambuc     BIO_set_fd ( s_to_c, "", chan );
297ebfedea0SLionel Sambuc */
298ebfedea0SLionel Sambuc     BIO_set_fd(c_to_s, 0, chan);
299ebfedea0SLionel Sambuc     BIO_set_fd(s_to_c, 0, chan);
300ebfedea0SLionel Sambuc 
301ebfedea0SLionel Sambuc     c_bio = BIO_new(BIO_f_ssl());
302ebfedea0SLionel Sambuc     s_bio = BIO_new(BIO_f_ssl());
303*0a6a1f1dSLionel Sambuc     if ((c_bio == NULL) || (s_bio == NULL))
304*0a6a1f1dSLionel Sambuc         goto err;
305ebfedea0SLionel Sambuc 
306ebfedea0SLionel Sambuc     SSL_set_accept_state(s_ssl);
307ebfedea0SLionel Sambuc     SSL_set_bio(s_ssl, c_to_s, s_to_c);
308ebfedea0SLionel Sambuc     BIO_set_ssl(s_bio, s_ssl, BIO_CLOSE);
309ebfedea0SLionel Sambuc 
310ebfedea0SLionel Sambuc     /* We can always do writes */
311ebfedea0SLionel Sambuc     printf("Begin doit main loop\n");
312ebfedea0SLionel Sambuc     /*
313ebfedea0SLionel Sambuc      * Link states: 0-idle, 1-read pending, 2-write pending, 3-closed.
314ebfedea0SLionel Sambuc      */
315ebfedea0SLionel Sambuc     for (link_state = 0; link_state < 3;) {
316ebfedea0SLionel Sambuc         /*
317ebfedea0SLionel Sambuc          * Wait for remote end to request data action on A channel.
318ebfedea0SLionel Sambuc          */
319ebfedea0SLionel Sambuc         while (link_state == 0) {
320ebfedea0SLionel Sambuc             status = get(chan, (char *)&msg, sizeof(msg), &length);
321ebfedea0SLionel Sambuc             if ((status & 1) == 0) {
322ebfedea0SLionel Sambuc                 printf("Error in main loop get: %d\n", status);
323ebfedea0SLionel Sambuc                 link_state = 3;
324ebfedea0SLionel Sambuc                 break;
325ebfedea0SLionel Sambuc             }
326ebfedea0SLionel Sambuc             if (length < RPC_HDR_SIZE) {
327ebfedea0SLionel Sambuc                 printf("Error in main loop get size: %d\n", length);
328ebfedea0SLionel Sambuc                 break;
329ebfedea0SLionel Sambuc                 link_state = 3;
330ebfedea0SLionel Sambuc             }
331ebfedea0SLionel Sambuc             if (msg.channel != 'A') {
332ebfedea0SLionel Sambuc                 printf("Error in main loop, unexpected channel: %c\n",
333ebfedea0SLionel Sambuc                        msg.channel);
334ebfedea0SLionel Sambuc                 break;
335ebfedea0SLionel Sambuc                 link_state = 3;
336ebfedea0SLionel Sambuc             }
337ebfedea0SLionel Sambuc             if (msg.function == 'G') {
338ebfedea0SLionel Sambuc                 link_state = 1;
339ebfedea0SLionel Sambuc             } else if (msg.function == 'P') {
340ebfedea0SLionel Sambuc                 link_state = 2; /* write pending */
341ebfedea0SLionel Sambuc             } else if (msg.function == 'X') {
342ebfedea0SLionel Sambuc                 link_state = 3;
343ebfedea0SLionel Sambuc             } else {
344ebfedea0SLionel Sambuc                 link_state = 3;
345ebfedea0SLionel Sambuc             }
346ebfedea0SLionel Sambuc         }
347ebfedea0SLionel Sambuc         if (link_state == 1) {
348ebfedea0SLionel Sambuc             i = BIO_read(s_bio, msg.data, msg.length);
349*0a6a1f1dSLionel Sambuc             if (i < 0)
350*0a6a1f1dSLionel Sambuc                 link_state = 3;
351ebfedea0SLionel Sambuc             else {
352ebfedea0SLionel Sambuc                 msg.channel = 'A';
353ebfedea0SLionel Sambuc                 msg.function = 'C'; /* confirm */
354ebfedea0SLionel Sambuc                 msg.length = i;
355ebfedea0SLionel Sambuc                 status = put(chan, (char *)&msg, i + RPC_HDR_SIZE);
356*0a6a1f1dSLionel Sambuc                 if ((status & 1) == 0)
357*0a6a1f1dSLionel Sambuc                     break;
358ebfedea0SLionel Sambuc                 link_state = 0;
359ebfedea0SLionel Sambuc             }
360ebfedea0SLionel Sambuc         } else if (link_state == 2) {
361ebfedea0SLionel Sambuc             i = BIO_write(s_bio, msg.data, msg.length);
362*0a6a1f1dSLionel Sambuc             if (i < 0)
363*0a6a1f1dSLionel Sambuc                 link_state = 3;
364ebfedea0SLionel Sambuc             else {
365ebfedea0SLionel Sambuc                 msg.channel = 'A';
366ebfedea0SLionel Sambuc                 msg.function = 'C'; /* confirm */
367ebfedea0SLionel Sambuc                 msg.length = 0;
368ebfedea0SLionel Sambuc                 status = put(chan, (char *)&msg, RPC_HDR_SIZE);
369*0a6a1f1dSLionel Sambuc                 if ((status & 1) == 0)
370*0a6a1f1dSLionel Sambuc                     break;
371ebfedea0SLionel Sambuc                 link_state = 0;
372ebfedea0SLionel Sambuc             }
373ebfedea0SLionel Sambuc         }
374ebfedea0SLionel Sambuc     }
375ebfedea0SLionel Sambuc     fprintf(stdout, "DONE\n");
376ebfedea0SLionel Sambuc  err:
377*0a6a1f1dSLionel Sambuc     /*
378*0a6a1f1dSLionel Sambuc      * We have to set the BIO's to NULL otherwise they will be free()ed
379*0a6a1f1dSLionel Sambuc      * twice.  Once when th s_ssl is SSL_free()ed and again when c_ssl is
380*0a6a1f1dSLionel Sambuc      * SSL_free()ed. This is a hack required because s_ssl and c_ssl are
381*0a6a1f1dSLionel Sambuc      * sharing the same BIO structure and SSL_set_bio() and SSL_free()
382*0a6a1f1dSLionel Sambuc      * automatically BIO_free non NULL entries. You should not normally do
383*0a6a1f1dSLionel Sambuc      * this or be required to do this
384*0a6a1f1dSLionel Sambuc      */
385ebfedea0SLionel Sambuc     s_ssl->rbio = NULL;
386ebfedea0SLionel Sambuc     s_ssl->wbio = NULL;
387ebfedea0SLionel Sambuc 
388*0a6a1f1dSLionel Sambuc     if (c_to_s != NULL)
389*0a6a1f1dSLionel Sambuc         BIO_free(c_to_s);
390*0a6a1f1dSLionel Sambuc     if (s_to_c != NULL)
391*0a6a1f1dSLionel Sambuc         BIO_free(s_to_c);
392*0a6a1f1dSLionel Sambuc     if (c_bio != NULL)
393*0a6a1f1dSLionel Sambuc         BIO_free(c_bio);
394*0a6a1f1dSLionel Sambuc     if (s_bio != NULL)
395*0a6a1f1dSLionel Sambuc         BIO_free(s_bio);
396ebfedea0SLionel Sambuc     return (0);
397ebfedea0SLionel Sambuc }
398