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

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