Blame SOURCES/0010-Add-clevis-luks-edit-command.patch

3700a9
From 46dc5904a9a6b0f1dcdb2511cc3f09671ef54b2a Mon Sep 17 00:00:00 2001
3700a9
From: Sergio Correia <scorreia@redhat.com>
3700a9
Date: Wed, 20 May 2020 21:09:15 -0300
3700a9
Subject: [PATCH] Add clevis luks edit command
3700a9
3700a9
---
3700a9
 src/luks/clevis-luks-common-functions    |  65 ++++--
3700a9
 src/luks/clevis-luks-edit                | 242 +++++++++++++++++++++++
3700a9
 src/luks/clevis-luks-edit.1.adoc         |  69 +++++++
3700a9
 src/luks/clevis-luks-regen               | 125 ++----------
3700a9
 src/luks/meson.build                     |   3 +
3700a9
 src/luks/tests/edit-tang-luks1           | 106 ++++++++++
3700a9
 src/luks/tests/edit-tang-luks2           | 106 ++++++++++
3700a9
 src/luks/tests/meson.build               |  28 ++-
3700a9
 src/luks/tests/regen-inplace-luks1       |   2 +-
3700a9
 src/luks/tests/regen-inplace-luks2       |   2 +-
3700a9
 src/luks/tests/regen-not-inplace-luks1   |   2 +-
3700a9
 src/luks/tests/regen-not-inplace-luks2   |   2 +-
3700a9
 src/luks/tests/tests-common-functions.in |  26 ---
3700a9
 src/luks/tests/unlock-tang-luks1         |   7 +-
3700a9
 src/luks/tests/unlock-tang-luks2         |   8 +-
3700a9
 15 files changed, 606 insertions(+), 187 deletions(-)
3700a9
 create mode 100755 src/luks/clevis-luks-edit
3700a9
 create mode 100644 src/luks/clevis-luks-edit.1.adoc
3700a9
 create mode 100755 src/luks/tests/edit-tang-luks1
3700a9
 create mode 100755 src/luks/tests/edit-tang-luks2
3700a9
3700a9
diff --git a/src/luks/clevis-luks-common-functions b/src/luks/clevis-luks-common-functions
3700a9
index c9d712a..f3a875c 100644
3700a9
--- a/src/luks/clevis-luks-common-functions
3700a9
+++ b/src/luks/clevis-luks-common-functions
3700a9
@@ -141,11 +141,7 @@ clevis_luks_decode_jwe() {
3700a9
     local jwe="${1}"
3700a9
 
3700a9
     local coded
3700a9
-    if ! coded=$(jose jwe fmt -i- <<< "${jwe}"); then
3700a9
-        return 1
3700a9
-    fi
3700a9
-
3700a9
-    coded=$(jose fmt -j- -g protected -u- <<< "${coded}" | tr -d '"')
3700a9
+    read -r -d . coded <<< "${jwe}"
3700a9
     jose b64 dec -i- <<< "${coded}"
3700a9
 }
3700a9
 
3700a9
@@ -291,6 +287,46 @@ clevis_luks_read_pins_from_slot() {
3700a9
     printf "%s: %s\n" "${SLOT}" "${cfg}"
3700a9
 }
3700a9
 
3700a9
+# clevis_luks_is_key_valid() checks whether the given key is valid to unlock
3700a9
+# the given device.
3700a9
+clevis_luks_is_key_valid() {
3700a9
+    local DEV="${1}"
3700a9
+    local KEY="${2}"
3700a9
+
3700a9
+    if ! cryptsetup open --test-passphrase "${DEV}" \
3700a9
+                         --key-file <(echo -n "${KEY}") 2>/dev/null; then
3700a9
+        return 1
3700a9
+    fi
3700a9
+    return 0
3700a9
+}
3700a9
+
3700a9
+# clevis_luks_unlock_device_by_slot() does the unlock of the device and slot
3700a9
+# passed as parameters and returns the decoded passphrase.
3700a9
+clevis_luks_unlock_device_by_slot() {
3700a9
+    local DEV="${1}"
3700a9
+    local SLT="${2}"
3700a9
+
3700a9
+    [ -z "${DEV}" ] && return 1
3700a9
+    [ -z "${SLT}" ] && return 1
3700a9
+
3700a9
+    local jwe passphrase
3700a9
+    if ! jwe="$(clevis_luks_read_slot "${DEV}" "${SLT}" 2>/dev/null)" \
3700a9
+                || [ -z "${jwe}" ]; then
3700a9
+        return 1
3700a9
+    fi
3700a9
+
3700a9
+    if ! passphrase="$(clevis decrypt < <(echo -n "${jwe}") 2>/dev/null)" \
3700a9
+                       || [ -z "${passphrase}" ]; then
3700a9
+        return 1
3700a9
+    fi
3700a9
+
3700a9
+    if ! clevis_luks_is_key_valid "${DEV}" "${passphrase}"; then
3700a9
+        return 1
3700a9
+    fi
3700a9
+    echo -n "${passphrase}"
3700a9
+    return 0
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
@@ -303,26 +339,15 @@ clevis_luks_unlock_device() {
3700a9
         return 1
3700a9
     fi
3700a9
 
3700a9
-    local slt jwe passphrase
3700a9
+    local slt pt
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
+        if ! pt=$(clevis_luks_unlock_device_by_slot "${DEV}" "${slt}") \
3700a9
+                  || [ -z "${pt}" ]; 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
+        echo -n "${pt}"
3700a9
         return 0
3700a9
     done
3700a9
-
3700a9
     return 1
3700a9
 }
3700a9
 
3700a9
diff --git a/src/luks/clevis-luks-edit b/src/luks/clevis-luks-edit
3700a9
new file mode 100755
3700a9
index 0000000..fc95f75
3700a9
--- /dev/null
3700a9
+++ b/src/luks/clevis-luks-edit
3700a9
@@ -0,0 +1,242 @@
3700a9
+#!/bin/bash -e
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
+
3700a9
+. clevis-luks-common-functions
3700a9
+
3700a9
+SUMMARY="Edit a binding from a clevis-bound slot in a LUKS device"
3700a9
+
3700a9
+usage() {
3700a9
+    echo >&2
3700a9
+    echo "Usage: clevis luks edit [-f] -d DEV -s SLT [-c CONFIG]" >&2
3700a9
+    echo >&2
3700a9
+    echo "$SUMMARY": >&2
3700a9
+    echo >&2
3700a9
+    echo "  -d DEV     The LUKS device to edit clevis-bound pins" >&2
3700a9
+    echo >&2
3700a9
+    echo "  -s SLOT    The slot to use when editing the clevis binding" >&2
3700a9
+    echo >&2
3700a9
+    echo "  -f         Proceed with the edit operation even if the configuration is the same" >&2
3700a9
+    echo >&2
3700a9
+    echo "  -c CONFIG  The updated config to use" >&2
3700a9
+    echo >&2
3700a9
+    exit 1
3700a9
+}
3700a9
+
3700a9
+on_exit() {
3700a9
+    [ -d "$TMP" ] && rm -rf "${TMP}"
3700a9
+}
3700a9
+
3700a9
+validate_cfg() {
3700a9
+    local json="${1}"
3700a9
+    jose fmt -j- -O <<< "${json}" 2>/dev/null
3700a9
+}
3700a9
+
3700a9
+edit_cfg() {
3700a9
+    local cfg_file="${1}"
3700a9
+    local editor="${EDITOR:-vi}"
3700a9
+
3700a9
+    "${editor}" "${cfg_file}" || true
3700a9
+    if ! validate_cfg "$(<"${cfg_file}")"; then
3700a9
+        local ans=
3700a9
+        while true; do
3700a9
+            read -r -p \
3700a9
+              "Malformed configuration. Would you like to edit again? [ynYN] " \
3700a9
+             ans < /dev/tty
3700a9
+
3700a9
+            [[ "${ans}" =~ ^[nN]$ ]] && return 1
3700a9
+            [[ "${ans}" =~ ^[yY]$ ]] && break
3700a9
+        done
3700a9
+        edit_cfg "${cfg_file}"
3700a9
+    fi
3700a9
+    return 0
3700a9
+}
3700a9
+
3700a9
+if [ "${#}" -eq 1 ] && [ "${1}" = "--summary" ]; then
3700a9
+    echo "${SUMMARY}"
3700a9
+    exit 0
3700a9
+fi
3700a9
+
3700a9
+CFG=
3700a9
+FRC=
3700a9
+while getopts ":fd:s:c:" o; do
3700a9
+    case "$o" in
3700a9
+    d) DEV=${OPTARG};;
3700a9
+    s) SLT=${OPTARG};;
3700a9
+    c) CFG=${OPTARG};;
3700a9
+    f) FRC=-f;;
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 [ -z "${SLT}" ]; then
3700a9
+    echo "Did not specify a slot!" >&2
3700a9
+    usage
3700a9
+fi
3700a9
+
3700a9
+if ! binding="$(clevis luks list -d "${DEV}" -s "${SLT}" 2>/dev/null)" \
3700a9
+                || [ -z "${binding}" ]; then
3700a9
+    echo "Error retrieving current configuration from ${DEV}:${SLT}" >&2
3700a9
+    exit 1
3700a9
+fi
3700a9
+
3700a9
+read -r _ pin cfg <<< "${binding}"
3700a9
+# Remove single quotes.
3700a9
+cfg=${cfg//\'}
3700a9
+if ! pretty_cfg="$(jq . <<< "${cfg}")" || [ -z "${pretty_cfg}" ]; then
3700a9
+    echo "Error reading the configuration from ${DEV}:${SLT}" >&2
3700a9
+    exit 1
3700a9
+fi
3700a9
+
3700a9
+if ! TMP="$(mktemp -d)" || [ -z "${TMP}" ]; then
3700a9
+    echo "Creating a temporary dir for editing binding failed" >&2
3700a9
+    exit 1
3700a9
+fi
3700a9
+
3700a9
+trap 'on_exit' EXIT
3700a9
+trap 'on_exit' ERR
3700a9
+
3700a9
+if [ -z "${CFG}" ]; then
3700a9
+   CFG_FILE="${TMP}/cfg"
3700a9
+    echo "${pretty_cfg}" > "${CFG_FILE}"
3700a9
+    if ! edit_cfg "${CFG_FILE}"; then
3700a9
+        exit 1
3700a9
+    fi
3700a9
+
3700a9
+    if ! new_cfg="$(jq . -S < "${CFG_FILE}")" || [ -z "${new_cfg}" ]; then
3700a9
+        echo "Error reading the updated config for ${DEV}:${SLT}" >&2
3700a9
+        exit 1
3700a9
+    fi
3700a9
+else
3700a9
+    if ! validate_cfg "${CFG}"; then
3700a9
+        echo "Invalid configuration given as parameter with -c" >&2
3700a9
+        exit 1
3700a9
+    fi
3700a9
+    new_cfg="$(jq . -S <<< "${CFG}")"
3700a9
+fi
3700a9
+
3700a9
+if [ "${new_cfg}" = "$(jq -S . <<< "${pretty_cfg}")" ] && [ -z "${FRC}" ]; then
3700a9
+    echo "No changes detected; exiting" >&2
3700a9
+    exit 1
3700a9
+fi
3700a9
+
3700a9
+if ! jcfg="$(jose fmt -j- -Oo- <<< "${new_cfg}" 2>/dev/null)" \
3700a9
+             || [ -z "${jcfg}" ]; then
3700a9
+    echo "Error preparing the configuration for the binding update" >&2
3700a9
+    exit 1
3700a9
+fi
3700a9
+
3700a9
+if [ -z "${CFG}" ]; then
3700a9
+    printf "Pin: %s\nNew config:\n%s\n" "${pin}" "${new_cfg}"
3700a9
+    while true; do
3700a9
+        read -r -p \
3700a9
+          "Would you like to proceed with the updated configuration? [ynYN] " \
3700a9
+         ans < /dev/tty
3700a9
+        [[ "${ans}" =~ ^[nN]$ ]] && exit 0
3700a9
+        [[ "${ans}" =~ ^[yY]$ ]] && break
3700a9
+    done
3700a9
+fi
3700a9
+
3700a9
+echo "Updating binding..."
3700a9
+
3700a9
+# Create new key.
3700a9
+if ! new_pass=$(generate_key "${DEV}"); then
3700a9
+    echo "Error generating new key for device ${DEV}" >&2
3700a9
+    exit 1
3700a9
+fi
3700a9
+
3700a9
+# Reencrypt the new password.
3700a9
+if ! jwe="$(clevis encrypt "${pin}" "${jcfg}" -y <<< "${new_pass}")"; then
3700a9
+    echo "Error using pin '${pin}' with config '${jcfg}'" >&2
3700a9
+    exit 1
3700a9
+fi
3700a9
+
3700a9
+# Backup LUKS header.
3700a9
+if ! clevis_luks_backup_dev "${DEV}" "${TMP}"; then
3700a9
+    echo "Error while trying to back up LUKS header from ${DEV}" >&2
3700a9
+    exit 1
3700a9
+fi
3700a9
+
3700a9
+# Get passphrase.
3700a9
+if ! pt="$(clevis_luks_unlock_device "${DEV}")" \
3700a9
+           || [ -z "${pt}" ]; then
3700a9
+    # Unable to retrieve a passphrase from the bindings, so let's query
3700a9
+    # the user.
3700a9
+    read -r -s -p "Enter existing LUKS password: " pt; echo
3700a9
+    # Check if the key is valid.
3700a9
+    if ! clevis_luks_is_key_valid "${DEV}" "${pt}"; then
3700a9
+        echo "The key provided is not valid for ${DEV}" >&2
3700a9
+        exit 1
3700a9
+    fi
3700a9
+fi
3700a9
+
3700a9
+# Check if we can do the update in-place, i.e., if the key we got is the one
3700a9
+# for the slot we are interested in.
3700a9
+in_place=
3700a9
+if cryptsetup open --test-passphrase --key-slot "${SLT}" "${DEV}" \
3700a9
+        <<< "${pt}"; then
3700a9
+    in_place=true
3700a9
+fi
3700a9
+
3700a9
+# Update the key slot with the new key. If we have the key for this slot,
3700a9
+# the change happens in-place. Otherwise, we kill the slot and re-add it.
3700a9
+if [ -n "${in_place}" ]; then
3700a9
+    if ! cryptsetup luksChangeKey "${DEV}" --key-slot "${SLT}" \
3700a9
+            <(echo -n "${new_pass}") <<< "${pt}"; then
3700a9
+        echo "Error updating LUKS passphrase in ${DEV}:${SLT}" >&2
3700a9
+        clevis_luks_restore_dev "${DEV}" "${TMP}"
3700a9
+        exit 1
3700a9
+    fi
3700a9
+else
3700a9
+    if ! cryptsetup luksKillSlot --batch-mode "${DEV}" "${SLT}"; then
3700a9
+        echo "Error wiping slot ${SLT} from ${DEV}" >&2
3700a9
+        clevis_luks_restore_dev "${DEV}" "${TMP}"
3700a9
+        exit 1
3700a9
+    fi
3700a9
+
3700a9
+    if ! echo -n "${new_pass}" \
3700a9
+            | cryptsetup luksAddKey --key-slot "${SLT}" \
3700a9
+                         --key-file <(echo -n "${pt}") "${DEV}"; then
3700a9
+        echo "Error updating LUKS passphrase in ${DEV}:${SLT}." >&2
3700a9
+        clevis_luks_restore_dev "${DEV}" "${TMP}"
3700a9
+        exit 1
3700a9
+    fi
3700a9
+fi
3700a9
+
3700a9
+# Update the metadata.
3700a9
+if ! clevis_luks_save_slot "${DEV}" "${SLT}" "${jwe}" "overwrite"; then
3700a9
+    echo "Error updating metadata in ${DEV}:${SLT}" >&2
3700a9
+    clevis_luks_restore_dev "${DEV}" "${TMP}"
3700a9
+    exit 1
3700a9
+fi
3700a9
+
3700a9
+# Make sure we can unlock the device with this keyslot.
3700a9
+if ! clevis_luks_unlock_device_by_slot "${DEV}" "${SLT}" >/dev/null \
3700a9
+                                       2>/dev/null; then
3700a9
+    echo "Invalid configuration detected. Reverting changes." >&2
3700a9
+    clevis_luks_restore_dev "${DEV}" "${TMP}"
3700a9
+    exit 1
3700a9
+fi
3700a9
+
3700a9
+echo "Binding edited successfully."
3700a9
diff --git a/src/luks/clevis-luks-edit.1.adoc b/src/luks/clevis-luks-edit.1.adoc
3700a9
new file mode 100644
3700a9
index 0000000..454de89
3700a9
--- /dev/null
3700a9
+++ b/src/luks/clevis-luks-edit.1.adoc
3700a9
@@ -0,0 +1,69 @@
3700a9
+CLEVIS-LUKS-EDIT(1)
3700a9
+===================
3700a9
+:doctype: manpage
3700a9
+
3700a9
+
3700a9
+== NAME
3700a9
+
3700a9
+clevis-luks-edit - Edit a binding from a clevis-bound slot in a LUKS device
3700a9
+
3700a9
+== SYNOPSIS
3700a9
+
3700a9
+*clevis luks edit* -d DEV -s SLT [-c CONFIG]
3700a9
+
3700a9
+== OVERVIEW
3700a9
+
3700a9
+The *clevis luks edit* command edits clevis bindings from a LUKS device.
3700a9
+For example:
3700a9
+
3700a9
+    clevis luks edit -d /dev/sda1 -s 1
3700a9
+
3700a9
+== OPTIONS
3700a9
+
3700a9
+* *-d* _DEV_ :
3700a9
+  The LUKS device to edit clevis-bound pins
3700a9
+
3700a9
+* *-s* _SLT_ :
3700a9
+  The slot to use when editing the clevis binding
3700a9
+
3700a9
+* *-f* :
3700a9
+  Proceed with the edit operation even if the config is the same as the current one
3700a9
+
3700a9
+* *-c* _CONFIG_ :
3700a9
+  The updated config to use
3700a9
+
3700a9
+
3700a9
+== EXAMPLES
3700a9
+
3700a9
+    clevis luks list -d /dev/sda1
3700a9
+    1: tang '{"url":"addr"}'
3700a9
+
3700a9
+As we can see in the example above, */dev/sda1* has one slots bound, in this case, to a _tang_ pin.
3700a9
+
3700a9
+We can edit this binding by issuing the following command:
3700a9
+
3700a9
+    clevis luks edit -d /dev/sda1 -s 1
3700a9
+
3700a9
+This will open a text editor -- the one set in the $EDITOR environment variable, or _vi_, as a fallback -- with the current
3700a9
+configuration of this binding to be edited. In this case, we should have the following:
3700a9
+
3700a9
+    {
3700a9
+        "url": "addr"
3700a9
+    }
3700a9
+
3700a9
+Once at the editor, we can edit the pin configuration. For _tang_, we could edit the _url_, for instance. After completing the change,
3700a9
+save the file and exit. The updated configuration will be validated for JSON, and if there are no errors, you will be shown the
3700a9
+updated configuration and prompted whether to proceed.
3700a9
+
3700a9
+By proceeding, the binding will be updated. There may be required to provide a valid LUKS passphrase for the device.
3700a9
+
3700a9
+In the second example, we will update the same device and slot, but we will be providing the updated configuration as well:
3700a9
+
3700a9
+    clevis luks edit -d /dev/sda1 -s 1 -c '{"url":"new-addr-here"}'
3700a9
+
3700a9
+In this case, the binding update will be done in non-interactive mode, however it also may be required to provide a valud LUKS
3700a9
+passphrase for the device.
3700a9
+
3700a9
+== SEE ALSO
3700a9
+
3700a9
+link:clevis-luks-list.1.adoc[*clevis-luks-list*(1)],
3700a9
diff --git a/src/luks/clevis-luks-regen b/src/luks/clevis-luks-regen
3700a9
index 6071d85..8f32e08 100755
3700a9
--- a/src/luks/clevis-luks-regen
3700a9
+++ b/src/luks/clevis-luks-regen
3700a9
@@ -41,15 +41,7 @@ function usage_and_exit () {
3700a9
     exit "${1}"
3700a9
 }
3700a9
 
3700a9
-on_exit() {
3700a9
-    if [ ! -d "${TMP}" ] || ! rm -rf "${TMP}"; then
3700a9
-        echo "Delete temporary files failed!" >&2
3700a9
-        echo "You need to clean up: ${TMP}" >&2
3700a9
-        exit 1
3700a9
-    fi
3700a9
-}
3700a9
-
3700a9
-while getopts ":hfd:s:" o; do
3700a9
+while getopts ":hd:s:" o; do
3700a9
     case "$o" in
3700a9
     d) DEV="$OPTARG";;
3700a9
     h) usage_and_exit 0;;
3700a9
@@ -68,117 +60,22 @@ if [ -z "$SLT" ]; then
3700a9
     exit 1
3700a9
 fi
3700a9
 
3700a9
-### ----------------------------------------------------------------------
3700a9
-if ! pin_cfg=$(clevis luks list -d "${DEV}" -s "${SLT}" 2>/dev/null); then
3700a9
-    echo "Error obtaining current configuration of device ${DEV}:${SLT}" >&2
3700a9
+if ! binding="$(clevis luks list -d "${DEV}" -s "${SLT}" 2>/dev/null)" \
3700a9
+                || [ -z "${binding}" ]; then
3700a9
+    echo "Error retrieving current configuration from ${DEV}:${SLT}" >&2
3700a9
     exit 1
3700a9
 fi
3700a9
 
3700a9
-PIN=$(echo "${pin_cfg}" | awk '{ print $2 }')
3700a9
-CFG=$(echo "${pin_cfg}" | awk '{ print $3 }' | tr -d "'")
3700a9
+read -r _ pin cfg <<< "${binding}"
3700a9
 
3700a9
 echo "Regenerating with:"
3700a9
-echo "PIN: $PIN"
3700a9
-echo "CONFIG: $CFG"
3700a9
-
3700a9
-trap 'echo "Ignoring CONTROL-C!"' INT TERM
3700a9
-
3700a9
-# Get the existing key.
3700a9
-if ! existing_key=$(clevis luks pass -d "${DEV}" -s "${SLT}" 2>/dev/null); then
3700a9
-    # We failed to obtain the passphrase for the slot -- perhaps
3700a9
-    # it was rotated? --, so let's request user input.
3700a9
-    read -r -s -p "Enter existing LUKS password: " existing_key; echo
3700a9
-fi
3700a9
-
3700a9
-# Check if the key is valid.
3700a9
-if ! cryptsetup open --test-passphrase "${DEV}" <<< "${existing_key}"; then
3700a9
-    exit 1
3700a9
-fi
3700a9
-
3700a9
-# Check if we can do the update in-place, i.e., if the key we got is the one
3700a9
-# for the slot we are interested in.
3700a9
-in_place=
3700a9
-if cryptsetup open --test-passphrase --key-slot "${SLT}" "${DEV}" \
3700a9
-        <<< "${existing_key}"; then
3700a9
-    in_place=true
3700a9
-fi
3700a9
-
3700a9
-# Create new key.
3700a9
-if ! new_passphrase=$(generate_key "${DEV}"); then
3700a9
-    echo "Error generating new key for device ${DEV}" >&2
3700a9
-    exit 1
3700a9
-fi
3700a9
-
3700a9
-# Reencrypt the new password.
3700a9
-if ! jwe="$(clevis encrypt "${PIN}" "${CFG}" <<< "${new_passphrase}")"; then
3700a9
-    echo "Error using pin '${PIN}' with config '${CFG}'" >&2
3700a9
-    exit 1
3700a9
-fi
3700a9
-
3700a9
-# Updating the metadata and the actual passphrase are destructive operations,
3700a9
-# hence we will do a backup of the LUKS header and restore it later in case
3700a9
-# we have issues performing these operations.
3700a9
-if ! TMP="$(mktemp -d)"; then
3700a9
-    echo "Creating a temporary dir for device backup/restore failed!" >&2
3700a9
-    exit 1
3700a9
-fi
3700a9
-trap 'on_exit' EXIT
3700a9
-
3700a9
-# Backup LUKS header.
3700a9
-if ! clevis_luks_backup_dev "${DEV}" "${TMP}"; then
3700a9
-    echo "Error while trying to back up LUKS header from ${DEV}" >&2
3700a9
-    exit 1
3700a9
-fi
3700a9
-
3700a9
-restore_device() {
3700a9
-    local DEV="${1}"
3700a9
-    local TMP="${2}"
3700a9
-
3700a9
-    if ! clevis_luks_restore_dev "${DEV}" "${TMP}"; then
3700a9
-        echo "Error while trying to restore LUKS header from ${DEV}." >&2
3700a9
-    else
3700a9
-        echo "LUKS header restored successfully." >&2
3700a9
-    fi
3700a9
-}
3700a9
-
3700a9
-# Update the key slot with the new key. If we have the key for this slot,
3700a9
-# the change happens in-place. Otherwise, we kill the slot and re-add it.
3700a9
-if [ -n "${in_place}" ]; then
3700a9
-    if ! cryptsetup luksChangeKey "${DEV}" --key-slot "${SLT}" \
3700a9
-            <(echo -n "${new_passphrase}") <<< "${existing_key}"; then
3700a9
-        echo "Error updating LUKS passphrase in ${DEV}:${SLT}" >&2
3700a9
-        restore_device "${DEV}" "${TMP}"
3700a9
-        exit 1
3700a9
-    fi
3700a9
-else
3700a9
-    if ! cryptsetup luksKillSlot --batch-mode "${DEV}" "${SLT}"; then
3700a9
-        echo "Error wiping slot ${SLT} from ${DEV}" >&2
3700a9
-        restore_device "${DEV}" "${TMP}"
3700a9
-        exit 1
3700a9
-    fi
3700a9
-
3700a9
-    if ! echo -n "${new_passphrase}" \
3700a9
-            | cryptsetup luksAddKey --key-slot "${SLT}" \
3700a9
-                         --key-file <(echo -n "${existing_key}") "${DEV}"; then
3700a9
-        echo "Error updating LUKS passphrase in ${DEV}:${SLT}." >&2
3700a9
-        restore_device "${DEV}" "${TMP}"
3700a9
-        exit 1
3700a9
-    fi
3700a9
-fi
3700a9
-
3700a9
-# Update the metadata.
3700a9
-if ! clevis_luks_save_slot "${DEV}" "${SLT}" "${jwe}" "overwrite"; then
3700a9
-    echo "Error updating metadata in ${DEV}:${SLT}" >&2
3700a9
-    restore_device "${DEV}" "${TMP}"
3700a9
-    exit 1
3700a9
-fi
3700a9
+echo "PIN: ${pin}"
3700a9
+echo "CONFIG: ${cfg}"
3700a9
 
3700a9
-# Now make sure that we can unlock this device after the change.
3700a9
-# If we can't, undo the changes.
3700a9
-if ! cryptsetup open --test-passphrase --key-slot "${SLT}" "${DEV}" 2>/dev/null \
3700a9
-        <<< "$(clevis luks pass -d "${DEV}" -s "${SLT}" 2>/dev/null)"; then
3700a9
-    echo "Invalid configuration detected after rebinding. Reverting changes."
3700a9
-    restore_device "${DEV}" "${TMP}"
3700a9
+# Remove single quotes.
3700a9
+cfg=${cfg//\'}
3700a9
+if ! clevis luks edit -f -d "${DEV}" -s "${SLT}" -c "${cfg}" >/dev/null; then
3700a9
+    echo "Error rotating keys in ${DEV}:${SLT}" >&2
3700a9
     exit 1
3700a9
 fi
3700a9
 
3700a9
diff --git a/src/luks/meson.build b/src/luks/meson.build
3700a9
index ee588c3..ba02bd9 100644
3700a9
--- a/src/luks/meson.build
3700a9
+++ b/src/luks/meson.build
3700a9
@@ -54,6 +54,9 @@ if libcryptsetup.found() and luksmeta.found() and pwmake.found()
3700a9
   bins += join_paths(meson.current_source_dir(), 'clevis-luks-report-sss')
3700a9
   bins += join_paths(meson.current_source_dir(), 'clevis-luks-report-tang')
3700a9
   mans += join_paths(meson.current_source_dir(), 'clevis-luks-report.1')
3700a9
+
3700a9
+  bins += join_paths(meson.current_source_dir(), 'clevis-luks-edit')
3700a9
+  mans += join_paths(meson.current_source_dir(), 'clevis-luks-edit.1')
3700a9
 else
3700a9
   warning('Will not install LUKS support due to missing dependencies!')
3700a9
 endif
3700a9
diff --git a/src/luks/tests/edit-tang-luks1 b/src/luks/tests/edit-tang-luks1
3700a9
new file mode 100755
3700a9
index 0000000..3d42d68
3700a9
--- /dev/null
3700a9
+++ b/src/luks/tests/edit-tang-luks1
3700a9
@@ -0,0 +1,106 @@
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
+    local d
3700a9
+    for d in "${TMP}" "${TMP2}"; do
3700a9
+        [ ! -d "${d}" ] && continue
3700a9
+        tang_stop "${d}"
3700a9
+        rm -rf "${d}"
3700a9
+    done
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
+
3700a9
+cfg=$(printf '{"url":"%s"}' "${url}")
3700a9
+
3700a9
+# LUKS1.
3700a9
+DEV="${TMP}/luks1-device"
3700a9
+new_device "luks1" "${DEV}"
3700a9
+
3700a9
+if ! clevis luks bind -y -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
3700a9
+    error "${TEST}: Bind should have succeeded."
3700a9
+fi
3700a9
+
3700a9
+# Now let's try to change the config but using the same one we already have.
3700a9
+if clevis luks edit -d "${DEV}" -s 1 -c "${cfg}"; then
3700a9
+    error "${TEST}: edit should not have succeeded because the config is the same."
3700a9
+fi
3700a9
+
3700a9
+# And now, just a broken config.
3700a9
+new_cfg=$(printf '{"url&:"%s"}' "${url}")
3700a9
+if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
3700a9
+    error "${TEST}: edit should have failed because of invalid JSON"
3700a9
+fi
3700a9
+
3700a9
+# Now let's have another tang instance running and change the config to use
3700a9
+# the new one.
3700a9
+port2=$(get_random_port)
3700a9
+TMP2="$(mktemp -d)"
3700a9
+tang_run "${TMP2}" "${port2}" &
3700a9
+tang_wait_until_ready "${port2}"
3700a9
+new_url="http://${TANG_HOST}:${port2}"
3700a9
+new_cfg=$(printf '{"url":"%s"}' "${new_url}")
3700a9
+
3700a9
+if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
3700a9
+    error "${TEST}: edit should have succeeded."
3700a9
+fi
3700a9
+
3700a9
+# And now let's use sss and start with a single tang server, then add a second
3700a9
+# one.
3700a9
+new_device "luks1" "${DEV}"
3700a9
+cfg=$(printf '{"t":1,"pins":{"tang":[{"url":"%s"}]}}' "${url}")
3700a9
+if ! clevis luks bind -y -d "${DEV}" sss "${cfg}" <<< "${DEFAULT_PASS}"; then
3700a9
+    error "${TEST}: Bind should have succeeded."
3700a9
+fi
3700a9
+new_cfg=$(printf '{"t":1,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
3700a9
+          "${url}" "${new_url}")
3700a9
+
3700a9
+if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
3700a9
+    error "${TEST}: edit should have succeeded and added a new tang server"
3700a9
+fi
3700a9
+
3700a9
+# Now let's change the threshold to 2.
3700a9
+new_cfg=$(printf '{"t":2,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
3700a9
+          "${url}" "${new_url}")
3700a9
+
3700a9
+if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
3700a9
+    error "${TEST}: edit should have succeeded and added a new tang server"
3700a9
+fi
3700a9
+
3700a9
+# And finally, let's try a broken config, with a wrong threshold.
3700a9
+new_cfg=$(printf '{"t":3,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
3700a9
+          "${url}" "${new_url}")
3700a9
+if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
3700a9
+    error "${TEST}: edit should have failed because threshold > number of servers"
3700a9
+fi
3700a9
diff --git a/src/luks/tests/edit-tang-luks2 b/src/luks/tests/edit-tang-luks2
3700a9
new file mode 100755
3700a9
index 0000000..9000053
3700a9
--- /dev/null
3700a9
+++ b/src/luks/tests/edit-tang-luks2
3700a9
@@ -0,0 +1,106 @@
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
+    local d
3700a9
+    for d in "${TMP}" "${TMP2}"; do
3700a9
+        [ ! -d "${d}" ] && continue
3700a9
+        tang_stop "${d}"
3700a9
+        rm -rf "${d}"
3700a9
+    done
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
+
3700a9
+cfg=$(printf '{"url":"%s"}' "${url}")
3700a9
+
3700a9
+# LUKS2.
3700a9
+DEV="${TMP}/luks2-device"
3700a9
+new_device "luks2" "${DEV}"
3700a9
+
3700a9
+if ! clevis luks bind -y -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
3700a9
+    error "${TEST}: Bind should have succeeded."
3700a9
+fi
3700a9
+
3700a9
+# Now let's try to change the config but using the same one we already have.
3700a9
+if clevis luks edit -d "${DEV}" -s 1 -c "${cfg}"; then
3700a9
+    error "${TEST}: edit should not have succeeded because the config is the same."
3700a9
+fi
3700a9
+
3700a9
+# And now, just a broken config.
3700a9
+new_cfg=$(printf '{"url&:"%s"}' "${url}")
3700a9
+if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
3700a9
+    error "${TEST}: edit should have failed because of invalid JSON"
3700a9
+fi
3700a9
+
3700a9
+# Now let's have another tang instance running and change the config to use
3700a9
+# the new one.
3700a9
+port2=$(get_random_port)
3700a9
+TMP2="$(mktemp -d)"
3700a9
+tang_run "${TMP2}" "${port2}" &
3700a9
+tang_wait_until_ready "${port2}"
3700a9
+new_url="http://${TANG_HOST}:${port2}"
3700a9
+new_cfg=$(printf '{"url":"%s"}' "${new_url}")
3700a9
+
3700a9
+if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
3700a9
+    error "${TEST}: edit should have succeeded."
3700a9
+fi
3700a9
+
3700a9
+# And now let's use sss and start with a single tang server, then add a second
3700a9
+# one.
3700a9
+new_device "luks2" "${DEV}"
3700a9
+cfg=$(printf '{"t":1,"pins":{"tang":[{"url":"%s"}]}}' "${url}")
3700a9
+if ! clevis luks bind -y -d "${DEV}" sss "${cfg}" <<< "${DEFAULT_PASS}"; then
3700a9
+    error "${TEST}: Bind should have succeeded."
3700a9
+fi
3700a9
+new_cfg=$(printf '{"t":1,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
3700a9
+          "${url}" "${new_url}")
3700a9
+
3700a9
+if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
3700a9
+    error "${TEST}: edit should have succeeded and added a new tang server"
3700a9
+fi
3700a9
+
3700a9
+# Now let's change the threshold to 2.
3700a9
+new_cfg=$(printf '{"t":2,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
3700a9
+          "${url}" "${new_url}")
3700a9
+
3700a9
+if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
3700a9
+    error "${TEST}: edit should have succeeded and added a new tang server"
3700a9
+fi
3700a9
+
3700a9
+# And finally, let's try a broken config, with a wrong threshold.
3700a9
+new_cfg=$(printf '{"t":3,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
3700a9
+          "${url}" "${new_url}")
3700a9
+if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
3700a9
+    error "${TEST}: edit should have failed because threshold > number of servers"
3700a9
+fi
3700a9
diff --git a/src/luks/tests/meson.build b/src/luks/tests/meson.build
3700a9
index 4795488..67fa597 100644
3700a9
--- a/src/luks/tests/meson.build
3700a9
+++ b/src/luks/tests/meson.build
3700a9
@@ -4,8 +4,9 @@ actv = find_program(
3700a9
   required: false
3700a9
 )
3700a9
 
3700a9
-# We use jq for comparing the pin config in the clevis luks list tests.
3700a9
-jq = find_program('jq', required: false)
3700a9
+# We use jq for comparing the pin config in the clevis luks list tests
3700a9
+# and also in clevis luks edit.
3700a9
+jq = find_program('jq', required: true)
3700a9
 
3700a9
 # we use systemd-socket-activate for running test tang servers.
3700a9
 actv = find_program(
3700a9
@@ -74,19 +75,16 @@ test('bind-pass-with-newline', find_program('bind-pass-with-newline-luks1'), env
3700a9
 test('bind-pass-with-newline-keyfile', find_program('bind-pass-with-newline-keyfile-luks1'), env: env)
3700a9
 # Bug #70.
3700a9
 test('bind-already-used-luksmeta-slot', find_program('bind-already-used-luksmeta-slot'), env: env, timeout: 60)
3700a9
-
3700a9
-if jq.found()
3700a9
-  test('list-recursive-luks1', find_program('list-recursive-luks1'), env: env)
3700a9
-  test('list-tang-luks1', find_program('list-tang-luks1'), env: env)
3700a9
-  test('list-sss-tang-luks1', find_program('list-sss-tang-luks1'), env: env)
3700a9
-else
3700a9
-  warning('Will not run "clevis luks list" tests due to missing jq dependency')
3700a9
-endif
3700a9
+test('list-recursive-luks1', find_program('list-recursive-luks1'), env: env)
3700a9
+test('list-tang-luks1', find_program('list-tang-luks1'), env: env)
3700a9
+test('list-sss-tang-luks1', find_program('list-sss-tang-luks1'), env: env)
3700a9
 
3700a9
 if has_tang
3700a9
   test('unlock-tang-luks1', find_program('unlock-tang-luks1'), env: env, timeout: 90)
3700a9
   test('assume-yes-luks1', find_program('assume-yes-luks1'), env: env)
3700a9
+  test('edit-tang-luks1', find_program('edit-tang-luks1'), env: env, timeout: 90)
3700a9
 endif
3700a9
+
3700a9
 test('pass-tang-luks1', find_program('pass-tang-luks1'), env: env)
3700a9
 test('backup-restore-luks1', find_program('backup-restore-luks1'), env: env)
3700a9
 test('regen-inplace-luks1', find_program('regen-inplace-luks1'), env: env, timeout: 90)
3700a9
@@ -100,16 +98,14 @@ if luksmeta_data.get('OLD_CRYPTSETUP') == '0'
3700a9
   test('bind-luks2', find_program('bind-luks2'), env: env, timeout: 60)
3700a9
   test('unbind-unbound-slot-luks2', find_program('unbind-unbound-slot-luks2'), env: env)
3700a9
   test('unbind-luks2', find_program('unbind-luks2'), env: env, timeout: 60)
3700a9
-
3700a9
-  if jq.found()
3700a9
-    test('list-recursive-luks2', find_program('list-recursive-luks2'), env: env, timeout: 60)
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
+  test('list-recursive-luks2', find_program('list-recursive-luks2'), env: env, timeout: 60)
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
 
3700a9
   if has_tang
3700a9
     test('unlock-tang-luks2', find_program('unlock-tang-luks2'), env: env, timeout: 120)
3700a9
     test('assume-yes-luks2', find_program('assume-yes-luks2'), env: env, timeout: 60)
3700a9
+    test('edit-tang-luks2', find_program('edit-tang-luks2'), env: env, timeout: 120)
3700a9
   endif
3700a9
   test('pass-tang-luks2', find_program('pass-tang-luks2'), env: env, timeout: 60)
3700a9
   test('backup-restore-luks2', find_program('backup-restore-luks2'), env:env, timeout: 90)
3700a9
diff --git a/src/luks/tests/regen-inplace-luks1 b/src/luks/tests/regen-inplace-luks1
3700a9
index 3a42ced..32072c4 100755
3700a9
--- a/src/luks/tests/regen-inplace-luks1
3700a9
+++ b/src/luks/tests/regen-inplace-luks1
3700a9
@@ -76,7 +76,7 @@ fi
3700a9
 old_key=$(clevis luks pass -d "${DEV}" -s "${SLT}")
3700a9
 
3700a9
 # Now let's try regen.
3700a9
-if ! clevis_regen "${DEV}" "${SLT}" "${DEFAULT_PASS}"; then
3700a9
+if ! clevis luks regen -d "${DEV}" -s "${SLT}"; then
3700a9
     error "${TEST}: clevis luks regen failed"
3700a9
 fi
3700a9
 
3700a9
diff --git a/src/luks/tests/regen-inplace-luks2 b/src/luks/tests/regen-inplace-luks2
3700a9
index 1cb7a29..d1bc182 100755
3700a9
--- a/src/luks/tests/regen-inplace-luks2
3700a9
+++ b/src/luks/tests/regen-inplace-luks2
3700a9
@@ -77,7 +77,7 @@ fi
3700a9
 old_key=$(clevis luks pass -d "${DEV}" -s "${SLT}")
3700a9
 
3700a9
 # Now let's try regen.
3700a9
-if ! clevis_regen "${DEV}" "${SLT}" "${DEFAULT_PASS}"; then
3700a9
+if ! clevis luks regen -d "${DEV}" -s "${SLT}"; then
3700a9
     error "${TEST}: clevis luks regen failed"
3700a9
 fi
3700a9
 
3700a9
diff --git a/src/luks/tests/regen-not-inplace-luks1 b/src/luks/tests/regen-not-inplace-luks1
3700a9
index 1b65ca7..fba61dc 100755
3700a9
--- a/src/luks/tests/regen-not-inplace-luks1
3700a9
+++ b/src/luks/tests/regen-not-inplace-luks1
3700a9
@@ -79,7 +79,7 @@ if [ "${enabled}" -ne 2 ]; then
3700a9
 fi
3700a9
 
3700a9
 # Now let's try regen.
3700a9
-if ! clevis_regen "${DEV}" "${SLT}" "${DEFAULT_PASS}"; then
3700a9
+if ! clevis luks regen -d "${DEV}" -s "${SLT}" <<< "${DEFAULT_PASS}"; then
3700a9
     error "${TEST}: clevis luks regen failed"
3700a9
 fi
3700a9
 
3700a9
diff --git a/src/luks/tests/regen-not-inplace-luks2 b/src/luks/tests/regen-not-inplace-luks2
3700a9
index dc91449..6e3b012 100755
3700a9
--- a/src/luks/tests/regen-not-inplace-luks2
3700a9
+++ b/src/luks/tests/regen-not-inplace-luks2
3700a9
@@ -80,7 +80,7 @@ if [ "${enabled}" -ne 2 ]; then
3700a9
 fi
3700a9
 
3700a9
 # Now let's try regen.
3700a9
-if ! clevis_regen "${DEV}" "${SLT}" "${DEFAULT_PASS}"; then
3700a9
+if ! clevis luks regen -d "${DEV}" -s "${SLT}" <<< "${DEFAULT_PASS}"; then
3700a9
     error "${TEST}: clevis luks regen failed"
3700a9
 fi
3700a9
 
3700a9
diff --git a/src/luks/tests/tests-common-functions.in b/src/luks/tests/tests-common-functions.in
3700a9
index 6101f28..7b3fdad 100755
3700a9
--- a/src/luks/tests/tests-common-functions.in
3700a9
+++ b/src/luks/tests/tests-common-functions.in
3700a9
@@ -229,31 +229,5 @@ tang_get_adv() {
3700a9
     curl -o "${adv}" http://"${TANG_HOST}":"${port}"/adv
3700a9
 }
3700a9
 
3700a9
-# Regenerate binding.
3700a9
-clevis_regen() {
3700a9
-    local DEV="${1}"
3700a9
-    local SLT="${2}"
3700a9
-    local PASS="${3}"
3700a9
-
3700a9
-    expect -d << CLEVIS_REGEN
3700a9
-        set timeout 120
3700a9
-        spawn sh -c "clevis luks regen -d $DEV -s $SLT"
3700a9
-        expect {
3700a9
-            "Enter existing LUKS password" {
3700a9
-                send "$PASS\r"
3700a9
-                exp_continue
3700a9
-            }
3700a9
-            "Do you wish to trust these keys" {
3700a9
-                send "y\r"
3700a9
-                exp_continue
3700a9
-            }
3700a9
-            expect eof
3700a9
-            wait
3700a9
-        }
3700a9
-CLEVIS_REGEN
3700a9
-    ret=$?
3700a9
-    return "${ret}"
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
index 841ba01..6ede47b 100755
3700a9
--- a/src/luks/tests/unlock-tang-luks1
3700a9
+++ b/src/luks/tests/unlock-tang-luks1
3700a9
@@ -31,6 +31,10 @@ on_exit() {
3700a9
 trap 'on_exit' EXIT
3700a9
 trap 'on_exit' ERR
3700a9
 
3700a9
+# LUKS1.
3700a9
+DEV="${TMP}/luks1-device"
3700a9
+new_device "luks1" "${DEV}"
3700a9
+
3700a9
 TMP="$(mktemp -d)"
3700a9
 
3700a9
 port=$(get_random_port)
3700a9
@@ -43,9 +47,6 @@ 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
diff --git a/src/luks/tests/unlock-tang-luks2 b/src/luks/tests/unlock-tang-luks2
3700a9
index 81822fb..b32c5aa 100755
3700a9
--- a/src/luks/tests/unlock-tang-luks2
3700a9
+++ b/src/luks/tests/unlock-tang-luks2
3700a9
@@ -31,6 +31,10 @@ on_exit() {
3700a9
 trap 'on_exit' EXIT
3700a9
 trap 'on_exit' ERR
3700a9
 
3700a9
+# LUKS2.
3700a9
+DEV="${TMP}/luks2-device"
3700a9
+new_device "luks2" "${DEV}"
3700a9
+
3700a9
 TMP="$(mktemp -d)"
3700a9
 
3700a9
 port=$(get_random_port)
3700a9
@@ -43,10 +47,6 @@ 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
2.18.4
3700a9