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 int ret=0; 129160814Ssimon#if 1 13055714Skris int num; 13155714Skris unsigned char n; 13255714Skris#endif 13355714Skris 13455714Skris if (out == NULL) return(0); 13555714Skris if (b->next_bio == NULL) return(0); 13655714Skris 13755714Skris BIO_clear_retry_flags(b); 138160814Ssimon#if 1 13959191Skris RAND_pseudo_bytes(&n,1); 14055714Skris num=(n&0x07); 14155714Skris 14255714Skris if (outl > num) outl=num; 14355714Skris 14455714Skris if (num == 0) 14555714Skris { 14655714Skris ret= -1; 14755714Skris BIO_set_retry_read(b); 14855714Skris } 14955714Skris else 15055714Skris#endif 15155714Skris { 15255714Skris ret=BIO_read(b->next_bio,out,outl); 15355714Skris if (ret < 0) 15455714Skris BIO_copy_next_retry(b); 15555714Skris } 15655714Skris return(ret); 15755714Skris } 15855714Skris 15968651Skrisstatic int nbiof_write(BIO *b, const char *in, int inl) 16055714Skris { 16155714Skris NBIO_TEST *nt; 16255714Skris int ret=0; 16355714Skris int num; 16455714Skris unsigned char n; 16555714Skris 16655714Skris if ((in == NULL) || (inl <= 0)) return(0); 16755714Skris if (b->next_bio == NULL) return(0); 16855714Skris nt=(NBIO_TEST *)b->ptr; 16955714Skris 17055714Skris BIO_clear_retry_flags(b); 17155714Skris 17255714Skris#if 1 17355714Skris if (nt->lwn > 0) 17455714Skris { 17555714Skris num=nt->lwn; 17655714Skris nt->lwn=0; 17755714Skris } 17855714Skris else 17955714Skris { 18059191Skris RAND_pseudo_bytes(&n,1); 18155714Skris num=(n&7); 18255714Skris } 18355714Skris 18455714Skris if (inl > num) inl=num; 18555714Skris 18655714Skris if (num == 0) 18755714Skris { 18855714Skris ret= -1; 18955714Skris BIO_set_retry_write(b); 19055714Skris } 19155714Skris else 19255714Skris#endif 19355714Skris { 19455714Skris ret=BIO_write(b->next_bio,in,inl); 19555714Skris if (ret < 0) 19655714Skris { 19755714Skris BIO_copy_next_retry(b); 19855714Skris nt->lwn=inl; 19955714Skris } 20055714Skris } 20155714Skris return(ret); 20255714Skris } 20355714Skris 20468651Skrisstatic long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr) 20555714Skris { 20655714Skris long ret; 20755714Skris 20855714Skris if (b->next_bio == NULL) return(0); 20955714Skris switch (cmd) 21055714Skris { 21155714Skris case BIO_C_DO_STATE_MACHINE: 21255714Skris BIO_clear_retry_flags(b); 21355714Skris ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 21455714Skris BIO_copy_next_retry(b); 21555714Skris break; 21655714Skris case BIO_CTRL_DUP: 21755714Skris ret=0L; 21855714Skris break; 21955714Skris default: 22055714Skris ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 22155714Skris break; 22255714Skris } 22355714Skris return(ret); 22455714Skris } 22555714Skris 22668651Skrisstatic long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 22759191Skris { 22859191Skris long ret=1; 22959191Skris 23059191Skris if (b->next_bio == NULL) return(0); 23159191Skris switch (cmd) 23259191Skris { 23359191Skris default: 23459191Skris ret=BIO_callback_ctrl(b->next_bio,cmd,fp); 23559191Skris break; 23659191Skris } 23759191Skris return(ret); 23859191Skris } 23959191Skris 24055714Skrisstatic int nbiof_gets(BIO *bp, char *buf, int size) 24155714Skris { 24255714Skris if (bp->next_bio == NULL) return(0); 24355714Skris return(BIO_gets(bp->next_bio,buf,size)); 24455714Skris } 24555714Skris 24655714Skris 24768651Skrisstatic int nbiof_puts(BIO *bp, const char *str) 24855714Skris { 24955714Skris if (bp->next_bio == NULL) return(0); 25055714Skris return(BIO_puts(bp->next_bio,str)); 25155714Skris } 25255714Skris 25355714Skris 254