Blame SOURCES/Add-clevis-luks-report-regen.patch

524513
From 70d3da5ce8d68e8ff258122592670eb70da0c839 Mon Sep 17 00:00:00 2001
524513
From: Sergio Correia <scorreia@redhat.com>
524513
Date: Wed, 16 Oct 2019 09:14:58 -0300
524513
Subject: [PATCH 2/2] Add clevis luks report/regen
524513
524513
---
524513
 src/luks/clevis-luks-common-functions | 143 ++++++++++++++++++++
524513
 src/luks/clevis-luks-regen            | 186 ++++++++++++++++++++++++++
524513
 src/luks/clevis-luks-regen.1.adoc     |  36 +++++
524513
 src/luks/clevis-luks-report           |  95 +++++++++++++
524513
 src/luks/clevis-luks-report-compare   |  71 ++++++++++
524513
 src/luks/clevis-luks-report-decode    |  59 ++++++++
524513
 src/luks/clevis-luks-report-sss       |  53 ++++++++
524513
 src/luks/clevis-luks-report-tang      |  67 ++++++++++
524513
 src/luks/clevis-luks-report.1.adoc    |  41 ++++++
524513
 src/luks/meson.build                  |  12 ++
524513
 10 files changed, 763 insertions(+)
524513
 create mode 100644 src/luks/clevis-luks-common-functions
524513
 create mode 100755 src/luks/clevis-luks-regen
524513
 create mode 100644 src/luks/clevis-luks-regen.1.adoc
524513
 create mode 100755 src/luks/clevis-luks-report
524513
 create mode 100755 src/luks/clevis-luks-report-compare
524513
 create mode 100755 src/luks/clevis-luks-report-decode
524513
 create mode 100755 src/luks/clevis-luks-report-sss
524513
 create mode 100755 src/luks/clevis-luks-report-tang
524513
 create mode 100644 src/luks/clevis-luks-report.1.adoc
524513
524513
diff --git a/src/luks/clevis-luks-common-functions b/src/luks/clevis-luks-common-functions
524513
new file mode 100644
524513
index 0000000..d676253
524513
--- /dev/null
524513
+++ b/src/luks/clevis-luks-common-functions
524513
@@ -0,0 +1,143 @@
524513
+#!/bin/bash -e
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
+# valid_slot() will check whether a given slot is possibly valid, i.e., if it
524513
+# is a numeric value within the specified range.
524513
+valid_slot() {
524513
+    local SLT="${1}"
524513
+    local MAX_SLOTS="${2}"
524513
+    case "${SLT}" in
524513
+        ''|*[!0-9]*)
524513
+            return 1
524513
+            ;;
524513
+        *)
524513
+            # We got an integer, now let's make sure it is within the
524513
+            # supported range.
524513
+            if [ "${SLT}" -ge "${MAX_SLOTS}" ]; then
524513
+                return 1
524513
+            fi
524513
+            ;;
524513
+    esac
524513
+}
524513
+
524513
+# clevis_luks_read_slot() will read a particular slot of a given device, which
524513
+# should be either LUKS1 or LUKS2. Returns 1 in case of failure; 0 in case of
524513
+# success.
524513
+clevis_luks_read_slot() {
524513
+    local DEV="${1}"
524513
+    local SLT="${2}"
524513
+
524513
+    if [ -z "${DEV}" ] || [ -z "${SLT}" ]; then
524513
+        echo "Need both a device and a slot as arguments." >&2
524513
+        return 1
524513
+    fi
524513
+
524513
+    local DATA_CODED=''
524513
+    local MAX_LUKS1_SLOTS=8
524513
+    local MAX_LUKS2_SLOTS=32
524513
+    if cryptsetup isLuks --type luks1 "${DEV}"; then
524513
+        if ! valid_slot "${SLT}" "${MAX_LUKS1_SLOTS}"; then
524513
+            echo "Please, provide a valid key slot number; 0-7 for LUKS1" >&2
524513
+            return 1
524513
+        fi
524513
+
524513
+        if ! luksmeta test -d "${DEV}"; then
524513
+            echo "The ${DEV} device is not valid!" >&2
524513
+            return 1
524513
+        fi
524513
+
524513
+        local uuid
524513
+        # Pattern from luksmeta: active slot uuid.
524513
+        read -r _ _ uuid <<< "$(luksmeta show -d "${DEV}" | grep "^${SLT} *")"
524513
+
524513
+        if [ "${uuid}" = "empty" ]; then
524513
+           echo "The LUKSMeta slot ${SLT} on device ${DEV} is already empty." >&2
524513
+           return 1
524513
+        fi
524513
+
524513
+        if ! DATA_CODED="$(luksmeta load -d "${DEV}" -s "${SLT}")"; then
524513
+            echo "Cannot load data from ${DEV} slot:${SLT}!" >&2
524513
+            return 1
524513
+        fi
524513
+    elif cryptsetup isLuks --type luks2 "${DEV}"; then
524513
+        if ! valid_slot "${SLT}" "${MAX_LUKS2_SLOTS}"; then
524513
+            echo "Please, provide a valid key slot number; 0-31 for LUKS2" >&2
524513
+            return 1
524513
+        fi
524513
+
524513
+        local token_id
524513
+        token_id=$(cryptsetup luksDump "${DEV}" \
524513
+                    | grep -E -B1 "^\s+Keyslot:\s+${SLT}$" \
524513
+                    | head -n 1 | sed -rn 's|^\s+([0-9]+): clevis|\1|p')
524513
+        if [ -z "${token_id}" ]; then
524513
+            echo "Cannot load data from ${DEV} slot:${SLT}. No token found!" >&2
524513
+            return 1
524513
+        fi
524513
+
524513
+        local token
524513
+        token=$(cryptsetup token export --token-id "${token_id}" "${DEV}")
524513
+        DATA_CODED=$(jose fmt -j- -Og jwe -o- <<< "${token}" \
524513
+                     | jose jwe fmt -i- -c)
524513
+
524513
+        if [ -z "${DATA_CODED}" ]; then
524513
+            echo "Cannot load data from ${DEV} slot:${SLT}!" >&2
524513
+            return 1
524513
+        fi
524513
+    else
524513
+        echo "${DEV} is not a supported LUKS device!" >&2
524513
+        return 1
524513
+    fi
524513
+    echo "${DATA_CODED}"
524513
+}
524513
+
524513
+# Generate a key with the same entropy as the LUKS Master key of a given
524513
+# device.
524513
+generate_key() {
524513
+    local DEV="${1}"
524513
+
524513
+    if [ -z "${DEV}" ]; then
524513
+        echo "Please, specify a device." >&2
524513
+        return 1
524513
+    fi
524513
+
524513
+    local dump
524513
+    local filter
524513
+    dump=$(cryptsetup luksDump "${DEV}")
524513
+    if cryptsetup isLuks --type luks1 "${DEV}"; then
524513
+        filter=$(sed -rn 's|MK bits:[ \t]*([0-9]+)|\1|p' <<< "${dump}")
524513
+    elif cryptsetup isLuks --type luks2 "${DEV}"; then
524513
+        filter=$(sed -rn 's|^\s+Key:\s+([0-9]+) bits\s*$|\1|p' <<< "${dump}")
524513
+    else
524513
+        echo "${DEV} is not a supported LUKS device!" >&2
524513
+        return 1
524513
+    fi
524513
+    local bits
524513
+    bits=$(sort -n <<< "${filter}" | tail -n 1)
524513
+    pwmake "${bits}"
524513
+}
524513
+
524513
+findexe() {
524513
+    while read -r -d: path; do
524513
+        [ -f "${path}/${1}" ] && [ -x "${path}/${1}" ] && \
524513
+          echo "${path}/${1}" && return 0
524513
+    done <<< "${PATH}:"
524513
+    return 1
524513
+}
524513
+
524513
diff --git a/src/luks/clevis-luks-regen b/src/luks/clevis-luks-regen
524513
new file mode 100755
524513
index 0000000..9535ba3
524513
--- /dev/null
524513
+++ b/src/luks/clevis-luks-regen
524513
@@ -0,0 +1,186 @@
524513
+#!/usr/bin/env bash
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2018 Red Hat, Inc.
524513
+# Author: Radovan Sroka <rsroka@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
+. clevis-luks-common-functions
524513
+
524513
+SUMMARY="Regenerate LUKS metadata"
524513
+
524513
+if [ "$1" == "--summary" ]; then
524513
+    echo "$SUMMARY"
524513
+    exit 0
524513
+fi
524513
+
524513
+function usage_and_exit () {
524513
+    echo >&2
524513
+    echo "Usage: clevis luks regen -d DEV -s SLOT" >&2
524513
+    echo >&2
524513
+    echo "$SUMMARY" >&2
524513
+    echo >&2
524513
+    exit "$1"
524513
+}
524513
+
524513
+if [ "$#" -ne "4" ]; then
524513
+    usage_and_exit 1
524513
+fi
524513
+
524513
+while getopts "hd:s:" o; do
524513
+    case "$o" in
524513
+    d) DEV="$OPTARG";;
524513
+    h) usage_and_exit 0;;
524513
+    s) SLT="$OPTARG";;
524513
+    *) usage_and_exit 1;;
524513
+    esac
524513
+done
524513
+
524513
+function decode_luks_header () {
524513
+    if DATA_CODED="$(jose jwe fmt -i- <<< "$1")"; then
524513
+        DATA_CODED="$(jose fmt -j- -g protected -u- <<< "$DATA_CODED")"
524513
+        DATA_DECODED="$(jose b64 dec -i- <<< "$DATA_CODED")"
524513
+    else
524513
+        echo "Error decoding JWE protected header!" >&2
524513
+        exit 1
524513
+    fi
524513
+
524513
+    echo "$DATA_DECODED"
524513
+}
524513
+
524513
+function generate_cfg () {
524513
+    echo -n "{"
524513
+    DATA="$(decode_luks_header "$1")"
524513
+
524513
+    if ! P="$(jose fmt -j- -g clevis -g pin -u- <<< "$DATA")" || [ -z "$P" ]; then
524513
+        echo "Pin wasn't found in LUKS metadata!" >&2
524513
+        exit 1
524513
+    fi
524513
+
524513
+    if ! CONTENT="$(jose fmt -j- -g clevis -g "$P" -o- <<< "$DATA")" || [ -z "$CONTENT" ]; then
524513
+        echo "Content was not found!" >&2
524513
+    fi
524513
+
524513
+    # echo -n "\"$P\": ["
524513
+
524513
+    if [ "$P" = "tang" ] || [ "$P" = "http" ]; then
524513
+        URL="$(jose fmt -j- -g url -u- <<< "$CONTENT")"
524513
+        echo -n "\"url\":\"$URL\""
524513
+    elif [ "$P" = "sss" ]; then
524513
+        THRESHOLD="$(jose fmt -j- -g t -o- <<< "$CONTENT")"
524513
+        if [ -n "$THRESHOLD" ]; then
524513
+            echo -n "\"t\":$THRESHOLD,"
524513
+        fi
524513
+
524513
+        echo -n "\"pins\":{"
524513
+
524513
+        CNT=0
524513
+        PREV=""
524513
+        while ITEM="$(jose fmt -j- -g jwe -g"$CNT" -u- <<< "$CONTENT")"; do
524513
+            if [ -z "$ITEM" ]; then
524513
+                CNT=$(( CNT + 1 ))
524513
+                continue # in some cases it can be empty string
524513
+            fi
524513
+
524513
+            DD="$(decode_luks_header "$ITEM")"
524513
+
524513
+            if ! PP="$(jose fmt -j- -g clevis -g pin -u- <<< "$DD")" || [ -z "$PP" ]; then
524513
+                echo "Pin wasn't found in LUKS metadata!" >&2
524513
+                exit 1
524513
+            fi
524513
+
524513
+            if [ "$CNT" -eq 0 ]; then
524513
+                PREV="$PP"
524513
+                echo -n "\"$PP\":["
524513
+                echo -n "$(generate_cfg "$ITEM")"
524513
+            else
524513
+                if ! [ "$PREV" = "$PP" ]; then
524513
+                    echo -n "],\"$PP\":["
524513
+                    echo -n "$(generate_cfg "$ITEM")"
524513
+                else
524513
+                    echo -n ",$(generate_cfg "$ITEM")"
524513
+                fi
524513
+            fi
524513
+
524513
+            PREV="$PP"
524513
+            CNT=$(( CNT + 1 ))
524513
+        done
524513
+
524513
+        echo -n "]}"
524513
+
524513
+    else
524513
+        echo "Unknown pin $P!" >&2
524513
+        exit 1
524513
+    fi
524513
+
524513
+    echo -n "}"
524513
+}
524513
+
524513
+### get luks metadata
524513
+
524513
+if [ -z "$DEV" ]; then
524513
+    echo "Did not specify a device!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+if [ -z "$SLT" ]; then
524513
+    echo "Did not specify a slot!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+if ! OLD_LUKS_CODED="$(clevis_luks_read_slot "$DEV" "$SLT")"; then
524513
+    echo "Error reading metadata from LUKS device!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+### ----------------------------------------------------------------------
524513
+
524513
+DECODED="$(decode_luks_header "$OLD_LUKS_CODED")"
524513
+
524513
+if ! PIN="$(jose fmt -j- -g clevis -g pin -u- <<< "$DECODED")" || [ -z "$PIN" ]; then
524513
+    echo "Pin wasn't found in LUKS metadata!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+CFG="$(generate_cfg "$OLD_LUKS_CODED")"
524513
+
524513
+### ----------------------------------------------------------------------
524513
+
524513
+echo "Regenerating with:"
524513
+echo "PIN: $PIN"
524513
+echo "CONFIG: $CFG"
524513
+
524513
+trap 'echo "Ignoring CONTROL-C!"' INT TERM
524513
+
524513
+# Get the existing key.
524513
+read -r -s -p "Enter existing LUKS password: " existing_key; echo
524513
+
524513
+# Check if the key is valid.
524513
+if ! cryptsetup luksOpen --test-passphrase "${DEV}" <<< "${existing_key}"; then
524513
+    exit 1
524513
+fi
524513
+
524513
+if ! clevis luks unbind -d "${DEV}" -s "${SLT}" -f; then
524513
+    echo "Error during unbind of rotated key from slot:$SLT in $DEV" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+if ! clevis luks bind -d "${DEV}" -s "${SLT}" "${PIN}" "${CFG}" -k - <<< "${existing_key}"; then
524513
+    echo "Error during bind of new key from slot:$SLT in $DEV" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+echo "Keys were succesfully rotated."
524513
diff --git a/src/luks/clevis-luks-regen.1.adoc b/src/luks/clevis-luks-regen.1.adoc
524513
new file mode 100644
524513
index 0000000..3cd6b7c
524513
--- /dev/null
524513
+++ b/src/luks/clevis-luks-regen.1.adoc
524513
@@ -0,0 +1,36 @@
524513
+CLEVIS-LUKS-REGEN(1)
524513
+=====================
524513
+:doctype: manpage
524513
+
524513
+
524513
+== NAME
524513
+
524513
+clevis-luks-regen - Regenerates LUKS metadata
524513
+
524513
+== SYNOPSIS
524513
+
524513
+*clevis luks regen* -d DEV -s SLT
524513
+
524513
+== OVERVIEW
524513
+
524513
+The *clevis luks regen* command regenerates the LUKS metadata for a given slot in a LUKS device. It effectively
524513
+performs an operation equivalent to *clevis luks unbind* and *clevis luks bind* for rebinding said slot and device.
524513
+
524513
+== OPTIONS
524513
+
524513
+* *-d* _DEV_ :
524513
+  The bound LUKS device
524513
+
524513
+* *-s* _SLT_ :
524513
+  The slot or key slot number for rebinding. Note that it requires that such slot is currently bound by clevis.
524513
+
524513
+== EXAMPLE
524513
+
524513
+    Regenerate the binding of slot 1 from /dev/sda1:
524513
+
524513
+    # clevis luks regen -d /dev/sda1 -s 1
524513
+
524513
+== SEE ALSO
524513
+
524513
+link:clevis-luks-bind.1.adoc[*clevis-luks-bind*(1)]
524513
+link:clevis-luks-unbind.1.adoc[*clevis-luks-unbind*(1)]
524513
diff --git a/src/luks/clevis-luks-report b/src/luks/clevis-luks-report
524513
new file mode 100755
524513
index 0000000..f047256
524513
--- /dev/null
524513
+++ b/src/luks/clevis-luks-report
524513
@@ -0,0 +1,95 @@
524513
+#!/usr/bin/bash -e
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2018 Red Hat, Inc.
524513
+# Author: Radovan Sroka <rsroka@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
+. clevis-luks-common-functions
524513
+
524513
+SUMMARY="Report any key rotation on the server side"
524513
+
524513
+if [ "$1" == "--summary" ]; then
524513
+    echo "$SUMMARY"
524513
+    exit 0
524513
+fi
524513
+
524513
+function usage_and_exit () {
524513
+    echo >&2
524513
+    echo "Usage: clevis luks report [-qr] -d DEV -s SLOT" >&2
524513
+    echo >&2
524513
+    echo -e "  -q\t Quiet mode" >&2
524513
+    echo -e "  -r\t Regenerate luks metadata with \"clevis luks regen -d DEV -s SLOT\"" >&2
524513
+    echo >&2
524513
+    echo "$SUMMARY" >&2
524513
+    echo >&2
524513
+    exit "$1"
524513
+}
524513
+
524513
+while getopts "hd:s:rq" o; do
524513
+    case "$o" in
524513
+    d) DEV="$OPTARG";;
524513
+    h) usage_and_exit 0;;
524513
+    r) ROPT="regen";;
524513
+    s) SLT="$OPTARG";;
524513
+    q) QOPT="quiet";;
524513
+    *) usage_and_exit 1;;
524513
+    esac
524513
+done
524513
+
524513
+### get luks metadata
524513
+
524513
+if [ -z "$DEV" ]; then
524513
+    echo "Did not specify a device!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+if [ -z "$SLT" ]; then
524513
+    echo "Did not specify a slot!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+if ! DATA_CODED=$(clevis_luks_read_slot "${DEV}" "${SLT}"); then
524513
+    # Error message was already displayed by clevis_luks_read_slot(),
524513
+    # at this point.
524513
+    exit 1
524513
+fi
524513
+
524513
+EXE="$(findexe clevis-luks-report-decode)"
524513
+RESULT="$($EXE "${DATA_CODED}")"
524513
+
524513
+if [ -n "$RESULT" ]; then
524513
+    echo "$RESULT"
524513
+    echo "Report detected that some keys were rotated."
524513
+    if [ -z "$QOPT" ]; then
524513
+        if [ -z "$ROPT" ]; then
524513
+            read -r -p "Do you want to regenerate luks metadata with \"clevis luks regen -d $DEV -s $SLT\"? [ynYN] " ans < /dev/tty
524513
+            [[ "$ans" =~ ^[yY]$ ]] && ROPT="regen"
524513
+        fi
524513
+    fi
524513
+else
524513
+    exit 0
524513
+fi
524513
+
524513
+if [ "$ROPT" = "regen" ]; then
524513
+    EXE="$(findexe clevis-luks-regen)"
524513
+    exec "$EXE" -d "$DEV" -s "$SLT"
524513
+else
524513
+    if [ -n "${RESULT}" ]; then
524513
+        # Keys were rotated.
524513
+        exit 1
524513
+    fi
524513
+fi
524513
diff --git a/src/luks/clevis-luks-report-compare b/src/luks/clevis-luks-report-compare
524513
new file mode 100755
524513
index 0000000..2ba5132
524513
--- /dev/null
524513
+++ b/src/luks/clevis-luks-report-compare
524513
@@ -0,0 +1,71 @@
524513
+#!/usr/bin/bash -e
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2018 Red Hat, Inc.
524513
+# Author: Radovan Sroka <rsroka@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
+SUMMARY="Compare two sets of keys"
524513
+
524513
+if [ "$1" == "--summary" ]; then
524513
+    echo "$SUMMARY"
524513
+    exit 1
524513
+fi
524513
+
524513
+if [ -z "$1" ]; then
524513
+    echo "$0 missing the first argument!"
524513
+    exit 1
524513
+fi
524513
+
524513
+if [ -z "$2" ]; then
524513
+    echo "$0 missing the second argument!"
524513
+    exit 1
524513
+fi
524513
+
524513
+ADV_KEYS="$1" # keys from advertisement
524513
+LUKS_KEYS="$2" # keys from luks metadata
524513
+
524513
+### iterate over adv keys and make thumbprints
524513
+CNT=0
524513
+declare -a ADV_KEYS_ARRAY
524513
+while res="$(jose fmt -j- -g keys -g"$CNT" -o- <<< "$ADV_KEYS")"; do
524513
+    thp="$(echo "$res" | jose jwk thp -i-)"
524513
+    ADV_KEYS_ARRAY["$CNT"]="$thp"
524513
+    CNT=$(( CNT + 1 ))
524513
+done
524513
+
524513
+CNT=0
524513
+while key="$(jose fmt -j- -g keys -g"$CNT" -o- <<< "$LUKS_KEYS")"; do
524513
+    thp="$(echo "$key" | jose jwk thp -i-)"
524513
+
524513
+    FOUND=0
524513
+    for k in "${ADV_KEYS_ARRAY[@]}"
524513
+    do
524513
+        if [ "$k" = "$thp" ]; then
524513
+            FOUND=1
524513
+            break
524513
+        fi
524513
+    done
524513
+
524513
+    if [ "$FOUND" -eq "0" ]; then
524513
+        echo "Key \"$thp\" is not in the advertisement and was probably rotated!"
524513
+        echo "$key"
524513
+        echo
524513
+    fi
524513
+    CNT=$(( CNT + 1 ))
524513
+done
524513
+
524513
+exit 0
524513
diff --git a/src/luks/clevis-luks-report-decode b/src/luks/clevis-luks-report-decode
524513
new file mode 100755
524513
index 0000000..f39d1e9
524513
--- /dev/null
524513
+++ b/src/luks/clevis-luks-report-decode
524513
@@ -0,0 +1,59 @@
524513
+#!/usr/bin/bash -e
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2018 Red Hat, Inc.
524513
+# Author: Radovan Sroka <rsroka@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
+. clevis-luks-common-functions
524513
+
524513
+SUMMARY="Decode luks header"
524513
+
524513
+if [ "$1" == "--summary" ]; then
524513
+    echo "$SUMMARY"
524513
+    exit 1
524513
+fi
524513
+
524513
+if [ -z "$1" ]; then
524513
+    echo "$0 missing the first argument!"
524513
+    exit 1
524513
+fi
524513
+
524513
+DATA_CODED="$1"
524513
+
524513
+if DATA_CODED="$(jose jwe fmt -i- <<< "$DATA_CODED")"; then
524513
+    DATA_CODED="$(jose fmt -j- -g protected -u- <<< "$DATA_CODED")"
524513
+    DATA_DECODED="$(jose b64 dec -i- <<< "$DATA_CODED")"
524513
+else
524513
+    echo "Error decoding JWE protected header!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+### get pin and url
524513
+
524513
+if ! PIN="$(jose fmt -j- -g clevis -g pin -u- <<< "$DATA_DECODED")" || [ -z "$PIN" ]; then
524513
+    echo "Pin wasn't found in luks metadata!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+if ! CONTENT="$(jose fmt -j- -g clevis -g "$PIN" -o- <<< "$DATA_DECODED")" || [ -z "$CONTENT" ]; then
524513
+    echo "Content wasn't found!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+EXE="$(findexe clevis-luks-report-"$PIN")"
524513
+
524513
+exec "$EXE" "$CONTENT"
524513
diff --git a/src/luks/clevis-luks-report-sss b/src/luks/clevis-luks-report-sss
524513
new file mode 100755
524513
index 0000000..1dba4c1
524513
--- /dev/null
524513
+++ b/src/luks/clevis-luks-report-sss
524513
@@ -0,0 +1,53 @@
524513
+#!/bin/bash -e
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2018 Red Hat, Inc.
524513
+# Author: Radovan Sroka <rsroka@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
+. clevis-luks-common-functions
524513
+
524513
+SUMMARY="SSS report plugin"
524513
+
524513
+if [ "$1" == "--summary" ]; then
524513
+    echo "$SUMMARY"
524513
+    exit 1
524513
+fi
524513
+
524513
+if [ -z "$1" ]; then
524513
+    echo "$0 missing the first argument!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+CONTENT="$1" # sss content
524513
+
524513
+CNT=0
524513
+while DATA_CODED="$(jose fmt -j- -g jwe -g"$CNT" -u- <<< "$CONTENT")"; do
524513
+    if [ -z "$DATA_CODED" ]; then
524513
+        CNT=$(( CNT + 1 ))
524513
+        continue # in some cases it can be empty string
524513
+    fi
524513
+
524513
+    EXE="$(findexe clevis-luks-report-decode)"
524513
+    if ! $EXE "$DATA_CODED"; then
524513
+        echo "Failed" >&2
524513
+        exit 1
524513
+    fi
524513
+
524513
+    CNT=$(( CNT + 1 ))
524513
+done
524513
+
524513
+exit 0
524513
diff --git a/src/luks/clevis-luks-report-tang b/src/luks/clevis-luks-report-tang
524513
new file mode 100755
524513
index 0000000..07f2a72
524513
--- /dev/null
524513
+++ b/src/luks/clevis-luks-report-tang
524513
@@ -0,0 +1,67 @@
524513
+#!/usr/bin/bash -e
524513
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
524513
+#
524513
+# Copyright (c) 2018 Red Hat, Inc.
524513
+# Author: Radovan Sroka <rsroka@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
+. clevis-luks-common-functions
524513
+
524513
+SUMMARY="Tang report plugin"
524513
+
524513
+if [ "$1" == "--summary" ]; then
524513
+    echo "$SUMMARY"
524513
+    exit 1
524513
+fi
524513
+
524513
+if [ -z "$1" ]; then
524513
+    echo "$0 missing the first argument!"
524513
+    exit 1
524513
+fi
524513
+
524513
+CONTENT="$1"
524513
+
524513
+### Get the advertisement
524513
+if ! URL="$(jose fmt -j- -g url -u- <<< "$CONTENT")" || [ -z "$URL" ]; then
524513
+    echo "URL was not found!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+if ! jws="$(curl -sfg "$URL/adv")"; then
524513
+    echo "Unable to fetch advertisement: $URL/adv!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+if ! TANG_KEYS="$(jose fmt -j- -Og payload -SyOg keys -AUo- <<< "$jws")"; then
524513
+    echo "Advertisement is malformed!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+### Check advertisement validity
524513
+ver="$(jose jwk use -i- -r -u verify -o- <<< "$TANG_KEYS")"
524513
+if ! jose jws ver -i "$jws" -k- -a <<< "$ver"; then
524513
+    echo "Advertisement is missing signatures!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+if ! LUKS_KEYS="$(jose fmt -j- -g adv -o- <<< "$CONTENT")" || [ -z "$LUKS_KEYS" ]; then
524513
+    echo "LUKS keys from LUKS metadata were not found!" >&2
524513
+    exit 1
524513
+fi
524513
+
524513
+EXE="$(findexe clevis-luks-report-compare)"
524513
+
524513
+exec "$EXE" "$TANG_KEYS" "$LUKS_KEYS"
524513
diff --git a/src/luks/clevis-luks-report.1.adoc b/src/luks/clevis-luks-report.1.adoc
524513
new file mode 100644
524513
index 0000000..cf42afe
524513
--- /dev/null
524513
+++ b/src/luks/clevis-luks-report.1.adoc
524513
@@ -0,0 +1,41 @@
524513
+CLEVIS-LUKS-REPORT(1)
524513
+=====================
524513
+:doctype: manpage
524513
+
524513
+
524513
+== NAME
524513
+
524513
+clevis-luks-report - Reports whether a pin bound to a LUKS1 or LUKS2 volume has been rotated
524513
+
524513
+== SYNOPSIS
524513
+
524513
+*clevis luks report* -d DEV -s SLT
524513
+
524513
+== OVERVIEW
524513
+
524513
+The *clevis luks report* command checks a given slot of a LUKS device and reports whether the pin bound to it
524513
+-- if any -- has been rotated.
524513
+
524513
+== OPTIONS
524513
+
524513
+* *-d* _DEV_ :
524513
+  The bound LUKS device
524513
+
524513
+* *-s* _SLT_ :
524513
+  The slot or key slot number for the pin to be verified
524513
+
524513
+* *-q* :
524513
+  Quiet mode. If used, we will not prompt whether to regenerate data with *clevis luks regen*
524513
+
524513
+* *-r* :
524513
+  Regenerates LUKS metadata with *clevis luks regen -d DEV -s SLOT*
524513
+
524513
+== EXAMPLE
524513
+
524513
+    Check whether the pin bound to slot 1 in /dev/sda1 has been rotated:
524513
+
524513
+    # clevis luks report -d /dev/sda1 -s 1
524513
+
524513
+== SEE ALSO
524513
+
524513
+link:clevis-luks-regen.1.adoc[*clevis-luks-regen*(1)]
524513
diff --git a/src/luks/meson.build b/src/luks/meson.build
524513
index 1f64ab0..7c045c4 100644
524513
--- a/src/luks/meson.build
524513
+++ b/src/luks/meson.build
524513
@@ -15,6 +15,18 @@ if libcryptsetup.found() and luksmeta.found() and pwmake.found()
524513
   bins += join_paths(meson.current_source_dir(), 'clevis-luks-bind')
524513
   mans += join_paths(meson.current_source_dir(), 'clevis-luks-bind.1')
524513
 
524513
+  bins += join_paths(meson.current_source_dir(), 'clevis-luks-common-functions')
524513
+
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-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
+  bins += join_paths(meson.current_source_dir(), 'clevis-luks-report-sss')
524513
+  bins += join_paths(meson.current_source_dir(), 'clevis-luks-report-tang')
524513
+  mans += join_paths(meson.current_source_dir(), 'clevis-luks-report.1')
524513
+
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
-- 
524513
2.21.0
524513