Blame SOURCES/Add-support-for-listing-existing-PBD-policies-in-pla.patch

524513
From a128130755bcd893ccf1d70b52c13fbaf29613c9 Mon Sep 17 00:00:00 2001
524513
From: Sergio Correia <scorreia@redhat.com>
524513
Date: Sat, 30 Nov 2019 14:26:59 -0500
524513
Subject: [PATCH] Add clevis luks list command
524513
524513
Usage:
524513
clevis luks list -d DEV [-s SLT]
524513
524513
Examples:
524513
524513
clevis luks list -d device
524513
1: sss '{"t":1,"pins":{"tang":[{"url":"addr1"},{"url":"addr2"}],"tpm2":[{"hash":"sha256","key":"ecc"}],"sss":{"t":1,"pins":{"tang":[{"url":"addr3"}]}}}}'
524513
2: tang '{"url":"addr"}'
524513
3: tpm2 '{"hash":"sha256","key":"ecc","pcr_bank":"sha1","pcr_ids":"7"}'
524513
524513
clevis luks list -d device -s 3
524513
3: tpm2 '{"hash":"sha256","key":"ecc","pcr_bank":"sha1","pcr_ids":"7"}'
524513
---
524513
 src/luks/clevis-luks-common-functions | 173 ++++++++++++++++++++++++++
524513
 src/luks/clevis-luks-list             |  77 ++++++++++++
524513
 src/luks/clevis-luks-list.1.adoc      |  58 +++++++++
524513
 src/luks/meson.build                  |   8 +-
524513
 src/luks/tests/list-recursive-luks1   |  85 +++++++++++++
524513
 src/luks/tests/list-recursive-luks2   |  85 +++++++++++++
524513
 src/luks/tests/list-sss-tang-luks1    |  77 ++++++++++++
524513
 src/luks/tests/list-sss-tang-luks2    |  77 ++++++++++++
524513
 src/luks/tests/list-tang-luks1        |  64 ++++++++++
524513
 src/luks/tests/list-tang-luks2        |  64 ++++++++++
524513
 src/luks/tests/meson.build            |  36 ++++++
524513
 src/luks/tests/tests-common-functions |  76 +++++++++++
524513
 12 files changed, 879 insertions(+), 1 deletion(-)
524513
 create mode 100755 src/luks/clevis-luks-list
524513
 create mode 100644 src/luks/clevis-luks-list.1.adoc
524513
 create mode 100755 src/luks/tests/list-recursive-luks1
524513
 create mode 100755 src/luks/tests/list-recursive-luks2
524513
 create mode 100755 src/luks/tests/list-sss-tang-luks1
524513
 create mode 100755 src/luks/tests/list-sss-tang-luks2
524513
 create mode 100755 src/luks/tests/list-tang-luks1
524513
 create mode 100755 src/luks/tests/list-tang-luks2
524513
 create mode 100644 src/luks/tests/meson.build
524513
 create mode 100644 src/luks/tests/tests-common-functions
524513
524513
diff --git a/src/luks/clevis-luks-common-functions b/src/luks/clevis-luks-common-functions
524513
index d676253..9ba1812 100644
524513
--- a/src/luks/clevis-luks-common-functions
524513
+++ b/src/luks/clevis-luks-common-functions
524513
@@ -141,3 +141,176 @@ findexe() {
524513
     return 1
524513
 }
524513
 
524513
+# clevis_luks_used_slots() will return the list of used slots for a given LUKS
524513
+# device.
524513
+clevis_luks_used_slots() {
524513
+    local DEV="${1}"
524513
+
524513
+    local slots
524513
+    if cryptsetup isLuks --type luks1 "${DEV}"; then
524513
+        readarray -t slots < <(cryptsetup luksDump "${DEV}" \
524513
+            | sed -rn 's|^Key Slot ([0-7]): ENABLED$|\1|p')
524513
+    elif cryptsetup isLuks --type luks2 "${DEV}"; then
524513
+        readarray -t slots < <(cryptsetup luksDump "${DEV}" \
524513
+            | sed -rn 's|^\s+([0-9]+): luks2$|\1|p')
524513
+    else
524513
+        echo "${DEV} is not a supported LUKS device!" >&2
524513
+        return 1
524513
+    fi
524513
+    echo "${slots[@]}"
524513
+}
524513
+
524513
+# clevis_luks_decode_jwe() will decode a given JWE.
524513
+clevis_luks_decode_jwe() {
524513
+    local jwe="${1}"
524513
+
524513
+    local coded
524513
+    if ! coded=$(jose jwe fmt -i- <<< "${jwe}"); then
524513
+        return 1
524513
+    fi
524513
+
524513
+    coded=$(jose fmt -j- -g protected -u- <<< "${coded}" | tr -d '"')
524513
+    jose b64 dec -i- <<< "${coded}"
524513
+}
524513
+
524513
+# clevis_luks_print_pin_config() will print the config of a given pin; i.e.
524513
+# for tang it will display the associated url address, and for tpm2, the
524513
+# properties in place, like the hash, for instance.
524513
+clevis_luks_print_pin_config() {
524513
+    local P="${1}"
524513
+    local decoded="${2}"
524513
+
524513
+    local content
524513
+    if ! content="$(jose fmt -j- -g clevis -g "${P}" -o- <<< "${decoded}")" \
524513
+                    || [[ -z "${content}" ]]; then
524513
+        return 1
524513
+    fi
524513
+
524513
+    local pin=
524513
+    case "${P}" in
524513
+    tang)
524513
+        local url
524513
+        url="$(jose fmt -j- -g url -u- <<< "${content}")"
524513
+        pin=$(printf '{"url":"%s"}' "${url}")
524513
+        printf "tang '%s'" "${pin}"
524513
+        ;;
524513
+    tpm2)
524513
+        # Valid properties for tpm2 pin are the following:
524513
+        # hash, key, pcr_bank, pcr_ids, pcr_digest.
524513
+        local key
524513
+        local value
524513
+        for key in 'hash' 'key' 'pcr_bank' 'pcr_ids' 'pcr_digest'; do
524513
+            if value=$(jose fmt -j- -g "${key}" -u- <<< "${content}"); then
524513
+                pin=$(printf '%s,"%s":"%s"' "${pin}" "${key}" "${value}")
524513
+            fi
524513
+        done
524513
+        # Remove possible leading comma.
524513
+        pin=${pin/#,/}
524513
+        printf "tpm2 '{%s}'" "${pin}"
524513
+        ;;
524513
+    sss)
524513
+        local threshold
524513
+        threshold=$(jose fmt -j- -Og t -o- <<< "${content}")
524513
+        clevis_luks_process_sss_pin "${content}" "${threshold}"
524513
+        ;;
524513
+    *)
524513
+        printf "unknown pin '%s'" "${P}"
524513
+        ;;
524513
+    esac
524513
+}
524513
+
524513
+# clevis_luks_decode_pin_config() will receive a JWE and extract a pin config
524513
+# from it.
524513
+clevis_luks_decode_pin_config() {
524513
+    local jwe="${1}"
524513
+
524513
+    local decoded
524513
+    if ! decoded=$(clevis_luks_decode_jwe "${jwe}"); then
524513
+        return 1
524513
+    fi
524513
+
524513
+    local P
524513
+    if ! P=$(jose fmt -j- -Og clevis -g pin -u- <<< "${decoded}"); then
524513
+        return 1
524513
+    fi
524513
+
524513
+    clevis_luks_print_pin_config "${P}" "${decoded}"
524513
+}
524513
+
524513
+# clevis_luks_join_sss_cfg() will receive a list of configurations for a given
524513
+# pin and returns it as list, in the format PIN [cfg1, cfg2, ..., cfgN].
524513
+clevis_luks_join_sss_cfg() {
524513
+    local pin="${1}"
524513
+    local cfg="${2}"
524513
+    cfg=$(echo "${cfg}" | tr -d "'" | sed -e 's/^,//')
524513
+    printf '"%s":[%s]' "${pin}" "${cfg}"
524513
+}
524513
+
524513
+# clevis_luks_process_sss_pin() will receive a JWE with information on the sss
524513
+# pin config, and also its associated threshold, and will extract the info.
524513
+clevis_luks_process_sss_pin() {
524513
+    local jwe="${1}"
524513
+    local threshold="${2}"
524513
+
524513
+    local sss_tang
524513
+    local sss_tpm2
524513
+    local sss
524513
+    local pin_cfg
524513
+    local pin
524513
+    local cfg
524513
+
524513
+    local coded
524513
+    for coded in $(jose fmt -j- -Og jwe -Af- <<< "${jwe}"| tr -d '"'); do
524513
+        if ! pin_cfg="$(clevis_luks_decode_pin_config "${coded}")"; then
524513
+            continue
524513
+        fi
524513
+        read -r pin cfg <<< "${pin_cfg}"
524513
+        case "${pin}" in
524513
+        tang)
524513
+            sss_tang="${sss_tang},${cfg}"
524513
+            ;;
524513
+        tpm2)
524513
+            sss_tpm2="${sss_tpm2},${cfg}"
524513
+            ;;
524513
+        sss)
524513
+            sss=$(echo "${cfg}" | tr -d "'")
524513
+            ;;
524513
+        esac
524513
+    done
524513
+
524513
+    cfg=
524513
+    if [[ -n "${sss_tang}" ]]; then
524513
+        cfg=$(clevis_luks_join_sss_cfg "tang" "${sss_tang}")
524513
+    fi
524513
+
524513
+    if [[ -n "${sss_tpm2}" ]]; then
524513
+        cfg="${cfg},"$(clevis_luks_join_sss_cfg "tpm2" "${sss_tpm2}")
524513
+    fi
524513
+
524513
+    if [[ -n "${sss}" ]]; then
524513
+        cfg=$(printf '%s,"sss":%s' "${cfg}" "${sss}")
524513
+    fi
524513
+
524513
+    # Remove possible leading comma.
524513
+    cfg=${cfg/#,/}
524513
+    pin=$(printf '{"t":%d,"pins":{%s}}' "${threshold}" "${cfg}")
524513
+    printf "sss '%s'" "${pin}"
524513
+}
524513
+
524513
+# clevis_luks_read_pins_from_slot() will receive a given device and slot and
524513
+# will then output its associated policy configuration.
524513
+clevis_luks_read_pins_from_slot() {
524513
+    local DEV="${1}"
524513
+    local SLOT="${2}"
524513
+
524513
+    local jwe
524513
+    if ! jwe=$(clevis_luks_read_slot "${DEV}" "${SLOT}" 2>/dev/null); then
524513
+        return 1
524513
+    fi
524513
+
524513
+    local cfg
524513
+    if ! cfg="$(clevis_luks_decode_pin_config "${jwe}")"; then
524513
+        return 1
524513
+    fi
524513
+    printf "%s: %s\n" "${SLOT}" "${cfg}"
524513
+}
524513
diff --git a/src/luks/clevis-luks-list b/src/luks/clevis-luks-list
524513
new file mode 100755
524513
index 0000000..58678c4
524513
--- /dev/null
524513
+++ b/src/luks/clevis-luks-list
524513
@@ -0,0 +1,77 @@
524513
+#!/bin/bash -e
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2017-2019 Red Hat, Inc.
524513
+# Author: Javier Martinez Canillas <javierm@redhat.com>
524513
+# Author: Sergio Correia <scorreia@redhat.com> - LUKS2 support.
524513
+#
524513
+# This program is free software: you can redistribute it and/or modify
524513
+# it under the terms of the GNU General Public License as published by
524513
+# the Free Software Foundation, either version 3 of the License, or
524513
+# (at your option) any later version.
524513
+#
524513
+# This program is distributed in the hope that it will be useful,
524513
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
524513
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
524513
+# GNU General Public License for more details.
524513
+#
524513
+# You should have received a copy of the GNU General Public License
524513
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
524513
+#
524513
+
524513
+. clevis-luks-common-functions
524513
+
524513
+SUMMARY="Lists pins bound to a LUKSv1 or LUKSv2 device"
524513
+
524513
+function usage() {
524513
+    echo >&2
524513
+    echo "Usage: clevis luks list -d DEV [-s SLT]" >&2
524513
+    echo >&2
524513
+    echo "$SUMMARY": >&2
524513
+    echo >&2
524513
+    echo "  -d DEV  The LUKS device to list bound pins" >&2
524513
+    echo >&2
524513
+    echo "  -s SLOT The slot number to list" >&2
524513
+    echo >&2
524513
+    exit 1
524513
+}
524513
+
524513
+if [ ${#} -eq 1 ] && [ "${1}" = "--summary" ]; then
524513
+    echo "${SUMMARY}"
524513
+    exit 0
524513
+fi
524513
+
524513
+while getopts ":d:s:" o; do
524513
+    case "$o" in
524513
+    d) DEV=${OPTARG};;
524513
+    s) SLT=${OPTARG};;
524513
+    *) usage;;
524513
+    esac
524513
+done
524513
+
524513
+if [ -z "${DEV}" ]; then
524513
+    echo "Did not specify a device!" >&2
524513
+    usage
524513
+fi
524513
+
524513
+if cryptsetup isLuks --type luks1 "${DEV}"; then
524513
+    if ! luksmeta test -d "${DEV}" 2>/dev/null; then
524513
+        echo "The ${DEV} device is not valid!" >&2
524513
+        exit 1
524513
+    fi
524513
+fi
524513
+
524513
+if [ -n "${SLT}" ]; then
524513
+    clevis_luks_read_pins_from_slot "${DEV}" "${SLT}"
524513
+else
524513
+    if ! slots=$(clevis_luks_used_slots "${DEV}"); then
524513
+        echo "No used slots detected for device ${DEV}!" >&2
524513
+        exit 1
524513
+    fi
524513
+
524513
+    for s in ${slots}; do
524513
+        if ! clevis_luks_read_pins_from_slot "${DEV}" "${s}"; then
524513
+            continue
524513
+        fi
524513
+    done
524513
+fi
524513
diff --git a/src/luks/clevis-luks-list.1.adoc b/src/luks/clevis-luks-list.1.adoc
524513
new file mode 100644
524513
index 0000000..2e84f05
524513
--- /dev/null
524513
+++ b/src/luks/clevis-luks-list.1.adoc
524513
@@ -0,0 +1,58 @@
524513
+CLEVIS-LUKS-LIST(1)
524513
+===================
524513
+:doctype: manpage
524513
+
524513
+
524513
+== NAME
524513
+
524513
+clevis-luks-list - Lists pins bound to a LUKS device
524513
+
524513
+== SYNOPSIS
524513
+
524513
+*clevis luks list* -d DEV [-s SLT]
524513
+
524513
+== OVERVIEW
524513
+
524513
+The *clevis luks list* command list the pins bound to LUKS device.
524513
+For example:
524513
+
524513
+    clevis luks list -d /dev/sda1
524513
+
524513
+== OPTIONS
524513
+
524513
+* *-d* _DEV_ :
524513
+  The LUKS device on which to list bound pins
524513
+
524513
+* *-s* _SLT_ :
524513
+  The slot to use for listing the pin from
524513
+
524513
+== EXAMPLES
524513
+
524513
+    clevis luks list -d /dev/sda1
524513
+    1: sss '{"t":1,"pins":{"tang":[{"url":"addr1"},{"url":"addr2"}],"tpm2":[{"hash":"sha256","key":"ecc"}],"sss":{"t":1,"pins":{"tang":[{"url":"addr3"}]}}}}'
524513
+    2: tang '{"url":"addr"}'
524513
+    3: tpm2 '{"hash":"sha256","key":"ecc","pcr_bank":"sha1","pcr_ids":"7"}'
524513
+
524513
+As we can see in the example above, */dev/sda1* has three slots bound each with a different pin.
524513
+- Slot #1 is bound with the _sss_ pin, and uses also tang and tpm2 pins in its policy.
524513
+- Slot #2 is bound using the _tang_ pin
524513
+- Slot #3 is bound with the _tpm2_ pin
524513
+
524513
+Note that the output of *clevis luks list* can be used with the *clevis luks bind* command, such as:
524513
+
524513
+    clevis luks bind -d /dev/sda1 tpm2 '{"hash":"sha256","key":"ecc","pcr_bank":"sha1","pcr_ids":"7"}'
524513
+
524513
+And we will bind another slot with a policy similar to the one we have in slot #3.
524513
+Also note that if you are interested in a particular slot, you can pass the _-s SLT_ argument to *clevis luks list*:
524513
+
524513
+  clevis luks list -d /dev/sda1 -s 2
524513
+  2: tang '{"url":"addr"}'
524513
+
524513
+In the above example, we listed only the pin bound to slot #2.
524513
+
524513
+== SEE ALSO
524513
+
524513
+link:clevis-luks-bind.1.adoc[*clevis-luks-bind*(1)],
524513
+link:clevis-encrypt-tang.1.adoc[*clevis-encrypt-tang*(1)],
524513
+link:clevis-encrypt-tpm2.1.adoc[*clevis-encrypt-tpm2*(1)],
524513
+link:clevis-encrypt-sss.1.adoc[*clevis-encrypt-sss*(1)],
524513
diff --git a/src/luks/meson.build b/src/luks/meson.build
524513
index 7c045c4..51d82fb 100644
524513
--- a/src/luks/meson.build
524513
+++ b/src/luks/meson.build
524513
@@ -20,6 +20,9 @@ if libcryptsetup.found() and luksmeta.found() and pwmake.found()
524513
   bins += join_paths(meson.current_source_dir(), 'clevis-luks-regen')
524513
   mans += join_paths(meson.current_source_dir(), 'clevis-luks-regen.1')
524513
 
524513
+  bins += join_paths(meson.current_source_dir(), 'clevis-luks-list')
524513
+  mans += join_paths(meson.current_source_dir(), 'clevis-luks-list.1')
524513
+
524513
   bins += join_paths(meson.current_source_dir(), 'clevis-luks-report')
524513
   bins += join_paths(meson.current_source_dir(), 'clevis-luks-report-compare')
524513
   bins += join_paths(meson.current_source_dir(), 'clevis-luks-report-decode')
524513
@@ -30,4 +33,7 @@ if libcryptsetup.found() and luksmeta.found() and pwmake.found()
524513
   mans += join_paths(meson.current_source_dir(), 'clevis-luks-unlockers.7')
524513
 else
524513
   warning('Will not install LUKS support due to missing dependencies!')
524513
-endif
524513
\ No newline at end of file
524513
+endif
524513
+
524513
+# Tests.
524513
+subdir('tests')
524513
diff --git a/src/luks/tests/list-recursive-luks1 b/src/luks/tests/list-recursive-luks1
524513
new file mode 100755
524513
index 0000000..d9eaa3a
524513
--- /dev/null
524513
+++ b/src/luks/tests/list-recursive-luks1
524513
@@ -0,0 +1,85 @@
524513
+#!/bin/bash -ex
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2019 Red Hat, Inc.
524513
+# Author: Sergio Correia <scorreia@redhat.com>
524513
+#
524513
+# This program is free software: you can redistribute it and/or modify
524513
+# it under the terms of the GNU General Public License as published by
524513
+# the Free Software Foundation, either version 3 of the License, or
524513
+# (at your option) any later version.
524513
+#
524513
+# This program is distributed in the hope that it will be useful,
524513
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
524513
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
524513
+# GNU General Public License for more details.
524513
+#
524513
+# You should have received a copy of the GNU General Public License
524513
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
524513
+#
524513
+
524513
+TEST=$(basename "${0}")
524513
+. tests-common-functions
524513
+
524513
+on_exit() {
524513
+    [ -d "${TMP}" ] && rm -rf "${TMP}"
524513
+}
524513
+
524513
+trap 'on_exit' EXIT
524513
+trap 'exit' ERR
524513
+
524513
+TMP="$(mktemp -d)"
524513
+
524513
+ADV="${TMP}/adv.jws"
524513
+create_tang_adv "${ADV}"
524513
+PIN="sss"
524513
+CFG=$(printf '
524513
+{
524513
+  "t": 1,
524513
+  "pins": {
524513
+    "sss": {
524513
+      "t": 1,
524513
+      "pins": {
524513
+        "sss": {
524513
+          "t": 1,
524513
+          "pins": {
524513
+            "tang": [
524513
+              {
524513
+                "url": "ADDR","adv": "%s"
524513
+              }
524513
+            ]
524513
+          }
524513
+        }
524513
+      }
524513
+    }
524513
+  }
524513
+}
524513
+' "${ADV}")
524513
+
524513
+# LUKS1.
524513
+DEV="${TMP}/luks1-device"
524513
+UUID="cb6e8904-81ff-40da-a84a-07ab9ab5715e"
524513
+new_device "luks1" "${DEV}"
524513
+
524513
+if ! clevis luks bind -f -d "${DEV}" "${PIN}" "${CFG}" <<< "${DEFAULT_PASS}"; then
524513
+    error "${TEST}: Binding is expected to succeed when given a correct (${DEFAULT_PASS}) password."
524513
+fi
524513
+
524513
+SLT=1
524513
+if ! read -r slot pin cfg < <(clevis luks list -d "${DEV}" -s "${SLT}"); then
524513
+    error "${TEST}: clevis luks list is expected to succeed for device(${DEV}) and slot (${SLT})"
524513
+fi
524513
+
524513
+if [[ "${slot}" != "${SLT}:" ]]; then
524513
+    error "${TEST}: slot (${slot}) is expected to be ${SLT}"
524513
+fi
524513
+
524513
+if [[ "${pin}" != "${PIN}" ]]; then
524513
+    error "${TEST}: pin (${pin}) is expected to be '${PIN}'"
524513
+fi
524513
+
524513
+to_remove_from_cfg=$(printf ',"adv": "%s"' "${ADV}")
524513
+cfg_for_cmp=${cfg//"${to_remove_from_cfg}"/}
524513
+if ! pin_cfg_equal "${cfg}" "${cfg_for_cmp}"; then
524513
+    error "${TEST}: config obtained from clevis luks list (${cfg}) is expected to match the one used to bind the test (${cfg_for_cmp})"
524513
+fi
524513
diff --git a/src/luks/tests/list-recursive-luks2 b/src/luks/tests/list-recursive-luks2
524513
new file mode 100755
524513
index 0000000..80a8278
524513
--- /dev/null
524513
+++ b/src/luks/tests/list-recursive-luks2
524513
@@ -0,0 +1,85 @@
524513
+#!/bin/bash -ex
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2019 Red Hat, Inc.
524513
+# Author: Sergio Correia <scorreia@redhat.com>
524513
+#
524513
+# This program is free software: you can redistribute it and/or modify
524513
+# it under the terms of the GNU General Public License as published by
524513
+# the Free Software Foundation, either version 3 of the License, or
524513
+# (at your option) any later version.
524513
+#
524513
+# This program is distributed in the hope that it will be useful,
524513
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
524513
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
524513
+# GNU General Public License for more details.
524513
+#
524513
+# You should have received a copy of the GNU General Public License
524513
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
524513
+#
524513
+
524513
+TEST=$(basename "${0}")
524513
+. tests-common-functions
524513
+
524513
+on_exit() {
524513
+    [ -d "${TMP}" ] && rm -rf "${TMP}"
524513
+}
524513
+
524513
+trap 'on_exit' EXIT
524513
+trap 'exit' ERR
524513
+
524513
+TMP="$(mktemp -d)"
524513
+
524513
+ADV="${TMP}/adv.jws"
524513
+create_tang_adv "${ADV}"
524513
+PIN="sss"
524513
+CFG=$(printf '
524513
+{
524513
+  "t": 1,
524513
+  "pins": {
524513
+    "sss": {
524513
+      "t": 1,
524513
+      "pins": {
524513
+        "sss": {
524513
+          "t": 1,
524513
+          "pins": {
524513
+            "tang": [
524513
+              {
524513
+                "url": "ADDR","adv": "%s"
524513
+              }
524513
+            ]
524513
+          }
524513
+        }
524513
+      }
524513
+    }
524513
+  }
524513
+}
524513
+' "${ADV}")
524513
+
524513
+# LUKS2.
524513
+DEV="${TMP}/luks1-device"
524513
+UUID="cb6e8904-81ff-40da-a84a-07ab9ab5715e"
524513
+new_device "luks2" "${DEV}"
524513
+
524513
+if ! clevis luks bind -f -d "${DEV}" "${PIN}" "${CFG}" <<< "${DEFAULT_PASS}"; then
524513
+    error "${TEST}: Binding is expected to succeed when given a correct (${DEFAULT_PASS}) password."
524513
+fi
524513
+
524513
+SLT=1
524513
+if ! read -r slot pin cfg < <(clevis luks list -d "${DEV}" -s "${SLT}"); then
524513
+    error "${TEST}: clevis luks list is expected to succeed for device(${DEV}) and slot (${SLT})"
524513
+fi
524513
+
524513
+if [[ "${slot}" != "${SLT}:" ]]; then
524513
+    error "${TEST}: slot (${slot}) is expected to be ${SLT}"
524513
+fi
524513
+
524513
+if [[ "${pin}" != "${PIN}" ]]; then
524513
+    error "${TEST}: pin (${pin}) is expected to be '${PIN}'"
524513
+fi
524513
+
524513
+to_remove_from_cfg=$(printf ',"adv": "%s"' "${ADV}")
524513
+cfg_for_cmp=${cfg//"${to_remove_from_cfg}"/}
524513
+if ! pin_cfg_equal "${cfg}" "${cfg_for_cmp}"; then
524513
+    error "${TEST}: config obtained from clevis luks list (${cfg}) is expected to match the one used to bind the test (${cfg_for_cmp})"
524513
+fi
524513
diff --git a/src/luks/tests/list-sss-tang-luks1 b/src/luks/tests/list-sss-tang-luks1
524513
new file mode 100755
524513
index 0000000..086fa35
524513
--- /dev/null
524513
+++ b/src/luks/tests/list-sss-tang-luks1
524513
@@ -0,0 +1,77 @@
524513
+#!/bin/bash -ex
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2019 Red Hat, Inc.
524513
+# Author: Sergio Correia <scorreia@redhat.com>
524513
+#
524513
+# This program is free software: you can redistribute it and/or modify
524513
+# it under the terms of the GNU General Public License as published by
524513
+# the Free Software Foundation, either version 3 of the License, or
524513
+# (at your option) any later version.
524513
+#
524513
+# This program is distributed in the hope that it will be useful,
524513
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
524513
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
524513
+# GNU General Public License for more details.
524513
+#
524513
+# You should have received a copy of the GNU General Public License
524513
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
524513
+#
524513
+
524513
+TEST=$(basename "${0}")
524513
+. tests-common-functions
524513
+
524513
+on_exit() {
524513
+    [ -d "${TMP}" ] && rm -rf "${TMP}"
524513
+}
524513
+
524513
+trap 'on_exit' EXIT
524513
+trap 'exit' ERR
524513
+
524513
+TMP="$(mktemp -d)"
524513
+
524513
+ADV="${TMP}/adv.jws"
524513
+create_tang_adv "${ADV}"
524513
+PIN="sss"
524513
+CFG=$(printf '
524513
+{
524513
+   "t": 2,
524513
+   "pins": {
524513
+     "tang": [
524513
+       {"url":"ADDR1","adv":"%s"},
524513
+       {"url":"ADDR2","adv":"%s"},
524513
+       {"url":"ADDR3","adv":"%s"},
524513
+       {"url":"ADDR4","adv":"%s"},
524513
+       {"url":"ADDR5","adv":"%s"}
524513
+     ]
524513
+   }
524513
+}
524513
+' "${ADV}" "${ADV}" "${ADV}" "${ADV}" "${ADV}")
524513
+
524513
+# LUKS1.
524513
+DEV="${TMP}/luks1-device"
524513
+UUID="cb6e8904-81ff-40da-a84a-07ab9ab5715e"
524513
+new_device "luks1" "${DEV}"
524513
+
524513
+if ! clevis luks bind -f -d "${DEV}" ${PIN} "${CFG}" <<< "${DEFAULT_PASS}"; then
524513
+    error "${TEST}: Binding is expected to succeed when given a correct (${DEFAULT_PASS}) password."
524513
+fi
524513
+
524513
+SLT=1
524513
+if ! read -r slot pin cfg < <(clevis luks list -d "${DEV}" -s "${SLT}"); then
524513
+    error "${TEST}: clevis luks list is expected to succeed for device(${DEV}) and slot (${SLT})"
524513
+fi
524513
+
524513
+if [[ "${slot}" != "${SLT}:" ]]; then
524513
+    error "${TEST}: slot (${slot}) is expected to be ${SLT}"
524513
+fi
524513
+
524513
+if [[ "${pin}" != "${PIN}" ]]; then
524513
+    error "${TEST}: pin (${pin}) is expected to be '${PIN}'"
524513
+fi
524513
+
524513
+to_remove_from_cfg=$(printf ',"adv": "%s"' "${ADV}")
524513
+cfg_for_cmp=${cfg//"${to_remove_from_cfg}"/}
524513
+if ! pin_cfg_equal "${cfg}" "${cfg_for_cmp}"; then
524513
+    error "${TEST}: config obtained from clevis luks list (${cfg}) is expected to match the one used to bind the test (${cfg_for_cmp})"
524513
+fi
524513
diff --git a/src/luks/tests/list-sss-tang-luks2 b/src/luks/tests/list-sss-tang-luks2
524513
new file mode 100755
524513
index 0000000..ea4cfbb
524513
--- /dev/null
524513
+++ b/src/luks/tests/list-sss-tang-luks2
524513
@@ -0,0 +1,77 @@
524513
+#!/bin/bash -ex
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2019 Red Hat, Inc.
524513
+# Author: Sergio Correia <scorreia@redhat.com>
524513
+#
524513
+# This program is free software: you can redistribute it and/or modify
524513
+# it under the terms of the GNU General Public License as published by
524513
+# the Free Software Foundation, either version 3 of the License, or
524513
+# (at your option) any later version.
524513
+#
524513
+# This program is distributed in the hope that it will be useful,
524513
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
524513
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
524513
+# GNU General Public License for more details.
524513
+#
524513
+# You should have received a copy of the GNU General Public License
524513
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
524513
+#
524513
+
524513
+TEST=$(basename "${0}")
524513
+. tests-common-functions
524513
+
524513
+on_exit() {
524513
+    [ -d "${TMP}" ] && rm -rf "${TMP}"
524513
+}
524513
+
524513
+trap 'on_exit' EXIT
524513
+trap 'exit' ERR
524513
+
524513
+TMP="$(mktemp -d)"
524513
+
524513
+ADV="${TMP}/adv.jws"
524513
+create_tang_adv "${ADV}"
524513
+PIN="sss"
524513
+CFG=$(printf '
524513
+{
524513
+   "t": 2,
524513
+   "pins": {
524513
+     "tang": [
524513
+       {"url":"ADDR1","adv":"%s"},
524513
+       {"url":"ADDR2","adv":"%s"},
524513
+       {"url":"ADDR3","adv":"%s"},
524513
+       {"url":"ADDR4","adv":"%s"},
524513
+       {"url":"ADDR5","adv":"%s"}
524513
+     ]
524513
+   }
524513
+}
524513
+' "${ADV}" "${ADV}" "${ADV}" "${ADV}" "${ADV}")
524513
+
524513
+# LUKS2.
524513
+DEV="${TMP}/luks1-device"
524513
+UUID="cb6e8904-81ff-40da-a84a-07ab9ab5715e"
524513
+new_device "luks2" "${DEV}"
524513
+
524513
+if ! clevis luks bind -f -d "${DEV}" ${PIN} "${CFG}" <<< "${DEFAULT_PASS}"; then
524513
+    error "${TEST}: Binding is expected to succeed when given a correct (${DEFAULT_PASS}) password."
524513
+fi
524513
+
524513
+SLT=1
524513
+if ! read -r slot pin cfg < <(clevis luks list -d "${DEV}" -s "${SLT}"); then
524513
+    error "${TEST}: clevis luks list is expected to succeed for device(${DEV}) and slot (${SLT})"
524513
+fi
524513
+
524513
+if [[ "${slot}" != "${SLT}:" ]]; then
524513
+    error "${TEST}: slot (${slot}) is expected to be ${SLT}"
524513
+fi
524513
+
524513
+if [[ "${pin}" != "${PIN}" ]]; then
524513
+    error "${TEST}: pin (${pin}) is expected to be '${PIN}'"
524513
+fi
524513
+
524513
+to_remove_from_cfg=$(printf ',"adv": "%s"' "${ADV}")
524513
+cfg_for_cmp=${cfg//"${to_remove_from_cfg}"/}
524513
+if ! pin_cfg_equal "${cfg}" "${cfg_for_cmp}"; then
524513
+    error "${TEST}: config obtained from clevis luks list (${cfg}) is expected to match the one used to bind the test (${cfg_for_cmp})"
524513
+fi
524513
diff --git a/src/luks/tests/list-tang-luks1 b/src/luks/tests/list-tang-luks1
524513
new file mode 100755
524513
index 0000000..c526693
524513
--- /dev/null
524513
+++ b/src/luks/tests/list-tang-luks1
524513
@@ -0,0 +1,64 @@
524513
+#!/bin/bash -ex
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2019 Red Hat, Inc.
524513
+# Author: Sergio Correia <scorreia@redhat.com>
524513
+#
524513
+# This program is free software: you can redistribute it and/or modify
524513
+# it under the terms of the GNU General Public License as published by
524513
+# the Free Software Foundation, either version 3 of the License, or
524513
+# (at your option) any later version.
524513
+#
524513
+# This program is distributed in the hope that it will be useful,
524513
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
524513
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
524513
+# GNU General Public License for more details.
524513
+#
524513
+# You should have received a copy of the GNU General Public License
524513
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
524513
+#
524513
+
524513
+TEST=$(basename "${0}")
524513
+. tests-common-functions
524513
+
524513
+on_exit() {
524513
+    [ -d "${TMP}" ] && rm -rf "${TMP}"
524513
+}
524513
+
524513
+trap 'on_exit' EXIT
524513
+trap 'exit' ERR
524513
+
524513
+TMP="$(mktemp -d)"
524513
+
524513
+ADV="${TMP}/adv.jws"
524513
+create_tang_adv "${ADV}"
524513
+PIN="tang"
524513
+CFG=$(printf '{"url": "ADDR","adv": "%s"}' "${ADV}")
524513
+
524513
+# LUKS1.
524513
+DEV="${TMP}/luks1-device"
524513
+UUID="cb6e8904-81ff-40da-a84a-07ab9ab5715e"
524513
+new_device "luks1" "${DEV}"
524513
+
524513
+if ! clevis luks bind -f -d "${DEV}" "${PIN}" "${CFG}" <<< "${DEFAULT_PASS}"; then
524513
+    error "${TEST}: Binding is expected to succeed when given a correct (${DEFAULT_PASS}) password."
524513
+fi
524513
+
524513
+SLT=1
524513
+if ! read -r slot pin cfg < <(clevis luks list -d "${DEV}" -s "${SLT}"); then
524513
+    error "${TEST}: clevis luks list is expected to succeed for device(${DEV}) and slot (${SLT})"
524513
+fi
524513
+
524513
+if [[ "${slot}" != "${SLT}:" ]]; then
524513
+    error "${TEST}: slot (${slot}) is expected to be ${SLT}"
524513
+fi
524513
+
524513
+if [[ "${pin}" != "${PIN}" ]]; then
524513
+    error "${TEST}: pin (${pin}) is expected to be '${PIN}'"
524513
+fi
524513
+
524513
+to_remove_from_cfg=$(printf ',"adv": "%s"' "${ADV}")
524513
+cfg_for_cmp=${cfg//"${to_remove_from_cfg}"/}
524513
+if ! pin_cfg_equal "${cfg}" "${cfg_for_cmp}"; then
524513
+    error "${TEST}: config obtained from clevis luks list (${cfg}) is expected to match the one used to bind the test (${cfg_for_cmp})"
524513
+fi
524513
diff --git a/src/luks/tests/list-tang-luks2 b/src/luks/tests/list-tang-luks2
524513
new file mode 100755
524513
index 0000000..d4d4849
524513
--- /dev/null
524513
+++ b/src/luks/tests/list-tang-luks2
524513
@@ -0,0 +1,64 @@
524513
+#!/bin/bash -ex
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2019 Red Hat, Inc.
524513
+# Author: Sergio Correia <scorreia@redhat.com>
524513
+#
524513
+# This program is free software: you can redistribute it and/or modify
524513
+# it under the terms of the GNU General Public License as published by
524513
+# the Free Software Foundation, either version 3 of the License, or
524513
+# (at your option) any later version.
524513
+#
524513
+# This program is distributed in the hope that it will be useful,
524513
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
524513
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
524513
+# GNU General Public License for more details.
524513
+#
524513
+# You should have received a copy of the GNU General Public License
524513
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
524513
+#
524513
+
524513
+TEST=$(basename "${0}")
524513
+. tests-common-functions
524513
+
524513
+on_exit() {
524513
+    [ -d "${TMP}" ] && rm -rf "${TMP}"
524513
+}
524513
+
524513
+trap 'on_exit' EXIT
524513
+trap 'exit' ERR
524513
+
524513
+TMP="$(mktemp -d)"
524513
+
524513
+ADV="${TMP}/adv.jws"
524513
+create_tang_adv "${ADV}"
524513
+PIN="tang"
524513
+CFG=$(printf '{"url": "ADDR","adv": "%s"}' "${ADV}")
524513
+
524513
+# LUKS2.
524513
+DEV="${TMP}/luks1-device"
524513
+UUID="cb6e8904-81ff-40da-a84a-07ab9ab5715e"
524513
+new_device "luks2" "${DEV}"
524513
+
524513
+if ! clevis luks bind -f -d "${DEV}" "${PIN}" "${CFG}" <<< "${DEFAULT_PASS}"; then
524513
+    error "${TEST}: Binding is expected to succeed when given a correct (${DEFAULT_PASS}) password."
524513
+fi
524513
+
524513
+SLT=1
524513
+if ! read -r slot pin cfg < <(clevis luks list -d "${DEV}" -s "${SLT}"); then
524513
+    error "${TEST}: clevis luks list is expected to succeed for device(${DEV}) and slot (${SLT})"
524513
+fi
524513
+
524513
+if [[ "${slot}" != "${SLT}:" ]]; then
524513
+    error "${TEST}: slot (${slot}) is expected to be ${SLT}"
524513
+fi
524513
+
524513
+if [[ "${pin}" != "${PIN}" ]]; then
524513
+    error "${TEST}: pin (${pin}) is expected to be '${PIN}'"
524513
+fi
524513
+
524513
+to_remove_from_cfg=$(printf ',"adv": "%s"' "${ADV}")
524513
+cfg_for_cmp=${cfg//"${to_remove_from_cfg}"/}
524513
+if ! pin_cfg_equal "${cfg}" "${cfg_for_cmp}"; then
524513
+    error "${TEST}: config obtained from clevis luks list (${cfg}) is expected to match the one used to bind the test (${cfg_for_cmp})"
524513
+fi
524513
diff --git a/src/luks/tests/meson.build b/src/luks/tests/meson.build
524513
new file mode 100644
524513
index 0000000..6513eaa
524513
--- /dev/null
524513
+++ b/src/luks/tests/meson.build
524513
@@ -0,0 +1,36 @@
524513
+# We use jq for comparing the pin config in the clevis luks list tests.
524513
+jq = find_program('jq', required: false)
524513
+
524513
+env = environment()
524513
+env.prepend('PATH',
524513
+  join_paths(meson.source_root(), 'src'),
524513
+  join_paths(meson.source_root(), 'src', 'luks'),
524513
+  join_paths(meson.source_root(), 'src', 'pins', 'sss'),
524513
+  join_paths(meson.source_root(), 'src', 'pins', 'tang'),
524513
+  join_paths(meson.source_root(), 'src', 'pins', 'tpm2'),
524513
+  meson.current_source_dir(),
524513
+  meson.current_build_dir(),
524513
+  join_paths(meson.build_root(), 'src'),
524513
+  join_paths(meson.build_root(), 'src', 'luks'),
524513
+  join_paths(meson.build_root(), 'src', 'pins', 'sss'),
524513
+  join_paths(meson.build_root(), 'src', 'pins', 'tang'),
524513
+  join_paths(meson.build_root(), 'src', 'pins', 'tpm2'),
524513
+  separator: ':'
524513
+)
524513
+
524513
+if jq.found()
524513
+  test('list-recursive-luks1', find_program('list-recursive-luks1'), env: env)
524513
+  test('list-tang-luks1', find_program('list-tang-luks1'), env: env)
524513
+  test('list-sss-tang-luks1', find_program('list-sss-tang-luks1'), env: env)
524513
+else
524513
+  warning('Will not run "clevis luks list" tests due to missing jq dependency')
524513
+endif
524513
+
524513
+# LUKS2 tests go here, and they get included if we get support for it, based
524513
+# on the cryptsetup version.
524513
+# Binding LUKS2 takes longer, so timeout is increased for a few tests.
524513
+if jq.found()
524513
+  test('list-recursive-luks2', find_program('list-recursive-luks2'), env: env, timeout: 60)
524513
+  test('list-tang-luks2', find_program('list-tang-luks2'), env: env, timeout: 60)
524513
+  test('list-sss-tang-luks2', find_program('list-sss-tang-luks2'), env: env, timeout: 60)
524513
+endif
524513
diff --git a/src/luks/tests/tests-common-functions b/src/luks/tests/tests-common-functions
524513
new file mode 100644
524513
index 0000000..b65a84a
524513
--- /dev/null
524513
+++ b/src/luks/tests/tests-common-functions
524513
@@ -0,0 +1,76 @@
524513
+#!/bin/bash -ex
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2019 Red Hat, Inc.
524513
+# Author: Sergio Correia <scorreia@redhat.com>
524513
+#
524513
+# This program is free software: you can redistribute it and/or modify
524513
+# it under the terms of the GNU General Public License as published by
524513
+# the Free Software Foundation, either version 3 of the License, or
524513
+# (at your option) any later version.
524513
+#
524513
+# This program is distributed in the hope that it will be useful,
524513
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
524513
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
524513
+# GNU General Public License for more details.
524513
+#
524513
+# You should have received a copy of the GNU General Public License
524513
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
524513
+#
524513
+
524513
+# We require cryptsetup >= 2.0.4 to fully support LUKSv2.
524513
+# Support is determined at build time.
524513
+luks2_supported() {
524513
+    # In RHEL8 we support LUKS2.
524513
+    return 0
524513
+}
524513
+
524513
+# Creates a tang adv to be used in the test.
524513
+create_tang_adv() {
524513
+    local adv="${1}"
524513
+    local SIG="${TMP}/sig.jwk"
524513
+    jose jwk gen -i '{"alg":"ES512"}' > "${SIG}"
524513
+
524513
+    local EXC="${TMP}/exc.jwk"
524513
+    jose jwk gen -i '{"alg":"ECMR"}' > "${EXC}"
524513
+
524513
+    local TEMPLATE='{"protected":{"cty":"jwk-set+json"}}'
524513
+    jose jwk pub -s -i "${SIG}" -i "${EXC}" \
524513
+        | jose jws sig -I- -s "${TEMPLATE}" -k "${SIG}" -o "${adv}"
524513
+}
524513
+
524513
+
524513
+# Creates a new LUKS1 or LUKS2 device to be used.
524513
+new_device() {
524513
+    local LUKS="${1}"
524513
+    local DEV="${2}"
524513
+
524513
+    local DEV_CACHED="${TMP}/${LUKS}.cached"
524513
+
524513
+    # Let's reuse an existing device, if there is one.
524513
+    if [ -f "${DEV_CACHED}" ]; then
524513
+        echo "Reusing cached ${LUKS} device..."
524513
+        cp -f "${DEV_CACHED}" "${DEV}"
524513
+        return 0
524513
+    fi
524513
+
524513
+    fallocate -l16M "${DEV}"
524513
+    cryptsetup luksFormat --type "${LUKS}" --batch-mode --force-password "${DEV}" <<< "${DEFAULT_PASS}"
524513
+    # Caching the just-formatted device for possible reuse.
524513
+    cp -f "${DEV}" "${DEV_CACHED}"
524513
+}
524513
+
524513
+error() {
524513
+    echo "${1}" >&2
524513
+    exit 1
524513
+}
524513
+
524513
+pin_cfg_equal() {
524513
+    local cfg1="${1}"
524513
+    local cfg2="${1}"
524513
+
524513
+    diff <(jq -S . < <(echo -n "${cfg1}")) \
524513
+         <(jq -S . < <(echo -n "${cfg2}"))
524513
+}
524513
+
524513
+export DEFAULT_PASS='just-some-test-password-here'
524513
-- 
524513
2.18.1
524513