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