/src/openssl/crypto/engine/tb_pkmeth.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (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  |  | /* We need to use some  deprecated APIs */  | 
11  |  | #include "internal/deprecated.h"  | 
12  |  |  | 
13  |  | #include "eng_local.h"  | 
14  |  | #include <openssl/evp.h>  | 
15  |  |  | 
16  |  | static ENGINE_TABLE *pkey_meth_table = NULL;  | 
17  |  |  | 
18  |  | void ENGINE_unregister_pkey_meths(ENGINE *e)  | 
19  | 0  | { | 
20  | 0  |     engine_table_unregister(&pkey_meth_table, e);  | 
21  | 0  | }  | 
22  |  |  | 
23  |  | static void engine_unregister_all_pkey_meths(void)  | 
24  | 0  | { | 
25  | 0  |     engine_table_cleanup(&pkey_meth_table);  | 
26  | 0  | }  | 
27  |  |  | 
28  |  | int ENGINE_register_pkey_meths(ENGINE *e)  | 
29  | 0  | { | 
30  | 0  |     if (e->pkey_meths) { | 
31  | 0  |         const int *nids;  | 
32  | 0  |         int num_nids = e->pkey_meths(e, NULL, &nids, 0);  | 
33  | 0  |         if (num_nids > 0)  | 
34  | 0  |             return engine_table_register(&pkey_meth_table,  | 
35  | 0  |                                          engine_unregister_all_pkey_meths, e,  | 
36  | 0  |                                          nids, num_nids, 0);  | 
37  | 0  |     }  | 
38  | 0  |     return 1;  | 
39  | 0  | }  | 
40  |  |  | 
41  |  | void ENGINE_register_all_pkey_meths(void)  | 
42  | 0  | { | 
43  | 0  |     ENGINE *e;  | 
44  |  | 
  | 
45  | 0  |     for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))  | 
46  | 0  |         ENGINE_register_pkey_meths(e);  | 
47  | 0  | }  | 
48  |  |  | 
49  |  | int ENGINE_set_default_pkey_meths(ENGINE *e)  | 
50  | 0  | { | 
51  | 0  |     if (e->pkey_meths) { | 
52  | 0  |         const int *nids;  | 
53  | 0  |         int num_nids = e->pkey_meths(e, NULL, &nids, 0);  | 
54  | 0  |         if (num_nids > 0)  | 
55  | 0  |             return engine_table_register(&pkey_meth_table,  | 
56  | 0  |                                          engine_unregister_all_pkey_meths, e,  | 
57  | 0  |                                          nids, num_nids, 1);  | 
58  | 0  |     }  | 
59  | 0  |     return 1;  | 
60  | 0  | }  | 
61  |  |  | 
62  |  | /*  | 
63  |  |  * Exposed API function to get a functional reference from the implementation  | 
64  |  |  * table (ie. try to get a functional reference from the tabled structural  | 
65  |  |  * references) for a given pkey_meth 'nid'  | 
66  |  |  */  | 
67  |  | ENGINE *ENGINE_get_pkey_meth_engine(int nid)  | 
68  | 0  | { | 
69  | 0  |     return ossl_engine_table_select(&pkey_meth_table, nid,  | 
70  | 0  |                                     OPENSSL_FILE, OPENSSL_LINE);  | 
71  | 0  | }  | 
72  |  |  | 
73  |  | /* Obtains a pkey_meth implementation from an ENGINE functional reference */  | 
74  |  | const EVP_PKEY_METHOD *ENGINE_get_pkey_meth(ENGINE *e, int nid)  | 
75  | 0  | { | 
76  | 0  |     EVP_PKEY_METHOD *ret;  | 
77  | 0  |     ENGINE_PKEY_METHS_PTR fn = ENGINE_get_pkey_meths(e);  | 
78  | 0  |     if (!fn || !fn(e, &ret, NULL, nid)) { | 
79  | 0  |         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);  | 
80  | 0  |         return NULL;  | 
81  | 0  |     }  | 
82  | 0  |     return ret;  | 
83  | 0  | }  | 
84  |  |  | 
85  |  | /* Gets the pkey_meth callback from an ENGINE structure */  | 
86  |  | ENGINE_PKEY_METHS_PTR ENGINE_get_pkey_meths(const ENGINE *e)  | 
87  | 0  | { | 
88  | 0  |     return e->pkey_meths;  | 
89  | 0  | }  | 
90  |  |  | 
91  |  | /* Sets the pkey_meth callback in an ENGINE structure */  | 
92  |  | int ENGINE_set_pkey_meths(ENGINE *e, ENGINE_PKEY_METHS_PTR f)  | 
93  | 0  | { | 
94  | 0  |     e->pkey_meths = f;  | 
95  | 0  |     return 1;  | 
96  | 0  | }  | 
97  |  |  | 
98  |  | /*  | 
99  |  |  * Internal function to free up EVP_PKEY_METHOD structures before an ENGINE  | 
100  |  |  * is destroyed  | 
101  |  |  */  | 
102  |  |  | 
103  |  | void engine_pkey_meths_free(ENGINE *e)  | 
104  | 0  | { | 
105  | 0  |     int i;  | 
106  | 0  |     EVP_PKEY_METHOD *pkm;  | 
107  | 0  |     if (e->pkey_meths) { | 
108  | 0  |         const int *pknids;  | 
109  | 0  |         int npknids;  | 
110  | 0  |         npknids = e->pkey_meths(e, NULL, &pknids, 0);  | 
111  | 0  |         for (i = 0; i < npknids; i++) { | 
112  | 0  |             if (e->pkey_meths(e, &pkm, NULL, pknids[i])) { | 
113  | 0  |                 EVP_PKEY_meth_free(pkm);  | 
114  | 0  |             }  | 
115  | 0  |         }  | 
116  | 0  |     }  | 
117  | 0  | }  |