Blame SOURCES/libkcapi-1.1.1-kcapi-hasher_Add_missing_-d_option_to_fipshmac.patch

b09f25
From 2a0642407dd227d24e646c170d8afd47ab917899 Mon Sep 17 00:00:00 2001
b09f25
From: Ondrej Mosnacek <omosnace@redhat.com>
b09f25
Date: Mon, 16 Jul 2018 15:17:29 +0200
b09f25
Subject: [PATCH] kcapi-hasher: Add missing -d option to fipshmac
b09f25
b09f25
---
b09f25
 apps/kcapi-hasher.c | 61 ++++++++++++++++++++++++++++-------------------------
b09f25
 1 file changed, 32 insertions(+), 29 deletions(-)
b09f25
b09f25
diff --git a/apps/kcapi-hasher.c b/apps/kcapi-hasher.c
b09f25
index 6782dbc..2fc3ddc 100644
b09f25
--- a/apps/kcapi-hasher.c
b09f25
+++ b/apps/kcapi-hasher.c
b09f25
@@ -71,7 +71,7 @@ struct hash_name {
b09f25
 };
b09f25
 
b09f25
 struct hash_key {
b09f25
-	const char *subdir;
b09f25
+	const char *checkdir;
b09f25
 	const uint8_t *data;
b09f25
 	uint32_t len;
b09f25
 };
b09f25
@@ -108,12 +108,20 @@ static const char hmaccalc_hmackey[] = "FIPS-FTW-RHT2009";
b09f25
 static const struct hash_key KEY_FIPSCHECK = {
b09f25
 	.data = (const uint8_t *)fipscheck_hmackey,
b09f25
 	.len = sizeof(fipscheck_hmackey) - 1,
b09f25
-	.subdir = "fipscheck",
b09f25
+#ifdef CHECK_DIR
b09f25
+	.checkdir = CHECK_DIR"/fipscheck",
b09f25
+#else
b09f25
+	.checkdir = NULL,
b09f25
+#endif
b09f25
 };
b09f25
 static const struct hash_key KEY_HMACCALC = {
b09f25
 	.data = (const uint8_t *)hmaccalc_hmackey,
b09f25
 	.len = sizeof(hmaccalc_hmackey) - 1,
b09f25
-	.subdir = "hmaccalc",
b09f25
+#ifdef CHECK_DIR
b09f25
+	.checkdir = CHECK_DIR"/hmaccalc",
b09f25
+#else
b09f25
+	.checkdir = NULL,
b09f25
+#endif
b09f25
 };
b09f25
 
b09f25
 static void usage(char *name, int fipscheck)
b09f25
@@ -142,7 +150,8 @@ static void usage(char *name, int fipscheck)
b09f25
 	fprintf(stderr, "\t-k --key-file FILE\tUse HMAC key from given file\n");
b09f25
 	fprintf(stderr, "\t-K --key KEY\t\tUse KEY as the HMAC key\n");
b09f25
 	fprintf(stderr, "\t   --tag\t\tCreate a BSD-style checksum\n");
b09f25
-	fprintf(stderr, "\t-b, -d, -P\t\tCompatibility hmaccalc options; ignored\n");
b09f25
+	fprintf(stderr, "\t-d\t\t\tCheck directory for fipshmac; otherwise ignored\n");
b09f25
+	fprintf(stderr, "\t-b, -P\t\t\tCompatibility hmaccalc options; ignored\n");
b09f25
 	fprintf(stderr, "\t   --help\t\tPrint this help text\n");
b09f25
 	fprintf(stderr, "\t-v --version\t\tShow version\n");
b09f25
 }
b09f25
@@ -368,7 +377,7 @@ static char *paste(char *dst, const char *src, size_t size)
b09f25
  * return: NULL when malloc failed, a pointer that the caller must free
b09f25
  * otherwise.
b09f25
  */
b09f25
-static char *get_hmac_file(const char *filename, const char *subdir)
b09f25
+static char *get_hmac_file(const char *filename, const char *checkdir)
b09f25
 {
b09f25
 	size_t i, filelen, pathlen, namelen, basenamestart = 0;
b09f25
 	size_t prefixlen = strlen(CHECK_PREFIX);
b09f25
@@ -386,12 +395,7 @@ static char *get_hmac_file(const char *filename, const char *subdir)
b09f25
 	}
b09f25
 
b09f25
 	namelen = filelen - basenamestart;
b09f25
-#ifdef CHECK_DIR
b09f25
-	pathlen = strlen(CHECK_DIR"/") + strlen(subdir) + 1;
b09f25
-#else
b09f25
-	(void)subdir; // avoid parameter unused warning
b09f25
-	pathlen = basenamestart;
b09f25
-#endif
b09f25
+	pathlen = checkdir ? strlen(checkdir) + 1 : basenamestart;
b09f25
 
b09f25
 	checkfile = malloc(pathlen + namelen + prefixlen + 1 /* "." */ +
b09f25
 		suffixlen + 1 /* null character */);
b09f25
@@ -399,14 +403,12 @@ static char *get_hmac_file(const char *filename, const char *subdir)
b09f25
 		return NULL;
b09f25
 
b09f25
 	cursor = checkfile;
b09f25
-#ifdef CHECK_DIR
b09f25
-	cursor = paste(cursor, CHECK_DIR"/", strlen(CHECK_DIR"/"));
b09f25
-	cursor = paste(cursor, subdir, strlen(subdir));
b09f25
-	cursor = paste(cursor, "/", 1);
b09f25
-#else
b09f25
-	if (pathlen > 0)
b09f25
+	if (checkdir) {
b09f25
+		cursor = paste(cursor, checkdir, strlen(checkdir));
b09f25
+		cursor = paste(cursor, "/", 1);
b09f25
+	} else if (pathlen > 0)
b09f25
 		cursor = paste(cursor, filename, pathlen);
b09f25
-#endif
b09f25
+
b09f25
 	cursor = paste(cursor, CHECK_PREFIX, prefixlen);
b09f25
 	cursor = paste(cursor, filename + basenamestart, namelen);
b09f25
 	cursor = paste(cursor, "."CHECK_SUFFIX, 1 + suffixlen);
b09f25
@@ -417,7 +419,7 @@ static char *get_hmac_file(const char *filename, const char *subdir)
b09f25
 
b09f25
 static int hash_files(const struct hash_params *params,
b09f25
 		      char *filenames[], uint32_t files,
b09f25
-		      int fipshmac, int just_print)
b09f25
+		      int fipshmac, const char *checkdir, int just_print)
b09f25
 {
b09f25
 	struct kcapi_handle *handle;
b09f25
 	const char *hashname = params->name.kcapiname;
b09f25
@@ -446,9 +448,7 @@ static int hash_files(const struct hash_params *params,
b09f25
 			const char *filename = filenames[i];
b09f25
 
b09f25
 			if (fipshmac) {
b09f25
-				char *outfile = get_hmac_file(filenames[i],
b09f25
-				                              params->key.subdir);
b09f25
-
b09f25
+				char *outfile = get_hmac_file(filenames[i], checkdir);
b09f25
 				if (!outfile) {
b09f25
 					fprintf(stderr,
b09f25
 						"Cannot create HMAC file name\n");
b09f25
@@ -712,11 +712,11 @@ static int fipscheck_self(const struct hash_params *params_bin,
b09f25
 		}
b09f25
 
b09f25
 		if (mode == SELFCHECK_PRINT_SELF) {
b09f25
-			ret = hash_files(params_bin, names, 1, 0, 1);
b09f25
+			ret = hash_files(params_bin, names, 1, 0, NULL, 1);
b09f25
 			goto out;
b09f25
 		}
b09f25
 
b09f25
-		checkfile = get_hmac_file(selfname, params_bin->key.subdir);
b09f25
+		checkfile = get_hmac_file(selfname, params_bin->key.checkdir);
b09f25
 		if (!checkfile) {
b09f25
 			ret = -ENOMEM;
b09f25
 			goto out;
b09f25
@@ -750,13 +750,13 @@ static int fipscheck_self(const struct hash_params *params_bin,
b09f25
 		strncpy(selfname, info.dli_fname, (sizeof(selfname) - 1));
b09f25
 
b09f25
 		if (mode == SELFCHECK_PRINT_LIB) {
b09f25
-			ret = hash_files(params_lib, names, 1, 0, 1);
b09f25
+			ret = hash_files(params_lib, names, 1, 0, NULL, 1);
b09f25
 			goto out;
b09f25
 		}
b09f25
 
b09f25
 		if (checkfile)
b09f25
 			free(checkfile);
b09f25
-		checkfile = get_hmac_file(selfname, params_lib->key.subdir);
b09f25
+		checkfile = get_hmac_file(selfname, params_lib->key.checkdir);
b09f25
 		if (!checkfile) {
b09f25
 			ret = -ENOMEM;
b09f25
 			goto out;
b09f25
@@ -799,6 +799,7 @@ int main(int argc, char *argv[])
b09f25
 
b09f25
 	char *checkfile = NULL;
b09f25
 	const char *targetfile = NULL;
b09f25
+	const char *checkdir = NULL;
b09f25
 	uint8_t *hmackey_alloc = NULL;
b09f25
 	uint8_t *hmackey_mmap = NULL;
b09f25
 	int opt_index = 0;
b09f25
@@ -1055,8 +1056,10 @@ int main(int argc, char *argv[])
b09f25
 				version(argv[0]);
b09f25
 				ret = 0;
b09f25
 				goto out;
b09f25
-			case 'b':
b09f25
 			case 'd':
b09f25
+				checkdir = optarg;
b09f25
+				break;
b09f25
+			case 'b':
b09f25
 			case 'P':
b09f25
 				/* Compatibility options, just ignore */
b09f25
 				break;
b09f25
@@ -1110,7 +1113,7 @@ int main(int argc, char *argv[])
b09f25
 		targetfile = argv[optind];
b09f25
 		if (checkfile)
b09f25
 			free(checkfile);
b09f25
-		checkfile = get_hmac_file(targetfile, params.key.subdir);
b09f25
+		checkfile = get_hmac_file(targetfile, params.key.checkdir);
b09f25
 		if (!checkfile) {
b09f25
 			ret = 1;
b09f25
 			goto out;
b09f25
@@ -1120,7 +1123,7 @@ int main(int argc, char *argv[])
b09f25
 
b09f25
 	if (!checkfile)
b09f25
 		ret = hash_files(&params, argv + optind, (argc - optind),
b09f25
-		                 fipshmac, 0);
b09f25
+		                 fipshmac, checkdir, 0);
b09f25
 	else if (optind == argc)
b09f25
 		ret = process_checkfile(&params, checkfile, targetfile, loglevel);
b09f25
 	else {