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