2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * Here is a set of wrappers for the ENGINE API, which are no-ops when the
12 * ENGINE API is disabled / removed.
13 * We need to suppress deprecation warnings to make this work.
15 #define OPENSSL_SUPPRESS_DEPRECATED
17 #include <string.h> /* strcmp */
19 #include <openssl/types.h> /* Ensure we have the ENGINE type, regardless */
20 #include <openssl/err.h>
21 #ifndef OPENSSL_NO_ENGINE
22 # include <openssl/engine.h>
26 #ifndef OPENSSL_NO_ENGINE
27 /* Try to load an engine in a shareable library */
28 static ENGINE *try_load_engine(const char *engine)
32 if ((e = ENGINE_by_id("dynamic")) != NULL) {
33 if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0)
34 || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) {
43 ENGINE *setup_engine_methods(const char *id, unsigned int methods, int debug)
47 #ifndef OPENSSL_NO_ENGINE
49 if (strcmp(id, "auto") == 0) {
50 BIO_printf(bio_err, "Enabling auto ENGINE support\n");
51 ENGINE_register_all_complete();
54 if ((e = ENGINE_by_id(id)) == NULL
55 && (e = try_load_engine(id)) == NULL) {
56 BIO_printf(bio_err, "Invalid engine \"%s\"\n", id);
57 ERR_print_errors(bio_err);
61 (void)ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM, 0, bio_err, 0);
62 if (!ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0,
63 (void *)get_ui_method(), 0, 1)
64 || !ENGINE_set_default(e, methods)) {
65 BIO_printf(bio_err, "Cannot use engine \"%s\"\n", ENGINE_get_id(e));
66 ERR_print_errors(bio_err);
71 BIO_printf(bio_err, "Engine \"%s\" set.\n", ENGINE_get_id(e));
77 void release_engine(ENGINE *e)
79 #ifndef OPENSSL_NO_ENGINE
80 /* Free our "structural" reference. */
85 int init_engine(ENGINE *e)
89 #ifndef OPENSSL_NO_ENGINE
95 int finish_engine(ENGINE *e)
99 #ifndef OPENSSL_NO_ENGINE
100 rv = ENGINE_finish(e);
105 char *make_engine_uri(ENGINE *e, const char *key_id, const char *desc)
107 char *new_uri = NULL;
109 #ifndef OPENSSL_NO_ENGINE
111 BIO_printf(bio_err, "No engine specified for loading %s\n", desc);
112 } else if (key_id == NULL) {
113 BIO_printf(bio_err, "No engine key id specified for loading %s\n", desc);
115 const char *engineid = ENGINE_get_id(e);
117 sizeof(ENGINE_SCHEME_COLON) - 1
124 new_uri = OPENSSL_malloc(uri_sz);
125 if (new_uri != NULL) {
126 OPENSSL_strlcpy(new_uri, ENGINE_SCHEME_COLON, uri_sz);
127 OPENSSL_strlcat(new_uri, engineid, uri_sz);
128 OPENSSL_strlcat(new_uri, ":", uri_sz);
129 OPENSSL_strlcat(new_uri, key_id, uri_sz);
133 BIO_printf(bio_err, "Engines not supported for loading %s\n", desc);
138 int get_legacy_pkey_id(OSSL_LIB_CTX *libctx, const char *algname, ENGINE *e)
140 const EVP_PKEY_ASN1_METHOD *ameth;
141 ENGINE *tmpeng = NULL;
142 int pkey_id = NID_undef;
145 ameth = EVP_PKEY_asn1_find_str(&tmpeng, algname, -1);
147 #if !defined(OPENSSL_NO_ENGINE)
148 ENGINE_finish(tmpeng);
150 if (ameth == NULL && e != NULL)
151 ameth = ENGINE_get_pkey_asn1_meth_str(e, algname, -1);
154 /* We're only interested if it comes from an ENGINE */
162 EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
167 const EVP_MD *get_digest_from_engine(const char *name)
169 #ifndef OPENSSL_NO_ENGINE
172 eng = ENGINE_get_digest_engine(OBJ_sn2nid(name));
175 return EVP_get_digestbyname(name);
181 const EVP_CIPHER *get_cipher_from_engine(const char *name)
183 #ifndef OPENSSL_NO_ENGINE
186 eng = ENGINE_get_cipher_engine(OBJ_sn2nid(name));
189 return EVP_get_cipherbyname(name);