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