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.
8296465Sdelphij *
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).
15296465Sdelphij *
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.
22296465Sdelphij *
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 :-).
37296465Sdelphij * 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)"
40296465Sdelphij *
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.
52296465Sdelphij *
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
65296465Sdelphij/*
66296465Sdelphij * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
67296465Sdelphij */
6855714Skris
69296465Sdelphijstatic int nbiof_write(BIO *h, const char *buf, int num);
70296465Sdelphijstatic int nbiof_read(BIO *h, char *buf, int size);
71296465Sdelphijstatic int nbiof_puts(BIO *h, const char *str);
72296465Sdelphijstatic int nbiof_gets(BIO *h, char *str, int size);
73296465Sdelphijstatic long nbiof_ctrl(BIO *h, int cmd, long arg1, void *arg2);
7455714Skrisstatic int nbiof_new(BIO *h);
7555714Skrisstatic int nbiof_free(BIO *data);
76296465Sdelphijstatic long nbiof_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
77296465Sdelphijtypedef struct nbio_test_st {
78296465Sdelphij    /* only set if we sent a 'should retry' error */
79296465Sdelphij    int lrn;
80296465Sdelphij    int lwn;
81296465Sdelphij} NBIO_TEST;
8255714Skris
83296465Sdelphijstatic BIO_METHOD methods_nbiof = {
84296465Sdelphij    BIO_TYPE_NBIO_TEST,
85296465Sdelphij    "non-blocking IO test filter",
86296465Sdelphij    nbiof_write,
87296465Sdelphij    nbiof_read,
88296465Sdelphij    nbiof_puts,
89296465Sdelphij    nbiof_gets,
90296465Sdelphij    nbiof_ctrl,
91296465Sdelphij    nbiof_new,
92296465Sdelphij    nbiof_free,
93296465Sdelphij    nbiof_callback_ctrl,
94296465Sdelphij};
9555714Skris
9655714SkrisBIO_METHOD *BIO_f_nbio_test(void)
97296465Sdelphij{
98296465Sdelphij    return (&methods_nbiof);
99296465Sdelphij}
10055714Skris
10155714Skrisstatic int nbiof_new(BIO *bi)
102296465Sdelphij{
103296465Sdelphij    NBIO_TEST *nt;
10455714Skris
105296465Sdelphij    if (!(nt = (NBIO_TEST *)OPENSSL_malloc(sizeof(NBIO_TEST))))
106296465Sdelphij        return (0);
107296465Sdelphij    nt->lrn = -1;
108296465Sdelphij    nt->lwn = -1;
109296465Sdelphij    bi->ptr = (char *)nt;
110296465Sdelphij    bi->init = 1;
111296465Sdelphij    bi->flags = 0;
112296465Sdelphij    return (1);
113296465Sdelphij}
11455714Skris
11555714Skrisstatic int nbiof_free(BIO *a)
116296465Sdelphij{
117296465Sdelphij    if (a == NULL)
118296465Sdelphij        return (0);
119296465Sdelphij    if (a->ptr != NULL)
120296465Sdelphij        OPENSSL_free(a->ptr);
121296465Sdelphij    a->ptr = NULL;
122296465Sdelphij    a->init = 0;
123296465Sdelphij    a->flags = 0;
124296465Sdelphij    return (1);
125296465Sdelphij}
126296465Sdelphij
12755714Skrisstatic int nbiof_read(BIO *b, char *out, int outl)
128296465Sdelphij{
129296465Sdelphij    int ret = 0;
130160814Ssimon#if 1
131296465Sdelphij    int num;
132296465Sdelphij    unsigned char n;
13355714Skris#endif
13455714Skris
135296465Sdelphij    if (out == NULL)
136296465Sdelphij        return (0);
137296465Sdelphij    if (b->next_bio == NULL)
138296465Sdelphij        return (0);
13955714Skris
140296465Sdelphij    BIO_clear_retry_flags(b);
141160814Ssimon#if 1
142296465Sdelphij    RAND_pseudo_bytes(&n, 1);
143296465Sdelphij    num = (n & 0x07);
14455714Skris
145296465Sdelphij    if (outl > num)
146296465Sdelphij        outl = num;
14755714Skris
148296465Sdelphij    if (num == 0) {
149296465Sdelphij        ret = -1;
150296465Sdelphij        BIO_set_retry_read(b);
151296465Sdelphij    } else
15255714Skris#endif
153296465Sdelphij    {
154296465Sdelphij        ret = BIO_read(b->next_bio, out, outl);
155296465Sdelphij        if (ret < 0)
156296465Sdelphij            BIO_copy_next_retry(b);
157296465Sdelphij    }
158296465Sdelphij    return (ret);
159296465Sdelphij}
16055714Skris
16168651Skrisstatic int nbiof_write(BIO *b, const char *in, int inl)
162296465Sdelphij{
163296465Sdelphij    NBIO_TEST *nt;
164296465Sdelphij    int ret = 0;
165296465Sdelphij    int num;
166296465Sdelphij    unsigned char n;
16755714Skris
168296465Sdelphij    if ((in == NULL) || (inl <= 0))
169296465Sdelphij        return (0);
170296465Sdelphij    if (b->next_bio == NULL)
171296465Sdelphij        return (0);
172296465Sdelphij    nt = (NBIO_TEST *)b->ptr;
17355714Skris
174296465Sdelphij    BIO_clear_retry_flags(b);
17555714Skris
17655714Skris#if 1
177296465Sdelphij    if (nt->lwn > 0) {
178296465Sdelphij        num = nt->lwn;
179296465Sdelphij        nt->lwn = 0;
180296465Sdelphij    } else {
181296465Sdelphij        RAND_pseudo_bytes(&n, 1);
182296465Sdelphij        num = (n & 7);
183296465Sdelphij    }
18455714Skris
185296465Sdelphij    if (inl > num)
186296465Sdelphij        inl = num;
18755714Skris
188296465Sdelphij    if (num == 0) {
189296465Sdelphij        ret = -1;
190296465Sdelphij        BIO_set_retry_write(b);
191296465Sdelphij    } else
19255714Skris#endif
193296465Sdelphij    {
194296465Sdelphij        ret = BIO_write(b->next_bio, in, inl);
195296465Sdelphij        if (ret < 0) {
196296465Sdelphij            BIO_copy_next_retry(b);
197296465Sdelphij            nt->lwn = inl;
198296465Sdelphij        }
199296465Sdelphij    }
200296465Sdelphij    return (ret);
201296465Sdelphij}
20255714Skris
20368651Skrisstatic long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
204296465Sdelphij{
205296465Sdelphij    long ret;
20655714Skris
207296465Sdelphij    if (b->next_bio == NULL)
208296465Sdelphij        return (0);
209296465Sdelphij    switch (cmd) {
210296465Sdelphij    case BIO_C_DO_STATE_MACHINE:
211296465Sdelphij        BIO_clear_retry_flags(b);
212296465Sdelphij        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
213296465Sdelphij        BIO_copy_next_retry(b);
214296465Sdelphij        break;
215296465Sdelphij    case BIO_CTRL_DUP:
216296465Sdelphij        ret = 0L;
217296465Sdelphij        break;
218296465Sdelphij    default:
219296465Sdelphij        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
220296465Sdelphij        break;
221296465Sdelphij    }
222296465Sdelphij    return (ret);
223296465Sdelphij}
22455714Skris
22568651Skrisstatic long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
226296465Sdelphij{
227296465Sdelphij    long ret = 1;
22859191Skris
229296465Sdelphij    if (b->next_bio == NULL)
230296465Sdelphij        return (0);
231296465Sdelphij    switch (cmd) {
232296465Sdelphij    default:
233296465Sdelphij        ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
234296465Sdelphij        break;
235296465Sdelphij    }
236296465Sdelphij    return (ret);
237296465Sdelphij}
23859191Skris
23955714Skrisstatic int nbiof_gets(BIO *bp, char *buf, int size)
240296465Sdelphij{
241296465Sdelphij    if (bp->next_bio == NULL)
242296465Sdelphij        return (0);
243296465Sdelphij    return (BIO_gets(bp->next_bio, buf, size));
244296465Sdelphij}
24555714Skris
24668651Skrisstatic int nbiof_puts(BIO *bp, const char *str)
247296465Sdelphij{
248296465Sdelphij    if (bp->next_bio == NULL)
249296465Sdelphij        return (0);
250296465Sdelphij    return (BIO_puts(bp->next_bio, str));
251296465Sdelphij}
252