Blame SOURCES/clevis-7-tpm2.patch

d77600
From fb3c135906cab203cfd11b3cb4a502ef8530987a Mon Sep 17 00:00:00 2001
d77600
From: Nathaniel McCallum <npmccallum@redhat.com>
d77600
Date: Mon, 9 Jul 2018 08:47:14 +0200
d77600
Subject: [PATCH] Add support for TPM 2.0
d77600
d77600
---
d77600
 Makefile.am                   |   5 ++
d77600
 configure.ac                  |  11 +++
d77600
 doc/clevis-encrypt-tpm2.1     | 142 +++++++++++++++++++++++++++++++
d77600
 doc/clevis-encrypt-tpm2.1.md  | 108 +++++++++++++++++++++++
d77600
 doc/clevis.1                  |  40 ++++++++-
d77600
 src/Makefile.am               |   6 ++
d77600
 src/clevis-decrypt-tpm2       | 126 +++++++++++++++++++++++++++
d77600
 src/clevis-encrypt-tpm2       | 156 ++++++++++++++++++++++++++++++++++
d77600
 src/dracut/module-setup.sh.in |  23 +++++
d77600
 9 files changed, 615 insertions(+), 2 deletions(-)
d77600
 create mode 100644 doc/clevis-encrypt-tpm2.1
d77600
 create mode 100644 doc/clevis-encrypt-tpm2.1.md
d77600
 create mode 100755 src/clevis-decrypt-tpm2
d77600
 create mode 100755 src/clevis-encrypt-tpm2
d77600
d77600
diff --git a/Makefile.am b/Makefile.am
d77600
index 141642a..75f9745 100644
d77600
--- a/Makefile.am
d77600
+++ b/Makefile.am
d77600
@@ -14,3 +14,8 @@ dist_man1_MANS = \
d77600
     doc/clevis-luks-bind.1 \
d77600
     doc/clevis-decrypt.1 \
d77600
     doc/clevis.1
d77600
+
d77600
+if HAVE_TPM2_TOOLS
d77600
+    dist_man1_MANS += \
d77600
+    doc/clevis-encrypt-tpm2.1
d77600
+endif
d77600
diff --git a/configure.ac b/configure.ac
d77600
index f9ed01a..313f64e 100644
d77600
--- a/configure.ac
d77600
+++ b/configure.ac
d77600
@@ -54,6 +54,17 @@ fi
d77600
 
d77600
 AC_SUBST(SD_ACTIVATE)
d77600
 
d77600
+for ac_prog in createprimary pcrlist createpolicy create load unseal; do
d77600
+    unset TPM2_TOOLS
d77600
+    unset ac_cv_prog_TPM2_TOOLS
d77600
+    AC_CHECK_PROG([TPM2_TOOLS], [tpm2_$ac_prog], [yes])
d77600
+    test -z "$TPM2_TOOLS" && break
d77600
+done
d77600
+
d77600
+test -n "$TPM2_TOOLS" || AC_MSG_WARN([tpm2_$ac_prog not found, tpm2 pin won't be installed])
d77600
+
d77600
+AM_CONDITIONAL([HAVE_TPM2_TOOLS], [test -n "$TPM2_TOOLS"])
d77600
+
d77600
 AC_ARG_ENABLE([user],
d77600
               AS_HELP_STRING([--enable-user=USER],
d77600
                              [Set unprivileged user (default: root)]),
d77600
diff --git a/doc/clevis-encrypt-tpm2.1 b/doc/clevis-encrypt-tpm2.1
d77600
new file mode 100644
d77600
index 0000000..096ad23
d77600
--- /dev/null
d77600
+++ b/doc/clevis-encrypt-tpm2.1
d77600
@@ -0,0 +1,142 @@
d77600
+.\" Automatically generated by Pandoc 1.19.1
d77600
+.\"
d77600
+.TH "CLEVIS\-ENCRYPT\-TPM2" "1" "November 2017" "" ""
d77600
+.hy
d77600
+.SH NAME
d77600
+.PP
d77600
+clevis\-encrypt\-tpm2 \-\- Encrypts using a TPM2.0 chip binding policy
d77600
+.SH SYNOPSIS
d77600
+.PP
d77600
+\f[C]clevis\ encrypt\ tpm2\f[] CONFIG < PT > JWE
d77600
+.SH OVERVIEW
d77600
+.PP
d77600
+The \f[C]clevis\ encrypt\ tpm2\f[] command encrypts using a Trusted
d77600
+Platform Module 2.0 (TPM2) chip.
d77600
+Its only argument is the JSON configuration object.
d77600
+.PP
d77600
+When using the tpm2 pin, we create a new, cryptographically\-strong,
d77600
+random key.
d77600
+This key is encrypted using the TPM2 chip.
d77600
+Then at decryption time, the key is decrypted again using the TPM2 chip.
d77600
+.IP
d77600
+.nf
d77600
+\f[C]
d77600
+$\ clevis\ encrypt\ tpm2\ \[aq]{}\[aq]\ <\ PT\ >\ JWE
d77600
+\f[]
d77600
+.fi
d77600
+.PP
d77600
+The pin has reasonable defaults for its configuration, but a different
d77600
+hierarchy, hash, and key algorithms can be chosen if the defaults used
d77600
+are not suitable:
d77600
+.IP
d77600
+.nf
d77600
+\f[C]
d77600
+$\ clevis\ encrypt\ tpm2\ \[aq]{"hash":"sha1","key":"rsa"}\[aq]\ <\ PT\ >\ JWE
d77600
+\f[]
d77600
+.fi
d77600
+.PP
d77600
+To decrypt the data, simply provide the ciphertext (JWE):
d77600
+.IP
d77600
+.nf
d77600
+\f[C]
d77600
+$\ clevis\ decrypt\ <\ JWE\ >\ PT
d77600
+\f[]
d77600
+.fi
d77600
+.PP
d77600
+Note that like other pins no configuration is used for decryption, this
d77600
+is due clevis storing the public and private keys to unseal the TPM2
d77600
+encrypted object in the JWE so clevis can fetch that information from
d77600
+there.
d77600
+.PP
d77600
+The pin also supports sealing data to a Platform Configuration Registers
d77600
+(PCR) state.
d77600
+That way the data can only be unsealed if the PCRs hashes values match
d77600
+the policy used when sealing.
d77600
+.PP
d77600
+For example, to seal the data to the PCR with index 0 and 1 for the SHA1
d77600
+bank:
d77600
+.IP
d77600
+.nf
d77600
+\f[C]
d77600
+$\ clevis\ encrypt\ tpm2\ \[aq]{"pcr_bank":"sha1","pcr_ids":"0,1"}\[aq]\ <\ PT\ >\ JWE
d77600
+\f[]
d77600
+.fi
d77600
+.PP
d77600
+The PCR digest values are looked up from the current hash values for the
d77600
+PCRs, but a digest can also be provided if the data needs to be sealed
d77600
+with values different to the current ones, by passing the binary hash
d77600
+encoded in base64:
d77600
+.IP
d77600
+.nf
d77600
+\f[C]
d77600
+$\ clevis\ encrypt\ tpm2\ \[aq]{"pcr_ids":"0","pcr_digest":"xy7J5svCtqlfM03d1lE5gdoA8MI"}\[aq]\ <\ PT\ >\ JWE
d77600
+\f[]
d77600
+.fi
d77600
+.SH Threat model
d77600
+.PP
d77600
+The Clevis security model relies in the fact that an attacker will not
d77600
+be able to access both the encrypted data and the decryption key.
d77600
+.PP
d77600
+For most Clevis pins, the decryption key is not locally stored, so the
d77600
+decryption policy is only satisfied if the decryption key can be
d77600
+remotely accessed.
d77600
+It could for example be stored in a remote server or in a hardware
d77600
+authentication device that has to be plugged into the machine.
d77600
+.PP
d77600
+The tpm2 pin is different in this regard, since a key is wrapped by a
d77600
+TPM2 chip that is always present in the machine.
d77600
+This does not mean that there are not use cases for this pin, but it is
d77600
+important to understand the fact that an attacker that has access to
d77600
+both the encrypted data and the local TPM2 chip will be able to decrypt
d77600
+the data.
d77600
+.SH CONFIG
d77600
+.PP
d77600
+This command uses the following configuration properties:
d77600
+.IP \[bu] 2
d77600
+\f[C]hash\f[] (string) : Hash algorithm used in the computation of the
d77600
+object name (default: sha256)
d77600
+.PP
d77600
+It must be one of the following:
d77600
+.IP \[bu] 2
d77600
+\f[C]sha1\f[]
d77600
+.IP \[bu] 2
d77600
+\f[C]sha256\f[]
d77600
+.IP \[bu] 2
d77600
+\f[C]sha384\f[]
d77600
+.IP \[bu] 2
d77600
+\f[C]sha512\f[]
d77600
+.IP \[bu] 2
d77600
+\f[C]sm3_256\f[]
d77600
+.IP \[bu] 2
d77600
+\f[C]key\f[] (string) : Algorithm type for the generated key (default:
d77600
+ecc)
d77600
+.PP
d77600
+It must be one of the following:
d77600
+.IP \[bu] 2
d77600
+\f[C]rsa\f[]
d77600
+.IP \[bu] 2
d77600
+\f[C]keyedhash\f[]
d77600
+.IP \[bu] 2
d77600
+\f[C]ecc\f[]
d77600
+.IP \[bu] 2
d77600
+\f[C]symcipher\f[]
d77600
+.IP \[bu] 2
d77600
+\f[C]pcr_bank\f[] (string) : PCR algorithm bank to use for policy
d77600
+(default: sha1)
d77600
+.PP
d77600
+It must be one of the following:
d77600
+.IP \[bu] 2
d77600
+\f[C]sha1\f[]
d77600
+.IP \[bu] 2
d77600
+\f[C]sha256\f[]
d77600
+.IP \[bu] 2
d77600
+\f[C]pcr_ids\f[] (string) : Comma separated list of PCR used for policy.
d77600
+If not present, no policy is used
d77600
+.IP \[bu] 2
d77600
+\f[C]pcr_digest\f[] (string) : Binary PCR hashes encoded in base64.
d77600
+If not present, the hash values are looked up
d77600
+.SH SEE ALSO
d77600
+.PP
d77600
+\f[C]clevis\-decrypt\f[](1)
d77600
+.SH AUTHORS
d77600
+Javier Martinez Canillas <javierm@redhat.com>.
d77600
diff --git a/doc/clevis-encrypt-tpm2.1.md b/doc/clevis-encrypt-tpm2.1.md
d77600
new file mode 100644
d77600
index 0000000..f533d67
d77600
--- /dev/null
d77600
+++ b/doc/clevis-encrypt-tpm2.1.md
d77600
@@ -0,0 +1,108 @@
d77600
+% CLEVIS-ENCRYPT-TPM2(1)
d77600
+% Javier Martinez Canillas <javierm@redhat.com>
d77600
+% November 2017
d77600
+
d77600
+# NAME
d77600
+
d77600
+clevis-encrypt-tpm2 -- Encrypts using a TPM2.0 chip binding policy
d77600
+
d77600
+# SYNOPSIS
d77600
+
d77600
+`clevis encrypt tpm2` CONFIG < PT > JWE
d77600
+
d77600
+# OVERVIEW
d77600
+
d77600
+The `clevis encrypt tpm2` command encrypts using a Trusted Platform Module 2.0
d77600
+(TPM2) chip. Its only argument is the JSON configuration object.
d77600
+
d77600
+When using the tpm2 pin, we create a new, cryptographically-strong, random key.
d77600
+This key is encrypted using the TPM2 chip.
d77600
+Then at decryption time, the key is decrypted again using the TPM2 chip.
d77600
+
d77600
+    $ clevis encrypt tpm2 '{}' < PT > JWE
d77600
+
d77600
+The pin has reasonable defaults for its configuration, but a different hierarchy,
d77600
+hash, and key algorithms can be chosen if the defaults used are not suitable:
d77600
+
d77600
+    $ clevis encrypt tpm2 '{"hash":"sha1","key":"rsa"}' < PT > JWE
d77600
+
d77600
+To decrypt the data, simply provide the ciphertext (JWE):
d77600
+
d77600
+    $ clevis decrypt < JWE > PT
d77600
+
d77600
+Note that like other pins no configuration is used for decryption, this is due
d77600
+clevis storing the public and private keys to unseal the TPM2 encrypted object
d77600
+in the JWE so clevis can fetch that information from there.
d77600
+
d77600
+The pin also supports sealing data to a Platform Configuration Registers (PCR)
d77600
+state. That way the data can only be unsealed if the PCRs hashes values match
d77600
+the policy used when sealing.
d77600
+
d77600
+For example, to seal the data to the PCR with index 0 and 1 for the SHA1 bank:
d77600
+
d77600
+    $ clevis encrypt tpm2 '{"pcr_bank":"sha1","pcr_ids":"0,1"}' < PT > JWE
d77600
+
d77600
+The PCR digest values are looked up from the current hash values for the PCRs,
d77600
+but a digest can also be provided if the data needs to be sealed with values
d77600
+different to the current ones, by passing the binary hash encoded in base64:
d77600
+
d77600
+    $ clevis encrypt tpm2 '{"pcr_ids":"0","pcr_digest":"xy7J5svCtqlfM03d1lE5gdoA8MI"}' < PT > JWE
d77600
+
d77600
+# Threat model
d77600
+
d77600
+The Clevis security model relies in the fact that an attacker will not be able to
d77600
+access both the encrypted data and the decryption key.
d77600
+
d77600
+For most Clevis pins, the decryption key is not locally stored, so the decryption
d77600
+policy is only satisfied if the decryption key can be remotely accessed. It could
d77600
+for example be stored in a remote server or in a hardware authentication device
d77600
+that has to be plugged into the machine.
d77600
+
d77600
+The tpm2 pin is different in this regard, since a key is wrapped by a TPM2 chip
d77600
+that is always present in the machine. This does not mean that there are not use
d77600
+cases for this pin, but it is important to understand the fact that an attacker
d77600
+that has access to both the encrypted data and the local TPM2 chip will be able
d77600
+to decrypt the data.
d77600
+
d77600
+# CONFIG
d77600
+
d77600
+This command uses the following configuration properties:
d77600
+
d77600
+* `hash`  (string) :
d77600
+  Hash algorithm used in the computation of the object name (default: sha256)
d77600
+
d77600
+  It must be one of the following:
d77600
+
d77600
+  * `sha1`
d77600
+  * `sha256`
d77600
+  * `sha384`
d77600
+  * `sha512`
d77600
+  * `sm3_256`
d77600
+
d77600
+* `key`  (string) :
d77600
+  Algorithm type for the generated key (default: ecc)
d77600
+
d77600
+  It must be one of the following:
d77600
+
d77600
+  * `rsa`
d77600
+  * `keyedhash`
d77600
+  * `ecc`
d77600
+  * `symcipher`
d77600
+
d77600
+* `pcr_bank`  (string) :
d77600
+  PCR algorithm bank to use for policy (default: sha1)
d77600
+
d77600
+  It must be one of the following:
d77600
+
d77600
+  * `sha1`
d77600
+  * `sha256`
d77600
+
d77600
+* `pcr_ids`  (string) :
d77600
+  Comma separated list of PCR used for policy. If not present, no policy is used
d77600
+
d77600
+* `pcr_digest`  (string) :
d77600
+  Binary PCR hashes encoded in base64. If not present, the hash values are looked up
d77600
+
d77600
+# SEE ALSO
d77600
+
d77600
+`clevis-decrypt`(1)
d77600
diff --git a/doc/clevis.1 b/doc/clevis.1
d77600
index 3d4a10d..0937533 100644
d77600
--- a/doc/clevis.1
d77600
+++ b/doc/clevis.1
d77600
@@ -80,6 +80,42 @@ $\ clevis\ decrypt\ <\ JWE\ >\ PT
d77600
 .fi
d77600
 .PP
d77600
 For more information, see \f[C]clevis\-encrypt\-tang\f[](1).
d77600
+.SH TPM2 BINDING
d77600
+.PP
d77600
+Clevis provides support to encrypt a key in a Trusted Platform Module
d77600
+2.0 (TPM2) chip.
d77600
+The cryptographically\-strong, random key used for encryption is
d77600
+encrypted using the TPM2 chip, and then at decryption time is decrypted
d77600
+using the TPM2 to allow clevis to decrypt the secret stored in the JWE.
d77600
+.PP
d77600
+Encrypting data using the tpm2 pin works the same than the pins
d77600
+mentioned above:
d77600
+.IP
d77600
+.nf
d77600
+\f[C]
d77600
+$\ clevis\ encrypt\ tpm2\ \[aq]{}\[aq]\ <\ PT\ >\ JWE
d77600
+\f[]
d77600
+.fi
d77600
+.PP
d77600
+The pin has reasonable defaults for its configuration, but a different
d77600
+hierarchy, hash, and key algorithms can be chosen if the defaults used
d77600
+are not suitable.
d77600
+.PP
d77600
+Decryption also works similar to other pins, only the JWE needs to be
d77600
+provided:
d77600
+.IP
d77600
+.nf
d77600
+\f[C]
d77600
+$\ clevis\ decrypt\ <\ JWE\ >\ PT
d77600
+\f[]
d77600
+.fi
d77600
+.PP
d77600
+Note that like other pins no configuration is used for decryption, this
d77600
+is due clevis storing the public and private keys to unseal the TPM2
d77600
+encrypted object in the JWE so clevis can fetch that information from
d77600
+there.
d77600
+.PP
d77600
+For more information see \f[C]clevis\-encrypt\-tpm2\f[](1).
d77600
 .SH SHAMIR\[aq]S SECRET SHARING
d77600
 .PP
d77600
 Clevis provides a way to mix pins together to create sophisticated
d77600
@@ -151,7 +187,7 @@ For more information, see \f[C]clevis\-luks\-bind\f[](1).
d77600
 .SH SEE ALSO
d77600
 .PP
d77600
 \f[C]clevis\-encrypt\-http\f[](1), \f[C]clevis\-encrypt\-tang\f[](1),
d77600
-\f[C]clevis\-encrypt\-sss\f[](1), \f[C]clevis\-luks\-bind\f[](1),
d77600
-\f[C]clevis\-decrypt\f[](1)
d77600
+\f[C]clevis\-encrypt\-tpm2\f[](1), \f[C]clevis\-encrypt\-sss\f[](1),
d77600
+\f[C]clevis\-luks\-bind\f[](1), \f[C]clevis\-decrypt\f[](1)
d77600
 .SH AUTHORS
d77600
 Nathaniel McCallum <npmccallum@redhat.com>.
d77600
diff --git a/src/Makefile.am b/src/Makefile.am
d77600
index 244874b..8562502 100644
d77600
--- a/src/Makefile.am
d77600
+++ b/src/Makefile.am
d77600
@@ -25,6 +25,12 @@ dist_bin_SCRIPTS = \
d77600
     clevis-decrypt \
d77600
     clevis
d77600
 
d77600
+if HAVE_TPM2_TOOLS
d77600
+    dist_bin_SCRIPTS += \
d77600
+    clevis-encrypt-tpm2 \
d77600
+    clevis-decrypt-tpm2
d77600
+endif
d77600
+
d77600
 clevis_encrypt_sss_SOURCES = clevis-encrypt-sss.c sss.c sss.h
d77600
 clevis_decrypt_sss_SOURCES = clevis-decrypt-sss.c sss.c sss.h
d77600
 clevis_encrypt_sss_LDADD = @jose_LIBS@ @libcrypto_LIBS@
d77600
diff --git a/src/clevis-decrypt-tpm2 b/src/clevis-decrypt-tpm2
d77600
new file mode 100755
d77600
index 0000000..f3871d8
d77600
--- /dev/null
d77600
+++ b/src/clevis-decrypt-tpm2
d77600
@@ -0,0 +1,126 @@
d77600
+#!/bin/bash -e
d77600
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
d77600
+#
d77600
+# Copyright (c) 2017 Red Hat, Inc.
d77600
+# Author: Javier Martinez Canillas <javierm@redhat.com>
d77600
+#
d77600
+# This program is free software: you can redistribute it and/or modify
d77600
+# it under the terms of the GNU General Public License as published by
d77600
+# the Free Software Foundation, either version 3 of the License, or
d77600
+# (at your option) any later version.
d77600
+#
d77600
+# This program is distributed in the hope that it will be useful,
d77600
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
d77600
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d77600
+# GNU General Public License for more details.
d77600
+#
d77600
+# You should have received a copy of the GNU General Public License
d77600
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
d77600
+#
d77600
+
d77600
+# The owner hierarchy is the one that should be used by the Operating System.
d77600
+auth="o"
d77600
+
d77600
+function on_exit() {
d77600
+    if ! rm -r $TMP; then
d77600
+        echo "Delete temporary files failed!" >&2
d77600
+        exit 1
d77600
+    fi
d77600
+}
d77600
+
d77600
+[ $# -eq 1 -a "$1" == "--summary" ] && exit 1
d77600
+
d77600
+if [ -t 0 ]; then
d77600
+    echo >&2
d77600
+    echo "Usage: clevis decrypt tpm2 < JWE > PLAINTEXT" >&2
d77600
+    echo >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+export TPM2TOOLS_TCTI_NAME=device
d77600
+export TPM2TOOLS_DEVICE_FILE=`ls /dev/tpmrm? 2>/dev/null`
d77600
+
d77600
+if [ -z "${TPM2TOOLS_DEVICE_FILE[0]}" ]; then
d77600
+    echo "A TPM2 device with the in-kernel resource manager is needed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! [[ -r "${TPM2TOOLS_DEVICE_FILE[0]}" && -w "${TPM2TOOLS_DEVICE_FILE[0]}" ]]; then
d77600
+    echo "The ${TPM2TOOLS_DEVICE_FILE[0]} device must be readable and writable!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+read -d . hdr
d77600
+
d77600
+if ! jhd=`jose b64 dec -i- <<< "$hdr"`; then
d77600
+    echo "Error decoding JWE protected header!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if [ `jose fmt -j- -Og clevis -g pin -u- <<< "$jhd"` != "tpm2" ]; then
d77600
+    echo "JWE pin mismatch!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! hash=`jose fmt -j- -Og clevis -g tpm2 -g hash -Su- <<< "$jhd"`; then
d77600
+    echo "JWE missing required 'hash' header parameter!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! key=`jose fmt -j- -Og clevis -g tpm2 -g key -Su- <<< "$jhd"`; then
d77600
+    echo "JWE missing required 'key' header parameter!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! jwk_pub=`jose fmt -j- -Og clevis -g tpm2 -g jwk_pub -Su- <<< "$jhd"`; then
d77600
+    echo "JWE missing required 'key' header parameter!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! jwk_priv=`jose fmt -j- -Og clevis -g tpm2 -g jwk_priv -Su- <<< "$jhd"`; then
d77600
+    echo "JWE missing required 'key' header parameter!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! TMP=`mktemp -d`; then
d77600
+    echo "Creating a temporary dir for TPM files failed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+trap 'on_exit' EXIT
d77600
+
d77600
+pcr_ids=`jose fmt -j- -Og clevis -g tpm2 -g pcr_ids -Su- <<< "$jhd"` || true
d77600
+
d77600
+if [ -n "$pcr_ids" ]; then
d77600
+    pcr_bank=`jose fmt -j- -Og clevis -g tpm2 -g pcr_bank -Su- <<< "$jhd"`
d77600
+    policy_options="-L $pcr_bank:$pcr_ids"
d77600
+fi
d77600
+
d77600
+if ! `jose b64 dec -i- -O $TMP/jwk.pub <<< "$jwk_pub"`; then
d77600
+    echo "Decoding jwk.pub from Base64 failed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! `jose b64 dec -i- -O $TMP/jwk.priv <<< "$jwk_priv"`; then
d77600
+    echo "Decoding jwk.priv from Base64 failed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! tpm2_createprimary -Q -H "$auth" -g "$hash" -G "$key" \
d77600
+     -C $TMP/primary.context 2>/dev/null; then
d77600
+    echo "Creating TPM2 primary key failed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! tpm2_load -Q -c $TMP/primary.context -u $TMP/jwk.pub -r $TMP/jwk.priv \
d77600
+     -C $TMP/load.context 2>/dev/null; then
d77600
+    echo "Loading jwk to TPM2 failed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! jwk=`tpm2_unseal -c $TMP/load.context $policy_options 2>/dev/null`; then
d77600
+    echo "Unsealing jwk from TPM failed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+jose jwe dec -k- -i- < <(echo -n "$jwk$hdr."; cat)
d77600
diff --git a/src/clevis-encrypt-tpm2 b/src/clevis-encrypt-tpm2
d77600
new file mode 100755
d77600
index 0000000..b99aa97
d77600
--- /dev/null
d77600
+++ b/src/clevis-encrypt-tpm2
d77600
@@ -0,0 +1,156 @@
d77600
+#!/bin/bash -e
d77600
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
d77600
+#
d77600
+# Copyright (c) 2017 Red Hat, Inc.
d77600
+# Author: Javier Martinez Canillas <javierm@redhat.com>
d77600
+#
d77600
+# This program is free software: you can redistribute it and/or modify
d77600
+# it under the terms of the GNU General Public License as published by
d77600
+# the Free Software Foundation, either version 3 of the License, or
d77600
+# (at your option) any later version.
d77600
+#
d77600
+# This program is distributed in the hope that it will be useful,
d77600
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
d77600
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d77600
+# GNU General Public License for more details.
d77600
+#
d77600
+# You should have received a copy of the GNU General Public License
d77600
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
d77600
+#
d77600
+
d77600
+SUMMARY="Encrypts using a TPM2.0 chip binding policy"
d77600
+# The owner hierarchy is the one that should be used by the Operating System.
d77600
+auth="o"
d77600
+# Algorithm type must be keyedhash for object with user provided sensitive data.
d77600
+alg_create_key="keyedhash"
d77600
+# Attributes for the created TPM2 object with the JWK as sensitive data.
d77600
+obj_attr="fixedtpm|fixedparent|sensitivedataorigin|noda|adminwithpolicy"
d77600
+
d77600
+function on_exit() {
d77600
+    if ! rm -rf $TMP; then
d77600
+        echo "Delete temporary files failed!" >&2
d77600
+        exit 1
d77600
+    fi
d77600
+}
d77600
+
d77600
+if [ "$1" == "--summary" ]; then
d77600
+    echo "$SUMMARY"
d77600
+    exit 0
d77600
+fi
d77600
+
d77600
+if [ -t 0 ]; then
d77600
+    echo >&2
d77600
+    echo "Usage: clevis encrypt tpm2 CONFIG < PLAINTEXT > JWE" >&2
d77600
+    echo >&2
d77600
+    echo $SUMMARY >&2
d77600
+    echo >&2
d77600
+    echo "This command uses the following configuration properties:" >&2
d77600
+    echo >&2
d77600
+    echo "  hash: <string>  Hash algorithm used in the computation of the object name (default: sha256)" >&2
d77600
+    echo >&2
d77600
+    echo "  key: <string>   Algorithm type for the generated key (default: ecc)" >&2
d77600
+    echo >&2
d77600
+    echo "  pcr_bank: <string>   PCR algorithm bank to use for policy (default: sha1)" >&2
d77600
+    echo >&2
d77600
+    echo "  pcr_ids: <string>   PCR list used for policy. If not present, no policy is used" >&2
d77600
+    echo >&2
d77600
+    echo "  pcr_digest: <string>   Binary PCR hashes encoded in base64. If not present, the hash values are looked up" >&2
d77600
+    echo >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+export TPM2TOOLS_TCTI_NAME=device
d77600
+export TPM2TOOLS_DEVICE_FILE=`ls /dev/tpmrm? 2>/dev/null`
d77600
+
d77600
+if [ -z "${TPM2TOOLS_DEVICE_FILE[0]}" ]; then
d77600
+    echo "A TPM2 device with the in-kernel resource manager is needed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! [[ -r "${TPM2TOOLS_DEVICE_FILE[0]}" && -w "${TPM2TOOLS_DEVICE_FILE[0]}" ]]; then
d77600
+    echo "The ${TPM2TOOLS_DEVICE_FILE[0]} device must be readable and writable!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! cfg=`jose fmt -j "$1" -Oo- 2>/dev/null`; then
d77600
+    echo "Configuration is malformed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+hash=`jose fmt -j- -Og hash -u- <<< "$cfg"` || hash="sha256"
d77600
+
d77600
+key=`jose fmt -j- -Og key -u- <<< "$cfg"` || key="ecc"
d77600
+
d77600
+pcr_bank=`jose fmt -j- -Og pcr_hash -u- <<< "$cfg"` || pcr_bank="sha1"
d77600
+
d77600
+pcr_ids=`jose fmt -j- -Og pcr_ids -u- <<< "$cfg"` || true
d77600
+
d77600
+pcr_digest=`jose fmt -j- -Og pcr_digest -u- <<< "$cfg"` || true
d77600
+
d77600
+if ! jwk=`jose jwk gen -i '{"alg":"A256GCM"}'`; then
d77600
+    echo "Generating a jwk failed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! TMP=`mktemp -d`; then
d77600
+    echo "Creating a temporary dir for TPM files failed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+trap 'on_exit' EXIT
d77600
+
d77600
+if ! tpm2_createprimary -Q -H "$auth" -g "$hash" -G "$key" -C $TMP/primary.context; then
d77600
+    echo "Creating TPM2 primary key failed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if [ -n "$pcr_ids" ]; then
d77600
+    if [ -z "$pcr_digest" ]; then
d77600
+        if ! tpm2_pcrlist -Q -L "$pcr_bank":"$pcr_ids" -o $TMP/pcr.digest; then
d77600
+            echo "Creating PCR hashes file failed!" >&2
d77600
+            exit 1
d77600
+        fi
d77600
+    else
d77600
+        if ! jose b64 dec -i- -O "$TMP"/pcr.digest <<< "$pcr_digest"; then
d77600
+            echo "Error decoding PCR digest!" >&2
d77600
+            exit 1
d77600
+        fi
d77600
+    fi
d77600
+
d77600
+    if ! tpm2_createpolicy -Q -P -L "$pcr_bank":"$pcr_ids" -F $TMP/pcr.digest -f $TMP/pcr.policy; then
d77600
+        echo "create policy fail, please check the environment or parameters!"
d77600
+        exit 1
d77600
+    fi
d77600
+
d77600
+    policy_options="-L $TMP/pcr.policy"
d77600
+fi
d77600
+
d77600
+if ! tpm2_create -Q -g "$hash" -G "$alg_create_key" -c $TMP/primary.context -u $TMP/jwk.pub \
d77600
+     -r $TMP/jwk.priv -A "$obj_attr" $policy_options -I- <<< "$jwk"; then
d77600
+    echo "Creating TPM2 object for jwk failed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! jwk_pub=`jose b64 enc -I $TMP/jwk.pub`; then
d77600
+    echo "Encoding jwk.pub in Base64 failed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+if ! jwk_priv=`jose b64 enc -I $TMP/jwk.priv`; then
d77600
+    echo "Encoding jwk.priv in Base64 failed!" >&2
d77600
+    exit 1
d77600
+fi
d77600
+
d77600
+jwe='{"protected":{"clevis":{"pin":"tpm2","tpm2":{}}}}'
d77600
+jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$hash" -s hash -UUUUo-`
d77600
+jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$key" -s key -UUUUo-`
d77600
+
d77600
+if [ -n "$pcr_ids" ]; then
d77600
+    jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$pcr_bank" -s pcr_bank -UUUUo-`
d77600
+    jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$pcr_ids" -s pcr_ids -UUUUo-`
d77600
+fi
d77600
+
d77600
+jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$jwk_pub" -s jwk_pub -UUUUo-`
d77600
+jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$jwk_priv" -s jwk_priv -UUUUo-`
d77600
+
d77600
+jose jwe enc -i- -k- -I- -c < <(echo -n "$jwe$jwk"; cat)
d77600
diff --git a/src/dracut/module-setup.sh.in b/src/dracut/module-setup.sh.in
d77600
index 5087d56..119762e 100755
d77600
--- a/src/dracut/module-setup.sh.in
d77600
+++ b/src/dracut/module-setup.sh.in
d77600
@@ -28,6 +28,8 @@ cmdline() {
d77600
 }
d77600
 
d77600
 install() {
d77600
+    local ret=0
d77600
+
d77600
     cmdline > "${initdir}/etc/cmdline.d/99clevis.conf"
d77600
 
d77600
     inst_hook initqueue/online 60 "$moddir/clevis-hook.sh"
d77600
@@ -41,10 +43,31 @@ install() {
d77600
         clevis-decrypt \
d77600
         luksmeta \
d77600
         clevis \
d77600
+        mktemp \
d77600
         curl \
d77600
         jose \
d77600
         nc
d77600
 
d77600
+    for cmd in clevis-decrypt-tpm2 \
d77600
+	tpm2_createprimary \
d77600
+	tpm2_unseal \
d77600
+	tpm2_load; do
d77600
+
d77600
+	if ! find_binary "$cmd" &>/dev/null; then
d77600
+	    ((ret++))
d77600
+	fi
d77600
+    done
d77600
+
d77600
+    if (($ret == 0)); then
d77600
+	inst_multiple clevis-decrypt-tpm2 \
d77600
+	    tpm2_createprimary \
d77600
+	    tpm2_unseal \
d77600
+	    tpm2_load
d77600
+    fi
d77600
+
d77600
     dracut_need_initqueue
d77600
 }
d77600
 
d77600
+installkernel() {
d77600
+    hostonly='' instmods =drivers/char/tpm
d77600
+}
d77600
-- 
d77600
2.17.1
d77600