Blame doc/man3/CRYPTO_get_ex_new_index.pod

Packit c4476c
=pod
Packit c4476c
Packit c4476c
=head1 NAME
Packit c4476c
Packit c4476c
CRYPTO_EX_new, CRYPTO_EX_free, CRYPTO_EX_dup,
Packit c4476c
CRYPTO_free_ex_index, CRYPTO_get_ex_new_index, CRYPTO_set_ex_data,
Packit c4476c
CRYPTO_get_ex_data, CRYPTO_free_ex_data, CRYPTO_new_ex_data
Packit c4476c
- functions supporting application-specific data
Packit c4476c
Packit c4476c
=head1 SYNOPSIS
Packit c4476c
Packit c4476c
 #include <openssl/crypto.h>
Packit c4476c
Packit c4476c
 int CRYPTO_get_ex_new_index(int class_index,
Packit c4476c
                             long argl, void *argp,
Packit c4476c
                             CRYPTO_EX_new *new_func,
Packit c4476c
                             CRYPTO_EX_dup *dup_func,
Packit c4476c
                             CRYPTO_EX_free *free_func);
Packit c4476c
Packit c4476c
 typedef void CRYPTO_EX_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
Packit c4476c
                            int idx, long argl, void *argp);
Packit c4476c
 typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
Packit c4476c
                             int idx, long argl, void *argp);
Packit c4476c
 typedef int CRYPTO_EX_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
Packit c4476c
                           void *from_d, int idx, long argl, void *argp);
Packit c4476c
Packit c4476c
 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
Packit c4476c
Packit c4476c
 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *r, int idx, void *arg);
Packit c4476c
Packit c4476c
 void *CRYPTO_get_ex_data(CRYPTO_EX_DATA *r, int idx);
Packit c4476c
Packit c4476c
 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *r);
Packit c4476c
Packit c4476c
 int CRYPTO_free_ex_index(int class_index, int idx);
Packit c4476c
Packit c4476c
=head1 DESCRIPTION
Packit c4476c
Packit c4476c
Several OpenSSL structures can have application-specific data attached to them,
Packit c4476c
known as "exdata."
Packit c4476c
The specific structures are:
Packit c4476c
Packit c4476c
    APP
Packit c4476c
    BIO
Packit c4476c
    DH
Packit c4476c
    DRBG
Packit c4476c
    DSA
Packit c4476c
    EC_KEY
Packit c4476c
    ENGINE
Packit c4476c
    RSA
Packit c4476c
    SSL
Packit c4476c
    SSL_CTX
Packit c4476c
    SSL_SESSION
Packit c4476c
    UI
Packit c4476c
    UI_METHOD
Packit c4476c
    X509
Packit c4476c
    X509_STORE
Packit c4476c
    X509_STORE_CTX
Packit c4476c
Packit c4476c
Each is identified by an B<CRYPTO_EX_INDEX_xxx> define in the B<crypto.h>
Packit c4476c
header file.  In addition, B<CRYPTO_EX_INDEX_APP> is reserved for
Packit c4476c
applications to use this facility for their own structures.
Packit c4476c
Packit c4476c
The API described here is used by OpenSSL to manipulate exdata for specific
Packit c4476c
structures.  Since the application data can be anything at all it is passed
Packit c4476c
and retrieved as a B<void *> type.
Packit c4476c
Packit c4476c
The B<CRYPTO_EX_DATA> type is opaque.  To initialize the exdata part of
Packit c4476c
a structure, call CRYPTO_new_ex_data(). This is only necessary for
Packit c4476c
B<CRYPTO_EX_INDEX_APP> objects.
Packit c4476c
Packit c4476c
Exdata types are identified by an B<index>, an integer guaranteed to be
Packit c4476c
unique within structures for the lifetime of the program.  Applications
Packit c4476c
using exdata typically call B<CRYPTO_get_ex_new_index> at startup, and
Packit c4476c
store the result in a global variable, or write a wrapper function to
Packit c4476c
provide lazy evaluation.  The B<class_index> should be one of the
Packit c4476c
B<CRYPTO_EX_INDEX_xxx> values. The B<argl> and B<argp> parameters are saved
Packit c4476c
to be passed to the callbacks but are otherwise not used.  In order to
Packit c4476c
transparently manipulate exdata, three callbacks must be provided. The
Packit c4476c
semantics of those callbacks are described below.
Packit c4476c
Packit c4476c
When copying or releasing objects with exdata, the callback functions
Packit c4476c
are called in increasing order of their B<index> value.
Packit c4476c
Packit c4476c
If a dynamic library can be unloaded, it should call CRYPTO_free_ex_index()
Packit c4476c
when this is done.
Packit c4476c
This will replace the callbacks with no-ops
Packit c4476c
so that applications don't crash.  Any existing exdata will be leaked.
Packit c4476c
Packit c4476c
To set or get the exdata on an object, the appropriate type-specific
Packit c4476c
routine must be used.  This is because the containing structure is opaque
Packit c4476c
and the B<CRYPTO_EX_DATA> field is not accessible.  In both API's, the
Packit c4476c
B<idx> parameter should be an already-created index value.
Packit c4476c
Packit c4476c
When setting exdata, the pointer specified with a particular index is saved,
Packit c4476c
and returned on a subsequent "get" call.  If the application is going to
Packit c4476c
release the data, it must make sure to set a B<NULL> value at the index,
Packit c4476c
to avoid likely double-free crashes.
Packit c4476c
Packit c4476c
The function B<CRYPTO_free_ex_data> is used to free all exdata attached
Packit c4476c
to a structure. The appropriate type-specific routine must be used.
Packit c4476c
The B<class_index> identifies the structure type, the B<obj> is
Packit c4476c
a pointer to the actual structure, and B<r> is a pointer to the
Packit c4476c
structure's exdata field.
Packit c4476c
Packit c4476c
=head2 Callback Functions
Packit c4476c
Packit c4476c
This section describes how the callback functions are used. Applications
Packit c4476c
that are defining their own exdata using B<CYPRTO_EX_INDEX_APP> must
Packit c4476c
call them as described here.
Packit c4476c
Packit c4476c
When a structure is initially allocated (such as RSA_new()) then the
Packit c4476c
new_func() is called for every defined index. There is no requirement
Packit c4476c
that the entire parent, or containing, structure has been set up.
Packit c4476c
The new_func() is typically used only to allocate memory to store the
Packit c4476c
exdata, and perhaps an "initialized" flag within that memory.
Packit c4476c
The exdata value should be set by calling CRYPTO_set_ex_data().
Packit c4476c
Packit c4476c
When a structure is free'd (such as SSL_CTX_free()) then the
Packit c4476c
free_func() is called for every defined index.  Again, the state of the
Packit c4476c
parent structure is not guaranteed.  The free_func() may be called with a
Packit c4476c
NULL pointer.
Packit c4476c
Packit c4476c
Both new_func() and free_func() take the same parameters.
Packit c4476c
The B<parent> is the pointer to the structure that contains the exdata.
Packit c4476c
The B<ptr> is the current exdata item; for new_func() this will typically
Packit c4476c
be NULL.  The B<r> parameter is a pointer to the exdata field of the object.
Packit c4476c
The B<idx> is the index and is the value returned when the callbacks were
Packit c4476c
initially registered via CRYPTO_get_ex_new_index() and can be used if
Packit c4476c
the same callback handles different types of exdata.
Packit c4476c
Packit c4476c
dup_func() is called when a structure is being copied.  This is only done
Packit c4476c
for B<SSL>, B<SSL_SESSION>, B<EC_KEY> objects and B<BIO> chains via
Packit c4476c
BIO_dup_chain().  The B<to> and B<from> parameters
Packit c4476c
are pointers to the destination and source B<CRYPTO_EX_DATA> structures,
Packit c4476c
respectively.  The B<from_d> parameter needs to be cast to a B<void **pptr>
Packit c4476c
as the API has currently the wrong signature; that will be changed in a
Packit c4476c
future version.  The B<*pptr> is a pointer to the source exdata.
Packit c4476c
When the dup_func() returns, the value in B<*pptr> is copied to the
Packit c4476c
destination ex_data.  If the pointer contained in B<*pptr> is not modified
Packit c4476c
by the dup_func(), then both B<to> and B<from> will point to the same data.
Packit c4476c
The B<idx>, B<argl> and B<argp> parameters are as described for the other
Packit c4476c
two callbacks.  If the dup_func() returns B<0> the whole CRYPTO_dup_ex_data()
Packit c4476c
will fail.
Packit c4476c
Packit c4476c
=head1 RETURN VALUES
Packit c4476c
Packit c4476c
CRYPTO_get_ex_new_index() returns a new index or -1 on failure.
Packit c4476c
Packit c4476c
CRYPTO_free_ex_index() and
Packit c4476c
CRYPTO_set_ex_data() return 1 on success or 0 on failure.
Packit c4476c
Packit c4476c
CRYPTO_get_ex_data() returns the application data or NULL on failure;
Packit c4476c
note that NULL may be a valid value.
Packit c4476c
Packit c4476c
dup_func() should return 0 for failure and 1 for success.
Packit c4476c
Packit c4476c
=head1 COPYRIGHT
Packit c4476c
Packit c4476c
Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
Packit c4476c
Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
this file except in compliance with the License.  You can obtain a copy
Packit c4476c
in the file LICENSE in the source distribution or at
Packit c4476c
L<https://www.openssl.org/source/license.html>.
Packit c4476c
Packit c4476c
=cut