1 | /*
|
---|
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the OpenSSL license (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_lcl.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 | nbiof_write,
|
---|
38 | nbiof_read,
|
---|
39 | nbiof_puts,
|
---|
40 | nbiof_gets,
|
---|
41 | nbiof_ctrl,
|
---|
42 | nbiof_new,
|
---|
43 | nbiof_free,
|
---|
44 | nbiof_callback_ctrl,
|
---|
45 | };
|
---|
46 |
|
---|
47 | const BIO_METHOD *BIO_f_nbio_test(void)
|
---|
48 | {
|
---|
49 | return (&methods_nbiof);
|
---|
50 | }
|
---|
51 |
|
---|
52 | static int nbiof_new(BIO *bi)
|
---|
53 | {
|
---|
54 | NBIO_TEST *nt;
|
---|
55 |
|
---|
56 | if ((nt = OPENSSL_zalloc(sizeof(*nt))) == NULL)
|
---|
57 | return (0);
|
---|
58 | nt->lrn = -1;
|
---|
59 | nt->lwn = -1;
|
---|
60 | bi->ptr = (char *)nt;
|
---|
61 | bi->init = 1;
|
---|
62 | return (1);
|
---|
63 | }
|
---|
64 |
|
---|
65 | static int nbiof_free(BIO *a)
|
---|
66 | {
|
---|
67 | if (a == NULL)
|
---|
68 | return (0);
|
---|
69 | OPENSSL_free(a->ptr);
|
---|
70 | a->ptr = NULL;
|
---|
71 | a->init = 0;
|
---|
72 | a->flags = 0;
|
---|
73 | return (1);
|
---|
74 | }
|
---|
75 |
|
---|
76 | static int nbiof_read(BIO *b, char *out, int outl)
|
---|
77 | {
|
---|
78 | int ret = 0;
|
---|
79 | int num;
|
---|
80 | unsigned char n;
|
---|
81 |
|
---|
82 | if (out == NULL)
|
---|
83 | return (0);
|
---|
84 | if (b->next_bio == NULL)
|
---|
85 | return (0);
|
---|
86 |
|
---|
87 | BIO_clear_retry_flags(b);
|
---|
88 | if (RAND_bytes(&n, 1) <= 0)
|
---|
89 | return -1;
|
---|
90 | num = (n & 0x07);
|
---|
91 |
|
---|
92 | if (outl > num)
|
---|
93 | outl = num;
|
---|
94 |
|
---|
95 | if (num == 0) {
|
---|
96 | ret = -1;
|
---|
97 | BIO_set_retry_read(b);
|
---|
98 | } else {
|
---|
99 | ret = BIO_read(b->next_bio, out, outl);
|
---|
100 | if (ret < 0)
|
---|
101 | BIO_copy_next_retry(b);
|
---|
102 | }
|
---|
103 | return (ret);
|
---|
104 | }
|
---|
105 |
|
---|
106 | static int nbiof_write(BIO *b, const char *in, int inl)
|
---|
107 | {
|
---|
108 | NBIO_TEST *nt;
|
---|
109 | int ret = 0;
|
---|
110 | int num;
|
---|
111 | unsigned char n;
|
---|
112 |
|
---|
113 | if ((in == NULL) || (inl <= 0))
|
---|
114 | return (0);
|
---|
115 | if (b->next_bio == NULL)
|
---|
116 | return (0);
|
---|
117 | nt = (NBIO_TEST *)b->ptr;
|
---|
118 |
|
---|
119 | BIO_clear_retry_flags(b);
|
---|
120 |
|
---|
121 | if (nt->lwn > 0) {
|
---|
122 | num = nt->lwn;
|
---|
123 | nt->lwn = 0;
|
---|
124 | } else {
|
---|
125 | if (RAND_bytes(&n, 1) <= 0)
|
---|
126 | return -1;
|
---|
127 | num = (n & 7);
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (inl > num)
|
---|
131 | inl = num;
|
---|
132 |
|
---|
133 | if (num == 0) {
|
---|
134 | ret = -1;
|
---|
135 | BIO_set_retry_write(b);
|
---|
136 | } else {
|
---|
137 | ret = BIO_write(b->next_bio, in, inl);
|
---|
138 | if (ret < 0) {
|
---|
139 | BIO_copy_next_retry(b);
|
---|
140 | nt->lwn = inl;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | return (ret);
|
---|
144 | }
|
---|
145 |
|
---|
146 | static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
|
---|
147 | {
|
---|
148 | long ret;
|
---|
149 |
|
---|
150 | if (b->next_bio == NULL)
|
---|
151 | return (0);
|
---|
152 | switch (cmd) {
|
---|
153 | case BIO_C_DO_STATE_MACHINE:
|
---|
154 | BIO_clear_retry_flags(b);
|
---|
155 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
|
---|
156 | BIO_copy_next_retry(b);
|
---|
157 | break;
|
---|
158 | case BIO_CTRL_DUP:
|
---|
159 | ret = 0L;
|
---|
160 | break;
|
---|
161 | default:
|
---|
162 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | return (ret);
|
---|
166 | }
|
---|
167 |
|
---|
168 | static long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
|
---|
169 | {
|
---|
170 | long ret = 1;
|
---|
171 |
|
---|
172 | if (b->next_bio == NULL)
|
---|
173 | return (0);
|
---|
174 | switch (cmd) {
|
---|
175 | default:
|
---|
176 | ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
|
---|
177 | break;
|
---|
178 | }
|
---|
179 | return (ret);
|
---|
180 | }
|
---|
181 |
|
---|
182 | static int nbiof_gets(BIO *bp, char *buf, int size)
|
---|
183 | {
|
---|
184 | if (bp->next_bio == NULL)
|
---|
185 | return (0);
|
---|
186 | return (BIO_gets(bp->next_bio, buf, size));
|
---|
187 | }
|
---|
188 |
|
---|
189 | static int nbiof_puts(BIO *bp, const char *str)
|
---|
190 | {
|
---|
191 | if (bp->next_bio == NULL)
|
---|
192 | return (0);
|
---|
193 | return (BIO_puts(bp->next_bio, str));
|
---|
194 | }
|
---|