This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / ssl / bio_ssl.c
1 /* ssl/bio_ssl.c */
2 /* Copyright (C) 1995-1997 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 <string.h>
61 #include <errno.h>
62 #include "bio.h"
63 #include "err.h"
64 #include "ssl.h"
65
66 #ifndef NOPROTO
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);
73 #else
74 static int ssl_write();
75 static int ssl_read();
76 static int ssl_puts();
77 static long ssl_ctrl();
78 static int ssl_new();
79 static int ssl_free();
80 #endif
81
82 static BIO_METHOD methods_sslp=
83         {
84         BIO_TYPE_SSL,"ssl",
85         ssl_write,
86         ssl_read,
87         ssl_puts,
88         NULL, /* ssl_gets, */
89         ssl_ctrl,
90         ssl_new,
91         ssl_free,
92         };
93
94 BIO_METHOD *BIO_f_ssl()
95         {
96         return(&methods_sslp);
97         }
98
99 static int ssl_new(bi)
100 BIO *bi;
101         {
102         bi->init=0;
103         bi->ptr=NULL;   /* The SSL structure */
104         bi->flags=0;
105         return(1);
106         }
107
108 static int ssl_free(a)
109 BIO *a;
110         {
111         if (a == NULL) return(0);
112         if (a->ptr != NULL) SSL_shutdown((SSL *)a->ptr);
113         if (a->shutdown)
114                 {
115                 if (a->init) SSL_free((SSL *)a->ptr);
116                 a->init=0;
117                 a->flags=0;
118                 a->ptr=NULL;
119                 }
120         return(1);
121         }
122         
123 static int ssl_read(b,out,outl)
124 BIO *b;
125 char *out;
126 int outl;
127         {
128         int ret=1;
129         int inflags,outflags;
130         SSL *ssl;
131         int retry_reason=0;
132
133         if (out == NULL) return(0);
134         ssl=(SSL *)b->ptr;
135
136         inflags=outflags=b->flags;
137
138         outflags&= ~(BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY);
139
140         if (!SSL_is_init_finished(ssl))
141                 {
142                 ret=SSL_do_handshake(ssl);
143 #if 0
144                 if (ret > 0)
145                         {
146                         outflags=(BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
147                         ret= -1;
148                         goto end;
149                         }
150 #endif
151                 }
152         if (ret > 0)
153                 ret=SSL_read(ssl,out,outl);
154
155         switch (SSL_get_error(ssl,ret))
156                 {
157         case SSL_ERROR_NONE:
158                 if (ret <= 0) break;
159                 break;
160         case SSL_ERROR_WANT_READ:
161                 outflags=(BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
162                 break;
163         case SSL_ERROR_WANT_WRITE:
164                 outflags=(BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY);
165                 break;
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;
169                 break;
170         case SSL_ERROR_WANT_CONNECT:
171                 outflags=(BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY);
172                 retry_reason=BIO_RR_CONNECT;
173                 break;
174         case SSL_ERROR_SYSCALL:
175         case SSL_ERROR_SSL:
176         case SSL_ERROR_ZERO_RETURN:
177         default:
178                 break;
179                 }
180
181         b->retry_reason=retry_reason;
182         b->flags=outflags;
183         return(ret);
184         }
185
186 static int ssl_write(b,out,outl)
187 BIO *b;
188 char *out;
189 int outl;
190         {
191         int ret;
192         int inflags,outflags,retry_reason=0;
193         SSL *ssl;
194
195         if (out == NULL) return(0);
196         ssl=(SSL *)b->ptr;
197
198         inflags=outflags=b->flags;
199
200         outflags&= ~(BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY);
201
202         ret=SSL_do_handshake(ssl);
203         if (ret > 0)
204                 ret=SSL_write(ssl,out,outl);
205
206         switch (SSL_get_error(ssl,ret))
207                 {
208         case SSL_ERROR_NONE:
209                 if (ret <= 0) break;
210                 break;
211         case SSL_ERROR_WANT_WRITE:
212                 outflags=(BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY);
213                 break;
214         case SSL_ERROR_WANT_READ:
215                 outflags=(BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
216                 break;
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;
220                 break;
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:
225         case SSL_ERROR_SSL:
226         default:
227                 break;
228                 }
229
230         b->retry_reason=retry_reason;
231         b->flags=outflags;
232         return(ret);
233         }
234
235 static long ssl_ctrl(b,cmd,num,ptr)
236 BIO *b;
237 int cmd;
238 long num;
239 char *ptr;
240         {
241         SSL **sslp,*ssl;
242         BIO *dbio,*bio;
243         long ret=1;
244
245         ssl=(SSL *)b->ptr;
246         switch (cmd)
247                 {
248         case BIO_CTRL_RESET:
249                 SSL_shutdown(ssl);
250
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);
255
256                 SSL_clear(ssl);
257
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);
262                 else
263                         ret=1;
264                 break;
265         case BIO_CTRL_EOF:
266         case BIO_CTRL_INFO:
267                 ret=0;
268                 break;
269         case BIO_C_SSL_MODE:
270                 if (num) /* client mode */
271                         SSL_set_connect_state(ssl);
272                 else
273                         SSL_set_accept_state(ssl);
274                 break;
275         case BIO_C_SET_SSL:
276                 ssl_free(b);
277                 b->shutdown=(int)num;
278                 b->ptr=ptr;
279                 ssl=(SSL *)ptr;
280                 bio=SSL_get_rbio(ssl);
281                 if (bio != NULL)
282                         {
283                         if (b->next_bio != NULL)
284                                 BIO_push(bio,b->next_bio);
285                         b->next_bio=bio;
286                         }
287                 b->init=1;
288                 break;
289         case BIO_C_GET_SSL:
290                 if (ptr != NULL)
291                         {
292                         sslp=(SSL **)ptr;
293                         *sslp=ssl;
294                         }
295                 break;
296         case BIO_CTRL_GET_CLOSE:
297                 ret=b->shutdown;
298                 break;
299         case BIO_CTRL_SET_CLOSE:
300                 b->shutdown=(int)num;
301                 break;
302         case BIO_CTRL_WPENDING:
303                 ret=BIO_ctrl(ssl->wbio,cmd,num,ptr);
304                 break;
305         case BIO_CTRL_PENDING:
306                 ret=SSL_pending(ssl);
307                 if (ret == 0)
308                         ret=BIO_pending(ssl->rbio);
309                 break;
310         case BIO_CTRL_FLUSH:
311                 BIO_clear_retry_flags(b);
312                 ret=BIO_ctrl(ssl->wbio,cmd,num,ptr);
313                 BIO_copy_next_retry(b);
314                 break;
315         case BIO_CTRL_PUSH:
316                 if (b->next_bio != NULL)
317                         {
318                         SSL_set_bio(ssl,b->next_bio,b->next_bio);
319                         b->next_bio->references++;
320                         }
321                 break;
322         case BIO_CTRL_POP:
323                 /* ugly bit of a hack */
324                 if (ssl->rbio != ssl->wbio) /* we are in trouble :-( */
325                         {
326                         BIO_free_all(ssl->wbio);
327                         }
328                 ssl->wbio=NULL;
329                 ssl->rbio=NULL;
330                 break;
331         case BIO_C_DO_STATE_MACHINE:
332                 BIO_clear_retry_flags(b);
333
334                 b->retry_reason=0;
335                 ret=(int)SSL_do_handshake(ssl);
336
337                 switch (SSL_get_error(ssl,(int)ret))
338                         {
339                 case SSL_ERROR_WANT_READ:
340                         BIO_set_flags(b,
341                                 BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
342                         break;
343                 case SSL_ERROR_WANT_WRITE:
344                         BIO_set_flags(b,
345                                 BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY);
346                         break;
347                 case SSL_ERROR_WANT_CONNECT:
348                         BIO_set_flags(b,
349                                 BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY);
350                         b->retry_reason=b->next_bio->retry_reason;
351                         break;
352                 default:
353                         break;
354                         }
355                 break;
356         case BIO_CTRL_DUP:
357                 dbio=(BIO *)ptr;
358                 if (dbio->ptr != NULL)
359                         SSL_free((SSL *)dbio->ptr);
360                 dbio->ptr=(char *)SSL_dup(ssl);
361                 ret=(dbio->ptr != NULL);
362                 break;
363         default:
364                 return(0);
365                 break;
366                 }
367         return(ret);
368         }
369
370 static int ssl_puts(bp,str)
371 BIO *bp;
372 char *str;
373         {
374         int n,ret;
375
376         n=strlen(str);
377         ret=BIO_write(bp,str,n);
378         return(ret);
379         }
380
381 BIO *BIO_new_ssl(ctx,client)
382 SSL_CTX *ctx;
383 int client;
384         {
385         BIO *ret;
386         SSL *ssl;
387
388         if ((ret=BIO_new(BIO_f_ssl())) == NULL)
389                 return(NULL);
390         if ((ssl=SSL_new(ctx)) == NULL)
391                 {
392                 BIO_free(ret);
393                 return(NULL);
394                 }
395         if (client)
396                 SSL_set_connect_state(ssl);
397         else
398                 SSL_set_accept_state(ssl);
399                 
400         BIO_set_ssl(ret,ssl,BIO_CLOSE);
401         return(ret);
402         }
403
404 int BIO_ssl_copy_session_id(t,f)
405 BIO *t,*f;
406         {
407         t=BIO_find_type(t,BIO_TYPE_SSL);
408         f=BIO_find_type(f,BIO_TYPE_SSL);
409         if ((t == NULL) || (f == NULL))
410                 return(0);
411         if ((t->ptr == NULL) || (f->ptr == NULL))
412                 return(0);
413         SSL_copy_session_id((SSL *)t->ptr,(SSL *)f->ptr);
414         return(1);
415         }
416
417 void BIO_ssl_shutdown(b)
418 BIO *b;
419         {
420         SSL *s;
421
422         while (b != NULL)
423                 {
424                 if (b->method->type == BIO_TYPE_SSL)
425                         {
426                         s=(SSL *)b->ptr;
427                         SSL_shutdown(s);
428                         break;
429                         }
430                 b=b->next_bio;
431                 }
432         }