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 "asn1_locl.h"
57238384Sjkim#include <openssl/evp.h>
58238384Sjkim
59296341Sdelphij/*
60296341Sdelphij * If this symbol is defined then ENGINE_get_pkey_asn1_meth_engine(), the
61238384Sjkim * function that is used by EVP to hook in pkey_asn1_meth code and cache
62238384Sjkim * defaults (etc), will display brief debugging summaries to stderr with the
63296341Sdelphij * 'nid'.
64296341Sdelphij */
65238384Sjkim/* #define ENGINE_PKEY_ASN1_METH_DEBUG */
66238384Sjkim
67238384Sjkimstatic ENGINE_TABLE *pkey_asn1_meth_table = NULL;
68238384Sjkim
69238384Sjkimvoid ENGINE_unregister_pkey_asn1_meths(ENGINE *e)
70296341Sdelphij{
71296341Sdelphij    engine_table_unregister(&pkey_asn1_meth_table, e);
72296341Sdelphij}
73238384Sjkim
74238384Sjkimstatic void engine_unregister_all_pkey_asn1_meths(void)
75296341Sdelphij{
76296341Sdelphij    engine_table_cleanup(&pkey_asn1_meth_table);
77296341Sdelphij}
78238384Sjkim
79238384Sjkimint ENGINE_register_pkey_asn1_meths(ENGINE *e)
80296341Sdelphij{
81296341Sdelphij    if (e->pkey_asn1_meths) {
82296341Sdelphij        const int *nids;
83296341Sdelphij        int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0);
84296341Sdelphij        if (num_nids > 0)
85296341Sdelphij            return engine_table_register(&pkey_asn1_meth_table,
86296341Sdelphij                                         engine_unregister_all_pkey_asn1_meths,
87296341Sdelphij                                         e, nids, num_nids, 0);
88296341Sdelphij    }
89296341Sdelphij    return 1;
90296341Sdelphij}
91238384Sjkim
92238384Sjkimvoid ENGINE_register_all_pkey_asn1_meths(void)
93296341Sdelphij{
94296341Sdelphij    ENGINE *e;
95238384Sjkim
96296341Sdelphij    for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
97296341Sdelphij        ENGINE_register_pkey_asn1_meths(e);
98296341Sdelphij}
99238384Sjkim
100238384Sjkimint ENGINE_set_default_pkey_asn1_meths(ENGINE *e)
101296341Sdelphij{
102296341Sdelphij    if (e->pkey_asn1_meths) {
103296341Sdelphij        const int *nids;
104296341Sdelphij        int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0);
105296341Sdelphij        if (num_nids > 0)
106296341Sdelphij            return engine_table_register(&pkey_asn1_meth_table,
107296341Sdelphij                                         engine_unregister_all_pkey_asn1_meths,
108296341Sdelphij                                         e, nids, num_nids, 1);
109296341Sdelphij    }
110296341Sdelphij    return 1;
111296341Sdelphij}
112238384Sjkim
113296341Sdelphij/*
114296341Sdelphij * Exposed API function to get a functional reference from the implementation
115238384Sjkim * table (ie. try to get a functional reference from the tabled structural
116296341Sdelphij * references) for a given pkey_asn1_meth 'nid'
117296341Sdelphij */
118238384SjkimENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid)
119296341Sdelphij{
120296341Sdelphij    return engine_table_select(&pkey_asn1_meth_table, nid);
121296341Sdelphij}
122238384Sjkim
123296341Sdelphij/*
124296341Sdelphij * Obtains a pkey_asn1_meth implementation from an ENGINE functional
125296341Sdelphij * reference
126296341Sdelphij */
127238384Sjkimconst EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid)
128296341Sdelphij{
129296341Sdelphij    EVP_PKEY_ASN1_METHOD *ret;
130296341Sdelphij    ENGINE_PKEY_ASN1_METHS_PTR fn = ENGINE_get_pkey_asn1_meths(e);
131296341Sdelphij    if (!fn || !fn(e, &ret, NULL, nid)) {
132296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_GET_PKEY_ASN1_METH,
133296341Sdelphij                  ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);
134296341Sdelphij        return NULL;
135296341Sdelphij    }
136296341Sdelphij    return ret;
137296341Sdelphij}
138238384Sjkim
139238384Sjkim/* Gets the pkey_asn1_meth callback from an ENGINE structure */
140238384SjkimENGINE_PKEY_ASN1_METHS_PTR ENGINE_get_pkey_asn1_meths(const ENGINE *e)
141296341Sdelphij{
142296341Sdelphij    return e->pkey_asn1_meths;
143296341Sdelphij}
144238384Sjkim
145238384Sjkim/* Sets the pkey_asn1_meth callback in an ENGINE structure */
146238384Sjkimint ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f)
147296341Sdelphij{
148296341Sdelphij    e->pkey_asn1_meths = f;
149296341Sdelphij    return 1;
150296341Sdelphij}
151238384Sjkim
152296341Sdelphij/*
153296341Sdelphij * Internal function to free up EVP_PKEY_ASN1_METHOD structures before an
154238384Sjkim * ENGINE is destroyed
155238384Sjkim */
156238384Sjkim
157238384Sjkimvoid engine_pkey_asn1_meths_free(ENGINE *e)
158296341Sdelphij{
159296341Sdelphij    int i;
160296341Sdelphij    EVP_PKEY_ASN1_METHOD *pkm;
161296341Sdelphij    if (e->pkey_asn1_meths) {
162296341Sdelphij        const int *pknids;
163296341Sdelphij        int npknids;
164296341Sdelphij        npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0);
165296341Sdelphij        for (i = 0; i < npknids; i++) {
166296341Sdelphij            if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i])) {
167296341Sdelphij                EVP_PKEY_asn1_free(pkm);
168296341Sdelphij            }
169296341Sdelphij        }
170296341Sdelphij    }
171296341Sdelphij}
172238384Sjkim
173296341Sdelphij/*
174296341Sdelphij * Find a method based on a string. This does a linear search through all
175296341Sdelphij * implemented algorithms. This is OK in practice because only a small number
176296341Sdelphij * of algorithms are likely to be implemented in an engine and it is not used
177296341Sdelphij * for speed critical operations.
178238384Sjkim */
179238384Sjkim
180238384Sjkimconst EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e,
181296341Sdelphij                                                          const char *str,
182296341Sdelphij                                                          int len)
183296341Sdelphij{
184296341Sdelphij    int i, nidcount;
185296341Sdelphij    const int *nids;
186296341Sdelphij    EVP_PKEY_ASN1_METHOD *ameth;
187296341Sdelphij    if (!e->pkey_asn1_meths)
188296341Sdelphij        return NULL;
189296341Sdelphij    if (len == -1)
190296341Sdelphij        len = strlen(str);
191296341Sdelphij    nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0);
192296341Sdelphij    for (i = 0; i < nidcount; i++) {
193296341Sdelphij        e->pkey_asn1_meths(e, &ameth, NULL, nids[i]);
194296341Sdelphij        if (((int)strlen(ameth->pem_str) == len) &&
195296341Sdelphij            !strncasecmp(ameth->pem_str, str, len))
196296341Sdelphij            return ameth;
197296341Sdelphij    }
198296341Sdelphij    return NULL;
199296341Sdelphij}
200238384Sjkim
201296341Sdelphijtypedef struct {
202296341Sdelphij    ENGINE *e;
203296341Sdelphij    const EVP_PKEY_ASN1_METHOD *ameth;
204296341Sdelphij    const char *str;
205296341Sdelphij    int len;
206296341Sdelphij} ENGINE_FIND_STR;
207238384Sjkim
208238384Sjkimstatic void look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg)
209296341Sdelphij{
210296341Sdelphij    ENGINE_FIND_STR *lk = arg;
211296341Sdelphij    int i;
212296341Sdelphij    if (lk->ameth)
213296341Sdelphij        return;
214296341Sdelphij    for (i = 0; i < sk_ENGINE_num(sk); i++) {
215296341Sdelphij        ENGINE *e = sk_ENGINE_value(sk, i);
216296341Sdelphij        EVP_PKEY_ASN1_METHOD *ameth;
217296341Sdelphij        e->pkey_asn1_meths(e, &ameth, NULL, nid);
218296341Sdelphij        if (((int)strlen(ameth->pem_str) == lk->len) &&
219296341Sdelphij            !strncasecmp(ameth->pem_str, lk->str, lk->len)) {
220296341Sdelphij            lk->e = e;
221296341Sdelphij            lk->ameth = ameth;
222296341Sdelphij            return;
223296341Sdelphij        }
224296341Sdelphij    }
225296341Sdelphij}
226238384Sjkim
227238384Sjkimconst EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe,
228296341Sdelphij                                                      const char *str,
229296341Sdelphij                                                      int len)
230296341Sdelphij{
231296341Sdelphij    ENGINE_FIND_STR fstr;
232296341Sdelphij    fstr.e = NULL;
233296341Sdelphij    fstr.ameth = NULL;
234296341Sdelphij    fstr.str = str;
235296341Sdelphij    fstr.len = len;
236296341Sdelphij    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
237296341Sdelphij    engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr);
238296341Sdelphij    /* If found obtain a structural reference to engine */
239296341Sdelphij    if (fstr.e) {
240296341Sdelphij        fstr.e->struct_ref++;
241296341Sdelphij        engine_ref_debug(fstr.e, 0, 1)
242296341Sdelphij    }
243296341Sdelphij    *pe = fstr.e;
244296341Sdelphij    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
245296341Sdelphij    return fstr.ameth;
246296341Sdelphij}
247