[monitoring-plugins] Use openssl message digest for state files

RincewindsHat git at monitoring-plugins.org
Sat Sep 17 03:00:13 CEST 2022


 Module: monitoring-plugins
 Branch: gnulib_2022
 Commit: 14f3305df20093bc897ccb9f4a91a572ea08e4d4
 Author: RincewindsHat <12514511+RincewindsHat at users.noreply.github.com>
   Date: Sat Sep 17 02:57:16 2022 +0200
    URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=14f3305

Use openssl message digest for state files

---

 lib/utils_base.c | 38 ++++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 10 deletions(-)

diff --git a/lib/utils_base.c b/lib/utils_base.c
index 08fa215..a78d6f8 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -33,6 +33,8 @@
 #include <unistd.h>
 #include <sys/types.h>
 
+#include <openssl/evp.h>
+
 #define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } }
 
 monitoring_plugin *this_monitoring_plugin=NULL;
@@ -402,25 +404,41 @@ int mp_translate_state (char *state_text) {
  * parse of argv, so that uniqueness in parameters are reflected there.
  */
 char *_np_state_generate_key() {
-	struct sha1_ctx ctx;
-	int i;
+	EVP_MD_CTX *mdctx = NULL;
 	char **argv = this_monitoring_plugin->argv;
-	unsigned char result[20];
-	char keyname[41];
+
+	unsigned char *result = NULL;
+	unsigned int result_length = 0;
+
+	const unsigned int output_string_length = 2 * 256 + 1;
+
+	char keyname[output_string_length];
 	char *p=NULL;
 
-	sha1_init_ctx(&ctx);
+	if((mdctx = EVP_MD_CTX_new()) == NULL)
+		die(STATE_UNKNOWN, _("Failed to create EVP MD context"));
+
+	if(1 != EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL))
+		die(STATE_UNKNOWN, _("Failed to initialized EVP MD context"));
 	
-	for(i=0; i<this_monitoring_plugin->argc; i++) {
-		sha1_process_bytes(argv[i], strlen(argv[i]), &ctx);
+	for(unsigned int i=0; i<this_monitoring_plugin->argc; i++) {
+		if(1 != EVP_DigestUpdate(mdctx, argv[i], strlen(argv[i])))
+			die(STATE_UNKNOWN, _("Failed to compute hash"));
+
 	}
 
-	sha1_finish_ctx(&ctx, &result);
+	if((result = (unsigned char *)OPENSSL_malloc(EVP_MD_size(EVP_sha256()))) == NULL)
+		die(STATE_UNKNOWN, _("Failed to allocate result string"));
+
+	if(1 != EVP_DigestFinal_ex(mdctx, result, &result_length))
+		die(STATE_UNKNOWN, _("Failed to finish hashing"));
+
+	EVP_MD_CTX_free(mdctx);
 	
-	for (i=0; i<20; ++i) {
+	for (unsigned int i=0; i<256; ++i) {
 		sprintf(&keyname[2*i], "%02x", result[i]);
 	}
-	keyname[40]='\0';
+	keyname[2 * 256]='\0';
 	
 	p = strdup(keyname);
 	if(p==NULL) {



More information about the Commits mailing list