1/*
2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License").  You may not use
5 * this file except in compliance with the License.  You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#include <stdio.h>
11#include <errno.h>
12#include "bio_local.h"
13#include "internal/cryptlib.h"
14#include <openssl/rand.h>
15
16/*
17 * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
18 */
19
20static int nbiof_write(BIO *h, const char *buf, int num);
21static int nbiof_read(BIO *h, char *buf, int size);
22static int nbiof_puts(BIO *h, const char *str);
23static int nbiof_gets(BIO *h, char *str, int size);
24static long nbiof_ctrl(BIO *h, int cmd, long arg1, void *arg2);
25static int nbiof_new(BIO *h);
26static int nbiof_free(BIO *data);
27static long nbiof_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
28typedef struct nbio_test_st {
29    /* only set if we sent a 'should retry' error */
30    int lrn;
31    int lwn;
32} NBIO_TEST;
33
34static const BIO_METHOD methods_nbiof = {
35    BIO_TYPE_NBIO_TEST,
36    "non-blocking IO test filter",
37    bwrite_conv,
38    nbiof_write,
39    bread_conv,
40    nbiof_read,
41    nbiof_puts,
42    nbiof_gets,
43    nbiof_ctrl,
44    nbiof_new,
45    nbiof_free,
46    nbiof_callback_ctrl,
47};
48
49const BIO_METHOD *BIO_f_nbio_test(void)
50{
51    return &methods_nbiof;
52}
53
54static int nbiof_new(BIO *bi)
55{
56    NBIO_TEST *nt;
57
58    if ((nt = OPENSSL_zalloc(sizeof(*nt))) == NULL) {
59        ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
60        return 0;
61    }
62    nt->lrn = -1;
63    nt->lwn = -1;
64    bi->ptr = (char *)nt;
65    bi->init = 1;
66    return 1;
67}
68
69static int nbiof_free(BIO *a)
70{
71    if (a == NULL)
72        return 0;
73    OPENSSL_free(a->ptr);
74    a->ptr = NULL;
75    a->init = 0;
76    a->flags = 0;
77    return 1;
78}
79
80static int nbiof_read(BIO *b, char *out, int outl)
81{
82    int ret = 0;
83    int num;
84    unsigned char n;
85
86    if (out == NULL)
87        return 0;
88    if (b->next_bio == NULL)
89        return 0;
90
91    BIO_clear_retry_flags(b);
92    if (RAND_priv_bytes(&n, 1) <= 0)
93        return -1;
94    num = (n & 0x07);
95
96    if (outl > num)
97        outl = num;
98
99    if (num == 0) {
100        ret = -1;
101        BIO_set_retry_read(b);
102    } else {
103        ret = BIO_read(b->next_bio, out, outl);
104        if (ret < 0)
105            BIO_copy_next_retry(b);
106    }
107    return ret;
108}
109
110static int nbiof_write(BIO *b, const char *in, int inl)
111{
112    NBIO_TEST *nt;
113    int ret = 0;
114    int num;
115    unsigned char n;
116
117    if ((in == NULL) || (inl <= 0))
118        return 0;
119    if (b->next_bio == NULL)
120        return 0;
121    nt = (NBIO_TEST *)b->ptr;
122
123    BIO_clear_retry_flags(b);
124
125    if (nt->lwn > 0) {
126        num = nt->lwn;
127        nt->lwn = 0;
128    } else {
129        if (RAND_priv_bytes(&n, 1) <= 0)
130            return -1;
131        num = (n & 7);
132    }
133
134    if (inl > num)
135        inl = num;
136
137    if (num == 0) {
138        ret = -1;
139        BIO_set_retry_write(b);
140    } else {
141        ret = BIO_write(b->next_bio, in, inl);
142        if (ret < 0) {
143            BIO_copy_next_retry(b);
144            nt->lwn = inl;
145        }
146    }
147    return ret;
148}
149
150static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
151{
152    long ret;
153
154    if (b->next_bio == NULL)
155        return 0;
156    switch (cmd) {
157    case BIO_C_DO_STATE_MACHINE:
158        BIO_clear_retry_flags(b);
159        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
160        BIO_copy_next_retry(b);
161        break;
162    case BIO_CTRL_DUP:
163        ret = 0L;
164        break;
165    default:
166        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
167        break;
168    }
169    return ret;
170}
171
172static long nbiof_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
173{
174    if (b->next_bio == NULL)
175        return 0;
176    return BIO_callback_ctrl(b->next_bio, cmd, fp);
177}
178
179static int nbiof_gets(BIO *bp, char *buf, int size)
180{
181    if (bp->next_bio == NULL)
182        return 0;
183    return BIO_gets(bp->next_bio, buf, size);
184}
185
186static int nbiof_puts(BIO *bp, const char *str)
187{
188    if (bp->next_bio == NULL)
189        return 0;
190    return BIO_puts(bp->next_bio, str);
191}
192