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