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