Skip to content

Commit 7081d3b

Browse files
authored
Merge pull request #781 from bukka/openssl-disable-engines
Add configure option and code to disable OpenSSL engines
2 parents 0053e2b + a393bf8 commit 7081d3b

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

m4/acx_crypto_backend.m4

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ AC_DEFUN([ACX_CRYPTO_BACKEND],[
4949
AC_MSG_RESULT(no)
5050
fi
5151
52+
# Option to disable usage engines
53+
54+
AC_ARG_ENABLE(openssl-engines,
55+
AS_HELP_STRING([--disable-openssl-engines],
56+
[Disable OpenSSL engines usage]
57+
),
58+
[enable_openssl_engines="${enableval}"],
59+
[enable_openssl_engines="yes"]
60+
)
61+
5262
# Then check what crypto library we want to use
5363
5464
AC_ARG_WITH(crypto-backend,
@@ -105,6 +115,22 @@ AC_DEFUN([ACX_CRYPTO_BACKEND],[
105115
ACX_OPENSSL_EVPAESWRAP
106116
fi
107117
118+
AC_MSG_CHECKING(for OpenSSL engines support)
119+
if test "x${enable_openssl_engines}" = "xyes"; then
120+
ACX_OPENSSL_ENGINES
121+
if test "x${have_lib_openssl_engines_support}" = "xyes"; then
122+
AC_MSG_RESULT([yes])
123+
else
124+
AC_MSG_RESULT([no])
125+
AC_DEFINE_UNQUOTED([WITHOUT_OPENSSL_ENGINES], [1],
126+
[Compile without OpenSSL engines support as it is unavailable])
127+
fi
128+
else
129+
AC_MSG_RESULT([disabled])
130+
AC_DEFINE([WITHOUT_OPENSSL_ENGINES], [1],
131+
[Compile without OpenSSL engines support as it is disabled])
132+
fi
133+
108134
AC_DEFINE_UNQUOTED(
109135
[WITH_RAW_PSS],
110136
[1],

m4/acx_openssl_engines.m4

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
AC_DEFUN([ACX_OPENSSL_ENGINES], [
2+
3+
tmp_CPPFLAGS=$CPPFLAGS
4+
tmp_LIBS=$LIBS
5+
6+
CPPFLAGS="$CPPFLAGS $CRYPTO_INCLUDES"
7+
LIBS="$CRYPTO_LIBS $LIBS"
8+
9+
AC_LANG_PUSH([C])
10+
AC_CACHE_VAL([acx_cv_lib_openssl_engines_support], [
11+
acx_cv_lib_openssl_engines_support=no
12+
AC_COMPILE_IFELSE([
13+
AC_LANG_SOURCE([[
14+
#include <openssl/engine.h>
15+
#ifdef OPENSSL_NO_ENGINE
16+
#error "Engines are disabled"
17+
#endif
18+
int main() {
19+
ENGINE_load_builtin_engines();
20+
return 0;
21+
}
22+
]])
23+
], [
24+
acx_cv_lib_openssl_engines_support=yes
25+
], [
26+
acx_cv_lib_openssl_engines_support=no
27+
])
28+
])
29+
AC_LANG_POP([C])
30+
31+
CPPFLAGS=$tmp_CPPFLAGS
32+
LIBS=$tmp_LIBS
33+
34+
have_lib_openssl_engines_support="${acx_cv_lib_openssl_engines_support}"
35+
])

src/lib/crypto/OSSLCryptoFactory.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ OSSLCryptoFactory::OSSLCryptoFactory()
141141
// Initialise OpenSSL
142142
OpenSSL_add_all_algorithms();
143143

144+
#ifdef WITH_ENGINES
144145
#if !( OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) )
145146
// Make sure RDRAND is loaded first
146147
ENGINE_load_rdrand();
@@ -161,11 +162,12 @@ OSSLCryptoFactory::OSSLCryptoFactory()
161162
WARNING_MSG("ENGINE_set_default returned %lu\n", ERR_get_error());
162163
}
163164
}
165+
#endif
164166

165167
// Initialise the one-and-only RNG
166168
rng = new OSSLRNG();
167169

168-
#ifdef WITH_GOST
170+
#if defined(WITH_ENGINES) && defined(WITH_GOST)
169171
// Load engines
170172
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
171173
ENGINE_load_builtin_engines();
@@ -228,7 +230,7 @@ OSSLCryptoFactory::~OSSLCryptoFactory()
228230
{
229231
bool ossl_shutdown = false;
230232

231-
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
233+
#if defined(WITH_ENGINES) && OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
232234
// OpenSSL 1.1.0+ will register an atexit() handler to run
233235
// OPENSSL_cleanup(). If that has already happened we must
234236
// not attempt to free any ENGINEs because they'll already
@@ -243,6 +245,7 @@ OSSLCryptoFactory::~OSSLCryptoFactory()
243245
#endif
244246
if (!ossl_shutdown)
245247
{
248+
#ifdef WITH_ENGINES
246249
#ifdef WITH_GOST
247250
// Finish the GOST engine
248251
if (eg != NULL)
@@ -257,6 +260,7 @@ OSSLCryptoFactory::~OSSLCryptoFactory()
257260
ENGINE_finish(rdrand_engine);
258261
ENGINE_free(rdrand_engine);
259262
rdrand_engine = NULL;
263+
#endif
260264

261265
// Recycle locks
262266
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)

src/lib/crypto/OSSLCryptoFactory.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@
4242
#include "RNG.h"
4343
#include <memory>
4444
#include <openssl/conf.h>
45+
#if !defined(WITHOUT_OPENSSL_ENGINES) && !defined(OPENSSL_NO_ENGINES)
46+
#define WITH_ENGINES 1
4547
#include <openssl/engine.h>
48+
#endif
49+
4650

4751
class OSSLCryptoFactory : public CryptoFactory
4852
{
@@ -103,13 +107,16 @@ class OSSLCryptoFactory : public CryptoFactory
103107

104108
// The one-and-only RNG instance
105109
RNG* rng;
110+
111+
#ifdef WITH_ENGINES
106112
// And RDRAND engine to use with it
107113
ENGINE *rdrand_engine;
108114

109115
#ifdef WITH_GOST
110116
// The GOST engine
111117
ENGINE *eg;
112118
#endif
119+
#endif
113120
};
114121

115122
#endif // !_SOFTHSM_V2_OSSLCRYPTOFACTORY_H

0 commit comments

Comments
 (0)