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

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