5 ossl_lib_ctx_get_data, ossl_lib_ctx_run_once, ossl_lib_ctx_onfree,
7 - internal OSSL_LIB_CTX routines
11 #include <openssl/types.h>
12 #include "internal/cryptlib.h"
14 typedef struct ossl_lib_ctx_method {
16 void *(*new_func)(OSSL_LIB_CTX *ctx);
17 void (*free_func)(void *);
18 } OSSL_LIB_CTX_METHOD;
20 void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index,
21 const OSSL_LIB_CTX_METHOD *meth);
23 int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
24 ossl_lib_ctx_run_once_fn run_once_fn);
25 int ossl_lib_ctx_onfree(OSSL_LIB_CTX *ctx, ossl_lib_ctx_onfree_fn onfreefn);
27 int ossl_lib_ctx_is_child(OSSL_LIB_CTX *ctx);
31 Internally, the OpenSSL library context B<OSSL_LIB_CTX> is implemented
32 as a B<CRYPTO_EX_DATA>, which allows data from diverse parts of the
33 library to be added and removed dynamically.
34 Each such data item must have a corresponding CRYPTO_EX_DATA index
35 associated with it. Unlike normal CRYPTO_EX_DATA objects we use static indexes
36 to identify data items. These are mapped transparently to CRYPTO_EX_DATA dynamic
37 indexes internally to the implementation.
38 See the example further down to see how that's done.
40 ossl_lib_ctx_get_data() is used to retrieve a pointer to the data in
41 the library context I<ctx> associated with the given I<index>. An
42 OSSL_LIB_CTX_METHOD must be defined and given in the I<meth> parameter. The index
43 for it should be defined in cryptlib.h. The functions through the method are
44 used to create or free items that are stored at that index whenever a library
45 context is created or freed, meaning that the code that use a data item of that
46 index doesn't have to worry about that, just use the data available.
48 Deallocation of an index happens automatically when the library
51 ossl_lib_ctx_run_once is used to run some initialisation routine I<run_once_fn>
52 exactly once per library context I<ctx> object. Each initialisation routine
53 should be allocate a unique run once index in cryptlib.h.
55 Any resources allocated via a run once initialisation routine can be cleaned up
56 using ossl_lib_ctx_onfree. This associates an "on free" routine I<onfreefn> with
57 the library context I<ctx>. When I<ctx> is freed all associated "on free"
60 ossl_lib_ctx_is_child() returns 1 if this library context is a child and 0
65 ossl_lib_ctx_get_data() returns a pointer on success, or NULL on
72 For a type C<FOO> that should end up in the OpenSSL library context, a
73 small bit of initialization is needed, i.e. to associate a constructor
74 and a destructor to an index.
76 typedef struct foo_st {
81 static void *foo_new(OSSL_LIB_CTX *ctx)
83 FOO *ptr = OPENSSL_zalloc(sizeof(*foo));
88 static void foo_free(void *ptr)
94 * Include a reference to this in the methods table in context.c
95 * OSSL_LIB_CTX_FOO_INDEX should be added to internal/cryptlib.h
96 * Priorities can be OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
97 * OSSL_LIB_CTX_METHOD_PRIORITY_1, OSSL_LIB_CTX_METHOD_PRIORITY_2, etc.
98 * Default priority is low (0). The higher the priority the earlier the
99 * method's destructor will be called when the library context is cleaned up.
101 const OSSL_LIB_CTX_METHOD foo_method = {
102 OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
109 To get and use the data stored in the library context, simply do this:
112 * ctx is received from a caller,
114 FOO *data = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_FOO_INDEX, &foo_method);
118 void foo_cleanup(OSSL_LIB_CTX *ctx)
120 /* Free foo resources associated with ctx */
123 static ossl_lib_ctx_run_once_fn do_foo_init;
124 static int do_foo_init(OSSL_LIB_CTX *ctx)
126 /* Allocate and initialise some foo resources and associated with ctx */
127 return ossl_lib_ctx_onfree(ctx, &foo_cleanup)
130 int foo_some_function(OSSL_LIB_CTX *ctx)
132 if (!ossl_lib_ctx_run_once(ctx,
133 OSSL_LIB_CTX_FOO_RUN_ONCE_INDEX,
137 /* Do some work using foo resources in ctx */
147 Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
149 Licensed under the Apache License 2.0 (the "License"). You may not use
150 this file except in compliance with the License. You can obtain a copy
151 in the file LICENSE in the source distribution or at
152 L<https://www.openssl.org/source/license.html>.