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