Blame SOURCES/0002-Fix-clevis-luks-unlock-and-add-related-tests.patch

64015d
From e5f6d87d5c71f3faf0c0dbe38534fd3eab30f43e Mon Sep 17 00:00:00 2001
64015d
From: Sergio Correia <scorreia@redhat.com>
64015d
Date: Wed, 13 May 2020 23:51:04 -0300
64015d
Subject: [PATCH 2/8] Fix clevis luks unlock and add related tests
64015d
64015d
---
64015d
 src/luks/clevis-luks-common-functions    |  35 ++++++
64015d
 src/luks/clevis-luks-unlock              |  68 ++++++++++++
64015d
 src/luks/clevis-luks-unlock.in           | 130 ----------------------
64015d
 src/luks/meson.build                     |  10 +-
64015d
 src/luks/tests/meson.build               |  40 +++++++
64015d
 src/luks/tests/tests-common-functions.in | 134 +++++++++++++++++++++--
64015d
 src/luks/tests/unlock-tang-luks1         |  83 ++++++++++++++
64015d
 src/luks/tests/unlock-tang-luks2         |  83 ++++++++++++++
64015d
 8 files changed, 439 insertions(+), 144 deletions(-)
64015d
 create mode 100755 src/luks/clevis-luks-unlock
64015d
 delete mode 100755 src/luks/clevis-luks-unlock.in
64015d
 create mode 100755 src/luks/tests/unlock-tang-luks1
64015d
 create mode 100755 src/luks/tests/unlock-tang-luks2
64015d
64015d
diff --git a/src/luks/clevis-luks-common-functions b/src/luks/clevis-luks-common-functions
64015d
index e27c444..d04fdb5 100644
64015d
--- a/src/luks/clevis-luks-common-functions
64015d
+++ b/src/luks/clevis-luks-common-functions
64015d
@@ -281,3 +281,38 @@ clevis_luks_read_pins_from_slot() {
64015d
     fi
64015d
     printf "%s: %s\n" "${SLOT}" "${cfg}"
64015d
 }
64015d
+
64015d
+# clevis_luks_unlock_device() does the unlock of the device passed as
64015d
+# parameter and returns the decoded passphrase.
64015d
+clevis_luks_unlock_device() {
64015d
+    local DEV="${1}"
64015d
+    [ -z "${DEV}" ] && return 1
64015d
+
64015d
+    local used_slots
64015d
+    if ! used_slots=$(clevis_luks_used_slots "${DEV}") \
64015d
+                      || [ -z "${used_slots}" ]; then
64015d
+        return 1
64015d
+    fi
64015d
+
64015d
+    local slt jwe passphrase
64015d
+    for slt in ${used_slots}; do
64015d
+        if ! jwe="$(clevis_luks_read_slot "${DEV}" "${slt}" 2>/dev/null)" \
64015d
+                   || [ -z "${jwe}" ]; then
64015d
+            continue
64015d
+        fi
64015d
+
64015d
+        if ! passphrase="$(clevis decrypt < <(echo -n "${jwe}"))" \
64015d
+                           || [ -z "${passphrase}" ]; then
64015d
+            continue
64015d
+        fi
64015d
+
64015d
+        if ! cryptsetup luksOpen --test-passphrase "${DEV}" \
64015d
+             --key-file <(echo -n "${passphrase}"); then
64015d
+            continue
64015d
+        fi
64015d
+        echo -n "${passphrase}"
64015d
+        return 0
64015d
+    done
64015d
+
64015d
+    return 1
64015d
+}
64015d
diff --git a/src/luks/clevis-luks-unlock b/src/luks/clevis-luks-unlock
64015d
new file mode 100755
64015d
index 0000000..580fde8
64015d
--- /dev/null
64015d
+++ b/src/luks/clevis-luks-unlock
64015d
@@ -0,0 +1,68 @@
64015d
+#!/bin/bash -e
64015d
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
64015d
+#
64015d
+# Copyright (c) 2016 Red Hat, Inc.
64015d
+# Author: Nathaniel McCallum <npmccallum@redhat.com>
64015d
+#
64015d
+# This program is free software: you can redistribute it and/or modify
64015d
+# it under the terms of the GNU General Public License as published by
64015d
+# the Free Software Foundation, either version 3 of the License, or
64015d
+# (at your option) any later version.
64015d
+#
64015d
+# This program is distributed in the hope that it will be useful,
64015d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
64015d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64015d
+# GNU General Public License for more details.
64015d
+#
64015d
+# You should have received a copy of the GNU General Public License
64015d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
64015d
+#
64015d
+. clevis-luks-common-functions
64015d
+
64015d
+SUMMARY="Unlocks a LUKS volume"
64015d
+
64015d
+function usage() {
64015d
+    exec >&2
64015d
+    echo
64015d
+    echo "Usage: clevis luks unlock -d DEV [-n NAME]"
64015d
+    echo
64015d
+    echo "$SUMMARY":
64015d
+    echo
64015d
+    echo "  -d DEV  The LUKS device on which to perform unlocking"
64015d
+    echo
64015d
+    echo "  -n NAME The name of the unlocked device node"
64015d
+    echo
64015d
+    exit 2
64015d
+}
64015d
+
64015d
+if [ $# -eq 1 ] && [ "$1" == "--summary" ]; then
64015d
+    echo "$SUMMARY"
64015d
+    exit 0
64015d
+fi
64015d
+
64015d
+while getopts ":d:n:" o; do
64015d
+    case "$o" in
64015d
+    d) DEV="$OPTARG";;
64015d
+    n) NAME="$OPTARG";;
64015d
+    *) usage;;
64015d
+    esac
64015d
+done
64015d
+
64015d
+if [ -z "$DEV" ]; then
64015d
+    echo "Did not specify a device!" >&2
64015d
+    usage
64015d
+fi
64015d
+
64015d
+if ! cryptsetup isLuks "$DEV"; then
64015d
+    echo "$DEV is not a LUKS device!" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+NAME="${NAME:-luks-"$(cryptsetup luksUUID "$DEV")"}"
64015d
+
64015d
+if ! pt=$(clevis_luks_unlock_device "${DEV}"); then
64015d
+    echo "${DEV} could not be opened." >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+cryptsetup open -d- "${DEV}" "${NAME}" < <(echo -n "${pt}")
64015d
diff --git a/src/luks/clevis-luks-unlock.in b/src/luks/clevis-luks-unlock.in
64015d
deleted file mode 100755
64015d
index aa3134b..0000000
64015d
--- a/src/luks/clevis-luks-unlock.in
64015d
+++ /dev/null
64015d
@@ -1,130 +0,0 @@
64015d
-#!/bin/bash -e
64015d
-# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
64015d
-#
64015d
-# Copyright (c) 2016 Red Hat, Inc.
64015d
-# Author: Nathaniel McCallum <npmccallum@redhat.com>
64015d
-#
64015d
-# This program is free software: you can redistribute it and/or modify
64015d
-# it under the terms of the GNU General Public License as published by
64015d
-# the Free Software Foundation, either version 3 of the License, or
64015d
-# (at your option) any later version.
64015d
-#
64015d
-# This program is distributed in the hope that it will be useful,
64015d
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
64015d
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64015d
-# GNU General Public License for more details.
64015d
-#
64015d
-# You should have received a copy of the GNU General Public License
64015d
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
64015d
-#
64015d
-
64015d
-SUMMARY="Unlocks a LUKS volume"
64015d
-UUID=cb6e8904-81ff-40da-a84a-07ab9ab5715e
64015d
-
64015d
-# We require cryptsetup >= 2.0.4 to fully support LUKSv2.
64015d
-# Support is determined at build time.
64015d
-function luks2_supported() {
64015d
-    return @OLD_CRYPTSETUP@
64015d
-}
64015d
-
64015d
-function usage() {
64015d
-    exec >&2
64015d
-    echo
64015d
-    echo "Usage: clevis luks unlock -d DEV [-n NAME]"
64015d
-    echo
64015d
-    echo "$SUMMARY":
64015d
-    echo
64015d
-    echo "  -d DEV  The LUKS device on which to perform unlocking"
64015d
-    echo
64015d
-    echo "  -n NAME The name of the unlocked device node"
64015d
-    echo
64015d
-    exit 2
64015d
-}
64015d
-
64015d
-if [ $# -eq 1 ] && [ "$1" == "--summary" ]; then
64015d
-    echo "$SUMMARY"
64015d
-    exit 0
64015d
-fi
64015d
-
64015d
-while getopts ":d:n:" o; do
64015d
-    case "$o" in
64015d
-    d) DEV="$OPTARG";;
64015d
-    n) NAME="$OPTARG";;
64015d
-    *) usage;;
64015d
-    esac
64015d
-done
64015d
-
64015d
-if [ -z "$DEV" ]; then
64015d
-    echo "Did not specify a device!" >&2
64015d
-    usage
64015d
-fi
64015d
-
64015d
-if ! cryptsetup isLuks "$DEV"; then
64015d
-    echo "$DEV is not a LUKS device!" >&2
64015d
-    exit 1
64015d
-fi
64015d
-
64015d
-if luks2_supported; then
64015d
-    if cryptsetup isLuks --type luks1 "$DEV"; then
64015d
-        luks_type="luks1"
64015d
-    elif cryptsetup isLuks --type luks2 "$DEV";then
64015d
-        luks_type="luks2"
64015d
-    else
64015d
-        echo "$DEV is not a supported LUKS device!" >&2
64015d
-        exit 1
64015d
-    fi
64015d
-else
64015d
-    luks_type="luks1"
64015d
-fi
64015d
-NAME="${NAME:-luks-"$(cryptsetup luksUUID "$DEV")"}"
64015d
-
64015d
-luks1_decrypt() {
64015d
-    luksmeta load "$@" \
64015d
-        | clevis decrypt
64015d
-
64015d
-    local rc
64015d
-    for rc in "${PIPESTATUS[@]}"; do
64015d
-        [ $rc -eq 0 ] || return $rc
64015d
-    done
64015d
-    return 0
64015d
-}
64015d
-
64015d
-luks2_decrypt() {
64015d
-    # jose jwe fmt -c outputs extra \n, so clean it up
64015d
-    cryptsetup token export "$@" \
64015d
-        | jose fmt -j- -Og jwe -o- \
64015d
-        | jose jwe fmt -i- -c \
64015d
-        | tr -d '\n' \
64015d
-        | clevis decrypt
64015d
-
64015d
-    local rc
64015d
-    for rc in "${PIPESTATUS[@]}"; do
64015d
-        [ $rc -eq 0 ] || return $rc
64015d
-    done
64015d
-    return 0
64015d
-}
64015d
-
64015d
-if [ "$luks_type" == "luks1" ]; then
64015d
-    while read -r slot state uuid; do
64015d
-        [ "$state" == "active" ] || continue
64015d
-        [ "$uuid" == "$UUID" ] || continue
64015d
-
64015d
-        pt="$(luks1_decrypt -d $DEV -s $slot -u $UUID)" \
64015d
-            || continue
64015d
-        exec cryptsetup open -d- "$DEV" "$NAME" < <(
64015d
-            echo -n "$pt"
64015d
-        )
64015d
-    done < <(luksmeta show -d "$DEV")
64015d
-
64015d
-elif [ "$luks_type" == "luks2" ]; then
64015d
-    while read -r id; do
64015d
-        pt="$(luks2_decrypt --token-id "$id" "$DEV")" \
64015d
-            || continue
64015d
-        exec cryptsetup open -d- "$DEV" "$NAME" < <(
64015d
-            echo -n "$pt"
64015d
-        )
64015d
-    done < <(cryptsetup luksDump "$DEV" | sed -rn 's|^\s+([0-9]+): clevis|\1|p')
64015d
-fi
64015d
-
64015d
-echo "$DEV could not be opened." >&2
64015d
-exit 1
64015d
diff --git a/src/luks/meson.build b/src/luks/meson.build
64015d
index bbba63f..0d24f8d 100644
64015d
--- a/src/luks/meson.build
64015d
+++ b/src/luks/meson.build
64015d
@@ -21,9 +21,7 @@ clevis_luks_bind = configure_file(input: 'clevis-luks-bind.in',
64015d
 clevis_luks_unbind = configure_file(input: 'clevis-luks-unbind.in',
64015d
                output: 'clevis-luks-unbind',
64015d
                configuration: luksmeta_data)
64015d
-clevis_luks_unlock = configure_file(input: 'clevis-luks-unlock.in',
64015d
-               output: 'clevis-luks-unlock',
64015d
-               configuration: luksmeta_data)
64015d
+
64015d
 if libcryptsetup.found() and luksmeta.found() and pwmake.found()
64015d
   subdir('systemd')
64015d
   subdir('udisks2')
64015d
@@ -31,18 +29,18 @@ if libcryptsetup.found() and luksmeta.found() and pwmake.found()
64015d
   bins += clevis_luks_unbind
64015d
   mans += join_paths(meson.current_source_dir(), 'clevis-luks-unbind.1')
64015d
 
64015d
-  bins += clevis_luks_unlock
64015d
-  mans += join_paths(meson.current_source_dir(), 'clevis-luks-unlock.1')
64015d
-
64015d
   bins += clevis_luks_bind
64015d
   mans += join_paths(meson.current_source_dir(), 'clevis-luks-bind.1')
64015d
 
64015d
   mans += join_paths(meson.current_source_dir(), 'clevis-luks-unlockers.7')
64015d
 
64015d
   bins += join_paths(meson.current_source_dir(), 'clevis-luks-common-functions')
64015d
+
64015d
   bins += join_paths(meson.current_source_dir(), 'clevis-luks-list')
64015d
   mans += join_paths(meson.current_source_dir(), 'clevis-luks-list.1')
64015d
 
64015d
+  bins += join_paths(meson.current_source_dir(), 'clevis-luks-unlock')
64015d
+  mans += join_paths(meson.current_source_dir(), 'clevis-luks-unlock.1')
64015d
 else
64015d
   warning('Will not install LUKS support due to missing dependencies!')
64015d
 endif
64015d
diff --git a/src/luks/tests/meson.build b/src/luks/tests/meson.build
64015d
index 2e0fb92..9a16b42 100644
64015d
--- a/src/luks/tests/meson.build
64015d
+++ b/src/luks/tests/meson.build
64015d
@@ -1,6 +1,30 @@
64015d
 # We use jq for comparing the pin config in the clevis luks list tests.
64015d
 jq = find_program('jq', required: false)
64015d
 
64015d
+# we use systemd-socket-activate for running test tang servers.
64015d
+actv = find_program(
64015d
+  'systemd-socket-activate',
64015d
+  'systemd-activate',
64015d
+  required: false
64015d
+)
64015d
+
64015d
+kgen = find_program(
64015d
+  join_paths(libexecdir, 'tangd-keygen'),
64015d
+  join_paths(get_option('prefix'), get_option('libdir'), 'tangd-keygen'),
64015d
+  join_paths(get_option('prefix'), get_option('libexecdir'), 'tangd-keygen'),
64015d
+  join_paths('/', 'usr', get_option('libdir'), 'tangd-keygen'),
64015d
+  join_paths('/', 'usr', get_option('libexecdir'), 'tangd-keygen'),
64015d
+  required: false
64015d
+)
64015d
+tang = find_program(
64015d
+  join_paths(libexecdir, 'tangd'),
64015d
+  join_paths(get_option('prefix'), get_option('libdir'), 'tangd'),
64015d
+  join_paths(get_option('prefix'), get_option('libexecdir'), 'tangd'),
64015d
+  join_paths('/', 'usr', get_option('libdir'), 'tangd'),
64015d
+  join_paths('/', 'usr', get_option('libexecdir'), 'tangd'),
64015d
+  required: false
64015d
+)
64015d
+
64015d
 common_functions = configure_file(input: 'tests-common-functions.in',
64015d
   output: 'tests-common-functions',
64015d
   configuration: luksmeta_data,
64015d
@@ -24,6 +48,14 @@ env.prepend('PATH',
64015d
   separator: ':'
64015d
 )
64015d
 
64015d
+has_tang = false
64015d
+if actv.found() and kgen.found() and tang.found()
64015d
+  has_tang = true
64015d
+  env.set('SD_ACTIVATE', actv.path())
64015d
+  env.set('TANGD_KEYGEN', kgen.path())
64015d
+  env.set('TANGD', tang.path())
64015d
+endif
64015d
+
64015d
 test('bind-wrong-pass-luks1', find_program('bind-wrong-pass-luks1'), env: env)
64015d
 test('bind-luks1', find_program('bind-luks1'), env: env)
64015d
 test('unbind-unbound-slot-luks1', find_program('unbind-unbound-slot-luks1'), env: env)
64015d
@@ -42,6 +74,10 @@ else
64015d
   warning('Will not run "clevis luks list" tests due to missing jq dependency')
64015d
 endif
64015d
 
64015d
+if has_tang
64015d
+  test('unlock-tang-luks1', find_program('unlock-tang-luks1'), env: env, timeout: 90)
64015d
+endif
64015d
+
64015d
 # LUKS2 tests go here, and they get included if we get support for it, based
64015d
 # on the cryptsetup version.
64015d
 # Binding LUKS2 takes longer, so timeout is increased for a few tests.
64015d
@@ -56,4 +92,8 @@ if luksmeta_data.get('OLD_CRYPTSETUP') == '0'
64015d
     test('list-tang-luks2', find_program('list-tang-luks2'), env: env, timeout: 60)
64015d
     test('list-sss-tang-luks2', find_program('list-sss-tang-luks2'), env: env, timeout: 60)
64015d
   endif
64015d
+
64015d
+  if has_tang
64015d
+    test('unlock-tang-luks2', find_program('unlock-tang-luks2'), env: env, timeout: 120)
64015d
+  endif
64015d
 endif
64015d
diff --git a/src/luks/tests/tests-common-functions.in b/src/luks/tests/tests-common-functions.in
64015d
index 90420d1..7b3fdad 100755
64015d
--- a/src/luks/tests/tests-common-functions.in
64015d
+++ b/src/luks/tests/tests-common-functions.in
64015d
@@ -56,7 +56,7 @@ new_device() {
64015d
 
64015d
     # Some builders fail if the cryptsetup steps are not ran as root, so let's
64015d
     # skip the test now if not running as root.
64015d
-    if [ $(id -u) != 0 ]; then
64015d
+    if [ "$(id -u)" != 0 ]; then
64015d
         skip_test "WARNING: You must be root to run this test; test skipped."
64015d
     fi
64015d
 
64015d
@@ -74,9 +74,9 @@ new_device() {
64015d
         return 0
64015d
     fi
64015d
 
64015d
-    fallocate -l16M "${DEV}"
64015d
-    local extra_options='--pbkdf pbkdf2 --pbkdf-force-iterations 1000'
64015d
-    cryptsetup luksFormat --type "${LUKS}" ${extra_options} --batch-mode \
64015d
+    fallocate -l64M "${DEV}"
64015d
+    cryptsetup luksFormat --type "${LUKS}" --pbkdf pbkdf2 \
64015d
+        --pbkdf-force-iterations 1000 --batch-mode \
64015d
         --force-password "${DEV}" <<< "${PASS}"
64015d
     # Caching the just-formatted device for possible reuse.
64015d
     cp -f "${DEV}" "${DEV_CACHED}"
64015d
@@ -90,7 +90,7 @@ new_device_keyfile() {
64015d
 
64015d
     # Some builders fail if the cryptsetup steps are not ran as root, so let's
64015d
     # skip the test now if not running as root.
64015d
-    if [ $(id -u) != 0 ]; then
64015d
+    if [ "$(id -u)" != 0 ]; then
64015d
         skip_test "WARNING: You must be root to run this test; test skipped."
64015d
     fi
64015d
 
64015d
@@ -98,9 +98,9 @@ new_device_keyfile() {
64015d
         error "Invalid keyfile (${KEYFILE})."
64015d
     fi
64015d
 
64015d
-    fallocate -l16M "${DEV}"
64015d
-    local extra_options='--pbkdf pbkdf2 --pbkdf-force-iterations 1000'
64015d
-    cryptsetup luksFormat --type "${LUKS}"  ${extra_options} --batch-mode \
64015d
+    fallocate -l64M "${DEV}"
64015d
+    cryptsetup luksFormat --type "${LUKS}" --pbkdf pbkdf2 \
64015d
+        --pbkdf-force-iterations 1000 --batch-mode \
64015d
         "${DEV}" "${KEYFILE}"
64015d
 }
64015d
 
64015d
@@ -112,4 +112,122 @@ pin_cfg_equal() {
64015d
          <(jq -S . < <(echo -n "${cfg2}"))
64015d
 }
64015d
 
64015d
+# Get a random port to be used with a test tang server.
64015d
+get_random_port() {
64015d
+    shuf -i 1024-65535 -n 1
64015d
+}
64015d
+
64015d
+# Removes tang rotated keys from the test server.
64015d
+tang_remove_rotated_keys() {
64015d
+    local basedir="${1}"
64015d
+
64015d
+    if [ -z "${basedir}" ]; then
64015d
+        echo "Please pass a valid base directory for tang"
64015d
+        return 1
64015d
+    fi
64015d
+
64015d
+    local db="${basedir}/db"
64015d
+    mkdir -p "${db}"
64015d
+
64015d
+    pushd "${db}"
64015d
+        find . -name ".*.jwk" -exec rm -f {} \;
64015d
+    popd
64015d
+}
64015d
+
64015d
+# Creates new keys for the test tang server.
64015d
+tang_new_keys() {
64015d
+    local basedir="${1}"
64015d
+    local rotate="${2}"
64015d
+
64015d
+    if [ -z "${basedir}" ]; then
64015d
+        echo "Please pass a valid base directory for tang"
64015d
+        return 1
64015d
+    fi
64015d
+
64015d
+    [ -z "${TANGD_KEYGEN}" ] && skip_test "WARNING: TANGD_KEYGEN is not defined."
64015d
+
64015d
+    local db="${basedir}/db"
64015d
+    mkdir -p "${db}"
64015d
+
64015d
+    if [ -n "${rotate}" ]; then
64015d
+        pushd "${db}"
64015d
+            local k
64015d
+            k=$(find . -name "*.jwk" | wc -l)
64015d
+            if [ "${k}" -gt 0 ]; then
64015d
+                for k in *.jwk; do
64015d
+                    mv -f -- "${k}" ".${k}"
64015d
+                done
64015d
+            fi
64015d
+        popd
64015d
+    fi
64015d
+
64015d
+    "${TANGD_KEYGEN}" "${db}"
64015d
+
64015d
+    return 0
64015d
+}
64015d
+
64015d
+# Start a test tang server.
64015d
+tang_run() {
64015d
+    local basedir="${1}"
64015d
+    local port="${2}"
64015d
+
64015d
+    if [ -z "${basedir}" ]; then
64015d
+        echo "Please pass a valid base directory for tang" >&2
64015d
+        return 1
64015d
+    fi
64015d
+
64015d
+    if [ -z "${port}" ]; then
64015d
+        echo "Please pass a valid port for tang" >&2
64015d
+        return 1
64015d
+    fi
64015d
+
64015d
+    if ! tang_new_keys "${basedir}"; then
64015d
+        echo "Error creating new keys for tang server" >&2
64015d
+        return 1
64015d
+    fi
64015d
+
64015d
+    local KEYS="${basedir}/db"
64015d
+
64015d
+    local inetd='--inetd'
64015d
+    [ "${SD_ACTIVATE##*/}" = "systemd-activate" ] && inetd=
64015d
+
64015d
+    local pid pidfile
64015d
+    pidfile="${basedir}/tang.pid"
64015d
+
64015d
+    "${SD_ACTIVATE}" ${inetd} -l "${TANG_HOST}":"${port}" \
64015d
+            -a "${TANGD}" "${KEYS}" &
64015d
+    pid=$!
64015d
+    echo "${pid}" > "${pidfile}"
64015d
+}
64015d
+
64015d
+# Stop tang server.
64015d
+tang_stop() {
64015d
+    local basedir="${1}"
64015d
+    local pidfile="${basedir}/tang.pid"
64015d
+    [ -f "${pidfile}" ] || return 0
64015d
+
64015d
+    local pid
64015d
+    pid=$(<"${pidfile}")
64015d
+    kill "${pid}"
64015d
+}
64015d
+
64015d
+# Wait for the tang server to be operational.
64015d
+tang_wait_until_ready() {
64015d
+   local port="${1}"
64015d
+   while ! curl --output /dev/null --silent --fail \
64015d
+                http://"${TANG_HOST}":"${port}"/adv; do
64015d
+       sleep 0.1
64015d
+       echo -n . >&2
64015d
+   done
64015d
+}
64015d
+
64015d
+# Get tang advertisement.
64015d
+tang_get_adv() {
64015d
+    local port="${1}"
64015d
+    local adv="${2}"
64015d
+
64015d
+    curl -o "${adv}" http://"${TANG_HOST}":"${port}"/adv
64015d
+}
64015d
+
64015d
+export TANG_HOST=127.0.0.1
64015d
 export DEFAULT_PASS='just-some-test-password-here'
64015d
diff --git a/src/luks/tests/unlock-tang-luks1 b/src/luks/tests/unlock-tang-luks1
64015d
new file mode 100755
64015d
index 0000000..841ba01
64015d
--- /dev/null
64015d
+++ b/src/luks/tests/unlock-tang-luks1
64015d
@@ -0,0 +1,83 @@
64015d
+#!/bin/bash -ex
64015d
+# vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
64015d
+#
64015d
+# Copyright (c) 2020 Red Hat, Inc.
64015d
+# Author: Sergio Correia <scorreia@redhat.com>
64015d
+#
64015d
+# This program is free software: you can redistribute it and/or modify
64015d
+# it under the terms of the GNU General Public License as published by
64015d
+# the Free Software Foundation, either version 3 of the License, or
64015d
+# (at your option) any later version.
64015d
+#
64015d
+# This program is distributed in the hope that it will be useful,
64015d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
64015d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64015d
+# GNU General Public License for more details.
64015d
+#
64015d
+# You should have received a copy of the GNU General Public License
64015d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
64015d
+
64015d
+TEST=$(basename "${0}")
64015d
+. tests-common-functions
64015d
+
64015d
+. clevis-luks-common-functions
64015d
+
64015d
+on_exit() {
64015d
+    [ ! -d "${TMP}" ] && return 0
64015d
+    tang_stop "${TMP}"
64015d
+    rm -rf "${TMP}"
64015d
+}
64015d
+
64015d
+trap 'on_exit' EXIT
64015d
+trap 'on_exit' ERR
64015d
+
64015d
+TMP="$(mktemp -d)"
64015d
+
64015d
+port=$(get_random_port)
64015d
+tang_run "${TMP}" "${port}" &
64015d
+tang_wait_until_ready "${port}"
64015d
+
64015d
+url="http://${TANG_HOST}:${port}"
64015d
+adv="${TMP}/adv"
64015d
+tang_get_adv "${port}" "${adv}"
64015d
+
64015d
+cfg=$(printf '{"url":"%s","adv":"%s"}' "$url" "$adv")
64015d
+
64015d
+# LUKS1.
64015d
+DEV="${TMP}/luks1-device"
64015d
+new_device "luks1" "${DEV}"
64015d
+
64015d
+if ! clevis luks bind -f -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
64015d
+    error "${TEST}: Bind should have succeeded."
64015d
+fi
64015d
+
64015d
+if ! clevis_luks_unlock_device "${DEV}"; then
64015d
+    error "${TEST}: we were unable to unlock ${DEV}."
64015d
+fi
64015d
+
64015d
+# Let's rotate the tang keys and add another binding with the new key.
64015d
+tang_new_keys "${TMP}" "rotate-keys"
64015d
+
64015d
+# Unlock should still work now.
64015d
+if ! clevis_luks_unlock_device "${DEV}"; then
64015d
+    error "${TEST}: we should still be able to unlock ${DEV}"
64015d
+fi
64015d
+
64015d
+# Now let's remove the rotated keys.
64015d
+tang_remove_rotated_keys "${TMP}"
64015d
+
64015d
+# Unlock should not work anymore.
64015d
+if clevis_luks_unlock_device "${DEV}"; then
64015d
+    error "${TEST}: we should not be able to unlock ${DEV}"
64015d
+fi
64015d
+
64015d
+# Now let's add another binding with the new keys.
64015d
+tang_get_adv "${port}" "${adv}" # Updating the advertisement.
64015d
+if ! clevis luks bind -f -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
64015d
+    error "${TEST}: Bind should have succeeded."
64015d
+fi
64015d
+
64015d
+# Unlock should work again, using the new keys.
64015d
+if ! clevis_luks_unlock_device "${DEV}"; then
64015d
+    error "${TEST}: we should be able to unlock ${DEV} with the new keys"
64015d
+fi
64015d
diff --git a/src/luks/tests/unlock-tang-luks2 b/src/luks/tests/unlock-tang-luks2
64015d
new file mode 100755
64015d
index 0000000..81822fb
64015d
--- /dev/null
64015d
+++ b/src/luks/tests/unlock-tang-luks2
64015d
@@ -0,0 +1,83 @@
64015d
+#!/bin/bash -ex
64015d
+# vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
64015d
+#
64015d
+# Copyright (c) 2020 Red Hat, Inc.
64015d
+# Author: Sergio Correia <scorreia@redhat.com>
64015d
+#
64015d
+# This program is free software: you can redistribute it and/or modify
64015d
+# it under the terms of the GNU General Public License as published by
64015d
+# the Free Software Foundation, either version 3 of the License, or
64015d
+# (at your option) any later version.
64015d
+#
64015d
+# This program is distributed in the hope that it will be useful,
64015d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
64015d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64015d
+# GNU General Public License for more details.
64015d
+#
64015d
+# You should have received a copy of the GNU General Public License
64015d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
64015d
+
64015d
+TEST=$(basename "${0}")
64015d
+. tests-common-functions
64015d
+
64015d
+. clevis-luks-common-functions
64015d
+
64015d
+on_exit() {
64015d
+    [ ! -d "${TMP}" ] && return 0
64015d
+    tang_stop "${TMP}"
64015d
+    rm -rf "${TMP}"
64015d
+}
64015d
+
64015d
+trap 'on_exit' EXIT
64015d
+trap 'on_exit' ERR
64015d
+
64015d
+TMP="$(mktemp -d)"
64015d
+
64015d
+port=$(get_random_port)
64015d
+tang_run "${TMP}" "${port}" &
64015d
+tang_wait_until_ready "${port}"
64015d
+
64015d
+url="http://${TANG_HOST}:${port}"
64015d
+adv="${TMP}/adv"
64015d
+tang_get_adv "${port}" "${adv}"
64015d
+
64015d
+cfg=$(printf '{"url":"%s","adv":"%s"}' "$url" "$adv")
64015d
+
64015d
+# LUKS2.
64015d
+DEV="${TMP}/luks2-device"
64015d
+new_device "luks2" "${DEV}"
64015d
+
64015d
+if ! clevis luks bind -f -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
64015d
+    error "${TEST}: Bind should have succeeded."
64015d
+fi
64015d
+
64015d
+if ! clevis_luks_unlock_device "${DEV}"; then
64015d
+    error "${TEST}: we were unable to unlock ${DEV}."
64015d
+fi
64015d
+
64015d
+# Let's rotate the tang keys and add another binding with the new key.
64015d
+tang_new_keys "${TMP}" "rotate-keys"
64015d
+
64015d
+# Unlock should still work now.
64015d
+if ! clevis_luks_unlock_device "${DEV}"; then
64015d
+    error "${TEST}: we should still be able to unlock ${DEV}"
64015d
+fi
64015d
+
64015d
+# Now let's remove the rotated keys.
64015d
+tang_remove_rotated_keys "${TMP}"
64015d
+
64015d
+# Unlock should not work anymore.
64015d
+if clevis_luks_unlock_device "${DEV}"; then
64015d
+    error "${TEST}: we should not be able to unlock ${DEV}"
64015d
+fi
64015d
+
64015d
+# Now let's add another binding with the new keys.
64015d
+tang_get_adv "${port}" "${adv}" # Updating the advertisement.
64015d
+if ! clevis luks bind -f -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
64015d
+    error "${TEST}: Bind should have succeeded."
64015d
+fi
64015d
+
64015d
+# Unlock should work again, using the new keys.
64015d
+if ! clevis_luks_unlock_device "${DEV}"; then
64015d
+    error "${TEST}: we should be able to unlock ${DEV} with the new keys"
64015d
+fi
64015d
-- 
64015d
2.18.4
64015d