summaryrefslogtreecommitdiff
path: root/lib/libcrypto/man/CRYPTO_set_ex_data.3
blob: c22fb223523c244b37dcaa7524a6d48575e0c23d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
.\" $OpenBSD: CRYPTO_set_ex_data.3,v 1.15 2023/09/18 14:49:43 schwarze Exp $
.\"
.\" Copyright (c) 2023 Ingo Schwarze <schwarze@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate: September 18 2023 $
.Dt CRYPTO_SET_EX_DATA 3
.Os
.Sh NAME
.Nm CRYPTO_get_ex_new_index ,
.Nm CRYPTO_EX_new ,
.Nm CRYPTO_EX_free ,
.Nm CRYPTO_EX_dup ,
.Nm CRYPTO_new_ex_data ,
.Nm CRYPTO_set_ex_data ,
.Nm CRYPTO_get_ex_data ,
.Nm CRYPTO_free_ex_data
.Nd low-level functions for application specific data
.Sh SYNOPSIS
.In openssl/crypto.h
.Ft int
.Fo CRYPTO_get_ex_new_index
.Fa "int class_index"
.Fa "long argl"
.Fa "void *argp"
.Fa "CRYPTO_EX_new *new_func"
.Fa "CRYPTO_EX_dup *dup_func"
.Fa "CRYPTO_EX_free *free_func"
.Fc
.Ft typedef int
.Fo CRYPTO_EX_new
.Fa "void *parent"
.Fa "void *data"
.Fa "CRYPTO_EX_DATA *ad"
.Fa "int idx"
.Fa "long argl"
.Fa "void *argp"
.Fc
.Ft typedef void
.Fo CRYPTO_EX_free
.Fa "void *parent"
.Fa "void *data"
.Fa "CRYPTO_EX_DATA *ad"
.Fa "int idx"
.Fa "long argl"
.Fa "void *argp"
.Fc
.Ft typedef int
.Fo CRYPTO_EX_dup
.Fa "CRYPTO_EX_DATA *to"
.Fa "const CRYPTO_EX_DATA *from"
.Fa "void *datap"
.Fa "int idx"
.Fa "long argl"
.Fa "void *argp"
.Fc
.Ft int
.Fo CRYPTO_new_ex_data
.Fa "int class_index"
.Fa "void *parent"
.Fa "CRYPTO_EX_DATA *ad"
.Fc
.Ft int
.Fo CRYPTO_set_ex_data
.Fa "CRYPTO_EX_DATA *ad"
.Fa "int idx"
.Fa "void *data"
.Fc
.Ft void *
.Fo CRYPTO_get_ex_data
.Fa "CRYPTO_EX_DATA *ad"
.Fa "int idx"
.Fc
.Ft void
.Fo CRYPTO_free_ex_data
.Fa "int class_index"
.Fa "void *parent"
.Fa "CRYPTO_EX_DATA *ad"
.Fc
.Sh DESCRIPTION
The library implements the functions documented in the
.Xr RSA_get_ex_new_index 3
manual page and similar functions for other parent object types
using the functions documented in the present manual page.
Application programs almost never need
to call the functions documented here directly.
.Pp
.Fn CRYPTO_get_ex_new_index
behaves in the same way as
.Xr RSA_get_ex_new_index 3
except that the parent object type that the new
.Fa idx
is reserved for is not part of the function name
but instead specified by the additional
.Fa class_index
argument receiving one of the
.Dv CRYPTO_EX_INDEX_*
constants defined in
.In openssl/crypto.h .
The recommendation given in
.Xr RSA_get_ex_new_index 3
to set the
.Fa argl
argument to 0 and the last four arguments all to
.Dv NULL
applies.
The library passes the
.Fa argl
and
.Fa argp
arguments through to the callback functions for the respective
.Fa idx ,
but ignores them otherwise.
.Pp
If a function pointer is passed for the
.Fa new_func
argument, that function is called for the returned
.Fa idx
whenever a new parent object is allocated with
.Xr RSA_new 3
or a similar function.
.Pp
If a function pointer is passed for the
.Fa free_func
argument, that function is called for the returned
.Fa idx
when a parent object is freed with
.Xr RSA_free 3
or a similar function.
.Pp
The arguments of
.Fa new_func
and
.Fa free_func
are as follows:
.Pp
.Bl -tag -width Ds -compact
.It Fa parent
the parent object that contains the
.Fa data
.It Fa data
the
.Fa data
previously set by
.Fn CRYPTO_set_ex_data
at
.Fa idx
in
.Fa parent
.It Fa ad
the
.Vt CRYPTO_EX_DATA
subobject of the
.Fa parent
object
.It Fa idx
return value of
.Fn CRYPTO_get_ex_new_index
that set this callback
.It Fa argl
the
.Fa argl
passed to
.Fn CRYPTO_get_ex_new_index
for this
.Fa idx
.It Fa argp
the
.Fa argp
passed to
.Fn CRYPTO_get_ex_new_index
for this
.Fa idx
.El
.Pp
If a function pointer is passed for the
.Fa dup_func ,
that function is supposed to be called for the returned
.Fa idx
whenever a parent object of the respective type is copied.
Actually, the only functions doing that are
.Xr BIO_dup_chain 3 ,
.Xr EC_KEY_copy 3 ,
and
.Xr SSL_dup 3 ,
and the TLS 1.3 network stack does it internally when duplicating a
.Vt SSL_SESSION
object after receiving a new session ticket message.
Most other object types supporting ex_data do not support
copying in the first place, whereas
.Xr DSA_dup_DH 3
and
.Xr X509_dup 3
simply ignore
.Fa dup_func .
.Pp
The arguments of
.Fa dup_func
are as follows:
.Pp
.Bl -tag -width Ds -compact
.It Fa to
the
.Vt CRYPTO_EX_DATA
subobject of the new parent object
.It Fa from
the
.Vt CRYPTO_EX_DATA
subobject of the original parent object
.It Fa datap
a pointer to a copy of the pointer to the original ex_data
.It Fa idx
return value of
.Fn CRYPTO_get_ex_new_index
that set this callback
.It Fa argl
the
.Fa argl
passed to
.Fn CRYPTO_get_ex_new_index
for this
.Fa idx
.It Fa argp
the
.Fa argp
passed to
.Fn CRYPTO_get_ex_new_index
for this
.Fa idx
.El
.Pp
Inside
.Fa dup_func ,
the
.Fa data
pointer contained in the original parent object being copied
can be accessed by casting and dereferencing
.Fa datap ,
for example:
.Pp
.Dl char *orig_data = *(char **)datap;
.Pp
If the original data is copied, for example in a manner similar to
.Bd -literal -offset indent
char *new_data;
if ((new_data = strdup(orig_data)) == NULL)
	return 0;
.Ed
.Pp
then the pointer to the newly allocated memory needs to be passed
back to the caller in the
.Fa datap
argument, for example:
.Bd -literal -offset indent
*(char **)datap = new_data;
return 1;
.Ed
.Pp
Calling
.Fn CRYPTO_set_ex_data to idx new_data
from inside
.Fa dup_func
has no effect because the code calling
.Fa dup_func
unconditionally calls
.Fn CRYPTO_set_ex_data to idx *datap
after
.Fa dup_func
returns successfully.
Consequently, if
.Fa dup_func
does not change
.Pf * Fa datap ,
the new parent object ends up containing a pointer to the same memory
as the original parent object and any memory allocated in
.Fa dup_func
is leaked.
.Pp
When multiple callback functions are called,
they are called in increasing order of their
.Fa idx
value.
.Pp
.Fn CRYPTO_new_ex_data
is an internal function that initializes the
.Fa ad
subobject of the
.Fa parent
object, with the type of the parent object specified by the
.Fa class_index
argument.
Initialization includes calling the respective
.Fa new_func
callbacks for all reserved
.Fa idx
values that have such callbacks configured.
Despite its name,
.Fn CRYPTO_new_ex_data
does not create a new object but requires that
.Fa ad
points to an already allocated but still uninitialized object.
.Pp
.Fn CRYPTO_set_ex_data
and
.Fn CRYPTO_get_ex_data
behave in the same way as
.Xr RSA_set_ex_data 3
and
.Xr RSA_get_ex_data 3 ,
respectively, except that they do not accept a pointer
to the parent object but instead require a pointer to the
.Vt CRYPTO_EX_DATA
subobject of that parent object.
.Pp
.Fn CRYPTO_free_ex_data
is an internal function that frees any memory used inside the
.Fa ad
subobject of the
.Fa parent
object, with the type of the parent object specified by the
.Fa class_index
argument.
This includes calling the respective
.Fa free_func
callbacks for all reserved
.Fa idx
values that have such callbacks configured.
Despite its name,
.Fn CRYPTO_free_ex_data
does not free
.Fa ad
itself.
.Sh RETURN VALUES
.Fn CRYPTO_get_ex_new_index
returns a new index equal to or greater than 1
or \-1 if memory allocation fails.
.Pp
.Fn CRYPTO_EX_new
and
.Fn CRYPTO_EX_dup
functions are supposed to return 1 on success or 0 on failure.
.Pp
.Fn CRYPTO_new_ex_data
and
.Fn CRYPTO_set_ex_data
return 1 on success or 0 if memory allocation fails.
.Pp
.Fn CRYPTO_get_ex_data
returns the application specific data or
.Dv NULL
if the parent object that contains
.Fa ad
does not contain application specific data at the given
.Fa idx .
.Sh ERRORS
After failure of
.Fn CRYPTO_get_ex_new_index ,
.Fn CRYPTO_new_ex_data ,
or
.Fn CRYPTO_set_ex_data ,
the following diagnostic can be retrieved with
.Xr ERR_get_error 3 ,
.Xr ERR_GET_REASON 3 ,
and
.Xr ERR_reason_error_string 3 :
.Bl -tag -width Ds
.It Dv ERR_R_MALLOC_FAILURE Qq "malloc failure"
Memory allocation failed.
.El
.Pp
In a few unusual failure cases,
.Xr ERR_get_error 3
may report different errors caused by
.Xr OPENSSL_init_crypto 3
or even none at all.
.Pp
Even though it cannot indicate failure,
.Fn CRYPTO_free_ex_data
may occasionally also set an error code that can be retrieved with
.Xr ERR_get_error 3 .
.Pp
.Fn CRYPTO_get_ex_data
does not distinguish success from failure.
Consequently, after
.Fn CRYPTO_get_ex_data
returns
.Dv NULL ,
.Xr ERR_get_error 3
returns 0 unless there is still an earlier error in the queue.
.Sh SEE ALSO
.Xr BIO_get_ex_new_index 3 ,
.Xr DH_get_ex_new_index 3 ,
.Xr DSA_get_ex_new_index 3 ,
.Xr RSA_get_ex_new_index 3 ,
.Xr SSL_CTX_get_ex_new_index 3 ,
.Xr SSL_get_ex_new_index 3 ,
.Xr SSL_SESSION_get_ex_new_index 3 ,
.Xr X509_STORE_CTX_get_ex_new_index 3 ,
.Xr X509_STORE_get_ex_new_index 3
.Sh HISTORY
.Fn CRYPTO_get_ex_new_index ,
.Fn CRYPTO_new_ex_data ,
.Fn CRYPTO_set_ex_data ,
.Fn CRYPTO_get_ex_data ,
and
.Fn CRYPTO_free_ex_data
first appeared in SSLeay 0.9.0 and have been available since
.Ox 2.4 .
.Pp
.Fn CRYPTO_EX_new ,
.Fn CRYPTO_EX_free ,
and
.Fn CRYPTO_EX_dup
first appeared in OpenSSL 0.9.5 and have been available since
.Ox 2.7 .
.Sh CAVEATS
If an program installs callback functions, the last call to
.Fn CRYPTO_get_ex_new_index
installing a function of a certain type for a certain
.Fa class_index
needs to be complete before the first object of that
.Fa class_index
can be created, freed, or copied, respectively.
Otherwise, incomplete initialization or cleanup will result.
.Pp
At the time
.Fa new_func
is called, the
.Fa parent
object is only partially initialized,
so trying to access any data in it is strongly discouraged.
The
.Fa data
argument is typically
.Dv NULL
in
.Fa new_func .
.Pp
At the time
.Fa free_func
is called, the
.Fa parent
object is already mostly deconstructed
and part of its content may have been cleared and freed.
Consequently, trying to access any data in
.Fa parent
is strongly discouraged.
According to the OpenSSL API documentation, the library code calling
.Fa free_func
would even be permitted to pass a
.Dv NULL
pointer for the
.Fa parent
argument.
.Pp
.Fn CRYPTO_set_ex_data
and
.Fn CRYPTO_get_ex_data
cannot reasonably be used outside the callback functions
because no API function provides access to any pointers of the type
.Vt CRYPTO_EX_DATA * .
.Pp
Inside
.Fa new_func ,
calling
.Fn CRYPTO_get_ex_data
makes no sense because it always returns
.Dv NULL ,
and calling
.Fn CRYPTO_set_ex_data
makes no sense because
.Fa new_func
does not have access to any meaningful
.Fa data
it could store, and the absence of application specific data at any given
.Fa idx
is already sufficiently indicated by the default return value
.Dv NULL
of
.Fn CRYPTO_get_ex_data ,
.Xr RSA_get_ex_data 3 ,
and similar functions.
.Pp
Inside
.Fa free_func ,
calling
.Fn CRYPTO_get_ex_data
makes no sense because the return value is already available in
.Fa data ,
and calling
.Fn CRYPTO_set_ex_data
makes no sense because the parent object, including any ex_data
contained in it, is already being deconstructed and will no longer
exist by the time application code regains control.
.Pp
Inside
.Fa dup_func ,
calling
.Fn CRYPTO_get_ex_data
makes no sense because the return value for
.Fa from
is already available as
.Pf * Fa datap ,
and the return value for
.Fa to
is
.Dv NULL .
Calling
.Fn CRYPTO_set_ex_data
makes no sense because changing
.Fa from
would cause an undesirable side effect in this context
and trying to change
.Fa to
is ineffective as explained above.
.Pp
Consequently, application code can never use
.Fn CRYPTO_set_ex_data
or
.Fn CRYPTO_get_ex_data
in a meaningful way.
.Pp
The fact that the functions documented in the present manual page
are part of the public API might create the impression
that application programs could add ex_data support
to additional object types not offering it by default.
However, for built-in object types not offering ex_support, this
is not possible because such objects do not contain the required
.Vt CRYPTO_EX_DATA
subobject.
.Pp
It is theoretically possible to add ex_data support to an
application-defined object type by adding a
.Vt CRYPTO_EX_DATA
field to the struct declaration, a call to
.Fn CRYPTO_new_ex_data
to the object constructor, and a call to
.Fn CRYPTO_free_ex_data
to the object destructor.
The OpenSSL documentation mentions that the constant
.Dv CRYPTO_EX_INDEX_APP
is reserved for this very purpose.
However, doing this would hardly be useful.
It is much more straightforward to just add
all the required data fields to the struct declaration itself.
.Sh BUGS
If
.Fa new_func
or
.Fa dup_func
fails, the failure is silently ignored by the library, potentially
resulting in an incompletely initialized object.
The application program cannot detect this kind of failure.