Skip to content

Commit e4bdd55

Browse files
Aditi AmbadkarRadovan Sroka
authored andcommitted
Add OpenSSL as crypto backend for device hashing
Use EVP_sha256 OpenSSL libcrypto API to implement SHA-256 hashing in the crypto backend for device hashing Pass parameter --with-crypto-library=openssl to configure USBGuard with OpenSSL libcrypto Add OpenSSL pipeline requirements Include libssl-dev package and openssl parameter
1 parent 9b881ed commit e4bdd55

4 files changed

Lines changed: 74 additions & 2 deletions

File tree

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ matrix:
7474
packages: ['libgcrypt-dev']
7575
env: CONFIGURE_ARGS=--with-crypto-library=gcrypt
7676

77+
- os: linux
78+
compiler: gcc
79+
addons:
80+
apt:
81+
packages: ['libssl-dev']
82+
env: CONFIGURE_ARGS=--with-crypto-library=openssl
83+
7784
- os: linux
7885
compiler: gcc
7986
env: CONFIGURE_ARGS=--without-ldap

configure.ac

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ libsodium_available=yes,
192192
[]
193193
)
194194

195+
#
196+
# libcrypto library (OpenSSL)
197+
#
198+
PKG_CHECK_MODULES([libcrypto], [libcrypto >= 1.0.0],
199+
[AC_DEFINE([HAVE_LIBCRYPTO], [1], [libcrypto API available])
200+
libcrypto_summary="system-wide; $libcrypto_CFLAGS $libcrypto_LIBS"]
201+
libcrypto_available=yes,
202+
[]
203+
)
204+
195205
#
196206
# gcrypt library
197207
#
@@ -211,12 +221,23 @@ libgcrypt_available=yes],
211221
#
212222
# sodium ... libsodium
213223
# gcrypt ... libgcrypt
224+
# openssl ... libcrypto
214225
#
215226
AC_ARG_WITH([crypto-library], AS_HELP_STRING([--with-crypto-library],
216-
[Select crypto backend library. Supported values: sodium, gcrypt.]),
227+
[Select crypto backend library. Supported values: sodium, gcrypt, openssl.]),
217228
[with_crypto_library=$withval], [with_crypto_library=sodium])
218229

219230
case "$with_crypto_library" in
231+
openssl)
232+
if test "x$libcrypto_available" = xyes; then
233+
crypto_CFLAGS="$libcrypto_CFLAGS"
234+
crypto_LIBS="$libcrypto_LIBS"
235+
crypto_summary="$libcrypto_summary"
236+
AC_DEFINE([USBGUARD_USE_OPENSSL], [1], [Use openssl as crypto backend])
237+
else
238+
AC_MSG_ERROR([The selected crypto backend library is not available.])
239+
fi
240+
;;
220241
sodium)
221242
if test "x$libsodium_available" = xyes; then
222243
crypto_CFLAGS="$sodium_CFLAGS"
@@ -238,7 +259,7 @@ case "$with_crypto_library" in
238259
fi
239260
;;
240261
*)
241-
AC_MSG_FAILURE([Invalid crypto library selector. Supported selectors: sodium, gcrypt])
262+
AC_MSG_FAILURE([Invalid crypto library selector. Supported selectors: sodium, gcrypt, openssl])
242263
esac
243264
AC_SUBST([crypto_CFLAGS])
244265
AC_SUBST([crypto_LIBS])

src/Library/Hash.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ namespace usbguard
3838
#if defined(USBGUARD_USE_LIBSODIUM)
3939
crypto_hash_sha256_init(&_state);
4040
#endif
41+
#if defined(USBGUARD_USE_OPENSSL)
42+
if ((_state = EVP_MD_CTX_new()) == nullptr)
43+
throw std::runtime_error("Dynamic memory allocation of message digest context failed.");
44+
if (!EVP_DigestInit_ex(_state, EVP_sha256(), nullptr))
45+
throw std::runtime_error("Context initialization of message digest context failed.");
46+
#endif
4147
#if defined(USBGUARD_USE_LIBGCRYPT)
4248
gcry_md_open(&_state, GCRY_MD_SHA256, 0);
4349
#endif
@@ -49,6 +55,9 @@ namespace usbguard
4955
#if defined(USBGUARD_USE_LIBSODIUM)
5056
_state = rhs._state;
5157
#endif
58+
#if defined(USBGUARD_USE_OPENSSL)
59+
_state = rhs._state;
60+
#endif
5261
#if defined(USBGUARD_USE_LIBGCRYPT)
5362
gcry_md_copy(&_state, rhs._state);
5463
#endif
@@ -61,6 +70,10 @@ namespace usbguard
6170
_state = rhs._state;
6271
memset(&rhs._state, 0, sizeof _state);
6372
#endif
73+
#if defined(USBGUARD_USE_OPENSSL)
74+
_state = rhs._state;
75+
memset(&rhs._state, 0, sizeof _state);
76+
#endif
6477
#if defined(USBGUARD_USE_LIBGCRYPT)
6578
_state = rhs._state;
6679
rhs._state = nullptr;
@@ -74,6 +87,10 @@ namespace usbguard
7487
_state = rhs._state;
7588
memset(&rhs._state, 0, sizeof _state);
7689
#endif
90+
#if defined(USBGUARD_USE_OPENSSL)
91+
_state = rhs._state;
92+
memset(&rhs._state, 0, sizeof _state);
93+
#endif
7794
#if defined(USBGUARD_USE_LIBGCRYPT)
7895
_state = rhs._state;
7996
rhs._state = nullptr;
@@ -83,6 +100,9 @@ namespace usbguard
83100

84101
Hash::~Hash()
85102
{
103+
#if defined(USBGUARD_USE_OPENSSL)
104+
EVP_MD_CTX_free(_state);
105+
#endif
86106
release();
87107
}
88108

@@ -91,6 +111,9 @@ namespace usbguard
91111
#if defined(USBGUARD_USE_LIBSODIUM)
92112
memset(&_state, 0, sizeof _state);
93113
#endif
114+
#if defined(USBGUARD_USE_OPENSSL)
115+
memset(&_state, 0, sizeof _state);
116+
#endif
94117
#if defined(USBGUARD_USE_LIBGCRYPT)
95118

96119
if (_state != nullptr) {
@@ -110,6 +133,10 @@ namespace usbguard
110133
#if defined(USBGUARD_USE_LIBSODIUM)
111134
crypto_hash_sha256_update(&_state, reinterpret_cast<const uint8_t*>(ptr), size);
112135
#endif
136+
#if defined(USBGUARD_USE_OPENSSL)
137+
if (!EVP_DigestUpdate(_state, reinterpret_cast<const uint8_t*>(ptr), size))
138+
throw std::runtime_error("Hashing data into message digest context failed.");
139+
#endif
113140
#if defined(USBGUARD_USE_LIBGCRYPT)
114141
gcry_md_write(_state, ptr, size);
115142
#endif
@@ -130,6 +157,10 @@ namespace usbguard
130157
#if defined(USBGUARD_USE_LIBSODIUM)
131158
crypto_hash_sha256_update(&_state, buffer, buflen);
132159
#endif
160+
#if defined(USBGUARD_USE_OPENSSL)
161+
if (!EVP_DigestUpdate(_state, buffer, buflen))
162+
throw std::runtime_error("Hashing data into message digest context failed.");
163+
#endif
133164
#if defined(USBGUARD_USE_LIBGCRYPT)
134165
gcry_md_write(_state, buffer, buflen);
135166
#endif
@@ -148,6 +179,14 @@ namespace usbguard
148179
const uint8_t* const hash_buffer = hash_binary;
149180
const size_t hash_buflen = sizeof hash_binary;
150181
#endif
182+
#if defined(USBGUARD_USE_OPENSSL)
183+
uint8_t hash_binary[EVP_MAX_MD_SIZE];
184+
unsigned int hash_len;
185+
if (!EVP_DigestFinal_ex(_state, hash_binary, &hash_len))
186+
throw std::runtime_error("Digest value retrieval failed.");
187+
const uint8_t* const hash_buffer = hash_binary;
188+
const size_t hash_buflen = hash_len;
189+
#endif
151190
#if defined(USBGUARD_USE_LIBGCRYPT)
152191
gcry_md_final(_state);
153192
const size_t hash_buflen = gcry_md_get_algo_dlen(GCRY_MD_SHA256);

src/Library/Hash.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
#if defined(USBGUARD_USE_LIBSODIUM)
3232
#include <sodium.h>
33+
#elif defined(USBGUARD_USE_OPENSSL)
34+
#include <openssl/evp.h>
3335
#elif defined(USBGUARD_USE_LIBGCRYPT)
3436
#include <gcrypt.h>
3537
#else
@@ -56,6 +58,9 @@ namespace usbguard
5658
#if defined(USBGUARD_USE_LIBSODIUM)
5759
crypto_hash_sha256_state _state;
5860
#endif
61+
#if defined(USBGUARD_USE_OPENSSL)
62+
EVP_MD_CTX *_state;
63+
#endif
5964
#if defined(USBGUARD_USE_LIBGCRYPT)
6065
gcry_md_hd_t _state {nullptr};
6166
#endif

0 commit comments

Comments
 (0)