From 3d6d385815e48dcee92dc0bcdc690ad811c14bba Mon Sep 17 00:00:00 2001 From: Alexios Zavras Date: Tue, 21 Mar 2023 12:28:34 +0100 Subject: [PATCH] Uses newer OpenSSL hash functions Signed-off-by: Alexios Zavras --- .indent.pro | 3 ++- configure.ac | 14 ++++++-------- src/hash.c | 26 +++++++++++++++++++------- src/tracer.c | 3 ++- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/.indent.pro b/.indent.pro index 9c07711..4b2a8a4 100644 --- a/.indent.pro +++ b/.indent.pro @@ -82,7 +82,8 @@ -Tva_list /* OpenSSL types */ --TSHA_CTX +-TEVP_MD +-TEVP_MD_CTX /* project-specific types */ -TFILE_INFO diff --git a/configure.ac b/configure.ac index 070eee0..8dc6df9 100644 --- a/configure.ac +++ b/configure.ac @@ -27,11 +27,11 @@ AC_CONFIG_SRCDIR([src/main.c]) dnl Checks for programs dnl Checks for libraries -AC_CHECK_LIB(crypto, SHA1_Init,, AC_MSG_ERROR([cannot find libcrypto])) +AC_CHECK_LIB(crypto, EVP_sha1,, AC_MSG_ERROR([cannot find libcrypto])) dnl Checks for headers AC_CHECK_HEADER([linux/ptrace.h],, AC_MSG_ERROR([cannot find linux/ptrace.h])) -AC_CHECK_HEADER([openssl/sha.h],, AC_MSG_ERROR([cannot find openssl/sha.h])) +AC_CHECK_HEADER([openssl/evp.h],, AC_MSG_ERROR([cannot find openssl/evp.h])) AC_CHECK_HEADER([sys/mman.h],, AC_MSG_ERROR([cannot find sys/mman.h])) AC_CHECK_HEADER([sys/ptrace.h],, AC_MSG_ERROR([cannot find sys/ptrace.h])) AC_CHECK_HEADER([sys/signal.h],, AC_MSG_ERROR([cannot find sys/signal.h])) @@ -47,8 +47,6 @@ AC_CHECK_TYPE([struct ptrace_syscall_info],, AC_MSG_ERROR([cannot find struct pt AC_CHECK_TYPE([pid_t],, AC_MSG_ERROR([cannot find pid_t]), [#include ]) -AC_CHECK_TYPE([SHA_CTX],, AC_MSG_ERROR([cannot find SHA_CTX]), [#include ]) - AC_CHECK_DECL([PATH_MAX],, AC_MSG_ERROR([cannot find PATH_MAX]), [#include ]) AC_CHECK_DECL([ARG_MAX],, AC_MSG_ERROR([cannot find ARG_MAX]), [#include ]) @@ -76,10 +74,6 @@ AC_CHECK_DECL([SYS_close],, AC_MSG_ERROR([cannot find SYS_close]), [#include ]) dnl Checks for functions -AC_CHECK_FUNC([SHA1_Init],, AC_MSG_ERROR([cannot find SHA1_Init(3)])) -AC_CHECK_FUNC([SHA1_Update],, AC_MSG_ERROR([cannot find SHA1_Update(3)])) -AC_CHECK_FUNC([SHA1_Final],, AC_MSG_ERROR([cannot find SHA1_Final(3)])) - AC_CHECK_FUNC([fork],, AC_MSG_ERROR([cannot find fork(2)])) AC_CHECK_FUNC([ptrace],, AC_MSG_ERROR([cannot find ptrace(2)])) AC_CHECK_FUNC([waitpid],, AC_MSG_ERROR([cannot find waitpid(2)])) @@ -87,6 +81,10 @@ AC_CHECK_FUNC([waitpid],, AC_MSG_ERROR([cannot find waitpid(2)])) AC_CHECK_FUNC([mmap],, AC_MSG_ERROR([cannot find mmap(2)])) AC_CHECK_FUNC([madvise],, AC_MSG_ERROR([cannot find madvise(2)])) +AC_CHECK_FUNC([EVP_DigestInit_ex],, AC_MSG_ERROR([cannot find EVP_DigestInit_ex(3)])) +AC_CHECK_FUNC([EVP_DigestUpdate],, AC_MSG_ERROR([cannot find EVP_DigestUpdate(3)])) +AC_CHECK_FUNC([EVP_DigestFinal_ex],, AC_MSG_ERROR([cannot find EVP_DigestFinal_ex(3)])) + dnl We need a C compiler AC_PROG_CC diff --git a/src/hash.c b/src/hash.c index a8b1204..cd390c2 100644 --- a/src/hash.c +++ b/src/hash.c @@ -22,8 +22,10 @@ SPDX-License-Identifier: LGPL-2.1-or-later #include // #include // optind, readlink(2) -#include // SHA_CTX, SHA1_Init, SHA1_Update, - // SHA1_Final +#include // EVP_sha1(), EVP_DigestInit_ex(), + // EVP_DigestUpdate(), + // EVP_DigestFinal_ex() + #define SHA1_OUTPUT_LEN 20 #define SHA1_HEXBUF_LEN (2 * SHA1_OUTPUT_LEN + 1) @@ -77,7 +79,6 @@ hash_file_contents(char *name, size_t sz) presize = sprintf(pre, "blob %lu%c", sz, 0); - SHA_CTX ctx; unsigned char *hash; hash = malloc(SHA1_OUTPUT_LEN); @@ -86,10 +87,17 @@ hash_file_contents(char *name, size_t sz) return NULL; } - SHA1_Init(&ctx); - SHA1_Update(&ctx, pre, presize); - SHA1_Update(&ctx, buf, sz); - SHA1_Final(hash, &ctx); + const static EVP_MD *sha1_md; + + if (sha1_md == 0) + sha1_md = EVP_sha1(); + + EVP_MD_CTX *ctx = EVP_MD_CTX_new(); + + EVP_DigestInit_ex(ctx, sha1_md, NULL); + EVP_DigestUpdate(ctx, pre, presize); + EVP_DigestUpdate(ctx, buf, sz); + EVP_DigestFinal_ex(ctx, hash, NULL); close(fd); @@ -114,6 +122,10 @@ get_file_hash(char *fname) if (sz > 0) { uint8_t *h = hash_file_contents(fname, sz); + + if (h == NULL) + return NULL; + char *ret = hash_to_str(h); free(h); diff --git a/src/tracer.c b/src/tracer.c index 7b18267..22f9224 100644 --- a/src/tracer.c +++ b/src/tracer.c @@ -143,7 +143,8 @@ find_finfo(char *abspath, char *hash) while (i >= 0) { if (!strcmp(abspath, finfo[i].abspath) && ((hash == NULL && finfo[i].hash == NULL) - || !strcmp(hash, finfo[i].hash))) { + || (hash != NULL && finfo[i].hash != NULL + && !strcmp(hash, finfo[i].hash)))) { break; }