summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2023-07-28 10:13:50 +0000
committertb <>2023-07-28 10:13:50 +0000
commite56dd594e13ac1e9a8e37ebda000cf90e2a9ad20 (patch)
treea171add36e20e14aac1e71e60b1bf649a99adc40
parent5a5bd1401f412a6edcc762b842246d253befc095 (diff)
downloadopenbsd-e56dd594e13ac1e9a8e37ebda000cf90e2a9ad20.tar.gz
openbsd-e56dd594e13ac1e9a8e37ebda000cf90e2a9ad20.tar.bz2
openbsd-e56dd594e13ac1e9a8e37ebda000cf90e2a9ad20.zip
Drop BIO_n{read,write}{,0}()
This is one of those strange things that should never have made it into a security-oriented libraries. From BIO_s_bio.3: .\" The following non-copying I/O functions are intentionally undocumented .\" because they seem fragile and unused by anything: It was used in a single place: the gorgeous ssltest. I'm not smart enough to follow. Also: /* WARNING: The non-copying interface is largely untested as of yet * and may contain bugs. */ Oh, really? Into the great bitbucket in the sky you go. ok jsing
-rw-r--r--src/lib/libcrypto/Symbols.list4
-rw-r--r--src/lib/libcrypto/Symbols.namespace4
-rw-r--r--src/lib/libcrypto/bio/bss_bio.c257
-rw-r--r--src/lib/libcrypto/hidden/openssl/bio.h6
4 files changed, 3 insertions, 268 deletions
diff --git a/src/lib/libcrypto/Symbols.list b/src/lib/libcrypto/Symbols.list
index 9f27a69512..94a22938fc 100644
--- a/src/lib/libcrypto/Symbols.list
+++ b/src/lib/libcrypto/Symbols.list
@@ -325,12 +325,8 @@ BIO_new_fp
325BIO_new_mem_buf 325BIO_new_mem_buf
326BIO_new_socket 326BIO_new_socket
327BIO_next 327BIO_next
328BIO_nread
329BIO_nread0
330BIO_number_read 328BIO_number_read
331BIO_number_written 329BIO_number_written
332BIO_nwrite
333BIO_nwrite0
334BIO_pop 330BIO_pop
335BIO_printf 331BIO_printf
336BIO_ptr_ctrl 332BIO_ptr_ctrl
diff --git a/src/lib/libcrypto/Symbols.namespace b/src/lib/libcrypto/Symbols.namespace
index e669cc8cc8..07a1b86ad1 100644
--- a/src/lib/libcrypto/Symbols.namespace
+++ b/src/lib/libcrypto/Symbols.namespace
@@ -1195,10 +1195,6 @@ _libre_BIO_get_retry_BIO
1195_libre_BIO_get_retry_reason 1195_libre_BIO_get_retry_reason
1196_libre_BIO_set_retry_reason 1196_libre_BIO_set_retry_reason
1197_libre_BIO_dup_chain 1197_libre_BIO_dup_chain
1198_libre_BIO_nread0
1199_libre_BIO_nread
1200_libre_BIO_nwrite0
1201_libre_BIO_nwrite
1202_libre_BIO_debug_callback 1198_libre_BIO_debug_callback
1203_libre_BIO_s_mem 1199_libre_BIO_s_mem
1204_libre_BIO_new_mem_buf 1200_libre_BIO_new_mem_buf
diff --git a/src/lib/libcrypto/bio/bss_bio.c b/src/lib/libcrypto/bio/bss_bio.c
index d4c03abda2..9a3215a7d4 100644
--- a/src/lib/libcrypto/bio/bss_bio.c
+++ b/src/lib/libcrypto/bio/bss_bio.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bss_bio.c,v 1.27 2023/07/07 19:37:53 beck Exp $ */ 1/* $OpenBSD: bss_bio.c,v 1.28 2023/07/28 10:13:50 tb Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -251,85 +251,6 @@ bio_read(BIO *bio, char *buf, int size_)
251 return size; 251 return size;
252} 252}
253 253
254/* non-copying interface: provide pointer to available data in buffer
255 * bio_nread0: return number of available bytes
256 * bio_nread: also advance index
257 * (example usage: bio_nread0(), read from buffer, bio_nread()
258 * or just bio_nread(), read from buffer)
259 */
260/* WARNING: The non-copying interface is largely untested as of yet
261 * and may contain bugs. */
262static ssize_t
263bio_nread0(BIO *bio, char **buf)
264{
265 struct bio_bio_st *b, *peer_b;
266 ssize_t num;
267
268 BIO_clear_retry_flags(bio);
269
270 if (!bio->init)
271 return 0;
272
273 b = bio->ptr;
274 assert(b != NULL);
275 assert(b->peer != NULL);
276 peer_b = b->peer->ptr;
277 assert(peer_b != NULL);
278 assert(peer_b->buf != NULL);
279
280 peer_b->request = 0;
281
282 if (peer_b->len == 0) {
283 char dummy;
284
285 /* avoid code duplication -- nothing available for reading */
286 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
287 }
288
289 num = peer_b->len;
290 if (peer_b->size < peer_b->offset + num)
291 /* no ring buffer wrap-around for non-copying interface */
292 num = peer_b->size - peer_b->offset;
293 assert(num > 0);
294
295 if (buf != NULL)
296 *buf = peer_b->buf + peer_b->offset;
297 return num;
298}
299
300static ssize_t
301bio_nread(BIO *bio, char **buf, size_t num_)
302{
303 struct bio_bio_st *b, *peer_b;
304 ssize_t num, available;
305
306 if (num_ > SSIZE_MAX)
307 num = SSIZE_MAX;
308 else
309 num = (ssize_t)num_;
310
311 available = bio_nread0(bio, buf);
312 if (num > available)
313 num = available;
314 if (num <= 0)
315 return num;
316
317 b = bio->ptr;
318 peer_b = b->peer->ptr;
319
320 peer_b->len -= num;
321 if (peer_b->len) {
322 peer_b->offset += num;
323 assert(peer_b->offset <= peer_b->size);
324 if (peer_b->offset == peer_b->size)
325 peer_b->offset = 0;
326 } else
327 peer_b->offset = 0;
328
329 return num;
330}
331
332
333static int 254static int
334bio_write(BIO *bio, const char *buf, int num_) 255bio_write(BIO *bio, const char *buf, int num_)
335{ 256{
@@ -402,85 +323,6 @@ bio_write(BIO *bio, const char *buf, int num_)
402 return num; 323 return num;
403} 324}
404 325
405/* non-copying interface: provide pointer to region to write to
406 * bio_nwrite0: check how much space is available
407 * bio_nwrite: also increase length
408 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
409 * or just bio_nwrite(), write to buffer)
410 */
411static ssize_t
412bio_nwrite0(BIO *bio, char **buf)
413{
414 struct bio_bio_st *b;
415 size_t num;
416 size_t write_offset;
417
418 BIO_clear_retry_flags(bio);
419
420 if (!bio->init)
421 return 0;
422
423 b = bio->ptr;
424
425 assert(b != NULL);
426 assert(b->peer != NULL);
427 assert(b->buf != NULL);
428
429 b->request = 0;
430 if (b->closed) {
431 BIOerror(BIO_R_BROKEN_PIPE);
432 return -1;
433 }
434
435 assert(b->len <= b->size);
436
437 if (b->len == b->size) {
438 BIO_set_retry_write(bio);
439 return -1;
440 }
441
442 num = b->size - b->len;
443 write_offset = b->offset + b->len;
444 if (write_offset >= b->size)
445 write_offset -= b->size;
446 if (write_offset + num > b->size)
447 /* no ring buffer wrap-around for non-copying interface
448 * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
449 * BIO_nwrite may have to be called twice) */
450 num = b->size - write_offset;
451
452 if (buf != NULL)
453 *buf = b->buf + write_offset;
454 assert(write_offset + num <= b->size);
455
456 return num;
457}
458
459static ssize_t
460bio_nwrite(BIO *bio, char **buf, size_t num_)
461{
462 struct bio_bio_st *b;
463 ssize_t num, space;
464
465 if (num_ > SSIZE_MAX)
466 num = SSIZE_MAX;
467 else
468 num = (ssize_t)num_;
469
470 space = bio_nwrite0(bio, buf);
471 if (num > space)
472 num = space;
473 if (num <= 0)
474 return num;
475 b = bio->ptr;
476 assert(b != NULL);
477 b->len += num;
478 assert(b->len <= b->size);
479
480 return num;
481}
482
483
484static long 326static long
485bio_ctrl(BIO *bio, int cmd, long num, void *ptr) 327bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
486{ 328{
@@ -564,28 +406,7 @@ bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
564 ret = 1; 406 ret = 1;
565 break; 407 break;
566 408
567 case BIO_C_NREAD0: 409 /* standard CTRL codes follow */
568 /* prepare for non-copying read */
569 ret = (long) bio_nread0(bio, ptr);
570 break;
571
572 case BIO_C_NREAD:
573 /* non-copying read */
574 ret = (long) bio_nread(bio, ptr, (size_t) num);
575 break;
576
577 case BIO_C_NWRITE0:
578 /* prepare for non-copying write */
579 ret = (long) bio_nwrite0(bio, ptr);
580 break;
581
582 case BIO_C_NWRITE:
583 /* non-copying write */
584 ret = (long) bio_nwrite(bio, ptr, (size_t) num);
585 break;
586
587
588 /* standard CTRL codes follow */
589 410
590 case BIO_CTRL_RESET: 411 case BIO_CTRL_RESET:
591 if (b->buf != NULL) { 412 if (b->buf != NULL) {
@@ -817,77 +638,3 @@ BIO_ctrl_reset_read_request(BIO *bio)
817 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0); 638 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
818} 639}
819LCRYPTO_ALIAS(BIO_ctrl_reset_read_request); 640LCRYPTO_ALIAS(BIO_ctrl_reset_read_request);
820
821
822/* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
823 * (conceivably some other BIOs could allow non-copying reads and writes too.)
824 */
825int
826BIO_nread0(BIO *bio, char **buf)
827{
828 long ret;
829
830 if (!bio->init) {
831 BIOerror(BIO_R_UNINITIALIZED);
832 return -2;
833 }
834
835 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
836 if (ret > INT_MAX)
837 return INT_MAX;
838 else
839 return (int) ret;
840}
841LCRYPTO_ALIAS(BIO_nread0);
842
843int
844BIO_nread(BIO *bio, char **buf, int num)
845{
846 int ret;
847
848 if (!bio->init) {
849 BIOerror(BIO_R_UNINITIALIZED);
850 return -2;
851 }
852
853 ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf);
854 if (ret > 0)
855 bio->num_read += ret;
856 return ret;
857}
858LCRYPTO_ALIAS(BIO_nread);
859
860int
861BIO_nwrite0(BIO *bio, char **buf)
862{
863 long ret;
864
865 if (!bio->init) {
866 BIOerror(BIO_R_UNINITIALIZED);
867 return -2;
868 }
869
870 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
871 if (ret > INT_MAX)
872 return INT_MAX;
873 else
874 return (int) ret;
875}
876LCRYPTO_ALIAS(BIO_nwrite0);
877
878int
879BIO_nwrite(BIO *bio, char **buf, int num)
880{
881 int ret;
882
883 if (!bio->init) {
884 BIOerror(BIO_R_UNINITIALIZED);
885 return -2;
886 }
887
888 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
889 if (ret > 0)
890 bio->num_write += ret;
891 return ret;
892}
893LCRYPTO_ALIAS(BIO_nwrite);
diff --git a/src/lib/libcrypto/hidden/openssl/bio.h b/src/lib/libcrypto/hidden/openssl/bio.h
index 46cbdf72fe..f7e7cd3d8e 100644
--- a/src/lib/libcrypto/hidden/openssl/bio.h
+++ b/src/lib/libcrypto/hidden/openssl/bio.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bio.h,v 1.3 2023/07/28 09:58:30 tb Exp $ */ 1/* $OpenBSD: bio.h,v 1.4 2023/07/28 10:13:50 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Bob Beck <beck@openbsd.org> 3 * Copyright (c) 2023 Bob Beck <beck@openbsd.org>
4 * 4 *
@@ -97,10 +97,6 @@ LCRYPTO_USED(BIO_get_retry_BIO);
97LCRYPTO_USED(BIO_get_retry_reason); 97LCRYPTO_USED(BIO_get_retry_reason);
98LCRYPTO_USED(BIO_set_retry_reason); 98LCRYPTO_USED(BIO_set_retry_reason);
99LCRYPTO_USED(BIO_dup_chain); 99LCRYPTO_USED(BIO_dup_chain);
100LCRYPTO_USED(BIO_nread0);
101LCRYPTO_USED(BIO_nread);
102LCRYPTO_USED(BIO_nwrite0);
103LCRYPTO_USED(BIO_nwrite);
104LCRYPTO_USED(BIO_debug_callback); 100LCRYPTO_USED(BIO_debug_callback);
105LCRYPTO_USED(BIO_s_mem); 101LCRYPTO_USED(BIO_s_mem);
106LCRYPTO_USED(BIO_new_mem_buf); 102LCRYPTO_USED(BIO_new_mem_buf);