sl@0: /* ssl/ssl_task.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: /* VMS */ sl@0: /* sl@0: * DECnet object for servicing SSL. We accept the inbound and speak a sl@0: * simple protocol for multiplexing the 2 data streams (application and sl@0: * ssl data) over this logical link. sl@0: * sl@0: * Logical names: sl@0: * SSL_CIPHER Defines a list of cipher specifications the server sl@0: * will support in order of preference. sl@0: * SSL_SERVER_CERTIFICATE sl@0: * Points to PEM (privacy enhanced mail) file that sl@0: * contains the server certificate and private password. sl@0: * SYS$NET Logical created by netserver.exe as hook for completing sl@0: * DECnet logical link. sl@0: * sl@0: * Each NSP message sent over the DECnet link has the following structure: sl@0: * struct rpc_msg { sl@0: * char channel; sl@0: * char function; sl@0: * short length; sl@0: * char data[MAX_DATA]; sl@0: * } msg; sl@0: * sl@0: * The channel field designates the virtual data stream this message applies sl@0: * to and is one of: sl@0: * A - Application data (payload). sl@0: * R - Remote client connection that initiated the SSL connection. Encrypted sl@0: * data is sent over this connection. sl@0: * G - General data, reserved for future use. sl@0: * sl@0: * The data streams are half-duplex read/write and have following functions: sl@0: * G - Get, requests that up to msg.length bytes of data be returned. The sl@0: * data is returned in the next 'C' function response that matches the sl@0: * requesting channel. sl@0: * P - Put, requests that the first msg.length bytes of msg.data be appended sl@0: * to the designated stream. sl@0: * C - Confirms a get or put. Every get and put will get a confirm response, sl@0: * you cannot initiate another function on a channel until the previous sl@0: * operation has been confirmed. sl@0: * sl@0: * The 2 channels may interleave their operations, for example: sl@0: * Server msg Client msg sl@0: * A, Get, 4092 ----> sl@0: * <---- R, get, 4092 sl@0: * R, Confirm, {hello} ----> sl@0: * <---- R, put, {srv hello} sl@0: * R, Confirm, 0 ----> sl@0: * . (SSL handshake completed) sl@0: * . (read first app data). sl@0: * <---- A, confirm, {http data} sl@0: * A, Put, {http data} ----> sl@0: * <---- A, confirm, 0 sl@0: * sl@0: * The length field is not permitted to be larger that 4092 bytes. sl@0: * sl@0: * Author: Dave Jones sl@0: * Date: 22-JUL-1996 sl@0: */ sl@0: #include sl@0: #include sl@0: #include /* VMS IO$_ definitions */ sl@0: #include /* VMS string descriptors */ sl@0: extern int SYS$QIOW(), SYS$ASSIGN(); sl@0: int LIB$INIT_TIMER(), LIB$SHOW_TIMER(); sl@0: sl@0: #include /* from ssltest.c */ sl@0: #include sl@0: sl@0: #include "e_os.h" sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: int MS_CALLBACK verify_callback(int ok, X509 *xs, X509 *xi, int depth, sl@0: int error); sl@0: BIO *bio_err=NULL; sl@0: BIO *bio_stdout=NULL; sl@0: BIO_METHOD *BIO_s_rtcp(); sl@0: sl@0: static char *cipher=NULL; sl@0: int verbose=1; sl@0: #ifdef FIONBIO sl@0: static int s_nbio=0; sl@0: #endif sl@0: #define TEST_SERVER_CERT "SSL_SERVER_CERTIFICATE" sl@0: /*************************************************************************/ sl@0: struct rpc_msg { /* Should have member alignment inhibited */ sl@0: char channel; /* 'A'-app data. 'R'-remote client 'G'-global */ sl@0: char function; /* 'G'-get, 'P'-put, 'C'-confirm, 'X'-close */ sl@0: unsigned short int length; /* Amount of data returned or max to return */ sl@0: char data[4092]; /* variable data */ sl@0: }; sl@0: #define RPC_HDR_SIZE (sizeof(struct rpc_msg) - 4092) sl@0: sl@0: static $DESCRIPTOR(sysnet, "SYS$NET"); sl@0: typedef unsigned short io_channel; sl@0: sl@0: struct io_status { sl@0: unsigned short status; sl@0: unsigned short count; sl@0: unsigned long stsval; sl@0: }; sl@0: int doit(io_channel chan, SSL_CTX *s_ctx ); sl@0: /*****************************************************************************/ sl@0: /* Decnet I/O routines. sl@0: */ sl@0: static int get ( io_channel chan, char *buffer, int maxlen, int *length ) sl@0: { sl@0: int status; sl@0: struct io_status iosb; sl@0: status = SYS$QIOW ( 0, chan, IO$_READVBLK, &iosb, 0, 0, sl@0: buffer, maxlen, 0, 0, 0, 0 ); sl@0: if ( (status&1) == 1 ) status = iosb.status; sl@0: if ( (status&1) == 1 ) *length = iosb.count; sl@0: return status; sl@0: } sl@0: sl@0: static int put ( io_channel chan, char *buffer, int length ) sl@0: { sl@0: int status; sl@0: struct io_status iosb; sl@0: status = SYS$QIOW ( 0, chan, IO$_WRITEVBLK, &iosb, 0, 0, sl@0: buffer, length, 0, 0, 0, 0 ); sl@0: if ( (status&1) == 1 ) status = iosb.status; sl@0: return status; sl@0: } sl@0: /***************************************************************************/ sl@0: /* Handle operations on the 'G' channel. sl@0: */ sl@0: static int general_request ( io_channel chan, struct rpc_msg *msg, int length ) sl@0: { sl@0: return 48; sl@0: } sl@0: /***************************************************************************/ sl@0: int main ( int argc, char **argv ) sl@0: { sl@0: int status, length; sl@0: io_channel chan; sl@0: struct rpc_msg msg; sl@0: sl@0: char *CApath=NULL,*CAfile=NULL; sl@0: int badop=0; sl@0: int ret=1; sl@0: int client_auth=0; sl@0: int server_auth=0; sl@0: SSL_CTX *s_ctx=NULL; sl@0: /* sl@0: * Confirm logical link with initiating client. sl@0: */ sl@0: LIB$INIT_TIMER(); sl@0: status = SYS$ASSIGN ( &sysnet, &chan, 0, 0, 0 ); sl@0: printf("status of assign to SYS$NET: %d\n", status ); sl@0: /* sl@0: * Initialize standard out and error files. sl@0: */ sl@0: if (bio_err == NULL) sl@0: if ((bio_err=BIO_new(BIO_s_file())) != NULL) sl@0: BIO_set_fp(bio_err,stderr,BIO_NOCLOSE); sl@0: if (bio_stdout == NULL) sl@0: if ((bio_stdout=BIO_new(BIO_s_file())) != NULL) sl@0: BIO_set_fp(bio_stdout,stdout,BIO_NOCLOSE); sl@0: /* sl@0: * get the preferred cipher list and other initialization sl@0: */ sl@0: if (cipher == NULL) cipher=getenv("SSL_CIPHER"); sl@0: printf("cipher list: %s\n", cipher ? cipher : "{undefined}" ); sl@0: sl@0: SSL_load_error_strings(); sl@0: OpenSSL_add_all_algorithms(); sl@0: sl@0: /* DRM, this was the original, but there is no such thing as SSLv2() sl@0: s_ctx=SSL_CTX_new(SSLv2()); sl@0: */ sl@0: s_ctx=SSL_CTX_new(SSLv2_server_method()); sl@0: sl@0: if (s_ctx == NULL) goto end; sl@0: sl@0: SSL_CTX_use_certificate_file(s_ctx,TEST_SERVER_CERT,SSL_FILETYPE_PEM); sl@0: SSL_CTX_use_RSAPrivateKey_file(s_ctx,TEST_SERVER_CERT,SSL_FILETYPE_PEM); sl@0: printf("Loaded server certificate: '%s'\n", TEST_SERVER_CERT ); sl@0: sl@0: /* sl@0: * Take commands from client until bad status. sl@0: */ sl@0: LIB$SHOW_TIMER(); sl@0: status = doit ( chan, s_ctx ); sl@0: LIB$SHOW_TIMER(); sl@0: /* sl@0: * do final cleanup and exit. sl@0: */ sl@0: end: sl@0: if (s_ctx != NULL) SSL_CTX_free(s_ctx); sl@0: LIB$SHOW_TIMER(); sl@0: return 1; sl@0: } sl@0: sl@0: int doit(io_channel chan, SSL_CTX *s_ctx ) sl@0: { sl@0: int status, length, link_state; sl@0: struct rpc_msg msg; sl@0: sl@0: SSL *s_ssl=NULL; sl@0: BIO *c_to_s=NULL; sl@0: BIO *s_to_c=NULL; sl@0: BIO *c_bio=NULL; sl@0: BIO *s_bio=NULL; sl@0: int i; sl@0: int done=0; sl@0: sl@0: s_ssl=SSL_new(s_ctx); sl@0: if (s_ssl == NULL) goto err; sl@0: sl@0: c_to_s=BIO_new(BIO_s_rtcp()); sl@0: s_to_c=BIO_new(BIO_s_rtcp()); sl@0: if ((s_to_c == NULL) || (c_to_s == NULL)) goto err; sl@0: /* original, DRM 24-SEP-1997 sl@0: BIO_set_fd ( c_to_s, "", chan ); sl@0: BIO_set_fd ( s_to_c, "", chan ); sl@0: */ sl@0: BIO_set_fd ( c_to_s, 0, chan ); sl@0: BIO_set_fd ( s_to_c, 0, chan ); sl@0: sl@0: c_bio=BIO_new(BIO_f_ssl()); sl@0: s_bio=BIO_new(BIO_f_ssl()); sl@0: if ((c_bio == NULL) || (s_bio == NULL)) goto err; sl@0: sl@0: SSL_set_accept_state(s_ssl); sl@0: SSL_set_bio(s_ssl,c_to_s,s_to_c); sl@0: BIO_set_ssl(s_bio,s_ssl,BIO_CLOSE); sl@0: sl@0: /* We can always do writes */ sl@0: printf("Begin doit main loop\n"); sl@0: /* sl@0: * Link states: 0-idle, 1-read pending, 2-write pending, 3-closed. sl@0: */ sl@0: for (link_state = 0; link_state < 3; ) { sl@0: /* sl@0: * Wait for remote end to request data action on A channel. sl@0: */ sl@0: while ( link_state == 0 ) { sl@0: status = get ( chan, (char *) &msg, sizeof(msg), &length ); sl@0: if ( (status&1) == 0 ) { sl@0: printf("Error in main loop get: %d\n", status ); sl@0: link_state = 3; sl@0: break; sl@0: } sl@0: if ( length < RPC_HDR_SIZE ) { sl@0: printf("Error in main loop get size: %d\n", length ); sl@0: break; sl@0: link_state = 3; sl@0: } sl@0: if ( msg.channel != 'A' ) { sl@0: printf("Error in main loop, unexpected channel: %c\n", sl@0: msg.channel ); sl@0: break; sl@0: link_state = 3; sl@0: } sl@0: if ( msg.function == 'G' ) { sl@0: link_state = 1; sl@0: } else if ( msg.function == 'P' ) { sl@0: link_state = 2; /* write pending */ sl@0: } else if ( msg.function == 'X' ) { sl@0: link_state = 3; sl@0: } else { sl@0: link_state = 3; sl@0: } sl@0: } sl@0: if ( link_state == 1 ) { sl@0: i = BIO_read ( s_bio, msg.data, msg.length ); sl@0: if ( i < 0 ) link_state = 3; sl@0: else { sl@0: msg.channel = 'A'; sl@0: msg.function = 'C'; /* confirm */ sl@0: msg.length = i; sl@0: status = put ( chan, (char *) &msg, i+RPC_HDR_SIZE ); sl@0: if ( (status&1) == 0 ) break; sl@0: link_state = 0; sl@0: } sl@0: } else if ( link_state == 2 ) { sl@0: i = BIO_write ( s_bio, msg.data, msg.length ); sl@0: if ( i < 0 ) link_state = 3; sl@0: else { sl@0: msg.channel = 'A'; sl@0: msg.function = 'C'; /* confirm */ sl@0: msg.length = 0; sl@0: status = put ( chan, (char *) &msg, RPC_HDR_SIZE ); sl@0: if ( (status&1) == 0 ) break; sl@0: link_state = 0; sl@0: } sl@0: } sl@0: } sl@0: fprintf(stdout,"DONE\n"); sl@0: err: sl@0: /* We have to set the BIO's to NULL otherwise they will be sl@0: * free()ed twice. Once when th s_ssl is SSL_free()ed and sl@0: * again when c_ssl is SSL_free()ed. sl@0: * This is a hack required because s_ssl and c_ssl are sharing the same sl@0: * BIO structure and SSL_set_bio() and SSL_free() automatically sl@0: * BIO_free non NULL entries. sl@0: * You should not normally do this or be required to do this */ sl@0: s_ssl->rbio=NULL; sl@0: s_ssl->wbio=NULL; sl@0: sl@0: if (c_to_s != NULL) BIO_free(c_to_s); sl@0: if (s_to_c != NULL) BIO_free(s_to_c); sl@0: if (c_bio != NULL) BIO_free(c_bio); sl@0: if (s_bio != NULL) BIO_free(s_bio); sl@0: return(0); sl@0: }