Skip to contentSkip to conversation

ima-evm-utils: Add --keyid option

PATCH Superseded Vitaly Chikunov 2 participants 12 messages Jul 1, 2021 – Jul 8, 2021 Keys-trusted

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v7 cover ima-evm-utils: Add --keyid option

Allow user to set signature's keyid using `--keyid' option. Keyid should correspond to SKID in certificate. When keyid is calculated using SHA-1 in libimaevm it may mismatch keyid extracted by the kernel from SKID of certificate (the way public key is presented to the kernel), thus making signatures not verifiable. This may happen when certificate is using non SHA-1 SKID (see rfc7093) or just 'unique number' (see rfc5280 4.2.1.2). As a last resort user may specify arbitrary keyid using the new option. Certificate filename could be used instead of the hex number with `--keyid-from-cert' option. And, third option is to read keyid from the cert appended to the key file.

These commits create backward incompatible ABI change for libimaevm, thus soname should be incremented on release.

Changes from v6:
- Applied changes requested by Mimi Zohar (intermediate variable for if, annotation update, new option '--keyid-from-cert' instead of '@'). Added more comments for questioned cases.
- Read_keyid functions are reworked in hopefully more understandable way (two new static functions: extract_keyid and read_cert).
- Test for key+cert case added.

Changes from v5:
- ima_read_keyid renamed to imaevm_read_keyid and its return value inverted to match other API functions. Suggested by Stefan Berger
- Rebased over next-testing. Noticed by Mimi Zohar.

Changes from v4:
- ima_read_keyid() API changed. As suggested by Stefan Berger.

Changes from v3:
- ima_read_keyid() is improved to better support both use cases.

Changes from v2:
- Add ima_read_keyid() function to libemaevm and use it in both evmctl (for --keyid) and sign_hash_v2() (for concatenated PEMs). Suggested by Stefan Berger.
- Autodetect PEM by openssl reading it instead of magic string. Suggested by Stefan Berger.
- Trivial change: s/unsigned long int/unsigned long/ for keyid type.

Changes from v1:
- Extract keyid from cert associated to key file.
- Use sizeof instead of constant.

Changes since rfc version:
- `imaevm_params.keyid' now stored as native integer (instead of network order). Suggested by Stefan Berger.
- Added support for `--keyid=@filename'.

Vitaly Chikunov (3):
ima-evm-utils: Allow manual setting keyid for signing
ima-evm-utils: Allow manual setting keyid from a cert file
ima-evm-utils: Read keyid from the cert appended to the key file

 README                 |   6 +++
 src/evmctl.c           |  34 ++++++++++++
 src/imaevm.h           |   2 +
 src/libimaevm.c        | 141 +++++++++++++++++++++++++++++++++++++++++++++++--
 tests/gen-keys.sh      |  25 +++++++--
 tests/sign_verify.test |   3 ++
 6 files changed, 204 insertions(+), 7 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH v7 1/3 ima-evm-utils: Allow manual setting keyid for signing

Allow user to set signature's keyid using `--keyid' option. Keyid should correspond to SKID in certificate, when keyid is calculated using SHA-1 in libimaevm it may mismatch keyid extracted by the kernel from SKID of certificate (the way public key is presented to the kernel), thus making signatures not verifiable. This may happen when certificate is using non SHA-1 SKID (see rfc7093) or just 'unique number' (see rfc5280 4.2.1.2). As a last resort user may specify arbitrary keyid using the new option.

This commit creates ABI change for libimaevm, because of adding additional parameter to imaevm_params - newer libimaevm cannot work with older clients.

---
 README                 |  1 +
 src/evmctl.c           | 20 ++++++++++++++++++++
 src/imaevm.h           |  1 +
 src/libimaevm.c        |  8 +++++---
 tests/sign_verify.test |  1 +
 5 files changed, 28 insertions(+), 3 deletions(-)
README +1 -0
index 321045d..20fa009 100644
@@ -48,6 +48,7 @@ OPTIONS
       --xattr-user   store xattrs in user namespace (for testing purposes)
       --rsa          use RSA key type and signing scheme v1
   -k, --key          path to signing key (default: /etc/keys/{privkey,pubkey}_evm.pem)
+      --keyid n      overwrite signature keyid with a 32-bit value in hex (for signing)
   -o, --portable     generate portable EVM signatures
   -p, --pass         password for encrypted signing key
   -r, --recursive    recurse into directories (sign)
src/evmctl.c +20 -0
index 7a6f202..f272250 100644
@@ -2514,6 +2514,7 @@ static void usage(void)
 		"      --xattr-user   store xattrs in user namespace (for testing purposes)\n"
 		"      --rsa          use RSA key type and signing scheme v1\n"
 		"  -k, --key          path to signing key (default: /etc/keys/{privkey,pubkey}_evm.pem)\n"
+		"      --keyid n      overwrite signature keyid with a 32-bit value in hex (for signing)\n"
 		"  -o, --portable     generate portable EVM signatures\n"
 		"  -p, --pass         password for encrypted signing key\n"
 		"  -r, --recursive    recurse into directories (sign)\n"
@@ -2594,6 +2595,7 @@ static struct option opts[] = {
 	{"ignore-violations", 0, 0, 141},
 	{"pcrs", 1, 0, 142},
 	{"verify-bank", 2, 0, 143},
+	{"keyid", 1, 0, 144},
 	{}
 
 };
@@ -2638,6 +2640,8 @@ int main(int argc, char *argv[])
 {
 	int err = 0, c, lind;
 	ENGINE *eng = NULL;
+	unsigned long keyid;
+	char *eptr;
 
 #if !(OPENSSL_VERSION_NUMBER < 0x10100000)
 	OPENSSL_init_crypto(
@@ -2785,6 +2789,22 @@ int main(int argc, char *argv[])
 		case 143:
 			verify_bank = optarg;
 			break;
+		case 144:
+			errno = 0;
+			keyid = strtoul(optarg, &eptr, 16);
+			/*
+			 * ULONG_MAX is error from strtoul(3),
+			 * UINT_MAX is `imaevm_params.keyid' maximum value,
+			 * 0 is reserved for keyid being unset.
+			 */
+			if (errno || eptr - optarg != strlen(optarg) ||
+			    keyid == ULONG_MAX || keyid > UINT_MAX ||
+			    keyid == 0) {
+				log_err("Invalid keyid value.\n");
+				exit(1);
+			}
+			imaevm_params.keyid = keyid;
+			break;
 		case '?':
 			exit(1);
 			break;
src/imaevm.h +1 -0
index 4503919..fe244f1 100644
@@ -196,6 +196,7 @@ struct libimaevm_params {
 	const char *hash_algo;
 	const char *keyfile;
 	const char *keypass;
+	uint32_t keyid;		/* keyid overriding value, unless 0. (Host order.) */
 };
 
 struct RSA_ASN1_template {
src/libimaevm.c +5 -3
index 2856270..e8e0d0e 100644
@@ -45,6 +45,7 @@
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <asm/byteorder.h>
+#include <arpa/inet.h>
 #include <unistd.h>
 #include <dirent.h>
 #include <string.h>
@@ -891,7 +892,6 @@ static int sign_hash_v2(const char *algo, const unsigned char *hash,
 	const EVP_MD *md;
 	size_t sigsize;
 	const char *st;
-	uint32_t keyid;
 
 	if (!hash) {
 		log_err("sign_hash_v2: hash is null\n");
@@ -929,8 +929,10 @@ static int sign_hash_v2(const char *algo, const unsigned char *hash,
 		return -1;
 	}
 
-	calc_keyid_v2(&keyid, name, pkey);
-	hdr->keyid = keyid;
+	if (imaevm_params.keyid)
+		hdr->keyid = htonl(imaevm_params.keyid);
+	else
+		calc_keyid_v2(&hdr->keyid, name, pkey);
 
 	st = "EVP_PKEY_CTX_new";
 	if (!(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
tests/sign_verify.test +1 -0
index 3d7aa51..eccf5fa 100755
@@ -365,6 +365,7 @@ sign_verify  rsa1024  sha256  0x0301 --rsa
 sign_verify  rsa1024  md5     0x030201:K:0080
 sign_verify  rsa1024  sha1    0x030202:K:0080
 sign_verify  rsa1024  sha224  0x030207:K:0080
+expect_pass check_sign TYPE=ima KEY=rsa1024 ALG=sha256 PREFIX=0x030204aabbccdd0080 OPTS=--keyid=aabbccdd
 sign_verify  rsa1024  sha256  0x030204:K:0080
   try_different_keys
   try_different_sigs
Signature · 1 line
2.11.0
#2 lore raw

PATCH v7 2/3 ima-evm-utils: Allow manual setting keyid from a cert file

In reply to Vitaly Chikunov

Allow user to specify `--keyid-from-cert cert.pem' to extract keyid from SKID of the certificate file. PEM or DER format is auto-detected.

This commit creates ABI change for libimaevm, due to adding new function ima_read_keyid(). Newer clients cannot work with older libimaevm. Together with previous commit it creates backward-incompatible ABI change, thus soname should be incremented on release.

---
 README                 |   2 +
 src/evmctl.c           |  14 +++++++
 src/imaevm.h           |   1 +
 src/libimaevm.c        | 107 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/sign_verify.test |   1 +
 5 files changed, 125 insertions(+)
README +2 -0
index 20fa009..a130519 100644
@@ -49,6 +49,8 @@ OPTIONS
       --rsa          use RSA key type and signing scheme v1
   -k, --key          path to signing key (default: /etc/keys/{privkey,pubkey}_evm.pem)
       --keyid n      overwrite signature keyid with a 32-bit value in hex (for signing)
+      --keyid-from-cert file
+                     read keyid value from SKID of a x509 cert file
   -o, --portable     generate portable EVM signatures
   -p, --pass         password for encrypted signing key
   -r, --recursive    recurse into directories (sign)
src/evmctl.c +14 -0
index f272250..78be387 100644
@@ -42,6 +42,7 @@
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+#include <arpa/inet.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -57,12 +58,14 @@
 #include <termios.h>
 #include <assert.h>
 
+#include <openssl/asn1.h>
 #include <openssl/sha.h>
 #include <openssl/pem.h>
 #include <openssl/hmac.h>
 #include <openssl/err.h>
 #include <openssl/rsa.h>
 #include <openssl/engine.h>
+#include <openssl/x509v3.h>
 #include "hash_info.h"
 #include "pcr.h"
 #include "utils.h"
@@ -2515,6 +2518,8 @@ static void usage(void)
 		"      --rsa          use RSA key type and signing scheme v1\n"
 		"  -k, --key          path to signing key (default: /etc/keys/{privkey,pubkey}_evm.pem)\n"
 		"      --keyid n      overwrite signature keyid with a 32-bit value in hex (for signing)\n"
+		"      --keyid-from-cert file\n"
+		"                     read keyid value from SKID of a x509 cert file\n"
 		"  -o, --portable     generate portable EVM signatures\n"
 		"  -p, --pass         password for encrypted signing key\n"
 		"  -r, --recursive    recurse into directories (sign)\n"
@@ -2596,6 +2601,7 @@ static struct option opts[] = {
 	{"pcrs", 1, 0, 142},
 	{"verify-bank", 2, 0, 143},
 	{"keyid", 1, 0, 144},
+	{"keyid-from-cert", 1, 0, 145},
 	{}
 
 };
@@ -2805,6 +2811,14 @@ int main(int argc, char *argv[])
 			}
 			imaevm_params.keyid = keyid;
 			break;
+		case 145:
+			keyid = imaevm_read_keyid(optarg);
+			if (keyid == 0) {
+				log_err("Error reading keyid.\n");
+				exit(1);
+			}
+			imaevm_params.keyid = keyid;
+			break;
 		case '?':
 			exit(1);
 			break;
src/imaevm.h +1 -0
index fe244f1..491f136 100644
@@ -219,6 +219,7 @@ EVP_PKEY *read_pub_pkey(const char *keyfile, int x509);
 void calc_keyid_v1(uint8_t *keyid, char *str, const unsigned char *pkey, int len);
 void calc_keyid_v2(uint32_t *keyid, char *str, EVP_PKEY *pkey);
 int key2bin(RSA *key, unsigned char *pub);
+uint32_t imaevm_read_keyid(const char *certfile);
 
 int sign_hash(const char *algo, const unsigned char *hash, int size, const char *keyfile, const char *keypass, unsigned char *sig);
 int verify_hash(const char *file, const unsigned char *hash, int size, unsigned char *sig, int siglen);
src/libimaevm.c +107 -0

119 lines not on this page · raw message

tests/sign_verify.test +1 -0
index eccf5fa..1fdd786 100755
@@ -366,6 +366,7 @@ sign_verify  rsa1024  md5     0x030201:K:0080
 sign_verify  rsa1024  sha1    0x030202:K:0080
 sign_verify  rsa1024  sha224  0x030207:K:0080
 expect_pass check_sign TYPE=ima KEY=rsa1024 ALG=sha256 PREFIX=0x030204aabbccdd0080 OPTS=--keyid=aabbccdd
+expect_pass check_sign TYPE=ima KEY=rsa1024 ALG=sha256 PREFIX=0x030204:K:0080 OPTS=--keyid-from-cert=test-rsa1024.cer
 sign_verify  rsa1024  sha256  0x030204:K:0080
   try_different_keys
   try_different_sigs
Signature · 1 line
2.11.0
maintainer
#3 lore raw

Re: [PATCH v7 2/3] ima-evm-utils: Allow manual setting keyid from a cert file

Hi Vitaly,

src/evmctl.c int main(int argc, char *argv[]) · line 2816

Quoted 7 lines · Vitaly Chikunov
src/evmctl.c +3 -0
@@ -2805,6 +2811,14 @@ int main(int argc, char *argv[])
 			}
 			imaevm_params.keyid = keyid;
 			break;
+		case 145:
+			keyid = imaevm_read_keyid(optarg);
+			if (keyid == 0) {

The function comment indicates zero is returned on error, which is normally true, but -1 is returned to indicate reading the cert failed.

src/evmctl.c int main(int argc, char *argv[]) · line 2820

Quoted 4 lines
src/evmctl.c +4 -0
+				log_err("Error reading keyid.\n");
+				exit(1);
+			}
+			imaevm_params.keyid = keyid;

As a result, imaevm_params.keyid is set to -1, which the "--keyid=" case would detect, but isn't detected here.

src/imaevm.h

Quoted 10 lines
src/evmctl.c +1 -0
+			break;
 		case '?':
 			exit(1);
 			break;
src/imaevm.h +1 -0
index fe244f1..491f136 100644
 
+/**

kernel doc? :)

Quoted 8 lines

+ * imaevm_read_keyid() - Read 32-bit keyid from the cert file
+ * @certfile: File with certificate in PEM or DER form.
+ *
+ * Try to read keyid from Subject Key Identifier (SKID) of x509 certificate.
+ * Autodetect if cert is in PEM (tried first) or DER encoding.
+ *
+ * Return: 0 on error, logged error message;
+ * 32-bit keyid in host order.

As mentioned above, -1 could be returned.

src/imaevm.h

Quoted 11 lines

+ */

src/imaevm.h +10 -0
+uint32_t imaevm_read_keyid(const char *certfile)
+{
+	uint32_t keyid_be = 0;
+	X509 *x;
+
+	/* true: to load in DER form too. */
+	if (!(x = read_cert(certfile, true)))
+		return -1;
+	extract_keyid(&keyid_be, x, certfile);
+	/* On error keyid_be will not be set, returning 0. */

Ok

thanks,

Mimi

Quoted 7 lines
src/imaevm.h +4 -0
+	X509_free(x);
+	return ntohl(keyid_be);
+}
+
 static EVP_PKEY *read_priv_pkey(const char *keyfile, const char *keypass)
 {
 	FILE *fp;
#4 lore raw

Re: [PATCH v7 2/3] ima-evm-utils: Allow manual setting keyid from a cert file

Mimi,

Quoted 12 lines · Mimi Zohar

src/evmctl.c int main(int argc, char *argv[]) · line 2816

Quoted 7 lines · Vitaly Chikunov
src/evmctl.c +3 -0
@@ -2805,6 +2811,14 @@ int main(int argc, char *argv[])
 			}
 			imaevm_params.keyid = keyid;
 			break;
+		case 145:
+			keyid = imaevm_read_keyid(optarg);
+			if (keyid == 0) {

The function comment indicates zero is returned on error, which is normally true, but -1 is returned to indicate reading the cert failed.

Ah, bug.

Thanks!

Quoted 58 lines

src/evmctl.c int main(int argc, char *argv[]) · line 2820

Quoted 5 lines
src/evmctl.c +4 -0
+				log_err("Error reading keyid.\n");
+				exit(1);
+			}
+			imaevm_params.keyid = keyid;

As a result, imaevm_params.keyid is set to -1, which the "--keyid=" case would detect, but isn't detected here.

src/imaevm.h

Quoted 10 lines
src/evmctl.c +1 -0
+			break;
 		case '?':
 			exit(1);
 			break;
src/imaevm.h +1 -0
index fe244f1..491f136 100644
 
+/**

kernel doc? :)

Quoted 8 lines

+ * imaevm_read_keyid() - Read 32-bit keyid from the cert file
+ * @certfile: File with certificate in PEM or DER form.
+ *
+ * Try to read keyid from Subject Key Identifier (SKID) of x509 certificate.
+ * Autodetect if cert is in PEM (tried first) or DER encoding.
+ *
+ * Return: 0 on error, logged error message;
+ * 32-bit keyid in host order.

As mentioned above, -1 could be returned.

src/imaevm.h

Quoted 11 lines

+ */

src/imaevm.h +10 -0
+uint32_t imaevm_read_keyid(const char *certfile)
+{
+	uint32_t keyid_be = 0;
+	X509 *x;
+
+	/* true: to load in DER form too. */
+	if (!(x = read_cert(certfile, true)))
+		return -1;
+	extract_keyid(&keyid_be, x, certfile);
+	/* On error keyid_be will not be set, returning 0. */

Ok

thanks,

Mimi

Quoted 7 lines
src/imaevm.h +4 -0
+	X509_free(x);
+	return ntohl(keyid_be);
+}
+
 static EVP_PKEY *read_priv_pkey(const char *keyfile, const char *keypass)
 {
 	FILE *fp;
#5 lore raw

PATCH v7 3/3 ima-evm-utils: Read keyid from the cert appended to the key file

In reply to Vitaly Chikunov

Allow to have certificate appended to the private key of `--key' specified (PEM) file (for v2 signing) to facilitate reading of keyid from the associated cert. This will allow users to have private and public key as a single file and avoid the need of manually specifying keyid. There is no check that public key form the cert matches associated private key.

---
 README                 |  3 +++
 src/libimaevm.c        | 32 +++++++++++++++++++++++++++++---
 tests/gen-keys.sh      | 25 +++++++++++++++++++++----
 tests/sign_verify.test |  1 +
 4 files changed, 54 insertions(+), 7 deletions(-)
README +3 -0
index a130519..23e7d17 100644
@@ -128,6 +128,9 @@ for signing and importing the key.
 Second key format uses X509 DER encoded public key certificates and uses asymmetric key support
 in the kernel (since kernel 3.9). CONFIG_INTEGRITY_ASYMMETRIC_KEYS must be enabled (default).
 
+For v2 signatures x509 certificate (containing the public key) could be appended to the
+private key (they both are in PEM format) to automatically extract keyid from its Subject
+Key Identifier (SKID).
 
 Integrity keyrings
 ----------------
src/libimaevm.c +29 -3
index c8a320e..15f702b 100644
@@ -852,6 +852,28 @@ uint32_t imaevm_read_keyid(const char *certfile)
 	return ntohl(keyid_be);
 }
 
+/**
+ * read_keyid_from_key() - Read 32-bit keyid from the key file
+ * @keyid_be:   Pointer to 32-bit value in network order (BE, unaligned).
+ * @keyfile:    PEM file with private key with optionally appended x509 cert.
+ * Return:      0 on success and keyid_be is written;
+ *              -1 on error, logged error message, and keyid_be isn't written.
+ */
+static int read_keyid_from_key(uint32_t *keyid_be, const char *keyfile)
+{
+	X509 *x;
+
+	/* false: to try to read optional PEM cert. */
+	if (!(x = read_cert(keyfile, false)))
+		return -1;
+	if (extract_keyid(keyid_be, x, keyfile)) {
+		X509_free(x);
+		return -1;
+	}
+	X509_free(x);
+	return 0;
+}
+
 static EVP_PKEY *read_priv_pkey(const char *keyfile, const char *keypass)
 {
 	FILE *fp;
@@ -1036,10 +1058,14 @@ static int sign_hash_v2(const char *algo, const unsigned char *hash,
 		return -1;
 	}
 
-	if (imaevm_params.keyid)
+	if (imaevm_params.keyid) {
 		hdr->keyid = htonl(imaevm_params.keyid);
-	else
-		calc_keyid_v2(&hdr->keyid, name, pkey);
+	} else {
+		int keyid_read_failed = read_keyid_from_key(&hdr->keyid, keyfile);
+
+		if (keyid_read_failed)
+			calc_keyid_v2(&hdr->keyid, name, pkey);
+	}
 
 	st = "EVP_PKEY_CTX_new";
 	if (!(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
tests/gen-keys.sh +21 -4
index 46130cf..d604c96 100755
@@ -20,7 +20,7 @@ PATH=../src:$PATH
 type openssl
 
 log() {
-  echo - "$*"
+  echo >&2 - "$*"
   eval "$@"
 }
 
@@ -43,26 +43,43 @@ cat > test-ca.conf <<- EOF
 	basicConstraints=CA:TRUE
 	subjectKeyIdentifier=hash
 	authorityKeyIdentifier=keyid:always,issuer
+
+	[ skid ]
+	basicConstraints=CA:TRUE
+	subjectKeyIdentifier=12345678
+	authorityKeyIdentifier=keyid:always,issuer
 EOF
 fi
 
 # RSA
 # Second key will be used for wrong key tests.
-for m in 1024 2048; do
+for m in 1024 1024_skid 2048; do
   if [ "$1" = clean ] || [ "$1" = force ]; then
     rm -f test-rsa$m.cer test-rsa$m.key test-rsa$m.pub
   fi
   if [ "$1" = clean ]; then
     continue
   fi
+  if [ -z "${m%%*_*}" ]; then
+    # Add named extension.
+    bits=${m%_*}
+    ext="-extensions ${m#*_}"
+  else
+    bits=$m
+    ext=
+  fi
   if [ ! -e test-rsa$m.key ]; then
-    log openssl req -verbose -new -nodes -utf8 -sha1 -days 10000 -batch -x509 \
+    log openssl req -verbose -new -nodes -utf8 -sha1 -days 10000 -batch -x509 $ext \
       -config test-ca.conf \
-      -newkey rsa:$m \
+      -newkey rsa:$bits \
       -out test-rsa$m.cer -outform DER \
       -keyout test-rsa$m.key
     # for v1 signatures
     log openssl pkey -in test-rsa$m.key -out test-rsa$m.pub -pubout
+    if [ $m = 1024_skid ]; then
+      # Create combined key+cert.
+      log openssl x509 -inform DER -in test-rsa$m.cer >> test-rsa$m.key
+    fi
   fi
 done
 
tests/sign_verify.test +1 -0
index 1fdd786..df4304a 100755
@@ -367,6 +367,7 @@ sign_verify  rsa1024  sha1    0x030202:K:0080
 sign_verify  rsa1024  sha224  0x030207:K:0080
 expect_pass check_sign TYPE=ima KEY=rsa1024 ALG=sha256 PREFIX=0x030204aabbccdd0080 OPTS=--keyid=aabbccdd
 expect_pass check_sign TYPE=ima KEY=rsa1024 ALG=sha256 PREFIX=0x030204:K:0080 OPTS=--keyid-from-cert=test-rsa1024.cer
+expect_pass check_sign TYPE=ima KEY=rsa1024_skid ALG=sha256 PREFIX=0x030204123456780080
 sign_verify  rsa1024  sha256  0x030204:K:0080
   try_different_keys
   try_different_sigs
Signature · 1 line
2.11.0
maintainer
#6 lore raw

Re: [PATCH v7 3/3] ima-evm-utils: Read keyid from the cert appended to the key file

Hi Vitaly,

src/libimaevm.c uint32_t imaevm_read_keyid(const char *certfile) · line 862

Quoted 9 lines · Vitaly Chikunov
src/libimaevm.c +8 -0
+/**
+ * read_keyid_from_key() - Read 32-bit keyid from the key file
+ * @keyid_be:   Pointer to 32-bit value in network order (BE, unaligned).
+ * @keyfile:    PEM file with private key with optionally appended x509 cert.
+ * Return:      0 on success and keyid_be is written;
+ *              -1 on error, logged error message, and keyid_be isn't written.
+ */
+static int read_keyid_from_key(uint32_t *keyid_be, const char *keyfile)

(With the new option "--keyid-from-cert" is this patch really still needed?)

The function name is a bit off. Both imaevm_read_keyid() and this function are getting the keyid from a cert. There's also quite a bit of code duplication between them. Refactoring the code might help. For example, perhaps imaevm_read_keyid() could be a wrapper for read_keyid_from_cert().

Otherwise renaming this function to read_keyid_from_keyfile(), read_appended_keyid() or read_appended_keyid_from_cert(), which is really wordy, would be better.

thanks,

Mimi

#7 lore raw

Re: [PATCH v7 3/3] ima-evm-utils: Read keyid from the cert appended to the key file

Mimi,

Quoted 13 lines · Mimi Zohar

src/libimaevm.c uint32_t imaevm_read_keyid(const char *certfile) · line 862

Quoted 9 lines · Vitaly Chikunov
src/libimaevm.c +8 -0
+/**
+ * read_keyid_from_key() - Read 32-bit keyid from the key file
+ * @keyid_be:   Pointer to 32-bit value in network order (BE, unaligned).
+ * @keyfile:    PEM file with private key with optionally appended x509 cert.
+ * Return:      0 on success and keyid_be is written;
+ *              -1 on error, logged error message, and keyid_be isn't written.
+ */
+static int read_keyid_from_key(uint32_t *keyid_be, const char *keyfile)

(With the new option "--keyid-from-cert" is this patch really still needed?)

Yes. Key+cert is a nice option and should be handy for users. Key is stored together with the cert which will verify them. Otherwise key format doesn't store keyid which is error prone.

Quoted 5 lines

The function name is a bit off. Both imaevm_read_keyid() and this function are getting the keyid from a cert. There's also quite a bit of code duplication between them. Refactoring the code might help. For example, perhaps imaevm_read_keyid() could be a wrapper for read_keyid_from_cert().

They have important difference too. Thus, its hard to refactor them into nested function that looked good and simple. This is third attempt. And it's like solving a unsolvable puzzle.

imaevm_read_keyid() reads from cert-only file where cert could be PEM or DER encoded. It's failure if there is no cert, because user intended to read a cert.

imaevm_read_keyid() reads from private key optionally combined with a cert (both are PEM-only). It's not failure if there is no cert.

We want to save "duplicated" call to PEM_read_X509() but really it causes more convoluted internal logic.

I tried to de-duplicate them as much as possible while remaining understandability.

Quoted 3 lines

Otherwise renaming this function to read_keyid_from_keyfile(), read_appended_keyid() or read_appended_keyid_from_cert(), which is really wordy, would be better.

Ok. read_keyid_from_keyfile looks good.

Quoted 4 lines

thanks,

Mimi

maintainer
#8 lore raw
In reply to Vitaly Chikunov

Hi Vitaly,

Quoted 11 lines · Vitaly Chikunov

Allow user to set signature's keyid using `--keyid' option. Keyid should correspond to SKID in certificate. When keyid is calculated using SHA-1 in libimaevm it may mismatch keyid extracted by the kernel from SKID of certificate (the way public key is presented to the kernel), thus making signatures not verifiable. This may happen when certificate is using non SHA-1 SKID (see rfc7093) or just 'unique number' (see rfc5280 4.2.1.2). As a last resort user may specify arbitrary keyid using the new option. Certificate filename could be used instead of the hex number with `--keyid-from-cert' option. And, third option is to read keyid from the cert appended to the key file.

Quoted 2 lines

These commits create backward incompatible ABI change for libimaevm, thus soname should be incremented on release.

I haven't started using Github actions. Here are some new Travis complaints:

Alpine:
libimaevm.c: In function 'sign_hash_v2':
libimaevm.c:996:47: warning: taking address of packed member of 'struct signature_v2_hdr' may result in an unaligned pointer value [-Waddress- of-packed-member]
996 | int keyid_read_failed = read_keyid_from_key(&hdr->keyid,
keyfile);
| ^~~~~~~~~~~
libimaevm.c:999:18: warning: taking address of packed member of 'struct signature_v2_hdr' may result in an unaligned pointer value [-Waddress- of-packed-member]

  999 |    calc_keyid_v2(&hdr->keyid, name, pkey);
      |                  ^~~~~~~~~~~

centos:
./.libs/libimaevm.so: undefined reference to X509_get0_subject_key_id' ./.libs/libimaevm.so: undefined reference to ASN1_STRING_get0_data'

xenial:
libimaevm.c: In function 'extract_keyid':
libimaevm.c:695:2: warning: implicit declaration of function 'X509_get0_subject_key_id' [-Wimplicit-function-declaration]
if (!(skid = X509_get0_subject_key_id(x))) {
^
libimaevm.c:695:13: warning: assignment makes pointer from integer without a cast [enabled by default]
if (!(skid = X509_get0_subject_key_id(x))) {

thanks,

Mimi

#9 lore raw

Mimi,

Quoted 12 lines · Mimi Zohar
Quoted 11 lines · Vitaly Chikunov

Allow user to set signature's keyid using `--keyid' option. Keyid should correspond to SKID in certificate. When keyid is calculated using SHA-1 in libimaevm it may mismatch keyid extracted by the kernel from SKID of certificate (the way public key is presented to the kernel), thus making signatures not verifiable. This may happen when certificate is using non SHA-1 SKID (see rfc7093) or just 'unique number' (see rfc5280 4.2.1.2). As a last resort user may specify arbitrary keyid using the new option. Certificate filename could be used instead of the hex number with `--keyid-from-cert' option. And, third option is to read keyid from the cert appended to the key file.

Quoted 4 lines
Quoted 2 lines

These commits create backward incompatible ABI change for libimaevm, thus soname should be incremented on release.

I haven't started using Github actions.

Will you create repo there?

Quoted 20 lines

Here are some new Travis complaints:

Alpine:
libimaevm.c: In function 'sign_hash_v2':
libimaevm.c:996:47: warning: taking address of packed member of 'struct signature_v2_hdr' may result in an unaligned pointer value [-Waddress- of-packed-member]
996 | int keyid_read_failed = read_keyid_from_key(&hdr->keyid,
keyfile);
| ^~~~~~~~~~~
libimaevm.c:999:18: warning: taking address of packed member of 'struct signature_v2_hdr' may result in an unaligned pointer value [-Waddress- of-packed-member]

  999 |    calc_keyid_v2(&hdr->keyid, name, pkey);
      |                  ^~~~~~~~~~~

centos:
./.libs/libimaevm.so: undefined reference to X509_get0_subject_key_id' ./.libs/libimaevm.so: undefined reference to ASN1_STRING_get0_data'

Quoted 9 lines

xenial:
libimaevm.c: In function 'extract_keyid':
libimaevm.c:695:2: warning: implicit declaration of function 'X509_get0_subject_key_id' [-Wimplicit-function-declaration]
if (!(skid = X509_get0_subject_key_id(x))) {
^
libimaevm.c:695:13: warning: assignment makes pointer from integer without a cast [enabled by default]
if (!(skid = X509_get0_subject_key_id(x))) {

Thanks!

Quoted 4 lines

thanks,

Mimi

maintainer
#10 lore raw
Quoted 4 lines · Vitaly Chikunov
Quoted 2 lines

I haven't started using Github actions.

Will you create repo there?

Done.

thanks,

Mimi

#11 lore raw
Quoted 7 lines · Mimi Zohar
Quoted 4 lines · Vitaly Chikunov
Quoted 2 lines

I haven't started using Github actions.

Will you create repo there?

Done.

Thanks you. Forked it already. Good there is already GA tests.

Vitaly,

Quoted 4 lines

thanks,

Mimi