]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
38fc02a7 | 2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
0f113f3e | 3 | * |
09abbca1 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <errno.h> | |
706457b7 | 12 | #include "bio_local.h" |
b39fc560 | 13 | #include "internal/cryptlib.h" |
ec577822 | 14 | #include <openssl/rand.h> |
d02b48c6 | 15 | |
0f113f3e MC |
16 | /* |
17 | * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest | |
18 | */ | |
d02b48c6 | 19 | |
0f113f3e MC |
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); | |
d02b48c6 RE |
25 | static int nbiof_new(BIO *h); |
26 | static int nbiof_free(BIO *data); | |
fce78bd4 | 27 | static long nbiof_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp); |
0f113f3e MC |
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 | ||
04f6b0fd | 34 | static const BIO_METHOD methods_nbiof = { |
0f113f3e MC |
35 | BIO_TYPE_NBIO_TEST, |
36 | "non-blocking IO test filter", | |
3befffa3 | 37 | bwrite_conv, |
0f113f3e | 38 | nbiof_write, |
d07aee2c | 39 | bread_conv, |
0f113f3e MC |
40 | nbiof_read, |
41 | nbiof_puts, | |
42 | nbiof_gets, | |
43 | nbiof_ctrl, | |
44 | nbiof_new, | |
45 | nbiof_free, | |
46 | nbiof_callback_ctrl, | |
47 | }; | |
d02b48c6 | 48 | |
04f6b0fd | 49 | const BIO_METHOD *BIO_f_nbio_test(void) |
0f113f3e | 50 | { |
26a7d938 | 51 | return &methods_nbiof; |
0f113f3e | 52 | } |
d02b48c6 | 53 | |
6b691a5c | 54 | static int nbiof_new(BIO *bi) |
0f113f3e MC |
55 | { |
56 | NBIO_TEST *nt; | |
57 | ||
e077455e | 58 | if ((nt = OPENSSL_zalloc(sizeof(*nt))) == NULL) |
26a7d938 | 59 | return 0; |
0f113f3e MC |
60 | nt->lrn = -1; |
61 | nt->lwn = -1; | |
62 | bi->ptr = (char *)nt; | |
63 | bi->init = 1; | |
208fb891 | 64 | return 1; |
0f113f3e | 65 | } |
d02b48c6 | 66 | |
6b691a5c | 67 | static int nbiof_free(BIO *a) |
0f113f3e MC |
68 | { |
69 | if (a == NULL) | |
26a7d938 | 70 | return 0; |
b548a1f1 | 71 | OPENSSL_free(a->ptr); |
0f113f3e MC |
72 | a->ptr = NULL; |
73 | a->init = 0; | |
74 | a->flags = 0; | |
208fb891 | 75 | return 1; |
0f113f3e MC |
76 | } |
77 | ||
6b691a5c | 78 | static int nbiof_read(BIO *b, char *out, int outl) |
0f113f3e MC |
79 | { |
80 | int ret = 0; | |
0f113f3e MC |
81 | int num; |
82 | unsigned char n; | |
d02b48c6 | 83 | |
0f113f3e | 84 | if (out == NULL) |
26a7d938 | 85 | return 0; |
0f113f3e | 86 | if (b->next_bio == NULL) |
26a7d938 | 87 | return 0; |
d02b48c6 | 88 | |
0f113f3e | 89 | BIO_clear_retry_flags(b); |
4cffafe9 | 90 | if (RAND_priv_bytes(&n, 1) <= 0) |
266483d2 | 91 | return -1; |
0f113f3e | 92 | num = (n & 0x07); |
d02b48c6 | 93 | |
0f113f3e MC |
94 | if (outl > num) |
95 | outl = num; | |
d02b48c6 | 96 | |
0f113f3e MC |
97 | if (num == 0) { |
98 | ret = -1; | |
99 | BIO_set_retry_read(b); | |
6f91b017 | 100 | } else { |
0f113f3e MC |
101 | ret = BIO_read(b->next_bio, out, outl); |
102 | if (ret < 0) | |
103 | BIO_copy_next_retry(b); | |
104 | } | |
26a7d938 | 105 | return ret; |
0f113f3e | 106 | } |
d02b48c6 | 107 | |
0e1c0612 | 108 | static int nbiof_write(BIO *b, const char *in, int inl) |
0f113f3e MC |
109 | { |
110 | NBIO_TEST *nt; | |
111 | int ret = 0; | |
112 | int num; | |
113 | unsigned char n; | |
d02b48c6 | 114 | |
0f113f3e | 115 | if ((in == NULL) || (inl <= 0)) |
26a7d938 | 116 | return 0; |
0f113f3e | 117 | if (b->next_bio == NULL) |
26a7d938 | 118 | return 0; |
0f113f3e | 119 | nt = (NBIO_TEST *)b->ptr; |
d02b48c6 | 120 | |
0f113f3e | 121 | BIO_clear_retry_flags(b); |
d02b48c6 | 122 | |
0f113f3e MC |
123 | if (nt->lwn > 0) { |
124 | num = nt->lwn; | |
125 | nt->lwn = 0; | |
126 | } else { | |
4cffafe9 | 127 | if (RAND_priv_bytes(&n, 1) <= 0) |
266483d2 | 128 | return -1; |
0f113f3e MC |
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); | |
6f91b017 | 138 | } else { |
0f113f3e MC |
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 | } | |
26a7d938 | 145 | return ret; |
0f113f3e | 146 | } |
d02b48c6 | 147 | |
0e1c0612 | 148 | static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr) |
0f113f3e MC |
149 | { |
150 | long ret; | |
151 | ||
152 | if (b->next_bio == NULL) | |
26a7d938 | 153 | return 0; |
0f113f3e MC |
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 | } | |
26a7d938 | 167 | return ret; |
0f113f3e | 168 | } |
d02b48c6 | 169 | |
fce78bd4 | 170 | static long nbiof_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
0f113f3e | 171 | { |
0f113f3e | 172 | if (b->next_bio == NULL) |
26a7d938 | 173 | return 0; |
8f2fe32d | 174 | return BIO_callback_ctrl(b->next_bio, cmd, fp); |
0f113f3e | 175 | } |
d3442bc7 | 176 | |
6b691a5c | 177 | static int nbiof_gets(BIO *bp, char *buf, int size) |
0f113f3e MC |
178 | { |
179 | if (bp->next_bio == NULL) | |
26a7d938 K |
180 | return 0; |
181 | return BIO_gets(bp->next_bio, buf, size); | |
0f113f3e | 182 | } |
d02b48c6 | 183 | |
0e1c0612 | 184 | static int nbiof_puts(BIO *bp, const char *str) |
0f113f3e MC |
185 | { |
186 | if (bp->next_bio == NULL) | |
26a7d938 K |
187 | return 0; |
188 | return BIO_puts(bp->next_bio, str); | |
0f113f3e | 189 | } |