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