Skip to contentSkip to conversation

ima-evm-utils: Implement function to only create IMA signature

PATCH Superseded Stefan Berger 2 participants 7 messages Apr 20, 2021 – Apr 27, 2021 Keys-trusted

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover ima-evm-utils: Implement function to only create IMA signature

This series of patches implements imaevm_create_ima_signature, a function that only creates an IMA v2 signature but does not write it into an xattr. This API fullfills a requirement of rpm-ostree where a tool is being built to sign all files from something similar to a tar byte stream, though where files are written to temporary files for signing purpose but the signature itself should not be written into the filesystem's xattr but simply returned via the API.

Stefan

Stefan Berger (3):
libimaevm: Implement imaevm_create_ima_signature
tests: Add program to create IMA signature with new API call
libimaevm: Have sign_hash_v2 call sign_hash_v2_pkey after reading key
file

 src/imaevm.h                 |   2 +
 src/libimaevm.c              | 191 ++++++++++++++++++++++++++++++-----
 tests/Makefile.am            |   6 ++
 tests/create_ima_signature.c | 111 ++++++++++++++++++++
 tests/sign_verify.test       |  21 +++-
 5 files changed, 304 insertions(+), 27 deletions(-)
 create mode 100644 tests/create_ima_signature.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/3 libimaevm: Implement imaevm_create_ima_signature

Implement imaevm_create_ima_signature that creates an IMA V2 signature with a given private key and without writing the signature into an extended attribute. This allows a caller to provide a single key for creating a signature on multiple files without having to read the key every time. It also allows the caller to store the signature wherever it wants, which may not necessarily be an extended attribute.

---
 src/imaevm.h    |   2 +
 src/libimaevm.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 188 insertions(+)
src/imaevm.h +2 -0
index 4503919..fc4536b 100644
@@ -225,5 +225,7 @@ int ima_verify_signature(const char *file, unsigned char *sig, int siglen, unsig
 void init_public_keys(const char *keyfiles);
 int imaevm_hash_algo_from_sig(unsigned char *sig);
 const char *imaevm_hash_algo_by_id(int algo);
+int imaevm_create_ima_signature(const char *filename, EVP_PKEY *pkey, const char *algo,
+                                unsigned char *sig, size_t siglen, char **error);
 
 #endif
src/libimaevm.c +186 -0

207 lines not on this page · raw message

Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 1/3] libimaevm: Implement imaevm_create_ima_signature

Quoted 1 line · Stefan Berger

+ fd = open(filename, O_RDONLY);

Missing O_CLOEXEC.

Quoted 1 line

+int imaevm_create_ima_signature(const char *filename, EVP_PKEY *pkey,

It'd maximize flexibility for the caller to pass a file descriptor, and not a file name.

src/libimaevm.c int sign_hash(const char *hashalgo, const unsigned char *hash, int size, const c · line 1133

Quoted 7 lines
src/libimaevm.c +7 -0
+	if (statbuf.st_size > 0) {
+		addr = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
+		if (addr == MAP_FAILED) {
+			asprintf(error, "Failed to mmap file: %s", strerror(errno));
+			goto err_close;
+		}
+	}

Tangentially related to this, I think we should consider doing the same optimization here:
https://github.com/ostreedev/ostree/blob/36693f064c63dad550ebcfed33bf9b95806ddef9/src/libotutil/ot-fs-utils.c#L171

Or alternatively, just have the caller provide a (mmap'd or copied-via-read()) buffer?

Though clearly the most flexible is a streaming API. But eh, I am not really concerned about that level of performance.

#3 lore raw

Re: [PATCH 1/3] libimaevm: Implement imaevm_create_ima_signature

Quoted 5 lines · Colin Walters
Quoted 1 line · Stefan Berger

+ fd = open(filename, O_RDONLY);

Missing O_CLOEXEC.

Will do.

Quoted 3 lines
Quoted 2 lines

+int imaevm_create_ima_signature(const char *filename, EVP_PKEY *pkey,

It'd maximize flexibility for the caller to pass a file descriptor, and not a file name.

We could do this with a callback where the user implements the callback function and providers buffer, size of buffer, and eof indicator, and gets called for providing the data to hash. That would maybe be even more flexible..

Quoted 11 lines

src/libimaevm.c int sign_hash(const char *hashalgo, const unsigned char *hash, int size, const c · line 1133

Quoted 8 lines
src/libimaevm.c +7 -0
+	if (statbuf.st_size > 0) {
+		addr = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
+		if (addr == MAP_FAILED) {
+			asprintf(error, "Failed to mmap file: %s", strerror(errno));
+			goto err_close;
+		}
+	}

Tangentially related to this, I think we should consider doing the same optimization here:
https://github.com/ostreedev/ostree/blob/36693f064c63dad550ebcfed33bf9b95806ddef9/src/libotutil/ot-fs-utils.c#L171

Quoted 3 lines

Or alternatively, just have the caller provide a (mmap'd or copied-via-read()) buffer?

Though clearly the most flexible is a streaming API. But eh, I am not really concerned about that level of performance.

I think using a callback would allow for the streaming as well...

   Stefan

#4 lore raw

Re: [PATCH 1/3] libimaevm: Implement imaevm_create_ima_signature

Quoted 9 lines · Stefan Berger
Quoted 5 lines · Colin Walters
Quoted 1 line · Stefan Berger

+ fd = open(filename, O_RDONLY);

Missing O_CLOEXEC.

Quoted 8 lines

Will do.

Quoted 3 lines
Quoted 2 lines

+int imaevm_create_ima_signature(const char *filename, EVP_PKEY *pkey,

It'd maximize flexibility for the caller to pass a file descriptor, and not a file name.

We could do this with a callback where the user implements the callback function and providers buffer, size of buffer, and eof indicator, and gets called for providing the data to hash. That would maybe be even more flexible..

Actually, I cannot do this. For this function we have to call ima_calc_hash, which hashes, files, links, directories etc.

    Stefan

#5 lore raw

PATCH 2/3 tests: Add program to create IMA signature with new API call

In reply to Stefan Berger

Since the new API call is not used by evmctl, implement a test program 'create_ima_signature' to use it. Extend _evmctl_sign to also created IMA v2 signatures with RSA keys using this test program and compare the results.

Evmctl's signature creation path is unmodified at this point, so the tests ensure that the existing sign_hash_v2 and the new sign_hash_v2_pkey create identical (RSA) signatures.

---
 tests/Makefile.am            |   6 ++
 tests/create_ima_signature.c | 111 +++++++++++++++++++++++++++++++++++
 tests/sign_verify.test       |  21 ++++++-
 3 files changed, 136 insertions(+), 2 deletions(-)
 create mode 100644 tests/create_ima_signature.c
tests/Makefile.am +6 -0
index ff928e1..5e255b1 100644
@@ -3,6 +3,12 @@ TESTS = $(check_SCRIPTS)
 
 check_SCRIPTS += ima_hash.test sign_verify.test boot_aggregate.test
 
+check_PROGRAMS = \
+	create_ima_signature
+
+create_ima_signature_CFLAGS = -I$(top_srcdir)/src
+create_ima_signature_LDFLAGS = -lcrypto -L$(top_builddir)/src/.libs -limaevm
+
 clean-local:
 	-rm -f *.txt *.out *.sig *.sig2
 
tests/create_ima_signature.c new file +111 -0

111 lines not on this page · raw message

tests/sign_verify.test +19 -2
index 288e133..14964ec 100755
@@ -16,10 +16,10 @@
 # GNU General Public License for more details.
 
 cd "$(dirname "$0")" || exit 1
-PATH=../src:$PATH
+PATH=../src:.:$PATH
 source ./functions.sh
 
-_require cmp evmctl getfattr openssl xxd
+_require cmp evmctl getfattr openssl xxd create_ima_signature
 
 if cmp -b 2>&1 | grep -q "invalid option"; then
 	echo "cmp does not support -b (cmp from busybox?) Use cmp from diffutils"
@@ -118,7 +118,24 @@ _evmctl_sign() {
 
   if [ "$type" = ima ]; then
     _test_sigfile "$file" "$(_xattr "$type")" "$file.sig" "$file.sig2"
+    if [ $? -ne $OK ]; then
+        return "$FAIL"
+    fi
+  fi
+  # Compare evmctl IMA v2 signatures with RSA keys versus those from create_ima_signature
+  if [ "$type" = ima ] && [[ $key =~ rsa ]] && ! [[ $opts =~ --rsa ]] ; then
+    create_ima_signature --key "${key}" --hashalgo "${alg}" "${file}" |
+      sed -n 's/^03.*/\0/p' |
+      xxd -r -p > "$file.sig2"
+
+    if ! cmp -bl "$file.sig" "$file.sig2"; then
+      color_red
+      echo "evmctl vs. create_ima_signature: signatures on $file differ"
+      color_restore
+      return "$FAIL"
+    fi
   fi
+  return $OK
 }
 
 # Run and test {ima_,}sign operation
Signature · 1 line
2.30.2
#6 lore raw

PATCH 3/3 libimaevm: Have sign_hash_v2 call sign_hash_v2_pkey after reading key file

In reply to Stefan Berger

Have sign_hash_v2 call sign_hash_v2_pkey after reading the key file.

---
 src/libimaevm.c | 65 ++++++++-----------------------------------------
 1 file changed, 10 insertions(+), 55 deletions(-)
src/libimaevm.c +10 -55
index bb425af..f7f070c 100644
@@ -991,15 +991,9 @@ err:
 static int sign_hash_v2(const char *algo, const unsigned char *hash,
 			int size, const char *keyfile, unsigned char *sig)
 {
-	struct signature_v2_hdr *hdr;
-	int len = -1;
+	char *error = NULL;
 	EVP_PKEY *pkey;
-	char name[20];
-	EVP_PKEY_CTX *ctx = NULL;
-	const EVP_MD *md;
-	size_t sigsize;
-	const char *st;
-	uint32_t keyid;
+	int len = -1;
 
 	if (!hash) {
 		log_err("sign_hash_v2: hash is null\n");
@@ -1011,16 +1005,6 @@ static int sign_hash_v2(const char *algo, const unsigned char *hash,
 		return -1;
 	}
 
-	if (!sig) {
-		log_err("sign_hash_v2: sig is null\n");
-		return -1;
-	}
-
-	if (!algo) {
-		log_err("sign_hash_v2: algo is null\n");
-		return -1;
-	}
-
 	log_info("hash(%s): ", imaevm_params.hash_algo);
 	log_dump(hash, size);
 
@@ -1028,49 +1012,20 @@ static int sign_hash_v2(const char *algo, const unsigned char *hash,
 	if (!pkey)
 		return -1;
 
-	hdr = (struct signature_v2_hdr *)sig;
-	hdr->version = (uint8_t) DIGSIG_VERSION_2;
-
-	hdr->hash_algo = imaevm_get_hash_algo(algo);
-	if (hdr->hash_algo == (uint8_t)-1) {
-		log_err("sign_hash_v2: hash algo is unknown: %s\n", algo);
-		return -1;
-	}
-
-	calc_keyid_v2(&keyid, name, pkey);
-	hdr->keyid = keyid;
-
-	st = "EVP_PKEY_CTX_new";
-	if (!(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
-		goto err;
-	st = "EVP_PKEY_sign_init";
-	if (!EVP_PKEY_sign_init(ctx))
-		goto err;
-	st = "EVP_get_digestbyname";
-	if (!(md = EVP_get_digestbyname(imaevm_params.hash_algo)))
-		goto err;
-	st = "EVP_PKEY_CTX_set_signature_md";
-	if (!EVP_PKEY_CTX_set_signature_md(ctx, md))
-		goto err;
-	st = "EVP_PKEY_sign";
-	sigsize = MAX_SIGNATURE_SIZE - sizeof(struct signature_v2_hdr) - 1;
-	if (!EVP_PKEY_sign(ctx, hdr->sig, &sigsize, hash, size))
+	len = sign_hash_v2_pkey(algo, hash, size, pkey, sig, MAX_SIGNATURE_SIZE - 1,
+	                        &error);
+	if (len < 0) {
+		log_err("%s\n", error);
+		output_openssl_errors();
 		goto err;
-	len = (int)sigsize;
+	}
 
-	/* we add bit length of the signature to make it gnupg compatible */
-	hdr->sig_size = __cpu_to_be16(len);
-	len += sizeof(*hdr);
 	log_info("evm/ima signature: %d bytes\n", len);
 
 err:
-	if (len == -1) {
-		log_err("sign_hash_v2: signing failed: (%s) in %s\n",
-			ERR_reason_error_string(ERR_peek_error()), st);
-		output_openssl_errors();
-	}
-	EVP_PKEY_CTX_free(ctx);
+	free(error);
 	EVP_PKEY_free(pkey);
+
 	return len;
 }
 
Signature · 1 line
2.30.2