summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/man/RSA_get_ex_new_index.3
diff options
context:
space:
mode:
authorschwarze <>2023-09-18 14:49:43 +0000
committerschwarze <>2023-09-18 14:49:43 +0000
commit00e4137864cefcf6444cf38e75c42732dcda1684 (patch)
tree3323615e721a5d3b9e6fbe62dba7bfe09394ce09 /src/lib/libcrypto/man/RSA_get_ex_new_index.3
parent647158289863bfec53e7444708536f0e5c277cc5 (diff)
downloadopenbsd-00e4137864cefcf6444cf38e75c42732dcda1684.tar.gz
openbsd-00e4137864cefcf6444cf38e75c42732dcda1684.tar.bz2
openbsd-00e4137864cefcf6444cf38e75c42732dcda1684.zip
Rewrite RSA_get_ex_new_index(3) and CRYPTO_set_ex_data(3) from scratch.
The defects of the old pages were too numerous to list in full but included vagueness, gaps, misleading statements, bad ordering, and duplication. Use my Copyright since none of the text we inherited from OpenSSL remains. Without doing a thorough review, tb@ thinks he likes the new pages after quickly reading through both of them.
Diffstat (limited to 'src/lib/libcrypto/man/RSA_get_ex_new_index.3')
-rw-r--r--src/lib/libcrypto/man/RSA_get_ex_new_index.3537
1 files changed, 315 insertions, 222 deletions
diff --git a/src/lib/libcrypto/man/RSA_get_ex_new_index.3 b/src/lib/libcrypto/man/RSA_get_ex_new_index.3
index ee1e0e82f7..51a8f24cd8 100644
--- a/src/lib/libcrypto/man/RSA_get_ex_new_index.3
+++ b/src/lib/libcrypto/man/RSA_get_ex_new_index.3
@@ -1,65 +1,27 @@
1.\" $OpenBSD: RSA_get_ex_new_index.3,v 1.11 2022/03/31 17:27:17 naddy Exp $ 1.\" $OpenBSD: RSA_get_ex_new_index.3,v 1.12 2023/09/18 14:49:43 schwarze Exp $
2.\" OpenSSL 35cb565a Nov 19 15:49:30 2015 -0500
3.\" 2.\"
4.\" This file was written by Ulf Moeller <ulf@openssl.org> and 3.\" Copyright (c) 2023 Ingo Schwarze <schwarze@openbsd.org>
5.\" Dr. Stephen Henson <steve@openssl.org>.
6.\" Copyright (c) 2000, 2006 The OpenSSL Project. All rights reserved.
7.\" 4.\"
8.\" Redistribution and use in source and binary forms, with or without 5.\" Permission to use, copy, modify, and distribute this software for any
9.\" modification, are permitted provided that the following conditions 6.\" purpose with or without fee is hereby granted, provided that the above
10.\" are met: 7.\" copyright notice and this permission notice appear in all copies.
11.\" 8.\"
12.\" 1. Redistributions of source code must retain the above copyright 9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13.\" notice, this list of conditions and the following disclaimer. 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.
14.\" 16.\"
15.\" 2. Redistributions in binary form must reproduce the above copyright 17.Dd $Mdocdate: September 18 2023 $
16.\" notice, this list of conditions and the following disclaimer in
17.\" the documentation and/or other materials provided with the
18.\" distribution.
19.\"
20.\" 3. All advertising materials mentioning features or use of this
21.\" software must display the following acknowledgment:
22.\" "This product includes software developed by the OpenSSL Project
23.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24.\"
25.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26.\" endorse or promote products derived from this software without
27.\" prior written permission. For written permission, please contact
28.\" openssl-core@openssl.org.
29.\"
30.\" 5. Products derived from this software may not be called "OpenSSL"
31.\" nor may "OpenSSL" appear in their names without prior written
32.\" permission of the OpenSSL Project.
33.\"
34.\" 6. Redistributions of any form whatsoever must retain the following
35.\" acknowledgment:
36.\" "This product includes software developed by the OpenSSL Project
37.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38.\"
39.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50.\" OF THE POSSIBILITY OF SUCH DAMAGE.
51.\"
52.Dd $Mdocdate: March 31 2022 $
53.Dt RSA_GET_EX_NEW_INDEX 3 18.Dt RSA_GET_EX_NEW_INDEX 3
54.Os 19.Os
55.Sh NAME 20.Sh NAME
56.Nm RSA_get_ex_new_index , 21.Nm RSA_get_ex_new_index ,
57.Nm RSA_set_ex_data , 22.Nm RSA_set_ex_data ,
58.Nm RSA_get_ex_data , 23.Nm RSA_get_ex_data
59.Nm CRYPTO_EX_new , 24.Nd add application specific data to RSA objects
60.Nm CRYPTO_EX_dup ,
61.Nm CRYPTO_EX_free
62.Nd add application specific data to RSA structures
63.Sh SYNOPSIS 25.Sh SYNOPSIS
64.In openssl/rsa.h 26.In openssl/rsa.h
65.Ft int 27.Ft int
@@ -72,194 +34,157 @@
72.Fc 34.Fc
73.Ft int 35.Ft int
74.Fo RSA_set_ex_data 36.Fo RSA_set_ex_data
75.Fa "RSA *r" 37.Fa "RSA *rsa"
76.Fa "int idx" 38.Fa "int idx"
77.Fa "void *arg" 39.Fa "void *data"
78.Fc 40.Fc
79.Ft void * 41.Ft void *
80.Fo RSA_get_ex_data 42.Fo RSA_get_ex_data
81.Fa "RSA *r" 43.Fa "RSA *rsa"
82.Fa "int idx"
83.Fc
84.In openssl/crypto.h
85.Ft typedef int
86.Fo CRYPTO_EX_new
87.Fa "void *parent"
88.Fa "void *ptr"
89.Fa "CRYPTO_EX_DATA *ad"
90.Fa "int idx" 44.Fa "int idx"
91.Fa "long argl"
92.Fa "void *argp"
93.Fc
94.Ft typedef void
95.Fo CRYPTO_EX_free
96.Fa "void *parent"
97.Fa "void *ptr"
98.Fa "CRYPTO_EX_DATA *ad"
99.Fa "int idx"
100.Fa "long argl"
101.Fa "void *argp"
102.Fc
103.Ft typedef int
104.Fo CRYPTO_EX_dup
105.Fa "CRYPTO_EX_DATA *to"
106.Fa "CRYPTO_EX_DATA *from"
107.Fa "void *from_d"
108.Fa "int idx"
109.Fa "long argl"
110.Fa "void *argp"
111.Fc 45.Fc
112.Sh DESCRIPTION 46.Sh DESCRIPTION
113Several OpenSSL structures can have application specific data attached 47The following parent objects can have application specific data called
114to them. 48.Dq ex_data
115This has several potential uses: it can be used to cache data associated 49attached to them:
116with a structure (for example the hash of some part of the structure) or 50.Vt BIO , DH , DSA , EC_KEY , ENGINE , RSA ,
117some additional data (for example a handle to the data in an external 51.Vt SSL , SSL_CTX , SSL_SESSION , UI , X509 , X509_STORE ,
118library). 52and
53.Vt X509_STORE_CTX .
54.\" CRYPTO_EX_INDEX_APP and CRYPTO_EX_INDEX_UI_METHOD are unused.
55The present manual page documents the related API functions taking the
56.Vt RSA
57object type as an example.
58The functions for the other object types work in exactly the same way:
59just replace the string
60.Qq RSA
61with the name of the respective object type
62throughout the rest of this manual page.
119.Pp 63.Pp
120Since the application data can be anything at all, it is passed and 64By default, each individual
121retrieved as a 65.Vt RSA
66object can store one
122.Vt void * 67.Vt void *
123type. 68pointing to application specific data.
69That specific pointer is identified by an
70.Fa idx
71argument of 0.
124.Pp 72.Pp
125The
126.Fn RSA_get_ex_new_index
127function is initially called to "register" some new application specific
128data.
129It takes three optional function pointers which are called when the
130parent structure (in this case an RSA structure) is initially created,
131when it is copied and when it is freed up.
132If any or all of these function pointer arguments are not used, they
133should be set to
134.Dv NULL .
135The precise manner in which these function pointers are called is
136described in more detail below.
137.Fn RSA_get_ex_new_index 73.Fn RSA_get_ex_new_index
138also takes additional long and pointer parameters which will be passed 74reserves the next consecutive
139to the supplied functions but which otherwise have no special meaning.
140It returns an index which should be stored (typically in a static
141variable) and passed as the
142.Fa idx 75.Fa idx
143parameter in the remaining functions. 76argument, enabling storage of one additional
144Each successful call to 77.Vt void *
145.Fn RSA_get_ex_new_index 78per
146will return an index greater than any previously returned.
147This is
148important because the optional functions are called in order of
149increasing index value.
150.Pp
151.Fn RSA_set_ex_data
152is used to set application specific data.
153The data is supplied in the
154.Fa arg
155parameter and its precise meaning is up to the application.
156.Pp
157.Fn RSA_get_ex_data
158is used to retrieve application specific data.
159The data is returned to the application, which will be the same value as
160supplied to a previous
161.Fn RSA_set_ex_data
162call.
163.Pp
164.Fa new_func
165is called when a structure is initially allocated (for example with
166.Xr RSA_new 3 .
167The parent structure members will not have any meaningful values at this
168point.
169This function will typically be used to allocate any application
170specific structure.
171.Pp
172.Fa free_func
173is called when a structure is being freed up.
174The dynamic parent structure members should not be accessed because they
175will be freed up when this function is called.
176.Pp
177.Fa new_func
178and
179.Fa free_func
180take the same parameters.
181.Fa parent
182is a pointer to the parent
183.Vt RSA 79.Vt RSA
184structure. 80object.
185.Fa ptr 81It is typically called at program startup.
186is the application specific data (this won't be of much use in 82It can be called more than once if some
187.Fa new_func ) .
188.Fa ad
189is a pointer to the
190.Vt CRYPTO_EX_DATA
191structure from the parent
192.Vt RSA 83.Vt RSA
193structure: the functions 84objects need to store more than two application specific pointers.
194.Fn CRYPTO_get_ex_data 85Reserving an additional index for one parent object type, for example for
86.Vt RSA ,
87does not change the numbers of indices that can be used
88with any other parent object type.
89.Pp
90It is strongly recommended to always pass three
91.Dv NULL
92pointers for the arguments
93.Fa new_func ,
94.Fa dup_func ,
195and 95and
196.Fn CRYPTO_set_ex_data 96.Fa free_func .
197can be called to manipulate it. 97When following this recommendation, the arguments
198The
199.Fa idx
200parameter is the index: this will be the same value returned by
201.Fn RSA_get_ex_new_index
202when the functions were initially registered.
203Finally the
204.Fa argl 98.Fa argl
205and 99and
206.Fa argp 100.Fa argp
207parameters are the values originally passed to the same corresponding 101are ignored; conventionally, passing 0 and
208parameters when 102.Dv NULL
209.Fn RSA_get_ex_new_index 103is recommended.
210was called. 104Because using them is discouraged, the three function callback types
105are only documented in the low-level
106.Xr CRYPTO_EX_new 3
107manual page.
211.Pp 108.Pp
212.Fa dup_func 109.Fn RSA_set_ex_data
213is called when a structure is being copied. 110stores the
214Pointers to the destination and source 111.Fa data
215.Vt CRYPTO_EX_DATA 112pointer as application specific data at the given
216structures are passed in the 113.Fa idx
217.Fa to 114in the given
218and 115.Fa rsa
219.Fa from 116object.
220parameters, respectively. 117The meaning of the data pointed to is up to the application.
221The 118The caller retains ownership of the
222.Fa from_d 119.Fa data
223parameter is passed a pointer to the source application data when the 120and is responsible for freeing it when neither the caller nor the
224function is called. 121.Fa rsa
225When the function returns, the value is copied to the destination: 122object need it any longer.
226the application can thus modify the data pointed to by 123Any other pointer that was previously stored at the same
227.Fa from_d 124.Fa idx
228and have different values in the source and destination. 125in the same
229The 126.Fa rsa
230.Fa idx , 127object is silently overwritten.
231.Fa argl , 128Passing a
232and 129.Dv NULL
233.Fa argp 130pointer for the
234parameters are the same as those in 131.Fa data
235.Fa new_func 132argument is valid and indicates that no application specific data
236and 133currently needs to be stored at the given
237.Fa free_func . 134.Fa idx .
135.Pp
136.Fn RSA_get_ex_data
137retrieves the last pointer that was stored using
138.Fn RSA_set_ex_data
139at the given
140.Fa idx
141in the given
142.Fa rsa
143object.
238.Sh RETURN VALUES 144.Sh RETURN VALUES
239.Fn RSA_get_ex_new_index 145.Fn RSA_get_ex_new_index
240returns a new index or -1 on failure. 146returns a new index equal to or greater than 1
241Note that 0 is a valid index value. 147or \-1 if memory allocation fails.
242.Pp 148.Pp
243.Fn RSA_set_ex_data 149.Fn RSA_set_ex_data
244returns 1 on success or 0 on failure. 150returns 1 on success or 0 if memory allocation fails.
245.Pp 151.Pp
246.Fn RSA_get_ex_data 152.Fn RSA_get_ex_data
247returns the application data or 153returns the application specific data or
248.Dv NULL
249on failure.
250.Dv NULL 154.Dv NULL
251may also be valid application data, but currently it can only fail if 155if
252given an invalid 156.Fa rsa
253.Fa idx 157does not contain application specific data at the given
254parameter. 158.Fa idx .
255.Pp 159.Sh ERRORS
256.Fa new_func 160After failure of
161.Fn RSA_get_ex_new_index
162or
163.Fn RSA_set_ex_data ,
164the following diagnostic can be retrieved with
165.Xr ERR_get_error 3 ,
166.Xr ERR_GET_REASON 3 ,
257and 167and
258.Fa dup_func 168.Xr ERR_reason_error_string 3 :
259should return 0 for failure and 1 for success. 169.Bl -tag -width Ds
170.It Dv ERR_R_MALLOC_FAILURE Qq "malloc failure"
171Memory allocation failed.
172.El
173.Pp
174In a few unusual failure cases,
175.Xr ERR_get_error 3
176may report different errors caused by
177.Xr OPENSSL_init_crypto 3
178or even none at all.
260.Pp 179.Pp
261On failure an error code can be obtained from 180.Fn RSA_get_ex_data
262.Xr ERR_get_error 3 . 181does not distinguish success from failure.
182Consequently, after
183.Fn RSA_get_ex_data
184returns
185.Dv NULL ,
186.Xr ERR_get_error 3
187returns 0 unless there is still an earlier error in the queue.
263.Sh SEE ALSO 188.Sh SEE ALSO
264.Xr BIO_set_ex_data 3 , 189.Xr BIO_set_ex_data 3 ,
265.Xr CRYPTO_set_ex_data 3 , 190.Xr CRYPTO_set_ex_data 3 ,
@@ -275,15 +200,183 @@ On failure an error code can be obtained from
275These functions first appeared in SSLeay 0.9.0 200These functions first appeared in SSLeay 0.9.0
276and have been available since 201and have been available since
277.Ox 2.4 . 202.Ox 2.4 .
278.Sh BUGS 203.Sh CAVEATS
279.Fa dup_func 204A relatively small minority of application programs
280is currently never called. 205attempt to change the API contract such that
206.Fn RSA_set_ex_data
207transfers ownership of the
208.Fa data
209to the
210.Fa rsa
211object.
212They do this by providing a
213.Fa free_func
214that calls
215.Xr free 3
216or higher-level
217.Fn *_free
218functions on the
219.Fa data
220and sometimes also attempt additional cleanup work as a side effect.
281.Pp 221.Pp
282The return value of 222This practice is discouraged for several reasons:
283.Fa new_func 223.Bl -enum
284is ignored. 224.It
225Due to a massive design mistake in the low-level API function
226.Xr CRYPTO_free_ex_data 3 ,
227this practice creates a possibility that
228.Xr RSA_free 3
229may fail due to memory allocation failure, consequently leaking the
230memory containing the application specific data and silently skipping
231any additional cleanup work the
232.Fa free_func
233was supposed to do, leaving the application in an undetectably
234inconsistent state.
235Arguably, leaking additional memory while trying to free some
236is most unfortunate especially when the program
237is already starved for memory.
238.It
239This practice introduces a risk of use-after-free and double-free
240bugs in case the
241.Fa rsa
242object gets destructed while a caller of
243.Fn RSA_set_ex_data
244or
245.Fn RSA_get_ex_data
246still holds a
247.Fa data
248pointer.
249No such risk exists when no
250.Fa free_func
251is installed.
252.It
253Attempting additional cleanup work in
254.Fa free_func
255is an even worse idea because
256.Fa free_func
257is unable to report any issues it might detect while doing that work.
258Instead, if any additional cleanup work is needed, it is recommended
259that the calling code takes care of that before calling
260.Xr RSA_free 3 .
261.El
285.Pp 262.Pp
286The 263Even fewer application programs install a
264.Fa new_func
265that allocates memory and stores a pointer to it in the
266.Fa rsa
267object by calling
268.Xr CRYPTO_set_ex_data 3 .
269That is useless because
287.Fa new_func 270.Fa new_func
288function isn't very useful because no meaningful values are present in 271does not have access to any useful information it could store in such memory
289the parent RSA structure when it is called. 272and because the default return value of
273.Dv NULL
274from
275.Fn RSA_get_ex_data
276is sufficient to indicate
277that no application specific data has been stored yet.
278In addition, allocating memory in
279.Fa new_func
280is also inadvisable because it introduces an additional responsibility
281for callers of
282.Fn RSA_set_ex_data
283to always call
284.Fn RSA_get_ex_data
285first, even when it is the first time the application wants to set
286application specific data in a particular
287.Fa rsa
288object, and to either modify whatever
289.Fn RSA_get_ex_data
290returns or to free it before calling
291.Fn RSA_set_ex_data .
292If that is forgotten, a memory leak results.
293.Pp
294Consequently, allocating any required memory
295is better left to the application code that calls
296.Fn RSA_set_ex_data .
297.Pp
298Installing a
299.Fa dup_func
300is often seen in combination with installing a
301.Fa free_func ,
302for obvious reasons.
303It is rarely useful because for most parent object types
304that support ex_data, including for
305.Vt RSA ,
306the library does not provide a copying API function in the first place, and
307even where copying functions exist, they tend to be fragile and error-prone.
308When a new object is needed, it is usually advisable to construct it from
309scratch whenever possible, rather than attempting a copy operation.
310.Pp
311On top of that, if
312.Fa dup_func
313fails, for example because of a memory allocation failure, the
314failure is neither reported nor detectable in any way, leaving the
315new parent object with incomplete data and potentially in an
316inconsistent state.
317.Sh BUGS
318If
319.Fn RSA_set_ex_data
320fails, recovery is very difficult.
321In particular, calling
322.Xr RSA_free 3
323on the parent
324.Fa rsa
325object right afterwards is likely to also hit a memory allocation
326failure, leaking all memory internally allocated by all earlier calls of
327.Fn RSA_set_ex_data
328on
329.Fa rsa
330rather than freeing that memory.
331In order to recover, the application program
332would have to free a sufficient amount of
333.Em other
334memory before calling
335.Xr RSA_free 3 ,
336which will rarely be feasible.
337Consequently, after a failure of
338.Fn RSA_set_ex_data ,
339terminating the program is likely the only reasonable option.
340.Pp
341If
342.Fn RSA_set_ex_data
343is called with an
344.Fa idx
345argument greater than the last one previously returned from
346.Fn RSA_get_ex_new_index ,
347it may still succeed, and though that is not guaranteed by the API,
348retrieving the
349.Fa data
350from such a bogus
351.Fa idx
352may even be possible with
353.Fn RSA_get_ex_data ,
354hiding the bug in the application program that caused passing the bogus
355.Fa idx
356to
357.Fn RSA_set_ex_data
358in the first place.
359.Pp
360If the bogus
361.Fa idx
362argument is large,
363.Fn RSA_set_ex_data
364may uselessly allocate a large amount of memory.
365Calling
366.Xr RSA_free 3
367on the parent
368.Fa rsa
369object is the only way to recover that memory.
370.Pp
371If the bogus
372.Fa idx
373argument is very large,
374.Fn RSA_set_ex_data
375is likely to cause a significant delay before eventually failing
376due to memory exhaustion.
377It is likely to return without releasing the memory already
378allocated, causing any subsequent attempt to allocate memory
379for other purposes to fail, too.
380In this situation, what was said above about failure of
381.Fn RSA_set_ex_data
382applies, so terminating the program is likely the only reasonable option.