summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bio
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
committercvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
commiteb8dd9dca1228af0cd132f515509051ecfabf6f6 (patch)
treeedb6da6af7e865d488dc1a29309f1e1ec226e603 /src/lib/libcrypto/bio
parent247f0352e0ed72a4f476db9dc91f4d982bc83eb2 (diff)
downloadopenbsd-tb_20250414.tar.gz
openbsd-tb_20250414.tar.bz2
openbsd-tb_20250414.zip
This commit was manufactured by cvs2git to create tag 'tb_20250414'.tb_20250414
Diffstat (limited to 'src/lib/libcrypto/bio')
-rw-r--r--src/lib/libcrypto/bio/b_dump.c211
-rw-r--r--src/lib/libcrypto/bio/b_posix.c93
-rw-r--r--src/lib/libcrypto/bio/b_print.c62
-rw-r--r--src/lib/libcrypto/bio/b_sock.c261
-rw-r--r--src/lib/libcrypto/bio/bf_buff.c523
-rw-r--r--src/lib/libcrypto/bio/bf_nbio.c255
-rw-r--r--src/lib/libcrypto/bio/bf_null.c199
-rw-r--r--src/lib/libcrypto/bio/bio.h717
-rw-r--r--src/lib/libcrypto/bio/bio_cb.c156
-rw-r--r--src/lib/libcrypto/bio/bio_err.c122
-rw-r--r--src/lib/libcrypto/bio/bio_lib.c886
-rw-r--r--src/lib/libcrypto/bio/bio_local.h127
-rw-r--r--src/lib/libcrypto/bio/bio_meth.c165
-rw-r--r--src/lib/libcrypto/bio/bss_acpt.c456
-rw-r--r--src/lib/libcrypto/bio/bss_bio.c641
-rw-r--r--src/lib/libcrypto/bio/bss_conn.c598
-rw-r--r--src/lib/libcrypto/bio/bss_dgram.c663
-rw-r--r--src/lib/libcrypto/bio/bss_fd.c273
-rw-r--r--src/lib/libcrypto/bio/bss_file.c325
-rw-r--r--src/lib/libcrypto/bio/bss_log.c216
-rw-r--r--src/lib/libcrypto/bio/bss_mem.c367
-rw-r--r--src/lib/libcrypto/bio/bss_null.c161
-rw-r--r--src/lib/libcrypto/bio/bss_sock.c244
23 files changed, 0 insertions, 7721 deletions
diff --git a/src/lib/libcrypto/bio/b_dump.c b/src/lib/libcrypto/bio/b_dump.c
deleted file mode 100644
index 4dcf710bbe..0000000000
--- a/src/lib/libcrypto/bio/b_dump.c
+++ /dev/null
@@ -1,211 +0,0 @@
1/* $OpenBSD: b_dump.c,v 1.30 2024/03/02 09:21:24 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <limits.h>
60#include <stdint.h>
61#include <stdio.h>
62#include <string.h>
63
64#include <openssl/bio.h>
65#include <openssl/err.h>
66
67#include "bytestring.h"
68
69#define MAX_BYTES_PER_LINE 16
70
71/*
72 * The byte string s is dumped as lines of the following form:
73 * indent | byte count (4 digits) | " - " | hex dump | " " | ASCII dump
74 * Each byte uses 4 characters (two hex digits followed by a space and one
75 * ASCII character).
76 */
77
78int
79BIO_dump_indent(BIO *bio, const char *s, int len, int indent)
80{
81 CBB cbb;
82 CBS cbs;
83 int bytes_per_line, dumped, printed, trailing, written;
84 int ret = -1;
85
86 memset(&cbb, 0, sizeof(cbb));
87
88 if (len < 0)
89 goto err;
90 CBS_init(&cbs, s, len);
91
92 if (indent < 0)
93 indent = 0;
94 if (indent > 64)
95 indent = 64;
96
97 /*
98 * Less obfuscated version of the original calculation attempting to
99 * ensure that the dump doesn't overshoot 80 characters per line. For
100 * a very long string the byte count will still make it go past that.
101 */
102 bytes_per_line = MAX_BYTES_PER_LINE;
103 if (indent > 6)
104 bytes_per_line -= (indent - 3) / 4;
105 if (bytes_per_line <= 0)
106 goto err;
107
108 /* Strip and count trailing spaces and NULs. */
109 trailing = 0;
110 while (CBS_len(&cbs) > 0) {
111 uint8_t u8;
112
113 if (!CBS_peek_last_u8(&cbs, &u8))
114 goto err;
115 if (u8 != '\0' && u8 != ' ')
116 break;
117 if (!CBS_get_last_u8(&cbs, &u8))
118 goto err;
119 trailing++;
120 }
121
122 printed = 0;
123 dumped = 0;
124 while (CBS_len(&cbs) > 0) {
125 CBS row;
126 uint8_t ascii_dump[MAX_BYTES_PER_LINE];
127 int missing, row_bytes;
128
129 if ((row_bytes = CBS_len(&cbs)) > bytes_per_line)
130 row_bytes = bytes_per_line;
131 if (!CBS_get_bytes(&cbs, &row, row_bytes))
132 goto err;
133
134 /* Write out indent, byte count and initial " - ". */
135 if ((written = BIO_printf(bio, "%*s%04x - ", indent, "",
136 dumped)) < 0)
137 goto err;
138 if (printed > INT_MAX - written)
139 goto err;
140 printed += written;
141
142 /*
143 * Write out hex dump, prepare ASCII dump.
144 */
145
146 if (!CBB_init_fixed(&cbb, ascii_dump, sizeof(ascii_dump)))
147 goto err;
148 while (CBS_len(&row) > 0) {
149 uint8_t u8;
150 char sep = ' ';
151
152 if (!CBS_get_u8(&row, &u8))
153 goto err;
154
155 /* Historic behavior: print a '-' after eighth byte. */
156 if (row_bytes - CBS_len(&row) == 8)
157 sep = '-';
158 if ((written = BIO_printf(bio, "%02x%c", u8, sep)) < 0)
159 goto err;
160 if (printed > INT_MAX - written)
161 goto err;
162 printed += written;
163
164 /* Locale-independent version of !isprint(u8). */
165 if (u8 < ' ' || u8 > '~')
166 u8 = '.';
167 if (!CBB_add_u8(&cbb, u8))
168 goto err;
169 }
170 if (!CBB_finish(&cbb, NULL, NULL))
171 goto err;
172
173 /* Calculate number of bytes missing in dump of last line. */
174 if ((missing = bytes_per_line - row_bytes) < 0)
175 goto err;
176
177 /* Pad missing bytes, add 2 spaces and print the ASCII dump. */
178 if ((written = BIO_printf(bio, "%*s%.*s\n", 3 * missing + 2, "",
179 row_bytes, ascii_dump)) < 0)
180 goto err;
181 if (printed > INT_MAX - written)
182 goto err;
183 printed += written;
184
185 dumped += row_bytes;
186 }
187
188 if (trailing > 0) {
189 if ((written = BIO_printf(bio, "%*s%04x - <SPACES/NULS>\n",
190 indent, "", dumped + trailing)) < 0)
191 goto err;
192 if (printed > INT_MAX - written)
193 goto err;
194 printed += written;
195 }
196
197 ret = printed;
198
199 err:
200 CBB_cleanup(&cbb);
201
202 return ret;
203}
204LCRYPTO_ALIAS(BIO_dump_indent);
205
206int
207BIO_dump(BIO *bio, const char *s, int len)
208{
209 return BIO_dump_indent(bio, s, len, 0);
210}
211LCRYPTO_ALIAS(BIO_dump);
diff --git a/src/lib/libcrypto/bio/b_posix.c b/src/lib/libcrypto/bio/b_posix.c
deleted file mode 100644
index d78f25a1f7..0000000000
--- a/src/lib/libcrypto/bio/b_posix.c
+++ /dev/null
@@ -1,93 +0,0 @@
1/* $OpenBSD: b_posix.c,v 1.3 2023/07/05 21:23:37 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/*
60 * Functions that need to be overridden by non-POSIX operating systems.
61 */
62
63#include <fcntl.h>
64#include <unistd.h>
65
66#include <openssl/bio.h>
67
68int
69BIO_sock_init(void)
70{
71 if (!OPENSSL_init_crypto(0, NULL)) /* XXX do we need this? */
72 return (0);
73 return (1);
74}
75LCRYPTO_ALIAS(BIO_sock_init);
76
77void
78BIO_sock_cleanup(void)
79{
80}
81LCRYPTO_ALIAS(BIO_sock_cleanup);
82
83int
84BIO_socket_nbio(int s, int mode)
85{
86 int flags = fcntl(s, F_GETFD);
87 if (mode && !(flags & O_NONBLOCK))
88 return (fcntl(s, F_SETFL, flags | O_NONBLOCK) != -1);
89 else if (!mode && (flags & O_NONBLOCK))
90 return (fcntl(s, F_SETFL, flags & ~O_NONBLOCK) != -1);
91 return (1);
92}
93LCRYPTO_ALIAS(BIO_socket_nbio);
diff --git a/src/lib/libcrypto/bio/b_print.c b/src/lib/libcrypto/bio/b_print.c
deleted file mode 100644
index f6943ea3f3..0000000000
--- a/src/lib/libcrypto/bio/b_print.c
+++ /dev/null
@@ -1,62 +0,0 @@
1/* $OpenBSD: b_print.c,v 1.28 2024/03/02 09:18:28 tb Exp $ */
2
3/* Theo de Raadt places this file in the public domain. */
4
5#include <openssl/bio.h>
6
7#include "bio_local.h"
8
9#ifdef HAVE_FUNOPEN
10static int
11_BIO_write(void *cookie, const char *buf, int nbytes)
12{
13 return BIO_write(cookie, buf, nbytes);
14}
15
16int
17BIO_vprintf(BIO *bio, const char *format, va_list args)
18{
19 int ret;
20 FILE *fp;
21
22 fp = funopen(bio, NULL, &_BIO_write, NULL, NULL);
23 if (fp == NULL) {
24 ret = -1;
25 goto fail;
26 }
27 ret = vfprintf(fp, format, args);
28 fclose(fp);
29fail:
30 return (ret);
31}
32
33#else /* !HAVE_FUNOPEN */
34
35int
36BIO_vprintf(BIO *bio, const char *format, va_list args)
37{
38 int ret;
39 char *buf = NULL;
40
41 ret = vasprintf(&buf, format, args);
42 if (ret == -1)
43 return (ret);
44 BIO_write(bio, buf, ret);
45 free(buf);
46 return (ret);
47}
48
49#endif /* HAVE_FUNOPEN */
50
51int
52BIO_printf(BIO *bio, const char *format, ...)
53{
54 va_list args;
55 int ret;
56
57 va_start(args, format);
58 ret = BIO_vprintf(bio, format, args);
59 va_end(args);
60 return (ret);
61}
62LCRYPTO_ALIAS(BIO_printf);
diff --git a/src/lib/libcrypto/bio/b_sock.c b/src/lib/libcrypto/bio/b_sock.c
deleted file mode 100644
index 00bbe9c37e..0000000000
--- a/src/lib/libcrypto/bio/b_sock.c
+++ /dev/null
@@ -1,261 +0,0 @@
1/* $OpenBSD: b_sock.c,v 1.71 2023/07/05 21:23:37 beck Exp $ */
2/*
3 * Copyright (c) 2017 Bob Beck <beck@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/ioctl.h>
19#include <sys/socket.h>
20#include <string.h>
21
22#include <arpa/inet.h>
23#include <netinet/in.h>
24#include <netinet/tcp.h>
25
26#include <errno.h>
27#include <limits.h>
28#include <netdb.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32
33#include <openssl/bio.h>
34#include <openssl/buffer.h>
35#include <openssl/err.h>
36
37int
38BIO_get_host_ip(const char *str, unsigned char *ip)
39{
40 struct addrinfo *res = NULL;
41 struct addrinfo hints = {
42 .ai_family = AF_INET,
43 .ai_socktype = SOCK_STREAM,
44 .ai_flags = AI_PASSIVE,
45 };
46 uint32_t *iap = (in_addr_t *)ip;
47 int error;
48
49 if (str == NULL) {
50 BIOerror(BIO_R_BAD_HOSTNAME_LOOKUP);
51 ERR_asprintf_error_data("NULL host provided");
52 return (0);
53 }
54
55 if ((error = getaddrinfo(str, NULL, &hints, &res)) != 0) {
56 BIOerror(BIO_R_BAD_HOSTNAME_LOOKUP);
57 ERR_asprintf_error_data("getaddrinfo: host='%s' : %s'", str,
58 gai_strerror(error));
59 return (0);
60 }
61 *iap = (uint32_t)(((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr);
62 freeaddrinfo(res);
63 return (1);
64}
65LCRYPTO_ALIAS(BIO_get_host_ip);
66
67int
68BIO_get_port(const char *str, unsigned short *port_ptr)
69{
70 struct addrinfo *res = NULL;
71 struct addrinfo hints = {
72 .ai_family = AF_UNSPEC,
73 .ai_socktype = SOCK_STREAM,
74 .ai_flags = AI_PASSIVE,
75 };
76 int error;
77
78 if (str == NULL) {
79 BIOerror(BIO_R_NO_PORT_SPECIFIED);
80 return (0);
81 }
82
83 if ((error = getaddrinfo(NULL, str, &hints, &res)) != 0) {
84 BIOerror(BIO_R_INVALID_ARGUMENT);
85 ERR_asprintf_error_data("getaddrinfo: service='%s' : %s'", str,
86 gai_strerror(error));
87 return (0);
88 }
89 *port_ptr = ntohs(((struct sockaddr_in *)(res->ai_addr))->sin_port);
90 freeaddrinfo(res);
91 return (1);
92}
93LCRYPTO_ALIAS(BIO_get_port);
94
95int
96BIO_sock_error(int sock)
97{
98 socklen_t len;
99 int err;
100
101 len = sizeof(err);
102 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &len) != 0)
103 return (1);
104 return (err);
105}
106LCRYPTO_ALIAS(BIO_sock_error);
107
108struct hostent *
109BIO_gethostbyname(const char *name)
110{
111 return gethostbyname(name);
112}
113LCRYPTO_ALIAS(BIO_gethostbyname);
114
115int
116BIO_socket_ioctl(int fd, long type, void *arg)
117{
118 int ret;
119
120 ret = ioctl(fd, type, arg);
121 if (ret < 0)
122 SYSerror(errno);
123 return (ret);
124}
125LCRYPTO_ALIAS(BIO_socket_ioctl);
126
127int
128BIO_get_accept_socket(char *host, int bind_mode)
129{
130 struct addrinfo hints = {
131 .ai_family = AF_INET,
132 .ai_socktype = SOCK_STREAM,
133 .ai_flags = AI_PASSIVE,
134 };
135 struct addrinfo *res = NULL;
136 char *h, *p, *str = NULL;
137 int error, ret = 0, s = -1;
138
139 if (host == NULL) {
140 BIOerror(BIO_R_NO_PORT_SPECIFIED);
141 return (-1);
142 }
143 if ((str = strdup(host)) == NULL) {
144 BIOerror(ERR_R_MALLOC_FAILURE);
145 return (-1);
146 }
147 p = NULL;
148 h = str;
149 if ((p = strrchr(str, ':')) == NULL) {
150 /* A string without a colon is treated as a port. */
151 p = str;
152 h = NULL;
153 } else {
154 *p++ = '\0';
155 if (*p == '\0') {
156 BIOerror(BIO_R_NO_PORT_SPECIFIED);
157 goto err;
158 }
159 if (*h == '\0' || strcmp(h, "*") == 0)
160 h = NULL;
161 }
162
163 if ((error = getaddrinfo(h, p, &hints, &res)) != 0) {
164 BIOerror(BIO_R_BAD_HOSTNAME_LOOKUP);
165 ERR_asprintf_error_data("getaddrinfo: '%s:%s': %s'", h, p,
166 gai_strerror(error));
167 goto err;
168 }
169 if (h == NULL) {
170 struct sockaddr_in *sin = (struct sockaddr_in *)res->ai_addr;
171 sin->sin_addr.s_addr = INADDR_ANY;
172 }
173
174 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
175 if (s == -1) {
176 SYSerror(errno);
177 ERR_asprintf_error_data("host='%s'", host);
178 BIOerror(BIO_R_UNABLE_TO_CREATE_SOCKET);
179 goto err;
180 }
181 if (bind_mode == BIO_BIND_REUSEADDR) {
182 int i = 1;
183
184 ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
185 bind_mode = BIO_BIND_NORMAL;
186 }
187 if (bind(s, res->ai_addr, res->ai_addrlen) == -1) {
188 SYSerror(errno);
189 ERR_asprintf_error_data("host='%s'", host);
190 BIOerror(BIO_R_UNABLE_TO_BIND_SOCKET);
191 goto err;
192 }
193 if (listen(s, SOMAXCONN) == -1) {
194 SYSerror(errno);
195 ERR_asprintf_error_data("host='%s'", host);
196 BIOerror(BIO_R_UNABLE_TO_LISTEN_SOCKET);
197 goto err;
198 }
199 ret = 1;
200
201err:
202 free(str);
203 if (res != NULL)
204 freeaddrinfo(res);
205 if ((ret == 0) && (s != -1)) {
206 close(s);
207 s = -1;
208 }
209 return (s);
210}
211LCRYPTO_ALIAS(BIO_get_accept_socket);
212
213int
214BIO_accept(int sock, char **addr)
215{
216 char h[NI_MAXHOST], s[NI_MAXSERV];
217 struct sockaddr_in sin;
218 socklen_t sin_len = sizeof(sin);
219 int ret = -1;
220
221 if (addr == NULL) {
222 BIOerror(BIO_R_NULL_PARAMETER);
223 goto end;
224 }
225 ret = accept(sock, (struct sockaddr *)&sin, &sin_len);
226 if (ret == -1) {
227 if (BIO_sock_should_retry(ret))
228 return -2;
229 SYSerror(errno);
230 BIOerror(BIO_R_ACCEPT_ERROR);
231 goto end;
232 }
233 /* XXX Crazy API. Can't be helped */
234 if (*addr != NULL) {
235 free(*addr);
236 *addr = NULL;
237 }
238
239 if (sin.sin_family != AF_INET)
240 goto end;
241
242 if (getnameinfo((struct sockaddr *)&sin, sin_len, h, sizeof(h),
243 s, sizeof(s), NI_NUMERICHOST|NI_NUMERICSERV) != 0)
244 goto end;
245
246 if ((asprintf(addr, "%s:%s", h, s)) == -1) {
247 BIOerror(ERR_R_MALLOC_FAILURE);
248 *addr = NULL;
249 goto end;
250 }
251end:
252 return (ret);
253}
254LCRYPTO_ALIAS(BIO_accept);
255
256int
257BIO_set_tcp_ndelay(int s, int on)
258{
259 return (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == 0);
260}
261LCRYPTO_ALIAS(BIO_set_tcp_ndelay);
diff --git a/src/lib/libcrypto/bio/bf_buff.c b/src/lib/libcrypto/bio/bf_buff.c
deleted file mode 100644
index 226c16835a..0000000000
--- a/src/lib/libcrypto/bio/bf_buff.c
+++ /dev/null
@@ -1,523 +0,0 @@
1/* $OpenBSD: bf_buff.c,v 1.28 2023/07/05 21:23:37 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <errno.h>
60#include <stdio.h>
61#include <string.h>
62
63#include <openssl/bio.h>
64#include <openssl/err.h>
65
66#include "bio_local.h"
67
68static int buffer_write(BIO *h, const char *buf, int num);
69static int buffer_read(BIO *h, char *buf, int size);
70static int buffer_puts(BIO *h, const char *str);
71static int buffer_gets(BIO *h, char *str, int size);
72static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
73static int buffer_new(BIO *h);
74static int buffer_free(BIO *data);
75static long buffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
76#define DEFAULT_BUFFER_SIZE 4096
77
78static const BIO_METHOD methods_buffer = {
79 .type = BIO_TYPE_BUFFER,
80 .name = "buffer",
81 .bwrite = buffer_write,
82 .bread = buffer_read,
83 .bputs = buffer_puts,
84 .bgets = buffer_gets,
85 .ctrl = buffer_ctrl,
86 .create = buffer_new,
87 .destroy = buffer_free,
88 .callback_ctrl = buffer_callback_ctrl
89};
90
91const BIO_METHOD *
92BIO_f_buffer(void)
93{
94 return (&methods_buffer);
95}
96LCRYPTO_ALIAS(BIO_f_buffer);
97
98static int
99buffer_new(BIO *bi)
100{
101 BIO_F_BUFFER_CTX *ctx;
102
103 ctx = malloc(sizeof(BIO_F_BUFFER_CTX));
104 if (ctx == NULL)
105 return (0);
106 ctx->ibuf = malloc(DEFAULT_BUFFER_SIZE);
107 if (ctx->ibuf == NULL) {
108 free(ctx);
109 return (0);
110 }
111 ctx->obuf = malloc(DEFAULT_BUFFER_SIZE);
112 if (ctx->obuf == NULL) {
113 free(ctx->ibuf);
114 free(ctx);
115 return (0);
116 }
117 ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
118 ctx->obuf_size = DEFAULT_BUFFER_SIZE;
119 ctx->ibuf_len = 0;
120 ctx->ibuf_off = 0;
121 ctx->obuf_len = 0;
122 ctx->obuf_off = 0;
123
124 bi->init = 1;
125 bi->ptr = (char *)ctx;
126 bi->flags = 0;
127 return (1);
128}
129
130static int
131buffer_free(BIO *a)
132{
133 BIO_F_BUFFER_CTX *b;
134
135 if (a == NULL)
136 return (0);
137 b = (BIO_F_BUFFER_CTX *)a->ptr;
138 free(b->ibuf);
139 free(b->obuf);
140 free(a->ptr);
141 a->ptr = NULL;
142 a->init = 0;
143 a->flags = 0;
144 return (1);
145}
146
147static int
148buffer_read(BIO *b, char *out, int outl)
149{
150 int i, num = 0;
151 BIO_F_BUFFER_CTX *ctx;
152
153 if (out == NULL)
154 return (0);
155 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
156
157 if ((ctx == NULL) || (b->next_bio == NULL))
158 return (0);
159 num = 0;
160 BIO_clear_retry_flags(b);
161
162start:
163 i = ctx->ibuf_len;
164 /* If there is stuff left over, grab it */
165 if (i != 0) {
166 if (i > outl)
167 i = outl;
168 memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
169 ctx->ibuf_off += i;
170 ctx->ibuf_len -= i;
171 num += i;
172 if (outl == i)
173 return (num);
174 outl -= i;
175 out += i;
176 }
177
178 /* We may have done a partial read. try to do more.
179 * We have nothing in the buffer.
180 * If we get an error and have read some data, just return it
181 * and let them retry to get the error again.
182 * copy direct to parent address space */
183 if (outl > ctx->ibuf_size) {
184 for (;;) {
185 i = BIO_read(b->next_bio, out, outl);
186 if (i <= 0) {
187 BIO_copy_next_retry(b);
188 if (i < 0)
189 return ((num > 0) ? num : i);
190 if (i == 0)
191 return (num);
192 }
193 num += i;
194 if (outl == i)
195 return (num);
196 out += i;
197 outl -= i;
198 }
199 }
200 /* else */
201
202 /* we are going to be doing some buffering */
203 i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
204 if (i <= 0) {
205 BIO_copy_next_retry(b);
206 if (i < 0)
207 return ((num > 0) ? num : i);
208 if (i == 0)
209 return (num);
210 }
211 ctx->ibuf_off = 0;
212 ctx->ibuf_len = i;
213
214 /* Lets re-read using ourselves :-) */
215 goto start;
216}
217
218static int
219buffer_write(BIO *b, const char *in, int inl)
220{
221 int i, num = 0;
222 BIO_F_BUFFER_CTX *ctx;
223
224 if ((in == NULL) || (inl <= 0))
225 return (0);
226 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
227 if ((ctx == NULL) || (b->next_bio == NULL))
228 return (0);
229
230 BIO_clear_retry_flags(b);
231start:
232 i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
233 /* add to buffer and return */
234 if (i >= inl) {
235 memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
236 ctx->obuf_len += inl;
237 return (num + inl);
238 }
239 /* else */
240 /* stuff already in buffer, so add to it first, then flush */
241 if (ctx->obuf_len != 0) {
242 if (i > 0) /* lets fill it up if we can */
243 {
244 memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
245 in += i;
246 inl -= i;
247 num += i;
248 ctx->obuf_len += i;
249 }
250 /* we now have a full buffer needing flushing */
251 for (;;) {
252 i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
253 ctx->obuf_len);
254 if (i <= 0) {
255 BIO_copy_next_retry(b);
256
257 if (i < 0)
258 return ((num > 0) ? num : i);
259 if (i == 0)
260 return (num);
261 }
262 ctx->obuf_off += i;
263 ctx->obuf_len -= i;
264 if (ctx->obuf_len == 0)
265 break;
266 }
267 }
268 /* we only get here if the buffer has been flushed and we
269 * still have stuff to write */
270 ctx->obuf_off = 0;
271
272 /* we now have inl bytes to write */
273 while (inl >= ctx->obuf_size) {
274 i = BIO_write(b->next_bio, in, inl);
275 if (i <= 0) {
276 BIO_copy_next_retry(b);
277 if (i < 0)
278 return ((num > 0) ? num : i);
279 if (i == 0)
280 return (num);
281 }
282 num += i;
283 in += i;
284 inl -= i;
285 if (inl == 0)
286 return (num);
287 }
288
289 /* copy the rest into the buffer since we have only a small
290 * amount left */
291 goto start;
292}
293
294static long
295buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
296{
297 BIO *dbio;
298 BIO_F_BUFFER_CTX *ctx;
299 long ret = 1;
300 char *p1, *p2;
301 int r, i, *ip;
302 int ibs, obs;
303
304 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
305
306 switch (cmd) {
307 case BIO_CTRL_RESET:
308 ctx->ibuf_off = 0;
309 ctx->ibuf_len = 0;
310 ctx->obuf_off = 0;
311 ctx->obuf_len = 0;
312 if (b->next_bio == NULL)
313 return (0);
314 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
315 break;
316 case BIO_CTRL_INFO:
317 ret = (long)ctx->obuf_len;
318 break;
319 case BIO_C_GET_BUFF_NUM_LINES:
320 ret = 0;
321 p1 = ctx->ibuf;
322 for (i = 0; i < ctx->ibuf_len; i++) {
323 if (p1[ctx->ibuf_off + i] == '\n')
324 ret++;
325 }
326 break;
327 case BIO_CTRL_WPENDING:
328 ret = (long)ctx->obuf_len;
329 if (ret == 0) {
330 if (b->next_bio == NULL)
331 return (0);
332 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
333 }
334 break;
335 case BIO_CTRL_PENDING:
336 ret = (long)ctx->ibuf_len;
337 if (ret == 0) {
338 if (b->next_bio == NULL)
339 return (0);
340 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
341 }
342 break;
343 case BIO_C_SET_BUFF_READ_DATA:
344 if (num > ctx->ibuf_size) {
345 p1 = malloc(num);
346 if (p1 == NULL)
347 goto malloc_error;
348 free(ctx->ibuf);
349 ctx->ibuf = p1;
350 }
351 ctx->ibuf_off = 0;
352 ctx->ibuf_len = (int)num;
353 memcpy(ctx->ibuf, ptr, num);
354 ret = 1;
355 break;
356 case BIO_C_SET_BUFF_SIZE:
357 if (ptr != NULL) {
358 ip = (int *)ptr;
359 if (*ip == 0) {
360 ibs = (int)num;
361 obs = ctx->obuf_size;
362 }
363 else /* if (*ip == 1) */
364 {
365 ibs = ctx->ibuf_size;
366 obs = (int)num;
367 }
368 } else {
369 ibs = (int)num;
370 obs = (int)num;
371 }
372 p1 = ctx->ibuf;
373 p2 = ctx->obuf;
374 if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
375 p1 = malloc(num);
376 if (p1 == NULL)
377 goto malloc_error;
378 }
379 if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
380 p2 = malloc(num);
381 if (p2 == NULL) {
382 if (p1 != ctx->ibuf)
383 free(p1);
384 goto malloc_error;
385 }
386 }
387 if (ctx->ibuf != p1) {
388 free(ctx->ibuf);
389 ctx->ibuf = p1;
390 ctx->ibuf_off = 0;
391 ctx->ibuf_len = 0;
392 ctx->ibuf_size = ibs;
393 }
394 if (ctx->obuf != p2) {
395 free(ctx->obuf);
396 ctx->obuf = p2;
397 ctx->obuf_off = 0;
398 ctx->obuf_len = 0;
399 ctx->obuf_size = obs;
400 }
401 break;
402 case BIO_C_DO_STATE_MACHINE:
403 if (b->next_bio == NULL)
404 return (0);
405 BIO_clear_retry_flags(b);
406 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
407 BIO_copy_next_retry(b);
408 break;
409
410 case BIO_CTRL_FLUSH:
411 if (b->next_bio == NULL)
412 return (0);
413 if (ctx->obuf_len <= 0) {
414 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
415 break;
416 }
417
418 for (;;) {
419 BIO_clear_retry_flags(b);
420 if (ctx->obuf_len > 0) {
421 r = BIO_write(b->next_bio,
422 &(ctx->obuf[ctx->obuf_off]),
423 ctx->obuf_len);
424 BIO_copy_next_retry(b);
425 if (r <= 0)
426 return ((long)r);
427 ctx->obuf_off += r;
428 ctx->obuf_len -= r;
429 } else {
430 ctx->obuf_len = 0;
431 ctx->obuf_off = 0;
432 break;
433 }
434 }
435 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
436 break;
437 case BIO_CTRL_DUP:
438 dbio = (BIO *)ptr;
439 if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
440 !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
441 ret = 0;
442 break;
443 default:
444 if (b->next_bio == NULL)
445 return (0);
446 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
447 break;
448 }
449 return (ret);
450malloc_error:
451 BIOerror(ERR_R_MALLOC_FAILURE);
452 return (0);
453}
454
455static long
456buffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
457{
458 long ret = 1;
459
460 if (b->next_bio == NULL)
461 return (0);
462 switch (cmd) {
463 default:
464 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
465 break;
466 }
467 return (ret);
468}
469
470static int
471buffer_gets(BIO *b, char *buf, int size)
472{
473 BIO_F_BUFFER_CTX *ctx;
474 int num = 0, i, flag;
475 char *p;
476
477 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
478 size--; /* reserve space for a '\0' */
479 BIO_clear_retry_flags(b);
480
481 for (;;) {
482 if (ctx->ibuf_len > 0) {
483 p = &(ctx->ibuf[ctx->ibuf_off]);
484 flag = 0;
485 for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
486 *(buf++) = p[i];
487 if (p[i] == '\n') {
488 flag = 1;
489 i++;
490 break;
491 }
492 }
493 num += i;
494 size -= i;
495 ctx->ibuf_len -= i;
496 ctx->ibuf_off += i;
497 if (flag || size == 0) {
498 *buf = '\0';
499 return (num);
500 }
501 }
502 else /* read another chunk */
503 {
504 i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
505 if (i <= 0) {
506 BIO_copy_next_retry(b);
507 *buf = '\0';
508 if (i < 0)
509 return ((num > 0) ? num : i);
510 if (i == 0)
511 return (num);
512 }
513 ctx->ibuf_len = i;
514 ctx->ibuf_off = 0;
515 }
516 }
517}
518
519static int
520buffer_puts(BIO *b, const char *str)
521{
522 return (buffer_write(b, str, strlen(str)));
523}
diff --git a/src/lib/libcrypto/bio/bf_nbio.c b/src/lib/libcrypto/bio/bf_nbio.c
deleted file mode 100644
index 2aed3b8fb6..0000000000
--- a/src/lib/libcrypto/bio/bf_nbio.c
+++ /dev/null
@@ -1,255 +0,0 @@
1/* $OpenBSD: bf_nbio.c,v 1.23 2023/07/05 21:23:37 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <errno.h>
60#include <stdio.h>
61#include <stdlib.h>
62
63#include <openssl/bio.h>
64
65#include "bio_local.h"
66
67/* BIO_put and BIO_get both add to the digest,
68 * BIO_gets returns the digest */
69
70static int nbiof_write(BIO *h, const char *buf, int num);
71static int nbiof_read(BIO *h, char *buf, int size);
72static int nbiof_puts(BIO *h, const char *str);
73static int nbiof_gets(BIO *h, char *str, int size);
74static long nbiof_ctrl(BIO *h, int cmd, long arg1, void *arg2);
75static int nbiof_new(BIO *h);
76static int nbiof_free(BIO *data);
77static long nbiof_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
78
79typedef struct nbio_test_st {
80 /* only set if we sent a 'should retry' error */
81 int lrn;
82 int lwn;
83} NBIO_TEST;
84
85static const BIO_METHOD methods_nbiof = {
86 .type = BIO_TYPE_NBIO_TEST,
87 .name = "non-blocking IO test filter",
88 .bwrite = nbiof_write,
89 .bread = nbiof_read,
90 .bputs = nbiof_puts,
91 .bgets = nbiof_gets,
92 .ctrl = nbiof_ctrl,
93 .create = nbiof_new,
94 .destroy = nbiof_free,
95 .callback_ctrl = nbiof_callback_ctrl
96};
97
98const BIO_METHOD *
99BIO_f_nbio_test(void)
100{
101 return (&methods_nbiof);
102}
103LCRYPTO_ALIAS(BIO_f_nbio_test);
104
105static int
106nbiof_new(BIO *bi)
107{
108 NBIO_TEST *nt;
109
110 if (!(nt = malloc(sizeof(NBIO_TEST))))
111 return (0);
112 nt->lrn = -1;
113 nt->lwn = -1;
114 bi->ptr = (char *)nt;
115 bi->init = 1;
116 bi->flags = 0;
117 return (1);
118}
119
120static int
121nbiof_free(BIO *a)
122{
123 if (a == NULL)
124 return (0);
125 free(a->ptr);
126 a->ptr = NULL;
127 a->init = 0;
128 a->flags = 0;
129 return (1);
130}
131
132static int
133nbiof_read(BIO *b, char *out, int outl)
134{
135 int ret = 0;
136 int num;
137 unsigned char n;
138
139 if (out == NULL)
140 return (0);
141 if (b->next_bio == NULL)
142 return (0);
143
144 BIO_clear_retry_flags(b);
145
146 arc4random_buf(&n, 1);
147 num = (n & 0x07);
148
149 if (outl > num)
150 outl = num;
151
152 if (num == 0) {
153 ret = -1;
154 BIO_set_retry_read(b);
155 } else {
156 ret = BIO_read(b->next_bio, out, outl);
157 if (ret < 0)
158 BIO_copy_next_retry(b);
159 }
160 return (ret);
161}
162
163static int
164nbiof_write(BIO *b, const char *in, int inl)
165{
166 NBIO_TEST *nt;
167 int ret = 0;
168 int num;
169 unsigned char n;
170
171 if ((in == NULL) || (inl <= 0))
172 return (0);
173 if (b->next_bio == NULL)
174 return (0);
175 nt = (NBIO_TEST *)b->ptr;
176
177 BIO_clear_retry_flags(b);
178
179 if (nt->lwn > 0) {
180 num = nt->lwn;
181 nt->lwn = 0;
182 } else {
183 arc4random_buf(&n, 1);
184 num = (n&7);
185 }
186
187 if (inl > num)
188 inl = num;
189
190 if (num == 0) {
191 ret = -1;
192 BIO_set_retry_write(b);
193 } else {
194 ret = BIO_write(b->next_bio, in, inl);
195 if (ret < 0) {
196 BIO_copy_next_retry(b);
197 nt->lwn = inl;
198 }
199 }
200 return (ret);
201}
202
203static long
204nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
205{
206 long ret;
207
208 if (b->next_bio == NULL)
209 return (0);
210 switch (cmd) {
211 case BIO_C_DO_STATE_MACHINE:
212 BIO_clear_retry_flags(b);
213 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
214 BIO_copy_next_retry(b);
215 break;
216 case BIO_CTRL_DUP:
217 ret = 0L;
218 break;
219 default:
220 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
221 break;
222 }
223 return (ret);
224}
225
226static long
227nbiof_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
228{
229 long ret = 1;
230
231 if (b->next_bio == NULL)
232 return (0);
233 switch (cmd) {
234 default:
235 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
236 break;
237 }
238 return (ret);
239}
240
241static int
242nbiof_gets(BIO *bp, char *buf, int size)
243{
244 if (bp->next_bio == NULL)
245 return (0);
246 return (BIO_gets(bp->next_bio, buf, size));
247}
248
249static int
250nbiof_puts(BIO *bp, const char *str)
251{
252 if (bp->next_bio == NULL)
253 return (0);
254 return (BIO_puts(bp->next_bio, str));
255}
diff --git a/src/lib/libcrypto/bio/bf_null.c b/src/lib/libcrypto/bio/bf_null.c
deleted file mode 100644
index 055daeb7a9..0000000000
--- a/src/lib/libcrypto/bio/bf_null.c
+++ /dev/null
@@ -1,199 +0,0 @@
1/* $OpenBSD: bf_null.c,v 1.15 2023/07/05 21:23:37 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <errno.h>
60#include <stdio.h>
61
62#include <openssl/bio.h>
63
64#include "bio_local.h"
65
66/* BIO_put and BIO_get both add to the digest,
67 * BIO_gets returns the digest */
68
69static int nullf_write(BIO *h, const char *buf, int num);
70static int nullf_read(BIO *h, char *buf, int size);
71static int nullf_puts(BIO *h, const char *str);
72static int nullf_gets(BIO *h, char *str, int size);
73static long nullf_ctrl(BIO *h, int cmd, long arg1, void *arg2);
74static int nullf_new(BIO *h);
75static int nullf_free(BIO *data);
76static long nullf_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
77
78static const BIO_METHOD methods_nullf = {
79 .type = BIO_TYPE_NULL_FILTER,
80 .name = "NULL filter",
81 .bwrite = nullf_write,
82 .bread = nullf_read,
83 .bputs = nullf_puts,
84 .bgets = nullf_gets,
85 .ctrl = nullf_ctrl,
86 .create = nullf_new,
87 .destroy = nullf_free,
88 .callback_ctrl = nullf_callback_ctrl
89};
90
91const BIO_METHOD *
92BIO_f_null(void)
93{
94 return (&methods_nullf);
95}
96LCRYPTO_ALIAS(BIO_f_null);
97
98static int
99nullf_new(BIO *bi)
100{
101 bi->init = 1;
102 bi->ptr = NULL;
103 bi->flags = 0;
104 return (1);
105}
106
107static int
108nullf_free(BIO *a)
109{
110 if (a == NULL)
111 return (0);
112/* a->ptr=NULL;
113 a->init=0;
114 a->flags=0;*/
115 return (1);
116}
117
118static int
119nullf_read(BIO *b, char *out, int outl)
120{
121 int ret = 0;
122
123 if (out == NULL)
124 return (0);
125 if (b->next_bio == NULL)
126 return (0);
127 ret = BIO_read(b->next_bio, out, outl);
128 BIO_clear_retry_flags(b);
129 BIO_copy_next_retry(b);
130 return (ret);
131}
132
133static int
134nullf_write(BIO *b, const char *in, int inl)
135{
136 int ret = 0;
137
138 if ((in == NULL) || (inl <= 0))
139 return (0);
140 if (b->next_bio == NULL)
141 return (0);
142 ret = BIO_write(b->next_bio, in, inl);
143 BIO_clear_retry_flags(b);
144 BIO_copy_next_retry(b);
145 return (ret);
146}
147
148static long
149nullf_ctrl(BIO *b, int cmd, long num, void *ptr)
150{
151 long ret;
152
153 if (b->next_bio == NULL)
154 return (0);
155 switch (cmd) {
156 case BIO_C_DO_STATE_MACHINE:
157 BIO_clear_retry_flags(b);
158 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
159 BIO_copy_next_retry(b);
160 break;
161 case BIO_CTRL_DUP:
162 ret = 0L;
163 break;
164 default:
165 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
166 }
167 return (ret);
168}
169
170static long
171nullf_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
172{
173 long ret = 1;
174
175 if (b->next_bio == NULL)
176 return (0);
177 switch (cmd) {
178 default:
179 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
180 break;
181 }
182 return (ret);
183}
184
185static int
186nullf_gets(BIO *bp, char *buf, int size)
187{
188 if (bp->next_bio == NULL)
189 return (0);
190 return (BIO_gets(bp->next_bio, buf, size));
191}
192
193static int
194nullf_puts(BIO *bp, const char *str)
195{
196 if (bp->next_bio == NULL)
197 return (0);
198 return (BIO_puts(bp->next_bio, str));
199}
diff --git a/src/lib/libcrypto/bio/bio.h b/src/lib/libcrypto/bio/bio.h
deleted file mode 100644
index 8327ffc071..0000000000
--- a/src/lib/libcrypto/bio/bio.h
+++ /dev/null
@@ -1,717 +0,0 @@
1/* $OpenBSD: bio.h,v 1.64 2024/05/19 07:12:50 jsg Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_BIO_H
60#define HEADER_BIO_H
61#if !defined(HAVE_ATTRIBUTE__BOUNDED__) && !defined(__OpenBSD__)
62#define __bounded__(x, y, z)
63#endif
64#include <openssl/opensslconf.h>
65
66# include <stdio.h>
67#include <stdarg.h>
68
69#include <openssl/crypto.h>
70
71
72#ifdef __cplusplus
73extern "C" {
74#endif
75
76/* These are the 'types' of BIOs */
77#define BIO_TYPE_NONE 0
78#define BIO_TYPE_MEM (1|0x0400)
79#define BIO_TYPE_FILE (2|0x0400)
80
81#define BIO_TYPE_FD (4|0x0400|0x0100)
82#define BIO_TYPE_SOCKET (5|0x0400|0x0100)
83#define BIO_TYPE_NULL (6|0x0400)
84#define BIO_TYPE_SSL (7|0x0200)
85#define BIO_TYPE_MD (8|0x0200) /* passive filter */
86#define BIO_TYPE_BUFFER (9|0x0200) /* filter */
87#define BIO_TYPE_CIPHER (10|0x0200) /* filter */
88#define BIO_TYPE_BASE64 (11|0x0200) /* filter */
89#define BIO_TYPE_CONNECT (12|0x0400|0x0100) /* socket - connect */
90#define BIO_TYPE_ACCEPT (13|0x0400|0x0100) /* socket for accept */
91#define BIO_TYPE_PROXY_CLIENT (14|0x0200) /* client proxy BIO */
92#define BIO_TYPE_PROXY_SERVER (15|0x0200) /* server proxy BIO */
93#define BIO_TYPE_NBIO_TEST (16|0x0200) /* server proxy BIO */
94#define BIO_TYPE_NULL_FILTER (17|0x0200)
95#define BIO_TYPE_BER (18|0x0200) /* BER -> bin filter */
96#define BIO_TYPE_BIO (19|0x0400) /* (half a) BIO pair */
97#define BIO_TYPE_LINEBUFFER (20|0x0200) /* filter */
98#define BIO_TYPE_DGRAM (21|0x0400|0x0100)
99#define BIO_TYPE_ASN1 (22|0x0200) /* filter */
100#define BIO_TYPE_COMP (23|0x0200) /* filter */
101
102#define BIO_TYPE_DESCRIPTOR 0x0100 /* socket, fd, connect or accept */
103#define BIO_TYPE_FILTER 0x0200
104#define BIO_TYPE_SOURCE_SINK 0x0400
105
106/*
107 * BIO_TYPE_START is the first user-allocated BIO type. No pre-defined type,
108 * flag bits aside, may exceed this value.
109 */
110#define BIO_TYPE_START 128
111
112/* BIO_FILENAME_READ|BIO_CLOSE to open or close on free.
113 * BIO_set_fp(in,stdin,BIO_NOCLOSE); */
114#define BIO_NOCLOSE 0x00
115#define BIO_CLOSE 0x01
116
117/* These are used in the following macros and are passed to
118 * BIO_ctrl() */
119#define BIO_CTRL_RESET 1 /* opt - rewind/zero etc */
120#define BIO_CTRL_EOF 2 /* opt - are we at the eof */
121#define BIO_CTRL_INFO 3 /* opt - extra tit-bits */
122#define BIO_CTRL_SET 4 /* man - set the 'IO' type */
123#define BIO_CTRL_GET 5 /* man - get the 'IO' type */
124#define BIO_CTRL_PUSH 6 /* opt - internal, used to signify change */
125#define BIO_CTRL_POP 7 /* opt - internal, used to signify change */
126#define BIO_CTRL_GET_CLOSE 8 /* man - set the 'close' on free */
127#define BIO_CTRL_SET_CLOSE 9 /* man - set the 'close' on free */
128#define BIO_CTRL_PENDING 10 /* opt - is their more data buffered */
129#define BIO_CTRL_FLUSH 11 /* opt - 'flush' buffered output */
130#define BIO_CTRL_DUP 12 /* man - extra stuff for 'duped' BIO */
131#define BIO_CTRL_WPENDING 13 /* opt - number of bytes still to write */
132/* callback is int cb(BIO *bio,state,ret); */
133#define BIO_CTRL_SET_CALLBACK 14 /* opt - set callback function */
134#define BIO_CTRL_GET_CALLBACK 15 /* opt - set callback function */
135
136#define BIO_CTRL_SET_FILENAME 30 /* BIO_s_file special */
137
138/* dgram BIO stuff */
139#define BIO_CTRL_DGRAM_CONNECT 31 /* BIO dgram special */
140#define BIO_CTRL_DGRAM_SET_CONNECTED 32 /* allow for an externally
141 * connected socket to be
142 * passed in */
143#define BIO_CTRL_DGRAM_SET_RECV_TIMEOUT 33 /* setsockopt, essentially */
144#define BIO_CTRL_DGRAM_GET_RECV_TIMEOUT 34 /* getsockopt, essentially */
145#define BIO_CTRL_DGRAM_SET_SEND_TIMEOUT 35 /* setsockopt, essentially */
146#define BIO_CTRL_DGRAM_GET_SEND_TIMEOUT 36 /* getsockopt, essentially */
147
148#define BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP 37 /* flag whether the last */
149#define BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP 38 /* I/O operation tiemd out */
150
151/* #ifdef IP_MTU_DISCOVER */
152#define BIO_CTRL_DGRAM_MTU_DISCOVER 39 /* set DF bit on egress packets */
153/* #endif */
154
155#define BIO_CTRL_DGRAM_QUERY_MTU 40 /* as kernel for current MTU */
156#define BIO_CTRL_DGRAM_GET_FALLBACK_MTU 47
157#define BIO_CTRL_DGRAM_GET_MTU 41 /* get cached value for MTU */
158#define BIO_CTRL_DGRAM_SET_MTU 42 /* set cached value for
159 * MTU. want to use this
160 * if asking the kernel
161 * fails */
162
163#define BIO_CTRL_DGRAM_MTU_EXCEEDED 43 /* check whether the MTU
164 * was exceed in the
165 * previous write
166 * operation */
167
168#define BIO_CTRL_DGRAM_GET_PEER 46
169#define BIO_CTRL_DGRAM_SET_PEER 44 /* Destination for the data */
170
171#define BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT 45 /* Next DTLS handshake timeout to
172 * adjust socket timeouts */
173
174
175/* modifiers */
176#define BIO_FP_READ 0x02
177#define BIO_FP_WRITE 0x04
178#define BIO_FP_APPEND 0x08
179#define BIO_FP_TEXT 0x10
180
181#define BIO_FLAGS_READ 0x01
182#define BIO_FLAGS_WRITE 0x02
183#define BIO_FLAGS_IO_SPECIAL 0x04
184#define BIO_FLAGS_RWS (BIO_FLAGS_READ|BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL)
185#define BIO_FLAGS_SHOULD_RETRY 0x08
186
187/* Used in BIO_gethostbyname() */
188#define BIO_GHBN_CTRL_HITS 1
189#define BIO_GHBN_CTRL_MISSES 2
190#define BIO_GHBN_CTRL_CACHE_SIZE 3
191#define BIO_GHBN_CTRL_GET_ENTRY 4
192#define BIO_GHBN_CTRL_FLUSH 5
193
194/* Mostly used in the SSL BIO */
195/* Not used anymore
196 * #define BIO_FLAGS_PROTOCOL_DELAYED_READ 0x10
197 * #define BIO_FLAGS_PROTOCOL_DELAYED_WRITE 0x20
198 * #define BIO_FLAGS_PROTOCOL_STARTUP 0x40
199 */
200
201#define BIO_FLAGS_BASE64_NO_NL 0x100
202
203/* This is used with memory BIOs: it means we shouldn't free up or change the
204 * data in any way.
205 */
206#define BIO_FLAGS_MEM_RDONLY 0x200
207
208void BIO_set_flags(BIO *b, int flags);
209int BIO_test_flags(const BIO *b, int flags);
210void BIO_clear_flags(BIO *b, int flags);
211
212#define BIO_get_flags(b) BIO_test_flags(b, ~(0x0))
213#define BIO_set_retry_special(b) \
214 BIO_set_flags(b, (BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY))
215#define BIO_set_retry_read(b) \
216 BIO_set_flags(b, (BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY))
217#define BIO_set_retry_write(b) \
218 BIO_set_flags(b, (BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY))
219
220/* These are normally used internally in BIOs */
221#define BIO_clear_retry_flags(b) \
222 BIO_clear_flags(b, (BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY))
223#define BIO_get_retry_flags(b) \
224 BIO_test_flags(b, (BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY))
225
226/* These should be used by the application to tell why we should retry */
227#define BIO_should_read(a) BIO_test_flags(a, BIO_FLAGS_READ)
228#define BIO_should_write(a) BIO_test_flags(a, BIO_FLAGS_WRITE)
229#define BIO_should_io_special(a) BIO_test_flags(a, BIO_FLAGS_IO_SPECIAL)
230#define BIO_retry_type(a) BIO_test_flags(a, BIO_FLAGS_RWS)
231#define BIO_should_retry(a) BIO_test_flags(a, BIO_FLAGS_SHOULD_RETRY)
232
233/* The next three are used in conjunction with the
234 * BIO_should_io_special() condition. After this returns true,
235 * BIO *BIO_get_retry_BIO(BIO *bio, int *reason); will walk the BIO
236 * stack and return the 'reason' for the special and the offending BIO.
237 * Given a BIO, BIO_get_retry_reason(bio) will return the code. */
238/* Returned from the SSL bio when the certificate retrieval code had an error */
239#define BIO_RR_SSL_X509_LOOKUP 0x01
240/* Returned from the connect BIO when a connect would have blocked */
241#define BIO_RR_CONNECT 0x02
242/* Returned from the accept BIO when an accept would have blocked */
243#define BIO_RR_ACCEPT 0x03
244
245/* These are passed by the BIO callback */
246#define BIO_CB_FREE 0x01
247#define BIO_CB_READ 0x02
248#define BIO_CB_WRITE 0x03
249#define BIO_CB_PUTS 0x04
250#define BIO_CB_GETS 0x05
251#define BIO_CB_CTRL 0x06
252
253/*
254 * The callback is called before and after the underling operation,
255 * the BIO_CB_RETURN flag indicates if it is after the call.
256 */
257#define BIO_CB_RETURN 0x80
258#define BIO_CB_return(a) ((a)|BIO_CB_RETURN))
259#define BIO_cb_pre(a) (!((a)&BIO_CB_RETURN))
260#define BIO_cb_post(a) ((a)&BIO_CB_RETURN)
261
262typedef long (*BIO_callback_fn)(BIO *b, int oper, const char *argp, int argi,
263 long argl, long ret);
264typedef long (*BIO_callback_fn_ex)(BIO *b, int oper, const char *argp,
265 size_t len, int argi, long argl, int ret, size_t *processed);
266
267BIO_callback_fn BIO_get_callback(const BIO *b);
268void BIO_set_callback(BIO *b, BIO_callback_fn callback);
269
270BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b);
271void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex callback);
272
273char *BIO_get_callback_arg(const BIO *b);
274void BIO_set_callback_arg(BIO *b, char *arg);
275
276const char *BIO_method_name(const BIO *b);
277int BIO_method_type(const BIO *b);
278
279typedef int BIO_info_cb(BIO *, int, int);
280/* Compatibility with OpenSSL's backward compatibility. */
281typedef BIO_info_cb bio_info_cb;
282
283typedef struct bio_method_st BIO_METHOD;
284
285DECLARE_STACK_OF(BIO)
286
287/* Prefix and suffix callback in ASN1 BIO */
288typedef int asn1_ps_func(BIO *b, unsigned char **pbuf, int *plen, void *parg);
289
290/* BIO_METHOD accessors */
291BIO_METHOD *BIO_meth_new(int type, const char *name);
292void BIO_meth_free(BIO_METHOD *biom);
293int (*BIO_meth_get_write(const BIO_METHOD *biom))(BIO *, const char *, int);
294int BIO_meth_set_write(BIO_METHOD *biom,
295 int (*write)(BIO *, const char *, int));
296int (*BIO_meth_get_read(const BIO_METHOD *biom))(BIO *, char *, int);
297int BIO_meth_set_read(BIO_METHOD *biom, int (*read)(BIO *, char *, int));
298int (*BIO_meth_get_puts(const BIO_METHOD *biom))(BIO *, const char *);
299int BIO_meth_set_puts(BIO_METHOD *biom, int (*puts)(BIO *, const char *));
300int (*BIO_meth_get_gets(const BIO_METHOD *biom))(BIO *, char *, int);
301int BIO_meth_set_gets(BIO_METHOD *biom, int (*gets)(BIO *, char *, int));
302long (*BIO_meth_get_ctrl(const BIO_METHOD *biom))(BIO *, int, long, void *);
303int BIO_meth_set_ctrl(BIO_METHOD *biom, long (*ctrl)(BIO *, int, long, void *));
304int (*BIO_meth_get_create(const BIO_METHOD *biom))(BIO *);
305int BIO_meth_set_create(BIO_METHOD *biom, int (*create)(BIO *));
306int (*BIO_meth_get_destroy(const BIO_METHOD *biom))(BIO *);
307int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy)(BIO *));
308long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *biom))(BIO *, int, BIO_info_cb *);
309int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
310 long (*callback_ctrl)(BIO *, int, BIO_info_cb *));
311
312/* connect BIO stuff */
313#define BIO_CONN_S_BEFORE 1
314#define BIO_CONN_S_GET_IP 2
315#define BIO_CONN_S_GET_PORT 3
316#define BIO_CONN_S_CREATE_SOCKET 4
317#define BIO_CONN_S_CONNECT 5
318#define BIO_CONN_S_OK 6
319#define BIO_CONN_S_BLOCKED_CONNECT 7
320#define BIO_CONN_S_NBIO 8
321/*#define BIO_CONN_get_param_hostname BIO_ctrl */
322
323#define BIO_C_SET_CONNECT 100
324#define BIO_C_DO_STATE_MACHINE 101
325#define BIO_C_SET_NBIO 102
326#define BIO_C_SET_PROXY_PARAM 103
327#define BIO_C_SET_FD 104
328#define BIO_C_GET_FD 105
329#define BIO_C_SET_FILE_PTR 106
330#define BIO_C_GET_FILE_PTR 107
331#define BIO_C_SET_FILENAME 108
332#define BIO_C_SET_SSL 109
333#define BIO_C_GET_SSL 110
334#define BIO_C_SET_MD 111
335#define BIO_C_GET_MD 112
336#define BIO_C_GET_CIPHER_STATUS 113
337#define BIO_C_SET_BUF_MEM 114
338#define BIO_C_GET_BUF_MEM_PTR 115
339#define BIO_C_GET_BUFF_NUM_LINES 116
340#define BIO_C_SET_BUFF_SIZE 117
341#define BIO_C_SET_ACCEPT 118
342#define BIO_C_SSL_MODE 119
343#define BIO_C_GET_MD_CTX 120
344#define BIO_C_GET_PROXY_PARAM 121
345#define BIO_C_SET_BUFF_READ_DATA 122 /* data to read first */
346#define BIO_C_GET_CONNECT 123
347#define BIO_C_GET_ACCEPT 124
348#define BIO_C_SET_SSL_RENEGOTIATE_BYTES 125
349#define BIO_C_GET_SSL_NUM_RENEGOTIATES 126
350#define BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT 127
351#define BIO_C_FILE_SEEK 128
352#define BIO_C_GET_CIPHER_CTX 129
353#define BIO_C_SET_BUF_MEM_EOF_RETURN 130/*return end of input value*/
354#define BIO_C_SET_BIND_MODE 131
355#define BIO_C_GET_BIND_MODE 132
356#define BIO_C_FILE_TELL 133
357#define BIO_C_GET_SOCKS 134
358#define BIO_C_SET_SOCKS 135
359
360#define BIO_C_SET_WRITE_BUF_SIZE 136/* for BIO_s_bio */
361#define BIO_C_GET_WRITE_BUF_SIZE 137
362#define BIO_C_MAKE_BIO_PAIR 138
363#define BIO_C_DESTROY_BIO_PAIR 139
364#define BIO_C_GET_WRITE_GUARANTEE 140
365#define BIO_C_GET_READ_REQUEST 141
366#define BIO_C_SHUTDOWN_WR 142
367#define BIO_C_RESET_READ_REQUEST 147
368#define BIO_C_SET_MD_CTX 148
369
370#define BIO_C_SET_EX_ARG 153
371#define BIO_C_GET_EX_ARG 154
372
373#define BIO_set_app_data(s,arg) BIO_set_ex_data(s,0,arg)
374#define BIO_get_app_data(s) BIO_get_ex_data(s,0)
375
376/* BIO_s_connect() and BIO_s_socks4a_connect() */
377#define BIO_set_conn_hostname(b,name) BIO_ctrl(b,BIO_C_SET_CONNECT,0,(char *)name)
378#define BIO_set_conn_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,1,(char *)port)
379#define BIO_set_conn_ip(b,ip) BIO_ctrl(b,BIO_C_SET_CONNECT,2,(char *)ip)
380#define BIO_set_conn_int_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,3,(char *)port)
381#define BIO_get_conn_hostname(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,0)
382#define BIO_get_conn_port(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,1)
383#define BIO_get_conn_ip(b) BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,2)
384#define BIO_get_conn_int_port(b) BIO_int_ctrl(b,BIO_C_GET_CONNECT,3,0)
385
386
387#define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL)
388
389/* BIO_s_accept_socket() */
390#define BIO_set_accept_port(b,name) BIO_ctrl(b,BIO_C_SET_ACCEPT,0,(char *)name)
391#define BIO_get_accept_port(b) BIO_ptr_ctrl(b,BIO_C_GET_ACCEPT,0)
392/* #define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL) */
393#define BIO_set_nbio_accept(b,n) BIO_ctrl(b,BIO_C_SET_ACCEPT,1,(n)?(void *)"a":NULL)
394#define BIO_set_accept_bios(b,bio) BIO_ctrl(b,BIO_C_SET_ACCEPT,2,(char *)bio)
395
396#define BIO_BIND_NORMAL 0
397#define BIO_BIND_REUSEADDR_IF_UNUSED 1
398#define BIO_BIND_REUSEADDR 2
399#define BIO_set_bind_mode(b,mode) BIO_ctrl(b,BIO_C_SET_BIND_MODE,mode,NULL)
400#define BIO_get_bind_mode(b,mode) BIO_ctrl(b,BIO_C_GET_BIND_MODE,0,NULL)
401
402#define BIO_do_connect(b) BIO_do_handshake(b)
403#define BIO_do_accept(b) BIO_do_handshake(b)
404#define BIO_do_handshake(b) BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL)
405
406/* BIO_s_proxy_client() */
407#define BIO_set_url(b,url) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,0,(char *)(url))
408#define BIO_set_proxies(b,p) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,1,(char *)(p))
409/* BIO_set_nbio(b,n) */
410#define BIO_set_filter_bio(b,s) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,2,(char *)(s))
411/* BIO *BIO_get_filter_bio(BIO *bio); */
412#define BIO_set_proxy_cb(b,cb) BIO_callback_ctrl(b,BIO_C_SET_PROXY_PARAM,3,(void *(*cb)()))
413#define BIO_set_proxy_header(b,sk) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,4,(char *)sk)
414#define BIO_set_no_connect_return(b,bool) BIO_int_ctrl(b,BIO_C_SET_PROXY_PARAM,5,bool)
415
416#define BIO_get_proxy_header(b,skp) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,0,(char *)skp)
417#define BIO_get_proxies(b,pxy_p) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,1,(char *)(pxy_p))
418#define BIO_get_url(b,url) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,2,(char *)(url))
419#define BIO_get_no_connect_return(b) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,5,NULL)
420
421#define BIO_set_fd(b,fd,c) BIO_int_ctrl(b,BIO_C_SET_FD,c,fd)
422#define BIO_get_fd(b,c) BIO_ctrl(b,BIO_C_GET_FD,0,(char *)c)
423
424#define BIO_set_fp(b,fp,c) BIO_ctrl(b,BIO_C_SET_FILE_PTR,c,(char *)fp)
425#define BIO_get_fp(b,fpp) BIO_ctrl(b,BIO_C_GET_FILE_PTR,0,(char *)fpp)
426
427#define BIO_seek(b,ofs) (int)BIO_ctrl(b,BIO_C_FILE_SEEK,ofs,NULL)
428#define BIO_tell(b) (int)BIO_ctrl(b,BIO_C_FILE_TELL,0,NULL)
429
430/* name is cast to lose const, but might be better to route through a function
431 so we can do it safely */
432#define BIO_read_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
433 BIO_CLOSE|BIO_FP_READ,(char *)name)
434#define BIO_write_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
435 BIO_CLOSE|BIO_FP_WRITE,name)
436#define BIO_append_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
437 BIO_CLOSE|BIO_FP_APPEND,name)
438#define BIO_rw_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
439 BIO_CLOSE|BIO_FP_READ|BIO_FP_WRITE,name)
440
441/* WARNING WARNING, this ups the reference count on the read bio of the
442 * SSL structure. This is because the ssl read BIO is now pointed to by
443 * the next_bio field in the bio. So when you free the BIO, make sure
444 * you are doing a BIO_free_all() to catch the underlying BIO. */
445#define BIO_set_ssl(b,ssl,c) BIO_ctrl(b,BIO_C_SET_SSL,c,(char *)ssl)
446#define BIO_get_ssl(b,sslp) BIO_ctrl(b,BIO_C_GET_SSL,0,(char *)sslp)
447#define BIO_set_ssl_mode(b,client) BIO_ctrl(b,BIO_C_SSL_MODE,client,NULL)
448#define BIO_set_ssl_renegotiate_bytes(b,num) \
449 BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL)
450#define BIO_get_num_renegotiates(b) \
451 BIO_ctrl(b,BIO_C_GET_SSL_NUM_RENEGOTIATES,0,NULL)
452#define BIO_set_ssl_renegotiate_timeout(b,seconds) \
453 BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL)
454
455/* defined in evp.h */
456/* #define BIO_set_md(b,md) BIO_ctrl(b,BIO_C_SET_MD,1,(char *)md) */
457
458#define BIO_get_mem_data(b,pp) BIO_ctrl(b,BIO_CTRL_INFO,0,(char *)pp)
459#define BIO_set_mem_buf(b,bm,c) BIO_ctrl(b,BIO_C_SET_BUF_MEM,c,(char *)bm)
460#define BIO_get_mem_ptr(b,pp) BIO_ctrl(b,BIO_C_GET_BUF_MEM_PTR,0,(char *)pp)
461#define BIO_set_mem_eof_return(b,v) \
462 BIO_ctrl(b,BIO_C_SET_BUF_MEM_EOF_RETURN,v,NULL)
463
464/* For the BIO_f_buffer() type */
465#define BIO_get_buffer_num_lines(b) BIO_ctrl(b,BIO_C_GET_BUFF_NUM_LINES,0,NULL)
466#define BIO_set_buffer_size(b,size) BIO_ctrl(b,BIO_C_SET_BUFF_SIZE,size,NULL)
467#define BIO_set_read_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,0)
468#define BIO_set_write_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,1)
469#define BIO_set_buffer_read_data(b,buf,num) BIO_ctrl(b,BIO_C_SET_BUFF_READ_DATA,num,buf)
470
471/* Don't use the next one unless you know what you are doing :-) */
472#define BIO_dup_state(b,ret) BIO_ctrl(b,BIO_CTRL_DUP,0,(char *)(ret))
473
474#define BIO_reset(b) (int)BIO_ctrl(b,BIO_CTRL_RESET,0,NULL)
475#define BIO_eof(b) (int)BIO_ctrl(b,BIO_CTRL_EOF,0,NULL)
476#define BIO_set_close(b,c) (int)BIO_ctrl(b,BIO_CTRL_SET_CLOSE,(c),NULL)
477#define BIO_get_close(b) (int)BIO_ctrl(b,BIO_CTRL_GET_CLOSE,0,NULL)
478#define BIO_pending(b) (int)BIO_ctrl(b,BIO_CTRL_PENDING,0,NULL)
479#define BIO_wpending(b) (int)BIO_ctrl(b,BIO_CTRL_WPENDING,0,NULL)
480/* ...pending macros have inappropriate return type */
481size_t BIO_ctrl_pending(BIO *b);
482size_t BIO_ctrl_wpending(BIO *b);
483#define BIO_flush(b) (int)BIO_ctrl(b,BIO_CTRL_FLUSH,0,NULL)
484#define BIO_get_info_callback(b,cbp) (int)BIO_ctrl(b,BIO_CTRL_GET_CALLBACK,0, \
485 cbp)
486#define BIO_set_info_callback(b,cb) (int)BIO_callback_ctrl(b,BIO_CTRL_SET_CALLBACK,cb)
487
488/* For the BIO_f_buffer() type */
489#define BIO_buffer_get_num_lines(b) BIO_ctrl(b,BIO_CTRL_GET,0,NULL)
490
491/* For BIO_s_bio() */
492#define BIO_set_write_buf_size(b,size) (int)BIO_ctrl(b,BIO_C_SET_WRITE_BUF_SIZE,size,NULL)
493#define BIO_get_write_buf_size(b,size) (size_t)BIO_ctrl(b,BIO_C_GET_WRITE_BUF_SIZE,size,NULL)
494#define BIO_make_bio_pair(b1,b2) (int)BIO_ctrl(b1,BIO_C_MAKE_BIO_PAIR,0,b2)
495#define BIO_destroy_bio_pair(b) (int)BIO_ctrl(b,BIO_C_DESTROY_BIO_PAIR,0,NULL)
496#define BIO_shutdown_wr(b) (int)BIO_ctrl(b, BIO_C_SHUTDOWN_WR, 0, NULL)
497/* macros with inappropriate type -- but ...pending macros use int too: */
498#define BIO_get_write_guarantee(b) (int)BIO_ctrl(b,BIO_C_GET_WRITE_GUARANTEE,0,NULL)
499#define BIO_get_read_request(b) (int)BIO_ctrl(b,BIO_C_GET_READ_REQUEST,0,NULL)
500size_t BIO_ctrl_get_write_guarantee(BIO *b);
501size_t BIO_ctrl_get_read_request(BIO *b);
502int BIO_ctrl_reset_read_request(BIO *b);
503
504/* ctrl macros for dgram */
505#define BIO_ctrl_dgram_connect(b,peer) \
506 (int)BIO_ctrl(b,BIO_CTRL_DGRAM_CONNECT,0, (char *)peer)
507#define BIO_ctrl_set_connected(b, state, peer) \
508 (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SET_CONNECTED, state, (char *)peer)
509#define BIO_dgram_recv_timedout(b) \
510 (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP, 0, NULL)
511#define BIO_dgram_send_timedout(b) \
512 (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP, 0, NULL)
513#define BIO_dgram_get_peer(b,peer) \
514 (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_PEER, 0, (char *)peer)
515#define BIO_dgram_set_peer(b,peer) \
516 (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, (char *)peer)
517
518/* These two aren't currently implemented */
519/* int BIO_get_ex_num(BIO *bio); */
520/* void BIO_set_ex_free_func(BIO *bio,int idx,void (*cb)()); */
521int BIO_set_ex_data(BIO *bio, int idx, void *data);
522void *BIO_get_ex_data(BIO *bio, int idx);
523int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
524 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
525unsigned long BIO_number_read(BIO *bio);
526unsigned long BIO_number_written(BIO *bio);
527
528int BIO_get_new_index(void);
529const BIO_METHOD *BIO_s_file(void);
530BIO *BIO_new_file(const char *filename, const char *mode);
531BIO *BIO_new_fp(FILE *stream, int close_flag);
532BIO *BIO_new(const BIO_METHOD *type);
533int BIO_free(BIO *a);
534int BIO_up_ref(BIO *bio);
535void *BIO_get_data(BIO *a);
536void BIO_set_data(BIO *a, void *ptr);
537int BIO_get_init(BIO *a);
538void BIO_set_init(BIO *a, int init);
539int BIO_get_shutdown(BIO *a);
540void BIO_set_shutdown(BIO *a, int shut);
541void BIO_vfree(BIO *a);
542int BIO_read(BIO *b, void *data, int len)
543 __attribute__((__bounded__(__buffer__,2,3)));
544int BIO_gets(BIO *bp, char *buf, int size)
545 __attribute__((__bounded__ (__string__,2,3)));
546int BIO_write(BIO *b, const void *data, int len)
547 __attribute__((__bounded__(__buffer__,2,3)));
548int BIO_puts(BIO *bp, const char *buf);
549int BIO_indent(BIO *b, int indent, int max);
550long BIO_ctrl(BIO *bp, int cmd, long larg, void *parg);
551long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
552char * BIO_ptr_ctrl(BIO *bp, int cmd, long larg);
553long BIO_int_ctrl(BIO *bp, int cmd, long larg, int iarg);
554BIO * BIO_push(BIO *b, BIO *append);
555BIO * BIO_pop(BIO *b);
556void BIO_free_all(BIO *a);
557BIO * BIO_find_type(BIO *b, int bio_type);
558BIO * BIO_next(BIO *b);
559void BIO_set_next(BIO *b, BIO *next);
560BIO * BIO_get_retry_BIO(BIO *bio, int *reason);
561int BIO_get_retry_reason(BIO *bio);
562void BIO_set_retry_reason(BIO *bio, int reason);
563BIO * BIO_dup_chain(BIO *in);
564
565long BIO_debug_callback(BIO *bio, int cmd, const char *argp, int argi,
566 long argl, long ret);
567
568const BIO_METHOD *BIO_s_mem(void);
569BIO *BIO_new_mem_buf(const void *buf, int len);
570const BIO_METHOD *BIO_s_socket(void);
571const BIO_METHOD *BIO_s_connect(void);
572const BIO_METHOD *BIO_s_accept(void);
573const BIO_METHOD *BIO_s_fd(void);
574const BIO_METHOD *BIO_s_log(void);
575const BIO_METHOD *BIO_s_bio(void);
576const BIO_METHOD *BIO_s_null(void);
577const BIO_METHOD *BIO_f_null(void);
578const BIO_METHOD *BIO_f_buffer(void);
579const BIO_METHOD *BIO_f_nbio_test(void);
580#ifndef OPENSSL_NO_DGRAM
581const BIO_METHOD *BIO_s_datagram(void);
582#endif
583
584/* BIO_METHOD *BIO_f_ber(void); */
585
586int BIO_sock_should_retry(int i);
587int BIO_sock_non_fatal_error(int _error);
588int BIO_dgram_non_fatal_error(int _error);
589
590int BIO_fd_should_retry(int i);
591int BIO_fd_non_fatal_error(int _error);
592
593int BIO_dump(BIO *b, const char *bytes, int len);
594int BIO_dump_indent(BIO *b, const char *bytes, int len, int indent);
595
596struct hostent *BIO_gethostbyname(const char *name);
597/* We might want a thread-safe interface too:
598 * struct hostent *BIO_gethostbyname_r(const char *name,
599 * struct hostent *result, void *buffer, size_t buflen);
600 * or something similar (caller allocates a struct hostent,
601 * pointed to by "result", and additional buffer space for the various
602 * substructures; if the buffer does not suffice, NULL is returned
603 * and an appropriate error code is set).
604 */
605int BIO_sock_error(int sock);
606int BIO_socket_ioctl(int fd, long type, void *arg);
607int BIO_socket_nbio(int fd, int mode);
608int BIO_get_port(const char *str, unsigned short *port_ptr);
609int BIO_get_host_ip(const char *str, unsigned char *ip);
610int BIO_get_accept_socket(char *host_port, int mode);
611int BIO_accept(int sock, char **ip_port);
612int BIO_sock_init(void );
613void BIO_sock_cleanup(void);
614int BIO_set_tcp_ndelay(int sock, int turn_on);
615
616BIO *BIO_new_socket(int sock, int close_flag);
617BIO *BIO_new_dgram(int fd, int close_flag);
618BIO *BIO_new_fd(int fd, int close_flag);
619BIO *BIO_new_connect(const char *host_port);
620BIO *BIO_new_accept(const char *host_port);
621
622int BIO_new_bio_pair(BIO **bio1, size_t writebuf1,
623 BIO **bio2, size_t writebuf2);
624/* If successful, returns 1 and in *bio1, *bio2 two BIO pair endpoints.
625 * Otherwise returns 0 and sets *bio1 and *bio2 to NULL.
626 * Size 0 uses default value.
627 */
628
629void BIO_copy_next_retry(BIO *b);
630
631/*long BIO_ghbn_ctrl(int cmd,int iarg,char *parg);*/
632
633/* Needed for libressl-portable. */
634#ifndef __MINGW_PRINTF_FORMAT
635int BIO_printf(BIO *bio, const char *format, ...)
636 __attribute__((__format__(__printf__, 2, 3), __nonnull__(2)));
637#else
638int BIO_printf(BIO *bio, const char *format, ...)
639 __attribute__((__format__(__MINGW_PRINTF_FORMAT, 2, 3), __nonnull__(2)));
640#endif
641
642void ERR_load_BIO_strings(void);
643
644/* Error codes for the BIO functions. */
645
646/* Function codes. */
647#define BIO_F_ACPT_STATE 100
648#define BIO_F_BIO_ACCEPT 101
649#define BIO_F_BIO_BER_GET_HEADER 102
650#define BIO_F_BIO_CALLBACK_CTRL 131
651#define BIO_F_BIO_CTRL 103
652#define BIO_F_BIO_GETHOSTBYNAME 120
653#define BIO_F_BIO_GETS 104
654#define BIO_F_BIO_GET_ACCEPT_SOCKET 105
655#define BIO_F_BIO_GET_HOST_IP 106
656#define BIO_F_BIO_GET_PORT 107
657#define BIO_F_BIO_MAKE_PAIR 121
658#define BIO_F_BIO_NEW 108
659#define BIO_F_BIO_NEW_FILE 109
660#define BIO_F_BIO_NEW_MEM_BUF 126
661#define BIO_F_BIO_NREAD 123
662#define BIO_F_BIO_NREAD0 124
663#define BIO_F_BIO_NWRITE 125
664#define BIO_F_BIO_NWRITE0 122
665#define BIO_F_BIO_PUTS 110
666#define BIO_F_BIO_READ 111
667#define BIO_F_BIO_SOCK_INIT 112
668#define BIO_F_BIO_WRITE 113
669#define BIO_F_BUFFER_CTRL 114
670#define BIO_F_CONN_CTRL 127
671#define BIO_F_CONN_STATE 115
672#define BIO_F_DGRAM_SCTP_READ 132
673#define BIO_F_FILE_CTRL 116
674#define BIO_F_FILE_READ 130
675#define BIO_F_LINEBUFFER_CTRL 129
676#define BIO_F_MEM_READ 128
677#define BIO_F_MEM_WRITE 117
678#define BIO_F_SSL_NEW 118
679#define BIO_F_WSASTARTUP 119
680
681/* Reason codes. */
682#define BIO_R_ACCEPT_ERROR 100
683#define BIO_R_BAD_FOPEN_MODE 101
684#define BIO_R_BAD_HOSTNAME_LOOKUP 102
685#define BIO_R_BROKEN_PIPE 124
686#define BIO_R_CONNECT_ERROR 103
687#define BIO_R_EOF_ON_MEMORY_BIO 127
688#define BIO_R_ERROR_SETTING_NBIO 104
689#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET 105
690#define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET 106
691#define BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET 107
692#define BIO_R_INVALID_ARGUMENT 125
693#define BIO_R_INVALID_IP_ADDRESS 108
694#define BIO_R_INVALID_PORT_NUMBER 129
695#define BIO_R_IN_USE 123
696#define BIO_R_KEEPALIVE 109
697#define BIO_R_LENGTH_TOO_LONG 130
698#define BIO_R_NBIO_CONNECT_ERROR 110
699#define BIO_R_NO_ACCEPT_PORT_SPECIFIED 111
700#define BIO_R_NO_HOSTNAME_SPECIFIED 112
701#define BIO_R_NO_PORT_DEFINED 113
702#define BIO_R_NO_PORT_SPECIFIED 114
703#define BIO_R_NO_SUCH_FILE 128
704#define BIO_R_NULL_PARAMETER 115
705#define BIO_R_TAG_MISMATCH 116
706#define BIO_R_UNABLE_TO_BIND_SOCKET 117
707#define BIO_R_UNABLE_TO_CREATE_SOCKET 118
708#define BIO_R_UNABLE_TO_LISTEN_SOCKET 119
709#define BIO_R_UNINITIALIZED 120
710#define BIO_R_UNSUPPORTED_METHOD 121
711#define BIO_R_WRITE_TO_READ_ONLY_BIO 126
712#define BIO_R_WSASTARTUP 122
713
714#ifdef __cplusplus
715}
716#endif
717#endif
diff --git a/src/lib/libcrypto/bio/bio_cb.c b/src/lib/libcrypto/bio/bio_cb.c
deleted file mode 100644
index 18e9be8d68..0000000000
--- a/src/lib/libcrypto/bio/bio_cb.c
+++ /dev/null
@@ -1,156 +0,0 @@
1/* $OpenBSD: bio_cb.c,v 1.19 2023/07/05 21:23:37 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62
63#include <openssl/err.h>
64#include <openssl/bio.h>
65
66#include "bio_local.h"
67
68long
69BIO_debug_callback(BIO *bio, int cmd, const char *argp, int argi, long argl,
70 long ret)
71{
72 BIO *b;
73 char buf[256];
74 char *p;
75 int nbuf;
76 long r = 1;
77 size_t p_maxlen;
78
79 if (BIO_CB_RETURN & cmd)
80 r = ret;
81
82 nbuf = snprintf(buf, sizeof(buf), "BIO[%p]: ", bio);
83 if (nbuf < 0)
84 nbuf = 0; /* Ignore error; continue printing. */
85 if (nbuf >= sizeof(buf))
86 goto out;
87
88 p = buf + nbuf;
89 p_maxlen = sizeof(buf) - nbuf;
90
91 switch (cmd) {
92 case BIO_CB_FREE:
93 snprintf(p, p_maxlen, "Free - %s\n", bio->method->name);
94 break;
95 case BIO_CB_READ:
96 if (bio->method->type & BIO_TYPE_DESCRIPTOR)
97 snprintf(p, p_maxlen,
98 "read(%d,%lu) - %s fd=%d\n",
99 bio->num, (unsigned long)argi,
100 bio->method->name, bio->num);
101 else
102 snprintf(p, p_maxlen, "read(%d,%lu) - %s\n",
103 bio->num, (unsigned long)argi, bio->method->name);
104 break;
105 case BIO_CB_WRITE:
106 if (bio->method->type & BIO_TYPE_DESCRIPTOR)
107 snprintf(p, p_maxlen,
108 "write(%d,%lu) - %s fd=%d\n",
109 bio->num, (unsigned long)argi,
110 bio->method->name, bio->num);
111 else
112 snprintf(p, p_maxlen, "write(%d,%lu) - %s\n",
113 bio->num, (unsigned long)argi, bio->method->name);
114 break;
115 case BIO_CB_PUTS:
116 snprintf(p, p_maxlen,
117 "puts() - %s\n", bio->method->name);
118 break;
119 case BIO_CB_GETS:
120 snprintf(p, p_maxlen, "gets(%lu) - %s\n",
121 (unsigned long)argi, bio->method->name);
122 break;
123 case BIO_CB_CTRL:
124 snprintf(p, p_maxlen, "ctrl(%lu) - %s\n",
125 (unsigned long)argi, bio->method->name);
126 break;
127 case BIO_CB_RETURN|BIO_CB_READ:
128 snprintf(p, p_maxlen, "read return %ld\n", ret);
129 break;
130 case BIO_CB_RETURN|BIO_CB_WRITE:
131 snprintf(p, p_maxlen, "write return %ld\n", ret);
132 break;
133 case BIO_CB_RETURN|BIO_CB_GETS:
134 snprintf(p, p_maxlen, "gets return %ld\n", ret);
135 break;
136 case BIO_CB_RETURN|BIO_CB_PUTS:
137 snprintf(p, p_maxlen, "puts return %ld\n", ret);
138 break;
139 case BIO_CB_RETURN|BIO_CB_CTRL:
140 snprintf(p, p_maxlen, "ctrl return %ld\n", ret);
141 break;
142 default:
143 snprintf(p, p_maxlen,
144 "bio callback - unknown type (%d)\n", cmd);
145 break;
146 }
147
148 out:
149 b = (BIO *)bio->cb_arg;
150 if (b != NULL)
151 BIO_write(b, buf, strlen(buf));
152 else
153 fputs(buf, stderr);
154 return (r);
155}
156LCRYPTO_ALIAS(BIO_debug_callback);
diff --git a/src/lib/libcrypto/bio/bio_err.c b/src/lib/libcrypto/bio/bio_err.c
deleted file mode 100644
index 4541adb240..0000000000
--- a/src/lib/libcrypto/bio/bio_err.c
+++ /dev/null
@@ -1,122 +0,0 @@
1/* $OpenBSD: bio_err.c,v 1.21 2024/06/24 06:43:22 tb Exp $ */
2/* ====================================================================
3 * Copyright (c) 1999-2011 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56#include <stdio.h>
57
58#include <openssl/opensslconf.h>
59
60#include <openssl/err.h>
61#include <openssl/bio.h>
62
63#include "err_local.h"
64
65#ifndef OPENSSL_NO_ERR
66
67#define ERR_FUNC(func) ERR_PACK(ERR_LIB_BIO,func,0)
68#define ERR_REASON(reason) ERR_PACK(ERR_LIB_BIO,0,reason)
69
70static const ERR_STRING_DATA BIO_str_functs[] = {
71 {ERR_FUNC(0xfff), "CRYPTO_internal"},
72 {0, NULL}
73};
74
75static const ERR_STRING_DATA BIO_str_reasons[] = {
76 {ERR_REASON(BIO_R_ACCEPT_ERROR) , "accept error"},
77 {ERR_REASON(BIO_R_BAD_FOPEN_MODE) , "bad fopen mode"},
78 {ERR_REASON(BIO_R_BAD_HOSTNAME_LOOKUP) , "bad hostname lookup"},
79 {ERR_REASON(BIO_R_BROKEN_PIPE) , "broken pipe"},
80 {ERR_REASON(BIO_R_CONNECT_ERROR) , "connect error"},
81 {ERR_REASON(BIO_R_EOF_ON_MEMORY_BIO) , "EOF on memory BIO"},
82 {ERR_REASON(BIO_R_ERROR_SETTING_NBIO) , "error setting nbio"},
83 {ERR_REASON(BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET), "error setting nbio on accepted socket"},
84 {ERR_REASON(BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET), "error setting nbio on accept socket"},
85 {ERR_REASON(BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET), "gethostbyname addr is not af inet"},
86 {ERR_REASON(BIO_R_INVALID_ARGUMENT) , "invalid argument"},
87 {ERR_REASON(BIO_R_INVALID_IP_ADDRESS) , "invalid ip address"},
88 {ERR_REASON(BIO_R_INVALID_PORT_NUMBER) , "invalid port number"},
89 {ERR_REASON(BIO_R_IN_USE) , "in use"},
90 {ERR_REASON(BIO_R_KEEPALIVE) , "keepalive"},
91 {ERR_REASON(BIO_R_LENGTH_TOO_LONG) , "too long"},
92 {ERR_REASON(BIO_R_NBIO_CONNECT_ERROR) , "nbio connect error"},
93 {ERR_REASON(BIO_R_NO_ACCEPT_PORT_SPECIFIED), "no accept port specified"},
94 {ERR_REASON(BIO_R_NO_HOSTNAME_SPECIFIED) , "no hostname specified"},
95 {ERR_REASON(BIO_R_NO_PORT_DEFINED) , "no port defined"},
96 {ERR_REASON(BIO_R_NO_PORT_SPECIFIED) , "no port specified"},
97 {ERR_REASON(BIO_R_NO_SUCH_FILE) , "no such file"},
98 {ERR_REASON(BIO_R_NULL_PARAMETER) , "null parameter"},
99 {ERR_REASON(BIO_R_TAG_MISMATCH) , "tag mismatch"},
100 {ERR_REASON(BIO_R_UNABLE_TO_BIND_SOCKET) , "unable to bind socket"},
101 {ERR_REASON(BIO_R_UNABLE_TO_CREATE_SOCKET), "unable to create socket"},
102 {ERR_REASON(BIO_R_UNABLE_TO_LISTEN_SOCKET), "unable to listen socket"},
103 {ERR_REASON(BIO_R_UNINITIALIZED) , "uninitialized"},
104 {ERR_REASON(BIO_R_UNSUPPORTED_METHOD) , "unsupported method"},
105 {ERR_REASON(BIO_R_WRITE_TO_READ_ONLY_BIO), "write to read only BIO"},
106 {ERR_REASON(BIO_R_WSASTARTUP) , "WSAStartup"},
107 {0, NULL}
108};
109
110#endif
111
112void
113ERR_load_BIO_strings(void)
114{
115#ifndef OPENSSL_NO_ERR
116 if (ERR_func_error_string(BIO_str_functs[0].error) == NULL) {
117 ERR_load_const_strings(BIO_str_functs);
118 ERR_load_const_strings(BIO_str_reasons);
119 }
120#endif
121}
122LCRYPTO_ALIAS(ERR_load_BIO_strings);
diff --git a/src/lib/libcrypto/bio/bio_lib.c b/src/lib/libcrypto/bio/bio_lib.c
deleted file mode 100644
index 463d2ad23a..0000000000
--- a/src/lib/libcrypto/bio/bio_lib.c
+++ /dev/null
@@ -1,886 +0,0 @@
1/* $OpenBSD: bio_lib.c,v 1.54 2024/07/09 06:14:59 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <errno.h>
60#include <limits.h>
61#include <stdio.h>
62
63#include <openssl/bio.h>
64#include <openssl/crypto.h>
65#include <openssl/err.h>
66#include <openssl/stack.h>
67
68#include "bio_local.h"
69
70/*
71 * Helper function to work out whether to call the new style callback or the old
72 * one, and translate between the two.
73 *
74 * This has a long return type for consistency with the old callback. Similarly
75 * for the "long" used for "inret"
76 */
77static long
78bio_call_callback(BIO *b, int oper, const char *argp, size_t len, int argi,
79 long argl, long inret, size_t *processed)
80{
81 long ret;
82 int bareoper;
83
84 if (b->callback_ex != NULL)
85 return b->callback_ex(b, oper, argp, len, argi, argl, inret,
86 processed);
87
88 /*
89 * We have an old style callback, so we will have to do nasty casts and
90 * check for overflows.
91 */
92
93 bareoper = oper & ~BIO_CB_RETURN;
94
95 if (bareoper == BIO_CB_READ || bareoper == BIO_CB_WRITE ||
96 bareoper == BIO_CB_GETS) {
97 /* In this case len is set and should be used instead of argi. */
98 if (len > INT_MAX)
99 return -1;
100 argi = (int)len;
101 }
102
103 if (inret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
104 if (*processed > INT_MAX)
105 return -1;
106 inret = *processed;
107 }
108
109 ret = b->callback(b, oper, argp, argi, argl, inret);
110
111 if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
112 *processed = (size_t)ret;
113 ret = 1;
114 }
115
116 return ret;
117}
118
119int
120BIO_get_new_index(void)
121{
122 static int bio_type_index = BIO_TYPE_START;
123 int index;
124
125 /* The index will collide with the BIO flag bits if it exceeds 255. */
126 index = CRYPTO_add(&bio_type_index, 1, CRYPTO_LOCK_BIO);
127 if (index > 255)
128 return -1;
129
130 return index;
131}
132LCRYPTO_ALIAS(BIO_get_new_index);
133
134BIO *
135BIO_new(const BIO_METHOD *method)
136{
137 BIO *bio = NULL;
138
139 if ((bio = calloc(1, sizeof(BIO))) == NULL) {
140 BIOerror(ERR_R_MALLOC_FAILURE);
141 return NULL;
142 }
143
144 bio->method = method;
145 bio->shutdown = 1;
146 bio->references = 1;
147
148 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
149
150 if (method->create != NULL) {
151 if (!method->create(bio)) {
152 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio,
153 &bio->ex_data);
154 free(bio);
155 return NULL;
156 }
157 }
158
159 return bio;
160}
161LCRYPTO_ALIAS(BIO_new);
162
163int
164BIO_free(BIO *bio)
165{
166 int ret;
167
168 if (bio == NULL)
169 return 0;
170
171 if (CRYPTO_add(&bio->references, -1, CRYPTO_LOCK_BIO) > 0)
172 return 1;
173
174 if (bio->callback != NULL || bio->callback_ex != NULL) {
175 if ((ret = (int)bio_call_callback(bio, BIO_CB_FREE, NULL, 0, 0,
176 0L, 1L, NULL)) <= 0)
177 return ret;
178 }
179
180 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
181
182 if (bio->method != NULL && bio->method->destroy != NULL)
183 bio->method->destroy(bio);
184
185 free(bio);
186
187 return 1;
188}
189LCRYPTO_ALIAS(BIO_free);
190
191void
192BIO_vfree(BIO *bio)
193{
194 BIO_free(bio);
195}
196LCRYPTO_ALIAS(BIO_vfree);
197
198int
199BIO_up_ref(BIO *bio)
200{
201 return CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO) > 1;
202}
203LCRYPTO_ALIAS(BIO_up_ref);
204
205void *
206BIO_get_data(BIO *bio)
207{
208 return bio->ptr;
209}
210LCRYPTO_ALIAS(BIO_get_data);
211
212void
213BIO_set_data(BIO *bio, void *ptr)
214{
215 bio->ptr = ptr;
216}
217LCRYPTO_ALIAS(BIO_set_data);
218
219int
220BIO_get_init(BIO *bio)
221{
222 return bio->init;
223}
224LCRYPTO_ALIAS(BIO_get_init);
225
226void
227BIO_set_init(BIO *bio, int init)
228{
229 bio->init = init;
230}
231LCRYPTO_ALIAS(BIO_set_init);
232
233int
234BIO_get_shutdown(BIO *bio)
235{
236 return bio->shutdown;
237}
238LCRYPTO_ALIAS(BIO_get_shutdown);
239
240void
241BIO_set_shutdown(BIO *bio, int shut)
242{
243 bio->shutdown = shut;
244}
245LCRYPTO_ALIAS(BIO_set_shutdown);
246
247void
248BIO_clear_flags(BIO *bio, int flags)
249{
250 bio->flags &= ~flags;
251}
252LCRYPTO_ALIAS(BIO_clear_flags);
253
254int
255BIO_test_flags(const BIO *bio, int flags)
256{
257 return (bio->flags & flags);
258}
259LCRYPTO_ALIAS(BIO_test_flags);
260
261void
262BIO_set_flags(BIO *bio, int flags)
263{
264 bio->flags |= flags;
265}
266LCRYPTO_ALIAS(BIO_set_flags);
267
268BIO_callback_fn
269BIO_get_callback(const BIO *bio)
270{
271 return bio->callback;
272}
273LCRYPTO_ALIAS(BIO_get_callback);
274
275void
276BIO_set_callback(BIO *bio, BIO_callback_fn cb)
277{
278 bio->callback = cb;
279}
280LCRYPTO_ALIAS(BIO_set_callback);
281
282BIO_callback_fn_ex
283BIO_get_callback_ex(const BIO *bio)
284{
285 return bio->callback_ex;
286}
287LCRYPTO_ALIAS(BIO_get_callback_ex);
288
289void
290BIO_set_callback_ex(BIO *bio, BIO_callback_fn_ex cb)
291{
292 bio->callback_ex = cb;
293}
294LCRYPTO_ALIAS(BIO_set_callback_ex);
295
296void
297BIO_set_callback_arg(BIO *bio, char *arg)
298{
299 bio->cb_arg = arg;
300}
301LCRYPTO_ALIAS(BIO_set_callback_arg);
302
303char *
304BIO_get_callback_arg(const BIO *bio)
305{
306 return bio->cb_arg;
307}
308LCRYPTO_ALIAS(BIO_get_callback_arg);
309
310const char *
311BIO_method_name(const BIO *bio)
312{
313 return bio->method->name;
314}
315LCRYPTO_ALIAS(BIO_method_name);
316
317int
318BIO_method_type(const BIO *bio)
319{
320 return bio->method->type;
321}
322LCRYPTO_ALIAS(BIO_method_type);
323
324int
325BIO_read(BIO *b, void *out, int outl)
326{
327 size_t readbytes = 0;
328 int ret;
329
330 if (b == NULL) {
331 BIOerror(ERR_R_PASSED_NULL_PARAMETER);
332 return (-1);
333 }
334
335 if (outl <= 0)
336 return (0);
337
338 if (out == NULL) {
339 BIOerror(ERR_R_PASSED_NULL_PARAMETER);
340 return (-1);
341 }
342
343 if (b->method == NULL || b->method->bread == NULL) {
344 BIOerror(BIO_R_UNSUPPORTED_METHOD);
345 return (-2);
346 }
347
348 if (b->callback != NULL || b->callback_ex != NULL) {
349 if ((ret = (int)bio_call_callback(b, BIO_CB_READ, out, outl, 0,
350 0L, 1L, NULL)) <= 0)
351 return (ret);
352 }
353
354 if (!b->init) {
355 BIOerror(BIO_R_UNINITIALIZED);
356 return (-2);
357 }
358
359 if ((ret = b->method->bread(b, out, outl)) > 0)
360 readbytes = (size_t)ret;
361
362 b->num_read += readbytes;
363
364 if (b->callback != NULL || b->callback_ex != NULL) {
365 ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN,
366 out, outl, 0, 0L, (ret > 0) ? 1 : ret, &readbytes);
367 }
368
369 if (ret > 0) {
370 if (readbytes > INT_MAX) {
371 BIOerror(BIO_R_LENGTH_TOO_LONG);
372 ret = -1;
373 } else {
374 ret = (int)readbytes;
375 }
376 }
377
378 return (ret);
379}
380LCRYPTO_ALIAS(BIO_read);
381
382int
383BIO_write(BIO *b, const void *in, int inl)
384{
385 size_t writebytes = 0;
386 int ret;
387
388 /* Not an error. Things like SMIME_text() assume that this succeeds. */
389 if (b == NULL)
390 return (0);
391
392 if (inl <= 0)
393 return (0);
394
395 if (in == NULL) {
396 BIOerror(ERR_R_PASSED_NULL_PARAMETER);
397 return (-1);
398 }
399
400 if (b->method == NULL || b->method->bwrite == NULL) {
401 BIOerror(BIO_R_UNSUPPORTED_METHOD);
402 return (-2);
403 }
404
405 if (b->callback != NULL || b->callback_ex != NULL) {
406 if ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, in, inl, 0,
407 0L, 1L, NULL)) <= 0)
408 return (ret);
409 }
410
411 if (!b->init) {
412 BIOerror(BIO_R_UNINITIALIZED);
413 return (-2);
414 }
415
416 if ((ret = b->method->bwrite(b, in, inl)) > 0)
417 writebytes = ret;
418
419 b->num_write += writebytes;
420
421 if (b->callback != NULL || b->callback_ex != NULL) {
422 ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN,
423 in, inl, 0, 0L, (ret > 0) ? 1 : ret, &writebytes);
424 }
425
426 if (ret > 0) {
427 if (writebytes > INT_MAX) {
428 BIOerror(BIO_R_LENGTH_TOO_LONG);
429 ret = -1;
430 } else {
431 ret = (int)writebytes;
432 }
433 }
434
435 return (ret);
436}
437LCRYPTO_ALIAS(BIO_write);
438
439int
440BIO_puts(BIO *b, const char *in)
441{
442 size_t writebytes = 0;
443 int ret;
444
445 if (b == NULL || b->method == NULL || b->method->bputs == NULL) {
446 BIOerror(BIO_R_UNSUPPORTED_METHOD);
447 return (-2);
448 }
449
450 if (b->callback != NULL || b->callback_ex != NULL) {
451 if ((ret = (int)bio_call_callback(b, BIO_CB_PUTS, in, 0, 0, 0L,
452 1L, NULL)) <= 0)
453 return (ret);
454 }
455
456 if (!b->init) {
457 BIOerror(BIO_R_UNINITIALIZED);
458 return (-2);
459 }
460
461 if ((ret = b->method->bputs(b, in)) > 0)
462 writebytes = ret;
463
464 b->num_write += writebytes;
465
466 if (b->callback != NULL || b->callback_ex != NULL) {
467 ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN,
468 in, 0, 0, 0L, (ret > 0) ? 1 : ret, &writebytes);
469 }
470
471 if (ret > 0) {
472 if (writebytes > INT_MAX) {
473 BIOerror(BIO_R_LENGTH_TOO_LONG);
474 ret = -1;
475 } else {
476 ret = (int)writebytes;
477 }
478 }
479
480 return (ret);
481}
482LCRYPTO_ALIAS(BIO_puts);
483
484int
485BIO_gets(BIO *b, char *in, int inl)
486{
487 size_t readbytes = 0;
488 int ret;
489
490 if (b == NULL || b->method == NULL || b->method->bgets == NULL) {
491 BIOerror(BIO_R_UNSUPPORTED_METHOD);
492 return (-2);
493 }
494
495 if (b->callback != NULL || b->callback_ex != NULL) {
496 if ((ret = (int)bio_call_callback(b, BIO_CB_GETS, in, inl, 0, 0L,
497 1, NULL)) <= 0)
498 return (ret);
499 }
500
501 if (!b->init) {
502 BIOerror(BIO_R_UNINITIALIZED);
503 return (-2);
504 }
505
506 if ((ret = b->method->bgets(b, in, inl)) > 0)
507 readbytes = ret;
508
509 if (b->callback != NULL || b->callback_ex != NULL) {
510 ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, in,
511 inl, 0, 0L, (ret > 0) ? 1 : ret, &readbytes);
512 }
513
514 if (ret > 0) {
515 if (readbytes > INT_MAX) {
516 BIOerror(BIO_R_LENGTH_TOO_LONG);
517 ret = -1;
518 } else {
519 ret = (int)readbytes;
520 }
521 }
522
523 return (ret);
524}
525LCRYPTO_ALIAS(BIO_gets);
526
527int
528BIO_indent(BIO *bio, int indent, int max)
529{
530 if (indent > max)
531 indent = max;
532 if (indent <= 0)
533 return 1;
534 if (BIO_printf(bio, "%*s", indent, "") <= 0)
535 return 0;
536 return 1;
537}
538LCRYPTO_ALIAS(BIO_indent);
539
540long
541BIO_int_ctrl(BIO *bio, int cmd, long larg, int iarg)
542{
543 int i;
544
545 i = iarg;
546 return BIO_ctrl(bio, cmd, larg, (char *)&i);
547}
548LCRYPTO_ALIAS(BIO_int_ctrl);
549
550char *
551BIO_ptr_ctrl(BIO *bio, int cmd, long larg)
552{
553 char *p = NULL;
554
555 if (BIO_ctrl(bio, cmd, larg, (char *)&p) <= 0)
556 return NULL;
557 else
558 return p;
559}
560LCRYPTO_ALIAS(BIO_ptr_ctrl);
561
562long
563BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
564{
565 long ret;
566
567 if (b == NULL)
568 return (0);
569
570 if (b->method == NULL || b->method->ctrl == NULL) {
571 BIOerror(BIO_R_UNSUPPORTED_METHOD);
572 return (-2);
573 }
574
575 if (b->callback != NULL || b->callback_ex != NULL) {
576 if ((ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg,
577 1L, NULL)) <= 0)
578 return (ret);
579 }
580
581 ret = b->method->ctrl(b, cmd, larg, parg);
582
583 if (b->callback != NULL || b->callback_ex != NULL) {
584 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0,
585 cmd, larg, ret, NULL);
586 }
587
588 return (ret);
589}
590LCRYPTO_ALIAS(BIO_ctrl);
591
592long
593BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
594{
595 long ret;
596
597 if (b == NULL)
598 return (0);
599
600 if (b->method == NULL || b->method->callback_ctrl == NULL ||
601 cmd != BIO_CTRL_SET_CALLBACK) {
602 BIOerror(BIO_R_UNSUPPORTED_METHOD);
603 return (-2);
604 }
605
606 if (b->callback != NULL || b->callback_ex != NULL) {
607 if ((ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0,
608 cmd, 0, 1L, NULL)) <= 0)
609 return (ret);
610 }
611
612 ret = b->method->callback_ctrl(b, cmd, fp);
613
614 if (b->callback != NULL || b->callback_ex != NULL) {
615 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN,
616 (void *)&fp, 0, cmd, 0, ret, NULL);
617 }
618
619 return (ret);
620}
621LCRYPTO_ALIAS(BIO_callback_ctrl);
622
623/* It is unfortunate to duplicate in functions what the BIO_(w)pending macros
624 * do; but those macros have inappropriate return type, and for interfacing
625 * from other programming languages, C macros aren't much of a help anyway. */
626size_t
627BIO_ctrl_pending(BIO *bio)
628{
629 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
630}
631LCRYPTO_ALIAS(BIO_ctrl_pending);
632
633size_t
634BIO_ctrl_wpending(BIO *bio)
635{
636 return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
637}
638LCRYPTO_ALIAS(BIO_ctrl_wpending);
639
640
641/*
642 * Append "bio" to the end of the chain containing "b":
643 * Two chains "b -> lb" and "oldhead -> bio"
644 * become two chains "b -> lb -> bio" and "oldhead".
645 */
646BIO *
647BIO_push(BIO *b, BIO *bio)
648{
649 BIO *lb;
650
651 if (b == NULL)
652 return (bio);
653 lb = b;
654 while (lb->next_bio != NULL)
655 lb = lb->next_bio;
656 lb->next_bio = bio;
657 if (bio != NULL) {
658 if (bio->prev_bio != NULL)
659 bio->prev_bio->next_bio = NULL;
660 bio->prev_bio = lb;
661 }
662 /* called to do internal processing */
663 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
664 return (b);
665}
666LCRYPTO_ALIAS(BIO_push);
667
668/* Remove the first and return the rest */
669BIO *
670BIO_pop(BIO *b)
671{
672 BIO *ret;
673
674 if (b == NULL)
675 return (NULL);
676 ret = b->next_bio;
677
678 BIO_ctrl(b, BIO_CTRL_POP, 0, b);
679
680 if (b->prev_bio != NULL)
681 b->prev_bio->next_bio = b->next_bio;
682 if (b->next_bio != NULL)
683 b->next_bio->prev_bio = b->prev_bio;
684
685 b->next_bio = NULL;
686 b->prev_bio = NULL;
687 return (ret);
688}
689LCRYPTO_ALIAS(BIO_pop);
690
691BIO *
692BIO_get_retry_BIO(BIO *bio, int *reason)
693{
694 BIO *b, *last;
695
696 b = last = bio;
697 for (;;) {
698 if (!BIO_should_retry(b))
699 break;
700 last = b;
701 b = b->next_bio;
702 if (b == NULL)
703 break;
704 }
705 if (reason != NULL)
706 *reason = last->retry_reason;
707 return (last);
708}
709LCRYPTO_ALIAS(BIO_get_retry_BIO);
710
711int
712BIO_get_retry_reason(BIO *bio)
713{
714 return bio->retry_reason;
715}
716LCRYPTO_ALIAS(BIO_get_retry_reason);
717
718void
719BIO_set_retry_reason(BIO *bio, int reason)
720{
721 bio->retry_reason = reason;
722}
723LCRYPTO_ALIAS(BIO_set_retry_reason);
724
725BIO *
726BIO_find_type(BIO *bio, int type)
727{
728 int mt, mask;
729
730 if (!bio)
731 return NULL;
732 mask = type & 0xff;
733 do {
734 if (bio->method != NULL) {
735 mt = bio->method->type;
736 if (!mask) {
737 if (mt & type)
738 return (bio);
739 } else if (mt == type)
740 return (bio);
741 }
742 bio = bio->next_bio;
743 } while (bio != NULL);
744 return (NULL);
745}
746LCRYPTO_ALIAS(BIO_find_type);
747
748BIO *
749BIO_next(BIO *b)
750{
751 if (!b)
752 return NULL;
753 return b->next_bio;
754}
755LCRYPTO_ALIAS(BIO_next);
756
757/*
758 * Two chains "bio -> oldtail" and "oldhead -> next" become
759 * three chains "oldtail", "bio -> next", and "oldhead".
760 */
761void
762BIO_set_next(BIO *bio, BIO *next)
763{
764 /* Cut off the tail of the chain containing bio after bio. */
765 if (bio->next_bio != NULL)
766 bio->next_bio->prev_bio = NULL;
767
768 /* Cut off the head of the chain containing next before next. */
769 if (next != NULL && next->prev_bio != NULL)
770 next->prev_bio->next_bio = NULL;
771
772 /* Append the chain starting at next to the chain ending at bio. */
773 bio->next_bio = next;
774 if (next != NULL)
775 next->prev_bio = bio;
776}
777LCRYPTO_ALIAS(BIO_set_next);
778
779void
780BIO_free_all(BIO *bio)
781{
782 BIO *b;
783 int ref;
784
785 while (bio != NULL) {
786 b = bio;
787 ref = b->references;
788 bio = bio->next_bio;
789 BIO_free(b);
790 /* Since ref count > 1, don't free anyone else. */
791 if (ref > 1)
792 break;
793 }
794}
795LCRYPTO_ALIAS(BIO_free_all);
796
797BIO *
798BIO_dup_chain(BIO *in)
799{
800 BIO *new_chain = NULL, *new_bio = NULL, *tail = NULL;
801 BIO *bio;
802
803 for (bio = in; bio != NULL; bio = bio->next_bio) {
804 if ((new_bio = BIO_new(bio->method)) == NULL)
805 goto err;
806 new_bio->callback = bio->callback;
807 new_bio->callback_ex = bio->callback_ex;
808 new_bio->cb_arg = bio->cb_arg;
809 new_bio->init = bio->init;
810 new_bio->shutdown = bio->shutdown;
811 new_bio->flags = bio->flags;
812 new_bio->num = bio->num;
813
814 if (!BIO_dup_state(bio, new_bio))
815 goto err;
816
817 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO,
818 &new_bio->ex_data, &bio->ex_data))
819 goto err;
820
821 if (BIO_push(tail, new_bio) == NULL)
822 goto err;
823
824 tail = new_bio;
825 if (new_chain == NULL)
826 new_chain = new_bio;
827 }
828
829 return new_chain;
830
831 err:
832 BIO_free(new_bio);
833 BIO_free_all(new_chain);
834
835 return NULL;
836}
837LCRYPTO_ALIAS(BIO_dup_chain);
838
839void
840BIO_copy_next_retry(BIO *b)
841{
842 BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
843 b->retry_reason = b->next_bio->retry_reason;
844}
845LCRYPTO_ALIAS(BIO_copy_next_retry);
846
847int
848BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
849 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
850{
851 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, argl, argp,
852 new_func, dup_func, free_func);
853}
854LCRYPTO_ALIAS(BIO_get_ex_new_index);
855
856int
857BIO_set_ex_data(BIO *bio, int idx, void *data)
858{
859 return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
860}
861LCRYPTO_ALIAS(BIO_set_ex_data);
862
863void *
864BIO_get_ex_data(BIO *bio, int idx)
865{
866 return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
867}
868LCRYPTO_ALIAS(BIO_get_ex_data);
869
870unsigned long
871BIO_number_read(BIO *bio)
872{
873 if (bio)
874 return bio->num_read;
875 return 0;
876}
877LCRYPTO_ALIAS(BIO_number_read);
878
879unsigned long
880BIO_number_written(BIO *bio)
881{
882 if (bio)
883 return bio->num_write;
884 return 0;
885}
886LCRYPTO_ALIAS(BIO_number_written);
diff --git a/src/lib/libcrypto/bio/bio_local.h b/src/lib/libcrypto/bio/bio_local.h
deleted file mode 100644
index f59b5756c9..0000000000
--- a/src/lib/libcrypto/bio/bio_local.h
+++ /dev/null
@@ -1,127 +0,0 @@
1/* $OpenBSD: bio_local.h,v 1.6 2024/03/02 09:18:28 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_BIO_LOCAL_H
60#define HEADER_BIO_LOCAL_H
61
62#include <stdarg.h>
63
64__BEGIN_HIDDEN_DECLS
65
66struct bio_method_st {
67 int type;
68 const char *name;
69 int (*bwrite)(BIO *, const char *, int);
70 int (*bread)(BIO *, char *, int);
71 int (*bputs)(BIO *, const char *);
72 int (*bgets)(BIO *, char *, int);
73 long (*ctrl)(BIO *, int, long, void *);
74 int (*create)(BIO *);
75 int (*destroy)(BIO *);
76 long (*callback_ctrl)(BIO *, int, BIO_info_cb *);
77} /* BIO_METHOD */;
78
79struct bio_st {
80 const BIO_METHOD *method;
81 BIO_callback_fn callback;
82 BIO_callback_fn_ex callback_ex;
83 char *cb_arg; /* first argument for the callback */
84
85 int init;
86 int shutdown;
87 int flags; /* extra storage */
88 int retry_reason;
89 int num;
90 void *ptr;
91 struct bio_st *next_bio; /* used by filter BIOs */
92 struct bio_st *prev_bio; /* used by filter BIOs */
93 int references;
94 unsigned long num_read;
95 unsigned long num_write;
96
97 CRYPTO_EX_DATA ex_data;
98} /* BIO */;
99
100typedef struct bio_f_buffer_ctx_struct {
101 /* Buffers are setup like this:
102 *
103 * <---------------------- size ----------------------->
104 * +---------------------------------------------------+
105 * | consumed | remaining | free space |
106 * +---------------------------------------------------+
107 * <-- off --><------- len ------->
108 */
109
110 /* BIO *bio; */ /* this is now in the BIO struct */
111 int ibuf_size; /* how big is the input buffer */
112 int obuf_size; /* how big is the output buffer */
113
114 char *ibuf; /* the char array */
115 int ibuf_len; /* how many bytes are in it */
116 int ibuf_off; /* write/read offset */
117
118 char *obuf; /* the char array */
119 int obuf_len; /* how many bytes are in it */
120 int obuf_off; /* write/read offset */
121} BIO_F_BUFFER_CTX;
122
123int BIO_vprintf(BIO *bio, const char *format, va_list args);
124
125__END_HIDDEN_DECLS
126
127#endif /* !HEADER_BIO_LOCAL_H */
diff --git a/src/lib/libcrypto/bio/bio_meth.c b/src/lib/libcrypto/bio/bio_meth.c
deleted file mode 100644
index 37f866a0c4..0000000000
--- a/src/lib/libcrypto/bio/bio_meth.c
+++ /dev/null
@@ -1,165 +0,0 @@
1/* $OpenBSD: bio_meth.c,v 1.9 2023/07/05 21:23:37 beck Exp $ */
2/*
3 * Copyright (c) 2018 Theo Buehler <tb@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <stdlib.h>
19
20#include <openssl/bio.h>
21
22#include "bio_local.h"
23
24BIO_METHOD *
25BIO_meth_new(int type, const char *name)
26{
27 BIO_METHOD *biom;
28
29 if ((biom = calloc(1, sizeof(*biom))) == NULL)
30 return NULL;
31
32 biom->type = type;
33 biom->name = name;
34
35 return biom;
36}
37LCRYPTO_ALIAS(BIO_meth_new);
38
39void
40BIO_meth_free(BIO_METHOD *biom)
41{
42 free(biom);
43}
44LCRYPTO_ALIAS(BIO_meth_free);
45
46int
47(*BIO_meth_get_write(const BIO_METHOD *biom))(BIO *, const char *, int)
48{
49 return biom->bwrite;
50}
51LCRYPTO_ALIAS(BIO_meth_get_write);
52
53int
54BIO_meth_set_write(BIO_METHOD *biom, int (*write)(BIO *, const char *, int))
55{
56 biom->bwrite = write;
57 return 1;
58}
59LCRYPTO_ALIAS(BIO_meth_set_write);
60
61int
62(*BIO_meth_get_read(const BIO_METHOD *biom))(BIO *, char *, int)
63{
64 return biom->bread;
65}
66LCRYPTO_ALIAS(BIO_meth_get_read);
67
68int
69BIO_meth_set_read(BIO_METHOD *biom, int (*read)(BIO *, char *, int))
70{
71 biom->bread = read;
72 return 1;
73}
74LCRYPTO_ALIAS(BIO_meth_set_read);
75
76int
77(*BIO_meth_get_puts(const BIO_METHOD *biom))(BIO *, const char *)
78{
79 return biom->bputs;
80}
81LCRYPTO_ALIAS(BIO_meth_get_puts);
82
83int
84BIO_meth_set_puts(BIO_METHOD *biom, int (*puts)(BIO *, const char *))
85{
86 biom->bputs = puts;
87 return 1;
88}
89LCRYPTO_ALIAS(BIO_meth_set_puts);
90
91int
92(*BIO_meth_get_gets(const BIO_METHOD *biom))(BIO *, char *, int)
93{
94 return biom->bgets;
95}
96LCRYPTO_ALIAS(BIO_meth_get_gets);
97
98int
99BIO_meth_set_gets(BIO_METHOD *biom, int (*gets)(BIO *, char *, int))
100{
101 biom->bgets = gets;
102 return 1;
103}
104LCRYPTO_ALIAS(BIO_meth_set_gets);
105
106long
107(*BIO_meth_get_ctrl(const BIO_METHOD *biom))(BIO *, int, long, void *)
108{
109 return biom->ctrl;
110}
111LCRYPTO_ALIAS(BIO_meth_get_ctrl);
112
113int
114BIO_meth_set_ctrl(BIO_METHOD *biom, long (*ctrl)(BIO *, int, long, void *))
115{
116 biom->ctrl = ctrl;
117 return 1;
118}
119LCRYPTO_ALIAS(BIO_meth_set_ctrl);
120
121int
122(*BIO_meth_get_create(const BIO_METHOD *biom))(BIO *)
123{
124 return biom->create;
125}
126LCRYPTO_ALIAS(BIO_meth_get_create);
127
128int
129BIO_meth_set_create(BIO_METHOD *biom, int (*create)(BIO *))
130{
131 biom->create = create;
132 return 1;
133}
134LCRYPTO_ALIAS(BIO_meth_set_create);
135
136int
137(*BIO_meth_get_destroy(const BIO_METHOD *biom))(BIO *)
138{
139 return biom->destroy;
140}
141LCRYPTO_ALIAS(BIO_meth_get_destroy);
142
143int
144BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy)(BIO *))
145{
146 biom->destroy = destroy;
147 return 1;
148}
149LCRYPTO_ALIAS(BIO_meth_set_destroy);
150
151long
152(*BIO_meth_get_callback_ctrl(const BIO_METHOD *biom))(BIO *, int, BIO_info_cb *)
153{
154 return biom->callback_ctrl;
155}
156LCRYPTO_ALIAS(BIO_meth_get_callback_ctrl);
157
158int
159BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
160 long (*callback_ctrl)(BIO *, int, BIO_info_cb *))
161{
162 biom->callback_ctrl = callback_ctrl;
163 return 1;
164}
165LCRYPTO_ALIAS(BIO_meth_set_callback_ctrl);
diff --git a/src/lib/libcrypto/bio/bss_acpt.c b/src/lib/libcrypto/bio/bss_acpt.c
deleted file mode 100644
index d74c710a7f..0000000000
--- a/src/lib/libcrypto/bio/bss_acpt.c
+++ /dev/null
@@ -1,456 +0,0 @@
1/* $OpenBSD: bss_acpt.c,v 1.31 2023/07/05 21:23:37 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <sys/socket.h>
60
61#include <errno.h>
62#include <stdio.h>
63#include <string.h>
64#include <unistd.h>
65
66#include <openssl/bio.h>
67#include <openssl/buffer.h>
68#include <openssl/err.h>
69
70#include "bio_local.h"
71
72#define SOCKET_PROTOCOL IPPROTO_TCP
73
74typedef struct bio_accept_st {
75 int state;
76 char *param_addr;
77
78 int accept_sock;
79 int accept_nbio;
80
81 char *addr;
82 int nbio;
83 /* If 0, it means normal, if 1, do a connect on bind failure,
84 * and if there is no-one listening, bind with SO_REUSEADDR.
85 * If 2, always use SO_REUSEADDR. */
86 int bind_mode;
87 BIO *bio_chain;
88} BIO_ACCEPT;
89
90static int acpt_write(BIO *h, const char *buf, int num);
91static int acpt_read(BIO *h, char *buf, int size);
92static int acpt_puts(BIO *h, const char *str);
93static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
94static int acpt_new(BIO *h);
95static int acpt_free(BIO *data);
96static int acpt_state(BIO *b, BIO_ACCEPT *c);
97static void acpt_close_socket(BIO *data);
98static BIO_ACCEPT *BIO_ACCEPT_new(void );
99static void BIO_ACCEPT_free(BIO_ACCEPT *a);
100
101#define ACPT_S_BEFORE 1
102#define ACPT_S_GET_ACCEPT_SOCKET 2
103#define ACPT_S_OK 3
104
105static const BIO_METHOD methods_acceptp = {
106 .type = BIO_TYPE_ACCEPT,
107 .name = "socket accept",
108 .bwrite = acpt_write,
109 .bread = acpt_read,
110 .bputs = acpt_puts,
111 .ctrl = acpt_ctrl,
112 .create = acpt_new,
113 .destroy = acpt_free
114};
115
116const BIO_METHOD *
117BIO_s_accept(void)
118{
119 return (&methods_acceptp);
120}
121LCRYPTO_ALIAS(BIO_s_accept);
122
123static int
124acpt_new(BIO *bi)
125{
126 BIO_ACCEPT *ba;
127
128 bi->init = 0;
129 bi->num = -1;
130 bi->flags = 0;
131 if ((ba = BIO_ACCEPT_new()) == NULL)
132 return (0);
133 bi->ptr = (char *)ba;
134 ba->state = ACPT_S_BEFORE;
135 bi->shutdown = 1;
136 return (1);
137}
138
139static BIO_ACCEPT *
140BIO_ACCEPT_new(void)
141{
142 BIO_ACCEPT *ret;
143
144 if ((ret = calloc(1, sizeof(BIO_ACCEPT))) == NULL)
145 return (NULL);
146 ret->accept_sock = -1;
147 ret->bind_mode = BIO_BIND_NORMAL;
148 return (ret);
149}
150
151static void
152BIO_ACCEPT_free(BIO_ACCEPT *a)
153{
154 if (a == NULL)
155 return;
156
157 free(a->param_addr);
158 free(a->addr);
159 BIO_free(a->bio_chain);
160 free(a);
161}
162
163static void
164acpt_close_socket(BIO *bio)
165{
166 BIO_ACCEPT *c;
167
168 c = (BIO_ACCEPT *)bio->ptr;
169 if (c->accept_sock != -1) {
170 shutdown(c->accept_sock, SHUT_RDWR);
171 close(c->accept_sock);
172 c->accept_sock = -1;
173 bio->num = -1;
174 }
175}
176
177static int
178acpt_free(BIO *a)
179{
180 BIO_ACCEPT *data;
181
182 if (a == NULL)
183 return (0);
184 data = (BIO_ACCEPT *)a->ptr;
185
186 if (a->shutdown) {
187 acpt_close_socket(a);
188 BIO_ACCEPT_free(data);
189 a->ptr = NULL;
190 a->flags = 0;
191 a->init = 0;
192 }
193 return (1);
194}
195
196static int
197acpt_state(BIO *b, BIO_ACCEPT *c)
198{
199 BIO *bio = NULL, *dbio;
200 int s = -1;
201 int i;
202
203again:
204 switch (c->state) {
205 case ACPT_S_BEFORE:
206 if (c->param_addr == NULL) {
207 BIOerror(BIO_R_NO_ACCEPT_PORT_SPECIFIED);
208 return (-1);
209 }
210 s = BIO_get_accept_socket(c->param_addr, c->bind_mode);
211 if (s == -1)
212 return (-1);
213
214 if (c->accept_nbio) {
215 if (!BIO_socket_nbio(s, 1)) {
216 close(s);
217 BIOerror(BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
218 return (-1);
219 }
220 }
221 c->accept_sock = s;
222 b->num = s;
223 c->state = ACPT_S_GET_ACCEPT_SOCKET;
224 return (1);
225 /* break; */
226 case ACPT_S_GET_ACCEPT_SOCKET:
227 if (b->next_bio != NULL) {
228 c->state = ACPT_S_OK;
229 goto again;
230 }
231 BIO_clear_retry_flags(b);
232 b->retry_reason = 0;
233 i = BIO_accept(c->accept_sock, &(c->addr));
234
235 /* -2 return means we should retry */
236 if (i == -2) {
237 BIO_set_retry_special(b);
238 b->retry_reason = BIO_RR_ACCEPT;
239 return -1;
240 }
241
242 if (i < 0)
243 return (i);
244
245 bio = BIO_new_socket(i, BIO_CLOSE);
246 if (bio == NULL)
247 goto err;
248
249 BIO_set_callback(bio, BIO_get_callback(b));
250 BIO_set_callback_arg(bio, BIO_get_callback_arg(b));
251
252 if (c->nbio) {
253 if (!BIO_socket_nbio(i, 1)) {
254 BIOerror(BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
255 goto err;
256 }
257 }
258
259 /* If the accept BIO has an bio_chain, we dup it and
260 * put the new socket at the end. */
261 if (c->bio_chain != NULL) {
262 if ((dbio = BIO_dup_chain(c->bio_chain)) == NULL)
263 goto err;
264 if (!BIO_push(dbio, bio)) goto err;
265 bio = dbio;
266 }
267 if (BIO_push(b, bio)
268 == NULL) goto err;
269
270 c->state = ACPT_S_OK;
271 return (1);
272
273err:
274 if (bio != NULL)
275 BIO_free(bio);
276 return (0);
277 /* break; */
278 case ACPT_S_OK:
279 if (b->next_bio == NULL) {
280 c->state = ACPT_S_GET_ACCEPT_SOCKET;
281 goto again;
282 }
283 return (1);
284 /* break; */
285 default:
286 return (0);
287 /* break; */
288 }
289}
290
291static int
292acpt_read(BIO *b, char *out, int outl)
293{
294 int ret = 0;
295 BIO_ACCEPT *data;
296
297 BIO_clear_retry_flags(b);
298 data = (BIO_ACCEPT *)b->ptr;
299
300 while (b->next_bio == NULL) {
301 ret = acpt_state(b, data);
302 if (ret <= 0)
303 return (ret);
304 }
305
306 ret = BIO_read(b->next_bio, out, outl);
307 BIO_copy_next_retry(b);
308 return (ret);
309}
310
311static int
312acpt_write(BIO *b, const char *in, int inl)
313{
314 int ret;
315 BIO_ACCEPT *data;
316
317 BIO_clear_retry_flags(b);
318 data = (BIO_ACCEPT *)b->ptr;
319
320 while (b->next_bio == NULL) {
321 ret = acpt_state(b, data);
322 if (ret <= 0)
323 return (ret);
324 }
325
326 ret = BIO_write(b->next_bio, in, inl);
327 BIO_copy_next_retry(b);
328 return (ret);
329}
330
331static long
332acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
333{
334 int *ip;
335 long ret = 1;
336 BIO_ACCEPT *data;
337 char **pp;
338
339 data = (BIO_ACCEPT *)b->ptr;
340
341 switch (cmd) {
342 case BIO_CTRL_RESET:
343 ret = 0;
344 data->state = ACPT_S_BEFORE;
345 acpt_close_socket(b);
346 b->flags = 0;
347 break;
348 case BIO_C_DO_STATE_MACHINE:
349 /* use this one to start the connection */
350 ret = (long)acpt_state(b, data);
351 break;
352 case BIO_C_SET_ACCEPT:
353 if (ptr != NULL) {
354 if (num == 0) {
355 b->init = 1;
356 free(data->param_addr);
357 data->param_addr = strdup(ptr);
358 } else if (num == 1) {
359 data->accept_nbio = (ptr != NULL);
360 } else if (num == 2) {
361 BIO_free(data->bio_chain);
362 data->bio_chain = (BIO *)ptr;
363 }
364 }
365 break;
366 case BIO_C_SET_NBIO:
367 data->nbio = (int)num;
368 break;
369 case BIO_C_SET_FD:
370 b->init = 1;
371 b->num= *((int *)ptr);
372 data->accept_sock = b->num;
373 data->state = ACPT_S_GET_ACCEPT_SOCKET;
374 b->shutdown = (int)num;
375 b->init = 1;
376 break;
377 case BIO_C_GET_FD:
378 if (b->init) {
379 ip = (int *)ptr;
380 if (ip != NULL)
381 *ip = data->accept_sock;
382 ret = data->accept_sock;
383 } else
384 ret = -1;
385 break;
386 case BIO_C_GET_ACCEPT:
387 if (b->init) {
388 if (ptr != NULL) {
389 pp = (char **)ptr;
390 *pp = data->param_addr;
391 } else
392 ret = -1;
393 } else
394 ret = -1;
395 break;
396 case BIO_CTRL_GET_CLOSE:
397 ret = b->shutdown;
398 break;
399 case BIO_CTRL_SET_CLOSE:
400 b->shutdown = (int)num;
401 break;
402 case BIO_CTRL_PENDING:
403 case BIO_CTRL_WPENDING:
404 ret = 0;
405 break;
406 case BIO_CTRL_FLUSH:
407 break;
408 case BIO_C_SET_BIND_MODE:
409 data->bind_mode = (int)num;
410 break;
411 case BIO_C_GET_BIND_MODE:
412 ret = (long)data->bind_mode;
413 break;
414 case BIO_CTRL_DUP:
415/* dbio=(BIO *)ptr;
416 if (data->param_port) EAY EAY
417 BIO_set_port(dbio,data->param_port);
418 if (data->param_hostname)
419 BIO_set_hostname(dbio,data->param_hostname);
420 BIO_set_nbio(dbio,data->nbio);
421*/
422 break;
423
424 default:
425 ret = 0;
426 break;
427 }
428 return (ret);
429}
430
431static int
432acpt_puts(BIO *bp, const char *str)
433{
434 int n, ret;
435
436 n = strlen(str);
437 ret = acpt_write(bp, str, n);
438 return (ret);
439}
440
441BIO *
442BIO_new_accept(const char *str)
443{
444 BIO *ret;
445
446 ret = BIO_new(BIO_s_accept());
447 if (ret == NULL)
448 return (NULL);
449 if (BIO_set_accept_port(ret, str))
450 return (ret);
451 else {
452 BIO_free(ret);
453 return (NULL);
454 }
455}
456LCRYPTO_ALIAS(BIO_new_accept);
diff --git a/src/lib/libcrypto/bio/bss_bio.c b/src/lib/libcrypto/bio/bss_bio.c
deleted file mode 100644
index 39d8d1e46c..0000000000
--- a/src/lib/libcrypto/bio/bss_bio.c
+++ /dev/null
@@ -1,641 +0,0 @@
1/* $OpenBSD: bss_bio.c,v 1.29 2024/07/09 06:14:59 beck Exp $ */
2/* ====================================================================
3 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56/* Special method for a BIO where the other endpoint is also a BIO
57 * of this kind, handled by the same thread (i.e. the "peer" is actually
58 * ourselves, wearing a different hat).
59 * Such "BIO pairs" are mainly for using the SSL library with I/O interfaces
60 * for which no specific BIO method is available.
61 * See ssl/ssltest.c for some hints on how this can be used. */
62
63/* BIO_DEBUG implies BIO_PAIR_DEBUG */
64#ifdef BIO_DEBUG
65# ifndef BIO_PAIR_DEBUG
66# define BIO_PAIR_DEBUG
67# endif
68#endif
69
70/* disable assert() unless BIO_PAIR_DEBUG has been defined */
71#ifndef BIO_PAIR_DEBUG
72# ifndef NDEBUG
73# define NDEBUG
74# endif
75#endif
76
77#include <assert.h>
78#include <limits.h>
79#include <stdlib.h>
80#include <string.h>
81#include <sys/types.h>
82
83#include <openssl/bio.h>
84#include <openssl/err.h>
85#include <openssl/crypto.h>
86
87#include "bio_local.h"
88
89static int bio_new(BIO *bio);
90static int bio_free(BIO *bio);
91static int bio_read(BIO *bio, char *buf, int size);
92static int bio_write(BIO *bio, const char *buf, int num);
93static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
94static int bio_puts(BIO *bio, const char *str);
95
96static int bio_make_pair(BIO *bio1, BIO *bio2);
97static void bio_destroy_pair(BIO *bio);
98
99static const BIO_METHOD methods_biop = {
100 .type = BIO_TYPE_BIO,
101 .name = "BIO pair",
102 .bwrite = bio_write,
103 .bread = bio_read,
104 .bputs = bio_puts,
105 .ctrl = bio_ctrl,
106 .create = bio_new,
107 .destroy = bio_free
108};
109
110const BIO_METHOD *
111BIO_s_bio(void)
112{
113 return &methods_biop;
114}
115LCRYPTO_ALIAS(BIO_s_bio);
116
117struct bio_bio_st {
118 BIO *peer; /* NULL if buf == NULL.
119 * If peer != NULL, then peer->ptr is also a bio_bio_st,
120 * and its "peer" member points back to us.
121 * peer != NULL iff init != 0 in the BIO. */
122
123 /* This is for what we write (i.e. reading uses peer's struct): */
124 int closed; /* valid iff peer != NULL */
125 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
126 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
127 size_t size;
128 char *buf; /* "size" elements (if != NULL) */
129
130 size_t request; /* valid iff peer != NULL; 0 if len != 0,
131 * otherwise set by peer to number of bytes
132 * it (unsuccessfully) tried to read,
133 * never more than buffer space (size-len) warrants. */
134};
135
136static int
137bio_new(BIO *bio)
138{
139 struct bio_bio_st *b;
140
141 b = malloc(sizeof *b);
142 if (b == NULL)
143 return 0;
144
145 b->peer = NULL;
146 b->size = 17 * 1024; /* enough for one TLS record (just a default) */
147 b->buf = NULL;
148
149 bio->ptr = b;
150 return 1;
151}
152
153static int
154bio_free(BIO *bio)
155{
156 struct bio_bio_st *b;
157
158 if (bio == NULL)
159 return 0;
160 b = bio->ptr;
161
162 assert(b != NULL);
163
164 if (b->peer)
165 bio_destroy_pair(bio);
166
167 free(b->buf);
168 free(b);
169 return 1;
170}
171
172
173
174static int
175bio_read(BIO *bio, char *buf, int size_)
176{
177 size_t size = size_;
178 size_t rest;
179 struct bio_bio_st *b, *peer_b;
180
181 BIO_clear_retry_flags(bio);
182
183 if (!bio->init)
184 return 0;
185
186 b = bio->ptr;
187 assert(b != NULL);
188 assert(b->peer != NULL);
189 peer_b = b->peer->ptr;
190 assert(peer_b != NULL);
191 assert(peer_b->buf != NULL);
192
193 peer_b->request = 0; /* will be set in "retry_read" situation */
194
195 if (buf == NULL || size == 0)
196 return 0;
197
198 if (peer_b->len == 0) {
199 if (peer_b->closed)
200 return 0; /* writer has closed, and no data is left */
201 else {
202 BIO_set_retry_read(bio); /* buffer is empty */
203 if (size <= peer_b->size)
204 peer_b->request = size;
205 else
206 /* don't ask for more than the peer can
207 * deliver in one write */
208 peer_b->request = peer_b->size;
209 return -1;
210 }
211 }
212
213 /* we can read */
214 if (peer_b->len < size)
215 size = peer_b->len;
216
217 /* now read "size" bytes */
218
219 rest = size;
220
221 assert(rest > 0);
222 do /* one or two iterations */
223 {
224 size_t chunk;
225
226 assert(rest <= peer_b->len);
227 if (peer_b->offset + rest <= peer_b->size)
228 chunk = rest;
229 else
230 /* wrap around ring buffer */
231 chunk = peer_b->size - peer_b->offset;
232 assert(peer_b->offset + chunk <= peer_b->size);
233
234 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
235
236 peer_b->len -= chunk;
237 if (peer_b->len) {
238 peer_b->offset += chunk;
239 assert(peer_b->offset <= peer_b->size);
240 if (peer_b->offset == peer_b->size)
241 peer_b->offset = 0;
242 buf += chunk;
243 } else {
244 /* buffer now empty, no need to advance "buf" */
245 assert(chunk == rest);
246 peer_b->offset = 0;
247 }
248 rest -= chunk;
249 } while (rest);
250
251 return size;
252}
253
254static int
255bio_write(BIO *bio, const char *buf, int num_)
256{
257 size_t num = num_;
258 size_t rest;
259 struct bio_bio_st *b;
260
261 BIO_clear_retry_flags(bio);
262
263 if (!bio->init || buf == NULL || num == 0)
264 return 0;
265
266 b = bio->ptr;
267
268 assert(b != NULL);
269 assert(b->peer != NULL);
270 assert(b->buf != NULL);
271
272 b->request = 0;
273 if (b->closed) {
274 /* we already closed */
275 BIOerror(BIO_R_BROKEN_PIPE);
276 return -1;
277 }
278
279 assert(b->len <= b->size);
280
281 if (b->len == b->size) {
282 BIO_set_retry_write(bio); /* buffer is full */
283 return -1;
284 }
285
286 /* we can write */
287 if (num > b->size - b->len)
288 num = b->size - b->len;
289
290 /* now write "num" bytes */
291
292 rest = num;
293
294 assert(rest > 0);
295 do /* one or two iterations */
296 {
297 size_t write_offset;
298 size_t chunk;
299
300 assert(b->len + rest <= b->size);
301
302 write_offset = b->offset + b->len;
303 if (write_offset >= b->size)
304 write_offset -= b->size;
305 /* b->buf[write_offset] is the first byte we can write to. */
306
307 if (write_offset + rest <= b->size)
308 chunk = rest;
309 else
310 /* wrap around ring buffer */
311 chunk = b->size - write_offset;
312
313 memcpy(b->buf + write_offset, buf, chunk);
314
315 b->len += chunk;
316
317 assert(b->len <= b->size);
318
319 rest -= chunk;
320 buf += chunk;
321 } while (rest);
322
323 return num;
324}
325
326static long
327bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
328{
329 long ret;
330 struct bio_bio_st *b = bio->ptr;
331
332 assert(b != NULL);
333
334 switch (cmd) {
335 /* specific CTRL codes */
336
337 case BIO_C_SET_WRITE_BUF_SIZE:
338 if (b->peer) {
339 BIOerror(BIO_R_IN_USE);
340 ret = 0;
341 } else if (num == 0) {
342 BIOerror(BIO_R_INVALID_ARGUMENT);
343 ret = 0;
344 } else {
345 size_t new_size = num;
346
347 if (b->size != new_size) {
348 free(b->buf);
349 b->buf = NULL;
350 b->size = new_size;
351 }
352 ret = 1;
353 }
354 break;
355
356 case BIO_C_GET_WRITE_BUF_SIZE:
357 ret = (long) b->size;
358 break;
359
360 case BIO_C_MAKE_BIO_PAIR:
361 {
362 BIO *other_bio = ptr;
363
364 if (bio_make_pair(bio, other_bio))
365 ret = 1;
366 else
367 ret = 0;
368 }
369 break;
370
371 case BIO_C_DESTROY_BIO_PAIR:
372 /* Affects both BIOs in the pair -- call just once!
373 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
374 bio_destroy_pair(bio);
375 ret = 1;
376 break;
377
378 case BIO_C_GET_WRITE_GUARANTEE:
379 /* How many bytes can the caller feed to the next write
380 * without having to keep any? */
381 if (b->peer == NULL || b->closed)
382 ret = 0;
383 else
384 ret = (long) b->size - b->len;
385 break;
386
387 case BIO_C_GET_READ_REQUEST:
388 /* If the peer unsuccessfully tried to read, how many bytes
389 * were requested? (As with BIO_CTRL_PENDING, that number
390 * can usually be treated as boolean.) */
391 ret = (long) b->request;
392 break;
393
394 case BIO_C_RESET_READ_REQUEST:
395 /* Reset request. (Can be useful after read attempts
396 * at the other side that are meant to be non-blocking,
397 * e.g. when probing SSL_read to see if any data is
398 * available.) */
399 b->request = 0;
400 ret = 1;
401 break;
402
403 case BIO_C_SHUTDOWN_WR:
404 /* similar to shutdown(..., SHUT_WR) */
405 b->closed = 1;
406 ret = 1;
407 break;
408
409 /* standard CTRL codes follow */
410
411 case BIO_CTRL_RESET:
412 if (b->buf != NULL) {
413 b->len = 0;
414 b->offset = 0;
415 }
416 ret = 0;
417 break;
418
419
420 case BIO_CTRL_GET_CLOSE:
421 ret = bio->shutdown;
422 break;
423
424 case BIO_CTRL_SET_CLOSE:
425 bio->shutdown = (int) num;
426 ret = 1;
427 break;
428
429 case BIO_CTRL_PENDING:
430 if (b->peer != NULL) {
431 struct bio_bio_st *peer_b = b->peer->ptr;
432
433 ret = (long) peer_b->len;
434 } else
435 ret = 0;
436 break;
437
438 case BIO_CTRL_WPENDING:
439 if (b->buf != NULL)
440 ret = (long) b->len;
441 else
442 ret = 0;
443 break;
444
445 case BIO_CTRL_DUP:
446 /* See BIO_dup_chain for circumstances we have to expect. */
447 {
448 BIO *other_bio = ptr;
449 struct bio_bio_st *other_b;
450
451 assert(other_bio != NULL);
452 other_b = other_bio->ptr;
453 assert(other_b != NULL);
454
455 assert(other_b->buf == NULL); /* other_bio is always fresh */
456
457 other_b->size = b->size;
458 }
459
460 ret = 1;
461 break;
462
463 case BIO_CTRL_FLUSH:
464 ret = 1;
465 break;
466
467 case BIO_CTRL_EOF:
468 {
469 BIO *other_bio = ptr;
470
471 if (other_bio) {
472 struct bio_bio_st *other_b = other_bio->ptr;
473
474 assert(other_b != NULL);
475 ret = other_b->len == 0 && other_b->closed;
476 } else
477 ret = 1;
478 }
479 break;
480
481 default:
482 ret = 0;
483 }
484 return ret;
485}
486
487static int
488bio_puts(BIO *bio, const char *str)
489{
490 return bio_write(bio, str, strlen(str));
491}
492
493
494static int
495bio_make_pair(BIO *bio1, BIO *bio2)
496{
497 struct bio_bio_st *b1, *b2;
498
499 assert(bio1 != NULL);
500 assert(bio2 != NULL);
501
502 b1 = bio1->ptr;
503 b2 = bio2->ptr;
504
505 if (b1->peer != NULL || b2->peer != NULL) {
506 BIOerror(BIO_R_IN_USE);
507 return 0;
508 }
509
510 if (b1->buf == NULL) {
511 b1->buf = malloc(b1->size);
512 if (b1->buf == NULL) {
513 BIOerror(ERR_R_MALLOC_FAILURE);
514 return 0;
515 }
516 b1->len = 0;
517 b1->offset = 0;
518 }
519
520 if (b2->buf == NULL) {
521 b2->buf = malloc(b2->size);
522 if (b2->buf == NULL) {
523 BIOerror(ERR_R_MALLOC_FAILURE);
524 return 0;
525 }
526 b2->len = 0;
527 b2->offset = 0;
528 }
529
530 b1->peer = bio2;
531 b1->closed = 0;
532 b1->request = 0;
533 b2->peer = bio1;
534 b2->closed = 0;
535 b2->request = 0;
536
537 bio1->init = 1;
538 bio2->init = 1;
539
540 return 1;
541}
542
543static void
544bio_destroy_pair(BIO *bio)
545{
546 struct bio_bio_st *b = bio->ptr;
547
548 if (b != NULL) {
549 BIO *peer_bio = b->peer;
550
551 if (peer_bio != NULL) {
552 struct bio_bio_st *peer_b = peer_bio->ptr;
553
554 assert(peer_b != NULL);
555 assert(peer_b->peer == bio);
556
557 peer_b->peer = NULL;
558 peer_bio->init = 0;
559 assert(peer_b->buf != NULL);
560 peer_b->len = 0;
561 peer_b->offset = 0;
562
563 b->peer = NULL;
564 bio->init = 0;
565 assert(b->buf != NULL);
566 b->len = 0;
567 b->offset = 0;
568 }
569 }
570}
571
572
573/* Exported convenience functions */
574int
575BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1, BIO **bio2_p, size_t writebuf2)
576{
577 BIO *bio1 = NULL, *bio2 = NULL;
578 long r;
579 int ret = 0;
580
581 bio1 = BIO_new(BIO_s_bio());
582 if (bio1 == NULL)
583 goto err;
584 bio2 = BIO_new(BIO_s_bio());
585 if (bio2 == NULL)
586 goto err;
587
588 if (writebuf1) {
589 r = BIO_set_write_buf_size(bio1, writebuf1);
590 if (!r)
591 goto err;
592 }
593 if (writebuf2) {
594 r = BIO_set_write_buf_size(bio2, writebuf2);
595 if (!r)
596 goto err;
597 }
598
599 r = BIO_make_bio_pair(bio1, bio2);
600 if (!r)
601 goto err;
602 ret = 1;
603
604 err:
605 if (ret == 0) {
606 if (bio1) {
607 BIO_free(bio1);
608 bio1 = NULL;
609 }
610 if (bio2) {
611 BIO_free(bio2);
612 bio2 = NULL;
613 }
614 }
615
616 *bio1_p = bio1;
617 *bio2_p = bio2;
618 return ret;
619}
620LCRYPTO_ALIAS(BIO_new_bio_pair);
621
622size_t
623BIO_ctrl_get_write_guarantee(BIO *bio)
624{
625 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
626}
627LCRYPTO_ALIAS(BIO_ctrl_get_write_guarantee);
628
629size_t
630BIO_ctrl_get_read_request(BIO *bio)
631{
632 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
633}
634LCRYPTO_ALIAS(BIO_ctrl_get_read_request);
635
636int
637BIO_ctrl_reset_read_request(BIO *bio)
638{
639 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
640}
641LCRYPTO_ALIAS(BIO_ctrl_reset_read_request);
diff --git a/src/lib/libcrypto/bio/bss_conn.c b/src/lib/libcrypto/bio/bss_conn.c
deleted file mode 100644
index 3b0e3d3bdd..0000000000
--- a/src/lib/libcrypto/bio/bss_conn.c
+++ /dev/null
@@ -1,598 +0,0 @@
1/* $OpenBSD: bss_conn.c,v 1.41 2024/04/19 09:54:36 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <sys/socket.h>
60
61#include <netinet/in.h>
62
63#include <errno.h>
64#include <netdb.h>
65#include <stdio.h>
66#include <string.h>
67#include <unistd.h>
68
69#include <openssl/bio.h>
70#include <openssl/buffer.h>
71#include <openssl/err.h>
72
73#include "bio_local.h"
74
75#define SOCKET_PROTOCOL IPPROTO_TCP
76
77typedef struct bio_connect_st {
78 int state;
79
80 char *param_hostname;
81 char *param_port;
82 int nbio;
83
84 unsigned char ip[4];
85 unsigned short port;
86
87 struct sockaddr_in them;
88
89 /* int socket; this will be kept in bio->num so that it is
90 * compatible with the bss_sock bio */
91
92 /* called when the connection is initially made
93 * callback(BIO,state,ret); The callback should return
94 * 'ret'. state is for compatibility with the ssl info_callback */
95 BIO_info_cb *info_callback;
96} BIO_CONNECT;
97
98static int conn_write(BIO *h, const char *buf, int num);
99static int conn_read(BIO *h, char *buf, int size);
100static int conn_puts(BIO *h, const char *str);
101static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
102static int conn_new(BIO *h);
103static int conn_free(BIO *data);
104static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *);
105
106static int conn_state(BIO *b, BIO_CONNECT *c);
107static void conn_close_socket(BIO *data);
108static BIO_CONNECT *BIO_CONNECT_new(void);
109static void BIO_CONNECT_free(BIO_CONNECT *a);
110
111static const BIO_METHOD methods_connectp = {
112 .type = BIO_TYPE_CONNECT,
113 .name = "socket connect",
114 .bwrite = conn_write,
115 .bread = conn_read,
116 .bputs = conn_puts,
117 .ctrl = conn_ctrl,
118 .create = conn_new,
119 .destroy = conn_free,
120 .callback_ctrl = conn_callback_ctrl
121};
122
123static int
124conn_state(BIO *b, BIO_CONNECT *c)
125{
126 int ret = -1, i;
127 unsigned long l;
128 char *p, *q;
129 BIO_info_cb *cb = NULL;
130
131 if (c->info_callback != NULL)
132 cb = c->info_callback;
133
134 for (;;) {
135 switch (c->state) {
136 case BIO_CONN_S_BEFORE:
137 p = c->param_hostname;
138 if (p == NULL) {
139 BIOerror(BIO_R_NO_HOSTNAME_SPECIFIED);
140 goto exit_loop;
141 }
142 for (; *p != '\0'; p++) {
143 if ((*p == ':') || (*p == '/'))
144 break;
145 }
146
147 i= *p;
148 if ((i == ':') || (i == '/')) {
149 *(p++) = '\0';
150 if (i == ':') {
151 for (q = p; *q; q++)
152 if (*q == '/') {
153 *q = '\0';
154 break;
155 }
156 free(c->param_port);
157 c->param_port = strdup(p);
158 }
159 }
160
161 if (c->param_port == NULL) {
162 BIOerror(BIO_R_NO_PORT_SPECIFIED);
163 ERR_asprintf_error_data("host=%s",
164 c->param_hostname);
165 goto exit_loop;
166 }
167 c->state = BIO_CONN_S_GET_IP;
168 break;
169
170 case BIO_CONN_S_GET_IP:
171 if (BIO_get_host_ip(c->param_hostname, &(c->ip[0])) <= 0)
172 goto exit_loop;
173 c->state = BIO_CONN_S_GET_PORT;
174 break;
175
176 case BIO_CONN_S_GET_PORT:
177 if (c->param_port == NULL) {
178 /* abort(); */
179 goto exit_loop;
180 } else if (BIO_get_port(c->param_port, &c->port) <= 0)
181 goto exit_loop;
182 c->state = BIO_CONN_S_CREATE_SOCKET;
183 break;
184
185 case BIO_CONN_S_CREATE_SOCKET:
186 /* now setup address */
187 memset((char *)&c->them, 0, sizeof(c->them));
188 c->them.sin_family = AF_INET;
189 c->them.sin_port = htons((unsigned short)c->port);
190 l = (unsigned long)
191 ((unsigned long)c->ip[0] << 24L)|
192 ((unsigned long)c->ip[1] << 16L)|
193 ((unsigned long)c->ip[2] << 8L)|
194 ((unsigned long)c->ip[3]);
195 c->them.sin_addr.s_addr = htonl(l);
196 c->state = BIO_CONN_S_CREATE_SOCKET;
197
198 ret = socket(AF_INET, SOCK_STREAM, SOCKET_PROTOCOL);
199 if (ret == -1) {
200 SYSerror(errno);
201 ERR_asprintf_error_data("host=%s:%s",
202 c->param_hostname, c->param_port);
203 BIOerror(BIO_R_UNABLE_TO_CREATE_SOCKET);
204 goto exit_loop;
205 }
206 b->num = ret;
207 c->state = BIO_CONN_S_NBIO;
208 break;
209
210 case BIO_CONN_S_NBIO:
211 if (c->nbio) {
212 if (!BIO_socket_nbio(b->num, 1)) {
213 BIOerror(BIO_R_ERROR_SETTING_NBIO);
214 ERR_asprintf_error_data("host=%s:%s",
215 c->param_hostname, c->param_port);
216 goto exit_loop;
217 }
218 }
219 c->state = BIO_CONN_S_CONNECT;
220
221#if defined(SO_KEEPALIVE)
222 i = 1;
223 i = setsockopt(b->num, SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i));
224 if (i < 0) {
225 SYSerror(errno);
226 ERR_asprintf_error_data("host=%s:%s",
227 c->param_hostname, c->param_port);
228 BIOerror(BIO_R_KEEPALIVE);
229 goto exit_loop;
230 }
231#endif
232 break;
233
234 case BIO_CONN_S_CONNECT:
235 BIO_clear_retry_flags(b);
236 ret = connect(b->num,
237 (struct sockaddr *)&c->them,
238 sizeof(c->them));
239 b->retry_reason = 0;
240 if (ret < 0) {
241 if (BIO_sock_should_retry(ret)) {
242 BIO_set_retry_special(b);
243 c->state = BIO_CONN_S_BLOCKED_CONNECT;
244 b->retry_reason = BIO_RR_CONNECT;
245 } else {
246 SYSerror(errno);
247 ERR_asprintf_error_data("host=%s:%s",
248 c->param_hostname, c->param_port);
249 BIOerror(BIO_R_CONNECT_ERROR);
250 }
251 goto exit_loop;
252 } else
253 c->state = BIO_CONN_S_OK;
254 break;
255
256 case BIO_CONN_S_BLOCKED_CONNECT:
257 i = BIO_sock_error(b->num);
258 if (i) {
259 BIO_clear_retry_flags(b);
260 SYSerror(i);
261 ERR_asprintf_error_data("host=%s:%s",
262 c->param_hostname, c->param_port);
263 BIOerror(BIO_R_NBIO_CONNECT_ERROR);
264 ret = 0;
265 goto exit_loop;
266 } else
267 c->state = BIO_CONN_S_OK;
268 break;
269
270 case BIO_CONN_S_OK:
271 ret = 1;
272 goto exit_loop;
273 default:
274 /* abort(); */
275 goto exit_loop;
276 }
277
278 if (cb != NULL) {
279 if (!(ret = cb((BIO *)b, c->state, ret)))
280 goto end;
281 }
282 }
283
284 /* Loop does not exit */
285exit_loop:
286 if (cb != NULL)
287 ret = cb((BIO *)b, c->state, ret);
288end:
289 return (ret);
290}
291
292static BIO_CONNECT *
293BIO_CONNECT_new(void)
294{
295 BIO_CONNECT *ret;
296
297 if ((ret = malloc(sizeof(BIO_CONNECT))) == NULL)
298 return (NULL);
299 ret->state = BIO_CONN_S_BEFORE;
300 ret->param_hostname = NULL;
301 ret->param_port = NULL;
302 ret->info_callback = NULL;
303 ret->nbio = 0;
304 ret->ip[0] = 0;
305 ret->ip[1] = 0;
306 ret->ip[2] = 0;
307 ret->ip[3] = 0;
308 ret->port = 0;
309 memset((char *)&ret->them, 0, sizeof(ret->them));
310 return (ret);
311}
312
313static void
314BIO_CONNECT_free(BIO_CONNECT *a)
315{
316 if (a == NULL)
317 return;
318
319 free(a->param_hostname);
320 free(a->param_port);
321 free(a);
322}
323
324const BIO_METHOD *
325BIO_s_connect(void)
326{
327 return (&methods_connectp);
328}
329LCRYPTO_ALIAS(BIO_s_connect);
330
331static int
332conn_new(BIO *bi)
333{
334 bi->init = 0;
335 bi->num = -1;
336 bi->flags = 0;
337 if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
338 return (0);
339 else
340 return (1);
341}
342
343static void
344conn_close_socket(BIO *bio)
345{
346 BIO_CONNECT *c;
347
348 c = (BIO_CONNECT *)bio->ptr;
349 if (bio->num != -1) {
350 /* Only do a shutdown if things were established */
351 if (c->state == BIO_CONN_S_OK)
352 shutdown(bio->num, SHUT_RDWR);
353 close(bio->num);
354 bio->num = -1;
355 }
356}
357
358static int
359conn_free(BIO *a)
360{
361 BIO_CONNECT *data;
362
363 if (a == NULL)
364 return (0);
365 data = (BIO_CONNECT *)a->ptr;
366
367 if (a->shutdown) {
368 conn_close_socket(a);
369 BIO_CONNECT_free(data);
370 a->ptr = NULL;
371 a->flags = 0;
372 a->init = 0;
373 }
374 return (1);
375}
376
377static int
378conn_read(BIO *b, char *out, int outl)
379{
380 int ret = 0;
381 BIO_CONNECT *data;
382
383 data = (BIO_CONNECT *)b->ptr;
384 if (data->state != BIO_CONN_S_OK) {
385 ret = conn_state(b, data);
386 if (ret <= 0)
387 return (ret);
388 }
389
390 if (out != NULL) {
391 errno = 0;
392 ret = read(b->num, out, outl);
393 BIO_clear_retry_flags(b);
394 if (ret <= 0) {
395 if (BIO_sock_should_retry(ret))
396 BIO_set_retry_read(b);
397 }
398 }
399 return (ret);
400}
401
402static int
403conn_write(BIO *b, const char *in, int inl)
404{
405 int ret;
406 BIO_CONNECT *data;
407
408 data = (BIO_CONNECT *)b->ptr;
409 if (data->state != BIO_CONN_S_OK) {
410 ret = conn_state(b, data);
411 if (ret <= 0)
412 return (ret);
413 }
414
415 errno = 0;
416 ret = write(b->num, in, inl);
417 BIO_clear_retry_flags(b);
418 if (ret <= 0) {
419 if (BIO_sock_should_retry(ret))
420 BIO_set_retry_write(b);
421 }
422 return (ret);
423}
424
425static long
426conn_ctrl(BIO *b, int cmd, long num, void *ptr)
427{
428 BIO *dbio;
429 int *ip;
430 const char **pptr;
431 long ret = 1;
432 BIO_CONNECT *data;
433
434 data = (BIO_CONNECT *)b->ptr;
435
436 switch (cmd) {
437 case BIO_CTRL_RESET:
438 ret = 0;
439 data->state = BIO_CONN_S_BEFORE;
440 conn_close_socket(b);
441 b->flags = 0;
442 break;
443 case BIO_C_DO_STATE_MACHINE:
444 /* use this one to start the connection */
445 if (data->state != BIO_CONN_S_OK)
446 ret = (long)conn_state(b, data);
447 else
448 ret = 1;
449 break;
450 case BIO_C_GET_CONNECT:
451 if (ptr != NULL) {
452 pptr = (const char **)ptr;
453 if (num == 0) {
454 *pptr = data->param_hostname;
455
456 } else if (num == 1) {
457 *pptr = data->param_port;
458 } else if (num == 2) {
459 *pptr = (char *)&(data->ip[0]);
460 } else if (num == 3) {
461 *((int *)ptr) = data->port;
462 }
463 if ((!b->init) || (ptr == NULL))
464 *pptr = "not initialized";
465 ret = 1;
466 }
467 break;
468 case BIO_C_SET_CONNECT:
469 if (ptr != NULL) {
470 b->init = 1;
471 if (num == 0) {
472 free(data->param_hostname);
473 data->param_hostname = strdup(ptr);
474 } else if (num == 1) {
475 free(data->param_port);
476 data->param_port = strdup(ptr);
477 } else if (num == 2) {
478 unsigned char *p = ptr;
479 free(data->param_hostname);
480 if (asprintf(&data->param_hostname,
481 "%u.%u.%u.%u", p[0], p[1],
482 p[2], p[3]) == -1)
483 data->param_hostname = NULL;
484 memcpy(&(data->ip[0]), ptr, 4);
485 } else if (num == 3) {
486 free(data->param_port);
487 data->port= *(int *)ptr;
488 if (asprintf(&data->param_port, "%d",
489 data->port) == -1)
490 data->param_port = NULL;
491 }
492 }
493 break;
494 case BIO_C_SET_NBIO:
495 data->nbio = (int)num;
496 break;
497 case BIO_C_GET_FD:
498 if (b->init) {
499 ip = (int *)ptr;
500 if (ip != NULL)
501 *ip = b->num;
502 ret = b->num;
503 } else
504 ret = -1;
505 break;
506 case BIO_CTRL_GET_CLOSE:
507 ret = b->shutdown;
508 break;
509 case BIO_CTRL_SET_CLOSE:
510 b->shutdown = (int)num;
511 break;
512 case BIO_CTRL_PENDING:
513 case BIO_CTRL_WPENDING:
514 ret = 0;
515 break;
516 case BIO_CTRL_FLUSH:
517 break;
518 case BIO_CTRL_DUP:
519 {
520 dbio = (BIO *)ptr;
521 if (data->param_port)
522 BIO_set_conn_port(dbio, data->param_port);
523 if (data->param_hostname)
524 BIO_set_conn_hostname(dbio,
525 data->param_hostname);
526 BIO_set_nbio(dbio, data->nbio);
527 (void)BIO_set_info_callback(dbio, data->info_callback);
528 }
529 break;
530 case BIO_CTRL_SET_CALLBACK:
531 {
532#if 0 /* FIXME: Should this be used? -- Richard Levitte */
533 BIOerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
534 ret = -1;
535#else
536 ret = 0;
537#endif
538 }
539 break;
540 case BIO_CTRL_GET_CALLBACK:
541 {
542 BIO_info_cb **fptr = ptr;
543
544 *fptr = data->info_callback;
545 }
546 break;
547 default:
548 ret = 0;
549 break;
550 }
551 return (ret);
552}
553
554static long
555conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
556{
557 long ret = 1;
558 BIO_CONNECT *data;
559
560 data = (BIO_CONNECT *)b->ptr;
561
562 switch (cmd) {
563 case BIO_CTRL_SET_CALLBACK:
564 data->info_callback = (BIO_info_cb *)fp;
565 break;
566 default:
567 ret = 0;
568 break;
569 }
570 return (ret);
571}
572
573static int
574conn_puts(BIO *bp, const char *str)
575{
576 int n, ret;
577
578 n = strlen(str);
579 ret = conn_write(bp, str, n);
580 return (ret);
581}
582
583BIO *
584BIO_new_connect(const char *str)
585{
586 BIO *ret;
587
588 ret = BIO_new(BIO_s_connect());
589 if (ret == NULL)
590 return (NULL);
591 if (BIO_set_conn_hostname(ret, str))
592 return (ret);
593 else {
594 BIO_free(ret);
595 return (NULL);
596 }
597}
598LCRYPTO_ALIAS(BIO_new_connect);
diff --git a/src/lib/libcrypto/bio/bss_dgram.c b/src/lib/libcrypto/bio/bss_dgram.c
deleted file mode 100644
index 65a8f6fae4..0000000000
--- a/src/lib/libcrypto/bio/bss_dgram.c
+++ /dev/null
@@ -1,663 +0,0 @@
1/* $OpenBSD: bss_dgram.c,v 1.45 2023/07/05 21:23:37 beck Exp $ */
2/*
3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
5 */
6/* ====================================================================
7 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59
60#include <sys/socket.h>
61#include <sys/time.h>
62
63#include <netinet/in.h>
64
65#include <errno.h>
66#include <netdb.h>
67#include <stdio.h>
68#include <string.h>
69#include <unistd.h>
70
71#include <openssl/opensslconf.h>
72
73#include <openssl/bio.h>
74
75#include "bio_local.h"
76
77#ifndef OPENSSL_NO_DGRAM
78
79
80static int dgram_write(BIO *h, const char *buf, int num);
81static int dgram_read(BIO *h, char *buf, int size);
82static int dgram_puts(BIO *h, const char *str);
83static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
84static int dgram_new(BIO *h);
85static int dgram_free(BIO *data);
86static int dgram_clear(BIO *bio);
87
88
89static int BIO_dgram_should_retry(int s);
90
91static const BIO_METHOD methods_dgramp = {
92 .type = BIO_TYPE_DGRAM,
93 .name = "datagram socket",
94 .bwrite = dgram_write,
95 .bread = dgram_read,
96 .bputs = dgram_puts,
97 .ctrl = dgram_ctrl,
98 .create = dgram_new,
99 .destroy = dgram_free
100};
101
102
103typedef struct bio_dgram_data_st {
104 union {
105 struct sockaddr sa;
106 struct sockaddr_in sa_in;
107 struct sockaddr_in6 sa_in6;
108 } peer;
109 unsigned int connected;
110 unsigned int _errno;
111 unsigned int mtu;
112 struct timeval next_timeout;
113 struct timeval socket_timeout;
114} bio_dgram_data;
115
116
117const BIO_METHOD *
118BIO_s_datagram(void)
119{
120 return (&methods_dgramp);
121}
122LCRYPTO_ALIAS(BIO_s_datagram);
123
124BIO *
125BIO_new_dgram(int fd, int close_flag)
126{
127 BIO *ret;
128
129 ret = BIO_new(BIO_s_datagram());
130 if (ret == NULL)
131 return (NULL);
132 BIO_set_fd(ret, fd, close_flag);
133 return (ret);
134}
135LCRYPTO_ALIAS(BIO_new_dgram);
136
137static int
138dgram_new(BIO *bi)
139{
140 bio_dgram_data *data = NULL;
141
142 bi->init = 0;
143 bi->num = 0;
144 data = calloc(1, sizeof(bio_dgram_data));
145 if (data == NULL)
146 return 0;
147 bi->ptr = data;
148
149 bi->flags = 0;
150 return (1);
151}
152
153static int
154dgram_free(BIO *a)
155{
156 bio_dgram_data *data;
157
158 if (a == NULL)
159 return (0);
160 if (!dgram_clear(a))
161 return 0;
162
163 data = (bio_dgram_data *)a->ptr;
164 free(data);
165
166 return (1);
167}
168
169static int
170dgram_clear(BIO *a)
171{
172 if (a == NULL)
173 return (0);
174 if (a->shutdown) {
175 if (a->init) {
176 shutdown(a->num, SHUT_RDWR);
177 close(a->num);
178 }
179 a->init = 0;
180 a->flags = 0;
181 }
182 return (1);
183}
184
185static void
186dgram_adjust_rcv_timeout(BIO *b)
187{
188#if defined(SO_RCVTIMEO)
189 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
190
191 /* Is a timer active? */
192 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
193 struct timeval timenow, timeleft;
194
195 /* Read current socket timeout */
196 socklen_t sz = sizeof(data->socket_timeout);
197 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
198 &(data->socket_timeout), &sz) < 0) {
199 perror("getsockopt");
200 }
201
202 /* Get current time */
203 gettimeofday(&timenow, NULL);
204
205 /* Calculate time left until timer expires */
206 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
207 timeleft.tv_sec -= timenow.tv_sec;
208 timeleft.tv_usec -= timenow.tv_usec;
209 if (timeleft.tv_usec < 0) {
210 timeleft.tv_sec--;
211 timeleft.tv_usec += 1000000;
212 }
213
214 if (timeleft.tv_sec < 0) {
215 timeleft.tv_sec = 0;
216 timeleft.tv_usec = 1;
217 }
218
219 /* Adjust socket timeout if next handshake message timer
220 * will expire earlier.
221 */
222 if ((data->socket_timeout.tv_sec == 0 &&
223 data->socket_timeout.tv_usec == 0) ||
224 (data->socket_timeout.tv_sec > timeleft.tv_sec) ||
225 (data->socket_timeout.tv_sec == timeleft.tv_sec &&
226 data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
227 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
228 &timeleft, sizeof(struct timeval)) < 0) {
229 perror("setsockopt");
230 }
231 }
232 }
233#endif
234}
235
236static void
237dgram_reset_rcv_timeout(BIO *b)
238{
239#if defined(SO_RCVTIMEO)
240 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
241
242 /* Is a timer active? */
243 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
244 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
245 &(data->socket_timeout), sizeof(struct timeval)) < 0) {
246 perror("setsockopt");
247 }
248 }
249#endif
250}
251
252static int
253dgram_read(BIO *b, char *out, int outl)
254{
255 int ret = 0;
256 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
257
258 struct {
259 socklen_t len;
260 union {
261 struct sockaddr sa;
262 struct sockaddr_in sa_in;
263 struct sockaddr_in6 sa_in6;
264 } peer;
265 } sa;
266
267 sa.len = sizeof(sa.peer);
268
269 if (out != NULL) {
270 errno = 0;
271 memset(&sa.peer, 0, sizeof(sa.peer));
272 dgram_adjust_rcv_timeout(b);
273 ret = recvfrom(b->num, out, outl, 0, &sa.peer.sa, &sa.len);
274
275 if (! data->connected && ret >= 0)
276 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
277
278 BIO_clear_retry_flags(b);
279 if (ret < 0) {
280 if (BIO_dgram_should_retry(ret)) {
281 BIO_set_retry_read(b);
282 data->_errno = errno;
283 }
284 }
285
286 dgram_reset_rcv_timeout(b);
287 }
288 return (ret);
289}
290
291static int
292dgram_write(BIO *b, const char *in, int inl)
293{
294 int ret;
295 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
296 errno = 0;
297
298 if (data->connected)
299 ret = write(b->num, in, inl);
300 else {
301 int peerlen = sizeof(data->peer);
302
303 if (data->peer.sa.sa_family == AF_INET)
304 peerlen = sizeof(data->peer.sa_in);
305 else if (data->peer.sa.sa_family == AF_INET6)
306 peerlen = sizeof(data->peer.sa_in6);
307 ret = sendto(b->num, in, inl, 0, &data->peer.sa, peerlen);
308 }
309
310 BIO_clear_retry_flags(b);
311 if (ret <= 0) {
312 if (BIO_dgram_should_retry(ret)) {
313 BIO_set_retry_write(b);
314
315 data->_errno = errno;
316 /*
317 * higher layers are responsible for querying MTU,
318 * if necessary
319 */
320 }
321 }
322 return (ret);
323}
324
325static long
326dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
327{
328 long ret = 1;
329 int *ip;
330 struct sockaddr *to = NULL;
331 bio_dgram_data *data = NULL;
332#if (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
333 int sockopt_val = 0;
334 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
335 * modern enough to define socklen_t */
336 socklen_t addr_len;
337 union {
338 struct sockaddr sa;
339 struct sockaddr_in s4;
340 struct sockaddr_in6 s6;
341 } addr;
342#endif
343
344 data = (bio_dgram_data *)b->ptr;
345
346 switch (cmd) {
347 case BIO_CTRL_RESET:
348 num = 0;
349 case BIO_C_FILE_SEEK:
350 ret = 0;
351 break;
352 case BIO_C_FILE_TELL:
353 case BIO_CTRL_INFO:
354 ret = 0;
355 break;
356 case BIO_C_SET_FD:
357 dgram_clear(b);
358 b->num= *((int *)ptr);
359 b->shutdown = (int)num;
360 b->init = 1;
361 break;
362 case BIO_C_GET_FD:
363 if (b->init) {
364 ip = (int *)ptr;
365 if (ip != NULL)
366 *ip = b->num;
367 ret = b->num;
368 } else
369 ret = -1;
370 break;
371 case BIO_CTRL_GET_CLOSE:
372 ret = b->shutdown;
373 break;
374 case BIO_CTRL_SET_CLOSE:
375 b->shutdown = (int)num;
376 break;
377 case BIO_CTRL_PENDING:
378 case BIO_CTRL_WPENDING:
379 ret = 0;
380 break;
381 case BIO_CTRL_DUP:
382 case BIO_CTRL_FLUSH:
383 ret = 1;
384 break;
385 case BIO_CTRL_DGRAM_CONNECT:
386 to = (struct sockaddr *)ptr;
387 switch (to->sa_family) {
388 case AF_INET:
389 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
390 break;
391 case AF_INET6:
392 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
393 break;
394 default:
395 memcpy(&data->peer, to, sizeof(data->peer.sa));
396 break;
397 }
398 break;
399 /* (Linux)kernel sets DF bit on outgoing IP packets */
400 case BIO_CTRL_DGRAM_MTU_DISCOVER:
401#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
402 addr_len = (socklen_t)sizeof(addr);
403 memset((void *)&addr, 0, sizeof(addr));
404 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
405 ret = 0;
406 break;
407 }
408 switch (addr.sa.sa_family) {
409 case AF_INET:
410 sockopt_val = IP_PMTUDISC_DO;
411 ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
412 &sockopt_val, sizeof(sockopt_val));
413 if (ret < 0)
414 perror("setsockopt");
415 break;
416#if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
417 case AF_INET6:
418 sockopt_val = IPV6_PMTUDISC_DO;
419 ret = setsockopt(b->num, IPPROTO_IPV6,
420 IPV6_MTU_DISCOVER, &sockopt_val,
421 sizeof(sockopt_val));
422 if (ret < 0)
423 perror("setsockopt");
424 break;
425#endif
426 default:
427 ret = -1;
428 break;
429 }
430#else
431 ret = -1;
432#endif
433 break;
434 case BIO_CTRL_DGRAM_QUERY_MTU:
435#if defined(IP_MTU)
436 addr_len = (socklen_t)sizeof(addr);
437 memset((void *)&addr, 0, sizeof(addr));
438 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
439 ret = 0;
440 break;
441 }
442 sockopt_len = sizeof(sockopt_val);
443 switch (addr.sa.sa_family) {
444 case AF_INET:
445 ret = getsockopt(b->num, IPPROTO_IP, IP_MTU,
446 &sockopt_val, &sockopt_len);
447 if (ret < 0 || sockopt_val < 0) {
448 ret = 0;
449 } else {
450 /* we assume that the transport protocol is UDP and no
451 * IP options are used.
452 */
453 data->mtu = sockopt_val - 8 - 20;
454 ret = data->mtu;
455 }
456 break;
457#if defined(IPV6_MTU)
458 case AF_INET6:
459 ret = getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
460 &sockopt_val, &sockopt_len);
461 if (ret < 0 || sockopt_val < 0) {
462 ret = 0;
463 } else {
464 /* we assume that the transport protocol is UDP and no
465 * IPV6 options are used.
466 */
467 data->mtu = sockopt_val - 8 - 40;
468 ret = data->mtu;
469 }
470 break;
471#endif
472default:
473 ret = 0;
474 break;
475 }
476#else
477 ret = 0;
478#endif
479 break;
480 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
481 switch (data->peer.sa.sa_family) {
482 case AF_INET:
483 ret = 576 - 20 - 8;
484 break;
485 case AF_INET6:
486#ifdef IN6_IS_ADDR_V4MAPPED
487 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
488 ret = 576 - 20 - 8;
489 else
490#endif
491 ret = 1280 - 40 - 8;
492 break;
493 default:
494 ret = 576 - 20 - 8;
495 break;
496 }
497 break;
498 case BIO_CTRL_DGRAM_GET_MTU:
499 return data->mtu;
500 break;
501 case BIO_CTRL_DGRAM_SET_MTU:
502 data->mtu = num;
503 ret = num;
504 break;
505 case BIO_CTRL_DGRAM_SET_CONNECTED:
506 to = (struct sockaddr *)ptr;
507
508 if (to != NULL) {
509 data->connected = 1;
510 switch (to->sa_family) {
511 case AF_INET:
512 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
513 break;
514 case AF_INET6:
515 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
516 break;
517 default:
518 memcpy(&data->peer, to, sizeof(data->peer.sa));
519 break;
520 }
521 } else {
522 data->connected = 0;
523 memset(&(data->peer), 0, sizeof(data->peer));
524 }
525 break;
526 case BIO_CTRL_DGRAM_GET_PEER:
527 switch (data->peer.sa.sa_family) {
528 case AF_INET:
529 ret = sizeof(data->peer.sa_in);
530 break;
531 case AF_INET6:
532 ret = sizeof(data->peer.sa_in6);
533 break;
534 default:
535 ret = sizeof(data->peer.sa);
536 break;
537 }
538 if (num == 0 || num > ret)
539 num = ret;
540 memcpy(ptr, &data->peer, (ret = num));
541 break;
542 case BIO_CTRL_DGRAM_SET_PEER:
543 to = (struct sockaddr *) ptr;
544 switch (to->sa_family) {
545 case AF_INET:
546 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
547 break;
548 case AF_INET6:
549 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
550 break;
551 default:
552 memcpy(&data->peer, to, sizeof(data->peer.sa));
553 break;
554 }
555 break;
556 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
557 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
558 break;
559#if defined(SO_RCVTIMEO)
560 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
561 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
562 sizeof(struct timeval)) < 0) {
563 perror("setsockopt");
564 ret = -1;
565 }
566 break;
567 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
568 {
569 socklen_t sz = sizeof(struct timeval);
570 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
571 ptr, &sz) < 0) {
572 perror("getsockopt");
573 ret = -1;
574 } else
575 ret = sz;
576 }
577 break;
578#endif
579#if defined(SO_SNDTIMEO)
580 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
581 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
582 sizeof(struct timeval)) < 0) {
583 perror("setsockopt");
584 ret = -1;
585 }
586 break;
587 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
588 {
589 socklen_t sz = sizeof(struct timeval);
590 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
591 ptr, &sz) < 0) {
592 perror("getsockopt");
593 ret = -1;
594 } else
595 ret = sz;
596 }
597 break;
598#endif
599 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
600 /* fall-through */
601 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
602 if (data->_errno == EAGAIN) {
603 ret = 1;
604 data->_errno = 0;
605 } else
606 ret = 0;
607 break;
608#ifdef EMSGSIZE
609 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
610 if (data->_errno == EMSGSIZE) {
611 ret = 1;
612 data->_errno = 0;
613 } else
614 ret = 0;
615 break;
616#endif
617 default:
618 ret = 0;
619 break;
620 }
621 return (ret);
622}
623
624static int
625dgram_puts(BIO *bp, const char *str)
626{
627 int n, ret;
628
629 n = strlen(str);
630 ret = dgram_write(bp, str, n);
631 return (ret);
632}
633
634
635static int
636BIO_dgram_should_retry(int i)
637{
638 int err;
639
640 if ((i == 0) || (i == -1)) {
641 err = errno;
642 return (BIO_dgram_non_fatal_error(err));
643 }
644 return (0);
645}
646
647int
648BIO_dgram_non_fatal_error(int err)
649{
650 switch (err) {
651 case EINTR:
652 case EAGAIN:
653 case EINPROGRESS:
654 case EALREADY:
655 return (1);
656 default:
657 break;
658 }
659 return (0);
660}
661LCRYPTO_ALIAS(BIO_dgram_non_fatal_error);
662
663#endif
diff --git a/src/lib/libcrypto/bio/bss_fd.c b/src/lib/libcrypto/bio/bss_fd.c
deleted file mode 100644
index 63eac32329..0000000000
--- a/src/lib/libcrypto/bio/bss_fd.c
+++ /dev/null
@@ -1,273 +0,0 @@
1/* $OpenBSD: bss_fd.c,v 1.21 2023/07/05 21:23:37 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <errno.h>
60#include <stdio.h>
61#include <string.h>
62#include <unistd.h>
63
64#include <openssl/opensslconf.h>
65
66#include <openssl/bio.h>
67
68#include "bio_local.h"
69
70static int fd_write(BIO *h, const char *buf, int num);
71static int fd_read(BIO *h, char *buf, int size);
72static int fd_puts(BIO *h, const char *str);
73static int fd_gets(BIO *h, char *buf, int size);
74static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2);
75static int fd_new(BIO *h);
76static int fd_free(BIO *data);
77int BIO_fd_should_retry(int s);
78
79static const BIO_METHOD methods_fdp = {
80 .type = BIO_TYPE_FD,
81 .name = "file descriptor",
82 .bwrite = fd_write,
83 .bread = fd_read,
84 .bputs = fd_puts,
85 .bgets = fd_gets,
86 .ctrl = fd_ctrl,
87 .create = fd_new,
88 .destroy = fd_free
89};
90
91const BIO_METHOD *
92BIO_s_fd(void)
93{
94 return (&methods_fdp);
95}
96LCRYPTO_ALIAS(BIO_s_fd);
97
98BIO *
99BIO_new_fd(int fd, int close_flag)
100{
101 BIO *ret;
102 ret = BIO_new(BIO_s_fd());
103 if (ret == NULL)
104 return (NULL);
105 BIO_set_fd(ret, fd, close_flag);
106 return (ret);
107}
108LCRYPTO_ALIAS(BIO_new_fd);
109
110static int
111fd_new(BIO *bi)
112{
113 bi->init = 0;
114 bi->num = -1;
115 bi->ptr = NULL;
116 bi->flags=0;
117 return (1);
118}
119
120static int
121fd_free(BIO *a)
122{
123 if (a == NULL)
124 return (0);
125 if (a->shutdown) {
126 if (a->init) {
127 close(a->num);
128 }
129 a->init = 0;
130 a->flags = 0;
131 }
132 return (1);
133}
134
135static int
136fd_read(BIO *b, char *out, int outl)
137{
138 int ret = 0;
139
140 if (out != NULL) {
141 errno = 0;
142 ret = read(b->num, out, outl);
143 BIO_clear_retry_flags(b);
144 if (ret <= 0) {
145 if (BIO_fd_should_retry(ret))
146 BIO_set_retry_read(b);
147 }
148 }
149 return (ret);
150}
151
152static int
153fd_write(BIO *b, const char *in, int inl)
154{
155 int ret;
156 errno = 0;
157 ret = write(b->num, in, inl);
158 BIO_clear_retry_flags(b);
159 if (ret <= 0) {
160 if (BIO_fd_should_retry(ret))
161 BIO_set_retry_write(b);
162 }
163 return (ret);
164}
165
166static long
167fd_ctrl(BIO *b, int cmd, long num, void *ptr)
168{
169 long ret = 1;
170 int *ip;
171
172 switch (cmd) {
173 case BIO_CTRL_RESET:
174 num = 0;
175 case BIO_C_FILE_SEEK:
176 ret = (long)lseek(b->num, num, 0);
177 break;
178 case BIO_C_FILE_TELL:
179 case BIO_CTRL_INFO:
180 ret = (long)lseek(b->num, 0, 1);
181 break;
182 case BIO_C_SET_FD:
183 fd_free(b);
184 b->num= *((int *)ptr);
185 b->shutdown = (int)num;
186 b->init = 1;
187 break;
188 case BIO_C_GET_FD:
189 if (b->init) {
190 ip = (int *)ptr;
191 if (ip != NULL)
192 *ip = b->num;
193 ret = b->num;
194 } else
195 ret = -1;
196 break;
197 case BIO_CTRL_GET_CLOSE:
198 ret = b->shutdown;
199 break;
200 case BIO_CTRL_SET_CLOSE:
201 b->shutdown = (int)num;
202 break;
203 case BIO_CTRL_PENDING:
204 case BIO_CTRL_WPENDING:
205 ret = 0;
206 break;
207 case BIO_CTRL_DUP:
208 case BIO_CTRL_FLUSH:
209 ret = 1;
210 break;
211 default:
212 ret = 0;
213 break;
214 }
215 return (ret);
216}
217
218static int
219fd_puts(BIO *bp, const char *str)
220{
221 int n, ret;
222
223 n = strlen(str);
224 ret = fd_write(bp, str, n);
225 return (ret);
226}
227
228static int
229fd_gets(BIO *bp, char *buf, int size)
230{
231 int ret = 0;
232 char *ptr = buf;
233 char *end = buf + size - 1;
234
235 while ((ptr < end) && (fd_read(bp, ptr, 1) > 0) && (ptr[0] != '\n'))
236 ptr++;
237
238 ptr[0] = '\0';
239
240 if (buf[0] != '\0')
241 ret = strlen(buf);
242 return (ret);
243}
244
245int
246BIO_fd_should_retry(int i)
247{
248 int err;
249
250 if ((i == 0) || (i == -1)) {
251 err = errno;
252 return (BIO_fd_non_fatal_error(err));
253 }
254 return (0);
255}
256LCRYPTO_ALIAS(BIO_fd_should_retry);
257
258int
259BIO_fd_non_fatal_error(int err)
260{
261 switch (err) {
262 case ENOTCONN:
263 case EINTR:
264 case EAGAIN:
265 case EINPROGRESS:
266 case EALREADY:
267 return (1);
268 default:
269 break;
270 }
271 return (0);
272}
273LCRYPTO_ALIAS(BIO_fd_non_fatal_error);
diff --git a/src/lib/libcrypto/bio/bss_file.c b/src/lib/libcrypto/bio/bss_file.c
deleted file mode 100644
index 9b6ca2bdd8..0000000000
--- a/src/lib/libcrypto/bio/bss_file.c
+++ /dev/null
@@ -1,325 +0,0 @@
1/* $OpenBSD: bss_file.c,v 1.35 2023/07/05 21:23:37 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/*
60 * 03-Dec-1997 rdenny@dc3.com Fix bug preventing use of stdin/stdout
61 * with binary data (e.g. asn1parse -inform DER < xxx) under
62 * Windows
63 */
64
65#ifndef HEADER_BSS_FILE_C
66#define HEADER_BSS_FILE_C
67
68#if defined(__linux) || defined(__sun) || defined(__hpux)
69/* Following definition aliases fopen to fopen64 on above mentioned
70 * platforms. This makes it possible to open and sequentially access
71 * files larger than 2GB from 32-bit application. It does not allow to
72 * traverse them beyond 2GB with fseek/ftell, but on the other hand *no*
73 * 32-bit platform permits that, not with fseek/ftell. Not to mention
74 * that breaking 2GB limit for seeking would require surgery to *our*
75 * API. But sequential access suffices for practical cases when you
76 * can run into large files, such as fingerprinting, so we can let API
77 * alone. For reference, the list of 32-bit platforms which allow for
78 * sequential access of large files without extra "magic" comprise *BSD,
79 * Darwin, IRIX...
80 */
81#ifndef _FILE_OFFSET_BITS
82#define _FILE_OFFSET_BITS 64
83#endif
84#endif
85
86#include <errno.h>
87#include <stdio.h>
88#include <string.h>
89
90#include <openssl/bio.h>
91#include <openssl/err.h>
92
93#include "bio_local.h"
94
95static int file_write(BIO *h, const char *buf, int num);
96static int file_read(BIO *h, char *buf, int size);
97static int file_puts(BIO *h, const char *str);
98static int file_gets(BIO *h, char *str, int size);
99static long file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
100static int file_new(BIO *h);
101static int file_free(BIO *data);
102
103static const BIO_METHOD methods_filep = {
104 .type = BIO_TYPE_FILE,
105 .name = "FILE pointer",
106 .bwrite = file_write,
107 .bread = file_read,
108 .bputs = file_puts,
109 .bgets = file_gets,
110 .ctrl = file_ctrl,
111 .create = file_new,
112 .destroy = file_free
113};
114
115BIO *
116BIO_new_file(const char *filename, const char *mode)
117{
118 BIO *ret;
119 FILE *file = NULL;
120
121 file = fopen(filename, mode);
122
123 if (file == NULL) {
124 SYSerror(errno);
125 ERR_asprintf_error_data("fopen('%s', '%s')", filename, mode);
126 if (errno == ENOENT)
127 BIOerror(BIO_R_NO_SUCH_FILE);
128 else
129 BIOerror(ERR_R_SYS_LIB);
130 return (NULL);
131 }
132 if ((ret = BIO_new(BIO_s_file())) == NULL) {
133 fclose(file);
134 return (NULL);
135 }
136
137 BIO_set_fp(ret, file, BIO_CLOSE);
138 return (ret);
139}
140LCRYPTO_ALIAS(BIO_new_file);
141
142BIO *
143BIO_new_fp(FILE *stream, int close_flag)
144{
145 BIO *ret;
146
147 if ((ret = BIO_new(BIO_s_file())) == NULL)
148 return (NULL);
149
150 BIO_set_fp(ret, stream, close_flag);
151 return (ret);
152}
153LCRYPTO_ALIAS(BIO_new_fp);
154
155const BIO_METHOD *
156BIO_s_file(void)
157{
158 return (&methods_filep);
159}
160LCRYPTO_ALIAS(BIO_s_file);
161
162static int
163file_new(BIO *bi)
164{
165 bi->init = 0;
166 bi->num = 0;
167 bi->ptr = NULL;
168 bi->flags=0;
169 return (1);
170}
171
172static int
173file_free(BIO *a)
174{
175 if (a == NULL)
176 return (0);
177 if (a->shutdown) {
178 if ((a->init) && (a->ptr != NULL)) {
179 fclose (a->ptr);
180 a->ptr = NULL;
181 a->flags = 0;
182 }
183 a->init = 0;
184 }
185 return (1);
186}
187
188static int
189file_read(BIO *b, char *out, int outl)
190{
191 int ret = 0;
192
193 if (b->init && out != NULL) {
194 ret = fread(out, 1, outl, (FILE *)b->ptr);
195 if (ret == 0 && ferror((FILE *)b->ptr)) {
196 SYSerror(errno);
197 BIOerror(ERR_R_SYS_LIB);
198 ret = -1;
199 }
200 }
201 return (ret);
202}
203
204static int
205file_write(BIO *b, const char *in, int inl)
206{
207 int ret = 0;
208
209 if (b->init && in != NULL)
210 ret = fwrite(in, 1, inl, (FILE *)b->ptr);
211 return (ret);
212}
213
214static long
215file_ctrl(BIO *b, int cmd, long num, void *ptr)
216{
217 long ret = 1;
218 FILE *fp = (FILE *)b->ptr;
219 FILE **fpp;
220 char p[4];
221
222 switch (cmd) {
223 case BIO_C_FILE_SEEK:
224 case BIO_CTRL_RESET:
225 ret = (long)fseek(fp, num, 0);
226 break;
227 case BIO_CTRL_EOF:
228 ret = (long)feof(fp);
229 break;
230 case BIO_C_FILE_TELL:
231 case BIO_CTRL_INFO:
232 ret = ftell(fp);
233 break;
234 case BIO_C_SET_FILE_PTR:
235 file_free(b);
236 b->shutdown = (int)num&BIO_CLOSE;
237 b->ptr = ptr;
238 b->init = 1;
239 break;
240 case BIO_C_SET_FILENAME:
241 file_free(b);
242 b->shutdown = (int)num&BIO_CLOSE;
243 if (num & BIO_FP_APPEND) {
244 if (num & BIO_FP_READ)
245 strlcpy(p, "a+", sizeof p);
246 else strlcpy(p, "a", sizeof p);
247 } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
248 strlcpy(p, "r+", sizeof p);
249 else if (num & BIO_FP_WRITE)
250 strlcpy(p, "w", sizeof p);
251 else if (num & BIO_FP_READ)
252 strlcpy(p, "r", sizeof p);
253 else {
254 BIOerror(BIO_R_BAD_FOPEN_MODE);
255 ret = 0;
256 break;
257 }
258 fp = fopen(ptr, p);
259 if (fp == NULL) {
260 SYSerror(errno);
261 ERR_asprintf_error_data("fopen('%s', '%s')", ptr, p);
262 BIOerror(ERR_R_SYS_LIB);
263 ret = 0;
264 break;
265 }
266 b->ptr = fp;
267 b->init = 1;
268 break;
269 case BIO_C_GET_FILE_PTR:
270 /* the ptr parameter is actually a FILE ** in this case. */
271 if (ptr != NULL) {
272 fpp = (FILE **)ptr;
273 *fpp = (FILE *)b->ptr;
274 }
275 break;
276 case BIO_CTRL_GET_CLOSE:
277 ret = (long)b->shutdown;
278 break;
279 case BIO_CTRL_SET_CLOSE:
280 b->shutdown = (int)num;
281 break;
282 case BIO_CTRL_FLUSH:
283 fflush((FILE *)b->ptr);
284 break;
285 case BIO_CTRL_DUP:
286 ret = 1;
287 break;
288
289 case BIO_CTRL_WPENDING:
290 case BIO_CTRL_PENDING:
291 case BIO_CTRL_PUSH:
292 case BIO_CTRL_POP:
293 default:
294 ret = 0;
295 break;
296 }
297 return (ret);
298}
299
300static int
301file_gets(BIO *bp, char *buf, int size)
302{
303 int ret = 0;
304
305 buf[0] = '\0';
306 if (!fgets(buf, size,(FILE *)bp->ptr))
307 goto err;
308 if (buf[0] != '\0')
309 ret = strlen(buf);
310err:
311 return (ret);
312}
313
314static int
315file_puts(BIO *bp, const char *str)
316{
317 int n, ret;
318
319 n = strlen(str);
320 ret = file_write(bp, str, n);
321 return (ret);
322}
323
324
325#endif /* HEADER_BSS_FILE_C */
diff --git a/src/lib/libcrypto/bio/bss_log.c b/src/lib/libcrypto/bio/bss_log.c
deleted file mode 100644
index 9e2e882646..0000000000
--- a/src/lib/libcrypto/bio/bss_log.c
+++ /dev/null
@@ -1,216 +0,0 @@
1/* $OpenBSD: bss_log.c,v 1.24 2023/07/05 21:23:37 beck Exp $ */
2/* ====================================================================
3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * licensing@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56/*
57 Why BIO_s_log?
58
59 BIO_s_log is useful for system daemons (or services under NT).
60 It is one-way BIO, it sends all stuff to syslogd (on system that
61 commonly use that), or event log (on NT), or OPCOM (on OpenVMS).
62
63*/
64
65#include <errno.h>
66#include <stdio.h>
67#include <string.h>
68#include <syslog.h>
69
70#include <openssl/buffer.h>
71#include <openssl/err.h>
72
73#include "bio_local.h"
74
75#ifndef NO_SYSLOG
76
77static int slg_write(BIO *h, const char *buf, int num);
78static int slg_puts(BIO *h, const char *str);
79static long slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
80static int slg_new(BIO *h);
81static int slg_free(BIO *data);
82static void xopenlog(BIO* bp, char* name, int level);
83static void xsyslog(BIO* bp, int priority, const char* string);
84static void xcloselog(BIO* bp);
85
86static const BIO_METHOD methods_slg = {
87 .type = BIO_TYPE_MEM,
88 .name = "syslog",
89 .bwrite = slg_write,
90 .bputs = slg_puts,
91 .ctrl = slg_ctrl,
92 .create = slg_new,
93 .destroy = slg_free
94};
95
96const BIO_METHOD *
97BIO_s_log(void)
98{
99 return (&methods_slg);
100}
101LCRYPTO_ALIAS(BIO_s_log);
102
103static int
104slg_new(BIO *bi)
105{
106 bi->init = 1;
107 bi->num = 0;
108 bi->ptr = NULL;
109 xopenlog(bi, "application", LOG_DAEMON);
110 return (1);
111}
112
113static int
114slg_free(BIO *a)
115{
116 if (a == NULL)
117 return (0);
118 xcloselog(a);
119 return (1);
120}
121
122static int
123slg_write(BIO *b, const char *in, int inl)
124{
125 int ret = inl;
126 char* buf;
127 char* pp;
128 int priority, i;
129 static const struct {
130 int strl;
131 char str[10];
132 int log_level;
133 }
134 mapping[] = {
135 { 6, "PANIC ", LOG_EMERG },
136 { 6, "EMERG ", LOG_EMERG },
137 { 4, "EMR ", LOG_EMERG },
138 { 6, "ALERT ", LOG_ALERT },
139 { 4, "ALR ", LOG_ALERT },
140 { 5, "CRIT ", LOG_CRIT },
141 { 4, "CRI ", LOG_CRIT },
142 { 6, "ERROR ", LOG_ERR },
143 { 4, "ERR ", LOG_ERR },
144 { 8, "WARNING ", LOG_WARNING },
145 { 5, "WARN ", LOG_WARNING },
146 { 4, "WAR ", LOG_WARNING },
147 { 7, "NOTICE ", LOG_NOTICE },
148 { 5, "NOTE ", LOG_NOTICE },
149 { 4, "NOT ", LOG_NOTICE },
150 { 5, "INFO ", LOG_INFO },
151 { 4, "INF ", LOG_INFO },
152 { 6, "DEBUG ", LOG_DEBUG },
153 { 4, "DBG ", LOG_DEBUG },
154 { 0, "", LOG_ERR } /* The default */
155 };
156
157 if ((buf = malloc(inl + 1)) == NULL) {
158 return (0);
159 }
160 strlcpy(buf, in, inl + 1);
161 i = 0;
162 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
163 i++;
164 priority = mapping[i].log_level;
165 pp = buf + mapping[i].strl;
166
167 xsyslog(b, priority, pp);
168
169 free(buf);
170 return (ret);
171}
172
173static long
174slg_ctrl(BIO *b, int cmd, long num, void *ptr)
175{
176 switch (cmd) {
177 case BIO_CTRL_SET:
178 xcloselog(b);
179 xopenlog(b, ptr, num);
180 break;
181 default:
182 break;
183 }
184 return (0);
185}
186
187static int
188slg_puts(BIO *bp, const char *str)
189{
190 int n, ret;
191
192 n = strlen(str);
193 ret = slg_write(bp, str, n);
194 return (ret);
195}
196
197
198static void
199xopenlog(BIO* bp, char* name, int level)
200{
201 openlog(name, LOG_PID|LOG_CONS, level);
202}
203
204static void
205xsyslog(BIO *bp, int priority, const char *string)
206{
207 syslog(priority, "%s", string);
208}
209
210static void
211xcloselog(BIO* bp)
212{
213 closelog();
214}
215
216#endif /* NO_SYSLOG */
diff --git a/src/lib/libcrypto/bio/bss_mem.c b/src/lib/libcrypto/bio/bss_mem.c
deleted file mode 100644
index 6d0d54db84..0000000000
--- a/src/lib/libcrypto/bio/bss_mem.c
+++ /dev/null
@@ -1,367 +0,0 @@
1/* $OpenBSD: bss_mem.c,v 1.22 2023/07/05 21:23:37 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <errno.h>
60#include <limits.h>
61#include <stdio.h>
62#include <string.h>
63
64#include <openssl/bio.h>
65#include <openssl/err.h>
66#include <openssl/buffer.h>
67
68#include "bio_local.h"
69
70struct bio_mem {
71 BUF_MEM *buf;
72 size_t read_offset;
73};
74
75static size_t
76bio_mem_pending(struct bio_mem *bm)
77{
78 if (bm->read_offset > bm->buf->length)
79 return 0;
80
81 return bm->buf->length - bm->read_offset;
82}
83
84static uint8_t *
85bio_mem_read_ptr(struct bio_mem *bm)
86{
87 return &bm->buf->data[bm->read_offset];
88}
89
90static int mem_new(BIO *bio);
91static int mem_free(BIO *bio);
92static int mem_write(BIO *bio, const char *in, int in_len);
93static int mem_read(BIO *bio, char *out, int out_len);
94static int mem_puts(BIO *bio, const char *in);
95static int mem_gets(BIO *bio, char *out, int out_len);
96static long mem_ctrl(BIO *bio, int cmd, long arg1, void *arg2);
97
98static const BIO_METHOD mem_method = {
99 .type = BIO_TYPE_MEM,
100 .name = "memory buffer",
101 .bwrite = mem_write,
102 .bread = mem_read,
103 .bputs = mem_puts,
104 .bgets = mem_gets,
105 .ctrl = mem_ctrl,
106 .create = mem_new,
107 .destroy = mem_free
108};
109
110/*
111 * bio->num is used to hold the value to return on 'empty', if it is
112 * 0, should_retry is not set.
113 */
114
115const BIO_METHOD *
116BIO_s_mem(void)
117{
118 return &mem_method;
119}
120LCRYPTO_ALIAS(BIO_s_mem);
121
122BIO *
123BIO_new_mem_buf(const void *buf, int buf_len)
124{
125 struct bio_mem *bm;
126 BIO *bio;
127
128 if (buf == NULL) {
129 BIOerror(BIO_R_NULL_PARAMETER);
130 return NULL;
131 }
132 if (buf_len == -1)
133 buf_len = strlen(buf);
134 if (buf_len < 0) {
135 BIOerror(BIO_R_INVALID_ARGUMENT);
136 return NULL;
137 }
138
139 if ((bio = BIO_new(BIO_s_mem())) == NULL)
140 return NULL;
141
142 bm = bio->ptr;
143 bm->buf->data = (void *)buf; /* Trust in the BIO_FLAGS_MEM_RDONLY flag. */
144 bm->buf->length = buf_len;
145 bm->buf->max = buf_len;
146 bio->flags |= BIO_FLAGS_MEM_RDONLY;
147 /* Since this is static data retrying will not help. */
148 bio->num = 0;
149
150 return bio;
151}
152LCRYPTO_ALIAS(BIO_new_mem_buf);
153
154static int
155mem_new(BIO *bio)
156{
157 struct bio_mem *bm;
158
159 if ((bm = calloc(1, sizeof(*bm))) == NULL)
160 return 0;
161 if ((bm->buf = BUF_MEM_new()) == NULL) {
162 free(bm);
163 return 0;
164 }
165
166 bio->shutdown = 1;
167 bio->init = 1;
168 bio->num = -1;
169 bio->ptr = bm;
170
171 return 1;
172}
173
174static int
175mem_free(BIO *bio)
176{
177 struct bio_mem *bm;
178
179 if (bio == NULL)
180 return 0;
181 if (!bio->init || bio->ptr == NULL)
182 return 1;
183
184 bm = bio->ptr;
185 if (bio->shutdown) {
186 if (bio->flags & BIO_FLAGS_MEM_RDONLY)
187 bm->buf->data = NULL;
188 BUF_MEM_free(bm->buf);
189 }
190 free(bm);
191 bio->ptr = NULL;
192
193 return 1;
194}
195
196static int
197mem_read(BIO *bio, char *out, int out_len)
198{
199 struct bio_mem *bm = bio->ptr;
200
201 BIO_clear_retry_flags(bio);
202
203 if (out == NULL || out_len <= 0)
204 return 0;
205
206 if ((size_t)out_len > bio_mem_pending(bm))
207 out_len = bio_mem_pending(bm);
208
209 if (out_len == 0) {
210 if (bio->num != 0)
211 BIO_set_retry_read(bio);
212 return bio->num;
213 }
214
215 memcpy(out, bio_mem_read_ptr(bm), out_len);
216 bm->read_offset += out_len;
217
218 return out_len;
219}
220
221static int
222mem_write(BIO *bio, const char *in, int in_len)
223{
224 struct bio_mem *bm = bio->ptr;
225 size_t buf_len;
226
227 BIO_clear_retry_flags(bio);
228
229 if (in == NULL || in_len <= 0)
230 return 0;
231
232 if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
233 BIOerror(BIO_R_WRITE_TO_READ_ONLY_BIO);
234 return -1;
235 }
236
237 if (bm->read_offset > 4096) {
238 memmove(bm->buf->data, bio_mem_read_ptr(bm),
239 bio_mem_pending(bm));
240 bm->buf->length = bio_mem_pending(bm);
241 bm->read_offset = 0;
242 }
243
244 /*
245 * Check for overflow and ensure we do not exceed an int, otherwise we
246 * cannot tell if BUF_MEM_grow_clean() succeeded.
247 */
248 buf_len = bm->buf->length + in_len;
249 if (buf_len < bm->buf->length || buf_len > INT_MAX)
250 return -1;
251
252 if (BUF_MEM_grow_clean(bm->buf, buf_len) != buf_len)
253 return -1;
254
255 memcpy(&bm->buf->data[buf_len - in_len], in, in_len);
256
257 return in_len;
258}
259
260static long
261mem_ctrl(BIO *bio, int cmd, long num, void *ptr)
262{
263 struct bio_mem *bm = bio->ptr;
264 void **pptr;
265 long ret = 1;
266
267 switch (cmd) {
268 case BIO_CTRL_RESET:
269 if (bm->buf->data != NULL) {
270 if (!(bio->flags & BIO_FLAGS_MEM_RDONLY)) {
271 memset(bm->buf->data, 0, bm->buf->max);
272 bm->buf->length = 0;
273 }
274 bm->read_offset = 0;
275 }
276 break;
277 case BIO_CTRL_EOF:
278 ret = (long)(bio_mem_pending(bm) == 0);
279 break;
280 case BIO_C_SET_BUF_MEM_EOF_RETURN:
281 bio->num = (int)num;
282 break;
283 case BIO_CTRL_INFO:
284 if (ptr != NULL) {
285 pptr = (void **)ptr;
286 *pptr = bio_mem_read_ptr(bm);
287 }
288 ret = (long)bio_mem_pending(bm);
289 break;
290 case BIO_C_SET_BUF_MEM:
291 BUF_MEM_free(bm->buf);
292 bio->shutdown = (int)num;
293 bm->buf = ptr;
294 bm->read_offset = 0;
295 break;
296 case BIO_C_GET_BUF_MEM_PTR:
297 if (ptr != NULL) {
298 pptr = (void **)ptr;
299 *pptr = bm->buf;
300 }
301 break;
302 case BIO_CTRL_GET_CLOSE:
303 ret = (long)bio->shutdown;
304 break;
305 case BIO_CTRL_SET_CLOSE:
306 bio->shutdown = (int)num;
307 break;
308 case BIO_CTRL_WPENDING:
309 ret = 0L;
310 break;
311 case BIO_CTRL_PENDING:
312 ret = (long)bio_mem_pending(bm);
313 break;
314 case BIO_CTRL_DUP:
315 case BIO_CTRL_FLUSH:
316 ret = 1;
317 break;
318 case BIO_CTRL_PUSH:
319 case BIO_CTRL_POP:
320 default:
321 ret = 0;
322 break;
323 }
324 return ret;
325}
326
327static int
328mem_gets(BIO *bio, char *out, int out_len)
329{
330 struct bio_mem *bm = bio->ptr;
331 int i, out_max;
332 char *p;
333 int ret = -1;
334
335 BIO_clear_retry_flags(bio);
336
337 out_max = bio_mem_pending(bm);
338 if (out_len - 1 < out_max)
339 out_max = out_len - 1;
340 if (out_max <= 0) {
341 *out = '\0';
342 return 0;
343 }
344
345 p = bio_mem_read_ptr(bm);
346 for (i = 0; i < out_max; i++) {
347 if (p[i] == '\n') {
348 i++;
349 break;
350 }
351 }
352
353 /*
354 * i is now the max num of bytes to copy, either out_max or up to and
355 * including the first newline
356 */
357 if ((ret = mem_read(bio, out, i)) > 0)
358 out[ret] = '\0';
359
360 return ret;
361}
362
363static int
364mem_puts(BIO *bio, const char *in)
365{
366 return mem_write(bio, in, strlen(in));
367}
diff --git a/src/lib/libcrypto/bio/bss_null.c b/src/lib/libcrypto/bio/bss_null.c
deleted file mode 100644
index 5f9340967b..0000000000
--- a/src/lib/libcrypto/bio/bss_null.c
+++ /dev/null
@@ -1,161 +0,0 @@
1/* $OpenBSD: bss_null.c,v 1.13 2023/07/05 21:23:37 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <errno.h>
60#include <stdio.h>
61#include <string.h>
62
63#include <openssl/bio.h>
64
65#include "bio_local.h"
66
67static int null_write(BIO *h, const char *buf, int num);
68static int null_read(BIO *h, char *buf, int size);
69static int null_puts(BIO *h, const char *str);
70static int null_gets(BIO *h, char *str, int size);
71static long null_ctrl(BIO *h, int cmd, long arg1, void *arg2);
72static int null_new(BIO *h);
73static int null_free(BIO *data);
74
75static const BIO_METHOD null_method = {
76 .type = BIO_TYPE_NULL,
77 .name = "NULL",
78 .bwrite = null_write,
79 .bread = null_read,
80 .bputs = null_puts,
81 .bgets = null_gets,
82 .ctrl = null_ctrl,
83 .create = null_new,
84 .destroy = null_free
85};
86
87const BIO_METHOD *
88BIO_s_null(void)
89{
90 return (&null_method);
91}
92LCRYPTO_ALIAS(BIO_s_null);
93
94static int
95null_new(BIO *bi)
96{
97 bi->init = 1;
98 bi->num = 0;
99 bi->ptr = (NULL);
100 return (1);
101}
102
103static int
104null_free(BIO *a)
105{
106 if (a == NULL)
107 return (0);
108 return (1);
109}
110
111static int
112null_read(BIO *b, char *out, int outl)
113{
114 return (0);
115}
116
117static int
118null_write(BIO *b, const char *in, int inl)
119{
120 return (inl);
121}
122
123static long
124null_ctrl(BIO *b, int cmd, long num, void *ptr)
125{
126 long ret = 1;
127
128 switch (cmd) {
129 case BIO_CTRL_RESET:
130 case BIO_CTRL_EOF:
131 case BIO_CTRL_SET:
132 case BIO_CTRL_SET_CLOSE:
133 case BIO_CTRL_FLUSH:
134 case BIO_CTRL_DUP:
135 ret = 1;
136 break;
137 case BIO_CTRL_GET_CLOSE:
138 case BIO_CTRL_INFO:
139 case BIO_CTRL_GET:
140 case BIO_CTRL_PENDING:
141 case BIO_CTRL_WPENDING:
142 default:
143 ret = 0;
144 break;
145 }
146 return (ret);
147}
148
149static int
150null_gets(BIO *bp, char *buf, int size)
151{
152 return (0);
153}
154
155static int
156null_puts(BIO *bp, const char *str)
157{
158 if (str == NULL)
159 return (0);
160 return (strlen(str));
161}
diff --git a/src/lib/libcrypto/bio/bss_sock.c b/src/lib/libcrypto/bio/bss_sock.c
deleted file mode 100644
index 79194a7e5e..0000000000
--- a/src/lib/libcrypto/bio/bss_sock.c
+++ /dev/null
@@ -1,244 +0,0 @@
1/* $OpenBSD: bss_sock.c,v 1.27 2023/08/07 10:54:14 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <sys/socket.h>
60
61#include <errno.h>
62#include <stdio.h>
63#include <string.h>
64#include <unistd.h>
65
66#include <openssl/bio.h>
67
68#include "bio_local.h"
69
70static int sock_write(BIO *h, const char *buf, int num);
71static int sock_read(BIO *h, char *buf, int size);
72static int sock_puts(BIO *h, const char *str);
73static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
74static int sock_new(BIO *h);
75static int sock_free(BIO *data);
76int BIO_sock_should_retry(int s);
77
78static const BIO_METHOD methods_sockp = {
79 .type = BIO_TYPE_SOCKET,
80 .name = "socket",
81 .bwrite = sock_write,
82 .bread = sock_read,
83 .bputs = sock_puts,
84 .ctrl = sock_ctrl,
85 .create = sock_new,
86 .destroy = sock_free
87};
88
89const BIO_METHOD *
90BIO_s_socket(void)
91{
92 return (&methods_sockp);
93}
94LCRYPTO_ALIAS(BIO_s_socket);
95
96BIO *
97BIO_new_socket(int fd, int close_flag)
98{
99 BIO *ret;
100
101 ret = BIO_new(BIO_s_socket());
102 if (ret == NULL)
103 return (NULL);
104 BIO_set_fd(ret, fd, close_flag);
105 return (ret);
106}
107LCRYPTO_ALIAS(BIO_new_socket);
108
109static int
110sock_new(BIO *bi)
111{
112 bi->init = 0;
113 bi->num = 0;
114 bi->ptr = NULL;
115 bi->flags = 0;
116 return (1);
117}
118
119static int
120sock_free(BIO *a)
121{
122 if (a == NULL)
123 return (0);
124 if (a->shutdown) {
125 if (a->init) {
126 shutdown(a->num, SHUT_RDWR);
127 close(a->num);
128 }
129 a->init = 0;
130 a->flags = 0;
131 }
132 return (1);
133}
134
135static int
136sock_read(BIO *b, char *out, int outl)
137{
138 int ret = 0;
139
140 if (out != NULL) {
141 errno = 0;
142 ret = read(b->num, out, outl);
143 BIO_clear_retry_flags(b);
144 if (ret <= 0) {
145 if (BIO_sock_should_retry(ret))
146 BIO_set_retry_read(b);
147 }
148 }
149 return (ret);
150}
151
152static int
153sock_write(BIO *b, const char *in, int inl)
154{
155 int ret;
156
157 errno = 0;
158 ret = write(b->num, in, inl);
159 BIO_clear_retry_flags(b);
160 if (ret <= 0) {
161 if (BIO_sock_should_retry(ret))
162 BIO_set_retry_write(b);
163 }
164 return (ret);
165}
166
167static long
168sock_ctrl(BIO *b, int cmd, long num, void *ptr)
169{
170 long ret = 1;
171 int *ip;
172
173 switch (cmd) {
174 case BIO_C_SET_FD:
175 sock_free(b);
176 b->num = *((int *)ptr);
177 b->shutdown = (int)num;
178 b->init = 1;
179 break;
180 case BIO_C_GET_FD:
181 if (b->init) {
182 ip = (int *)ptr;
183 if (ip != NULL)
184 *ip = b->num;
185 ret = b->num;
186 } else
187 ret = -1;
188 break;
189 case BIO_CTRL_GET_CLOSE:
190 ret = b->shutdown;
191 break;
192 case BIO_CTRL_SET_CLOSE:
193 b->shutdown = (int)num;
194 break;
195 case BIO_CTRL_DUP:
196 case BIO_CTRL_FLUSH:
197 ret = 1;
198 break;
199 default:
200 ret = 0;
201 break;
202 }
203 return (ret);
204}
205
206static int
207sock_puts(BIO *bp, const char *str)
208{
209 int n, ret;
210
211 n = strlen(str);
212 ret = sock_write(bp, str, n);
213 return (ret);
214}
215
216int
217BIO_sock_should_retry(int i)
218{
219 int err;
220
221 if ((i == 0) || (i == -1)) {
222 err = errno;
223 return (BIO_sock_non_fatal_error(err));
224 }
225 return (0);
226}
227LCRYPTO_ALIAS(BIO_sock_should_retry);
228
229int
230BIO_sock_non_fatal_error(int err)
231{
232 switch (err) {
233 case ENOTCONN:
234 case EINTR:
235 case EAGAIN:
236 case EINPROGRESS:
237 case EALREADY:
238 return (1);
239 default:
240 break;
241 }
242 return (0);
243}
244LCRYPTO_ALIAS(BIO_sock_non_fatal_error);