1109998Smarkm/* ====================================================================
2109998Smarkm * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
3109998Smarkm *
4109998Smarkm * Redistribution and use in source and binary forms, with or without
5109998Smarkm * modification, are permitted provided that the following conditions
6109998Smarkm * are met:
7109998Smarkm *
8109998Smarkm * 1. Redistributions of source code must retain the above copyright
9296341Sdelphij *    notice, this list of conditions and the following disclaimer.
10109998Smarkm *
11109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
12109998Smarkm *    notice, this list of conditions and the following disclaimer in
13109998Smarkm *    the documentation and/or other materials provided with the
14109998Smarkm *    distribution.
15109998Smarkm *
16109998Smarkm * 3. All advertising materials mentioning features or use of this
17109998Smarkm *    software must display the following acknowledgment:
18109998Smarkm *    "This product includes software developed by the OpenSSL Project
19109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20109998Smarkm *
21109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22109998Smarkm *    endorse or promote products derived from this software without
23109998Smarkm *    prior written permission. For written permission, please contact
24109998Smarkm *    licensing@OpenSSL.org.
25109998Smarkm *
26109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
27109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
28109998Smarkm *    permission of the OpenSSL Project.
29109998Smarkm *
30109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
31109998Smarkm *    acknowledgment:
32109998Smarkm *    "This product includes software developed by the OpenSSL Project
33109998Smarkm *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34109998Smarkm *
35109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
47109998Smarkm * ====================================================================
48109998Smarkm *
49109998Smarkm * This product includes cryptographic software written by Eric Young
50109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
51109998Smarkm * Hudson (tjh@cryptsoft.com).
52109998Smarkm *
53109998Smarkm */
54109998Smarkm
55109998Smarkm#include "eng_int.h"
56109998Smarkm
57296341Sdelphij/*
58296341Sdelphij * If this symbol is defined then ENGINE_get_cipher_engine(), the function
59296341Sdelphij * that is used by EVP to hook in cipher code and cache defaults (etc), will
60296341Sdelphij * display brief debugging summaries to stderr with the 'nid'.
61296341Sdelphij */
62109998Smarkm/* #define ENGINE_CIPHER_DEBUG */
63109998Smarkm
64109998Smarkmstatic ENGINE_TABLE *cipher_table = NULL;
65109998Smarkm
66109998Smarkmvoid ENGINE_unregister_ciphers(ENGINE *e)
67296341Sdelphij{
68296341Sdelphij    engine_table_unregister(&cipher_table, e);
69296341Sdelphij}
70109998Smarkm
71109998Smarkmstatic void engine_unregister_all_ciphers(void)
72296341Sdelphij{
73296341Sdelphij    engine_table_cleanup(&cipher_table);
74296341Sdelphij}
75109998Smarkm
76109998Smarkmint ENGINE_register_ciphers(ENGINE *e)
77296341Sdelphij{
78296341Sdelphij    if (e->ciphers) {
79296341Sdelphij        const int *nids;
80296341Sdelphij        int num_nids = e->ciphers(e, NULL, &nids, 0);
81296341Sdelphij        if (num_nids > 0)
82296341Sdelphij            return engine_table_register(&cipher_table,
83296341Sdelphij                                         engine_unregister_all_ciphers, e,
84296341Sdelphij                                         nids, num_nids, 0);
85296341Sdelphij    }
86296341Sdelphij    return 1;
87296341Sdelphij}
88109998Smarkm
89109998Smarkmvoid ENGINE_register_all_ciphers()
90296341Sdelphij{
91296341Sdelphij    ENGINE *e;
92109998Smarkm
93296341Sdelphij    for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
94296341Sdelphij        ENGINE_register_ciphers(e);
95296341Sdelphij}
96109998Smarkm
97109998Smarkmint ENGINE_set_default_ciphers(ENGINE *e)
98296341Sdelphij{
99296341Sdelphij    if (e->ciphers) {
100296341Sdelphij        const int *nids;
101296341Sdelphij        int num_nids = e->ciphers(e, NULL, &nids, 0);
102296341Sdelphij        if (num_nids > 0)
103296341Sdelphij            return engine_table_register(&cipher_table,
104296341Sdelphij                                         engine_unregister_all_ciphers, e,
105296341Sdelphij                                         nids, num_nids, 1);
106296341Sdelphij    }
107296341Sdelphij    return 1;
108296341Sdelphij}
109109998Smarkm
110296341Sdelphij/*
111296341Sdelphij * Exposed API function to get a functional reference from the implementation
112109998Smarkm * table (ie. try to get a functional reference from the tabled structural
113296341Sdelphij * references) for a given cipher 'nid'
114296341Sdelphij */
115109998SmarkmENGINE *ENGINE_get_cipher_engine(int nid)
116296341Sdelphij{
117296341Sdelphij    return engine_table_select(&cipher_table, nid);
118296341Sdelphij}
119109998Smarkm
120109998Smarkm/* Obtains a cipher implementation from an ENGINE functional reference */
121109998Smarkmconst EVP_CIPHER *ENGINE_get_cipher(ENGINE *e, int nid)
122296341Sdelphij{
123296341Sdelphij    const EVP_CIPHER *ret;
124296341Sdelphij    ENGINE_CIPHERS_PTR fn = ENGINE_get_ciphers(e);
125296341Sdelphij    if (!fn || !fn(e, &ret, NULL, nid)) {
126296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_GET_CIPHER, ENGINE_R_UNIMPLEMENTED_CIPHER);
127296341Sdelphij        return NULL;
128296341Sdelphij    }
129296341Sdelphij    return ret;
130296341Sdelphij}
131109998Smarkm
132109998Smarkm/* Gets the cipher callback from an ENGINE structure */
133109998SmarkmENGINE_CIPHERS_PTR ENGINE_get_ciphers(const ENGINE *e)
134296341Sdelphij{
135296341Sdelphij    return e->ciphers;
136296341Sdelphij}
137109998Smarkm
138109998Smarkm/* Sets the cipher callback in an ENGINE structure */
139109998Smarkmint ENGINE_set_ciphers(ENGINE *e, ENGINE_CIPHERS_PTR f)
140296341Sdelphij{
141296341Sdelphij    e->ciphers = f;
142296341Sdelphij    return 1;
143296341Sdelphij}
144