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
16static ENGINE_TABLE *pkey_meth_table = NULL;
17
18void ENGINE_unregister_pkey_meths(ENGINE *e)
19{
20    engine_table_unregister(&pkey_meth_table, e);
21}
22
23static void engine_unregister_all_pkey_meths(void)
24{
25    engine_table_cleanup(&pkey_meth_table);
26}
27
28int ENGINE_register_pkey_meths(ENGINE *e)
29{
30    if (e->pkey_meths) {
31        const int *nids;
32        int num_nids = e->pkey_meths(e, NULL, &nids, 0);
33        if (num_nids > 0)
34            return engine_table_register(&pkey_meth_table,
35                                         engine_unregister_all_pkey_meths, e,
36                                         nids, num_nids, 0);
37    }
38    return 1;
39}
40
41void ENGINE_register_all_pkey_meths(void)
42{
43    ENGINE *e;
44
45    for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
46        ENGINE_register_pkey_meths(e);
47}
48
49int ENGINE_set_default_pkey_meths(ENGINE *e)
50{
51    if (e->pkey_meths) {
52        const int *nids;
53        int num_nids = e->pkey_meths(e, NULL, &nids, 0);
54        if (num_nids > 0)
55            return engine_table_register(&pkey_meth_table,
56                                         engine_unregister_all_pkey_meths, e,
57                                         nids, num_nids, 1);
58    }
59    return 1;
60}
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 */
67ENGINE *ENGINE_get_pkey_meth_engine(int nid)
68{
69    return ossl_engine_table_select(&pkey_meth_table, nid,
70                                    OPENSSL_FILE, OPENSSL_LINE);
71}
72
73/* Obtains a pkey_meth implementation from an ENGINE functional reference */
74const EVP_PKEY_METHOD *ENGINE_get_pkey_meth(ENGINE *e, int nid)
75{
76    EVP_PKEY_METHOD *ret;
77    ENGINE_PKEY_METHS_PTR fn = ENGINE_get_pkey_meths(e);
78    if (!fn || !fn(e, &ret, NULL, nid)) {
79        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);
80        return NULL;
81    }
82    return ret;
83}
84
85/* Gets the pkey_meth callback from an ENGINE structure */
86ENGINE_PKEY_METHS_PTR ENGINE_get_pkey_meths(const ENGINE *e)
87{
88    return e->pkey_meths;
89}
90
91/* Sets the pkey_meth callback in an ENGINE structure */
92int ENGINE_set_pkey_meths(ENGINE *e, ENGINE_PKEY_METHS_PTR f)
93{
94    e->pkey_meths = f;
95    return 1;
96}
97
98/*
99 * Internal function to free up EVP_PKEY_METHOD structures before an ENGINE
100 * is destroyed
101 */
102
103void engine_pkey_meths_free(ENGINE *e)
104{
105    int i;
106    EVP_PKEY_METHOD *pkm;
107    if (e->pkey_meths) {
108        const int *pknids;
109        int npknids;
110        npknids = e->pkey_meths(e, NULL, &pknids, 0);
111        for (i = 0; i < npknids; i++) {
112            if (e->pkey_meths(e, &pkm, NULL, pknids[i])) {
113                EVP_PKEY_meth_free(pkm);
114            }
115        }
116    }
117}
118