linux-integrity.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v7 0/3] ima-evm-utils: Add --keyid option
@ 2021-07-01  1:13 Vitaly Chikunov
  2021-07-01  1:13 ` [PATCH v7 1/3] ima-evm-utils: Allow manual setting keyid for signing Vitaly Chikunov
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Vitaly Chikunov @ 2021-07-01  1:13 UTC (permalink / raw)
  To: Mimi Zohar, Dmitry Kasatkin, linux-integrity

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(-)

-- 
2.11.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v7 1/3] ima-evm-utils: Allow manual setting keyid for signing
  2021-07-01  1:13 [PATCH v7 0/3] ima-evm-utils: Add --keyid option Vitaly Chikunov
@ 2021-07-01  1:13 ` Vitaly Chikunov
  2021-07-01  1:13 ` [PATCH v7 2/3] ima-evm-utils: Allow manual setting keyid from a cert file Vitaly Chikunov
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 12+ messages in thread
From: Vitaly Chikunov @ 2021-07-01  1:13 UTC (permalink / raw)
  To: Mimi Zohar, Dmitry Kasatkin, linux-integrity
  Cc: Elvira Khabirova, Stefan Berger

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.

Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
Reported-by: Elvira Khabirova <lineprinter0@gmail.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
---
 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(-)

diff --git a/README b/README
index 321045d..20fa009 100644
--- a/README
+++ b/README
@@ -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)
diff --git a/src/evmctl.c b/src/evmctl.c
index 7a6f202..f272250 100644
--- a/src/evmctl.c
+++ b/src/evmctl.c
@@ -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;
diff --git a/src/imaevm.h b/src/imaevm.h
index 4503919..fe244f1 100644
--- a/src/imaevm.h
+++ b/src/imaevm.h
@@ -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 {
diff --git a/src/libimaevm.c b/src/libimaevm.c
index 2856270..e8e0d0e 100644
--- a/src/libimaevm.c
+++ b/src/libimaevm.c
@@ -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)))
diff --git a/tests/sign_verify.test b/tests/sign_verify.test
index 3d7aa51..eccf5fa 100755
--- a/tests/sign_verify.test
+++ b/tests/sign_verify.test
@@ -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
-- 
2.11.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v7 2/3] ima-evm-utils: Allow manual setting keyid from a cert file
  2021-07-01  1:13 [PATCH v7 0/3] ima-evm-utils: Add --keyid option Vitaly Chikunov
  2021-07-01  1:13 ` [PATCH v7 1/3] ima-evm-utils: Allow manual setting keyid for signing Vitaly Chikunov
@ 2021-07-01  1:13 ` Vitaly Chikunov
  2021-07-05 19:59   ` Mimi Zohar
  2021-07-01  1:13 ` [PATCH v7 3/3] ima-evm-utils: Read keyid from the cert appended to the key file Vitaly Chikunov
  2021-07-06 16:23 ` [PATCH v7 0/3] ima-evm-utils: Add --keyid option Mimi Zohar
  3 siblings, 1 reply; 12+ messages in thread
From: Vitaly Chikunov @ 2021-07-01  1:13 UTC (permalink / raw)
  To: Mimi Zohar, Dmitry Kasatkin, linux-integrity

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.

Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
---
 README                 |   2 +
 src/evmctl.c           |  14 +++++++
 src/imaevm.h           |   1 +
 src/libimaevm.c        | 107 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/sign_verify.test |   1 +
 5 files changed, 125 insertions(+)

diff --git a/README b/README
index 20fa009..a130519 100644
--- a/README
+++ b/README
@@ -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)
diff --git a/src/evmctl.c b/src/evmctl.c
index f272250..78be387 100644
--- a/src/evmctl.c
+++ b/src/evmctl.c
@@ -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;
diff --git a/src/imaevm.h b/src/imaevm.h
index fe244f1..491f136 100644
--- a/src/imaevm.h
+++ b/src/imaevm.h
@@ -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);
diff --git a/src/libimaevm.c b/src/libimaevm.c
index e8e0d0e..c8a320e 100644
--- a/src/libimaevm.c
+++ b/src/libimaevm.c
@@ -57,6 +57,7 @@
 #include <openssl/pem.h>
 #include <openssl/evp.h>
 #include <openssl/x509.h>
+#include <openssl/x509v3.h>
 #include <openssl/err.h>
 
 #include "imaevm.h"
@@ -745,6 +746,112 @@ void calc_keyid_v2(uint32_t *keyid, char *str, EVP_PKEY *pkey)
 	X509_PUBKEY_free(pk);
 }
 
+/**
+ * extract_keyid - extract keyid from x509 SKID
+ * @keyid_be:	output 32-bit keyid in network order (BE);
+ * @x:		input x509 cert;
+ * @infile:	input filename for logging;
+ * Return:	0 on success, keyid_be is written;
+ *		-1 on error, keyid_be is not written, error logged.
+ */
+static int extract_keyid(uint32_t *keyid_be, X509 *x, const char *infile)
+{
+	uint32_t keyid_tmp; /* BE */
+	const ASN1_OCTET_STRING *skid;
+	int skid_len;
+
+	if (!(skid = X509_get0_subject_key_id(x))) {
+		log_err("read keyid: %s: SKID not found\n", infile);
+		goto err_free;
+	}
+	skid_len = ASN1_STRING_length(skid);
+	if (skid_len < sizeof(keyid_tmp)) {
+		log_err("read keyid: %s: SKID too short (len %d)\n", infile,
+			skid_len);
+		goto err_free;
+	}
+	memcpy(&keyid_tmp, ASN1_STRING_get0_data(skid) + skid_len
+	       - sizeof(keyid_tmp), sizeof(keyid_tmp));
+	log_info("keyid %04x (from %s)\n", ntohl(keyid_tmp), infile);
+	memcpy(keyid_be, &keyid_tmp, sizeof(*keyid_be));
+	return 0;
+
+err_free:
+	X509_free(x);
+	return -1;
+}
+
+/**
+ * read_cert() - Read x509 certificate
+ * @certfile:	Input filename.
+ * @try_der:	true:  try to read in DER from if there is no PEM,
+ *		       cert is considered mandatory and error will be issued
+ *		       if there is no cert;
+ *		false: only try to read in PEM form, cert is considered
+ *		       optional.
+ * Return:	X509 cert or NULL.
+ */
+static X509 *read_cert(const char *certfile, int try_der)
+{
+	X509 *x = NULL;
+	FILE *fp;
+
+	if (!(fp = fopen(certfile, "r"))) {
+		log_err("Cannot open %s: %s\n", certfile, strerror(errno));
+		return NULL;
+	}
+	if (!PEM_read_X509(fp, &x, NULL, NULL)) {
+		if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) {
+			ERR_clear_error();
+			if (try_der) {
+				rewind(fp);
+				d2i_X509_fp(fp, &x);
+			} else {
+				/*
+				 * Cert is optional and there is just no PEM
+				 * header, then issue debug message and stop
+				 * trying.
+				 */
+				log_debug("%s: x509 certificate not found\n",
+					  certfile);
+				fclose(fp);
+				return NULL;
+			}
+		}
+	}
+	fclose(fp);
+	if (!x) {
+		ERR_print_errors_fp(stderr);
+		log_err("read keyid: %s: Error reading x509 certificate\n",
+			certfile);
+	}
+	return x;
+}
+
+/**
+ * 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.
+ */
+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. */
+	X509_free(x);
+	return ntohl(keyid_be);
+}
+
 static EVP_PKEY *read_priv_pkey(const char *keyfile, const char *keypass)
 {
 	FILE *fp;
diff --git a/tests/sign_verify.test b/tests/sign_verify.test
index eccf5fa..1fdd786 100755
--- a/tests/sign_verify.test
+++ b/tests/sign_verify.test
@@ -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
-- 
2.11.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v7 3/3] ima-evm-utils: Read keyid from the cert appended to the key file
  2021-07-01  1:13 [PATCH v7 0/3] ima-evm-utils: Add --keyid option Vitaly Chikunov
  2021-07-01  1:13 ` [PATCH v7 1/3] ima-evm-utils: Allow manual setting keyid for signing Vitaly Chikunov
  2021-07-01  1:13 ` [PATCH v7 2/3] ima-evm-utils: Allow manual setting keyid from a cert file Vitaly Chikunov
@ 2021-07-01  1:13 ` Vitaly Chikunov
  2021-07-05 20:04   ` Mimi Zohar
  2021-07-06 16:23 ` [PATCH v7 0/3] ima-evm-utils: Add --keyid option Mimi Zohar
  3 siblings, 1 reply; 12+ messages in thread
From: Vitaly Chikunov @ 2021-07-01  1:13 UTC (permalink / raw)
  To: Mimi Zohar, Dmitry Kasatkin, linux-integrity

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.

Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
---
 README                 |  3 +++
 src/libimaevm.c        | 32 +++++++++++++++++++++++++++++---
 tests/gen-keys.sh      | 25 +++++++++++++++++++++----
 tests/sign_verify.test |  1 +
 4 files changed, 54 insertions(+), 7 deletions(-)

diff --git a/README b/README
index a130519..23e7d17 100644
--- a/README
+++ b/README
@@ -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
 ----------------
diff --git a/src/libimaevm.c b/src/libimaevm.c
index c8a320e..15f702b 100644
--- a/src/libimaevm.c
+++ b/src/libimaevm.c
@@ -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)))
diff --git a/tests/gen-keys.sh b/tests/gen-keys.sh
index 46130cf..d604c96 100755
--- a/tests/gen-keys.sh
+++ b/tests/gen-keys.sh
@@ -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
 
diff --git a/tests/sign_verify.test b/tests/sign_verify.test
index 1fdd786..df4304a 100755
--- a/tests/sign_verify.test
+++ b/tests/sign_verify.test
@@ -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
-- 
2.11.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v7 2/3] ima-evm-utils: Allow manual setting keyid from a cert file
  2021-07-01  1:13 ` [PATCH v7 2/3] ima-evm-utils: Allow manual setting keyid from a cert file Vitaly Chikunov
@ 2021-07-05 19:59   ` Mimi Zohar
  2021-07-05 20:50     ` Vitaly Chikunov
  0 siblings, 1 reply; 12+ messages in thread
From: Mimi Zohar @ 2021-07-05 19:59 UTC (permalink / raw)
  To: Vitaly Chikunov, Mimi Zohar, Dmitry Kasatkin, linux-integrity

Hi Vitaly,

On Thu, 2021-07-01 at 04:13 +0300, Vitaly Chikunov wrote:

> @@ -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. 

> +				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.

> +			break;
>  		case '?':
>  			exit(1);
>  			break;
> diff --git a/src/imaevm.h b/src/imaevm.h
> index fe244f1..491f136 100644
> --- a/src/imaevm.h
> +++ b/src/imaevm.h
> 
> +/**

kernel doc?  :)

> + * 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.

> + */
> +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

> +	X509_free(x);
> +	return ntohl(keyid_be);
> +}
> +
>  static EVP_PKEY *read_priv_pkey(const char *keyfile, const char *keypass)
>  {
>  	FILE *fp;


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v7 3/3] ima-evm-utils: Read keyid from the cert appended to the key file
  2021-07-01  1:13 ` [PATCH v7 3/3] ima-evm-utils: Read keyid from the cert appended to the key file Vitaly Chikunov
@ 2021-07-05 20:04   ` Mimi Zohar
  2021-07-05 21:15     ` Vitaly Chikunov
  0 siblings, 1 reply; 12+ messages in thread
From: Mimi Zohar @ 2021-07-05 20:04 UTC (permalink / raw)
  To: Vitaly Chikunov, Mimi Zohar, Dmitry Kasatkin, linux-integrity

Hi Vitaly,

On Thu, 2021-07-01 at 04:13 +0300, Vitaly Chikunov wrote:
> 
> +/**
> + * 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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v7 2/3] ima-evm-utils: Allow manual setting keyid from a cert file
  2021-07-05 19:59   ` Mimi Zohar
@ 2021-07-05 20:50     ` Vitaly Chikunov
  0 siblings, 0 replies; 12+ messages in thread
From: Vitaly Chikunov @ 2021-07-05 20:50 UTC (permalink / raw)
  To: Mimi Zohar; +Cc: Mimi Zohar, Dmitry Kasatkin, linux-integrity

Mimi,

On Mon, Jul 05, 2021 at 03:59:02PM -0400, Mimi Zohar wrote:
> On Thu, 2021-07-01 at 04:13 +0300, Vitaly Chikunov wrote:
> 
> > @@ -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!

> 
> > +				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.
> 
> > +			break;
> >  		case '?':
> >  			exit(1);
> >  			break;
> > diff --git a/src/imaevm.h b/src/imaevm.h
> > index fe244f1..491f136 100644
> > --- a/src/imaevm.h
> > +++ b/src/imaevm.h
> > 
> > +/**
> 
> kernel doc?  :)
> 
> > + * 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.
> 
> > + */
> > +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
> 
> > +	X509_free(x);
> > +	return ntohl(keyid_be);
> > +}
> > +
> >  static EVP_PKEY *read_priv_pkey(const char *keyfile, const char *keypass)
> >  {
> >  	FILE *fp;

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v7 3/3] ima-evm-utils: Read keyid from the cert appended to the key file
  2021-07-05 20:04   ` Mimi Zohar
@ 2021-07-05 21:15     ` Vitaly Chikunov
  0 siblings, 0 replies; 12+ messages in thread
From: Vitaly Chikunov @ 2021-07-05 21:15 UTC (permalink / raw)
  To: Mimi Zohar; +Cc: Mimi Zohar, Dmitry Kasatkin, linux-integrity

Mimi,

On Mon, Jul 05, 2021 at 04:04:52PM -0400, Mimi Zohar wrote:
> On Thu, 2021-07-01 at 04:13 +0300, Vitaly Chikunov wrote:
> > 
> > +/**
> > + * 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.

> 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.

> 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.

> 
> thanks,
> 
> Mimi

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v7 0/3] ima-evm-utils: Add --keyid option
  2021-07-01  1:13 [PATCH v7 0/3] ima-evm-utils: Add --keyid option Vitaly Chikunov
                   ` (2 preceding siblings ...)
  2021-07-01  1:13 ` [PATCH v7 3/3] ima-evm-utils: Read keyid from the cert appended to the key file Vitaly Chikunov
@ 2021-07-06 16:23 ` Mimi Zohar
  2021-07-06 21:13   ` Vitaly Chikunov
  3 siblings, 1 reply; 12+ messages in thread
From: Mimi Zohar @ 2021-07-06 16:23 UTC (permalink / raw)
  To: Vitaly Chikunov, Mimi Zohar, Dmitry Kasatkin, linux-integrity

Hi Vitaly,

On Thu, 2021-07-01 at 04:13 +0300, Vitaly Chikunov wrote:
> 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.

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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v7 0/3] ima-evm-utils: Add --keyid option
  2021-07-06 16:23 ` [PATCH v7 0/3] ima-evm-utils: Add --keyid option Mimi Zohar
@ 2021-07-06 21:13   ` Vitaly Chikunov
  2021-07-08 15:37     ` Mimi Zohar
  0 siblings, 1 reply; 12+ messages in thread
From: Vitaly Chikunov @ 2021-07-06 21:13 UTC (permalink / raw)
  To: Mimi Zohar; +Cc: Dmitry Kasatkin, linux-integrity

Mimi,

On Tue, Jul 06, 2021 at 12:23:26PM -0400, Mimi Zohar wrote:
> On Thu, 2021-07-01 at 04:13 +0300, Vitaly Chikunov wrote:
> > 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.
> 
> I haven't started using Github actions.

Will you create repo there?

> 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!

> 
> thanks,
> 
> Mimi

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v7 0/3] ima-evm-utils: Add --keyid option
  2021-07-06 21:13   ` Vitaly Chikunov
@ 2021-07-08 15:37     ` Mimi Zohar
  2021-07-08 15:46       ` Vitaly Chikunov
  0 siblings, 1 reply; 12+ messages in thread
From: Mimi Zohar @ 2021-07-08 15:37 UTC (permalink / raw)
  To: Vitaly Chikunov; +Cc: Dmitry Kasatkin, linux-integrity

On Wed, 2021-07-07 at 00:13 +0300, Vitaly Chikunov wrote:
> 
> > I haven't started using Github actions.
> 
> Will you create repo there?

Done.

thanks,

Mimi


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v7 0/3] ima-evm-utils: Add --keyid option
  2021-07-08 15:37     ` Mimi Zohar
@ 2021-07-08 15:46       ` Vitaly Chikunov
  0 siblings, 0 replies; 12+ messages in thread
From: Vitaly Chikunov @ 2021-07-08 15:46 UTC (permalink / raw)
  To: Mimi Zohar; +Cc: Dmitry Kasatkin, linux-integrity

On Thu, Jul 08, 2021 at 11:37:31AM -0400, Mimi Zohar wrote:
> On Wed, 2021-07-07 at 00:13 +0300, Vitaly Chikunov wrote:
> > 
> > > 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,

> 
> thanks,
> 
> Mimi

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2021-07-08 15:46 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-01  1:13 [PATCH v7 0/3] ima-evm-utils: Add --keyid option Vitaly Chikunov
2021-07-01  1:13 ` [PATCH v7 1/3] ima-evm-utils: Allow manual setting keyid for signing Vitaly Chikunov
2021-07-01  1:13 ` [PATCH v7 2/3] ima-evm-utils: Allow manual setting keyid from a cert file Vitaly Chikunov
2021-07-05 19:59   ` Mimi Zohar
2021-07-05 20:50     ` Vitaly Chikunov
2021-07-01  1:13 ` [PATCH v7 3/3] ima-evm-utils: Read keyid from the cert appended to the key file Vitaly Chikunov
2021-07-05 20:04   ` Mimi Zohar
2021-07-05 21:15     ` Vitaly Chikunov
2021-07-06 16:23 ` [PATCH v7 0/3] ima-evm-utils: Add --keyid option Mimi Zohar
2021-07-06 21:13   ` Vitaly Chikunov
2021-07-08 15:37     ` Mimi Zohar
2021-07-08 15:46       ` Vitaly Chikunov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).