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

64015d
From a85f50f789d69d9ca0a4096a64ac912f5967f97f Mon Sep 17 00:00:00 2001
64015d
From: Sergio Correia <scorreia@redhat.com>
64015d
Date: Sun, 10 May 2020 15:32:50 -0300
64015d
Subject: [PATCH 7/8] Add clevis luks report
64015d
64015d
---
64015d
 src/luks/clevis-luks-report         | 95 +++++++++++++++++++++++++++++
64015d
 src/luks/clevis-luks-report-compare | 71 +++++++++++++++++++++
64015d
 src/luks/clevis-luks-report-decode  | 59 ++++++++++++++++++
64015d
 src/luks/clevis-luks-report-sss     | 53 ++++++++++++++++
64015d
 src/luks/clevis-luks-report-tang    | 67 ++++++++++++++++++++
64015d
 src/luks/clevis-luks-report.1.adoc  | 41 +++++++++++++
64015d
 src/luks/meson.build                |  7 +++
64015d
 7 files changed, 393 insertions(+)
64015d
 create mode 100755 src/luks/clevis-luks-report
64015d
 create mode 100755 src/luks/clevis-luks-report-compare
64015d
 create mode 100755 src/luks/clevis-luks-report-decode
64015d
 create mode 100755 src/luks/clevis-luks-report-sss
64015d
 create mode 100755 src/luks/clevis-luks-report-tang
64015d
 create mode 100644 src/luks/clevis-luks-report.1.adoc
64015d
64015d
diff --git a/src/luks/clevis-luks-report b/src/luks/clevis-luks-report
64015d
new file mode 100755
64015d
index 0000000..f047256
64015d
--- /dev/null
64015d
+++ b/src/luks/clevis-luks-report
64015d
@@ -0,0 +1,95 @@
64015d
+#!/usr/bin/bash -e
64015d
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
64015d
+#
64015d
+# Copyright (c) 2018 Red Hat, Inc.
64015d
+# Author: Radovan Sroka <rsroka@redhat.com>
64015d
+#
64015d
+# This program is free software: you can redistribute it and/or modify
64015d
+# it under the terms of the GNU General Public License as published by
64015d
+# the Free Software Foundation, either version 3 of the License, or
64015d
+# (at your option) any later version.
64015d
+#
64015d
+# This program is distributed in the hope that it will be useful,
64015d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
64015d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64015d
+# GNU General Public License for more details.
64015d
+#
64015d
+# You should have received a copy of the GNU General Public License
64015d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
64015d
+#
64015d
+
64015d
+. clevis-luks-common-functions
64015d
+
64015d
+SUMMARY="Report any key rotation on the server side"
64015d
+
64015d
+if [ "$1" == "--summary" ]; then
64015d
+    echo "$SUMMARY"
64015d
+    exit 0
64015d
+fi
64015d
+
64015d
+function usage_and_exit () {
64015d
+    echo >&2
64015d
+    echo "Usage: clevis luks report [-qr] -d DEV -s SLOT" >&2
64015d
+    echo >&2
64015d
+    echo -e "  -q\t Quiet mode" >&2
64015d
+    echo -e "  -r\t Regenerate luks metadata with \"clevis luks regen -d DEV -s SLOT\"" >&2
64015d
+    echo >&2
64015d
+    echo "$SUMMARY" >&2
64015d
+    echo >&2
64015d
+    exit "$1"
64015d
+}
64015d
+
64015d
+while getopts "hd:s:rq" o; do
64015d
+    case "$o" in
64015d
+    d) DEV="$OPTARG";;
64015d
+    h) usage_and_exit 0;;
64015d
+    r) ROPT="regen";;
64015d
+    s) SLT="$OPTARG";;
64015d
+    q) QOPT="quiet";;
64015d
+    *) usage_and_exit 1;;
64015d
+    esac
64015d
+done
64015d
+
64015d
+### get luks metadata
64015d
+
64015d
+if [ -z "$DEV" ]; then
64015d
+    echo "Did not specify a device!" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if [ -z "$SLT" ]; then
64015d
+    echo "Did not specify a slot!" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if ! DATA_CODED=$(clevis_luks_read_slot "${DEV}" "${SLT}"); then
64015d
+    # Error message was already displayed by clevis_luks_read_slot(),
64015d
+    # at this point.
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+EXE="$(findexe clevis-luks-report-decode)"
64015d
+RESULT="$($EXE "${DATA_CODED}")"
64015d
+
64015d
+if [ -n "$RESULT" ]; then
64015d
+    echo "$RESULT"
64015d
+    echo "Report detected that some keys were rotated."
64015d
+    if [ -z "$QOPT" ]; then
64015d
+        if [ -z "$ROPT" ]; then
64015d
+            read -r -p "Do you want to regenerate luks metadata with \"clevis luks regen -d $DEV -s $SLT\"? [ynYN] " ans < /dev/tty
64015d
+            [[ "$ans" =~ ^[yY]$ ]] && ROPT="regen"
64015d
+        fi
64015d
+    fi
64015d
+else
64015d
+    exit 0
64015d
+fi
64015d
+
64015d
+if [ "$ROPT" = "regen" ]; then
64015d
+    EXE="$(findexe clevis-luks-regen)"
64015d
+    exec "$EXE" -d "$DEV" -s "$SLT"
64015d
+else
64015d
+    if [ -n "${RESULT}" ]; then
64015d
+        # Keys were rotated.
64015d
+        exit 1
64015d
+    fi
64015d
+fi
64015d
diff --git a/src/luks/clevis-luks-report-compare b/src/luks/clevis-luks-report-compare
64015d
new file mode 100755
64015d
index 0000000..2ba5132
64015d
--- /dev/null
64015d
+++ b/src/luks/clevis-luks-report-compare
64015d
@@ -0,0 +1,71 @@
64015d
+#!/usr/bin/bash -e
64015d
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
64015d
+#
64015d
+# Copyright (c) 2018 Red Hat, Inc.
64015d
+# Author: Radovan Sroka <rsroka@redhat.com>
64015d
+#
64015d
+# This program is free software: you can redistribute it and/or modify
64015d
+# it under the terms of the GNU General Public License as published by
64015d
+# the Free Software Foundation, either version 3 of the License, or
64015d
+# (at your option) any later version.
64015d
+#
64015d
+# This program is distributed in the hope that it will be useful,
64015d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
64015d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64015d
+# GNU General Public License for more details.
64015d
+#
64015d
+# You should have received a copy of the GNU General Public License
64015d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
64015d
+#
64015d
+
64015d
+SUMMARY="Compare two sets of keys"
64015d
+
64015d
+if [ "$1" == "--summary" ]; then
64015d
+    echo "$SUMMARY"
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if [ -z "$1" ]; then
64015d
+    echo "$0 missing the first argument!"
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if [ -z "$2" ]; then
64015d
+    echo "$0 missing the second argument!"
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+ADV_KEYS="$1" # keys from advertisement
64015d
+LUKS_KEYS="$2" # keys from luks metadata
64015d
+
64015d
+### iterate over adv keys and make thumbprints
64015d
+CNT=0
64015d
+declare -a ADV_KEYS_ARRAY
64015d
+while res="$(jose fmt -j- -g keys -g"$CNT" -o- <<< "$ADV_KEYS")"; do
64015d
+    thp="$(echo "$res" | jose jwk thp -i-)"
64015d
+    ADV_KEYS_ARRAY["$CNT"]="$thp"
64015d
+    CNT=$(( CNT + 1 ))
64015d
+done
64015d
+
64015d
+CNT=0
64015d
+while key="$(jose fmt -j- -g keys -g"$CNT" -o- <<< "$LUKS_KEYS")"; do
64015d
+    thp="$(echo "$key" | jose jwk thp -i-)"
64015d
+
64015d
+    FOUND=0
64015d
+    for k in "${ADV_KEYS_ARRAY[@]}"
64015d
+    do
64015d
+        if [ "$k" = "$thp" ]; then
64015d
+            FOUND=1
64015d
+            break
64015d
+        fi
64015d
+    done
64015d
+
64015d
+    if [ "$FOUND" -eq "0" ]; then
64015d
+        echo "Key \"$thp\" is not in the advertisement and was probably rotated!"
64015d
+        echo "$key"
64015d
+        echo
64015d
+    fi
64015d
+    CNT=$(( CNT + 1 ))
64015d
+done
64015d
+
64015d
+exit 0
64015d
diff --git a/src/luks/clevis-luks-report-decode b/src/luks/clevis-luks-report-decode
64015d
new file mode 100755
64015d
index 0000000..f39d1e9
64015d
--- /dev/null
64015d
+++ b/src/luks/clevis-luks-report-decode
64015d
@@ -0,0 +1,59 @@
64015d
+#!/usr/bin/bash -e
64015d
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
64015d
+#
64015d
+# Copyright (c) 2018 Red Hat, Inc.
64015d
+# Author: Radovan Sroka <rsroka@redhat.com>
64015d
+#
64015d
+# This program is free software: you can redistribute it and/or modify
64015d
+# it under the terms of the GNU General Public License as published by
64015d
+# the Free Software Foundation, either version 3 of the License, or
64015d
+# (at your option) any later version.
64015d
+#
64015d
+# This program is distributed in the hope that it will be useful,
64015d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
64015d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64015d
+# GNU General Public License for more details.
64015d
+#
64015d
+# You should have received a copy of the GNU General Public License
64015d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
64015d
+#
64015d
+
64015d
+. clevis-luks-common-functions
64015d
+
64015d
+SUMMARY="Decode luks header"
64015d
+
64015d
+if [ "$1" == "--summary" ]; then
64015d
+    echo "$SUMMARY"
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if [ -z "$1" ]; then
64015d
+    echo "$0 missing the first argument!"
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+DATA_CODED="$1"
64015d
+
64015d
+if DATA_CODED="$(jose jwe fmt -i- <<< "$DATA_CODED")"; then
64015d
+    DATA_CODED="$(jose fmt -j- -g protected -u- <<< "$DATA_CODED")"
64015d
+    DATA_DECODED="$(jose b64 dec -i- <<< "$DATA_CODED")"
64015d
+else
64015d
+    echo "Error decoding JWE protected header!" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+### get pin and url
64015d
+
64015d
+if ! PIN="$(jose fmt -j- -g clevis -g pin -u- <<< "$DATA_DECODED")" || [ -z "$PIN" ]; then
64015d
+    echo "Pin wasn't found in luks metadata!" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if ! CONTENT="$(jose fmt -j- -g clevis -g "$PIN" -o- <<< "$DATA_DECODED")" || [ -z "$CONTENT" ]; then
64015d
+    echo "Content wasn't found!" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+EXE="$(findexe clevis-luks-report-"$PIN")"
64015d
+
64015d
+exec "$EXE" "$CONTENT"
64015d
diff --git a/src/luks/clevis-luks-report-sss b/src/luks/clevis-luks-report-sss
64015d
new file mode 100755
64015d
index 0000000..1dba4c1
64015d
--- /dev/null
64015d
+++ b/src/luks/clevis-luks-report-sss
64015d
@@ -0,0 +1,53 @@
64015d
+#!/bin/bash -e
64015d
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
64015d
+#
64015d
+# Copyright (c) 2018 Red Hat, Inc.
64015d
+# Author: Radovan Sroka <rsroka@redhat.com>
64015d
+#
64015d
+# This program is free software: you can redistribute it and/or modify
64015d
+# it under the terms of the GNU General Public License as published by
64015d
+# the Free Software Foundation, either version 3 of the License, or
64015d
+# (at your option) any later version.
64015d
+#
64015d
+# This program is distributed in the hope that it will be useful,
64015d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
64015d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64015d
+# GNU General Public License for more details.
64015d
+#
64015d
+# You should have received a copy of the GNU General Public License
64015d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
64015d
+#
64015d
+
64015d
+. clevis-luks-common-functions
64015d
+
64015d
+SUMMARY="SSS report plugin"
64015d
+
64015d
+if [ "$1" == "--summary" ]; then
64015d
+    echo "$SUMMARY"
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if [ -z "$1" ]; then
64015d
+    echo "$0 missing the first argument!" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+CONTENT="$1" # sss content
64015d
+
64015d
+CNT=0
64015d
+while DATA_CODED="$(jose fmt -j- -g jwe -g"$CNT" -u- <<< "$CONTENT")"; do
64015d
+    if [ -z "$DATA_CODED" ]; then
64015d
+        CNT=$(( CNT + 1 ))
64015d
+        continue # in some cases it can be empty string
64015d
+    fi
64015d
+
64015d
+    EXE="$(findexe clevis-luks-report-decode)"
64015d
+    if ! $EXE "$DATA_CODED"; then
64015d
+        echo "Failed" >&2
64015d
+        exit 1
64015d
+    fi
64015d
+
64015d
+    CNT=$(( CNT + 1 ))
64015d
+done
64015d
+
64015d
+exit 0
64015d
diff --git a/src/luks/clevis-luks-report-tang b/src/luks/clevis-luks-report-tang
64015d
new file mode 100755
64015d
index 0000000..07f2a72
64015d
--- /dev/null
64015d
+++ b/src/luks/clevis-luks-report-tang
64015d
@@ -0,0 +1,67 @@
64015d
+#!/usr/bin/bash -e
64015d
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
64015d
+#
64015d
+# Copyright (c) 2018 Red Hat, Inc.
64015d
+# Author: Radovan Sroka <rsroka@redhat.com>
64015d
+#
64015d
+# This program is free software: you can redistribute it and/or modify
64015d
+# it under the terms of the GNU General Public License as published by
64015d
+# the Free Software Foundation, either version 3 of the License, or
64015d
+# (at your option) any later version.
64015d
+#
64015d
+# This program is distributed in the hope that it will be useful,
64015d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
64015d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64015d
+# GNU General Public License for more details.
64015d
+#
64015d
+# You should have received a copy of the GNU General Public License
64015d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
64015d
+#
64015d
+
64015d
+. clevis-luks-common-functions
64015d
+
64015d
+SUMMARY="Tang report plugin"
64015d
+
64015d
+if [ "$1" == "--summary" ]; then
64015d
+    echo "$SUMMARY"
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if [ -z "$1" ]; then
64015d
+    echo "$0 missing the first argument!"
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+CONTENT="$1"
64015d
+
64015d
+### Get the advertisement
64015d
+if ! URL="$(jose fmt -j- -g url -u- <<< "$CONTENT")" || [ -z "$URL" ]; then
64015d
+    echo "URL was not found!" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if ! jws="$(curl -sfg "$URL/adv")"; then
64015d
+    echo "Unable to fetch advertisement: $URL/adv!" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if ! TANG_KEYS="$(jose fmt -j- -Og payload -SyOg keys -AUo- <<< "$jws")"; then
64015d
+    echo "Advertisement is malformed!" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+### Check advertisement validity
64015d
+ver="$(jose jwk use -i- -r -u verify -o- <<< "$TANG_KEYS")"
64015d
+if ! jose jws ver -i "$jws" -k- -a <<< "$ver"; then
64015d
+    echo "Advertisement is missing signatures!" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if ! LUKS_KEYS="$(jose fmt -j- -g adv -o- <<< "$CONTENT")" || [ -z "$LUKS_KEYS" ]; then
64015d
+    echo "LUKS keys from LUKS metadata were not found!" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+EXE="$(findexe clevis-luks-report-compare)"
64015d
+
64015d
+exec "$EXE" "$TANG_KEYS" "$LUKS_KEYS"
64015d
diff --git a/src/luks/clevis-luks-report.1.adoc b/src/luks/clevis-luks-report.1.adoc
64015d
new file mode 100644
64015d
index 0000000..cf42afe
64015d
--- /dev/null
64015d
+++ b/src/luks/clevis-luks-report.1.adoc
64015d
@@ -0,0 +1,41 @@
64015d
+CLEVIS-LUKS-REPORT(1)
64015d
+=====================
64015d
+:doctype: manpage
64015d
+
64015d
+
64015d
+== NAME
64015d
+
64015d
+clevis-luks-report - Reports whether a pin bound to a LUKS1 or LUKS2 volume has been rotated
64015d
+
64015d
+== SYNOPSIS
64015d
+
64015d
+*clevis luks report* -d DEV -s SLT
64015d
+
64015d
+== OVERVIEW
64015d
+
64015d
+The *clevis luks report* command checks a given slot of a LUKS device and reports whether the pin bound to it
64015d
+-- if any -- has been rotated.
64015d
+
64015d
+== OPTIONS
64015d
+
64015d
+* *-d* _DEV_ :
64015d
+  The bound LUKS device
64015d
+
64015d
+* *-s* _SLT_ :
64015d
+  The slot or key slot number for the pin to be verified
64015d
+
64015d
+* *-q* :
64015d
+  Quiet mode. If used, we will not prompt whether to regenerate data with *clevis luks regen*
64015d
+
64015d
+* *-r* :
64015d
+  Regenerates LUKS metadata with *clevis luks regen -d DEV -s SLOT*
64015d
+
64015d
+== EXAMPLE
64015d
+
64015d
+    Check whether the pin bound to slot 1 in /dev/sda1 has been rotated:
64015d
+
64015d
+    # clevis luks report -d /dev/sda1 -s 1
64015d
+
64015d
+== SEE ALSO
64015d
+
64015d
+link:clevis-luks-regen.1.adoc[*clevis-luks-regen*(1)]
64015d
diff --git a/src/luks/meson.build b/src/luks/meson.build
64015d
index f21388d..ee588c3 100644
64015d
--- a/src/luks/meson.build
64015d
+++ b/src/luks/meson.build
64015d
@@ -47,6 +47,13 @@ if libcryptsetup.found() and luksmeta.found() and pwmake.found()
64015d
 
64015d
   bins += join_paths(meson.current_source_dir(), 'clevis-luks-regen')
64015d
   mans += join_paths(meson.current_source_dir(), 'clevis-luks-regen.1')
64015d
+
64015d
+  bins += join_paths(meson.current_source_dir(), 'clevis-luks-report')
64015d
+  bins += join_paths(meson.current_source_dir(), 'clevis-luks-report-compare')
64015d
+  bins += join_paths(meson.current_source_dir(), 'clevis-luks-report-decode')
64015d
+  bins += join_paths(meson.current_source_dir(), 'clevis-luks-report-sss')
64015d
+  bins += join_paths(meson.current_source_dir(), 'clevis-luks-report-tang')
64015d
+  mans += join_paths(meson.current_source_dir(), 'clevis-luks-report.1')
64015d
 else
64015d
   warning('Will not install LUKS support due to missing dependencies!')
64015d
 endif
64015d
-- 
64015d
2.18.4
64015d