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
20 static int nbiof_write(BIO *h, const char *buf, int num);
21 static int nbiof_read(BIO *h, char *buf, int size);
22 static int nbiof_puts(BIO *h, const char *str);
23 static int nbiof_gets(BIO *h, char *str, int size);
24 static long nbiof_ctrl(BIO *h, int cmd, long arg1, void *arg2);
25 static int nbiof_new(BIO *h);
26 static int nbiof_free(BIO *data);
27 static long nbiof_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
28 typedef struct nbio_test_st {
29 /* only set if we sent a 'should retry' error */
30 int lrn;
31 int lwn;
32 } NBIO_TEST;
33
34 static 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
BIO_f_nbio_test(void)49 const BIO_METHOD *BIO_f_nbio_test(void)
50 {
51 return &methods_nbiof;
52 }
53
nbiof_new(BIO * bi)54 static int nbiof_new(BIO *bi)
55 {
56 NBIO_TEST *nt;
57
58 if ((nt = OPENSSL_zalloc(sizeof(*nt))) == NULL)
59 return 0;
60 nt->lrn = -1;
61 nt->lwn = -1;
62 bi->ptr = (char *)nt;
63 bi->init = 1;
64 return 1;
65 }
66
nbiof_free(BIO * a)67 static int nbiof_free(BIO *a)
68 {
69 if (a == NULL)
70 return 0;
71 OPENSSL_free(a->ptr);
72 a->ptr = NULL;
73 a->init = 0;
74 a->flags = 0;
75 return 1;
76 }
77
nbiof_read(BIO * b,char * out,int outl)78 static int nbiof_read(BIO *b, char *out, int outl)
79 {
80 int ret = 0;
81 int num;
82 unsigned char n;
83
84 if (out == NULL)
85 return 0;
86 if (b->next_bio == NULL)
87 return 0;
88
89 BIO_clear_retry_flags(b);
90 if (RAND_priv_bytes(&n, 1) <= 0)
91 return -1;
92 num = (n & 0x07);
93
94 if (outl > num)
95 outl = num;
96
97 if (num == 0) {
98 ret = -1;
99 BIO_set_retry_read(b);
100 } else {
101 ret = BIO_read(b->next_bio, out, outl);
102 if (ret < 0)
103 BIO_copy_next_retry(b);
104 }
105 return ret;
106 }
107
nbiof_write(BIO * b,const char * in,int inl)108 static int nbiof_write(BIO *b, const char *in, int inl)
109 {
110 NBIO_TEST *nt;
111 int ret = 0;
112 int num;
113 unsigned char n;
114
115 if ((in == NULL) || (inl <= 0))
116 return 0;
117 if (b->next_bio == NULL)
118 return 0;
119 nt = (NBIO_TEST *)b->ptr;
120
121 BIO_clear_retry_flags(b);
122
123 if (nt->lwn > 0) {
124 num = nt->lwn;
125 nt->lwn = 0;
126 } else {
127 if (RAND_priv_bytes(&n, 1) <= 0)
128 return -1;
129 num = (n & 7);
130 }
131
132 if (inl > num)
133 inl = num;
134
135 if (num == 0) {
136 ret = -1;
137 BIO_set_retry_write(b);
138 } else {
139 ret = BIO_write(b->next_bio, in, inl);
140 if (ret < 0) {
141 BIO_copy_next_retry(b);
142 nt->lwn = inl;
143 }
144 }
145 return ret;
146 }
147
nbiof_ctrl(BIO * b,int cmd,long num,void * ptr)148 static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
149 {
150 long ret;
151
152 if (b->next_bio == NULL)
153 return 0;
154 switch (cmd) {
155 case BIO_C_DO_STATE_MACHINE:
156 BIO_clear_retry_flags(b);
157 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
158 BIO_copy_next_retry(b);
159 break;
160 case BIO_CTRL_DUP:
161 ret = 0L;
162 break;
163 default:
164 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
165 break;
166 }
167 return ret;
168 }
169
nbiof_callback_ctrl(BIO * b,int cmd,BIO_info_cb * fp)170 static long nbiof_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
171 {
172 if (b->next_bio == NULL)
173 return 0;
174 return BIO_callback_ctrl(b->next_bio, cmd, fp);
175 }
176
nbiof_gets(BIO * bp,char * buf,int size)177 static int nbiof_gets(BIO *bp, char *buf, int size)
178 {
179 if (bp->next_bio == NULL)
180 return 0;
181 return BIO_gets(bp->next_bio, buf, size);
182 }
183
nbiof_puts(BIO * bp,const char * str)184 static int nbiof_puts(BIO *bp, const char *str)
185 {
186 if (bp->next_bio == NULL)
187 return 0;
188 return BIO_puts(bp->next_bio, str);
189 }
190