1/*
2 * Copyright 2006-2018 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#include "eng_local.h"
11#include <openssl/evp.h>
12
13static ENGINE_TABLE *pkey_meth_table = NULL;
14
15void ENGINE_unregister_pkey_meths(ENGINE *e)
16{
17 engine_table_unregister(&pkey_meth_table, e);
18}
19
20static void engine_unregister_all_pkey_meths(void)
21{
22 engine_table_cleanup(&pkey_meth_table);
23}
24
25int ENGINE_register_pkey_meths(ENGINE *e)
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}
37
38void ENGINE_register_all_pkey_meths(void)
39{
40 ENGINE *e;
41
42 for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
43 ENGINE_register_pkey_meths(e);
44}
45
46int ENGINE_set_default_pkey_meths(ENGINE *e)
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}
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 */
64ENGINE *ENGINE_get_pkey_meth_engine(int nid)
65{
66 return engine_table_select(&pkey_meth_table, nid);
67}
68
69/* Obtains a pkey_meth implementation from an ENGINE functional reference */
70const EVP_PKEY_METHOD *ENGINE_get_pkey_meth(ENGINE *e, int nid)
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}
81
82/* Gets the pkey_meth callback from an ENGINE structure */
83ENGINE_PKEY_METHS_PTR ENGINE_get_pkey_meths(const ENGINE *e)
84{
85 return e->pkey_meths;
86}
87
88/* Sets the pkey_meth callback in an ENGINE structure */
89int ENGINE_set_pkey_meths(ENGINE *e, ENGINE_PKEY_METHS_PTR f)
90{
91 e->pkey_meths = f;
92 return 1;
93}
94
95/*
96 * Internal function to free up EVP_PKEY_METHOD structures before an ENGINE
97 * is destroyed
98 */
99
100void engine_pkey_meths_free(ENGINE *e)
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}
115