bf_nbio.c revision 302408
1290650Shselasky/* crypto/bio/bf_nbio.c */ 2347819Shselasky/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3290650Shselasky * All rights reserved. 4290650Shselasky * 5290650Shselasky * This package is an SSL implementation written 6290650Shselasky * by Eric Young (eay@cryptsoft.com). 7290650Shselasky * The implementation was written so as to conform with Netscapes SSL. 8290650Shselasky * 9290650Shselasky * This library is free for commercial and non-commercial use as long as 10290650Shselasky * the following conditions are aheared to. The following conditions 11290650Shselasky * apply to all code found in this distribution, be it the RC4, RSA, 12290650Shselasky * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13290650Shselasky * included with this distribution is covered by the same copyright terms 14290650Shselasky * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15290650Shselasky * 16290650Shselasky * Copyright remains Eric Young's, and as such any Copyright notices in 17290650Shselasky * the code are not to be removed. 18290650Shselasky * If this package is used in a product, Eric Young should be given attribution 19290650Shselasky * as the author of the parts of the library used. 20290650Shselasky * This can be in the form of a textual message at program startup or 21290650Shselasky * in documentation (online or textual) provided with the package. 22290650Shselasky * 23290650Shselasky * Redistribution and use in source and binary forms, with or without 24290650Shselasky * modification, are permitted provided that the following conditions 25290650Shselasky * are met: 26290650Shselasky * 1. Redistributions of source code must retain the copyright 27290650Shselasky * notice, this list of conditions and the following disclaimer. 28300676Shselasky * 2. Redistributions in binary form must reproduce the above copyright 29300676Shselasky * notice, this list of conditions and the following disclaimer in the 30290650Shselasky * documentation and/or other materials provided with the distribution. 31290650Shselasky * 3. All advertising materials mentioning features or use of this software 32290650Shselasky * must display the following acknowledgement: 33290650Shselasky * "This product includes cryptographic software written by 34290650Shselasky * Eric Young (eay@cryptsoft.com)" 35290650Shselasky * The word 'cryptographic' can be left out if the rouines from the library 36290650Shselasky * being used are not cryptographic related :-). 37290650Shselasky * 4. If you include any Windows specific code (or a derivative thereof) from 38347802Shselasky * the apps directory (application code) you must include an acknowledgement: 39290650Shselasky * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40290650Shselasky * 41290650Shselasky * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42290650Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43290650Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44290650Shselasky * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45341958Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46341958Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47290650Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48329200Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49290650Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50341948Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51341948Shselasky * SUCH DAMAGE. 52290650Shselasky * 53290650Shselasky * The licence and distribution terms for any publically available version or 54290650Shselasky * derivative of this code cannot be changed. i.e. this code cannot simply be 55290650Shselasky * copied and put under another distribution licence 56290650Shselasky * [including the GNU Public Licence.] 57290650Shselasky */ 58290650Shselasky 59290650Shselasky#include <stdio.h> 60290650Shselasky#include <errno.h> 61290650Shselasky#include "cryptlib.h" 62290650Shselasky#include <openssl/rand.h> 63290650Shselasky#include <openssl/bio.h> 64290650Shselasky 65290650Shselasky/* 66290650Shselasky * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest 67290650Shselasky */ 68290650Shselasky 69341931Shselaskystatic int nbiof_write(BIO *h, const char *buf, int num); 70341931Shselaskystatic int nbiof_read(BIO *h, char *buf, int size); 71347819Shselaskystatic int nbiof_puts(BIO *h, const char *str); 72347819Shselaskystatic int nbiof_gets(BIO *h, char *str, int size); 73347819Shselaskystatic long nbiof_ctrl(BIO *h, int cmd, long arg1, void *arg2); 74347819Shselaskystatic int nbiof_new(BIO *h); 75347819Shselaskystatic int nbiof_free(BIO *data); 76290650Shselaskystatic long nbiof_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); 77290650Shselaskytypedef struct nbio_test_st { 78290650Shselasky /* only set if we sent a 'should retry' error */ 79290650Shselasky int lrn; 80290650Shselasky int lwn; 81290650Shselasky} NBIO_TEST; 82290650Shselasky 83290650Shselaskystatic BIO_METHOD methods_nbiof = { 84290650Shselasky BIO_TYPE_NBIO_TEST, 85290650Shselasky "non-blocking IO test filter", 86290650Shselasky nbiof_write, 87290650Shselasky nbiof_read, 88329209Shselasky nbiof_puts, 89329209Shselasky nbiof_gets, 90329209Shselasky nbiof_ctrl, 91329209Shselasky nbiof_new, 92329209Shselasky nbiof_free, 93290650Shselasky nbiof_callback_ctrl, 94290650Shselasky}; 95290650Shselasky 96290650ShselaskyBIO_METHOD *BIO_f_nbio_test(void) 97290650Shselasky{ 98290650Shselasky return (&methods_nbiof); 99290650Shselasky} 100290650Shselasky 101290650Shselaskystatic int nbiof_new(BIO *bi) 102290650Shselasky{ 103290650Shselasky NBIO_TEST *nt; 104290650Shselasky 105290650Shselasky if (!(nt = (NBIO_TEST *)OPENSSL_malloc(sizeof(NBIO_TEST)))) 106290650Shselasky return (0); 107290650Shselasky nt->lrn = -1; 108290650Shselasky nt->lwn = -1; 109290650Shselasky bi->ptr = (char *)nt; 110290650Shselasky bi->init = 1; 111290650Shselasky bi->flags = 0; 112290650Shselasky return (1); 113290650Shselasky} 114290650Shselasky 115290650Shselaskystatic int nbiof_free(BIO *a) 116290650Shselasky{ 117290650Shselasky if (a == NULL) 118290650Shselasky return (0); 119290650Shselasky if (a->ptr != NULL) 120290650Shselasky OPENSSL_free(a->ptr); 121290650Shselasky a->ptr = NULL; 122290650Shselasky a->init = 0; 123290650Shselasky a->flags = 0; 124290650Shselasky return (1); 125290650Shselasky} 126290650Shselasky 127290650Shselaskystatic int nbiof_read(BIO *b, char *out, int outl) 128290650Shselasky{ 129290650Shselasky int ret = 0; 130290650Shselasky#if 1 131290650Shselasky int num; 132290650Shselasky unsigned char n; 133290650Shselasky#endif 134290650Shselasky 135290650Shselasky if (out == NULL) 136290650Shselasky return (0); 137290650Shselasky if (b->next_bio == NULL) 138290650Shselasky return (0); 139290650Shselasky 140290650Shselasky BIO_clear_retry_flags(b); 141290650Shselasky#if 1 142290650Shselasky if (RAND_pseudo_bytes(&n, 1) < 0) 143290650Shselasky return -1; 144290650Shselasky num = (n & 0x07); 145290650Shselasky 146290650Shselasky if (outl > num) 147290650Shselasky outl = num; 148290650Shselasky 149290650Shselasky if (num == 0) { 150290650Shselasky ret = -1; 151290650Shselasky BIO_set_retry_read(b); 152290650Shselasky } else 153290650Shselasky#endif 154290650Shselasky { 155290650Shselasky ret = BIO_read(b->next_bio, out, outl); 156290650Shselasky if (ret < 0) 157290650Shselasky BIO_copy_next_retry(b); 158290650Shselasky } 159290650Shselasky return (ret); 160290650Shselasky} 161290650Shselasky 162290650Shselaskystatic int nbiof_write(BIO *b, const char *in, int inl) 163290650Shselasky{ 164290650Shselasky NBIO_TEST *nt; 165290650Shselasky int ret = 0; 166290650Shselasky int num; 167290650Shselasky unsigned char n; 168290650Shselasky 169290650Shselasky if ((in == NULL) || (inl <= 0)) 170290650Shselasky return (0); 171290650Shselasky if (b->next_bio == NULL) 172290650Shselasky return (0); 173290650Shselasky nt = (NBIO_TEST *)b->ptr; 174290650Shselasky 175290650Shselasky BIO_clear_retry_flags(b); 176290650Shselasky 177290650Shselasky#if 1 178290650Shselasky if (nt->lwn > 0) { 179290650Shselasky num = nt->lwn; 180290650Shselasky nt->lwn = 0; 181290650Shselasky } else { 182290650Shselasky if (RAND_pseudo_bytes(&n, 1) < 0) 183290650Shselasky return -1; 184290650Shselasky num = (n & 7); 185290650Shselasky } 186290650Shselasky 187290650Shselasky if (inl > num) 188290650Shselasky inl = num; 189290650Shselasky 190290650Shselasky if (num == 0) { 191290650Shselasky ret = -1; 192290650Shselasky BIO_set_retry_write(b); 193290650Shselasky } else 194290650Shselasky#endif 195290650Shselasky { 196290650Shselasky ret = BIO_write(b->next_bio, in, inl); 197290650Shselasky if (ret < 0) { 198290650Shselasky BIO_copy_next_retry(b); 199290650Shselasky nt->lwn = inl; 200331580Shselasky } 201331580Shselasky } 202331580Shselasky return (ret); 203331580Shselasky} 204331580Shselasky 205331580Shselaskystatic long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr) 206331580Shselasky{ 207331580Shselasky long ret; 208331580Shselasky 209331580Shselasky if (b->next_bio == NULL) 210331580Shselasky return (0); 211331580Shselasky switch (cmd) { 212331580Shselasky case BIO_C_DO_STATE_MACHINE: 213331580Shselasky BIO_clear_retry_flags(b); 214331580Shselasky ret = BIO_ctrl(b->next_bio, cmd, num, ptr); 215331580Shselasky BIO_copy_next_retry(b); 216331580Shselasky break; 217331580Shselasky case BIO_CTRL_DUP: 218331580Shselasky ret = 0L; 219331580Shselasky break; 220331580Shselasky default: 221331580Shselasky ret = BIO_ctrl(b->next_bio, cmd, num, ptr); 222331580Shselasky break; 223331580Shselasky } 224331580Shselasky return (ret); 225331580Shselasky} 226331580Shselasky 227331580Shselaskystatic long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 228290650Shselasky{ 229290650Shselasky long ret = 1; 230290650Shselasky 231290650Shselasky if (b->next_bio == NULL) 232290650Shselasky return (0); 233290650Shselasky switch (cmd) { 234290650Shselasky default: 235290650Shselasky ret = BIO_callback_ctrl(b->next_bio, cmd, fp); 236290650Shselasky break; 237290650Shselasky } 238290650Shselasky return (ret); 239290650Shselasky} 240290650Shselasky 241290650Shselaskystatic int nbiof_gets(BIO *bp, char *buf, int size) 242290650Shselasky{ 243290650Shselasky if (bp->next_bio == NULL) 244290650Shselasky return (0); 245290650Shselasky return (BIO_gets(bp->next_bio, buf, size)); 246290650Shselasky} 247290650Shselasky 248290650Shselaskystatic int nbiof_puts(BIO *bp, const char *str) 249290650Shselasky{ 250290650Shselasky if (bp->next_bio == NULL) 251290650Shselasky return (0); 252290650Shselasky return (BIO_puts(bp->next_bio, str)); 253290650Shselasky} 254338554Shselasky