diff options
-rw-r--r-- | src/lib/libcrypto/man/CRYPTO_set_ex_data.3 | 678 | ||||
-rw-r--r-- | src/lib/libcrypto/man/RSA_get_ex_new_index.3 | 537 |
2 files changed, 753 insertions, 462 deletions
diff --git a/src/lib/libcrypto/man/CRYPTO_set_ex_data.3 b/src/lib/libcrypto/man/CRYPTO_set_ex_data.3 index abdef79d87..c22fb22352 100644 --- a/src/lib/libcrypto/man/CRYPTO_set_ex_data.3 +++ b/src/lib/libcrypto/man/CRYPTO_set_ex_data.3 | |||
@@ -1,70 +1,32 @@ | |||
1 | .\" $OpenBSD: CRYPTO_set_ex_data.3,v 1.14 2023/07/28 14:34:54 tb Exp $ | 1 | .\" $OpenBSD: CRYPTO_set_ex_data.3,v 1.15 2023/09/18 14:49:43 schwarze Exp $ |
2 | .\" full merge up to: | ||
3 | .\" OpenSSL CRYPTO_get_ex_new_index 9e183d22 Mar 11 08:56:44 2017 -0500 | ||
4 | .\" selective merge up to: 72a7a702 Feb 26 14:05:09 2019 +0000 | ||
5 | .\" | 2 | .\" |
6 | .\" This file was written by Dr. Stephen Henson <steve@openssl.org> | 3 | .\" Copyright (c) 2023 Ingo Schwarze <schwarze@openbsd.org> |
7 | .\" and by Rich Salz <rsalz@akamai.com>. | ||
8 | .\" Copyright (c) 2000, 2006, 2015, 2016 The OpenSSL Project. | ||
9 | .\" All rights reserved. | ||
10 | .\" | 4 | .\" |
11 | .\" Redistribution and use in source and binary forms, with or without | 5 | .\" Permission to use, copy, modify, and distribute this software for any |
12 | .\" modification, are permitted provided that the following conditions | 6 | .\" purpose with or without fee is hereby granted, provided that the above |
13 | .\" are met: | 7 | .\" copyright notice and this permission notice appear in all copies. |
14 | .\" | 8 | .\" |
15 | .\" 1. Redistributions of source code must retain the above copyright | 9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
16 | .\" 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. | ||
17 | .\" | 16 | .\" |
18 | .\" 2. Redistributions in binary form must reproduce the above copyright | 17 | .Dd $Mdocdate: September 18 2023 $ |
19 | .\" notice, this list of conditions and the following disclaimer in | ||
20 | .\" the documentation and/or other materials provided with the | ||
21 | .\" distribution. | ||
22 | .\" | ||
23 | .\" 3. All advertising materials mentioning features or use of this | ||
24 | .\" software must display the following acknowledgment: | ||
25 | .\" "This product includes software developed by the OpenSSL Project | ||
26 | .\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
27 | .\" | ||
28 | .\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
29 | .\" endorse or promote products derived from this software without | ||
30 | .\" prior written permission. For written permission, please contact | ||
31 | .\" openssl-core@openssl.org. | ||
32 | .\" | ||
33 | .\" 5. Products derived from this software may not be called "OpenSSL" | ||
34 | .\" nor may "OpenSSL" appear in their names without prior written | ||
35 | .\" permission of the OpenSSL Project. | ||
36 | .\" | ||
37 | .\" 6. Redistributions of any form whatsoever must retain the following | ||
38 | .\" acknowledgment: | ||
39 | .\" "This product includes software developed by the OpenSSL Project | ||
40 | .\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
41 | .\" | ||
42 | .\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
43 | .\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
44 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
45 | .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
46 | .\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
47 | .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
48 | .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
49 | .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
50 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
51 | .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
52 | .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
53 | .\" OF THE POSSIBILITY OF SUCH DAMAGE. | ||
54 | .\" | ||
55 | .Dd $Mdocdate: July 28 2023 $ | ||
56 | .Dt CRYPTO_SET_EX_DATA 3 | 18 | .Dt CRYPTO_SET_EX_DATA 3 |
57 | .Os | 19 | .Os |
58 | .Sh NAME | 20 | .Sh NAME |
21 | .Nm CRYPTO_get_ex_new_index , | ||
59 | .Nm CRYPTO_EX_new , | 22 | .Nm CRYPTO_EX_new , |
60 | .Nm CRYPTO_EX_free , | 23 | .Nm CRYPTO_EX_free , |
61 | .Nm CRYPTO_EX_dup , | 24 | .Nm CRYPTO_EX_dup , |
62 | .Nm CRYPTO_get_ex_new_index , | 25 | .Nm CRYPTO_new_ex_data , |
63 | .Nm CRYPTO_set_ex_data , | 26 | .Nm CRYPTO_set_ex_data , |
64 | .Nm CRYPTO_get_ex_data , | 27 | .Nm CRYPTO_get_ex_data , |
65 | .Nm CRYPTO_free_ex_data , | 28 | .Nm CRYPTO_free_ex_data |
66 | .Nm CRYPTO_new_ex_data | 29 | .Nd low-level functions for application specific data |
67 | .Nd functions supporting application-specific data | ||
68 | .Sh SYNOPSIS | 30 | .Sh SYNOPSIS |
69 | .In openssl/crypto.h | 31 | .In openssl/crypto.h |
70 | .Ft int | 32 | .Ft int |
@@ -79,7 +41,7 @@ | |||
79 | .Ft typedef int | 41 | .Ft typedef int |
80 | .Fo CRYPTO_EX_new | 42 | .Fo CRYPTO_EX_new |
81 | .Fa "void *parent" | 43 | .Fa "void *parent" |
82 | .Fa "void *ptr" | 44 | .Fa "void *data" |
83 | .Fa "CRYPTO_EX_DATA *ad" | 45 | .Fa "CRYPTO_EX_DATA *ad" |
84 | .Fa "int idx" | 46 | .Fa "int idx" |
85 | .Fa "long argl" | 47 | .Fa "long argl" |
@@ -88,7 +50,7 @@ | |||
88 | .Ft typedef void | 50 | .Ft typedef void |
89 | .Fo CRYPTO_EX_free | 51 | .Fo CRYPTO_EX_free |
90 | .Fa "void *parent" | 52 | .Fa "void *parent" |
91 | .Fa "void *ptr" | 53 | .Fa "void *data" |
92 | .Fa "CRYPTO_EX_DATA *ad" | 54 | .Fa "CRYPTO_EX_DATA *ad" |
93 | .Fa "int idx" | 55 | .Fa "int idx" |
94 | .Fa "long argl" | 56 | .Fa "long argl" |
@@ -98,7 +60,7 @@ | |||
98 | .Fo CRYPTO_EX_dup | 60 | .Fo CRYPTO_EX_dup |
99 | .Fa "CRYPTO_EX_DATA *to" | 61 | .Fa "CRYPTO_EX_DATA *to" |
100 | .Fa "const CRYPTO_EX_DATA *from" | 62 | .Fa "const CRYPTO_EX_DATA *from" |
101 | .Fa "void *from_d" | 63 | .Fa "void *datap" |
102 | .Fa "int idx" | 64 | .Fa "int idx" |
103 | .Fa "long argl" | 65 | .Fa "long argl" |
104 | .Fa "void *argp" | 66 | .Fa "void *argp" |
@@ -106,238 +68,336 @@ | |||
106 | .Ft int | 68 | .Ft int |
107 | .Fo CRYPTO_new_ex_data | 69 | .Fo CRYPTO_new_ex_data |
108 | .Fa "int class_index" | 70 | .Fa "int class_index" |
109 | .Fa "void *obj" | 71 | .Fa "void *parent" |
110 | .Fa "CRYPTO_EX_DATA *ad" | 72 | .Fa "CRYPTO_EX_DATA *ad" |
111 | .Fc | 73 | .Fc |
112 | .Ft int | 74 | .Ft int |
113 | .Fo CRYPTO_set_ex_data | 75 | .Fo CRYPTO_set_ex_data |
114 | .Fa "CRYPTO_EX_DATA *r" | 76 | .Fa "CRYPTO_EX_DATA *ad" |
115 | .Fa "int idx" | 77 | .Fa "int idx" |
116 | .Fa "void *arg" | 78 | .Fa "void *data" |
117 | .Fc | 79 | .Fc |
118 | .Ft void * | 80 | .Ft void * |
119 | .Fo CRYPTO_get_ex_data | 81 | .Fo CRYPTO_get_ex_data |
120 | .Fa "CRYPTO_EX_DATA *r" | 82 | .Fa "CRYPTO_EX_DATA *ad" |
121 | .Fa "int idx" | 83 | .Fa "int idx" |
122 | .Fc | 84 | .Fc |
123 | .Ft void | 85 | .Ft void |
124 | .Fo CRYPTO_free_ex_data | 86 | .Fo CRYPTO_free_ex_data |
125 | .Fa "int class_index" | 87 | .Fa "int class_index" |
126 | .Fa "void *obj" | 88 | .Fa "void *parent" |
127 | .Fa "CRYPTO_EX_DATA *r" | 89 | .Fa "CRYPTO_EX_DATA *ad" |
128 | .Fc | 90 | .Fc |
129 | .Sh DESCRIPTION | 91 | .Sh DESCRIPTION |
130 | Several OpenSSL structures can have application specific data attached | 92 | The library implements the functions documented in the |
131 | to them, known as "exdata". | 93 | .Xr RSA_get_ex_new_index 3 |
132 | The specific structures are: | 94 | manual page and similar functions for other parent object types |
133 | .Bd -literal | 95 | using the functions documented in the present manual page. |
134 | BIO | 96 | Application programs almost never need |
135 | DH | 97 | to call the functions documented here directly. |
136 | DSA | ||
137 | EC_KEY | ||
138 | ECDH | ||
139 | ECDSA | ||
140 | ENGINE | ||
141 | RSA | ||
142 | SSL | ||
143 | SSL_CTX | ||
144 | SSL_SESSION | ||
145 | UI | ||
146 | X509 | ||
147 | X509_STORE | ||
148 | X509_STORE_CTX | ||
149 | .Ed | ||
150 | .Pp | ||
151 | Each is identified by a | ||
152 | .Dv CRYPTO_EX_INDEX_* | ||
153 | constant defined in the | ||
154 | .In openssl/crypto.h | ||
155 | header file. | ||
156 | .Pp | ||
157 | The API described here is used by OpenSSL to manipulate exdata for | ||
158 | specific structures. | ||
159 | Since the application data can be anything at all, it is passed and | ||
160 | retrieved as a | ||
161 | .Vt void * | ||
162 | type. | ||
163 | .Pp | 98 | .Pp |
164 | To initialize the exdata part of a structure, call | ||
165 | .Fn CRYPTO_new_ex_data . | ||
166 | .Pp | ||
167 | Exdata types are identified by an index, an integer guaranteed to | ||
168 | be unique within structures for the lifetime of the program. | ||
169 | Applications using exdata typically call | ||
170 | .Fn CRYPTO_get_ex_new_index | 99 | .Fn CRYPTO_get_ex_new_index |
171 | at startup and store the result in a global variable, or write a | 100 | behaves in the same way as |
172 | wrapper function to provide lazy evaluation. | 101 | .Xr RSA_get_ex_new_index 3 |
173 | The | 102 | except that the parent object type that the new |
103 | .Fa idx | ||
104 | is reserved for is not part of the function name | ||
105 | but instead specified by the additional | ||
174 | .Fa class_index | 106 | .Fa class_index |
175 | should be one of the | 107 | argument receiving one of the |
176 | .Dv CRYPTO_EX_INDEX_* | 108 | .Dv CRYPTO_EX_INDEX_* |
177 | values. | 109 | constants defined in |
178 | The | 110 | .In openssl/crypto.h . |
111 | The recommendation given in | ||
112 | .Xr RSA_get_ex_new_index 3 | ||
113 | to set the | ||
114 | .Fa argl | ||
115 | argument to 0 and the last four arguments all to | ||
116 | .Dv NULL | ||
117 | applies. | ||
118 | The library passes the | ||
179 | .Fa argl | 119 | .Fa argl |
180 | and | 120 | and |
181 | .Fa argp | 121 | .Fa argp |
182 | parameters are saved to be passed to the callbacks but are otherwise not | 122 | arguments through to the callback functions for the respective |
183 | used. | 123 | .Fa idx , |
184 | In order to transparently manipulate exdata, three callbacks must be | 124 | but ignores them otherwise. |
185 | provided. | ||
186 | The semantics of those callbacks are described below. | ||
187 | .Pp | ||
188 | When copying or releasing objects with exdata, the callback functions | ||
189 | are called in increasing order of their index value. | ||
190 | .Pp | ||
191 | To set or get the exdata on an object, the appropriate type-specific | ||
192 | routine must be used. | ||
193 | This is because the containing structure is opaque and the | ||
194 | .Vt CRYPTO_EX_DATA | ||
195 | field is not accessible. | ||
196 | In both APIs, the | ||
197 | .Fa idx | ||
198 | parameter should be an already-created index value. | ||
199 | .Pp | ||
200 | When setting exdata, the pointer specified with a particular index is | ||
201 | saved, and returned on a subsequent "get" call. | ||
202 | If the application is going to release the data, it must make sure to | ||
203 | set a | ||
204 | .Dv NULL | ||
205 | value at the index, to avoid likely double-free crashes. | ||
206 | .Pp | ||
207 | The function | ||
208 | .Fn CRYPTO_free_ex_data | ||
209 | is used to free all exdata attached to a structure. | ||
210 | The appropriate type-specific routine must be used. | ||
211 | The | ||
212 | .Fa class_index | ||
213 | identifies the structure type, the | ||
214 | .Fa obj | ||
215 | is a pointer to the actual structure, and | ||
216 | .Fa r | ||
217 | is a pointer to the structure's exdata field. | ||
218 | .Pp | ||
219 | The callback functions are used as follows. | ||
220 | .Pp | 125 | .Pp |
221 | When a structure is initially allocated (such as by | 126 | If a function pointer is passed for the |
222 | .Xr RSA_new 3 ) , | ||
223 | then | ||
224 | .Fa new_func | 127 | .Fa new_func |
225 | is called for every defined index. | 128 | argument, that function is called for the returned |
226 | There is no requirement that the entire parent, or containing, structure | 129 | .Fa idx |
227 | has been set up. | 130 | whenever a new parent object is allocated with |
228 | The | 131 | .Xr RSA_new 3 |
229 | .Fa new_func | 132 | or a similar function. |
230 | is typically used only to allocate memory to store the | 133 | .Pp |
231 | exdata, and perhaps an "initialized" flag within that memory. | 134 | If a function pointer is passed for the |
232 | The exdata value should be set by calling | ||
233 | .Fn CRYPTO_set_ex_data . | ||
234 | .Pp | ||
235 | When a structure is free'd (such as by | ||
236 | .Xr SSL_CTX_free 3 ) , | ||
237 | then the | ||
238 | .Fa free_func | ||
239 | is called for every defined index. | ||
240 | Again, the state of the parent structure is not guaranteed. | ||
241 | The | ||
242 | .Fa free_func | 135 | .Fa free_func |
243 | may be called with a | 136 | argument, that function is called for the returned |
244 | .Dv NULL | 137 | .Fa idx |
245 | pointer. | 138 | when a parent object is freed with |
139 | .Xr RSA_free 3 | ||
140 | or a similar function. | ||
246 | .Pp | 141 | .Pp |
247 | Both | 142 | The arguments of |
248 | .Fa new_func | 143 | .Fa new_func |
249 | and | 144 | and |
250 | .Fa free_func | 145 | .Fa free_func |
251 | take the same parameters. | 146 | are as follows: |
252 | The | 147 | .Pp |
148 | .Bl -tag -width Ds -compact | ||
149 | .It Fa parent | ||
150 | the parent object that contains the | ||
151 | .Fa data | ||
152 | .It Fa data | ||
153 | the | ||
154 | .Fa data | ||
155 | previously set by | ||
156 | .Fn CRYPTO_set_ex_data | ||
157 | at | ||
158 | .Fa idx | ||
159 | in | ||
253 | .Fa parent | 160 | .Fa parent |
254 | is the pointer to the structure that contains the exdata. | 161 | .It Fa ad |
255 | The | 162 | the |
256 | .Fa ptr | 163 | .Vt CRYPTO_EX_DATA |
257 | is the current exdata item; for | 164 | subobject of the |
258 | .Fa new_func | 165 | .Fa parent |
259 | this will typically be | 166 | object |
260 | .Dv NULL . | 167 | .It Fa idx |
261 | The | 168 | return value of |
262 | .Fa r | 169 | .Fn CRYPTO_get_ex_new_index |
263 | parameter is a pointer to the exdata field of the object. | 170 | that set this callback |
264 | The | 171 | .It Fa argl |
172 | the | ||
173 | .Fa argl | ||
174 | passed to | ||
175 | .Fn CRYPTO_get_ex_new_index | ||
176 | for this | ||
265 | .Fa idx | 177 | .Fa idx |
266 | is the index and is the value returned when the callbacks were initially | 178 | .It Fa argp |
267 | registered via | 179 | the |
180 | .Fa argp | ||
181 | passed to | ||
268 | .Fn CRYPTO_get_ex_new_index | 182 | .Fn CRYPTO_get_ex_new_index |
269 | and can be used if the same callback handles different types of exdata. | 183 | for this |
184 | .Fa idx | ||
185 | .El | ||
270 | .Pp | 186 | .Pp |
271 | .Fa dup_func | 187 | If a function pointer is passed for the |
272 | is called when a structure is being copied. | 188 | .Fa dup_func , |
273 | This is only done for | 189 | that function is supposed to be called for the returned |
274 | .Vt SSL | 190 | .Fa idx |
191 | whenever a parent object of the respective type is copied. | ||
192 | Actually, the only functions doing that are | ||
193 | .Xr BIO_dup_chain 3 , | ||
194 | .Xr EC_KEY_copy 3 , | ||
275 | and | 195 | and |
196 | .Xr SSL_dup 3 , | ||
197 | and the TLS 1.3 network stack does it internally when duplicating a | ||
276 | .Vt SSL_SESSION | 198 | .Vt SSL_SESSION |
277 | objects. | 199 | object after receiving a new session ticket message. |
278 | The | 200 | Most other object types supporting ex_data do not support |
279 | .Fa to | 201 | copying in the first place, whereas |
202 | .Xr DSA_dup_DH 3 | ||
280 | and | 203 | and |
281 | .Fa from | 204 | .Xr X509_dup 3 |
282 | parameters are pointers to the destination and source | 205 | simply ignore |
283 | .Vt CRYPTO_EX_DATA | 206 | .Fa dup_func . |
284 | structures, respectively. | 207 | .Pp |
285 | The | 208 | The arguments of |
286 | .Fa from_d | ||
287 | parameter is a pointer to the source exdata. | ||
288 | When | ||
289 | .Fa dup_func | 209 | .Fa dup_func |
290 | returns, the value in | 210 | are as follows: |
291 | .Fa from_d | 211 | .Pp |
292 | is copied to the destination ex_data. | 212 | .Bl -tag -width Ds -compact |
293 | If the pointer contained in | 213 | .It Fa to |
294 | .Fa from_d | 214 | the |
295 | is not modified by the | 215 | .Vt CRYPTO_EX_DATA |
296 | .Fa dup_func , | 216 | subobject of the new parent object |
297 | then both | 217 | .It Fa from |
298 | .Fa to | 218 | the |
299 | and | 219 | .Vt CRYPTO_EX_DATA |
300 | .Fa from | 220 | subobject of the original parent object |
301 | will point to the same data. | 221 | .It Fa datap |
302 | The | 222 | a pointer to a copy of the pointer to the original ex_data |
303 | .Fa idx , | 223 | .It Fa idx |
224 | return value of | ||
225 | .Fn CRYPTO_get_ex_new_index | ||
226 | that set this callback | ||
227 | .It Fa argl | ||
228 | the | ||
304 | .Fa argl | 229 | .Fa argl |
305 | and | 230 | passed to |
231 | .Fn CRYPTO_get_ex_new_index | ||
232 | for this | ||
233 | .Fa idx | ||
234 | .It Fa argp | ||
235 | the | ||
306 | .Fa argp | 236 | .Fa argp |
307 | parameters are as described for the other two callbacks. | 237 | passed to |
238 | .Fn CRYPTO_get_ex_new_index | ||
239 | for this | ||
240 | .Fa idx | ||
241 | .El | ||
308 | .Pp | 242 | .Pp |
309 | .Fn CRYPTO_set_ex_data | 243 | Inside |
310 | is used to set application specific data. | 244 | .Fa dup_func , |
311 | The data is supplied in the | 245 | the |
312 | .Fa arg | 246 | .Fa data |
313 | parameter and its precise meaning is up to the application. | 247 | pointer contained in the original parent object being copied |
248 | can be accessed by casting and dereferencing | ||
249 | .Fa datap , | ||
250 | for example: | ||
251 | .Pp | ||
252 | .Dl char *orig_data = *(char **)datap; | ||
253 | .Pp | ||
254 | If the original data is copied, for example in a manner similar to | ||
255 | .Bd -literal -offset indent | ||
256 | char *new_data; | ||
257 | if ((new_data = strdup(orig_data)) == NULL) | ||
258 | return 0; | ||
259 | .Ed | ||
260 | .Pp | ||
261 | then the pointer to the newly allocated memory needs to be passed | ||
262 | back to the caller in the | ||
263 | .Fa datap | ||
264 | argument, for example: | ||
265 | .Bd -literal -offset indent | ||
266 | *(char **)datap = new_data; | ||
267 | return 1; | ||
268 | .Ed | ||
269 | .Pp | ||
270 | Calling | ||
271 | .Fn CRYPTO_set_ex_data to idx new_data | ||
272 | from inside | ||
273 | .Fa dup_func | ||
274 | has no effect because the code calling | ||
275 | .Fa dup_func | ||
276 | unconditionally calls | ||
277 | .Fn CRYPTO_set_ex_data to idx *datap | ||
278 | after | ||
279 | .Fa dup_func | ||
280 | returns successfully. | ||
281 | Consequently, if | ||
282 | .Fa dup_func | ||
283 | does not change | ||
284 | .Pf * Fa datap , | ||
285 | the new parent object ends up containing a pointer to the same memory | ||
286 | as the original parent object and any memory allocated in | ||
287 | .Fa dup_func | ||
288 | is leaked. | ||
289 | .Pp | ||
290 | When multiple callback functions are called, | ||
291 | they are called in increasing order of their | ||
292 | .Fa idx | ||
293 | value. | ||
294 | .Pp | ||
295 | .Fn CRYPTO_new_ex_data | ||
296 | is an internal function that initializes the | ||
297 | .Fa ad | ||
298 | subobject of the | ||
299 | .Fa parent | ||
300 | object, with the type of the parent object specified by the | ||
301 | .Fa class_index | ||
302 | argument. | ||
303 | Initialization includes calling the respective | ||
304 | .Fa new_func | ||
305 | callbacks for all reserved | ||
306 | .Fa idx | ||
307 | values that have such callbacks configured. | ||
308 | Despite its name, | ||
309 | .Fn CRYPTO_new_ex_data | ||
310 | does not create a new object but requires that | ||
311 | .Fa ad | ||
312 | points to an already allocated but still uninitialized object. | ||
314 | .Pp | 313 | .Pp |
315 | .Fn CRYPTO_get_ex_data | ||
316 | is used to retrieve application specific data. | ||
317 | The data is returned to the application; this will be the same value as | ||
318 | supplied to a previous | ||
319 | .Fn CRYPTO_set_ex_data | 314 | .Fn CRYPTO_set_ex_data |
320 | call. | 315 | and |
316 | .Fn CRYPTO_get_ex_data | ||
317 | behave in the same way as | ||
318 | .Xr RSA_set_ex_data 3 | ||
319 | and | ||
320 | .Xr RSA_get_ex_data 3 , | ||
321 | respectively, except that they do not accept a pointer | ||
322 | to the parent object but instead require a pointer to the | ||
323 | .Vt CRYPTO_EX_DATA | ||
324 | subobject of that parent object. | ||
325 | .Pp | ||
326 | .Fn CRYPTO_free_ex_data | ||
327 | is an internal function that frees any memory used inside the | ||
328 | .Fa ad | ||
329 | subobject of the | ||
330 | .Fa parent | ||
331 | object, with the type of the parent object specified by the | ||
332 | .Fa class_index | ||
333 | argument. | ||
334 | This includes calling the respective | ||
335 | .Fa free_func | ||
336 | callbacks for all reserved | ||
337 | .Fa idx | ||
338 | values that have such callbacks configured. | ||
339 | Despite its name, | ||
340 | .Fn CRYPTO_free_ex_data | ||
341 | does not free | ||
342 | .Fa ad | ||
343 | itself. | ||
321 | .Sh RETURN VALUES | 344 | .Sh RETURN VALUES |
322 | .Fn CRYPTO_get_ex_new_index | 345 | .Fn CRYPTO_get_ex_new_index |
323 | returns a new index or -1 on failure; the value 0 is reserved for | 346 | returns a new index equal to or greater than 1 |
324 | the legacy "app_data" APIs. | 347 | or \-1 if memory allocation fails. |
348 | .Pp | ||
349 | .Fn CRYPTO_EX_new | ||
350 | and | ||
351 | .Fn CRYPTO_EX_dup | ||
352 | functions are supposed to return 1 on success or 0 on failure. | ||
325 | .Pp | 353 | .Pp |
354 | .Fn CRYPTO_new_ex_data | ||
355 | and | ||
326 | .Fn CRYPTO_set_ex_data | 356 | .Fn CRYPTO_set_ex_data |
327 | returns 1 on success or 0 on failure. | 357 | return 1 on success or 0 if memory allocation fails. |
328 | .Pp | 358 | .Pp |
329 | .Fn CRYPTO_get_ex_data | 359 | .Fn CRYPTO_get_ex_data |
330 | returns the application data or | 360 | returns the application specific data or |
331 | .Dv NULL | ||
332 | on failure; note that | ||
333 | .Dv NULL | 361 | .Dv NULL |
334 | may be a valid value. | 362 | if the parent object that contains |
363 | .Fa ad | ||
364 | does not contain application specific data at the given | ||
365 | .Fa idx . | ||
366 | .Sh ERRORS | ||
367 | After failure of | ||
368 | .Fn CRYPTO_get_ex_new_index , | ||
369 | .Fn CRYPTO_new_ex_data , | ||
370 | or | ||
371 | .Fn CRYPTO_set_ex_data , | ||
372 | the following diagnostic can be retrieved with | ||
373 | .Xr ERR_get_error 3 , | ||
374 | .Xr ERR_GET_REASON 3 , | ||
375 | and | ||
376 | .Xr ERR_reason_error_string 3 : | ||
377 | .Bl -tag -width Ds | ||
378 | .It Dv ERR_R_MALLOC_FAILURE Qq "malloc failure" | ||
379 | Memory allocation failed. | ||
380 | .El | ||
335 | .Pp | 381 | .Pp |
336 | .Fa dup_func | 382 | In a few unusual failure cases, |
337 | should return 0 for failure and 1 for success. | 383 | .Xr ERR_get_error 3 |
384 | may report different errors caused by | ||
385 | .Xr OPENSSL_init_crypto 3 | ||
386 | or even none at all. | ||
338 | .Pp | 387 | .Pp |
339 | On failure an error code can be obtained from | 388 | Even though it cannot indicate failure, |
389 | .Fn CRYPTO_free_ex_data | ||
390 | may occasionally also set an error code that can be retrieved with | ||
340 | .Xr ERR_get_error 3 . | 391 | .Xr ERR_get_error 3 . |
392 | .Pp | ||
393 | .Fn CRYPTO_get_ex_data | ||
394 | does not distinguish success from failure. | ||
395 | Consequently, after | ||
396 | .Fn CRYPTO_get_ex_data | ||
397 | returns | ||
398 | .Dv NULL , | ||
399 | .Xr ERR_get_error 3 | ||
400 | returns 0 unless there is still an earlier error in the queue. | ||
341 | .Sh SEE ALSO | 401 | .Sh SEE ALSO |
342 | .Xr BIO_get_ex_new_index 3 , | 402 | .Xr BIO_get_ex_new_index 3 , |
343 | .Xr DH_get_ex_new_index 3 , | 403 | .Xr DH_get_ex_new_index 3 , |
@@ -350,11 +410,11 @@ On failure an error code can be obtained from | |||
350 | .Xr X509_STORE_get_ex_new_index 3 | 410 | .Xr X509_STORE_get_ex_new_index 3 |
351 | .Sh HISTORY | 411 | .Sh HISTORY |
352 | .Fn CRYPTO_get_ex_new_index , | 412 | .Fn CRYPTO_get_ex_new_index , |
413 | .Fn CRYPTO_new_ex_data , | ||
353 | .Fn CRYPTO_set_ex_data , | 414 | .Fn CRYPTO_set_ex_data , |
354 | .Fn CRYPTO_get_ex_data , | 415 | .Fn CRYPTO_get_ex_data , |
355 | .Fn CRYPTO_free_ex_data , | ||
356 | and | 416 | and |
357 | .Fn CRYPTO_new_ex_data | 417 | .Fn CRYPTO_free_ex_data |
358 | first appeared in SSLeay 0.9.0 and have been available since | 418 | first appeared in SSLeay 0.9.0 and have been available since |
359 | .Ox 2.4 . | 419 | .Ox 2.4 . |
360 | .Pp | 420 | .Pp |
@@ -364,3 +424,141 @@ and | |||
364 | .Fn CRYPTO_EX_dup | 424 | .Fn CRYPTO_EX_dup |
365 | first appeared in OpenSSL 0.9.5 and have been available since | 425 | first appeared in OpenSSL 0.9.5 and have been available since |
366 | .Ox 2.7 . | 426 | .Ox 2.7 . |
427 | .Sh CAVEATS | ||
428 | If an program installs callback functions, the last call to | ||
429 | .Fn CRYPTO_get_ex_new_index | ||
430 | installing a function of a certain type for a certain | ||
431 | .Fa class_index | ||
432 | needs to be complete before the first object of that | ||
433 | .Fa class_index | ||
434 | can be created, freed, or copied, respectively. | ||
435 | Otherwise, incomplete initialization or cleanup will result. | ||
436 | .Pp | ||
437 | At the time | ||
438 | .Fa new_func | ||
439 | is called, the | ||
440 | .Fa parent | ||
441 | object is only partially initialized, | ||
442 | so trying to access any data in it is strongly discouraged. | ||
443 | The | ||
444 | .Fa data | ||
445 | argument is typically | ||
446 | .Dv NULL | ||
447 | in | ||
448 | .Fa new_func . | ||
449 | .Pp | ||
450 | At the time | ||
451 | .Fa free_func | ||
452 | is called, the | ||
453 | .Fa parent | ||
454 | object is already mostly deconstructed | ||
455 | and part of its content may have been cleared and freed. | ||
456 | Consequently, trying to access any data in | ||
457 | .Fa parent | ||
458 | is strongly discouraged. | ||
459 | According to the OpenSSL API documentation, the library code calling | ||
460 | .Fa free_func | ||
461 | would even be permitted to pass a | ||
462 | .Dv NULL | ||
463 | pointer for the | ||
464 | .Fa parent | ||
465 | argument. | ||
466 | .Pp | ||
467 | .Fn CRYPTO_set_ex_data | ||
468 | and | ||
469 | .Fn CRYPTO_get_ex_data | ||
470 | cannot reasonably be used outside the callback functions | ||
471 | because no API function provides access to any pointers of the type | ||
472 | .Vt CRYPTO_EX_DATA * . | ||
473 | .Pp | ||
474 | Inside | ||
475 | .Fa new_func , | ||
476 | calling | ||
477 | .Fn CRYPTO_get_ex_data | ||
478 | makes no sense because it always returns | ||
479 | .Dv NULL , | ||
480 | and calling | ||
481 | .Fn CRYPTO_set_ex_data | ||
482 | makes no sense because | ||
483 | .Fa new_func | ||
484 | does not have access to any meaningful | ||
485 | .Fa data | ||
486 | it could store, and the absence of application specific data at any given | ||
487 | .Fa idx | ||
488 | is already sufficiently indicated by the default return value | ||
489 | .Dv NULL | ||
490 | of | ||
491 | .Fn CRYPTO_get_ex_data , | ||
492 | .Xr RSA_get_ex_data 3 , | ||
493 | and similar functions. | ||
494 | .Pp | ||
495 | Inside | ||
496 | .Fa free_func , | ||
497 | calling | ||
498 | .Fn CRYPTO_get_ex_data | ||
499 | makes no sense because the return value is already available in | ||
500 | .Fa data , | ||
501 | and calling | ||
502 | .Fn CRYPTO_set_ex_data | ||
503 | makes no sense because the parent object, including any ex_data | ||
504 | contained in it, is already being deconstructed and will no longer | ||
505 | exist by the time application code regains control. | ||
506 | .Pp | ||
507 | Inside | ||
508 | .Fa dup_func , | ||
509 | calling | ||
510 | .Fn CRYPTO_get_ex_data | ||
511 | makes no sense because the return value for | ||
512 | .Fa from | ||
513 | is already available as | ||
514 | .Pf * Fa datap , | ||
515 | and the return value for | ||
516 | .Fa to | ||
517 | is | ||
518 | .Dv NULL . | ||
519 | Calling | ||
520 | .Fn CRYPTO_set_ex_data | ||
521 | makes no sense because changing | ||
522 | .Fa from | ||
523 | would cause an undesirable side effect in this context | ||
524 | and trying to change | ||
525 | .Fa to | ||
526 | is ineffective as explained above. | ||
527 | .Pp | ||
528 | Consequently, application code can never use | ||
529 | .Fn CRYPTO_set_ex_data | ||
530 | or | ||
531 | .Fn CRYPTO_get_ex_data | ||
532 | in a meaningful way. | ||
533 | .Pp | ||
534 | The fact that the functions documented in the present manual page | ||
535 | are part of the public API might create the impression | ||
536 | that application programs could add ex_data support | ||
537 | to additional object types not offering it by default. | ||
538 | However, for built-in object types not offering ex_support, this | ||
539 | is not possible because such objects do not contain the required | ||
540 | .Vt CRYPTO_EX_DATA | ||
541 | subobject. | ||
542 | .Pp | ||
543 | It is theoretically possible to add ex_data support to an | ||
544 | application-defined object type by adding a | ||
545 | .Vt CRYPTO_EX_DATA | ||
546 | field to the struct declaration, a call to | ||
547 | .Fn CRYPTO_new_ex_data | ||
548 | to the object constructor, and a call to | ||
549 | .Fn CRYPTO_free_ex_data | ||
550 | to the object destructor. | ||
551 | The OpenSSL documentation mentions that the constant | ||
552 | .Dv CRYPTO_EX_INDEX_APP | ||
553 | is reserved for this very purpose. | ||
554 | However, doing this would hardly be useful. | ||
555 | It is much more straightforward to just add | ||
556 | all the required data fields to the struct declaration itself. | ||
557 | .Sh BUGS | ||
558 | If | ||
559 | .Fa new_func | ||
560 | or | ||
561 | .Fa dup_func | ||
562 | fails, the failure is silently ignored by the library, potentially | ||
563 | resulting in an incompletely initialized object. | ||
564 | The application program cannot detect this kind of failure. | ||
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 |
113 | Several OpenSSL structures can have application specific data attached | 47 | The following parent objects can have application specific data called |
114 | to them. | 48 | .Dq ex_data |
115 | This has several potential uses: it can be used to cache data associated | 49 | attached to them: |
116 | with a structure (for example the hash of some part of the structure) or | 50 | .Vt BIO , DH , DSA , EC_KEY , ENGINE , RSA , |
117 | some additional data (for example a handle to the data in an external | 51 | .Vt SSL , SSL_CTX , SSL_SESSION , UI , X509 , X509_STORE , |
118 | library). | 52 | and |
53 | .Vt X509_STORE_CTX . | ||
54 | .\" CRYPTO_EX_INDEX_APP and CRYPTO_EX_INDEX_UI_METHOD are unused. | ||
55 | The present manual page documents the related API functions taking the | ||
56 | .Vt RSA | ||
57 | object type as an example. | ||
58 | The functions for the other object types work in exactly the same way: | ||
59 | just replace the string | ||
60 | .Qq RSA | ||
61 | with the name of the respective object type | ||
62 | throughout the rest of this manual page. | ||
119 | .Pp | 63 | .Pp |
120 | Since the application data can be anything at all, it is passed and | 64 | By default, each individual |
121 | retrieved as a | 65 | .Vt RSA |
66 | object can store one | ||
122 | .Vt void * | 67 | .Vt void * |
123 | type. | 68 | pointing to application specific data. |
69 | That specific pointer is identified by an | ||
70 | .Fa idx | ||
71 | argument of 0. | ||
124 | .Pp | 72 | .Pp |
125 | The | ||
126 | .Fn RSA_get_ex_new_index | ||
127 | function is initially called to "register" some new application specific | ||
128 | data. | ||
129 | It takes three optional function pointers which are called when the | ||
130 | parent structure (in this case an RSA structure) is initially created, | ||
131 | when it is copied and when it is freed up. | ||
132 | If any or all of these function pointer arguments are not used, they | ||
133 | should be set to | ||
134 | .Dv NULL . | ||
135 | The precise manner in which these function pointers are called is | ||
136 | described in more detail below. | ||
137 | .Fn RSA_get_ex_new_index | 73 | .Fn RSA_get_ex_new_index |
138 | also takes additional long and pointer parameters which will be passed | 74 | reserves the next consecutive |
139 | to the supplied functions but which otherwise have no special meaning. | ||
140 | It returns an index which should be stored (typically in a static | ||
141 | variable) and passed as the | ||
142 | .Fa idx | 75 | .Fa idx |
143 | parameter in the remaining functions. | 76 | argument, enabling storage of one additional |
144 | Each successful call to | 77 | .Vt void * |
145 | .Fn RSA_get_ex_new_index | 78 | per |
146 | will return an index greater than any previously returned. | ||
147 | This is | ||
148 | important because the optional functions are called in order of | ||
149 | increasing index value. | ||
150 | .Pp | ||
151 | .Fn RSA_set_ex_data | ||
152 | is used to set application specific data. | ||
153 | The data is supplied in the | ||
154 | .Fa arg | ||
155 | parameter and its precise meaning is up to the application. | ||
156 | .Pp | ||
157 | .Fn RSA_get_ex_data | ||
158 | is used to retrieve application specific data. | ||
159 | The data is returned to the application, which will be the same value as | ||
160 | supplied to a previous | ||
161 | .Fn RSA_set_ex_data | ||
162 | call. | ||
163 | .Pp | ||
164 | .Fa new_func | ||
165 | is called when a structure is initially allocated (for example with | ||
166 | .Xr RSA_new 3 . | ||
167 | The parent structure members will not have any meaningful values at this | ||
168 | point. | ||
169 | This function will typically be used to allocate any application | ||
170 | specific structure. | ||
171 | .Pp | ||
172 | .Fa free_func | ||
173 | is called when a structure is being freed up. | ||
174 | The dynamic parent structure members should not be accessed because they | ||
175 | will be freed up when this function is called. | ||
176 | .Pp | ||
177 | .Fa new_func | ||
178 | and | ||
179 | .Fa free_func | ||
180 | take the same parameters. | ||
181 | .Fa parent | ||
182 | is a pointer to the parent | ||
183 | .Vt RSA | 79 | .Vt RSA |
184 | structure. | 80 | object. |
185 | .Fa ptr | 81 | It is typically called at program startup. |
186 | is the application specific data (this won't be of much use in | 82 | It can be called more than once if some |
187 | .Fa new_func ) . | ||
188 | .Fa ad | ||
189 | is a pointer to the | ||
190 | .Vt CRYPTO_EX_DATA | ||
191 | structure from the parent | ||
192 | .Vt RSA | 83 | .Vt RSA |
193 | structure: the functions | 84 | objects need to store more than two application specific pointers. |
194 | .Fn CRYPTO_get_ex_data | 85 | Reserving an additional index for one parent object type, for example for |
86 | .Vt RSA , | ||
87 | does not change the numbers of indices that can be used | ||
88 | with any other parent object type. | ||
89 | .Pp | ||
90 | It is strongly recommended to always pass three | ||
91 | .Dv NULL | ||
92 | pointers for the arguments | ||
93 | .Fa new_func , | ||
94 | .Fa dup_func , | ||
195 | and | 95 | and |
196 | .Fn CRYPTO_set_ex_data | 96 | .Fa free_func . |
197 | can be called to manipulate it. | 97 | When following this recommendation, the arguments |
198 | The | ||
199 | .Fa idx | ||
200 | parameter is the index: this will be the same value returned by | ||
201 | .Fn RSA_get_ex_new_index | ||
202 | when the functions were initially registered. | ||
203 | Finally the | ||
204 | .Fa argl | 98 | .Fa argl |
205 | and | 99 | and |
206 | .Fa argp | 100 | .Fa argp |
207 | parameters are the values originally passed to the same corresponding | 101 | are ignored; conventionally, passing 0 and |
208 | parameters when | 102 | .Dv NULL |
209 | .Fn RSA_get_ex_new_index | 103 | is recommended. |
210 | was called. | 104 | Because using them is discouraged, the three function callback types |
105 | are only documented in the low-level | ||
106 | .Xr CRYPTO_EX_new 3 | ||
107 | manual page. | ||
211 | .Pp | 108 | .Pp |
212 | .Fa dup_func | 109 | .Fn RSA_set_ex_data |
213 | is called when a structure is being copied. | 110 | stores the |
214 | Pointers to the destination and source | 111 | .Fa data |
215 | .Vt CRYPTO_EX_DATA | 112 | pointer as application specific data at the given |
216 | structures are passed in the | 113 | .Fa idx |
217 | .Fa to | 114 | in the given |
218 | and | 115 | .Fa rsa |
219 | .Fa from | 116 | object. |
220 | parameters, respectively. | 117 | The meaning of the data pointed to is up to the application. |
221 | The | 118 | The caller retains ownership of the |
222 | .Fa from_d | 119 | .Fa data |
223 | parameter is passed a pointer to the source application data when the | 120 | and is responsible for freeing it when neither the caller nor the |
224 | function is called. | 121 | .Fa rsa |
225 | When the function returns, the value is copied to the destination: | 122 | object need it any longer. |
226 | the application can thus modify the data pointed to by | 123 | Any other pointer that was previously stored at the same |
227 | .Fa from_d | 124 | .Fa idx |
228 | and have different values in the source and destination. | 125 | in the same |
229 | The | 126 | .Fa rsa |
230 | .Fa idx , | 127 | object is silently overwritten. |
231 | .Fa argl , | 128 | Passing a |
232 | and | 129 | .Dv NULL |
233 | .Fa argp | 130 | pointer for the |
234 | parameters are the same as those in | 131 | .Fa data |
235 | .Fa new_func | 132 | argument is valid and indicates that no application specific data |
236 | and | 133 | currently needs to be stored at the given |
237 | .Fa free_func . | 134 | .Fa idx . |
135 | .Pp | ||
136 | .Fn RSA_get_ex_data | ||
137 | retrieves the last pointer that was stored using | ||
138 | .Fn RSA_set_ex_data | ||
139 | at the given | ||
140 | .Fa idx | ||
141 | in the given | ||
142 | .Fa rsa | ||
143 | object. | ||
238 | .Sh RETURN VALUES | 144 | .Sh RETURN VALUES |
239 | .Fn RSA_get_ex_new_index | 145 | .Fn RSA_get_ex_new_index |
240 | returns a new index or -1 on failure. | 146 | returns a new index equal to or greater than 1 |
241 | Note that 0 is a valid index value. | 147 | or \-1 if memory allocation fails. |
242 | .Pp | 148 | .Pp |
243 | .Fn RSA_set_ex_data | 149 | .Fn RSA_set_ex_data |
244 | returns 1 on success or 0 on failure. | 150 | returns 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 |
247 | returns the application data or | 153 | returns the application specific data or |
248 | .Dv NULL | ||
249 | on failure. | ||
250 | .Dv NULL | 154 | .Dv NULL |
251 | may also be valid application data, but currently it can only fail if | 155 | if |
252 | given an invalid | 156 | .Fa rsa |
253 | .Fa idx | 157 | does not contain application specific data at the given |
254 | parameter. | 158 | .Fa idx . |
255 | .Pp | 159 | .Sh ERRORS |
256 | .Fa new_func | 160 | After failure of |
161 | .Fn RSA_get_ex_new_index | ||
162 | or | ||
163 | .Fn RSA_set_ex_data , | ||
164 | the following diagnostic can be retrieved with | ||
165 | .Xr ERR_get_error 3 , | ||
166 | .Xr ERR_GET_REASON 3 , | ||
257 | and | 167 | and |
258 | .Fa dup_func | 168 | .Xr ERR_reason_error_string 3 : |
259 | should return 0 for failure and 1 for success. | 169 | .Bl -tag -width Ds |
170 | .It Dv ERR_R_MALLOC_FAILURE Qq "malloc failure" | ||
171 | Memory allocation failed. | ||
172 | .El | ||
173 | .Pp | ||
174 | In a few unusual failure cases, | ||
175 | .Xr ERR_get_error 3 | ||
176 | may report different errors caused by | ||
177 | .Xr OPENSSL_init_crypto 3 | ||
178 | or even none at all. | ||
260 | .Pp | 179 | .Pp |
261 | On failure an error code can be obtained from | 180 | .Fn RSA_get_ex_data |
262 | .Xr ERR_get_error 3 . | 181 | does not distinguish success from failure. |
182 | Consequently, after | ||
183 | .Fn RSA_get_ex_data | ||
184 | returns | ||
185 | .Dv NULL , | ||
186 | .Xr ERR_get_error 3 | ||
187 | returns 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 | |||
275 | These functions first appeared in SSLeay 0.9.0 | 200 | These functions first appeared in SSLeay 0.9.0 |
276 | and have been available since | 201 | and have been available since |
277 | .Ox 2.4 . | 202 | .Ox 2.4 . |
278 | .Sh BUGS | 203 | .Sh CAVEATS |
279 | .Fa dup_func | 204 | A relatively small minority of application programs |
280 | is currently never called. | 205 | attempt to change the API contract such that |
206 | .Fn RSA_set_ex_data | ||
207 | transfers ownership of the | ||
208 | .Fa data | ||
209 | to the | ||
210 | .Fa rsa | ||
211 | object. | ||
212 | They do this by providing a | ||
213 | .Fa free_func | ||
214 | that calls | ||
215 | .Xr free 3 | ||
216 | or higher-level | ||
217 | .Fn *_free | ||
218 | functions on the | ||
219 | .Fa data | ||
220 | and sometimes also attempt additional cleanup work as a side effect. | ||
281 | .Pp | 221 | .Pp |
282 | The return value of | 222 | This practice is discouraged for several reasons: |
283 | .Fa new_func | 223 | .Bl -enum |
284 | is ignored. | 224 | .It |
225 | Due to a massive design mistake in the low-level API function | ||
226 | .Xr CRYPTO_free_ex_data 3 , | ||
227 | this practice creates a possibility that | ||
228 | .Xr RSA_free 3 | ||
229 | may fail due to memory allocation failure, consequently leaking the | ||
230 | memory containing the application specific data and silently skipping | ||
231 | any additional cleanup work the | ||
232 | .Fa free_func | ||
233 | was supposed to do, leaving the application in an undetectably | ||
234 | inconsistent state. | ||
235 | Arguably, leaking additional memory while trying to free some | ||
236 | is most unfortunate especially when the program | ||
237 | is already starved for memory. | ||
238 | .It | ||
239 | This practice introduces a risk of use-after-free and double-free | ||
240 | bugs in case the | ||
241 | .Fa rsa | ||
242 | object gets destructed while a caller of | ||
243 | .Fn RSA_set_ex_data | ||
244 | or | ||
245 | .Fn RSA_get_ex_data | ||
246 | still holds a | ||
247 | .Fa data | ||
248 | pointer. | ||
249 | No such risk exists when no | ||
250 | .Fa free_func | ||
251 | is installed. | ||
252 | .It | ||
253 | Attempting additional cleanup work in | ||
254 | .Fa free_func | ||
255 | is an even worse idea because | ||
256 | .Fa free_func | ||
257 | is unable to report any issues it might detect while doing that work. | ||
258 | Instead, if any additional cleanup work is needed, it is recommended | ||
259 | that the calling code takes care of that before calling | ||
260 | .Xr RSA_free 3 . | ||
261 | .El | ||
285 | .Pp | 262 | .Pp |
286 | The | 263 | Even fewer application programs install a |
264 | .Fa new_func | ||
265 | that allocates memory and stores a pointer to it in the | ||
266 | .Fa rsa | ||
267 | object by calling | ||
268 | .Xr CRYPTO_set_ex_data 3 . | ||
269 | That is useless because | ||
287 | .Fa new_func | 270 | .Fa new_func |
288 | function isn't very useful because no meaningful values are present in | 271 | does not have access to any useful information it could store in such memory |
289 | the parent RSA structure when it is called. | 272 | and because the default return value of |
273 | .Dv NULL | ||
274 | from | ||
275 | .Fn RSA_get_ex_data | ||
276 | is sufficient to indicate | ||
277 | that no application specific data has been stored yet. | ||
278 | In addition, allocating memory in | ||
279 | .Fa new_func | ||
280 | is also inadvisable because it introduces an additional responsibility | ||
281 | for callers of | ||
282 | .Fn RSA_set_ex_data | ||
283 | to always call | ||
284 | .Fn RSA_get_ex_data | ||
285 | first, even when it is the first time the application wants to set | ||
286 | application specific data in a particular | ||
287 | .Fa rsa | ||
288 | object, and to either modify whatever | ||
289 | .Fn RSA_get_ex_data | ||
290 | returns or to free it before calling | ||
291 | .Fn RSA_set_ex_data . | ||
292 | If that is forgotten, a memory leak results. | ||
293 | .Pp | ||
294 | Consequently, allocating any required memory | ||
295 | is better left to the application code that calls | ||
296 | .Fn RSA_set_ex_data . | ||
297 | .Pp | ||
298 | Installing a | ||
299 | .Fa dup_func | ||
300 | is often seen in combination with installing a | ||
301 | .Fa free_func , | ||
302 | for obvious reasons. | ||
303 | It is rarely useful because for most parent object types | ||
304 | that support ex_data, including for | ||
305 | .Vt RSA , | ||
306 | the library does not provide a copying API function in the first place, and | ||
307 | even where copying functions exist, they tend to be fragile and error-prone. | ||
308 | When a new object is needed, it is usually advisable to construct it from | ||
309 | scratch whenever possible, rather than attempting a copy operation. | ||
310 | .Pp | ||
311 | On top of that, if | ||
312 | .Fa dup_func | ||
313 | fails, for example because of a memory allocation failure, the | ||
314 | failure is neither reported nor detectable in any way, leaving the | ||
315 | new parent object with incomplete data and potentially in an | ||
316 | inconsistent state. | ||
317 | .Sh BUGS | ||
318 | If | ||
319 | .Fn RSA_set_ex_data | ||
320 | fails, recovery is very difficult. | ||
321 | In particular, calling | ||
322 | .Xr RSA_free 3 | ||
323 | on the parent | ||
324 | .Fa rsa | ||
325 | object right afterwards is likely to also hit a memory allocation | ||
326 | failure, leaking all memory internally allocated by all earlier calls of | ||
327 | .Fn RSA_set_ex_data | ||
328 | on | ||
329 | .Fa rsa | ||
330 | rather than freeing that memory. | ||
331 | In order to recover, the application program | ||
332 | would have to free a sufficient amount of | ||
333 | .Em other | ||
334 | memory before calling | ||
335 | .Xr RSA_free 3 , | ||
336 | which will rarely be feasible. | ||
337 | Consequently, after a failure of | ||
338 | .Fn RSA_set_ex_data , | ||
339 | terminating the program is likely the only reasonable option. | ||
340 | .Pp | ||
341 | If | ||
342 | .Fn RSA_set_ex_data | ||
343 | is called with an | ||
344 | .Fa idx | ||
345 | argument greater than the last one previously returned from | ||
346 | .Fn RSA_get_ex_new_index , | ||
347 | it may still succeed, and though that is not guaranteed by the API, | ||
348 | retrieving the | ||
349 | .Fa data | ||
350 | from such a bogus | ||
351 | .Fa idx | ||
352 | may even be possible with | ||
353 | .Fn RSA_get_ex_data , | ||
354 | hiding the bug in the application program that caused passing the bogus | ||
355 | .Fa idx | ||
356 | to | ||
357 | .Fn RSA_set_ex_data | ||
358 | in the first place. | ||
359 | .Pp | ||
360 | If the bogus | ||
361 | .Fa idx | ||
362 | argument is large, | ||
363 | .Fn RSA_set_ex_data | ||
364 | may uselessly allocate a large amount of memory. | ||
365 | Calling | ||
366 | .Xr RSA_free 3 | ||
367 | on the parent | ||
368 | .Fa rsa | ||
369 | object is the only way to recover that memory. | ||
370 | .Pp | ||
371 | If the bogus | ||
372 | .Fa idx | ||
373 | argument is very large, | ||
374 | .Fn RSA_set_ex_data | ||
375 | is likely to cause a significant delay before eventually failing | ||
376 | due to memory exhaustion. | ||
377 | It is likely to return without releasing the memory already | ||
378 | allocated, causing any subsequent attempt to allocate memory | ||
379 | for other purposes to fail, too. | ||
380 | In this situation, what was said above about failure of | ||
381 | .Fn RSA_set_ex_data | ||
382 | applies, so terminating the program is likely the only reasonable option. | ||