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 dr
=inflags
&BIO_FLAGS_PROTOCOL_DELAYED_READ
;
139 dw
=inflags
&BIO_FLAGS_PROTOCOL_DELAYED_WRITE
;
141 outflags
&= ~(BIO_FLAGS_RWS
|BIO_FLAGS_SHOULD_RETRY
|
142 BIO_FLAGS_PROTOCOL_DELAYED_WRITE
|
143 BIO_FLAGS_PROTOCOL_DELAYED_READ
);
145 if (!SSL_is_init_finished(ssl
))
147 ret
=SSL_do_handshake(ssl
);
151 outflags
=(BIO_FLAGS_READ
|BIO_FLAGS_SHOULD_RETRY
|
152 BIO_FLAGS_PROTOCOL_DELAYED_READ
|dw
);
159 ret
=SSL_read(ssl
,out
,outl
);
161 switch (SSL_get_error(ssl
,ret
))
166 outflags
|=(BIO_FLAGS_WRITE
|BIO_FLAGS_SHOULD_RETRY
);
168 case SSL_ERROR_WANT_READ
:
169 outflags
=(BIO_FLAGS_READ
|BIO_FLAGS_SHOULD_RETRY
|
170 BIO_FLAGS_PROTOCOL_DELAYED_READ
|dw
);
172 case SSL_ERROR_WANT_WRITE
:
173 outflags
=(BIO_FLAGS_WRITE
|BIO_FLAGS_SHOULD_RETRY
|
174 BIO_FLAGS_PROTOCOL_DELAYED_READ
|dw
);
176 case SSL_ERROR_WANT_X509_LOOKUP
:
177 outflags
=(BIO_FLAGS_IO_SPECIAL
|BIO_FLAGS_SHOULD_RETRY
|
178 BIO_FLAGS_PROTOCOL_DELAYED_READ
|dw
);
179 retry_reason
=BIO_RR_SSL_X509_LOOKUP
;
181 case SSL_ERROR_WANT_CONNECT
:
182 outflags
=(BIO_FLAGS_IO_SPECIAL
|BIO_FLAGS_SHOULD_RETRY
|
183 BIO_FLAGS_PROTOCOL_DELAYED_READ
|dw
);
184 retry_reason
=BIO_RR_CONNECT
;
186 case SSL_ERROR_SYSCALL
:
188 case SSL_ERROR_ZERO_RETURN
:
193 b
->retry_reason
=retry_reason
;
198 static int ssl_write(b
,out
,outl
)
204 int inflags
,outflags
,retry_reason
=0;
207 if (out
== NULL
) return(0);
210 inflags
=outflags
=b
->flags
;
212 dr
=inflags
&BIO_FLAGS_PROTOCOL_DELAYED_READ
;
213 dw
=inflags
&BIO_FLAGS_PROTOCOL_DELAYED_WRITE
;
215 outflags
&= ~(BIO_FLAGS_RWS
|BIO_FLAGS_SHOULD_RETRY
|
216 BIO_FLAGS_PROTOCOL_DELAYED_WRITE
|
217 BIO_FLAGS_PROTOCOL_DELAYED_READ
);
219 ret
=SSL_do_handshake(ssl
);
221 ret
=SSL_write(ssl
,out
,outl
);
223 switch (SSL_get_error(ssl
,ret
))
228 outflags
|=(BIO_FLAGS_READ
|BIO_FLAGS_SHOULD_RETRY
);
230 case SSL_ERROR_WANT_WRITE
:
231 outflags
=(BIO_FLAGS_WRITE
|BIO_FLAGS_SHOULD_RETRY
|
232 BIO_FLAGS_PROTOCOL_DELAYED_WRITE
|dr
);
234 case SSL_ERROR_WANT_READ
:
235 outflags
=(BIO_FLAGS_READ
|BIO_FLAGS_SHOULD_RETRY
|
236 BIO_FLAGS_PROTOCOL_DELAYED_WRITE
|dr
);
238 case SSL_ERROR_WANT_X509_LOOKUP
:
239 outflags
=(BIO_FLAGS_IO_SPECIAL
|BIO_FLAGS_SHOULD_RETRY
|
240 BIO_FLAGS_PROTOCOL_DELAYED_WRITE
|dr
);
241 retry_reason
=BIO_RR_SSL_X509_LOOKUP
;
243 case SSL_ERROR_WANT_CONNECT
:
244 outflags
=(BIO_FLAGS_IO_SPECIAL
|BIO_FLAGS_SHOULD_RETRY
|
245 BIO_FLAGS_PROTOCOL_DELAYED_READ
|dw
);
246 retry_reason
=BIO_RR_CONNECT
;
247 case SSL_ERROR_SYSCALL
:
253 b
->retry_reason
=retry_reason
;
258 static long ssl_ctrl(b
,cmd
,num
,ptr
)
274 if (ssl
->handshake_func
== ssl
->method
->ssl_connect
)
275 SSL_set_connect_state(ssl
);
276 else if (ssl
->handshake_func
== ssl
->method
->ssl_accept
)
277 SSL_set_accept_state(ssl
);
281 if (b
->next_bio
!= NULL
)
282 ret
=BIO_ctrl(b
->next_bio
,cmd
,num
,ptr
);
283 else if (ssl
->rbio
!= NULL
)
284 ret
=BIO_ctrl(ssl
->rbio
,cmd
,num
,ptr
);
293 if (num
) /* client mode */
294 SSL_set_connect_state(ssl
);
296 SSL_set_accept_state(ssl
);
300 b
->shutdown
=(int)num
;
303 bio
=SSL_get_rbio(ssl
);
306 if (b
->next_bio
!= NULL
)
307 BIO_push(bio
,b
->next_bio
);
319 case BIO_CTRL_GET_CLOSE
:
322 case BIO_CTRL_SET_CLOSE
:
323 b
->shutdown
=(int)num
;
325 case BIO_CTRL_WPENDING
:
326 ret
=BIO_ctrl(ssl
->wbio
,cmd
,num
,ptr
);
328 case BIO_CTRL_PENDING
:
329 ret
=SSL_pending(ssl
);
331 ret
=BIO_pending(ssl
->rbio
);
334 BIO_clear_retry_flags(b
);
335 ret
=BIO_ctrl(ssl
->wbio
,cmd
,num
,ptr
);
336 BIO_copy_next_retry(b
);
339 if (b
->next_bio
!= NULL
)
341 SSL_set_bio(ssl
,b
->next_bio
,b
->next_bio
);
342 b
->next_bio
->references
++;
346 /* ugly bit of a hack */
347 if (ssl
->rbio
!= ssl
->wbio
) /* we are in trouble :-( */
349 BIO_free_all(ssl
->wbio
);
354 case BIO_C_DO_STATE_MACHINE
:
355 BIO_clear_retry_flags(b
);
358 ret
=(int)SSL_do_handshake(ssl
);
360 switch (SSL_get_error(ssl
,ret
))
362 case SSL_ERROR_WANT_READ
:
364 BIO_FLAGS_READ
|BIO_FLAGS_SHOULD_RETRY
);
366 case SSL_ERROR_WANT_WRITE
:
368 BIO_FLAGS_WRITE
|BIO_FLAGS_SHOULD_RETRY
);
370 case SSL_ERROR_WANT_CONNECT
:
372 BIO_FLAGS_IO_SPECIAL
|BIO_FLAGS_SHOULD_RETRY
);
373 b
->retry_reason
=b
->next_bio
->retry_reason
;
381 if (dbio
->ptr
!= NULL
)
382 SSL_free((SSL
*)dbio
->ptr
);
383 dbio
->ptr
=(char *)SSL_dup(ssl
);
384 ret
=(dbio
->ptr
!= NULL
);
393 static int ssl_puts(bp
,str
)
400 ret
=BIO_write(bp
,str
,n
);
404 BIO
*BIO_new_ssl(ctx
,client
)
411 if ((ret
=BIO_new(BIO_f_ssl())) == NULL
)
413 if ((ssl
=SSL_new(ctx
)) == NULL
)
419 SSL_set_connect_state(ssl
);
421 SSL_set_accept_state(ssl
);
423 BIO_set_ssl(ret
,ssl
,BIO_CLOSE
);
427 int BIO_ssl_copy_session_id(t
,f
)
430 t
=BIO_find_type(t
,BIO_TYPE_SSL
);
431 f
=BIO_find_type(f
,BIO_TYPE_SSL
);
432 if ((t
== NULL
) || (f
== NULL
))
434 if ((t
->ptr
== NULL
) || (f
->ptr
== NULL
))
436 SSL_copy_session_id((SSL
*)t
->ptr
,(SSL
*)f
->ptr
);