bf_nbio.c revision 68651
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#include <openssl/evp.h> 6555714Skris 6655714Skris/* BIO_put and BIO_get both add to the digest, 6755714Skris * BIO_gets returns the digest */ 6855714Skris 6968651Skrisstatic int nbiof_write(BIO *h,const char *buf,int num); 7055714Skrisstatic int nbiof_read(BIO *h,char *buf,int size); 7168651Skrisstatic int nbiof_puts(BIO *h,const char *str); 7255714Skrisstatic int nbiof_gets(BIO *h,char *str,int size); 7368651Skrisstatic long nbiof_ctrl(BIO *h,int cmd,long arg1,void *arg2); 7455714Skrisstatic int nbiof_new(BIO *h); 7555714Skrisstatic int nbiof_free(BIO *data); 7668651Skrisstatic long nbiof_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp); 7755714Skristypedef struct nbio_test_st 7855714Skris { 7955714Skris /* only set if we sent a 'should retry' error */ 8055714Skris int lrn; 8155714Skris int lwn; 8255714Skris } NBIO_TEST; 8355714Skris 8455714Skrisstatic BIO_METHOD methods_nbiof= 8555714Skris { 8655714Skris BIO_TYPE_NBIO_TEST, 8755714Skris "non-blocking IO test filter", 8855714Skris nbiof_write, 8955714Skris nbiof_read, 9055714Skris nbiof_puts, 9155714Skris nbiof_gets, 9255714Skris nbiof_ctrl, 9355714Skris nbiof_new, 9455714Skris nbiof_free, 9559191Skris nbiof_callback_ctrl, 9655714Skris }; 9755714Skris 9855714SkrisBIO_METHOD *BIO_f_nbio_test(void) 9955714Skris { 10055714Skris return(&methods_nbiof); 10155714Skris } 10255714Skris 10355714Skrisstatic int nbiof_new(BIO *bi) 10455714Skris { 10555714Skris NBIO_TEST *nt; 10655714Skris 10768651Skris nt=(NBIO_TEST *)OPENSSL_malloc(sizeof(NBIO_TEST)); 10855714Skris nt->lrn= -1; 10955714Skris nt->lwn= -1; 11055714Skris bi->ptr=(char *)nt; 11155714Skris bi->init=1; 11255714Skris bi->flags=0; 11355714Skris return(1); 11455714Skris } 11555714Skris 11655714Skrisstatic int nbiof_free(BIO *a) 11755714Skris { 11855714Skris if (a == NULL) return(0); 11955714Skris if (a->ptr != NULL) 12068651Skris OPENSSL_free(a->ptr); 12155714Skris a->ptr=NULL; 12255714Skris a->init=0; 12355714Skris a->flags=0; 12455714Skris return(1); 12555714Skris } 12655714Skris 12755714Skrisstatic int nbiof_read(BIO *b, char *out, int outl) 12855714Skris { 12955714Skris NBIO_TEST *nt; 13055714Skris int ret=0; 13155714Skris#if 0 13255714Skris int num; 13355714Skris unsigned char n; 13455714Skris#endif 13555714Skris 13655714Skris if (out == NULL) return(0); 13755714Skris if (b->next_bio == NULL) return(0); 13855714Skris nt=(NBIO_TEST *)b->ptr; 13955714Skris 14055714Skris BIO_clear_retry_flags(b); 14155714Skris#if 0 14259191Skris RAND_pseudo_bytes(&n,1); 14355714Skris num=(n&0x07); 14455714Skris 14555714Skris if (outl > num) outl=num; 14655714Skris 14755714Skris if (num == 0) 14855714Skris { 14955714Skris ret= -1; 15055714Skris BIO_set_retry_read(b); 15155714Skris } 15255714Skris else 15355714Skris#endif 15455714Skris { 15555714Skris ret=BIO_read(b->next_bio,out,outl); 15655714Skris if (ret < 0) 15755714Skris BIO_copy_next_retry(b); 15855714Skris } 15955714Skris return(ret); 16055714Skris } 16155714Skris 16268651Skrisstatic int nbiof_write(BIO *b, const char *in, int inl) 16355714Skris { 16455714Skris NBIO_TEST *nt; 16555714Skris int ret=0; 16655714Skris int num; 16755714Skris unsigned char n; 16855714Skris 16955714Skris if ((in == NULL) || (inl <= 0)) return(0); 17055714Skris if (b->next_bio == NULL) return(0); 17155714Skris nt=(NBIO_TEST *)b->ptr; 17255714Skris 17355714Skris BIO_clear_retry_flags(b); 17455714Skris 17555714Skris#if 1 17655714Skris if (nt->lwn > 0) 17755714Skris { 17855714Skris num=nt->lwn; 17955714Skris nt->lwn=0; 18055714Skris } 18155714Skris else 18255714Skris { 18359191Skris RAND_pseudo_bytes(&n,1); 18455714Skris num=(n&7); 18555714Skris } 18655714Skris 18755714Skris if (inl > num) inl=num; 18855714Skris 18955714Skris if (num == 0) 19055714Skris { 19155714Skris ret= -1; 19255714Skris BIO_set_retry_write(b); 19355714Skris } 19455714Skris else 19555714Skris#endif 19655714Skris { 19755714Skris ret=BIO_write(b->next_bio,in,inl); 19855714Skris if (ret < 0) 19955714Skris { 20055714Skris BIO_copy_next_retry(b); 20155714Skris nt->lwn=inl; 20255714Skris } 20355714Skris } 20455714Skris return(ret); 20555714Skris } 20655714Skris 20768651Skrisstatic long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr) 20855714Skris { 20955714Skris long ret; 21055714Skris 21155714Skris if (b->next_bio == NULL) return(0); 21255714Skris switch (cmd) 21355714Skris { 21455714Skris case BIO_C_DO_STATE_MACHINE: 21555714Skris BIO_clear_retry_flags(b); 21655714Skris ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 21755714Skris BIO_copy_next_retry(b); 21855714Skris break; 21955714Skris case BIO_CTRL_DUP: 22055714Skris ret=0L; 22155714Skris break; 22255714Skris default: 22355714Skris ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 22455714Skris break; 22555714Skris } 22655714Skris return(ret); 22755714Skris } 22855714Skris 22968651Skrisstatic long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 23059191Skris { 23159191Skris long ret=1; 23259191Skris 23359191Skris if (b->next_bio == NULL) return(0); 23459191Skris switch (cmd) 23559191Skris { 23659191Skris default: 23759191Skris ret=BIO_callback_ctrl(b->next_bio,cmd,fp); 23859191Skris break; 23959191Skris } 24059191Skris return(ret); 24159191Skris } 24259191Skris 24355714Skrisstatic int nbiof_gets(BIO *bp, char *buf, int size) 24455714Skris { 24555714Skris if (bp->next_bio == NULL) return(0); 24655714Skris return(BIO_gets(bp->next_bio,buf,size)); 24755714Skris } 24855714Skris 24955714Skris 25068651Skrisstatic int nbiof_puts(BIO *bp, const char *str) 25155714Skris { 25255714Skris if (bp->next_bio == NULL) return(0); 25355714Skris return(BIO_puts(bp->next_bio,str)); 25455714Skris } 25555714Skris 25655714Skris 257