117f01e99SJung-uk Kim /*
26f1af0d7SPierre Pronchery  * Copyright 2001-2023 The OpenSSL Project Authors. All Rights Reserved.
317f01e99SJung-uk Kim  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
417f01e99SJung-uk Kim  *
5b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
617f01e99SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
717f01e99SJung-uk Kim  * in the file LICENSE in the source distribution or at
817f01e99SJung-uk Kim  * https://www.openssl.org/source/license.html
917f01e99SJung-uk Kim  */
1017f01e99SJung-uk Kim 
1117f01e99SJung-uk Kim #ifndef OSSL_CRYPTO_ENGINE_ENG_LOCAL_H
1217f01e99SJung-uk Kim # define OSSL_CRYPTO_ENGINE_ENG_LOCAL_H
1317f01e99SJung-uk Kim 
14b077aed3SPierre Pronchery # include <openssl/trace.h>
1517f01e99SJung-uk Kim # include "internal/cryptlib.h"
1617f01e99SJung-uk Kim # include "crypto/engine.h"
1717f01e99SJung-uk Kim # include "internal/thread_once.h"
1817f01e99SJung-uk Kim # include "internal/refcount.h"
1917f01e99SJung-uk Kim 
2017f01e99SJung-uk Kim extern CRYPTO_RWLOCK *global_engine_lock;
2117f01e99SJung-uk Kim 
2217f01e99SJung-uk Kim /*
23b077aed3SPierre Pronchery  * This prints the engine's pointer address, "struct" or "funct" to
24b077aed3SPierre Pronchery  * indicate the reference type, the before and after reference count, and
25b077aed3SPierre Pronchery  * the file:line-number pair. The "ENGINE_REF_PRINT" statements must come
26b077aed3SPierre Pronchery  * *after* the change.
2717f01e99SJung-uk Kim  */
28b077aed3SPierre Pronchery # define ENGINE_REF_PRINT(e, isfunct, diff)                             \
29b077aed3SPierre Pronchery     OSSL_TRACE6(ENGINE_REF_COUNT,                                       \
30b077aed3SPierre Pronchery                "engine: %p %s from %d to %d (%s:%d)\n",                 \
31b077aed3SPierre Pronchery                (void *)(e), (isfunct ? "funct" : "struct"),             \
32b077aed3SPierre Pronchery                ((isfunct)                                               \
33b077aed3SPierre Pronchery                 ? ((e)->funct_ref - (diff))                             \
34b077aed3SPierre Pronchery                 : ((e)->struct_ref - (diff))),                          \
3517f01e99SJung-uk Kim                ((isfunct) ? (e)->funct_ref : (e)->struct_ref),          \
3617f01e99SJung-uk Kim                (OPENSSL_FILE), (OPENSSL_LINE))
3717f01e99SJung-uk Kim 
3817f01e99SJung-uk Kim /*
3917f01e99SJung-uk Kim  * Any code that will need cleanup operations should use these functions to
4017f01e99SJung-uk Kim  * register callbacks. engine_cleanup_int() will call all registered
4117f01e99SJung-uk Kim  * callbacks in order. NB: both the "add" functions assume the engine lock to
4217f01e99SJung-uk Kim  * already be held (in "write" mode).
4317f01e99SJung-uk Kim  */
4417f01e99SJung-uk Kim typedef void (ENGINE_CLEANUP_CB) (void);
4517f01e99SJung-uk Kim typedef struct st_engine_cleanup_item {
4617f01e99SJung-uk Kim     ENGINE_CLEANUP_CB *cb;
4717f01e99SJung-uk Kim } ENGINE_CLEANUP_ITEM;
4817f01e99SJung-uk Kim DEFINE_STACK_OF(ENGINE_CLEANUP_ITEM)
496f1af0d7SPierre Pronchery int engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb);
506f1af0d7SPierre Pronchery int engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb);
5117f01e99SJung-uk Kim 
5217f01e99SJung-uk Kim /* We need stacks of ENGINEs for use in eng_table.c */
5317f01e99SJung-uk Kim DEFINE_STACK_OF(ENGINE)
5417f01e99SJung-uk Kim 
5517f01e99SJung-uk Kim /*
5617f01e99SJung-uk Kim  * This represents an implementation table. Dependent code should instantiate
5717f01e99SJung-uk Kim  * it as a (ENGINE_TABLE *) pointer value set initially to NULL.
5817f01e99SJung-uk Kim  */
5917f01e99SJung-uk Kim typedef struct st_engine_table ENGINE_TABLE;
6017f01e99SJung-uk Kim int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
6117f01e99SJung-uk Kim                           ENGINE *e, const int *nids, int num_nids,
6217f01e99SJung-uk Kim                           int setdefault);
6317f01e99SJung-uk Kim void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e);
6417f01e99SJung-uk Kim void engine_table_cleanup(ENGINE_TABLE **table);
65b077aed3SPierre Pronchery ENGINE *ossl_engine_table_select(ENGINE_TABLE **table, int nid,
66b077aed3SPierre Pronchery                                  const char *f, int l);
6717f01e99SJung-uk Kim typedef void (engine_table_doall_cb) (int nid, STACK_OF(ENGINE) *sk,
6817f01e99SJung-uk Kim                                       ENGINE *def, void *arg);
6917f01e99SJung-uk Kim void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
7017f01e99SJung-uk Kim                         void *arg);
7117f01e99SJung-uk Kim 
7217f01e99SJung-uk Kim /*
7317f01e99SJung-uk Kim  * Internal versions of API functions that have control over locking. These
7417f01e99SJung-uk Kim  * are used between C files when functionality needs to be shared but the
7517f01e99SJung-uk Kim  * caller may already be controlling of the engine lock.
7617f01e99SJung-uk Kim  */
7717f01e99SJung-uk Kim int engine_unlocked_init(ENGINE *e);
7817f01e99SJung-uk Kim int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers);
7917f01e99SJung-uk Kim int engine_free_util(ENGINE *e, int not_locked);
8017f01e99SJung-uk Kim 
8117f01e99SJung-uk Kim /*
8217f01e99SJung-uk Kim  * This function will reset all "set"able values in an ENGINE to NULL. This
8317f01e99SJung-uk Kim  * won't touch reference counts or ex_data, but is equivalent to calling all
8417f01e99SJung-uk Kim  * the ENGINE_set_***() functions with a NULL value.
8517f01e99SJung-uk Kim  */
8617f01e99SJung-uk Kim void engine_set_all_null(ENGINE *e);
8717f01e99SJung-uk Kim 
8817f01e99SJung-uk Kim /*
8917f01e99SJung-uk Kim  * NB: Bitwise OR-able values for the "flags" variable in ENGINE are now
9017f01e99SJung-uk Kim  * exposed in engine.h.
9117f01e99SJung-uk Kim  */
9217f01e99SJung-uk Kim 
9317f01e99SJung-uk Kim /* Free up dynamically allocated public key methods associated with ENGINE */
9417f01e99SJung-uk Kim 
9517f01e99SJung-uk Kim void engine_pkey_meths_free(ENGINE *e);
9617f01e99SJung-uk Kim void engine_pkey_asn1_meths_free(ENGINE *e);
9717f01e99SJung-uk Kim 
9817f01e99SJung-uk Kim /* Once initialisation function */
9917f01e99SJung-uk Kim extern CRYPTO_ONCE engine_lock_init;
10017f01e99SJung-uk Kim DECLARE_RUN_ONCE(do_engine_lock_init)
10117f01e99SJung-uk Kim 
102b2bf0c7eSJung-uk Kim typedef void (*ENGINE_DYNAMIC_ID)(void);
103b2bf0c7eSJung-uk Kim int engine_add_dynamic_id(ENGINE *e, ENGINE_DYNAMIC_ID dynamic_id,
104b2bf0c7eSJung-uk Kim                           int not_locked);
105b2bf0c7eSJung-uk Kim void engine_remove_dynamic_id(ENGINE *e, int not_locked);
106b2bf0c7eSJung-uk Kim 
10717f01e99SJung-uk Kim /*
10817f01e99SJung-uk Kim  * This is a structure for storing implementations of various crypto
10917f01e99SJung-uk Kim  * algorithms and functions.
11017f01e99SJung-uk Kim  */
11117f01e99SJung-uk Kim struct engine_st {
11217f01e99SJung-uk Kim     const char *id;
11317f01e99SJung-uk Kim     const char *name;
11417f01e99SJung-uk Kim     const RSA_METHOD *rsa_meth;
11517f01e99SJung-uk Kim     const DSA_METHOD *dsa_meth;
11617f01e99SJung-uk Kim     const DH_METHOD *dh_meth;
11717f01e99SJung-uk Kim     const EC_KEY_METHOD *ec_meth;
11817f01e99SJung-uk Kim     const RAND_METHOD *rand_meth;
11917f01e99SJung-uk Kim     /* Cipher handling is via this callback */
12017f01e99SJung-uk Kim     ENGINE_CIPHERS_PTR ciphers;
12117f01e99SJung-uk Kim     /* Digest handling is via this callback */
12217f01e99SJung-uk Kim     ENGINE_DIGESTS_PTR digests;
12317f01e99SJung-uk Kim     /* Public key handling via this callback */
12417f01e99SJung-uk Kim     ENGINE_PKEY_METHS_PTR pkey_meths;
12517f01e99SJung-uk Kim     /* ASN1 public key handling via this callback */
12617f01e99SJung-uk Kim     ENGINE_PKEY_ASN1_METHS_PTR pkey_asn1_meths;
12717f01e99SJung-uk Kim     ENGINE_GEN_INT_FUNC_PTR destroy;
12817f01e99SJung-uk Kim     ENGINE_GEN_INT_FUNC_PTR init;
12917f01e99SJung-uk Kim     ENGINE_GEN_INT_FUNC_PTR finish;
13017f01e99SJung-uk Kim     ENGINE_CTRL_FUNC_PTR ctrl;
13117f01e99SJung-uk Kim     ENGINE_LOAD_KEY_PTR load_privkey;
13217f01e99SJung-uk Kim     ENGINE_LOAD_KEY_PTR load_pubkey;
13317f01e99SJung-uk Kim     ENGINE_SSL_CLIENT_CERT_PTR load_ssl_client_cert;
13417f01e99SJung-uk Kim     const ENGINE_CMD_DEFN *cmd_defns;
13517f01e99SJung-uk Kim     int flags;
13617f01e99SJung-uk Kim     /* reference count on the structure itself */
13717f01e99SJung-uk Kim     CRYPTO_REF_COUNT struct_ref;
13817f01e99SJung-uk Kim     /*
13917f01e99SJung-uk Kim      * reference count on usability of the engine type. NB: This controls the
14017f01e99SJung-uk Kim      * loading and initialisation of any functionality required by this
14117f01e99SJung-uk Kim      * engine, whereas the previous count is simply to cope with
14217f01e99SJung-uk Kim      * (de)allocation of this structure. Hence, running_ref <= struct_ref at
14317f01e99SJung-uk Kim      * all times.
14417f01e99SJung-uk Kim      */
14517f01e99SJung-uk Kim     int funct_ref;
14617f01e99SJung-uk Kim     /* A place to store per-ENGINE data */
14717f01e99SJung-uk Kim     CRYPTO_EX_DATA ex_data;
14817f01e99SJung-uk Kim     /* Used to maintain the linked-list of engines. */
14917f01e99SJung-uk Kim     struct engine_st *prev;
15017f01e99SJung-uk Kim     struct engine_st *next;
151b2bf0c7eSJung-uk Kim     /* Used to maintain the linked-list of dynamic engines. */
152b2bf0c7eSJung-uk Kim     struct engine_st *prev_dyn;
153b2bf0c7eSJung-uk Kim     struct engine_st *next_dyn;
154b2bf0c7eSJung-uk Kim     ENGINE_DYNAMIC_ID dynamic_id;
15517f01e99SJung-uk Kim };
15617f01e99SJung-uk Kim 
15717f01e99SJung-uk Kim typedef struct st_engine_pile ENGINE_PILE;
15817f01e99SJung-uk Kim 
15917f01e99SJung-uk Kim DEFINE_LHASH_OF(ENGINE_PILE);
16017f01e99SJung-uk Kim 
16117f01e99SJung-uk Kim #endif                          /* OSSL_CRYPTO_ENGINE_ENG_LOCAL_H */
162