xref: /freebsd/crypto/openssl/doc/man3/BIO_s_mem.pod (revision 6f1af0d7d2af54b339b5212434cd6d4fda628d80)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5e71b7053SJung-uk KimBIO_s_secmem,
6e71b7053SJung-uk KimBIO_s_mem, BIO_set_mem_eof_return, BIO_get_mem_data, BIO_set_mem_buf,
7e71b7053SJung-uk KimBIO_get_mem_ptr, BIO_new_mem_buf - memory BIO
8e71b7053SJung-uk Kim
9e71b7053SJung-uk Kim=head1 SYNOPSIS
10e71b7053SJung-uk Kim
11e71b7053SJung-uk Kim #include <openssl/bio.h>
12e71b7053SJung-uk Kim
13e71b7053SJung-uk Kim const BIO_METHOD *BIO_s_mem(void);
14e71b7053SJung-uk Kim const BIO_METHOD *BIO_s_secmem(void);
15e71b7053SJung-uk Kim
16b077aed3SPierre Pronchery BIO_set_mem_eof_return(BIO *b, int v);
17b077aed3SPierre Pronchery long BIO_get_mem_data(BIO *b, char **pp);
18b077aed3SPierre Pronchery BIO_set_mem_buf(BIO *b, BUF_MEM *bm, int c);
19b077aed3SPierre Pronchery BIO_get_mem_ptr(BIO *b, BUF_MEM **pp);
20e71b7053SJung-uk Kim
21e71b7053SJung-uk Kim BIO *BIO_new_mem_buf(const void *buf, int len);
22e71b7053SJung-uk Kim
23e71b7053SJung-uk Kim=head1 DESCRIPTION
24e71b7053SJung-uk Kim
25e71b7053SJung-uk KimBIO_s_mem() returns the memory BIO method function.
26e71b7053SJung-uk Kim
27e71b7053SJung-uk KimA memory BIO is a source/sink BIO which uses memory for its I/O. Data
28e71b7053SJung-uk Kimwritten to a memory BIO is stored in a BUF_MEM structure which is extended
29e71b7053SJung-uk Kimas appropriate to accommodate the stored data.
30e71b7053SJung-uk Kim
31e71b7053SJung-uk KimBIO_s_secmem() is like BIO_s_mem() except that the secure heap is used
32e71b7053SJung-uk Kimfor buffer storage.
33e71b7053SJung-uk Kim
34e71b7053SJung-uk KimAny data written to a memory BIO can be recalled by reading from it.
35e71b7053SJung-uk KimUnless the memory BIO is read only any data read from it is deleted from
36e71b7053SJung-uk Kimthe BIO.
37e71b7053SJung-uk Kim
38e71b7053SJung-uk KimMemory BIOs support BIO_gets() and BIO_puts().
39e71b7053SJung-uk Kim
40e71b7053SJung-uk KimIf the BIO_CLOSE flag is set when a memory BIO is freed then the underlying
41e71b7053SJung-uk KimBUF_MEM structure is also freed.
42e71b7053SJung-uk Kim
43e71b7053SJung-uk KimCalling BIO_reset() on a read write memory BIO clears any data in it if the
44da327cd2SJung-uk Kimflag BIO_FLAGS_NONCLEAR_RST is not set, otherwise it just restores the read
45da327cd2SJung-uk Kimpointer to the state it was just after the last write was performed and the
46da327cd2SJung-uk Kimdata can be read again. On a read only BIO it similarly restores the BIO to
47da327cd2SJung-uk Kimits original state and the read only data can be read again.
48e71b7053SJung-uk Kim
49e71b7053SJung-uk KimBIO_eof() is true if no data is in the BIO.
50e71b7053SJung-uk Kim
51e71b7053SJung-uk KimBIO_ctrl_pending() returns the number of bytes currently stored.
52e71b7053SJung-uk Kim
53e71b7053SJung-uk KimBIO_set_mem_eof_return() sets the behaviour of memory BIO B<b> when it is
54e71b7053SJung-uk Kimempty. If the B<v> is zero then an empty memory BIO will return EOF (that is
55e71b7053SJung-uk Kimit will return zero and BIO_should_retry(b) will be false. If B<v> is non
56e71b7053SJung-uk Kimzero then it will return B<v> when it is empty and it will set the read retry
57e71b7053SJung-uk Kimflag (that is BIO_read_retry(b) is true). To avoid ambiguity with a normal
58e71b7053SJung-uk Kimpositive return value B<v> should be set to a negative value, typically -1.
59e71b7053SJung-uk Kim
60e71b7053SJung-uk KimBIO_get_mem_data() sets *B<pp> to a pointer to the start of the memory BIOs data
61e71b7053SJung-uk Kimand returns the total amount of data available. It is implemented as a macro.
62*6f1af0d7SPierre ProncheryNote the pointer returned by this call is informative, no transfer of ownership
63*6f1af0d7SPierre Proncheryof this memory is implied.  See notes on BIO_set_close().
64e71b7053SJung-uk Kim
65e71b7053SJung-uk KimBIO_set_mem_buf() sets the internal BUF_MEM structure to B<bm> and sets the
66e71b7053SJung-uk Kimclose flag to B<c>, that is B<c> should be either BIO_CLOSE or BIO_NOCLOSE.
67e71b7053SJung-uk KimIt is a macro.
68e71b7053SJung-uk Kim
69e71b7053SJung-uk KimBIO_get_mem_ptr() places the underlying BUF_MEM structure in *B<pp>. It is
70e71b7053SJung-uk Kima macro.
71e71b7053SJung-uk Kim
72e71b7053SJung-uk KimBIO_new_mem_buf() creates a memory BIO using B<len> bytes of data at B<buf>,
73e71b7053SJung-uk Kimif B<len> is -1 then the B<buf> is assumed to be nul terminated and its
74e71b7053SJung-uk Kimlength is determined by B<strlen>. The BIO is set to a read only state and
75e71b7053SJung-uk Kimas a result cannot be written to. This is useful when some data needs to be
76e71b7053SJung-uk Kimmade available from a static area of memory in the form of a BIO. The
77e71b7053SJung-uk Kimsupplied data is read directly from the supplied buffer: it is B<not> copied
78e71b7053SJung-uk Kimfirst, so the supplied area of memory must be unchanged until the BIO is freed.
79e71b7053SJung-uk Kim
80e71b7053SJung-uk Kim=head1 NOTES
81e71b7053SJung-uk Kim
82e71b7053SJung-uk KimWrites to memory BIOs will always succeed if memory is available: that is
83e71b7053SJung-uk Kimtheir size can grow indefinitely.
84e71b7053SJung-uk Kim
85da327cd2SJung-uk KimEvery write after partial read (not all data in the memory buffer was read)
86da327cd2SJung-uk Kimto a read write memory BIO will have to move the unread data with an internal
87da327cd2SJung-uk Kimcopy operation, if a BIO contains a lot of data and it is read in small
88da327cd2SJung-uk Kimchunks intertwined with writes the operation can be very slow. Adding
89da327cd2SJung-uk Kima buffering BIO to the chain can speed up the process.
90e71b7053SJung-uk Kim
91e71b7053SJung-uk KimCalling BIO_set_mem_buf() on a BIO created with BIO_new_secmem() will
92e71b7053SJung-uk Kimgive undefined results, including perhaps a program crash.
93e71b7053SJung-uk Kim
94610a21fdSJung-uk KimSwitching the memory BIO from read write to read only is not supported and
95610a21fdSJung-uk Kimcan give undefined results including a program crash. There are two notable
96610a21fdSJung-uk Kimexceptions to the rule. The first one is to assign a static memory buffer
97610a21fdSJung-uk Kimimmediately after BIO creation and set the BIO as read only.
98610a21fdSJung-uk Kim
99610a21fdSJung-uk KimThe other supported sequence is to start with read write BIO then temporarily
100610a21fdSJung-uk Kimswitch it to read only and call BIO_reset() on the read only BIO immediately
101610a21fdSJung-uk Kimbefore switching it back to read write. Before the BIO is freed it must be
102610a21fdSJung-uk Kimswitched back to the read write mode.
103610a21fdSJung-uk Kim
104610a21fdSJung-uk KimCalling BIO_get_mem_ptr() on read only BIO will return a BUF_MEM that
105610a21fdSJung-uk Kimcontains only the remaining data to be read. If the close status of the
106610a21fdSJung-uk KimBIO is set to BIO_NOCLOSE, before freeing the BUF_MEM the data pointer
107610a21fdSJung-uk Kimin it must be set to NULL as the data pointer does not point to an
108610a21fdSJung-uk Kimallocated memory.
109610a21fdSJung-uk Kim
110da327cd2SJung-uk KimCalling BIO_reset() on a read write memory BIO with BIO_FLAGS_NONCLEAR_RST
111da327cd2SJung-uk Kimflag set can have unexpected outcome when the reads and writes to the
112da327cd2SJung-uk KimBIO are intertwined. As documented above the BIO will be reset to the
113da327cd2SJung-uk Kimstate after the last completed write operation. The effects of reads
114da327cd2SJung-uk Kimpreceding that write operation cannot be undone.
115da327cd2SJung-uk Kim
116da327cd2SJung-uk KimCalling BIO_get_mem_ptr() prior to a BIO_reset() call with
117da327cd2SJung-uk KimBIO_FLAGS_NONCLEAR_RST set has the same effect as a write operation.
118da327cd2SJung-uk Kim
119*6f1af0d7SPierre ProncheryCalling BIO_set_close() with BIO_NOCLOSE orphans the BUF_MEM internal to the
120*6f1af0d7SPierre ProncheryBIO, _not_ its actual data buffer. See the examples section for the proper
121*6f1af0d7SPierre Proncherymethod for claiming ownership of the data pointer for a deferred free operation.
122*6f1af0d7SPierre Pronchery
123e71b7053SJung-uk Kim=head1 BUGS
124e71b7053SJung-uk Kim
125e71b7053SJung-uk KimThere should be an option to set the maximum size of a memory BIO.
126e71b7053SJung-uk Kim
127da327cd2SJung-uk Kim=head1 RETURN VALUES
128da327cd2SJung-uk Kim
129da327cd2SJung-uk KimBIO_s_mem() and BIO_s_secmem() return a valid memory B<BIO_METHOD> structure.
130da327cd2SJung-uk Kim
131da327cd2SJung-uk KimBIO_set_mem_eof_return(), BIO_set_mem_buf() and BIO_get_mem_ptr()
132da327cd2SJung-uk Kimreturn 1 on success or a value which is less than or equal to 0 if an error occurred.
133da327cd2SJung-uk Kim
134da327cd2SJung-uk KimBIO_get_mem_data() returns the total number of bytes available on success,
135da327cd2SJung-uk Kim0 if b is NULL, or a negative value in case of other errors.
136da327cd2SJung-uk Kim
137da327cd2SJung-uk KimBIO_new_mem_buf() returns a valid B<BIO> structure on success or NULL on error.
138da327cd2SJung-uk Kim
139da327cd2SJung-uk Kim=head1 EXAMPLES
140e71b7053SJung-uk Kim
141e71b7053SJung-uk KimCreate a memory BIO and write some data to it:
142e71b7053SJung-uk Kim
143e71b7053SJung-uk Kim BIO *mem = BIO_new(BIO_s_mem());
144e71b7053SJung-uk Kim
145e71b7053SJung-uk Kim BIO_puts(mem, "Hello World\n");
146e71b7053SJung-uk Kim
147e71b7053SJung-uk KimCreate a read only memory BIO:
148e71b7053SJung-uk Kim
149e71b7053SJung-uk Kim char data[] = "Hello World";
150e71b7053SJung-uk Kim BIO *mem = BIO_new_mem_buf(data, -1);
151e71b7053SJung-uk Kim
152e71b7053SJung-uk KimExtract the BUF_MEM structure from a memory BIO and then free up the BIO:
153e71b7053SJung-uk Kim
154e71b7053SJung-uk Kim BUF_MEM *bptr;
155e71b7053SJung-uk Kim
156e71b7053SJung-uk Kim BIO_get_mem_ptr(mem, &bptr);
157e71b7053SJung-uk Kim BIO_set_close(mem, BIO_NOCLOSE); /* So BIO_free() leaves BUF_MEM alone */
158e71b7053SJung-uk Kim BIO_free(mem);
159e71b7053SJung-uk Kim
160*6f1af0d7SPierre ProncheryExtract the BUF_MEM ptr, claim ownership of the internal data and free the BIO
161*6f1af0d7SPierre Proncheryand BUF_MEM structure:
162*6f1af0d7SPierre Pronchery
163*6f1af0d7SPierre Pronchery BUF_MEM *bptr;
164*6f1af0d7SPierre Pronchery char *data;
165*6f1af0d7SPierre Pronchery
166*6f1af0d7SPierre Pronchery BIO_get_mem_data(bio, &data);
167*6f1af0d7SPierre Pronchery BIO_get_mem_ptr(bio, &bptr);
168*6f1af0d7SPierre Pronchery BIO_set_close(mem, BIO_NOCLOSE); /* So BIO_free orphans BUF_MEM */
169*6f1af0d7SPierre Pronchery BIO_free(bio);
170*6f1af0d7SPierre Pronchery bptr->data = NULL; /* Tell BUF_MEM to orphan data */
171*6f1af0d7SPierre Pronchery BUF_MEM_free(bptr);
172*6f1af0d7SPierre Pronchery ...
173*6f1af0d7SPierre Pronchery free(data);
174e71b7053SJung-uk Kim
175e71b7053SJung-uk Kim=head1 COPYRIGHT
176e71b7053SJung-uk Kim
177*6f1af0d7SPierre ProncheryCopyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
178e71b7053SJung-uk Kim
179b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
180e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
181e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
182e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
183e71b7053SJung-uk Kim
184e71b7053SJung-uk Kim=cut
185