CRYPTO_get_ex_new_index.pod 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. =pod
  2. =head1 NAME
  3. CRYPTO_EX_new, CRYPTO_EX_free, CRYPTO_EX_dup,
  4. CRYPTO_free_ex_index, CRYPTO_get_ex_new_index,
  5. CRYPTO_alloc_ex_data, CRYPTO_set_ex_data, CRYPTO_get_ex_data,
  6. CRYPTO_free_ex_data, CRYPTO_new_ex_data
  7. - functions supporting application-specific data
  8. =head1 SYNOPSIS
  9. #include <openssl/crypto.h>
  10. int CRYPTO_get_ex_new_index(int class_index,
  11. long argl, void *argp,
  12. CRYPTO_EX_new *new_func,
  13. CRYPTO_EX_dup *dup_func,
  14. CRYPTO_EX_free *free_func);
  15. typedef void CRYPTO_EX_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  16. int idx, long argl, void *argp);
  17. typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  18. int idx, long argl, void *argp);
  19. typedef int CRYPTO_EX_dup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
  20. void **from_d, int idx, long argl, void *argp);
  21. int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
  22. int CRYPTO_alloc_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad,
  23. int idx);
  24. int CRYPTO_set_ex_data(CRYPTO_EX_DATA *r, int idx, void *arg);
  25. void *CRYPTO_get_ex_data(CRYPTO_EX_DATA *r, int idx);
  26. void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *r);
  27. int CRYPTO_free_ex_index(int class_index, int idx);
  28. =head1 DESCRIPTION
  29. Several OpenSSL structures can have application-specific data attached to them,
  30. known as "exdata."
  31. The specific structures are:
  32. BIO
  33. DH
  34. DSA
  35. EC_KEY
  36. ENGINE
  37. EVP_PKEY
  38. RSA
  39. SSL
  40. SSL_CTX
  41. SSL_SESSION
  42. UI
  43. UI_METHOD
  44. X509
  45. X509_STORE
  46. X509_STORE_CTX
  47. In addition, the B<APP> name is reserved for use by application code.
  48. Each is identified by an B<CRYPTO_EX_INDEX_xxx> define in the B<crypto.h>
  49. header file. In addition, B<CRYPTO_EX_INDEX_APP> is reserved for
  50. applications to use this facility for their own structures.
  51. The API described here is used by OpenSSL to manipulate exdata for specific
  52. structures. Since the application data can be anything at all it is passed
  53. and retrieved as a B<void *> type.
  54. The B<CRYPTO_EX_DATA> type is opaque. To initialize the exdata part of
  55. a structure, call CRYPTO_new_ex_data(). This is only necessary for
  56. B<CRYPTO_EX_INDEX_APP> objects.
  57. Exdata types are identified by an B<index>, an integer guaranteed to be
  58. unique within structures for the lifetime of the program. Applications
  59. using exdata typically call B<CRYPTO_get_ex_new_index> at startup, and
  60. store the result in a global variable, or write a wrapper function to
  61. provide lazy evaluation. The B<class_index> should be one of the
  62. B<CRYPTO_EX_INDEX_xxx> values. The B<argl> and B<argp> parameters are saved
  63. to be passed to the callbacks but are otherwise not used. In order to
  64. transparently manipulate exdata, three callbacks must be provided. The
  65. semantics of those callbacks are described below.
  66. When copying or releasing objects with exdata, the callback functions
  67. are called in increasing order of their B<index> value.
  68. If a dynamic library can be unloaded, it should call CRYPTO_free_ex_index()
  69. when this is done.
  70. This will replace the callbacks with no-ops
  71. so that applications don't crash. Any existing exdata will be leaked.
  72. To set or get the exdata on an object, the appropriate type-specific
  73. routine must be used. This is because the containing structure is opaque
  74. and the B<CRYPTO_EX_DATA> field is not accessible. In both API's, the
  75. B<idx> parameter should be an already-created index value.
  76. When setting exdata, the pointer specified with a particular index is saved,
  77. and returned on a subsequent "get" call. If the application is going to
  78. release the data, it must make sure to set a B<NULL> value at the index,
  79. to avoid likely double-free crashes.
  80. The function B<CRYPTO_free_ex_data> is used to free all exdata attached
  81. to a structure. The appropriate type-specific routine must be used.
  82. The B<class_index> identifies the structure type, the B<obj> is
  83. a pointer to the actual structure, and B<r> is a pointer to the
  84. structure's exdata field.
  85. =head2 Callback Functions
  86. This section describes how the callback functions are used. Applications
  87. that are defining their own exdata using B<CYPRTO_EX_INDEX_APP> must
  88. call them as described here.
  89. When a structure is initially allocated (such as RSA_new()) then the
  90. new_func() is called for every defined index. There is no requirement
  91. that the entire parent, or containing, structure has been set up.
  92. The new_func() is typically used only to allocate memory to store the
  93. exdata, and perhaps an "initialized" flag within that memory.
  94. The exdata value may be allocated later on with CRYPTO_alloc_ex_data(),
  95. or may be set by calling CRYPTO_set_ex_data().
  96. When a structure is free'd (such as SSL_CTX_free()) then the
  97. free_func() is called for every defined index. Again, the state of the
  98. parent structure is not guaranteed. The free_func() may be called with a
  99. NULL pointer.
  100. Both new_func() and free_func() take the same parameters.
  101. The B<parent> is the pointer to the structure that contains the exdata.
  102. The B<ptr> is the current exdata item; for new_func() this will typically
  103. be NULL. The B<r> parameter is a pointer to the exdata field of the object.
  104. The B<idx> is the index and is the value returned when the callbacks were
  105. initially registered via CRYPTO_get_ex_new_index() and can be used if
  106. the same callback handles different types of exdata.
  107. dup_func() is called when a structure is being copied. This is only done
  108. for B<SSL>, B<SSL_SESSION>, B<EC_KEY> objects and B<BIO> chains via
  109. BIO_dup_chain(). The B<to> and B<from> parameters
  110. are pointers to the destination and source B<CRYPTO_EX_DATA> structures,
  111. respectively. The B<*from_d> parameter is a pointer to the source exdata.
  112. When the dup_func() returns, the value in B<*from_d> is copied to the
  113. destination ex_data. If the pointer contained in B<*pptr> is not modified
  114. by the dup_func(), then both B<to> and B<from> will point to the same data.
  115. The B<idx>, B<argl> and B<argp> parameters are as described for the other
  116. two callbacks. If the dup_func() returns B<0> the whole CRYPTO_dup_ex_data()
  117. will fail.
  118. =head1 RETURN VALUES
  119. CRYPTO_get_ex_new_index() returns a new index or -1 on failure.
  120. CRYPTO_free_ex_index(), CRYPTO_alloc_ex_data() and CRYPTO_set_ex_data()
  121. return 1 on success or 0 on failure.
  122. CRYPTO_get_ex_data() returns the application data or NULL on failure;
  123. note that NULL may be a valid value.
  124. dup_func() should return 0 for failure and 1 for success.
  125. =head1 HISTORY
  126. CRYPTO_alloc_ex_data() was added in OpenSSL 3.0.
  127. The signature of the dup_func() callback was changed in OpenSSL 3.0 to use the
  128. type B<void **> for B<from_d>. Previously this parameter was of type B<void *>.
  129. Support for ENGINE "exdata" was deprecated in OpenSSL 3.0.
  130. =head1 COPYRIGHT
  131. Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
  132. Licensed under the Apache License 2.0 (the "License"). You may not use
  133. this file except in compliance with the License. You can obtain a copy
  134. in the file LICENSE in the source distribution or at
  135. L<https://www.openssl.org/source/license.html>.
  136. =cut