2
0

evp_generic_fetch.pod 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. =pod
  2. =head1 NAME
  3. evp_generic_fetch, evp_generic_fetch_by_number
  4. - generic algorithm fetchers and method creators for EVP
  5. =head1 SYNOPSIS
  6. /* Only for EVP source */
  7. #include "evp_local.h"
  8. void *evp_generic_fetch(OSSL_LIB_CTX *libctx, int operation_id,
  9. const char *name, const char *properties,
  10. void *(*new_method)(int name_id,
  11. const OSSL_DISPATCH *fns,
  12. OSSL_PROVIDER *prov,
  13. void *method_data),
  14. void *method_data,
  15. int (*up_ref_method)(void *),
  16. void (*free_method)(void *));
  17. void *evp_generic_fetch_by_number(OSSL_LIB_CTX *ctx, int operation_id,
  18. int name_id, const char *properties,
  19. void *(*new_method)(int name_id,
  20. const OSSL_DISPATCH *fns,
  21. OSSL_PROVIDER *prov,
  22. void *method_data),
  23. void *method_data,
  24. int (*up_ref_method)(void *),
  25. void (*free_method)(void *));
  26. =head1 DESCRIPTION
  27. evp_generic_fetch() calls ossl_method_construct() with the given
  28. I<libctx>, I<operation_id>, I<name>, and I<properties> and uses
  29. it to create an EVP method with the help of the functions
  30. I<new_method>, I<up_ref_method>, and I<free_method>.
  31. evp_generic_fetch_by_number() does the same thing as evp_generic_fetch(),
  32. but takes a numeric I<name_id> instead of a name.
  33. I<name_id> must always be nonzero; as a matter of fact, it being zero
  34. is considered a programming error.
  35. This is meant to be used when one method needs to fetch an associated
  36. other method, and is typically called from inside the given function
  37. I<new_method>.
  38. The three functions I<new_method>, I<up_ref_method>, and
  39. I<free_method> are supposed to:
  40. =over 4
  41. =item new_method()
  42. creates an internal method from function pointers found in the
  43. dispatch table I<fns>, with name identity I<name_id>.
  44. The provider I<prov> and I<method_data> are also passed to be used as
  45. new_method() sees fit.
  46. =item up_ref_method()
  47. increments the reference counter for the given method, if there is
  48. one.
  49. =item free_method()
  50. frees the given method.
  51. =back
  52. =head1 RETURN VALUES
  53. evp_generic_fetch() returns a method on success, or NULL on error.
  54. =head1 EXAMPLES
  55. This is a short example of the fictitious EVP API and operation called
  56. B<EVP_FOO>.
  57. To begin with, let's assume something like this in
  58. F<include/openssl/core_dispatch.h>:
  59. #define OSSL_OP_FOO 100
  60. #define OSSL_FUNC_FOO_NEWCTX_FUNC 2001
  61. #define OSSL_FUNC_FOO_INIT 2002
  62. #define OSSL_FUNC_FOO_OPERATE 2003
  63. #define OSSL_FUNC_FOO_CLEANCTX_FUNC 2004
  64. #define OSSL_FUNC_FOO_FREECTX_FUNC 2005
  65. OSSL_CORE_MAKE_FUNC(void *, foo_newctx, (void))
  66. OSSL_CORE_MAKE_FUNC(int, foo_init, (void *vctx))
  67. OSSL_CORE_MAKE_FUNC(int, foo_operate, (void *vctx,
  68. unsigned char *out, size_t *out_l,
  69. unsigned char *in, size_t in_l))
  70. OSSL_CORE_MAKE_FUNC(void, foo_cleanctx, (void *vctx))
  71. OSSL_CORE_MAKE_FUNC(void, foo_freectx, (void *vctx))
  72. And here's the implementation of the FOO method fetcher:
  73. /* typedef struct evp_foo_st EVP_FOO */
  74. struct evp_foo_st {
  75. OSSL_PROVIDER *prov;
  76. int name_id;
  77. CRYPTO_REF_COUNT refcnt;
  78. OSSL_FUNC_foo_newctx_fn *newctx;
  79. OSSL_FUNC_foo_init_fn *init;
  80. OSSL_FUNC_foo_operate_fn *operate;
  81. OSSL_FUNC_foo_cleanctx_fn *cleanctx;
  82. OSSL_FUNC_foo_freectx_fn *freectx;
  83. };
  84. /*
  85. * In this example, we have a public method creator and destructor.
  86. * It's not absolutely necessary, but is in the spirit of OpenSSL.
  87. */
  88. EVP_FOO *EVP_FOO_meth_from_algorithm(int name_id,
  89. const OSSL_DISPATCH *fns,
  90. OSSL_PROVIDER *prov,
  91. void *data)
  92. {
  93. EVP_FOO *foo = NULL;
  94. if ((foo = OPENSSL_zalloc(sizeof(*foo))) == NULL)
  95. return NULL;
  96. foo->name_id = name_id;
  97. for (; fns->function_id != 0; fns++) {
  98. switch (fns->function_id) {
  99. case OSSL_FUNC_FOO_NEWCTX:
  100. foo->newctx = OSSL_FUNC_foo_newctx(fns);
  101. break;
  102. case OSSL_FUNC_FOO_INIT:
  103. foo->init = OSSL_FUNC_foo_init(fns);
  104. break;
  105. case OSSL_FUNC_FOO_OPERATE:
  106. foo->operate = OSSL_FUNC_foo_operate(fns);
  107. break;
  108. case OSSL_FUNC_FOO_CLEANCTX:
  109. foo->cleanctx = OSSL_FUNC_foo_cleanctx(fns);
  110. break;
  111. case OSSL_FUNC_FOO_FREECTX:
  112. foo->freectx = OSSL_FUNC_foo_freectx(fns);
  113. break;
  114. }
  115. }
  116. foo->prov = prov;
  117. if (prov)
  118. ossl_provider_up_ref(prov);
  119. return foo;
  120. }
  121. EVP_FOO_meth_free(EVP_FOO *foo)
  122. {
  123. if (foo != NULL) {
  124. OSSL_PROVIDER *prov = foo->prov;
  125. OPENSSL_free(foo);
  126. ossl_provider_free(prov);
  127. }
  128. }
  129. static void *foo_from_algorithm(const OSSL_DISPATCH *fns,
  130. OSSL_PROVIDER *prov)
  131. {
  132. return EVP_FOO_meth_from_algorithm(fns, prov);
  133. }
  134. static int foo_up_ref(void *vfoo)
  135. {
  136. EVP_FOO *foo = vfoo;
  137. int ref = 0;
  138. CRYPTO_UP_REF(&foo->refcnt, &ref, foo_lock);
  139. return 1;
  140. }
  141. static void foo_free(void *vfoo)
  142. {
  143. EVP_FOO_meth_free(vfoo);
  144. }
  145. EVP_FOO *EVP_FOO_fetch(OSSL_LIB_CTX *ctx,
  146. const char *name,
  147. const char *properties)
  148. {
  149. EVP_FOO *foo =
  150. evp_generic_fetch(ctx, OSSL_OP_FOO, name, properties,
  151. foo_from_algorithm, foo_up_ref, foo_free);
  152. /*
  153. * If this method exists in legacy form, with a constant NID for the
  154. * given |name|, this is the spot to find that NID and set it in
  155. * the newly constructed EVP_FOO instance.
  156. */
  157. return foo;
  158. }
  159. And finally, the library functions:
  160. /* typedef struct evp_foo_st EVP_FOO_CTX */
  161. struct evp_foo_ctx_st {
  162. const EVP_FOO *foo;
  163. void *provctx; /* corresponding provider context */
  164. };
  165. int EVP_FOO_CTX_reset(EVP_FOO_CTX *c)
  166. {
  167. if (c == NULL)
  168. return 1;
  169. if (c->foo != NULL && c->foo->cleanctx != NULL)
  170. c->foo->cleanctx(c->provctx);
  171. return 1;
  172. }
  173. EVP_FOO_CTX *EVP_FOO_CTX_new(void)
  174. {
  175. return OPENSSL_zalloc(sizeof(EVP_FOO_CTX));
  176. }
  177. void EVP_FOO_CTX_free(EVP_FOO_CTX *c)
  178. {
  179. EVP_FOO_CTX_reset(c);
  180. c->foo->freectx(c->provctx);
  181. OPENSSL_free(c);
  182. }
  183. int EVP_FooInit(EVP_FOO_CTX *c, const EVP_FOO *foo)
  184. {
  185. int ok = 1;
  186. c->foo = foo;
  187. if (c->provctx == NULL)
  188. c->provctx = c->foo->newctx();
  189. ok = c->foo->init(c->provctx);
  190. return ok;
  191. }
  192. int EVP_FooOperate(EVP_FOO_CTX *c, unsigned char *out, size_t *outl,
  193. const unsigned char *in, size_t inl)
  194. {
  195. int ok = 1;
  196. ok = c->foo->update(c->provctx, out, inl, &outl, in, inl);
  197. return ok;
  198. }
  199. =head1 SEE ALSO
  200. L<ossl_method_construct(3)>
  201. =head1 HISTORY
  202. The functions described here were all added in OpenSSL 3.0.
  203. =head1 COPYRIGHT
  204. Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  205. Licensed under the Apache License 2.0 (the "License"). You may not use
  206. this file except in compliance with the License. You can obtain a copy
  207. in the file LICENSE in the source distribution or at
  208. L<https://www.openssl.org/source/license.html>.
  209. =cut