1c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root/* ====================================================================
2c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
3c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *
4c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * Redistribution and use in source and binary forms, with or without
5c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * modification, are permitted provided that the following conditions
6c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * are met:
7c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *
8c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * 1. Redistributions of source code must retain the above copyright
9c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    notice, this list of conditions and the following disclaimer.
10c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *
11c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * 2. Redistributions in binary form must reproduce the above copyright
12c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    notice, this list of conditions and the following disclaimer in
13c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    the documentation and/or other materials provided with the
14c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    distribution.
15c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *
16c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * 3. All advertising materials mentioning features or use of this
17c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    software must display the following acknowledgment:
18c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    "This product includes software developed by the OpenSSL Project
19c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *
21c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    endorse or promote products derived from this software without
23c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    prior written permission. For written permission, please contact
24c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    licensing@OpenSSL.org.
25c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *
26c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * 5. Products derived from this software may not be called "OpenSSL"
27c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    nor may "OpenSSL" appear in their names without prior written
28c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    permission of the OpenSSL Project.
29c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *
30c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * 6. Redistributions of any form whatsoever must retain the following
31c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    acknowledgment:
32c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    "This product includes software developed by the OpenSSL Project
33c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *
35c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * OF THE POSSIBILITY OF SUCH DAMAGE.
47c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * ====================================================================
48c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *
49c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * This product includes cryptographic software written by Eric Young
50c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * (eay@cryptsoft.com).  This product includes software written by Tim
51c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * Hudson (tjh@cryptsoft.com).
52c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root *
53c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root */
54c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
55c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root#include "eng_int.h"
56c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root#include <openssl/evp.h>
57c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
58c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root/* If this symbol is defined then ENGINE_get_pkey_meth_engine(), the function
59c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * that is used by EVP to hook in pkey_meth code and cache defaults (etc), will
60c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * display brief debugging summaries to stderr with the 'nid'. */
61c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root/* #define ENGINE_PKEY_METH_DEBUG */
62c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
63c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Rootstatic ENGINE_TABLE *pkey_meth_table = NULL;
64c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
65c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Rootvoid ENGINE_unregister_pkey_meths(ENGINE *e)
66c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	{
67c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	engine_table_unregister(&pkey_meth_table, e);
68c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	}
69c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
70c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Rootstatic void engine_unregister_all_pkey_meths(void)
71c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	{
72c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	engine_table_cleanup(&pkey_meth_table);
73c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	}
74c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
75c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Rootint ENGINE_register_pkey_meths(ENGINE *e)
76c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	{
77c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	if(e->pkey_meths)
78c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		{
79c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		const int *nids;
80c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		int num_nids = e->pkey_meths(e, NULL, &nids, 0);
81c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		if(num_nids > 0)
82c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root			return engine_table_register(&pkey_meth_table,
83c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root				engine_unregister_all_pkey_meths, e, nids,
84c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root					num_nids, 0);
85c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		}
86c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	return 1;
87c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	}
88c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
89c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Rootvoid ENGINE_register_all_pkey_meths()
90c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	{
91c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	ENGINE *e;
92c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
93c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	for(e=ENGINE_get_first() ; e ; e=ENGINE_get_next(e))
94c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		ENGINE_register_pkey_meths(e);
95c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	}
96c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
97c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Rootint ENGINE_set_default_pkey_meths(ENGINE *e)
98c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	{
99c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	if(e->pkey_meths)
100c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		{
101c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		const int *nids;
102c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		int num_nids = e->pkey_meths(e, NULL, &nids, 0);
103c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		if(num_nids > 0)
104c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root			return engine_table_register(&pkey_meth_table,
105c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root				engine_unregister_all_pkey_meths, e, nids,
106c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root					num_nids, 1);
107c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		}
108c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	return 1;
109c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	}
110c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
111c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root/* Exposed API function to get a functional reference from the implementation
112c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * table (ie. try to get a functional reference from the tabled structural
113c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * references) for a given pkey_meth 'nid' */
114c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny RootENGINE *ENGINE_get_pkey_meth_engine(int nid)
115c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	{
116c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	return engine_table_select(&pkey_meth_table, nid);
117c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	}
118c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
119c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root/* Obtains a pkey_meth implementation from an ENGINE functional reference */
120c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Rootconst EVP_PKEY_METHOD *ENGINE_get_pkey_meth(ENGINE *e, int nid)
121c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	{
122c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	EVP_PKEY_METHOD *ret;
123c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	ENGINE_PKEY_METHS_PTR fn = ENGINE_get_pkey_meths(e);
124c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	if(!fn || !fn(e, &ret, NULL, nid))
125c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		{
126c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		ENGINEerr(ENGINE_F_ENGINE_GET_PKEY_METH,
127c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root				ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);
128c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		return NULL;
129c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		}
130c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	return ret;
131c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	}
132c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
133c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root/* Gets the pkey_meth callback from an ENGINE structure */
134c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny RootENGINE_PKEY_METHS_PTR ENGINE_get_pkey_meths(const ENGINE *e)
135c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	{
136c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	return e->pkey_meths;
137c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	}
138c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
139c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root/* Sets the pkey_meth callback in an ENGINE structure */
140c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Rootint ENGINE_set_pkey_meths(ENGINE *e, ENGINE_PKEY_METHS_PTR f)
141c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	{
142c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	e->pkey_meths = f;
143c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	return 1;
144c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	}
145c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
146c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root/* Internal function to free up EVP_PKEY_METHOD structures before an
147c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root * ENGINE is destroyed
148c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root */
149c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root
150c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Rootvoid engine_pkey_meths_free(ENGINE *e)
151c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	{
152c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	int i;
153c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	EVP_PKEY_METHOD *pkm;
154c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	if (e->pkey_meths)
155c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		{
156c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		const int *pknids;
157c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		int npknids;
158c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		npknids = e->pkey_meths(e, NULL, &pknids, 0);
159c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		for (i = 0; i < npknids; i++)
160c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root			{
161c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root			if (e->pkey_meths(e, &pkm, NULL, pknids[i]))
162c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root				{
163c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root				EVP_PKEY_meth_free(pkm);
164c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root				}
165c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root			}
166c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root		}
167c642ac3a56765a59cd8c52e608f11c2b6cf61cc5Kenny Root	}
168