5 openssl_ctx_get_data, openssl_ctx_run_once, openssl_ctx_onfree
6 - internal OPENSSL_CTX routines
10 #include <openssl/ossl_typ.h>
11 #include "internal/cryptlib.h"
13 typedef struct openssl_ctx_method {
14 void *(*new_func)(OPENSSL_CTX *ctx);
15 void (*free_func)(void *);
18 void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index,
19 const OPENSSL_CTX_METHOD *meth);
21 int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx,
22 openssl_ctx_run_once_fn run_once_fn);
23 int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn);
27 Internally, the OpenSSL library context C<OPENSSL_CTX> is implemented
28 as a C<CRYPTO_EX_DATA>, which allows data from diverse parts of the
29 library to be added and removed dynamically.
30 Each such data item must have a corresponding CRYPTO_EX_DATA index
31 associated with it. Unlike normal CRYPTO_EX_DATA objects we use static indexes
32 to identify data items. These are mapped transparently to CRYPTO_EX_DATA dynamic
33 indexes internally to the implementation.
34 See the example further down to see how that's done.
36 openssl_ctx_get_data() is used to retrieve a pointer to the data in
37 the library context C<ctx> associated with the given C<index>. An
38 OPENSSL_CTX_METHOD must be defined and given in the C<meth> parameter. The index
39 for it should be defined in cryptlib.h. The functions through the method are
40 used to create or free items that are stored at that index whenever a library
41 context is created or freed, meaning that the code that use a data item of that
42 index doesn't have to worry about that, just use the data available.
44 Deallocation of an index happens automatically when the library
47 openssl_ctx_run_once is used to run some initialisation routine C<run_once_fn>
48 exactly once per library context C<ctx> object. Each initialisation routine
49 should be allocate a unique run once index in cryptlib.h.
51 Any resources allocated via a run once initialisation routine can be cleaned up
52 using openssl_ctx_onfree. This associates an "on free" routine C<onfreefn> with
53 the library context C<ctx>. When C<ctx> is freed all associated "on free"
58 openssl_ctx_get_data() returns a pointer on success, or C<NULL> on
65 For a type C<FOO> that should end up in the OpenSSL library context, a
66 small bit of initialization is needed, i.e. to associate a constructor
67 and a destructor to an index.
69 typedef struct foo_st {
74 static void *foo_new(OPENSSL_CTX *ctx)
76 FOO *ptr = OPENSSL_zalloc(sizeof(*foo));
81 static void foo_free(void *ptr)
87 * Include a reference to this in the methods table in context.c
88 * OPENSSL_CTX_FOO_INDEX should be added to internal/cryptlib.h
90 const OPENSSL_CTX_METHOD foo_method = {
97 To get and use the data stored in the library context, simply do this:
100 * ctx is received from a caller,
102 FOO *data = openssl_ctx_get_data(ctx, OPENSSL_CTX_FOO_INDEX, &foo_method);
106 void foo_cleanup(OPENSSL_CTX *ctx)
108 /* Free foo resources associated with ctx */
111 static openssl_ctx_run_once_fn do_foo_init;
112 static int do_foo_init(OPENSSL_CTX *ctx)
114 /* Allocate and initialise some foo resources and associated with ctx */
115 return openssl_ctx_onfree(ctx, &foo_cleanup)
118 int foo_some_function(OPENSSL_CTX *ctx)
120 if (!openssl_ctx_run_once(ctx,
121 OPENSSL_CTX_FOO_RUN_ONCE_INDEX,
125 /* Do some work using foo resources in ctx */
135 Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
137 Licensed under the Apache License 2.0 (the "License"). You may not use
138 this file except in compliance with the License. You can obtain a copy
139 in the file LICENSE in the source distribution or at
140 L<https://www.openssl.org/source/license.html>.