2
0

eng_local.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright 2001-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #ifndef OSSL_CRYPTO_ENGINE_ENG_LOCAL_H
  11. # define OSSL_CRYPTO_ENGINE_ENG_LOCAL_H
  12. # include <openssl/trace.h>
  13. # include "internal/cryptlib.h"
  14. # include "crypto/engine.h"
  15. # include "internal/thread_once.h"
  16. # include "internal/refcount.h"
  17. extern CRYPTO_RWLOCK *global_engine_lock;
  18. /*
  19. * This prints the engine's pointer address, "struct" or "funct" to
  20. * indicate the reference type, the before and after reference count, and
  21. * the file:line-number pair. The "ENGINE_REF_PRINT" statements must come
  22. * *after* the change.
  23. */
  24. # define ENGINE_REF_PRINT(e, isfunct, diff) \
  25. OSSL_TRACE6(ENGINE_REF_COUNT, \
  26. "engine: %p %s from %d to %d (%s:%d)\n", \
  27. (void *)(e), (isfunct ? "funct" : "struct"), \
  28. ((isfunct) \
  29. ? ((e)->funct_ref - (diff)) \
  30. : (eng_struct_ref(e) - (diff))), \
  31. ((isfunct) ? (e)->funct_ref : eng_struct_ref(e)), \
  32. (OPENSSL_FILE), (OPENSSL_LINE))
  33. /*
  34. * Any code that will need cleanup operations should use these functions to
  35. * register callbacks. engine_cleanup_int() will call all registered
  36. * callbacks in order. NB: both the "add" functions assume the engine lock to
  37. * already be held (in "write" mode).
  38. */
  39. typedef void (ENGINE_CLEANUP_CB) (void);
  40. typedef struct st_engine_cleanup_item {
  41. ENGINE_CLEANUP_CB *cb;
  42. } ENGINE_CLEANUP_ITEM;
  43. DEFINE_STACK_OF(ENGINE_CLEANUP_ITEM)
  44. int engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb);
  45. int engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb);
  46. /* We need stacks of ENGINEs for use in eng_table.c */
  47. DEFINE_STACK_OF(ENGINE)
  48. /*
  49. * This represents an implementation table. Dependent code should instantiate
  50. * it as a (ENGINE_TABLE *) pointer value set initially to NULL.
  51. */
  52. typedef struct st_engine_table ENGINE_TABLE;
  53. int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
  54. ENGINE *e, const int *nids, int num_nids,
  55. int setdefault);
  56. void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e);
  57. void engine_table_cleanup(ENGINE_TABLE **table);
  58. ENGINE *ossl_engine_table_select(ENGINE_TABLE **table, int nid,
  59. const char *f, int l);
  60. typedef void (engine_table_doall_cb) (int nid, STACK_OF(ENGINE) *sk,
  61. ENGINE *def, void *arg);
  62. void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
  63. void *arg);
  64. /*
  65. * Internal versions of API functions that have control over locking. These
  66. * are used between C files when functionality needs to be shared but the
  67. * caller may already be controlling of the engine lock.
  68. */
  69. int engine_unlocked_init(ENGINE *e);
  70. int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers);
  71. int engine_free_util(ENGINE *e, int not_locked);
  72. /*
  73. * This function will reset all "set"able values in an ENGINE to NULL. This
  74. * won't touch reference counts or ex_data, but is equivalent to calling all
  75. * the ENGINE_set_***() functions with a NULL value.
  76. */
  77. void engine_set_all_null(ENGINE *e);
  78. /*
  79. * NB: Bitwise OR-able values for the "flags" variable in ENGINE are now
  80. * exposed in engine.h.
  81. */
  82. /* Free up dynamically allocated public key methods associated with ENGINE */
  83. void engine_pkey_meths_free(ENGINE *e);
  84. void engine_pkey_asn1_meths_free(ENGINE *e);
  85. /* Once initialisation function */
  86. extern CRYPTO_ONCE engine_lock_init;
  87. DECLARE_RUN_ONCE(do_engine_lock_init)
  88. typedef void (*ENGINE_DYNAMIC_ID)(void);
  89. int engine_add_dynamic_id(ENGINE *e, ENGINE_DYNAMIC_ID dynamic_id,
  90. int not_locked);
  91. void engine_remove_dynamic_id(ENGINE *e, int not_locked);
  92. /*
  93. * This is a structure for storing implementations of various crypto
  94. * algorithms and functions.
  95. */
  96. struct engine_st {
  97. const char *id;
  98. const char *name;
  99. const RSA_METHOD *rsa_meth;
  100. const DSA_METHOD *dsa_meth;
  101. const DH_METHOD *dh_meth;
  102. const EC_KEY_METHOD *ec_meth;
  103. const RAND_METHOD *rand_meth;
  104. /* Cipher handling is via this callback */
  105. ENGINE_CIPHERS_PTR ciphers;
  106. /* Digest handling is via this callback */
  107. ENGINE_DIGESTS_PTR digests;
  108. /* Public key handling via this callback */
  109. ENGINE_PKEY_METHS_PTR pkey_meths;
  110. /* ASN1 public key handling via this callback */
  111. ENGINE_PKEY_ASN1_METHS_PTR pkey_asn1_meths;
  112. ENGINE_GEN_INT_FUNC_PTR destroy;
  113. ENGINE_GEN_INT_FUNC_PTR init;
  114. ENGINE_GEN_INT_FUNC_PTR finish;
  115. ENGINE_CTRL_FUNC_PTR ctrl;
  116. ENGINE_LOAD_KEY_PTR load_privkey;
  117. ENGINE_LOAD_KEY_PTR load_pubkey;
  118. ENGINE_SSL_CLIENT_CERT_PTR load_ssl_client_cert;
  119. const ENGINE_CMD_DEFN *cmd_defns;
  120. int flags;
  121. /* reference count on the structure itself */
  122. CRYPTO_REF_COUNT struct_ref;
  123. /*
  124. * reference count on usability of the engine type. NB: This controls the
  125. * loading and initialisation of any functionality required by this
  126. * engine, whereas the previous count is simply to cope with
  127. * (de)allocation of this structure. Hence, running_ref <= struct_ref at
  128. * all times.
  129. */
  130. int funct_ref;
  131. /* A place to store per-ENGINE data */
  132. CRYPTO_EX_DATA ex_data;
  133. /* Used to maintain the linked-list of engines. */
  134. struct engine_st *prev;
  135. struct engine_st *next;
  136. /* Used to maintain the linked-list of dynamic engines. */
  137. struct engine_st *prev_dyn;
  138. struct engine_st *next_dyn;
  139. ENGINE_DYNAMIC_ID dynamic_id;
  140. };
  141. typedef struct st_engine_pile ENGINE_PILE;
  142. DEFINE_LHASH_OF_EX(ENGINE_PILE);
  143. static ossl_unused ossl_inline int eng_struct_ref(ENGINE *e)
  144. {
  145. int res;
  146. CRYPTO_GET_REF(&e->struct_ref, &res);
  147. return res;
  148. }
  149. #endif /* OSSL_CRYPTO_ENGINE_ENG_LOCAL_H */