Skip to content

Commit

Permalink
Add some documentation for the BIO_s_mem() datagram capability
Browse files Browse the repository at this point in the history
Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Hugo Landau <[email protected]>
(Merged from openssl#18596)
  • Loading branch information
mattcaswell authored and hlandau committed Jul 28, 2022
1 parent 3bfc58a commit ce602bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ OpenSSL 3.1

### Changes between 3.0 and 3.1 [xx XXX xxxx]

* Added a new BIO_s_dgram_mem() to read/write datagrams to memory

*Matt Caswell*

* Add a mac salt length option for the pkcs12 command.

*Xinping Chen*
Expand Down
35 changes: 25 additions & 10 deletions doc/man3/BIO_s_mem.pod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

=head1 NAME

BIO_s_secmem,
BIO_s_secmem, BIO_s_dgram_mem,
BIO_s_mem, BIO_set_mem_eof_return, BIO_get_mem_data, BIO_set_mem_buf,
BIO_get_mem_ptr, BIO_new_mem_buf - memory BIO

Expand All @@ -11,6 +11,7 @@ BIO_get_mem_ptr, BIO_new_mem_buf - memory BIO
#include <openssl/bio.h>

const BIO_METHOD *BIO_s_mem(void);
const BIO_METHOD *BIO_s_dgram_mem(void);
const BIO_METHOD *BIO_s_secmem(void);

BIO_set_mem_eof_return(BIO *b, int v);
Expand All @@ -31,6 +32,22 @@ as appropriate to accommodate the stored data.
BIO_s_secmem() is like BIO_s_mem() except that the secure heap is used
for buffer storage.

BIO_s_dgram_mem() is a memory BIO that respects datagram semantics. A single
call to L<BIO_write(3)> will write a single datagram to the memory BIO. A
subsequent call to L<BIO_read(3)> will read the data in that datagram. The
L<BIO_read(3)> call will never return more data than was written in the original
L<BIO_write(3)> call even if there were subsequent L<BIO_write(3)> calls that
wrote more datagrams. Each successive call to L<BIO_read(3)> will read the next
datagram. If a L<BIO_read(3)> call supplies a read buffer that is smaller than
the size of the datagram, then the read buffer will be completely filled and the
remaining data from the datagram will be discarded.

It is not possible to write a zero length datagram. Calling L<BIO_write(3)> in
this case will return 0 and no datagrams will be written. Calling L<BIO_read(3)>
when there are no datagrams in the BIO to read will return a negative result and
the "retry" flags will be set (i.e. calling L<BIO_should_retry(3)> will return
true). A datagram mem BIO will never return true from L<BIO_eof(3)>.

Any data written to a memory BIO can be recalled by reading from it.
Unless the memory BIO is read only any data read from it is deleted from
the BIO.
Expand All @@ -56,6 +73,7 @@ it will return zero and BIO_should_retry(b) will be false. If B<v> is non
zero then it will return B<v> when it is empty and it will set the read retry
flag (that is BIO_read_retry(b) is true). To avoid ambiguity with a normal
positive return value B<v> should be set to a negative value, typically -1.
Calling this macro will fail for datagram mem BIOs.

BIO_get_mem_data() sets *B<pp> to a pointer to the start of the memory BIOs data
and returns the total amount of data available. It is implemented as a macro.
Expand Down Expand Up @@ -86,15 +104,15 @@ copy operation, if a BIO contains a lot of data and it is read in small
chunks intertwined with writes the operation can be very slow. Adding
a buffering BIO to the chain can speed up the process.

Calling BIO_set_mem_buf() on a BIO created with BIO_new_secmem() will
give undefined results, including perhaps a program crash.
Calling BIO_set_mem_buf() on a secmem or dgram BIO will give undefined results,
including perhaps a program crash.

Switching the memory BIO from read write to read only is not supported and
Switching a memory BIO from read write to read only is not supported and
can give undefined results including a program crash. There are two notable
exceptions to the rule. The first one is to assign a static memory buffer
immediately after BIO creation and set the BIO as read only.

The other supported sequence is to start with read write BIO then temporarily
The other supported sequence is to start with a read write BIO then temporarily
switch it to read only and call BIO_reset() on the read only BIO immediately
before switching it back to read write. Before the BIO is freed it must be
switched back to the read write mode.
Expand All @@ -114,13 +132,10 @@ preceding that write operation cannot be undone.
Calling BIO_get_mem_ptr() prior to a BIO_reset() call with
BIO_FLAGS_NONCLEAR_RST set has the same effect as a write operation.

=head1 BUGS

There should be an option to set the maximum size of a memory BIO.

=head1 RETURN VALUES

BIO_s_mem() and BIO_s_secmem() return a valid memory B<BIO_METHOD> structure.
BIO_s_mem(), BIO_s_dgram_mem() and BIO_s_secmem() return a valid memory
B<BIO_METHOD> structure.

BIO_set_mem_eof_return(), BIO_set_mem_buf() and BIO_get_mem_ptr()
return 1 on success or a value which is less than or equal to 0 if an error occurred.
Expand Down

0 comments on commit ce602bb

Please sign in to comment.