os/ossrv/ssl/libssl/src/ssl_task.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libssl/src/ssl_task.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,369 @@
     1.4 +/* ssl/ssl_task.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +
    1.62 +/* VMS */
    1.63 +/*
    1.64 + * DECnet object for servicing SSL.  We accept the inbound and speak a
    1.65 + * simple protocol for multiplexing the 2 data streams (application and
    1.66 + * ssl data) over this logical link.
    1.67 + *
    1.68 + * Logical names:
    1.69 + *    SSL_CIPHER	Defines a list of cipher specifications the server
    1.70 + *			will support in order of preference.
    1.71 + *    SSL_SERVER_CERTIFICATE
    1.72 + *			Points to PEM (privacy enhanced mail) file that
    1.73 + *			contains the server certificate and private password.
    1.74 + *    SYS$NET		Logical created by netserver.exe as hook for completing
    1.75 + *			DECnet logical link.
    1.76 + *
    1.77 + * Each NSP message sent over the DECnet link has the following structure:
    1.78 + *    struct rpc_msg { 
    1.79 + *      char channel;
    1.80 + *      char function;
    1.81 + *      short length;
    1.82 + *      char data[MAX_DATA];
    1.83 + *    } msg;
    1.84 + *
    1.85 + * The channel field designates the virtual data stream this message applies
    1.86 + * to and is one of:
    1.87 + *   A - Application data (payload).
    1.88 + *   R - Remote client connection that initiated the SSL connection.  Encrypted
    1.89 + *       data is sent over this connection.
    1.90 + *   G - General data, reserved for future use.
    1.91 + *
    1.92 + * The data streams are half-duplex read/write and have following functions:
    1.93 + *   G - Get, requests that up to msg.length bytes of data be returned.  The
    1.94 + *       data is returned in the next 'C' function response that matches the
    1.95 + *       requesting channel.
    1.96 + *   P - Put, requests that the first msg.length bytes of msg.data be appended
    1.97 + *       to the designated stream.
    1.98 + *   C - Confirms a get or put.  Every get and put will get a confirm response,
    1.99 + *       you cannot initiate another function on a channel until the previous
   1.100 + *       operation has been confirmed.
   1.101 + *
   1.102 + *  The 2 channels may interleave their operations, for example:
   1.103 + *        Server msg           Client msg
   1.104 + *         A, Get, 4092          ---->
   1.105 + *                               <----  R, get, 4092
   1.106 + *         R, Confirm, {hello}   ---->
   1.107 + *                               <----  R, put, {srv hello}
   1.108 + *         R, Confirm, 0         ---->
   1.109 + *                               .		(SSL handshake completed)
   1.110 + *                               .              (read first app data).
   1.111 + *                               <----  A, confirm, {http data}
   1.112 + *         A, Put, {http data}   ---->
   1.113 + *                               <----  A, confirm, 0
   1.114 + *
   1.115 + *  The length field is not permitted to be larger that 4092 bytes.
   1.116 + *
   1.117 + * Author: Dave Jones
   1.118 + * Date:   22-JUL-1996
   1.119 + */
   1.120 +#include <stdlib.h>
   1.121 +#include <stdio.h>
   1.122 +#include <iodef.h>		/* VMS IO$_ definitions */
   1.123 +#include <descrip.h>		/* VMS string descriptors */
   1.124 +extern int SYS$QIOW(), SYS$ASSIGN();
   1.125 +int LIB$INIT_TIMER(), LIB$SHOW_TIMER();
   1.126 +
   1.127 +#include <string.h>		/* from ssltest.c */
   1.128 +#include <errno.h>
   1.129 +
   1.130 +#include "e_os.h"
   1.131 +
   1.132 +#include <openssl/buffer.h>
   1.133 +#include <openssl/x509.h>
   1.134 +#include <openssl/ssl.h>
   1.135 +#include <openssl/err.h>
   1.136 +
   1.137 +int MS_CALLBACK verify_callback(int ok, X509 *xs, X509 *xi, int depth,
   1.138 +	int error);
   1.139 +BIO *bio_err=NULL;
   1.140 +BIO *bio_stdout=NULL;
   1.141 +BIO_METHOD *BIO_s_rtcp();
   1.142 +
   1.143 +static char *cipher=NULL;
   1.144 +int verbose=1;
   1.145 +#ifdef FIONBIO
   1.146 +static int s_nbio=0;
   1.147 +#endif
   1.148 +#define TEST_SERVER_CERT "SSL_SERVER_CERTIFICATE"
   1.149 +/*************************************************************************/
   1.150 +struct rpc_msg {		/* Should have member alignment inhibited */
   1.151 +   char channel;		/* 'A'-app data. 'R'-remote client 'G'-global */
   1.152 +   char function;		/* 'G'-get, 'P'-put, 'C'-confirm, 'X'-close */
   1.153 +   unsigned short int length;	/* Amount of data returned or max to return */
   1.154 +   char data[4092];		/* variable data */
   1.155 +};
   1.156 +#define RPC_HDR_SIZE (sizeof(struct rpc_msg) - 4092)
   1.157 +
   1.158 +static $DESCRIPTOR(sysnet, "SYS$NET");
   1.159 +typedef unsigned short io_channel;
   1.160 +
   1.161 +struct io_status {
   1.162 +    unsigned short status;
   1.163 +    unsigned short count;
   1.164 +    unsigned long stsval;
   1.165 +};
   1.166 +int doit(io_channel chan, SSL_CTX *s_ctx );
   1.167 +/*****************************************************************************/
   1.168 +/* Decnet I/O routines.
   1.169 + */
   1.170 +static int get ( io_channel chan, char *buffer, int maxlen, int *length )
   1.171 +{
   1.172 +    int status;
   1.173 +    struct io_status iosb;
   1.174 +    status = SYS$QIOW ( 0, chan, IO$_READVBLK, &iosb, 0, 0,
   1.175 +	buffer, maxlen, 0, 0, 0, 0 );
   1.176 +    if ( (status&1) == 1 ) status = iosb.status;
   1.177 +    if ( (status&1) == 1 ) *length = iosb.count;
   1.178 +    return status;
   1.179 +}
   1.180 +
   1.181 +static int put ( io_channel chan, char *buffer, int length )
   1.182 +{
   1.183 +    int status;
   1.184 +    struct io_status iosb;
   1.185 +    status = SYS$QIOW ( 0, chan, IO$_WRITEVBLK, &iosb, 0, 0,
   1.186 +	buffer, length, 0, 0, 0, 0 );
   1.187 +    if ( (status&1) == 1 ) status = iosb.status;
   1.188 +    return status;
   1.189 +}
   1.190 +/***************************************************************************/
   1.191 +/* Handle operations on the 'G' channel.
   1.192 + */
   1.193 +static int general_request ( io_channel chan, struct rpc_msg *msg, int length )
   1.194 +{
   1.195 +    return 48;
   1.196 +}
   1.197 +/***************************************************************************/
   1.198 +int main ( int argc, char **argv )
   1.199 +{
   1.200 +    int status, length;
   1.201 +    io_channel chan;
   1.202 +    struct rpc_msg msg;
   1.203 +
   1.204 +	char *CApath=NULL,*CAfile=NULL;
   1.205 +	int badop=0;
   1.206 +	int ret=1;
   1.207 +	int client_auth=0;
   1.208 +	int server_auth=0;
   1.209 +	SSL_CTX *s_ctx=NULL;
   1.210 +    /*
   1.211 +     * Confirm logical link with initiating client.
   1.212 +     */
   1.213 +    LIB$INIT_TIMER();
   1.214 +    status = SYS$ASSIGN ( &sysnet, &chan, 0, 0, 0 );
   1.215 +    printf("status of assign to SYS$NET: %d\n", status );
   1.216 +    /*
   1.217 +     * Initialize standard out and error files.
   1.218 +     */
   1.219 +	if (bio_err == NULL)
   1.220 +		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
   1.221 +			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE);
   1.222 +	if (bio_stdout == NULL)
   1.223 +		if ((bio_stdout=BIO_new(BIO_s_file())) != NULL)
   1.224 +			BIO_set_fp(bio_stdout,stdout,BIO_NOCLOSE);
   1.225 +    /*
   1.226 +     * get the preferred cipher list and other initialization
   1.227 +     */
   1.228 +	if (cipher == NULL) cipher=getenv("SSL_CIPHER");
   1.229 +	printf("cipher list: %s\n", cipher ? cipher : "{undefined}" );
   1.230 +
   1.231 +	SSL_load_error_strings();
   1.232 +	OpenSSL_add_all_algorithms();
   1.233 +
   1.234 +/* DRM, this was the original, but there is no such thing as SSLv2()
   1.235 +	s_ctx=SSL_CTX_new(SSLv2());
   1.236 +*/
   1.237 +	s_ctx=SSL_CTX_new(SSLv2_server_method());
   1.238 +
   1.239 +	if (s_ctx == NULL) goto end;
   1.240 +
   1.241 +	SSL_CTX_use_certificate_file(s_ctx,TEST_SERVER_CERT,SSL_FILETYPE_PEM);
   1.242 +	SSL_CTX_use_RSAPrivateKey_file(s_ctx,TEST_SERVER_CERT,SSL_FILETYPE_PEM);
   1.243 +	printf("Loaded server certificate: '%s'\n", TEST_SERVER_CERT );
   1.244 +
   1.245 +    /*
   1.246 +     * Take commands from client until bad status.
   1.247 +     */
   1.248 +    LIB$SHOW_TIMER();
   1.249 +    status = doit ( chan, s_ctx );
   1.250 +    LIB$SHOW_TIMER();
   1.251 +    /*
   1.252 +     * do final cleanup and exit.
   1.253 +     */
   1.254 +end:
   1.255 +	if (s_ctx != NULL) SSL_CTX_free(s_ctx);
   1.256 +    LIB$SHOW_TIMER();
   1.257 +    return 1;
   1.258 +}
   1.259 +
   1.260 +int doit(io_channel chan, SSL_CTX *s_ctx )
   1.261 +{
   1.262 +    int status, length, link_state;
   1.263 +     struct rpc_msg msg;
   1.264 +
   1.265 +	SSL *s_ssl=NULL;
   1.266 +	BIO *c_to_s=NULL;
   1.267 +	BIO *s_to_c=NULL;
   1.268 +	BIO *c_bio=NULL;
   1.269 +	BIO *s_bio=NULL;
   1.270 +	int i;
   1.271 +	int done=0;
   1.272 +
   1.273 +	s_ssl=SSL_new(s_ctx);
   1.274 +	if (s_ssl == NULL) goto err;
   1.275 +
   1.276 +	c_to_s=BIO_new(BIO_s_rtcp());
   1.277 +	s_to_c=BIO_new(BIO_s_rtcp());
   1.278 +	if ((s_to_c == NULL) || (c_to_s == NULL)) goto err;
   1.279 +/* original, DRM 24-SEP-1997
   1.280 +	BIO_set_fd ( c_to_s, "", chan );
   1.281 +	BIO_set_fd ( s_to_c, "", chan );
   1.282 +*/
   1.283 +	BIO_set_fd ( c_to_s, 0, chan );
   1.284 +	BIO_set_fd ( s_to_c, 0, chan );
   1.285 +
   1.286 +	c_bio=BIO_new(BIO_f_ssl());
   1.287 +	s_bio=BIO_new(BIO_f_ssl());
   1.288 +	if ((c_bio == NULL) || (s_bio == NULL)) goto err;
   1.289 +
   1.290 +	SSL_set_accept_state(s_ssl);
   1.291 +	SSL_set_bio(s_ssl,c_to_s,s_to_c);
   1.292 +	BIO_set_ssl(s_bio,s_ssl,BIO_CLOSE);
   1.293 +
   1.294 +	/* We can always do writes */
   1.295 +	printf("Begin doit main loop\n");
   1.296 +	/*
   1.297 +	 * Link states: 0-idle, 1-read pending, 2-write pending, 3-closed.
   1.298 +	 */
   1.299 +	for (link_state = 0; link_state < 3; ) {
   1.300 +	    /*
   1.301 +	     * Wait for remote end to request data action on A channel.
   1.302 +	     */
   1.303 +	    while ( link_state == 0 ) {
   1.304 +		status = get ( chan, (char *) &msg, sizeof(msg), &length );
   1.305 +		if ( (status&1) == 0 ) {
   1.306 +		    printf("Error in main loop get: %d\n", status );
   1.307 +		    link_state = 3;
   1.308 +		    break;
   1.309 +		}
   1.310 +	   	if ( length < RPC_HDR_SIZE ) {
   1.311 +		    printf("Error in main loop get size: %d\n", length );
   1.312 +		    break;
   1.313 +		    link_state = 3;
   1.314 +		}
   1.315 +	   	if ( msg.channel != 'A' ) {
   1.316 +		    printf("Error in main loop, unexpected channel: %c\n", 
   1.317 +			msg.channel );
   1.318 +		    break;
   1.319 +		    link_state = 3;
   1.320 +		}
   1.321 +		if ( msg.function == 'G' ) {
   1.322 +		    link_state = 1;
   1.323 +		} else if ( msg.function == 'P' ) {
   1.324 +		    link_state = 2;	/* write pending */
   1.325 +		} else if ( msg.function == 'X' ) {
   1.326 +		    link_state = 3;
   1.327 +		} else {
   1.328 +		    link_state = 3;
   1.329 +		}
   1.330 +	    }
   1.331 +	    if ( link_state == 1 ) {
   1.332 +		i = BIO_read ( s_bio, msg.data, msg.length );
   1.333 +		if ( i < 0 ) link_state = 3;
   1.334 +		else {
   1.335 +		    msg.channel = 'A';
   1.336 +		    msg.function = 'C';		/* confirm */
   1.337 +		    msg.length = i;
   1.338 +		    status = put ( chan, (char *) &msg, i+RPC_HDR_SIZE );
   1.339 +		    if ( (status&1) == 0 ) break;
   1.340 +		    link_state = 0;
   1.341 +		}
   1.342 +	    } else if ( link_state == 2 ) {
   1.343 +		i = BIO_write ( s_bio, msg.data, msg.length );
   1.344 +		if ( i < 0 ) link_state = 3;
   1.345 +		else {
   1.346 +		    msg.channel = 'A';
   1.347 +		    msg.function = 'C';		/* confirm */
   1.348 +		    msg.length = 0;
   1.349 +		    status = put ( chan, (char *) &msg, RPC_HDR_SIZE );
   1.350 +		    if ( (status&1) == 0 ) break;
   1.351 +		    link_state = 0;
   1.352 +		}
   1.353 +	    }
   1.354 +	}
   1.355 +	fprintf(stdout,"DONE\n");
   1.356 +err:
   1.357 +	/* We have to set the BIO's to NULL otherwise they will be
   1.358 +	 * free()ed twice.  Once when th s_ssl is SSL_free()ed and
   1.359 +	 * again when c_ssl is SSL_free()ed.
   1.360 +	 * This is a hack required because s_ssl and c_ssl are sharing the same
   1.361 +	 * BIO structure and SSL_set_bio() and SSL_free() automatically
   1.362 +	 * BIO_free non NULL entries.
   1.363 +	 * You should not normally do this or be required to do this */
   1.364 +	s_ssl->rbio=NULL;
   1.365 +	s_ssl->wbio=NULL;
   1.366 +
   1.367 +	if (c_to_s != NULL) BIO_free(c_to_s);
   1.368 +	if (s_to_c != NULL) BIO_free(s_to_c);
   1.369 +	if (c_bio != NULL) BIO_free(c_bio);
   1.370 +	if (s_bio != NULL) BIO_free(s_bio);
   1.371 +	return(0);
   1.372 +}