os/ossrv/ssl/libssl/src/bio_ssl.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/bio_ssl.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
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    60
 */
sl@0
    61
 
sl@0
    62
#include <stdio.h>
sl@0
    63
#include <stdlib.h>
sl@0
    64
#include <string.h>
sl@0
    65
#include <errno.h>
sl@0
    66
#include <openssl/crypto.h>
sl@0
    67
#include <openssl/bio.h>
sl@0
    68
#include <openssl/err.h>
sl@0
    69
#include <openssl/ssl.h>
sl@0
    70
sl@0
    71
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    72
#include "libssl_wsd.h"
sl@0
    73
#endif
sl@0
    74
sl@0
    75
static int ssl_write(BIO *h, const char *buf, int num);
sl@0
    76
static int ssl_read(BIO *h, char *buf, int size);
sl@0
    77
static int ssl_puts(BIO *h, const char *str);
sl@0
    78
static long ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2);
sl@0
    79
static int ssl_new(BIO *h);
sl@0
    80
static int ssl_free(BIO *data);
sl@0
    81
static long ssl_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
sl@0
    82
typedef struct bio_ssl_st
sl@0
    83
	{
sl@0
    84
	SSL *ssl; /* The ssl handle :-) */
sl@0
    85
	/* re-negotiate every time the total number of bytes is this size */
sl@0
    86
	int num_renegotiates;
sl@0
    87
	unsigned long renegotiate_count;
sl@0
    88
	unsigned long byte_count;
sl@0
    89
	unsigned long renegotiate_timeout;
sl@0
    90
	unsigned long last_time;
sl@0
    91
	} BIO_SSL;
sl@0
    92
sl@0
    93
#ifdef __cplusplus
sl@0
    94
extern "C"
sl@0
    95
{
sl@0
    96
#endif
sl@0
    97
#ifndef EMULATOR
sl@0
    98
sl@0
    99
	static BIO_METHOD methods_sslp=
sl@0
   100
#else
sl@0
   101
	const BIO_METHOD temp_methods_sslp=
sl@0
   102
sl@0
   103
#endif
sl@0
   104
	{
sl@0
   105
	BIO_TYPE_SSL,"ssl",
sl@0
   106
	ssl_write,
sl@0
   107
	ssl_read,
sl@0
   108
	ssl_puts,
sl@0
   109
	NULL, /* ssl_gets, */
sl@0
   110
	ssl_ctrl,
sl@0
   111
	ssl_new,
sl@0
   112
	ssl_free,
sl@0
   113
	ssl_callback_ctrl,
sl@0
   114
	};
sl@0
   115
#ifdef __cplusplus
sl@0
   116
}
sl@0
   117
#endif
sl@0
   118
sl@0
   119
#ifdef EMULATOR
sl@0
   120
sl@0
   121
GET_STATIC_VAR_FROM_TLS(methods_sslp,bio_ssl,BIO_METHOD)
sl@0
   122
sl@0
   123
#define methods_sslp (*GET_WSD_VAR_NAME(methods_sslp,bio_ssl,s)())
sl@0
   124
	
sl@0
   125
#endif //EMULATOR
sl@0
   126
sl@0
   127
sl@0
   128
sl@0
   129
EXPORT_C BIO_METHOD *BIO_f_ssl(void)
sl@0
   130
	{
sl@0
   131
	return(&methods_sslp);
sl@0
   132
	}
sl@0
   133
sl@0
   134
static int ssl_new(BIO *bi)
sl@0
   135
	{
sl@0
   136
	BIO_SSL *bs;
sl@0
   137
sl@0
   138
	bs=(BIO_SSL *)OPENSSL_malloc(sizeof(BIO_SSL));
sl@0
   139
	if (bs == NULL)
sl@0
   140
		{
sl@0
   141
		BIOerr(BIO_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
sl@0
   142
		return(0);
sl@0
   143
		}
sl@0
   144
	memset(bs,0,sizeof(BIO_SSL));
sl@0
   145
	bi->init=0;
sl@0
   146
	bi->ptr=(char *)bs;
sl@0
   147
	bi->flags=0;
sl@0
   148
	return(1);
sl@0
   149
	}
sl@0
   150
sl@0
   151
static int ssl_free(BIO *a)
sl@0
   152
	{
sl@0
   153
	BIO_SSL *bs;
sl@0
   154
sl@0
   155
	if (a == NULL) return(0);
sl@0
   156
	bs=(BIO_SSL *)a->ptr;
sl@0
   157
	if (bs->ssl != NULL) SSL_shutdown(bs->ssl);
sl@0
   158
	if (a->shutdown)
sl@0
   159
		{
sl@0
   160
		if (a->init && (bs->ssl != NULL))
sl@0
   161
			SSL_free(bs->ssl);
sl@0
   162
		a->init=0;
sl@0
   163
		a->flags=0;
sl@0
   164
		}
sl@0
   165
	if (a->ptr != NULL)
sl@0
   166
		OPENSSL_free(a->ptr);
sl@0
   167
	return(1);
sl@0
   168
	}
sl@0
   169
	
sl@0
   170
static int ssl_read(BIO *b, char *out, int outl)
sl@0
   171
	{
sl@0
   172
	int ret=1;
sl@0
   173
	BIO_SSL *sb;
sl@0
   174
	SSL *ssl;
sl@0
   175
	int retry_reason=0;
sl@0
   176
	int r=0;
sl@0
   177
sl@0
   178
	if (out == NULL) return(0);
sl@0
   179
	sb=(BIO_SSL *)b->ptr;
sl@0
   180
	ssl=sb->ssl;
sl@0
   181
sl@0
   182
	BIO_clear_retry_flags(b);
sl@0
   183
sl@0
   184
#if 0
sl@0
   185
	if (!SSL_is_init_finished(ssl))
sl@0
   186
		{
sl@0
   187
/*		ret=SSL_do_handshake(ssl); */
sl@0
   188
		if (ret > 0)
sl@0
   189
			{
sl@0
   190
sl@0
   191
			outflags=(BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
sl@0
   192
			ret= -1;
sl@0
   193
			goto end;
sl@0
   194
			}
sl@0
   195
		}
sl@0
   196
#endif
sl@0
   197
/*	if (ret > 0) */
sl@0
   198
	ret=SSL_read(ssl,out,outl);
sl@0
   199
sl@0
   200
	switch (SSL_get_error(ssl,ret))
sl@0
   201
		{
sl@0
   202
	case SSL_ERROR_NONE:
sl@0
   203
		if (ret <= 0) break;
sl@0
   204
		if (sb->renegotiate_count > 0)
sl@0
   205
			{
sl@0
   206
			sb->byte_count+=ret;
sl@0
   207
			if (sb->byte_count > sb->renegotiate_count)
sl@0
   208
				{
sl@0
   209
				sb->byte_count=0;
sl@0
   210
				sb->num_renegotiates++;
sl@0
   211
				SSL_renegotiate(ssl);
sl@0
   212
				r=1;
sl@0
   213
				}
sl@0
   214
			}
sl@0
   215
		if ((sb->renegotiate_timeout > 0) && (!r))
sl@0
   216
			{
sl@0
   217
			unsigned long tm;
sl@0
   218
sl@0
   219
			tm=(unsigned long)time(NULL);
sl@0
   220
			if (tm > sb->last_time+sb->renegotiate_timeout)
sl@0
   221
				{
sl@0
   222
				sb->last_time=tm;
sl@0
   223
				sb->num_renegotiates++;
sl@0
   224
				SSL_renegotiate(ssl);
sl@0
   225
				}
sl@0
   226
			}
sl@0
   227
sl@0
   228
		break;
sl@0
   229
	case SSL_ERROR_WANT_READ:
sl@0
   230
		BIO_set_retry_read(b);
sl@0
   231
		break;
sl@0
   232
	case SSL_ERROR_WANT_WRITE:
sl@0
   233
		BIO_set_retry_write(b);
sl@0
   234
		break;
sl@0
   235
	case SSL_ERROR_WANT_X509_LOOKUP:
sl@0
   236
		BIO_set_retry_special(b);
sl@0
   237
		retry_reason=BIO_RR_SSL_X509_LOOKUP;
sl@0
   238
		break;
sl@0
   239
	case SSL_ERROR_WANT_ACCEPT:
sl@0
   240
		BIO_set_retry_special(b);
sl@0
   241
		retry_reason=BIO_RR_ACCEPT;
sl@0
   242
		break;
sl@0
   243
	case SSL_ERROR_WANT_CONNECT:
sl@0
   244
		BIO_set_retry_special(b);
sl@0
   245
		retry_reason=BIO_RR_CONNECT;
sl@0
   246
		break;
sl@0
   247
	case SSL_ERROR_SYSCALL:
sl@0
   248
	case SSL_ERROR_SSL:
sl@0
   249
	case SSL_ERROR_ZERO_RETURN:
sl@0
   250
	default:
sl@0
   251
		break;
sl@0
   252
		}
sl@0
   253
sl@0
   254
	b->retry_reason=retry_reason;
sl@0
   255
	return(ret);
sl@0
   256
	}
sl@0
   257
sl@0
   258
static int ssl_write(BIO *b, const char *out, int outl)
sl@0
   259
	{
sl@0
   260
	int ret,r=0;
sl@0
   261
	int retry_reason=0;
sl@0
   262
	SSL *ssl;
sl@0
   263
	BIO_SSL *bs;
sl@0
   264
sl@0
   265
	if (out == NULL) return(0);
sl@0
   266
	bs=(BIO_SSL *)b->ptr;
sl@0
   267
	ssl=bs->ssl;
sl@0
   268
sl@0
   269
	BIO_clear_retry_flags(b);
sl@0
   270
sl@0
   271
/*	ret=SSL_do_handshake(ssl);
sl@0
   272
	if (ret > 0) */
sl@0
   273
	ret=SSL_write(ssl,out,outl);
sl@0
   274
sl@0
   275
	switch (SSL_get_error(ssl,ret))
sl@0
   276
		{
sl@0
   277
	case SSL_ERROR_NONE:
sl@0
   278
		if (ret <= 0) break;
sl@0
   279
		if (bs->renegotiate_count > 0)
sl@0
   280
			{
sl@0
   281
			bs->byte_count+=ret;
sl@0
   282
			if (bs->byte_count > bs->renegotiate_count)
sl@0
   283
				{
sl@0
   284
				bs->byte_count=0;
sl@0
   285
				bs->num_renegotiates++;
sl@0
   286
				SSL_renegotiate(ssl);
sl@0
   287
				r=1;
sl@0
   288
				}
sl@0
   289
			}
sl@0
   290
		if ((bs->renegotiate_timeout > 0) && (!r))
sl@0
   291
			{
sl@0
   292
			unsigned long tm;
sl@0
   293
sl@0
   294
			tm=(unsigned long)time(NULL);
sl@0
   295
			if (tm > bs->last_time+bs->renegotiate_timeout)
sl@0
   296
				{
sl@0
   297
				bs->last_time=tm;
sl@0
   298
				bs->num_renegotiates++;
sl@0
   299
				SSL_renegotiate(ssl);
sl@0
   300
				}
sl@0
   301
			}
sl@0
   302
		break;
sl@0
   303
	case SSL_ERROR_WANT_WRITE:
sl@0
   304
		BIO_set_retry_write(b);
sl@0
   305
		break;
sl@0
   306
	case SSL_ERROR_WANT_READ:
sl@0
   307
		BIO_set_retry_read(b);
sl@0
   308
		break;
sl@0
   309
	case SSL_ERROR_WANT_X509_LOOKUP:
sl@0
   310
		BIO_set_retry_special(b);
sl@0
   311
		retry_reason=BIO_RR_SSL_X509_LOOKUP;
sl@0
   312
		break;
sl@0
   313
	case SSL_ERROR_WANT_CONNECT:
sl@0
   314
		BIO_set_retry_special(b);
sl@0
   315
		retry_reason=BIO_RR_CONNECT;
sl@0
   316
	case SSL_ERROR_SYSCALL:
sl@0
   317
	case SSL_ERROR_SSL:
sl@0
   318
	default:
sl@0
   319
		break;
sl@0
   320
		}
sl@0
   321
sl@0
   322
	b->retry_reason=retry_reason;
sl@0
   323
	return(ret);
sl@0
   324
	}
sl@0
   325
sl@0
   326
static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
sl@0
   327
	{
sl@0
   328
	SSL **sslp,*ssl;
sl@0
   329
	BIO_SSL *bs;
sl@0
   330
	BIO *dbio,*bio;
sl@0
   331
	long ret=1;
sl@0
   332
sl@0
   333
	bs=(BIO_SSL *)b->ptr;
sl@0
   334
	ssl=bs->ssl;
sl@0
   335
	if ((ssl == NULL)  && (cmd != BIO_C_SET_SSL))
sl@0
   336
		return(0);
sl@0
   337
	switch (cmd)
sl@0
   338
		{
sl@0
   339
	case BIO_CTRL_RESET:
sl@0
   340
		SSL_shutdown(ssl);
sl@0
   341
sl@0
   342
		if (ssl->handshake_func == ssl->method->ssl_connect)
sl@0
   343
			SSL_set_connect_state(ssl);
sl@0
   344
		else if (ssl->handshake_func == ssl->method->ssl_accept)
sl@0
   345
			SSL_set_accept_state(ssl);
sl@0
   346
sl@0
   347
		SSL_clear(ssl);
sl@0
   348
sl@0
   349
		if (b->next_bio != NULL)
sl@0
   350
			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   351
		else if (ssl->rbio != NULL)
sl@0
   352
			ret=BIO_ctrl(ssl->rbio,cmd,num,ptr);
sl@0
   353
		else
sl@0
   354
			ret=1;
sl@0
   355
		break;
sl@0
   356
	case BIO_CTRL_INFO:
sl@0
   357
		ret=0;
sl@0
   358
		break;
sl@0
   359
	case BIO_C_SSL_MODE:
sl@0
   360
		if (num) /* client mode */
sl@0
   361
			SSL_set_connect_state(ssl);
sl@0
   362
		else
sl@0
   363
			SSL_set_accept_state(ssl);
sl@0
   364
		break;
sl@0
   365
	case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
sl@0
   366
		ret=bs->renegotiate_timeout;
sl@0
   367
		if (num < 60) num=5;
sl@0
   368
		bs->renegotiate_timeout=(unsigned long)num;
sl@0
   369
		bs->last_time=(unsigned long)time(NULL);
sl@0
   370
		break;
sl@0
   371
	case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
sl@0
   372
		ret=bs->renegotiate_count;
sl@0
   373
		if ((long)num >=512)
sl@0
   374
			bs->renegotiate_count=(unsigned long)num;
sl@0
   375
		break;
sl@0
   376
	case BIO_C_GET_SSL_NUM_RENEGOTIATES:
sl@0
   377
		ret=bs->num_renegotiates;
sl@0
   378
		break;
sl@0
   379
	case BIO_C_SET_SSL:
sl@0
   380
		if (ssl != NULL)
sl@0
   381
			ssl_free(b);
sl@0
   382
		b->shutdown=(int)num;
sl@0
   383
		ssl=(SSL *)ptr;
sl@0
   384
		((BIO_SSL *)b->ptr)->ssl=ssl;
sl@0
   385
		bio=SSL_get_rbio(ssl);
sl@0
   386
		if (bio != NULL)
sl@0
   387
			{
sl@0
   388
			if (b->next_bio != NULL)
sl@0
   389
				BIO_push(bio,b->next_bio);
sl@0
   390
			b->next_bio=bio;
sl@0
   391
			CRYPTO_add(&bio->references,1,CRYPTO_LOCK_BIO);
sl@0
   392
			}
sl@0
   393
		b->init=1;
sl@0
   394
		break;
sl@0
   395
	case BIO_C_GET_SSL:
sl@0
   396
		if (ptr != NULL)
sl@0
   397
			{
sl@0
   398
			sslp=(SSL **)ptr;
sl@0
   399
			*sslp=ssl;
sl@0
   400
			}
sl@0
   401
		else
sl@0
   402
			ret=0;
sl@0
   403
		break;
sl@0
   404
	case BIO_CTRL_GET_CLOSE:
sl@0
   405
		ret=b->shutdown;
sl@0
   406
		break;
sl@0
   407
	case BIO_CTRL_SET_CLOSE:
sl@0
   408
		b->shutdown=(int)num;
sl@0
   409
		break;
sl@0
   410
	case BIO_CTRL_WPENDING:
sl@0
   411
		ret=BIO_ctrl(ssl->wbio,cmd,num,ptr);
sl@0
   412
		break;
sl@0
   413
	case BIO_CTRL_PENDING:
sl@0
   414
		ret=SSL_pending(ssl);
sl@0
   415
		if (ret == 0)
sl@0
   416
			ret=BIO_pending(ssl->rbio);
sl@0
   417
		break;
sl@0
   418
	case BIO_CTRL_FLUSH:
sl@0
   419
		BIO_clear_retry_flags(b);
sl@0
   420
		ret=BIO_ctrl(ssl->wbio,cmd,num,ptr);
sl@0
   421
		BIO_copy_next_retry(b);
sl@0
   422
		break;
sl@0
   423
	case BIO_CTRL_PUSH:
sl@0
   424
		if ((b->next_bio != NULL) && (b->next_bio != ssl->rbio))
sl@0
   425
			{
sl@0
   426
			SSL_set_bio(ssl,b->next_bio,b->next_bio);
sl@0
   427
			CRYPTO_add(&b->next_bio->references,1,CRYPTO_LOCK_BIO);
sl@0
   428
			}
sl@0
   429
		break;
sl@0
   430
	case BIO_CTRL_POP:
sl@0
   431
		/* ugly bit of a hack */
sl@0
   432
		if (ssl->rbio != ssl->wbio) /* we are in trouble :-( */
sl@0
   433
			{
sl@0
   434
			BIO_free_all(ssl->wbio);
sl@0
   435
			}
sl@0
   436
		if (b->next_bio != NULL)
sl@0
   437
			{
sl@0
   438
			CRYPTO_add(&b->next_bio->references,1,CRYPTO_LOCK_BIO);
sl@0
   439
			}
sl@0
   440
		ssl->wbio=NULL;
sl@0
   441
		ssl->rbio=NULL;
sl@0
   442
		break;
sl@0
   443
	case BIO_C_DO_STATE_MACHINE:
sl@0
   444
		BIO_clear_retry_flags(b);
sl@0
   445
sl@0
   446
		b->retry_reason=0;
sl@0
   447
		ret=(int)SSL_do_handshake(ssl);
sl@0
   448
sl@0
   449
		switch (SSL_get_error(ssl,(int)ret))
sl@0
   450
			{
sl@0
   451
		case SSL_ERROR_WANT_READ:
sl@0
   452
			BIO_set_flags(b,
sl@0
   453
				BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
sl@0
   454
			break;
sl@0
   455
		case SSL_ERROR_WANT_WRITE:
sl@0
   456
			BIO_set_flags(b,
sl@0
   457
				BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY);
sl@0
   458
			break;
sl@0
   459
		case SSL_ERROR_WANT_CONNECT:
sl@0
   460
			BIO_set_flags(b,
sl@0
   461
				BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY);
sl@0
   462
			b->retry_reason=b->next_bio->retry_reason;
sl@0
   463
			break;
sl@0
   464
		default:
sl@0
   465
			break;
sl@0
   466
			}
sl@0
   467
		break;
sl@0
   468
	case BIO_CTRL_DUP:
sl@0
   469
		dbio=(BIO *)ptr;
sl@0
   470
		if (((BIO_SSL *)dbio->ptr)->ssl != NULL)
sl@0
   471
			SSL_free(((BIO_SSL *)dbio->ptr)->ssl);
sl@0
   472
		((BIO_SSL *)dbio->ptr)->ssl=SSL_dup(ssl);
sl@0
   473
		((BIO_SSL *)dbio->ptr)->renegotiate_count=
sl@0
   474
			((BIO_SSL *)b->ptr)->renegotiate_count;
sl@0
   475
		((BIO_SSL *)dbio->ptr)->byte_count=
sl@0
   476
			((BIO_SSL *)b->ptr)->byte_count;
sl@0
   477
		((BIO_SSL *)dbio->ptr)->renegotiate_timeout=
sl@0
   478
			((BIO_SSL *)b->ptr)->renegotiate_timeout;
sl@0
   479
		((BIO_SSL *)dbio->ptr)->last_time=
sl@0
   480
			((BIO_SSL *)b->ptr)->last_time;
sl@0
   481
		ret=(((BIO_SSL *)dbio->ptr)->ssl != NULL);
sl@0
   482
		break;
sl@0
   483
	case BIO_C_GET_FD:
sl@0
   484
		ret=BIO_ctrl(ssl->rbio,cmd,num,ptr);
sl@0
   485
		break;
sl@0
   486
	case BIO_CTRL_SET_CALLBACK:
sl@0
   487
		{
sl@0
   488
#if 0 /* FIXME: Should this be used?  -- Richard Levitte */
sl@0
   489
		SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
sl@0
   490
		ret = -1;
sl@0
   491
#else
sl@0
   492
		ret=0;
sl@0
   493
#endif
sl@0
   494
		}
sl@0
   495
		break;
sl@0
   496
	case BIO_CTRL_GET_CALLBACK:
sl@0
   497
		{
sl@0
   498
		void (**fptr)(const SSL *xssl,int type,int val);
sl@0
   499
sl@0
   500
		fptr=(void (**)(const SSL *xssl,int type,int val))ptr;
sl@0
   501
		*fptr=SSL_get_info_callback(ssl);
sl@0
   502
		}
sl@0
   503
		break;
sl@0
   504
	default:
sl@0
   505
		ret=BIO_ctrl(ssl->rbio,cmd,num,ptr);
sl@0
   506
		break;
sl@0
   507
		}
sl@0
   508
	return(ret);
sl@0
   509
	}
sl@0
   510
sl@0
   511
static long ssl_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
sl@0
   512
	{
sl@0
   513
	SSL *ssl;
sl@0
   514
	BIO_SSL *bs;
sl@0
   515
	long ret=1;
sl@0
   516
sl@0
   517
	bs=(BIO_SSL *)b->ptr;
sl@0
   518
	ssl=bs->ssl;
sl@0
   519
	switch (cmd)
sl@0
   520
		{
sl@0
   521
	case BIO_CTRL_SET_CALLBACK:
sl@0
   522
		{
sl@0
   523
		/* FIXME: setting this via a completely different prototype
sl@0
   524
		   seems like a crap idea */
sl@0
   525
		SSL_set_info_callback(ssl,(void (*)(const SSL *,int,int))fp);
sl@0
   526
		}
sl@0
   527
		break;
sl@0
   528
	default:
sl@0
   529
		ret=BIO_callback_ctrl(ssl->rbio,cmd,fp);
sl@0
   530
		break;
sl@0
   531
		}
sl@0
   532
	return(ret);
sl@0
   533
	}
sl@0
   534
sl@0
   535
static int ssl_puts(BIO *bp, const char *str)
sl@0
   536
	{
sl@0
   537
	int n,ret;
sl@0
   538
sl@0
   539
	n=strlen(str);
sl@0
   540
	ret=BIO_write(bp,str,n);
sl@0
   541
	return(ret);
sl@0
   542
	}
sl@0
   543
sl@0
   544
EXPORT_C BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
sl@0
   545
	{
sl@0
   546
#ifndef OPENSSL_NO_SOCK
sl@0
   547
	BIO *ret=NULL,*buf=NULL,*ssl=NULL;
sl@0
   548
sl@0
   549
	if ((buf=BIO_new(BIO_f_buffer())) == NULL)
sl@0
   550
		return(NULL);
sl@0
   551
	if ((ssl=BIO_new_ssl_connect(ctx)) == NULL)
sl@0
   552
		goto err;
sl@0
   553
	if ((ret=BIO_push(buf,ssl)) == NULL)
sl@0
   554
		goto err;
sl@0
   555
	return(ret);
sl@0
   556
err:
sl@0
   557
	if (buf != NULL) BIO_free(buf);
sl@0
   558
	if (ssl != NULL) BIO_free(ssl);
sl@0
   559
#endif
sl@0
   560
	return(NULL);
sl@0
   561
	}
sl@0
   562
sl@0
   563
EXPORT_C BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
sl@0
   564
	{
sl@0
   565
	BIO *ret=NULL,*con=NULL,*ssl=NULL;
sl@0
   566
sl@0
   567
	if ((con=BIO_new(BIO_s_connect())) == NULL)
sl@0
   568
		return(NULL);
sl@0
   569
	if ((ssl=BIO_new_ssl(ctx,1)) == NULL)
sl@0
   570
		goto err;
sl@0
   571
	if ((ret=BIO_push(ssl,con)) == NULL)
sl@0
   572
		goto err;
sl@0
   573
	return(ret);
sl@0
   574
err:
sl@0
   575
	if (con != NULL) BIO_free(con);
sl@0
   576
	if (ret != NULL) BIO_free(ret);
sl@0
   577
	return(NULL);
sl@0
   578
	}
sl@0
   579
sl@0
   580
EXPORT_C BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
sl@0
   581
	{
sl@0
   582
	BIO *ret;
sl@0
   583
	SSL *ssl;
sl@0
   584
sl@0
   585
	if ((ret=BIO_new(BIO_f_ssl())) == NULL)
sl@0
   586
		return(NULL);
sl@0
   587
	if ((ssl=SSL_new(ctx)) == NULL)
sl@0
   588
		{
sl@0
   589
		BIO_free(ret);
sl@0
   590
		return(NULL);
sl@0
   591
		}
sl@0
   592
	if (client)
sl@0
   593
		SSL_set_connect_state(ssl);
sl@0
   594
	else
sl@0
   595
		SSL_set_accept_state(ssl);
sl@0
   596
		
sl@0
   597
	BIO_set_ssl(ret,ssl,BIO_CLOSE);
sl@0
   598
	return(ret);
sl@0
   599
	}
sl@0
   600
sl@0
   601
EXPORT_C int BIO_ssl_copy_session_id(BIO *t, BIO *f)
sl@0
   602
	{
sl@0
   603
	t=BIO_find_type(t,BIO_TYPE_SSL);
sl@0
   604
	f=BIO_find_type(f,BIO_TYPE_SSL);
sl@0
   605
	if ((t == NULL) || (f == NULL))
sl@0
   606
		return(0);
sl@0
   607
	if (	(((BIO_SSL *)t->ptr)->ssl == NULL) || 
sl@0
   608
		(((BIO_SSL *)f->ptr)->ssl == NULL))
sl@0
   609
		return(0);
sl@0
   610
	SSL_copy_session_id(((BIO_SSL *)t->ptr)->ssl,((BIO_SSL *)f->ptr)->ssl);
sl@0
   611
	return(1);
sl@0
   612
	}
sl@0
   613
sl@0
   614
EXPORT_C void BIO_ssl_shutdown(BIO *b)
sl@0
   615
	{
sl@0
   616
	SSL *s;
sl@0
   617
sl@0
   618
	while (b != NULL)
sl@0
   619
		{
sl@0
   620
		if (b->method->type == BIO_TYPE_SSL)
sl@0
   621
			{
sl@0
   622
			s=((BIO_SSL *)b->ptr)->ssl;
sl@0
   623
			SSL_shutdown(s);
sl@0
   624
			break;
sl@0
   625
			}
sl@0
   626
		b=b->next_bio;
sl@0
   627
		}
sl@0
   628
	}