/src/openssl31/crypto/bio/bss_sock.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 "internal/ktls.h" |
15 | | |
16 | | #ifndef OPENSSL_NO_SOCK |
17 | | |
18 | | # include <openssl/bio.h> |
19 | | |
20 | | # ifdef WATT32 |
21 | | /* Watt-32 uses same names */ |
22 | | # undef sock_write |
23 | | # undef sock_read |
24 | | # undef sock_puts |
25 | | # define sock_write SockWrite |
26 | | # define sock_read SockRead |
27 | | # define sock_puts SockPuts |
28 | | # endif |
29 | | |
30 | | static int sock_write(BIO *h, const char *buf, int num); |
31 | | static int sock_read(BIO *h, char *buf, int size); |
32 | | static int sock_puts(BIO *h, const char *str); |
33 | | static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
34 | | static int sock_new(BIO *h); |
35 | | static int sock_free(BIO *data); |
36 | | int BIO_sock_should_retry(int s); |
37 | | |
38 | | static const BIO_METHOD methods_sockp = { |
39 | | BIO_TYPE_SOCKET, |
40 | | "socket", |
41 | | bwrite_conv, |
42 | | sock_write, |
43 | | bread_conv, |
44 | | sock_read, |
45 | | sock_puts, |
46 | | NULL, /* sock_gets, */ |
47 | | sock_ctrl, |
48 | | sock_new, |
49 | | sock_free, |
50 | | NULL, /* sock_callback_ctrl */ |
51 | | }; |
52 | | |
53 | | const BIO_METHOD *BIO_s_socket(void) |
54 | 0 | { |
55 | 0 | return &methods_sockp; |
56 | 0 | } |
57 | | |
58 | | BIO *BIO_new_socket(int fd, int close_flag) |
59 | 0 | { |
60 | 0 | BIO *ret; |
61 | |
|
62 | 0 | ret = BIO_new(BIO_s_socket()); |
63 | 0 | if (ret == NULL) |
64 | 0 | return NULL; |
65 | 0 | BIO_set_fd(ret, fd, close_flag); |
66 | | # ifndef OPENSSL_NO_KTLS |
67 | | { |
68 | | /* |
69 | | * The new socket is created successfully regardless of ktls_enable. |
70 | | * ktls_enable doesn't change any functionality of the socket, except |
71 | | * changing the setsockopt to enable the processing of ktls_start. |
72 | | * Thus, it is not a problem to call it for non-TLS sockets. |
73 | | */ |
74 | | ktls_enable(fd); |
75 | | } |
76 | | # endif |
77 | 0 | return ret; |
78 | 0 | } |
79 | | |
80 | | static int sock_new(BIO *bi) |
81 | 0 | { |
82 | 0 | bi->init = 0; |
83 | 0 | bi->num = 0; |
84 | 0 | bi->ptr = NULL; |
85 | 0 | bi->flags = 0; |
86 | 0 | return 1; |
87 | 0 | } |
88 | | |
89 | | static int sock_free(BIO *a) |
90 | 0 | { |
91 | 0 | if (a == NULL) |
92 | 0 | return 0; |
93 | 0 | if (a->shutdown) { |
94 | 0 | if (a->init) { |
95 | 0 | BIO_closesocket(a->num); |
96 | 0 | } |
97 | 0 | a->init = 0; |
98 | 0 | a->flags = 0; |
99 | 0 | } |
100 | 0 | return 1; |
101 | 0 | } |
102 | | |
103 | | static int sock_read(BIO *b, char *out, int outl) |
104 | 0 | { |
105 | 0 | int ret = 0; |
106 | |
|
107 | 0 | if (out != NULL) { |
108 | 0 | clear_socket_error(); |
109 | | # ifndef OPENSSL_NO_KTLS |
110 | | if (BIO_get_ktls_recv(b)) |
111 | | ret = ktls_read_record(b->num, out, outl); |
112 | | else |
113 | | # endif |
114 | 0 | ret = readsocket(b->num, out, outl); |
115 | 0 | BIO_clear_retry_flags(b); |
116 | 0 | if (ret <= 0) { |
117 | 0 | if (BIO_sock_should_retry(ret)) |
118 | 0 | BIO_set_retry_read(b); |
119 | 0 | else if (ret == 0) |
120 | 0 | b->flags |= BIO_FLAGS_IN_EOF; |
121 | 0 | } |
122 | 0 | } |
123 | 0 | return ret; |
124 | 0 | } |
125 | | |
126 | | static int sock_write(BIO *b, const char *in, int inl) |
127 | 0 | { |
128 | 0 | int ret = 0; |
129 | |
|
130 | 0 | clear_socket_error(); |
131 | | # ifndef OPENSSL_NO_KTLS |
132 | | if (BIO_should_ktls_ctrl_msg_flag(b)) { |
133 | | unsigned char record_type = (intptr_t)b->ptr; |
134 | | ret = ktls_send_ctrl_message(b->num, record_type, in, inl); |
135 | | if (ret >= 0) { |
136 | | ret = inl; |
137 | | BIO_clear_ktls_ctrl_msg_flag(b); |
138 | | } |
139 | | } else |
140 | | # endif |
141 | 0 | ret = writesocket(b->num, in, inl); |
142 | 0 | BIO_clear_retry_flags(b); |
143 | 0 | if (ret <= 0) { |
144 | 0 | if (BIO_sock_should_retry(ret)) |
145 | 0 | BIO_set_retry_write(b); |
146 | 0 | } |
147 | 0 | return ret; |
148 | 0 | } |
149 | | |
150 | | static long sock_ctrl(BIO *b, int cmd, long num, void *ptr) |
151 | 0 | { |
152 | 0 | long ret = 1; |
153 | 0 | int *ip; |
154 | | # ifndef OPENSSL_NO_KTLS |
155 | | ktls_crypto_info_t *crypto_info; |
156 | | # endif |
157 | |
|
158 | 0 | switch (cmd) { |
159 | 0 | case BIO_C_SET_FD: |
160 | 0 | sock_free(b); |
161 | 0 | b->num = *((int *)ptr); |
162 | 0 | b->shutdown = (int)num; |
163 | 0 | b->init = 1; |
164 | 0 | break; |
165 | 0 | case BIO_C_GET_FD: |
166 | 0 | if (b->init) { |
167 | 0 | ip = (int *)ptr; |
168 | 0 | if (ip != NULL) |
169 | 0 | *ip = b->num; |
170 | 0 | ret = b->num; |
171 | 0 | } else |
172 | 0 | ret = -1; |
173 | 0 | break; |
174 | 0 | case BIO_CTRL_GET_CLOSE: |
175 | 0 | ret = b->shutdown; |
176 | 0 | break; |
177 | 0 | case BIO_CTRL_SET_CLOSE: |
178 | 0 | b->shutdown = (int)num; |
179 | 0 | break; |
180 | 0 | case BIO_CTRL_DUP: |
181 | 0 | case BIO_CTRL_FLUSH: |
182 | 0 | ret = 1; |
183 | 0 | break; |
184 | | # ifndef OPENSSL_NO_KTLS |
185 | | case BIO_CTRL_SET_KTLS: |
186 | | crypto_info = (ktls_crypto_info_t *)ptr; |
187 | | ret = ktls_start(b->num, crypto_info, num); |
188 | | if (ret) |
189 | | BIO_set_ktls_flag(b, num); |
190 | | break; |
191 | | case BIO_CTRL_GET_KTLS_SEND: |
192 | | return BIO_should_ktls_flag(b, 1) != 0; |
193 | | case BIO_CTRL_GET_KTLS_RECV: |
194 | | return BIO_should_ktls_flag(b, 0) != 0; |
195 | | case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG: |
196 | | BIO_set_ktls_ctrl_msg_flag(b); |
197 | | b->ptr = (void *)num; |
198 | | ret = 0; |
199 | | break; |
200 | | case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG: |
201 | | BIO_clear_ktls_ctrl_msg_flag(b); |
202 | | ret = 0; |
203 | | break; |
204 | | # endif |
205 | 0 | case BIO_CTRL_EOF: |
206 | 0 | ret = (b->flags & BIO_FLAGS_IN_EOF) != 0; |
207 | 0 | break; |
208 | 0 | default: |
209 | 0 | ret = 0; |
210 | 0 | break; |
211 | 0 | } |
212 | 0 | return ret; |
213 | 0 | } |
214 | | |
215 | | static int sock_puts(BIO *bp, const char *str) |
216 | 0 | { |
217 | 0 | int n, ret; |
218 | |
|
219 | 0 | n = strlen(str); |
220 | 0 | ret = sock_write(bp, str, n); |
221 | 0 | return ret; |
222 | 0 | } |
223 | | |
224 | | int BIO_sock_should_retry(int i) |
225 | 0 | { |
226 | 0 | int err; |
227 | |
|
228 | 0 | if ((i == 0) || (i == -1)) { |
229 | 0 | err = get_last_socket_error(); |
230 | |
|
231 | 0 | return BIO_sock_non_fatal_error(err); |
232 | 0 | } |
233 | 0 | return 0; |
234 | 0 | } |
235 | | |
236 | | int BIO_sock_non_fatal_error(int err) |
237 | 0 | { |
238 | 0 | switch (err) { |
239 | | # if defined(OPENSSL_SYS_WINDOWS) |
240 | | # if defined(WSAEWOULDBLOCK) |
241 | | case WSAEWOULDBLOCK: |
242 | | # endif |
243 | | # endif |
244 | | |
245 | 0 | # ifdef EWOULDBLOCK |
246 | | # ifdef WSAEWOULDBLOCK |
247 | | # if WSAEWOULDBLOCK != EWOULDBLOCK |
248 | | case EWOULDBLOCK: |
249 | | # endif |
250 | | # else |
251 | 0 | case EWOULDBLOCK: |
252 | 0 | # endif |
253 | 0 | # endif |
254 | |
|
255 | 0 | # if defined(ENOTCONN) |
256 | 0 | case ENOTCONN: |
257 | 0 | # endif |
258 | |
|
259 | 0 | # ifdef EINTR |
260 | 0 | case EINTR: |
261 | 0 | # endif |
262 | |
|
263 | 0 | # ifdef EAGAIN |
264 | | # if EWOULDBLOCK != EAGAIN |
265 | | case EAGAIN: |
266 | | # endif |
267 | 0 | # endif |
268 | |
|
269 | 0 | # ifdef EPROTO |
270 | 0 | case EPROTO: |
271 | 0 | # endif |
272 | |
|
273 | 0 | # ifdef EINPROGRESS |
274 | 0 | case EINPROGRESS: |
275 | 0 | # endif |
276 | |
|
277 | 0 | # ifdef EALREADY |
278 | 0 | case EALREADY: |
279 | 0 | # endif |
280 | 0 | return 1; |
281 | 0 | default: |
282 | 0 | break; |
283 | 0 | } |
284 | 0 | return 0; |
285 | 0 | } |
286 | | |
287 | | #endif /* #ifndef OPENSSL_NO_SOCK */ |