]>
git.saurik.com Git - apple/security.git/blob - Security/libsecurity_apple_csp/open_ssl/bio/bio_lib.c
2 * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
19 /* crypto/bio/bio_lib.c */
20 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
21 * All rights reserved.
23 * This package is an SSL implementation written
24 * by Eric Young (eay@cryptsoft.com).
25 * The implementation was written so as to conform with Netscapes SSL.
27 * This library is free for commercial and non-commercial use as long as
28 * the following conditions are aheared to. The following conditions
29 * apply to all code found in this distribution, be it the RC4, RSA,
30 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
31 * included with this distribution is covered by the same copyright terms
32 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
34 * Copyright remains Eric Young's, and as such any Copyright notices in
35 * the code are not to be removed.
36 * If this package is used in a product, Eric Young should be given attribution
37 * as the author of the parts of the library used.
38 * This can be in the form of a textual message at program startup or
39 * in documentation (online or textual) provided with the package.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * "This product includes cryptographic software written by
52 * Eric Young (eay@cryptsoft.com)"
53 * The word 'cryptographic' can be left out if the rouines from the library
54 * being used are not cryptographic related :-).
55 * 4. If you include any Windows specific code (or a derivative thereof) from
56 * the apps directory (application code) you must include an acknowledgement:
57 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
59 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * The licence and distribution terms for any publically available version or
72 * derivative of this code cannot be changed. i.e. this code cannot simply be
73 * copied and put under another distribution licence
74 * [including the GNU Public Licence.]
79 #include <openssl/crypto.h>
81 #include <openssl/bio.h>
82 #include <openssl/stack.h>
84 static STACK_OF(CRYPTO_EX_DATA_FUNCS
) *bio_meth
=NULL
;
85 static int bio_meth_num
=0;
87 BIO
*BIO_new(const BIO_METHOD
*method
)
91 ret
=(BIO
*)Malloc(sizeof(BIO
));
94 BIOerr(BIO_F_BIO_NEW
,ERR_R_MALLOC_FAILURE
);
97 if (!BIO_set(ret
,method
))
105 int BIO_set(BIO
*bio
, const BIO_METHOD
*method
)
121 CRYPTO_new_ex_data(bio_meth
,bio
,&bio
->ex_data
);
122 if (method
->create
!= NULL
)
123 if (!method
->create(bio
))
132 if (a
== NULL
) return(0);
134 i
=CRYPTO_add(&a
->references
,-1,CRYPTO_LOCK_BIO
);
138 if (i
> 0) return(1);
142 fprintf(stderr
,"BIO_free, bad reference count\n");
146 if ((a
->callback
!= NULL
) &&
147 ((i
=(int)a
->callback(a
,BIO_CB_FREE
,NULL
,0,0L,1L)) <= 0))
150 CRYPTO_free_ex_data(bio_meth
,a
,&a
->ex_data
);
152 if ((a
->method
== NULL
) || (a
->method
->destroy
== NULL
)) return(1);
153 ret
=a
->method
->destroy(a
);
158 int BIO_read(BIO
*b
, void *out
, int outl
)
163 if ((b
== NULL
) || (b
->method
== NULL
) || (b
->method
->bread
== NULL
))
165 BIOerr(BIO_F_BIO_READ
,BIO_R_UNSUPPORTED_METHOD
);
171 ((i
=(int)cb(b
,BIO_CB_READ
,out
,outl
,0L,1L)) <= 0))
176 BIOerr(BIO_F_BIO_READ
,BIO_R_UNINITIALIZED
);
180 i
=b
->method
->bread(b
,out
,outl
);
182 if (i
> 0) b
->num_read
+=(unsigned long)i
;
185 i
=(int)cb(b
,BIO_CB_READ
|BIO_CB_RETURN
,out
,outl
,
190 int BIO_write(BIO
*b
, const void *in
, int inl
)
199 if ((b
->method
== NULL
) || (b
->method
->bwrite
== NULL
))
201 BIOerr(BIO_F_BIO_WRITE
,BIO_R_UNSUPPORTED_METHOD
);
206 ((i
=(int)cb(b
,BIO_CB_WRITE
,in
,inl
,0L,1L)) <= 0))
211 BIOerr(BIO_F_BIO_WRITE
,BIO_R_UNINITIALIZED
);
215 i
=b
->method
->bwrite(b
,in
,inl
);
217 if (i
> 0) b
->num_write
+=(unsigned long)i
;
219 /* This is evil and not thread safe. If the BIO has been freed,
220 * we must not call the callback. The only way to be able to
221 * determine this is the reference count which is now invalid since
222 * the memory has been free()ed.
224 if (b
->references
<= 0) abort();
225 if (cb
!= NULL
) /* && (b->references >= 1)) */
226 i
=(int)cb(b
,BIO_CB_WRITE
|BIO_CB_RETURN
,in
,inl
,
231 int BIO_puts(BIO
*b
, const char *in
)
236 if ((b
== NULL
) || (b
->method
== NULL
) || (b
->method
->bputs
== NULL
))
238 BIOerr(BIO_F_BIO_PUTS
,BIO_R_UNSUPPORTED_METHOD
);
245 ((i
=(int)cb(b
,BIO_CB_PUTS
,in
,0,0L,1L)) <= 0))
250 BIOerr(BIO_F_BIO_PUTS
,BIO_R_UNINITIALIZED
);
254 i
=b
->method
->bputs(b
,in
);
257 i
=(int)cb(b
,BIO_CB_PUTS
|BIO_CB_RETURN
,in
,0,
262 int BIO_gets(BIO
*b
, char *in
, int inl
)
267 if ((b
== NULL
) || (b
->method
== NULL
) || (b
->method
->bgets
== NULL
))
269 BIOerr(BIO_F_BIO_GETS
,BIO_R_UNSUPPORTED_METHOD
);
276 ((i
=(int)cb(b
,BIO_CB_GETS
,in
,inl
,0L,1L)) <= 0))
281 BIOerr(BIO_F_BIO_GETS
,BIO_R_UNINITIALIZED
);
285 i
=b
->method
->bgets(b
,in
,inl
);
288 i
=(int)cb(b
,BIO_CB_GETS
|BIO_CB_RETURN
,in
,inl
,
293 long BIO_int_ctrl(BIO
*b
, int cmd
, long larg
, int iarg
)
298 return(BIO_ctrl(b
,cmd
,larg
,(char *)&i
));
301 char *BIO_ptr_ctrl(BIO
*b
, int cmd
, long larg
)
305 if (BIO_ctrl(b
,cmd
,larg
,(char *)&p
) <= 0)
311 long BIO_ctrl(BIO
*b
, int cmd
, long larg
, void *parg
)
316 if (b
== NULL
) return(0);
318 if ((b
->method
== NULL
) || (b
->method
->ctrl
== NULL
))
320 BIOerr(BIO_F_BIO_CTRL
,BIO_R_UNSUPPORTED_METHOD
);
327 ((ret
=cb(b
,BIO_CB_CTRL
,parg
,cmd
,larg
,1L)) <= 0))
330 ret
=b
->method
->ctrl(b
,cmd
,larg
,parg
);
333 ret
=cb(b
,BIO_CB_CTRL
|BIO_CB_RETURN
,parg
,cmd
,
338 long BIO_callback_ctrl(BIO
*b
, int cmd
, void (*fp
)())
343 if (b
== NULL
) return(0);
345 if ((b
->method
== NULL
) || (b
->method
->callback_ctrl
== NULL
))
347 BIOerr(BIO_F_BIO_CTRL
,BIO_R_UNSUPPORTED_METHOD
);
354 ((ret
=cb(b
,BIO_CB_CTRL
,(void *)&fp
,cmd
,0,1L)) <= 0))
357 ret
=b
->method
->callback_ctrl(b
,cmd
,fp
);
360 ret
=cb(b
,BIO_CB_CTRL
|BIO_CB_RETURN
,(void *)&fp
,cmd
,
365 /* It is unfortunate to duplicate in functions what the BIO_(w)pending macros
366 * do; but those macros have inappropriate return type, and for interfacing
367 * from other programming languages, C macros aren't much of a help anyway. */
368 size_t BIO_ctrl_pending(BIO
*bio
)
370 return BIO_ctrl(bio
, BIO_CTRL_PENDING
, 0, NULL
);
373 size_t BIO_ctrl_wpending(BIO
*bio
)
375 return BIO_ctrl(bio
, BIO_CTRL_WPENDING
, 0, NULL
);
379 /* put the 'bio' on the end of b's list of operators */
380 BIO
*BIO_push(BIO
*b
, BIO
*bio
)
384 if (b
== NULL
) return(bio
);
386 while (lb
->next_bio
!= NULL
)
391 /* called to do internal processing */
392 BIO_ctrl(b
,BIO_CTRL_PUSH
,0,NULL
);
396 /* Remove the first and return the rest */
401 if (b
== NULL
) return(NULL
);
404 if (b
->prev_bio
!= NULL
)
405 b
->prev_bio
->next_bio
=b
->next_bio
;
406 if (b
->next_bio
!= NULL
)
407 b
->next_bio
->prev_bio
=b
->prev_bio
;
411 BIO_ctrl(b
,BIO_CTRL_POP
,0,NULL
);
415 BIO
*BIO_get_retry_BIO(BIO
*bio
, int *reason
)
422 if (!BIO_should_retry(b
)) break;
425 if (b
== NULL
) break;
427 if (reason
!= NULL
) *reason
=last
->retry_reason
;
431 int BIO_get_retry_reason(BIO
*bio
)
433 return(bio
->retry_reason
);
436 BIO
*BIO_find_type(BIO
*bio
, int type
)
442 if (bio
->method
!= NULL
)
444 mt
=bio
->method
->type
;
448 if (mt
& type
) return(bio
);
454 } while (bio
!= NULL
);
458 void BIO_free_all(BIO
*bio
)
469 /* Since ref count > 1, don't free anyone else. */
474 BIO
*BIO_dup_chain(BIO
*in
)
476 BIO
*ret
=NULL
,*eoc
=NULL
,*bio
,*new;
478 for (bio
=in
; bio
!= NULL
; bio
=bio
->next_bio
)
480 if ((new=BIO_new(bio
->method
)) == NULL
) goto err
;
481 new->callback
=bio
->callback
;
482 new->cb_arg
=bio
->cb_arg
;
484 new->shutdown
=bio
->shutdown
;
485 new->flags
=bio
->flags
;
487 /* This will let SSL_s_sock() work with stdin/stdout */
490 if (!BIO_dup_state(bio
,(char *)new))
497 if (!CRYPTO_dup_ex_data(bio_meth
,&new->ex_data
,&bio
->ex_data
))
518 void BIO_copy_next_retry(BIO
*b
)
520 BIO_set_flags(b
,BIO_get_retry_flags(b
->next_bio
));
521 b
->retry_reason
=b
->next_bio
->retry_reason
;
524 int BIO_get_ex_new_index(long argl
, void *argp
, CRYPTO_EX_new
*new_func
,
525 CRYPTO_EX_dup
*dup_func
, CRYPTO_EX_free
*free_func
)
528 return(CRYPTO_get_ex_new_index(bio_meth_num
-1,&bio_meth
,
529 argl
,argp
,new_func
,dup_func
,free_func
));
532 int BIO_set_ex_data(BIO
*bio
, int idx
, void *data
)
534 return(CRYPTO_set_ex_data(&(bio
->ex_data
),idx
,data
));
537 void *BIO_get_ex_data(BIO
*bio
, int idx
)
539 return(CRYPTO_get_ex_data(&(bio
->ex_data
),idx
));
542 unsigned long BIO_number_read(BIO
*bio
)
544 if(bio
) return bio
->num_read
;
548 unsigned long BIO_number_written(BIO
*bio
)
550 if(bio
) return bio
->num_write
;