Skip to contentSkip to conversation

ima-evm-utils: Add support for signing with pkcs11 URIs

PATCH Superseded Stefan Berger 3 participants 7 messages Aug 9, 2021 Keys-trusted

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover ima-evm-utils: Add support for signing with pkcs11 URIs

From: Stefan Berger <[email protected]>

This series of patches adds support for signing with pkcs11 URIs and the keyid explicityly provided via a command line option.
A test program is provided setting up softhsm for test cases to use.

Stefan

Stefan Berger (4):
evmctl: Implement support for EVMCTL_KEY_PASSWORD environment variable
libimaevm: Add support for pkcs11 private keys for signing a v2 hash
tests: Extend sign_verify test with pkcs11 specific test
tests: Get the packages for pkcs11 testing on the CI/CD system

 README                 |   5 +
 ci/alt.sh              |   3 +
 ci/fedora.sh           |   8 ++
 ci/tumbleweed.sh       |   3 +
 src/evmctl.c           |   7 +
 src/libimaevm.c        |  59 +++++++--
 tests/functions.sh     |  26 ++++
 tests/sign_verify.test |  50 +++++--
 tests/softhsm_setup    | 290 +++++++++++++++++++++++++++++++++++++++++
 9 files changed, 426 insertions(+), 25 deletions(-)
 create mode 100755 tests/softhsm_setup
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/4 evmctl: Implement support for EVMCTL_KEY_PASSWORD environment variable

From: Stefan Berger <[email protected]>

If the user did not use the --pass option to provide a key password, get the key password from the EVMCTL_KEY_PASSWORD environment variable.

---
 README       | 4 ++++
 src/evmctl.c | 6 ++++++
 2 files changed, 10 insertions(+)
README +4 -0
index 87cd3b5..1cc027f 100644
@@ -70,6 +70,10 @@ OPTIONS
   -v                 increase verbosity level
   -h, --help         display this help and exit
 
+Environment variables:
+
+EVMCTL_KEY_PASSWORD  : Private key password to use; do not use --pass option
+
 
 INTRODUCTION
 ------------
src/evmctl.c +6 -0
index a8065bb..58f8e66 100644
@@ -2530,6 +2530,9 @@ static void usage(void)
 		"      --ignore-violations ignore ToMToU measurement violations\n"
 		"  -v                 increase verbosity level\n"
 		"  -h, --help         display this help and exit\n"
+		"\n"
+		"Environment variables:\n\n"
+		"EVMCTL_KEY_PASSWORD  : Private key password to use; do not use --pass option\n"
 		"\n");
 }
 
@@ -2813,6 +2816,9 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	if (!imaevm_params.keypass)
+		imaevm_params.keypass = getenv("EVMCTL_KEY_PASSWORD");
+
 	if (argv[optind] == NULL)
 		usage();
 	else
Signature · 1 line
2.31.1
#2 lore raw

PATCH 2/4 libimaevm: Add support for pkcs11 private keys for signing a v2 hash

In reply to Stefan Berger

From: Stefan Berger <[email protected]>

Add support for pkcs11 private keys for signing a v2 hash.

---
 README          |  1 +
 src/evmctl.c    |  1 +
 src/libimaevm.c | 59 ++++++++++++++++++++++++++++++++++++++++---------
 3 files changed, 50 insertions(+), 11 deletions(-)
README +1 -0
index 1cc027f..2bb363c 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)
+                     or a pkcs11 URI
       --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
src/evmctl.c +1 -0
index 58f8e66..2e85f8b 100644
@@ -2503,6 +2503,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"
+		"                     or a pkcs11 URI\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"
src/libimaevm.c +48 -11

75 lines not on this page · raw message

Signature · 1 line
2.31.1
maintainer
#3 lore raw

Re: [PATCH 2/4] libimaevm: Add support for pkcs11 private keys for signing a v2 hash

Quoted 117 lines · Stefan Berger

From: Stefan Berger <[email protected]>

Add support for pkcs11 private keys for signing a v2 hash.

---
 README          |  1 +
 src/evmctl.c    |  1 +
 src/libimaevm.c | 59 ++++++++++++++++++++++++++++++++++++++++-------
--
 3 files changed, 50 insertions(+), 11 deletions(-)
README +1 -0
index 1cc027f..2bb363c 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)
+                     or a pkcs11 URI
       --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

src/evmctl.c +0 -0
index 58f8e66..2e85f8b 100644
@@ -2503,6 +2503,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"

src/evmctl.c +1 -0
+		"                     or a pkcs11 URI\n"
 		"      --keyid n      overwrite signature keyid with a

32-bit value in hex (for signing)\n"

src/evmctl.c +0 -0
 		"      --keyid-from-cert file\n"
 		"                     read keyid value from SKID of a

x509 cert file\n"

src/libimaevm.c +36 -10
index 8e96157..b84e5b8 100644
@@ -60,6 +60,7 @@
 #include <openssl/x509.h>
 #include <openssl/x509v3.h>
 #include <openssl/err.h>
+#include <openssl/engine.h>
 
 #include "imaevm.h"
 #include "hash_info.h"
@@ -803,21 +804,57 @@ static EVP_PKEY *read_priv_pkey(const char
 *keyfile, const char *keypass)
 {
 	FILE *fp;
 	EVP_PKEY *pkey;
+	ENGINE *e;
 
-	fp = fopen(keyfile, "r");
-	if (!fp) {
-		log_err("Failed to open keyfile: %s\n", keyfile);
-		return NULL;
-	}
-	pkey = PEM_read_PrivateKey(fp, NULL, NULL, (void *)keypass);
-	if (!pkey) {
-		log_err("Failed to PEM_read_PrivateKey key file: %s\n",
-			keyfile);
-		output_openssl_errors();
+	if (!strncmp(keyfile, "pkcs11:", 7)) {
+		if (!imaevm_params.keyid) {
+			log_err("When using a pkcs11 URI you must
 provide the keyid with an option\n");
+			return NULL;
+		}
+
+		ENGINE_load_builtin_engines();
+		e = ENGINE_by_id("pkcs11");
+		if (!e) {
+			log_err("Failed to load pkcs11 engine\n");
+			goto err_pkcs11;
+		}
+		if (!ENGINE_init(e)) {
+			log_err("Failed to initialize the pkcs11
 engine\n");
+			goto err_pkcs11;
+		}
+		if (keypass) {
+			if (!ENGINE_ctrl_cmd_string(e, "PIN", keypass,
 0)) {
+				log_err("Failed to set the PIN for the
 private key\n");
+				goto err_pkcs11;
+			}
+		}
+		pkey = ENGINE_load_private_key(e, keyfile, NULL, NULL);
+		if (!pkey) {
+			log_err("Failed to load private key %s\n",
 keyfile);
+			goto err_pkcs11;
+		}
+	} else {
+		fp = fopen(keyfile, "r");
+		if (!fp) {
+			log_err("Failed to open keyfile: %s\n",
 keyfile);
+			return NULL;
+		}
+		pkey = PEM_read_PrivateKey(fp, NULL, NULL, (void

*)keypass);

src/libimaevm.c +2 -0
+		if (!pkey) {
+			log_err("Failed to PEM_read_PrivateKey key

file: %s\n",

src/libimaevm.c static EVP_PKEY *read_priv_pkey(const char *keyfile, const char *keypass) · line 849

Quoted 5 lines
src/libimaevm.c +5 -0
+				keyfile);
+			output_openssl_errors();
+		}
+
+		fclose(fp);

This looks a bit narrow. Isn't the problem that *any* engine key might be a specifier not a file? In which case the generic fix is not to validate file existence if an engine is present on the command line. That way you can specify any engine key and the pkcs11 key simply becomes a special case of this.

If you insist on not having to specify --engine for pkcs11 keys then you do the prefix check earlier on and set the engine to pkcs11 (if it's not already set to something else).

James

#4 lore raw

Re: [PATCH 2/4] libimaevm: Add support for pkcs11 private keys for signing a v2 hash

Quoted 129 lines · James Bottomley
Quoted 117 lines · Stefan Berger

From: Stefan Berger <[email protected]>

Add support for pkcs11 private keys for signing a v2 hash.

---
  README          |  1 +
  src/evmctl.c    |  1 +
  src/libimaevm.c | 59 ++++++++++++++++++++++++++++++++++++++++-------
--
  3 files changed, 50 insertions(+), 11 deletions(-)
README +1 -0
index 1cc027f..2bb363c 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)
+                     or a pkcs11 URI
        --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

src/evmctl.c +0 -0
index 58f8e66..2e85f8b 100644
@@ -2503,6 +2503,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"

src/evmctl.c +1 -0
+		"                     or a pkcs11 URI\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"

src/libimaevm.c +36 -10
index 8e96157..b84e5b8 100644
@@ -60,6 +60,7 @@
  #include <openssl/x509.h>
  #include <openssl/x509v3.h>
  #include <openssl/err.h>
+#include <openssl/engine.h>
 
  #include "imaevm.h"
  #include "hash_info.h"
@@ -803,21 +804,57 @@ static EVP_PKEY *read_priv_pkey(const char
 *keyfile, const char *keypass)
  {
  	FILE *fp;
  	EVP_PKEY *pkey;
+	ENGINE *e;
 
-	fp = fopen(keyfile, "r");
-	if (!fp) {
-		log_err("Failed to open keyfile: %s\n", keyfile);
-		return NULL;
-	}
-	pkey = PEM_read_PrivateKey(fp, NULL, NULL, (void *)keypass);
-	if (!pkey) {
-		log_err("Failed to PEM_read_PrivateKey key file: %s\n",
-			keyfile);
-		output_openssl_errors();
+	if (!strncmp(keyfile, "pkcs11:", 7)) {
+		if (!imaevm_params.keyid) {
+			log_err("When using a pkcs11 URI you must
 provide the keyid with an option\n");
+			return NULL;
+		}
+
+		ENGINE_load_builtin_engines();
+		e = ENGINE_by_id("pkcs11");
+		if (!e) {
+			log_err("Failed to load pkcs11 engine\n");
+			goto err_pkcs11;
+		}
+		if (!ENGINE_init(e)) {
+			log_err("Failed to initialize the pkcs11
 engine\n");
+			goto err_pkcs11;
+		}
+		if (keypass) {
+			if (!ENGINE_ctrl_cmd_string(e, "PIN", keypass,
 0)) {
+				log_err("Failed to set the PIN for the
 private key\n");
+				goto err_pkcs11;
+			}
+		}
+		pkey = ENGINE_load_private_key(e, keyfile, NULL, NULL);
+		if (!pkey) {
+			log_err("Failed to load private key %s\n",
 keyfile);
+			goto err_pkcs11;
+		}
+	} else {
+		fp = fopen(keyfile, "r");
+		if (!fp) {
+			log_err("Failed to open keyfile: %s\n",
 keyfile);
+			return NULL;
+		}
+		pkey = PEM_read_PrivateKey(fp, NULL, NULL, (void

*)keypass);

src/libimaevm.c +2 -0
+		if (!pkey) {
+			log_err("Failed to PEM_read_PrivateKey key

file: %s\n",

src/libimaevm.c static EVP_PKEY *read_priv_pkey(const char *keyfile, const char *keypass) · line 849

Quoted 5 lines
src/libimaevm.c +5 -0
+				keyfile);
+			output_openssl_errors();
+		}
+
+		fclose(fp);

This looks a bit narrow. Isn't the problem that *any* engine key might be a specifier not a file? In which case the generic fix is not to validate file existence if an engine is present on the command line. That way you can specify any engine key and the pkcs11 key simply becomes a special case of this.

Quoted 3 lines

If you insist on not having to specify --engine for pkcs11 keys then you do the prefix check earlier on and set the engine to pkcs11 (if it's not already set to something else).

I had thought about it but what kept me from wanting to use the engine in evmctl is the crossing into the library and having to extend the library API there just for this purpose or extending the global variable 'imaevm_params', which is less than ideal as well.

   Stefan

Quoted 4 lines

James

#5 lore raw

PATCH 3/4 tests: Extend sign_verify test with pkcs11 specific test

In reply to Stefan Berger

From: Stefan Berger <[email protected]>

---
 tests/functions.sh     |  26 ++++
 tests/sign_verify.test |  50 +++++--
 tests/softhsm_setup    | 290 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 352 insertions(+), 14 deletions(-)
 create mode 100755 tests/softhsm_setup
tests/functions.sh +26 -0
index 91cd5d9..cbb7ea4 100755
@@ -272,3 +272,29 @@ _report_exit() {
   fi
 }
 
+_at_exit() {
+  _report_exit
+  if [ -n "${WORKDIR}" ]; then
+    rm -f "${WORKDIR}"
+  fi
+}
+
+_softhsm_setup() {
+  local workdir="$1"
+
+  local msg
+
+  export SOFTHSM_SETUP_CONFIGDIR="${workdir}"
+  export SOFTHSM2_CONF="${workdir}/softhsm2.conf"
+
+  msg=$(./softhsm_setup setup 2>&1)
+  if [ $? -eq 0 ]; then
+    echo "softhsm_setup setup succeeded: $msg"
+    PKCS11_KEYURI=$(echo $msg | sed -n 's|^keyuri: \(.*\)|\1|p')
+
+    export OPENSSL_ENGINE="-engine pkcs11"
+    export OPENSSL_KEYFORM="-keyform engine"
+  else
+    echo "softhsm_setup setup failed: ${msg}"
+  fi
+}
tests/sign_verify.test +36 -14

104 lines not on this page · raw message

tests/softhsm_setup new file +290 -0

290 lines not on this page · raw message

Signature · 1 line
2.31.1
#6 lore raw

PATCH 4/4 tests: Get the packages for pkcs11 testing on the CI/CD system

In reply to Stefan Berger

From: Stefan Berger <[email protected]>

Get the packages for pkcs11 testing on the CI/CD system.

This is the status on various distros:

- Alpine: could not find package with pkcs11 engine
- Alt Linux: works
- Debian: debian:stable: evmctl is not able to find the pkcs11 module but

          preceeding openssl command line tests with the pkcs11 URI succeeded;
          cannot recreate the issue locally in the debian:stable container
          --> disabled on Ubuntu and Debian

- CentOS7: tests with pkcs11 URI fail on openssl command line level
- CentOS: works
- Fedora: works
- OpenSuSE Leap: package not available in main repo
- OpenSuSE Tumbleweed: works

---
 ci/alt.sh        | 3 +++
 ci/fedora.sh     | 8 ++++++++
 ci/tumbleweed.sh | 3 +++
 3 files changed, 14 insertions(+)
ci/alt.sh +3 -0
index 884c995..65389be 100755
@@ -12,12 +12,15 @@ apt-get install -y \
 		asciidoc \
 		attr \
 		docbook-style-xsl \
+		gnutls-utils \
 		libattr-devel \
 		libkeyutils-devel \
+		libp11 \
 		libssl-devel \
 		openssl \
 		openssl-gost-engine \
 		rpm-build \
+		softhsm \
 		wget \
 		xsltproc \
 		xxd \
ci/fedora.sh +8 -0
index 2d80915..0993607 100755
@@ -25,6 +25,7 @@ yum -y install \
 	automake \
 	diffutils \
 	docbook-xsl \
+	gnutls-utils \
 	gzip \
 	keyutils-libs-devel \
 	libattr-devel \
@@ -33,6 +34,7 @@ yum -y install \
 	make \
 	openssl \
 	openssl-devel \
+	openssl-pkcs11 \
 	pkg-config \
 	procps \
 	sudo \
@@ -42,3 +44,9 @@ yum -y install \
 
 yum -y install docbook5-style-xsl || true
 yum -y install swtpm || true
+
+# SoftHSM is available via EPEL on CentOS
+if [ -f /etc/centos-release ]; then
+	yum -y install epel-release
+fi
+yum -y install softhsm || true
\ No newline at end of file
ci/tumbleweed.sh +3 -0
index dfc478b..4e3da0c 100755
@@ -42,6 +42,9 @@ zypper --non-interactive install --force-resolution --no-recommends \
 	which \
 	xsltproc
 
+zypper --non-interactive install --force-resolution --no-recommends \
+	gnutls openssl-engine-libp11 softhsm || true
+
 if [ -f /usr/lib/ibmtss/tpm_server -a ! -e /usr/local/bin/tpm_server ]; then
 	ln -s /usr/lib/ibmtss/tpm_server /usr/local/bin
 fi
Signature · 1 line
2.31.1