1109998Smarkm/* crypto/engine/eng_int.h */
2296341Sdelphij/*
3296341Sdelphij * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
4296341Sdelphij * 2000.
5109998Smarkm */
6109998Smarkm/* ====================================================================
7109998Smarkm * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
8109998Smarkm *
9109998Smarkm * Redistribution and use in source and binary forms, with or without
10109998Smarkm * modification, are permitted provided that the following conditions
11109998Smarkm * are met:
12109998Smarkm *
13109998Smarkm * 1. Redistributions of source code must retain the above copyright
14296341Sdelphij *    notice, this list of conditions and the following disclaimer.
15109998Smarkm *
16109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
17109998Smarkm *    notice, this list of conditions and the following disclaimer in
18109998Smarkm *    the documentation and/or other materials provided with the
19109998Smarkm *    distribution.
20109998Smarkm *
21109998Smarkm * 3. All advertising materials mentioning features or use of this
22109998Smarkm *    software must display the following acknowledgment:
23109998Smarkm *    "This product includes software developed by the OpenSSL Project
24109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25109998Smarkm *
26109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27109998Smarkm *    endorse or promote products derived from this software without
28109998Smarkm *    prior written permission. For written permission, please contact
29109998Smarkm *    licensing@OpenSSL.org.
30109998Smarkm *
31109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
32109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
33109998Smarkm *    permission of the OpenSSL Project.
34109998Smarkm *
35109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
36109998Smarkm *    acknowledgment:
37109998Smarkm *    "This product includes software developed by the OpenSSL Project
38109998Smarkm *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39109998Smarkm *
40109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
52109998Smarkm * ====================================================================
53109998Smarkm *
54109998Smarkm * This product includes cryptographic software written by Eric Young
55109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
56109998Smarkm * Hudson (tjh@cryptsoft.com).
57109998Smarkm *
58109998Smarkm */
59160814Ssimon/* ====================================================================
60160814Ssimon * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
61296341Sdelphij * ECDH support in OpenSSL originally developed by
62160814Ssimon * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
63160814Ssimon */
64109998Smarkm
65109998Smarkm#ifndef HEADER_ENGINE_INT_H
66296341Sdelphij# define HEADER_ENGINE_INT_H
67109998Smarkm
68296341Sdelphij# include "cryptlib.h"
69109998Smarkm/* Take public definitions from engine.h */
70296341Sdelphij# include <openssl/engine.h>
71109998Smarkm
72109998Smarkm#ifdef  __cplusplus
73109998Smarkmextern "C" {
74109998Smarkm#endif
75109998Smarkm
76296341Sdelphij/*
77296341Sdelphij * If we compile with this symbol defined, then both reference counts in the
78296341Sdelphij * ENGINE structure will be monitored with a line of output on stderr for
79296341Sdelphij * each change. This prints the engine's pointer address (truncated to
80296341Sdelphij * unsigned int), "struct" or "funct" to indicate the reference type, the
81296341Sdelphij * before and after reference count, and the file:line-number pair. The
82296341Sdelphij * "engine_ref_debug" statements must come *after* the change.
83296341Sdelphij */
84296341Sdelphij# ifdef ENGINE_REF_COUNT_DEBUG
85109998Smarkm
86296341Sdelphij#  define engine_ref_debug(e, isfunct, diff) \
87296341Sdelphij        fprintf(stderr, "engine: %08x %s from %d to %d (%s:%d)\n", \
88296341Sdelphij                (unsigned int)(e), (isfunct ? "funct" : "struct"), \
89296341Sdelphij                ((isfunct) ? ((e)->funct_ref - (diff)) : ((e)->struct_ref - (diff))), \
90296341Sdelphij                ((isfunct) ? (e)->funct_ref : (e)->struct_ref), \
91296341Sdelphij                (__FILE__), (__LINE__));
92109998Smarkm
93296341Sdelphij# else
94109998Smarkm
95296341Sdelphij#  define engine_ref_debug(e, isfunct, diff)
96109998Smarkm
97296341Sdelphij# endif
98109998Smarkm
99296341Sdelphij/*
100296341Sdelphij * Any code that will need cleanup operations should use these functions to
101109998Smarkm * register callbacks. ENGINE_cleanup() will call all registered callbacks in
102109998Smarkm * order. NB: both the "add" functions assume CRYPTO_LOCK_ENGINE to already be
103296341Sdelphij * held (in "write" mode).
104296341Sdelphij */
105296341Sdelphijtypedef void (ENGINE_CLEANUP_CB) (void);
106296341Sdelphijtypedef struct st_engine_cleanup_item {
107296341Sdelphij    ENGINE_CLEANUP_CB *cb;
108296341Sdelphij} ENGINE_CLEANUP_ITEM;
109109998SmarkmDECLARE_STACK_OF(ENGINE_CLEANUP_ITEM)
110109998Smarkmvoid engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb);
111109998Smarkmvoid engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb);
112109998Smarkm
113109998Smarkm/* We need stacks of ENGINEs for use in eng_table.c */
114109998SmarkmDECLARE_STACK_OF(ENGINE)
115109998Smarkm
116296341Sdelphij/*
117296341Sdelphij * If this symbol is defined then engine_table_select(), the function that is
118296341Sdelphij * used by RSA, DSA (etc) code to select registered ENGINEs, cache defaults
119296341Sdelphij * and functional references (etc), will display debugging summaries to
120296341Sdelphij * stderr.
121296341Sdelphij */
122109998Smarkm/* #define ENGINE_TABLE_DEBUG */
123109998Smarkm
124296341Sdelphij/*
125296341Sdelphij * This represents an implementation table. Dependent code should instantiate
126296341Sdelphij * it as a (ENGINE_TABLE *) pointer value set initially to NULL.
127296341Sdelphij */
128109998Smarkmtypedef struct st_engine_table ENGINE_TABLE;
129109998Smarkmint engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
130296341Sdelphij                          ENGINE *e, const int *nids, int num_nids,
131296341Sdelphij                          int setdefault);
132109998Smarkmvoid engine_table_unregister(ENGINE_TABLE **table, ENGINE *e);
133109998Smarkmvoid engine_table_cleanup(ENGINE_TABLE **table);
134296341Sdelphij# ifndef ENGINE_TABLE_DEBUG
135109998SmarkmENGINE *engine_table_select(ENGINE_TABLE **table, int nid);
136296341Sdelphij# else
137296341SdelphijENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,
138296341Sdelphij                                int l);
139296341Sdelphij#  define engine_table_select(t,n) engine_table_select_tmp(t,n,__FILE__,__LINE__)
140296341Sdelphij# endif
141296341Sdelphijtypedef void (engine_table_doall_cb) (int nid, STACK_OF(ENGINE) *sk,
142296341Sdelphij                                      ENGINE *def, void *arg);
143296341Sdelphijvoid engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
144296341Sdelphij                        void *arg);
145109998Smarkm
146296341Sdelphij/*
147296341Sdelphij * Internal versions of API functions that have control over locking. These
148296341Sdelphij * are used between C files when functionality needs to be shared but the
149296341Sdelphij * caller may already be controlling of the CRYPTO_LOCK_ENGINE lock.
150296341Sdelphij */
151109998Smarkmint engine_unlocked_init(ENGINE *e);
152109998Smarkmint engine_unlocked_finish(ENGINE *e, int unlock_for_handlers);
153109998Smarkmint engine_free_util(ENGINE *e, int locked);
154109998Smarkm
155296341Sdelphij/*
156296341Sdelphij * This function will reset all "set"able values in an ENGINE to NULL. This
157296341Sdelphij * won't touch reference counts or ex_data, but is equivalent to calling all
158296341Sdelphij * the ENGINE_set_***() functions with a NULL value.
159296341Sdelphij */
160109998Smarkmvoid engine_set_all_null(ENGINE *e);
161109998Smarkm
162296341Sdelphij/*
163296341Sdelphij * NB: Bitwise OR-able values for the "flags" variable in ENGINE are now
164296341Sdelphij * exposed in engine.h.
165296341Sdelphij */
166109998Smarkm
167238405Sjkim/* Free up dynamically allocated public key methods associated with ENGINE */
168238405Sjkim
169238405Sjkimvoid engine_pkey_meths_free(ENGINE *e);
170238405Sjkimvoid engine_pkey_asn1_meths_free(ENGINE *e);
171238405Sjkim
172296341Sdelphij/*
173296341Sdelphij * This is a structure for storing implementations of various crypto
174296341Sdelphij * algorithms and functions.
175296341Sdelphij */
176296341Sdelphijstruct engine_st {
177296341Sdelphij    const char *id;
178296341Sdelphij    const char *name;
179296341Sdelphij    const RSA_METHOD *rsa_meth;
180296341Sdelphij    const DSA_METHOD *dsa_meth;
181296341Sdelphij    const DH_METHOD *dh_meth;
182296341Sdelphij    const ECDH_METHOD *ecdh_meth;
183296341Sdelphij    const ECDSA_METHOD *ecdsa_meth;
184296341Sdelphij    const RAND_METHOD *rand_meth;
185296341Sdelphij    const STORE_METHOD *store_meth;
186296341Sdelphij    /* Cipher handling is via this callback */
187296341Sdelphij    ENGINE_CIPHERS_PTR ciphers;
188296341Sdelphij    /* Digest handling is via this callback */
189296341Sdelphij    ENGINE_DIGESTS_PTR digests;
190296341Sdelphij    /* Public key handling via this callback */
191296341Sdelphij    ENGINE_PKEY_METHS_PTR pkey_meths;
192296341Sdelphij    /* ASN1 public key handling via this callback */
193296341Sdelphij    ENGINE_PKEY_ASN1_METHS_PTR pkey_asn1_meths;
194296341Sdelphij    ENGINE_GEN_INT_FUNC_PTR destroy;
195296341Sdelphij    ENGINE_GEN_INT_FUNC_PTR init;
196296341Sdelphij    ENGINE_GEN_INT_FUNC_PTR finish;
197296341Sdelphij    ENGINE_CTRL_FUNC_PTR ctrl;
198296341Sdelphij    ENGINE_LOAD_KEY_PTR load_privkey;
199296341Sdelphij    ENGINE_LOAD_KEY_PTR load_pubkey;
200296341Sdelphij    ENGINE_SSL_CLIENT_CERT_PTR load_ssl_client_cert;
201296341Sdelphij    const ENGINE_CMD_DEFN *cmd_defns;
202296341Sdelphij    int flags;
203296341Sdelphij    /* reference count on the structure itself */
204296341Sdelphij    int struct_ref;
205296341Sdelphij    /*
206296341Sdelphij     * reference count on usability of the engine type. NB: This controls the
207296341Sdelphij     * loading and initialisation of any functionlity required by this
208296341Sdelphij     * engine, whereas the previous count is simply to cope with
209296341Sdelphij     * (de)allocation of this structure. Hence, running_ref <= struct_ref at
210296341Sdelphij     * all times.
211296341Sdelphij     */
212296341Sdelphij    int funct_ref;
213296341Sdelphij    /* A place to store per-ENGINE data */
214296341Sdelphij    CRYPTO_EX_DATA ex_data;
215296341Sdelphij    /* Used to maintain the linked-list of engines. */
216296341Sdelphij    struct engine_st *prev;
217296341Sdelphij    struct engine_st *next;
218296341Sdelphij};
219109998Smarkm
220109998Smarkm#ifdef  __cplusplus
221109998Smarkm}
222109998Smarkm#endif
223109998Smarkm
224296341Sdelphij#endif                          /* HEADER_ENGINE_INT_H */
225