ossl_lib_ctx_get_data.pod 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. =pod
  2. =head1 NAME
  3. ossl_lib_ctx_get_data, ossl_lib_ctx_run_once, ossl_lib_ctx_onfree,
  4. ossl_lib_ctx_is_child
  5. - internal OSSL_LIB_CTX routines
  6. =head1 SYNOPSIS
  7. #include <openssl/types.h>
  8. #include "internal/cryptlib.h"
  9. typedef struct ossl_lib_ctx_method {
  10. int priority;
  11. void *(*new_func)(OSSL_LIB_CTX *ctx);
  12. void (*free_func)(void *);
  13. } OSSL_LIB_CTX_METHOD;
  14. void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index,
  15. const OSSL_LIB_CTX_METHOD *meth);
  16. int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
  17. ossl_lib_ctx_run_once_fn run_once_fn);
  18. int ossl_lib_ctx_onfree(OSSL_LIB_CTX *ctx, ossl_lib_ctx_onfree_fn onfreefn);
  19. int ossl_lib_ctx_is_child(OSSL_LIB_CTX *ctx);
  20. =head1 DESCRIPTION
  21. Internally, the OpenSSL library context B<OSSL_LIB_CTX> is implemented
  22. as a B<CRYPTO_EX_DATA>, which allows data from diverse parts of the
  23. library to be added and removed dynamically.
  24. Each such data item must have a corresponding CRYPTO_EX_DATA index
  25. associated with it. Unlike normal CRYPTO_EX_DATA objects we use static indexes
  26. to identify data items. These are mapped transparently to CRYPTO_EX_DATA dynamic
  27. indexes internally to the implementation.
  28. See the example further down to see how that's done.
  29. ossl_lib_ctx_get_data() is used to retrieve a pointer to the data in
  30. the library context I<ctx> associated with the given I<index>. An
  31. OSSL_LIB_CTX_METHOD must be defined and given in the I<meth> parameter. The index
  32. for it should be defined in cryptlib.h. The functions through the method are
  33. used to create or free items that are stored at that index whenever a library
  34. context is created or freed, meaning that the code that use a data item of that
  35. index doesn't have to worry about that, just use the data available.
  36. Deallocation of an index happens automatically when the library
  37. context is freed.
  38. ossl_lib_ctx_run_once is used to run some initialisation routine I<run_once_fn>
  39. exactly once per library context I<ctx> object. Each initialisation routine
  40. should be allocate a unique run once index in cryptlib.h.
  41. Any resources allocated via a run once initialisation routine can be cleaned up
  42. using ossl_lib_ctx_onfree. This associates an "on free" routine I<onfreefn> with
  43. the library context I<ctx>. When I<ctx> is freed all associated "on free"
  44. routines are called.
  45. ossl_lib_ctx_is_child() returns 1 if this library context is a child and 0
  46. otherwise.
  47. =head1 RETURN VALUES
  48. ossl_lib_ctx_get_data() returns a pointer on success, or NULL on
  49. failure.
  50. =head1 EXAMPLES
  51. =head2 Initialization
  52. For a type C<FOO> that should end up in the OpenSSL library context, a
  53. small bit of initialization is needed, i.e. to associate a constructor
  54. and a destructor to an index.
  55. typedef struct foo_st {
  56. int i;
  57. void *data;
  58. } FOO;
  59. static void *foo_new(OSSL_LIB_CTX *ctx)
  60. {
  61. FOO *ptr = OPENSSL_zalloc(sizeof(*foo));
  62. if (ptr != NULL)
  63. ptr->i = 42;
  64. return ptr;
  65. }
  66. static void foo_free(void *ptr)
  67. {
  68. OPENSSL_free(ptr);
  69. }
  70. /*
  71. * Include a reference to this in the methods table in context.c
  72. * OSSL_LIB_CTX_FOO_INDEX should be added to internal/cryptlib.h
  73. * Priorities can be OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
  74. * OSSL_LIB_CTX_METHOD_PRIORITY_1, OSSL_LIB_CTX_METHOD_PRIORITY_2, etc.
  75. * Default priority is low (0). The higher the priority the earlier the
  76. * method's destructor will be called when the library context is cleaned up.
  77. */
  78. const OSSL_LIB_CTX_METHOD foo_method = {
  79. OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
  80. foo_new,
  81. foo_free
  82. };
  83. =head2 Usage
  84. To get and use the data stored in the library context, simply do this:
  85. /*
  86. * ctx is received from a caller,
  87. */
  88. FOO *data = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_FOO_INDEX, &foo_method);
  89. =head2 Run Once
  90. void foo_cleanup(OSSL_LIB_CTX *ctx)
  91. {
  92. /* Free foo resources associated with ctx */
  93. }
  94. static ossl_lib_ctx_run_once_fn do_foo_init;
  95. static int do_foo_init(OSSL_LIB_CTX *ctx)
  96. {
  97. /* Allocate and initialise some foo resources and associated with ctx */
  98. return ossl_lib_ctx_onfree(ctx, &foo_cleanup)
  99. }
  100. int foo_some_function(OSSL_LIB_CTX *ctx)
  101. {
  102. if (!ossl_lib_ctx_run_once(ctx,
  103. OSSL_LIB_CTX_FOO_RUN_ONCE_INDEX,
  104. do_foo_init))
  105. return 0;
  106. /* Do some work using foo resources in ctx */
  107. }
  108. =head1 SEE ALSO
  109. L<OSSL_LIB_CTX(3)>
  110. =head1 COPYRIGHT
  111. Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  112. Licensed under the Apache License 2.0 (the "License"). You may not use
  113. this file except in compliance with the License. You can obtain a copy
  114. in the file LICENSE in the source distribution or at
  115. L<https://www.openssl.org/source/license.html>.
  116. =cut