2 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
67 static int ssl_write(BIO *h,char *buf,int num);
68 static int ssl_read(BIO *h,char *buf,int size);
69 static int ssl_puts(BIO *h,char *str);
70 static long ssl_ctrl(BIO *h,int cmd,long arg1,char *arg2);
71 static int ssl_new(BIO *h);
72 static int ssl_free(BIO *data);
74 static int ssl_write();
75 static int ssl_read();
76 static int ssl_puts();
77 static long ssl_ctrl();
79 static int ssl_free();
82 static BIO_METHOD methods_sslp=
94 BIO_METHOD *BIO_f_ssl()
96 return(&methods_sslp);
99 static int ssl_new(bi)
103 bi->ptr=NULL; /* The SSL structure */
108 static int ssl_free(a)
111 if (a == NULL) return(0);
112 if (a->ptr != NULL) SSL_shutdown((SSL *)a->ptr);
115 if (a->init) SSL_free((SSL *)a->ptr);
123 static int ssl_read(b,out,outl)
129 int inflags,outflags;
133 if (out == NULL) return(0);
136 inflags=outflags=b->flags;
138 outflags&= ~(BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY);
140 if (!SSL_is_init_finished(ssl))
142 ret=SSL_do_handshake(ssl);
146 outflags=(BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
153 ret=SSL_read(ssl,out,outl);
155 switch (SSL_get_error(ssl,ret))
160 case SSL_ERROR_WANT_READ:
161 outflags=(BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
163 case SSL_ERROR_WANT_WRITE:
164 outflags=(BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY);
166 case SSL_ERROR_WANT_X509_LOOKUP:
167 outflags=(BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY);
168 retry_reason=BIO_RR_SSL_X509_LOOKUP;
170 case SSL_ERROR_WANT_CONNECT:
171 outflags=(BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY);
172 retry_reason=BIO_RR_CONNECT;
174 case SSL_ERROR_SYSCALL:
176 case SSL_ERROR_ZERO_RETURN:
181 b->retry_reason=retry_reason;
186 static int ssl_write(b,out,outl)
192 int inflags,outflags,retry_reason=0;
195 if (out == NULL) return(0);
198 inflags=outflags=b->flags;
200 outflags&= ~(BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY);
202 ret=SSL_do_handshake(ssl);
204 ret=SSL_write(ssl,out,outl);
206 switch (SSL_get_error(ssl,ret))
211 case SSL_ERROR_WANT_WRITE:
212 outflags=(BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY);
214 case SSL_ERROR_WANT_READ:
215 outflags=(BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
217 case SSL_ERROR_WANT_X509_LOOKUP:
218 outflags=(BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY);
219 retry_reason=BIO_RR_SSL_X509_LOOKUP;
221 case SSL_ERROR_WANT_CONNECT:
222 outflags=(BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY);
223 retry_reason=BIO_RR_CONNECT;
224 case SSL_ERROR_SYSCALL:
230 b->retry_reason=retry_reason;
235 static long ssl_ctrl(b,cmd,num,ptr)
251 if (ssl->handshake_func == ssl->method->ssl_connect)
252 SSL_set_connect_state(ssl);
253 else if (ssl->handshake_func == ssl->method->ssl_accept)
254 SSL_set_accept_state(ssl);
258 if (b->next_bio != NULL)
259 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
260 else if (ssl->rbio != NULL)
261 ret=BIO_ctrl(ssl->rbio,cmd,num,ptr);
270 if (num) /* client mode */
271 SSL_set_connect_state(ssl);
273 SSL_set_accept_state(ssl);
277 b->shutdown=(int)num;
280 bio=SSL_get_rbio(ssl);
283 if (b->next_bio != NULL)
284 BIO_push(bio,b->next_bio);
296 case BIO_CTRL_GET_CLOSE:
299 case BIO_CTRL_SET_CLOSE:
300 b->shutdown=(int)num;
302 case BIO_CTRL_WPENDING:
303 ret=BIO_ctrl(ssl->wbio,cmd,num,ptr);
305 case BIO_CTRL_PENDING:
306 ret=SSL_pending(ssl);
308 ret=BIO_pending(ssl->rbio);
311 BIO_clear_retry_flags(b);
312 ret=BIO_ctrl(ssl->wbio,cmd,num,ptr);
313 BIO_copy_next_retry(b);
316 if (b->next_bio != NULL)
318 SSL_set_bio(ssl,b->next_bio,b->next_bio);
319 b->next_bio->references++;
323 /* ugly bit of a hack */
324 if (ssl->rbio != ssl->wbio) /* we are in trouble :-( */
326 BIO_free_all(ssl->wbio);
331 case BIO_C_DO_STATE_MACHINE:
332 BIO_clear_retry_flags(b);
335 ret=(int)SSL_do_handshake(ssl);
337 switch (SSL_get_error(ssl,(int)ret))
339 case SSL_ERROR_WANT_READ:
341 BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
343 case SSL_ERROR_WANT_WRITE:
345 BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY);
347 case SSL_ERROR_WANT_CONNECT:
349 BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY);
350 b->retry_reason=b->next_bio->retry_reason;
358 if (dbio->ptr != NULL)
359 SSL_free((SSL *)dbio->ptr);
360 dbio->ptr=(char *)SSL_dup(ssl);
361 ret=(dbio->ptr != NULL);
370 static int ssl_puts(bp,str)
377 ret=BIO_write(bp,str,n);
381 BIO *BIO_new_ssl(ctx,client)
388 if ((ret=BIO_new(BIO_f_ssl())) == NULL)
390 if ((ssl=SSL_new(ctx)) == NULL)
396 SSL_set_connect_state(ssl);
398 SSL_set_accept_state(ssl);
400 BIO_set_ssl(ret,ssl,BIO_CLOSE);
404 int BIO_ssl_copy_session_id(t,f)
407 t=BIO_find_type(t,BIO_TYPE_SSL);
408 f=BIO_find_type(f,BIO_TYPE_SSL);
409 if ((t == NULL) || (f == NULL))
411 if ((t->ptr == NULL) || (f->ptr == NULL))
413 SSL_copy_session_id((SSL *)t->ptr,(SSL *)f->ptr);
417 void BIO_ssl_shutdown(b)
424 if (b->method->type == BIO_TYPE_SSL)