Fix no-dh and no-dsa
[openssl.git] / crypto / engine / tb_asnmth.c
1 /*
2  * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include "eng_int.h"
11 #include <openssl/evp.h>
12 #include "internal/asn1_int.h"
13
14 /*
15  * If this symbol is defined then ENGINE_get_pkey_asn1_meth_engine(), the
16  * function that is used by EVP to hook in pkey_asn1_meth code and cache
17  * defaults (etc), will display brief debugging summaries to stderr with the
18  * 'nid'.
19  */
20 /* #define ENGINE_PKEY_ASN1_METH_DEBUG */
21
22 static ENGINE_TABLE *pkey_asn1_meth_table = NULL;
23
24 void ENGINE_unregister_pkey_asn1_meths(ENGINE *e)
25 {
26     engine_table_unregister(&pkey_asn1_meth_table, e);
27 }
28
29 static void engine_unregister_all_pkey_asn1_meths(void)
30 {
31     engine_table_cleanup(&pkey_asn1_meth_table);
32 }
33
34 int ENGINE_register_pkey_asn1_meths(ENGINE *e)
35 {
36     if (e->pkey_asn1_meths) {
37         const int *nids;
38         int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0);
39         if (num_nids > 0)
40             return engine_table_register(&pkey_asn1_meth_table,
41                                          engine_unregister_all_pkey_asn1_meths,
42                                          e, nids, num_nids, 0);
43     }
44     return 1;
45 }
46
47 void ENGINE_register_all_pkey_asn1_meths(void)
48 {
49     ENGINE *e;
50
51     for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
52         ENGINE_register_pkey_asn1_meths(e);
53 }
54
55 int ENGINE_set_default_pkey_asn1_meths(ENGINE *e)
56 {
57     if (e->pkey_asn1_meths) {
58         const int *nids;
59         int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0);
60         if (num_nids > 0)
61             return engine_table_register(&pkey_asn1_meth_table,
62                                          engine_unregister_all_pkey_asn1_meths,
63                                          e, nids, num_nids, 1);
64     }
65     return 1;
66 }
67
68 /*
69  * Exposed API function to get a functional reference from the implementation
70  * table (ie. try to get a functional reference from the tabled structural
71  * references) for a given pkey_asn1_meth 'nid'
72  */
73 ENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid)
74 {
75     return engine_table_select(&pkey_asn1_meth_table, nid);
76 }
77
78 /*
79  * Obtains a pkey_asn1_meth implementation from an ENGINE functional
80  * reference
81  */
82 const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid)
83 {
84     EVP_PKEY_ASN1_METHOD *ret;
85     ENGINE_PKEY_ASN1_METHS_PTR fn = ENGINE_get_pkey_asn1_meths(e);
86     if (!fn || !fn(e, &ret, NULL, nid)) {
87         ENGINEerr(ENGINE_F_ENGINE_GET_PKEY_ASN1_METH,
88                   ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);
89         return NULL;
90     }
91     return ret;
92 }
93
94 /* Gets the pkey_asn1_meth callback from an ENGINE structure */
95 ENGINE_PKEY_ASN1_METHS_PTR ENGINE_get_pkey_asn1_meths(const ENGINE *e)
96 {
97     return e->pkey_asn1_meths;
98 }
99
100 /* Sets the pkey_asn1_meth callback in an ENGINE structure */
101 int ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f)
102 {
103     e->pkey_asn1_meths = f;
104     return 1;
105 }
106
107 /*
108  * Internal function to free up EVP_PKEY_ASN1_METHOD structures before an
109  * ENGINE is destroyed
110  */
111
112 void engine_pkey_asn1_meths_free(ENGINE *e)
113 {
114     int i;
115     EVP_PKEY_ASN1_METHOD *pkm;
116     if (e->pkey_asn1_meths) {
117         const int *pknids;
118         int npknids;
119         npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0);
120         for (i = 0; i < npknids; i++) {
121             if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i])) {
122                 EVP_PKEY_asn1_free(pkm);
123             }
124         }
125     }
126 }
127
128 /*
129  * Find a method based on a string. This does a linear search through all
130  * implemented algorithms. This is OK in practice because only a small number
131  * of algorithms are likely to be implemented in an engine and it is not used
132  * for speed critical operations.
133  */
134
135 const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e,
136                                                           const char *str,
137                                                           int len)
138 {
139     int i, nidcount;
140     const int *nids;
141     EVP_PKEY_ASN1_METHOD *ameth;
142     if (!e->pkey_asn1_meths)
143         return NULL;
144     if (len == -1)
145         len = strlen(str);
146     nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0);
147     for (i = 0; i < nidcount; i++) {
148         e->pkey_asn1_meths(e, &ameth, NULL, nids[i]);
149         if (((int)strlen(ameth->pem_str) == len)
150             && strncasecmp(ameth->pem_str, str, len) == 0)
151             return ameth;
152     }
153     return NULL;
154 }
155
156 typedef struct {
157     ENGINE *e;
158     const EVP_PKEY_ASN1_METHOD *ameth;
159     const char *str;
160     int len;
161 } ENGINE_FIND_STR;
162
163 static void look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg)
164 {
165     ENGINE_FIND_STR *lk = arg;
166     int i;
167     if (lk->ameth)
168         return;
169     for (i = 0; i < sk_ENGINE_num(sk); i++) {
170         ENGINE *e = sk_ENGINE_value(sk, i);
171         EVP_PKEY_ASN1_METHOD *ameth;
172         e->pkey_asn1_meths(e, &ameth, NULL, nid);
173         if (((int)strlen(ameth->pem_str) == lk->len)
174                 && strncasecmp(ameth->pem_str, lk->str, lk->len) == 0) {
175             lk->e = e;
176             lk->ameth = ameth;
177             return;
178         }
179     }
180 }
181
182 const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe,
183                                                       const char *str,
184                                                       int len)
185 {
186     ENGINE_FIND_STR fstr;
187     fstr.e = NULL;
188     fstr.ameth = NULL;
189     fstr.str = str;
190     fstr.len = len;
191
192     if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
193         ENGINEerr(ENGINE_F_ENGINE_PKEY_ASN1_FIND_STR, ERR_R_MALLOC_FAILURE);
194         return NULL;
195     }
196
197     CRYPTO_THREAD_write_lock(global_engine_lock);
198     engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr);
199     /* If found obtain a structural reference to engine */
200     if (fstr.e) {
201         fstr.e->struct_ref++;
202         engine_ref_debug(fstr.e, 0, 1);
203     }
204     *pe = fstr.e;
205     CRYPTO_THREAD_unlock(global_engine_lock);
206     return fstr.ameth;
207 }