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