bf_nbio.c revision 160814
155714Skris/* crypto/bio/bf_nbio.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 855714Skris * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1555714Skris * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 2255714Skris * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4055714Skris * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 5255714Skris * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include <errno.h> 6155714Skris#include "cryptlib.h" 6255714Skris#include <openssl/rand.h> 6355714Skris#include <openssl/bio.h> 6455714Skris 6555714Skris/* BIO_put and BIO_get both add to the digest, 6655714Skris * BIO_gets returns the digest */ 6755714Skris 6868651Skrisstatic int nbiof_write(BIO *h,const char *buf,int num); 6955714Skrisstatic int nbiof_read(BIO *h,char *buf,int size); 7068651Skrisstatic int nbiof_puts(BIO *h,const char *str); 7155714Skrisstatic int nbiof_gets(BIO *h,char *str,int size); 7268651Skrisstatic long nbiof_ctrl(BIO *h,int cmd,long arg1,void *arg2); 7355714Skrisstatic int nbiof_new(BIO *h); 7455714Skrisstatic int nbiof_free(BIO *data); 7568651Skrisstatic long nbiof_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp); 7655714Skristypedef struct nbio_test_st 7755714Skris { 7855714Skris /* only set if we sent a 'should retry' error */ 7955714Skris int lrn; 8055714Skris int lwn; 8155714Skris } NBIO_TEST; 8255714Skris 8355714Skrisstatic BIO_METHOD methods_nbiof= 8455714Skris { 8555714Skris BIO_TYPE_NBIO_TEST, 8655714Skris "non-blocking IO test filter", 8755714Skris nbiof_write, 8855714Skris nbiof_read, 8955714Skris nbiof_puts, 9055714Skris nbiof_gets, 9155714Skris nbiof_ctrl, 9255714Skris nbiof_new, 9355714Skris nbiof_free, 9459191Skris nbiof_callback_ctrl, 9555714Skris }; 9655714Skris 9755714SkrisBIO_METHOD *BIO_f_nbio_test(void) 9855714Skris { 9955714Skris return(&methods_nbiof); 10055714Skris } 10155714Skris 10255714Skrisstatic int nbiof_new(BIO *bi) 10355714Skris { 10455714Skris NBIO_TEST *nt; 10555714Skris 106100936Snectar if (!(nt=(NBIO_TEST *)OPENSSL_malloc(sizeof(NBIO_TEST)))) return(0); 10755714Skris nt->lrn= -1; 10855714Skris nt->lwn= -1; 10955714Skris bi->ptr=(char *)nt; 11055714Skris bi->init=1; 11155714Skris bi->flags=0; 11255714Skris return(1); 11355714Skris } 11455714Skris 11555714Skrisstatic int nbiof_free(BIO *a) 11655714Skris { 11755714Skris if (a == NULL) return(0); 11855714Skris if (a->ptr != NULL) 11968651Skris OPENSSL_free(a->ptr); 12055714Skris a->ptr=NULL; 12155714Skris a->init=0; 12255714Skris a->flags=0; 12355714Skris return(1); 12455714Skris } 12555714Skris 12655714Skrisstatic int nbiof_read(BIO *b, char *out, int outl) 12755714Skris { 12855714Skris NBIO_TEST *nt; 12955714Skris int ret=0; 130160814Ssimon#if 1 13155714Skris int num; 13255714Skris unsigned char n; 13355714Skris#endif 13455714Skris 13555714Skris if (out == NULL) return(0); 13655714Skris if (b->next_bio == NULL) return(0); 13755714Skris nt=(NBIO_TEST *)b->ptr; 13855714Skris 13955714Skris BIO_clear_retry_flags(b); 140160814Ssimon#if 1 14159191Skris RAND_pseudo_bytes(&n,1); 14255714Skris num=(n&0x07); 14355714Skris 14455714Skris if (outl > num) outl=num; 14555714Skris 14655714Skris if (num == 0) 14755714Skris { 14855714Skris ret= -1; 14955714Skris BIO_set_retry_read(b); 15055714Skris } 15155714Skris else 15255714Skris#endif 15355714Skris { 15455714Skris ret=BIO_read(b->next_bio,out,outl); 15555714Skris if (ret < 0) 15655714Skris BIO_copy_next_retry(b); 15755714Skris } 15855714Skris return(ret); 15955714Skris } 16055714Skris 16168651Skrisstatic int nbiof_write(BIO *b, const char *in, int inl) 16255714Skris { 16355714Skris NBIO_TEST *nt; 16455714Skris int ret=0; 16555714Skris int num; 16655714Skris unsigned char n; 16755714Skris 16855714Skris if ((in == NULL) || (inl <= 0)) return(0); 16955714Skris if (b->next_bio == NULL) return(0); 17055714Skris nt=(NBIO_TEST *)b->ptr; 17155714Skris 17255714Skris BIO_clear_retry_flags(b); 17355714Skris 17455714Skris#if 1 17555714Skris if (nt->lwn > 0) 17655714Skris { 17755714Skris num=nt->lwn; 17855714Skris nt->lwn=0; 17955714Skris } 18055714Skris else 18155714Skris { 18259191Skris RAND_pseudo_bytes(&n,1); 18355714Skris num=(n&7); 18455714Skris } 18555714Skris 18655714Skris if (inl > num) inl=num; 18755714Skris 18855714Skris if (num == 0) 18955714Skris { 19055714Skris ret= -1; 19155714Skris BIO_set_retry_write(b); 19255714Skris } 19355714Skris else 19455714Skris#endif 19555714Skris { 19655714Skris ret=BIO_write(b->next_bio,in,inl); 19755714Skris if (ret < 0) 19855714Skris { 19955714Skris BIO_copy_next_retry(b); 20055714Skris nt->lwn=inl; 20155714Skris } 20255714Skris } 20355714Skris return(ret); 20455714Skris } 20555714Skris 20668651Skrisstatic long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr) 20755714Skris { 20855714Skris long ret; 20955714Skris 21055714Skris if (b->next_bio == NULL) return(0); 21155714Skris switch (cmd) 21255714Skris { 21355714Skris case BIO_C_DO_STATE_MACHINE: 21455714Skris BIO_clear_retry_flags(b); 21555714Skris ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 21655714Skris BIO_copy_next_retry(b); 21755714Skris break; 21855714Skris case BIO_CTRL_DUP: 21955714Skris ret=0L; 22055714Skris break; 22155714Skris default: 22255714Skris ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 22355714Skris break; 22455714Skris } 22555714Skris return(ret); 22655714Skris } 22755714Skris 22868651Skrisstatic long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 22959191Skris { 23059191Skris long ret=1; 23159191Skris 23259191Skris if (b->next_bio == NULL) return(0); 23359191Skris switch (cmd) 23459191Skris { 23559191Skris default: 23659191Skris ret=BIO_callback_ctrl(b->next_bio,cmd,fp); 23759191Skris break; 23859191Skris } 23959191Skris return(ret); 24059191Skris } 24159191Skris 24255714Skrisstatic int nbiof_gets(BIO *bp, char *buf, int size) 24355714Skris { 24455714Skris if (bp->next_bio == NULL) return(0); 24555714Skris return(BIO_gets(bp->next_bio,buf,size)); 24655714Skris } 24755714Skris 24855714Skris 24968651Skrisstatic int nbiof_puts(BIO *bp, const char *str) 25055714Skris { 25155714Skris if (bp->next_bio == NULL) return(0); 25255714Skris return(BIO_puts(bp->next_bio,str)); 25355714Skris } 25455714Skris 25555714Skris 256