1238384Sjkim/* ====================================================================
2238384Sjkim * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
3238384Sjkim *
4238384Sjkim * Redistribution and use in source and binary forms, with or without
5238384Sjkim * modification, are permitted provided that the following conditions
6238384Sjkim * are met:
7238384Sjkim *
8238384Sjkim * 1. Redistributions of source code must retain the above copyright
9296341Sdelphij *    notice, this list of conditions and the following disclaimer.
10238384Sjkim *
11238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
12238384Sjkim *    notice, this list of conditions and the following disclaimer in
13238384Sjkim *    the documentation and/or other materials provided with the
14238384Sjkim *    distribution.
15238384Sjkim *
16238384Sjkim * 3. All advertising materials mentioning features or use of this
17238384Sjkim *    software must display the following acknowledgment:
18238384Sjkim *    "This product includes software developed by the OpenSSL Project
19238384Sjkim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20238384Sjkim *
21238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22238384Sjkim *    endorse or promote products derived from this software without
23238384Sjkim *    prior written permission. For written permission, please contact
24238384Sjkim *    licensing@OpenSSL.org.
25238384Sjkim *
26238384Sjkim * 5. Products derived from this software may not be called "OpenSSL"
27238384Sjkim *    nor may "OpenSSL" appear in their names without prior written
28238384Sjkim *    permission of the OpenSSL Project.
29238384Sjkim *
30238384Sjkim * 6. Redistributions of any form whatsoever must retain the following
31238384Sjkim *    acknowledgment:
32238384Sjkim *    "This product includes software developed by the OpenSSL Project
33238384Sjkim *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34238384Sjkim *
35238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38238384Sjkim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE.
47238384Sjkim * ====================================================================
48238384Sjkim *
49238384Sjkim * This product includes cryptographic software written by Eric Young
50238384Sjkim * (eay@cryptsoft.com).  This product includes software written by Tim
51238384Sjkim * Hudson (tjh@cryptsoft.com).
52238384Sjkim *
53238384Sjkim */
54238384Sjkim
55238384Sjkim#include "eng_int.h"
56238384Sjkim#include <openssl/evp.h>
57238384Sjkim
58296341Sdelphij/*
59296341Sdelphij * If this symbol is defined then ENGINE_get_pkey_meth_engine(), the function
60296341Sdelphij * that is used by EVP to hook in pkey_meth code and cache defaults (etc),
61296341Sdelphij * will display brief debugging summaries to stderr with the 'nid'.
62296341Sdelphij */
63238384Sjkim/* #define ENGINE_PKEY_METH_DEBUG */
64238384Sjkim
65238384Sjkimstatic ENGINE_TABLE *pkey_meth_table = NULL;
66238384Sjkim
67238384Sjkimvoid ENGINE_unregister_pkey_meths(ENGINE *e)
68296341Sdelphij{
69296341Sdelphij    engine_table_unregister(&pkey_meth_table, e);
70296341Sdelphij}
71238384Sjkim
72238384Sjkimstatic void engine_unregister_all_pkey_meths(void)
73296341Sdelphij{
74296341Sdelphij    engine_table_cleanup(&pkey_meth_table);
75296341Sdelphij}
76238384Sjkim
77238384Sjkimint ENGINE_register_pkey_meths(ENGINE *e)
78296341Sdelphij{
79296341Sdelphij    if (e->pkey_meths) {
80296341Sdelphij        const int *nids;
81296341Sdelphij        int num_nids = e->pkey_meths(e, NULL, &nids, 0);
82296341Sdelphij        if (num_nids > 0)
83296341Sdelphij            return engine_table_register(&pkey_meth_table,
84296341Sdelphij                                         engine_unregister_all_pkey_meths, e,
85296341Sdelphij                                         nids, num_nids, 0);
86296341Sdelphij    }
87296341Sdelphij    return 1;
88296341Sdelphij}
89238384Sjkim
90238384Sjkimvoid ENGINE_register_all_pkey_meths()
91296341Sdelphij{
92296341Sdelphij    ENGINE *e;
93238384Sjkim
94296341Sdelphij    for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
95296341Sdelphij        ENGINE_register_pkey_meths(e);
96296341Sdelphij}
97238384Sjkim
98238384Sjkimint ENGINE_set_default_pkey_meths(ENGINE *e)
99296341Sdelphij{
100296341Sdelphij    if (e->pkey_meths) {
101296341Sdelphij        const int *nids;
102296341Sdelphij        int num_nids = e->pkey_meths(e, NULL, &nids, 0);
103296341Sdelphij        if (num_nids > 0)
104296341Sdelphij            return engine_table_register(&pkey_meth_table,
105296341Sdelphij                                         engine_unregister_all_pkey_meths, e,
106296341Sdelphij                                         nids, num_nids, 1);
107296341Sdelphij    }
108296341Sdelphij    return 1;
109296341Sdelphij}
110238384Sjkim
111296341Sdelphij/*
112296341Sdelphij * Exposed API function to get a functional reference from the implementation
113238384Sjkim * table (ie. try to get a functional reference from the tabled structural
114296341Sdelphij * references) for a given pkey_meth 'nid'
115296341Sdelphij */
116238384SjkimENGINE *ENGINE_get_pkey_meth_engine(int nid)
117296341Sdelphij{
118296341Sdelphij    return engine_table_select(&pkey_meth_table, nid);
119296341Sdelphij}
120238384Sjkim
121238384Sjkim/* Obtains a pkey_meth implementation from an ENGINE functional reference */
122238384Sjkimconst EVP_PKEY_METHOD *ENGINE_get_pkey_meth(ENGINE *e, int nid)
123296341Sdelphij{
124296341Sdelphij    EVP_PKEY_METHOD *ret;
125296341Sdelphij    ENGINE_PKEY_METHS_PTR fn = ENGINE_get_pkey_meths(e);
126296341Sdelphij    if (!fn || !fn(e, &ret, NULL, nid)) {
127296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_GET_PKEY_METH,
128296341Sdelphij                  ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);
129296341Sdelphij        return NULL;
130296341Sdelphij    }
131296341Sdelphij    return ret;
132296341Sdelphij}
133238384Sjkim
134238384Sjkim/* Gets the pkey_meth callback from an ENGINE structure */
135238384SjkimENGINE_PKEY_METHS_PTR ENGINE_get_pkey_meths(const ENGINE *e)
136296341Sdelphij{
137296341Sdelphij    return e->pkey_meths;
138296341Sdelphij}
139238384Sjkim
140238384Sjkim/* Sets the pkey_meth callback in an ENGINE structure */
141238384Sjkimint ENGINE_set_pkey_meths(ENGINE *e, ENGINE_PKEY_METHS_PTR f)
142296341Sdelphij{
143296341Sdelphij    e->pkey_meths = f;
144296341Sdelphij    return 1;
145296341Sdelphij}
146238384Sjkim
147296341Sdelphij/*
148296341Sdelphij * Internal function to free up EVP_PKEY_METHOD structures before an ENGINE
149296341Sdelphij * is destroyed
150238384Sjkim */
151238384Sjkim
152238384Sjkimvoid engine_pkey_meths_free(ENGINE *e)
153296341Sdelphij{
154296341Sdelphij    int i;
155296341Sdelphij    EVP_PKEY_METHOD *pkm;
156296341Sdelphij    if (e->pkey_meths) {
157296341Sdelphij        const int *pknids;
158296341Sdelphij        int npknids;
159296341Sdelphij        npknids = e->pkey_meths(e, NULL, &pknids, 0);
160296341Sdelphij        for (i = 0; i < npknids; i++) {
161296341Sdelphij            if (e->pkey_meths(e, &pkm, NULL, pknids[i])) {
162296341Sdelphij                EVP_PKEY_meth_free(pkm);
163296341Sdelphij            }
164296341Sdelphij        }
165296341Sdelphij    }
166296341Sdelphij}
167