Blame SOURCES/0002-Add-the-option-to-extract-luks-passphrase-used-for-b.patch

d78bb9
From aa52396c35e76aabd085a819b08167d559042a20 Mon Sep 17 00:00:00 2001
d78bb9
From: rpm-build <rpm-build>
d78bb9
Date: Tue, 3 Nov 2020 08:42:48 -0300
d78bb9
Subject: [PATCH 2/2] Add the option to extract luks passphrase used for
3700a9
 binding
ef20f4
ef20f4
Usage:
ef20f4
ef20f4
clevis luks pass -d /dev/sda1 -s 1
ef20f4
<passphrase here>
ef20f4
---
d78bb9
 src/luks/clevis-luks-pass        | 64 ++++++++++++++++++++++++++++++++
d78bb9
 src/luks/clevis-luks-pass.1.adoc | 43 +++++++++++++++++++++
ef20f4
 src/luks/meson.build             |  3 ++
d78bb9
 src/luks/tests/meson.build       |  2 +
d78bb9
 src/luks/tests/pass-tang-luks1   | 59 +++++++++++++++++++++++++++++
d78bb9
 src/luks/tests/pass-tang-luks2   | 59 +++++++++++++++++++++++++++++
d78bb9
 6 files changed, 230 insertions(+)
ef20f4
 create mode 100755 src/luks/clevis-luks-pass
ef20f4
 create mode 100644 src/luks/clevis-luks-pass.1.adoc
ef20f4
 create mode 100755 src/luks/tests/pass-tang-luks1
ef20f4
 create mode 100755 src/luks/tests/pass-tang-luks2
ef20f4
ef20f4
diff --git a/src/luks/clevis-luks-pass b/src/luks/clevis-luks-pass
ef20f4
new file mode 100755
d78bb9
index 0000000..1f59b39
ef20f4
--- /dev/null
ef20f4
+++ b/src/luks/clevis-luks-pass
d78bb9
@@ -0,0 +1,64 @@
ef20f4
+#!/bin/bash -e
d78bb9
+# vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
ef20f4
+#
ef20f4
+# Copyright (c) 2019 Red Hat, Inc.
ef20f4
+# Author: Sergio Correia <scorreia@redhat.com> - LUKS2 support.
ef20f4
+#
ef20f4
+# This program is free software: you can redistribute it and/or modify
ef20f4
+# it under the terms of the GNU General Public License as published by
ef20f4
+# the Free Software Foundation, either version 3 of the License, or
ef20f4
+# (at your option) any later version.
ef20f4
+#
ef20f4
+# This program is distributed in the hope that it will be useful,
ef20f4
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
ef20f4
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ef20f4
+# GNU General Public License for more details.
ef20f4
+#
ef20f4
+# You should have received a copy of the GNU General Public License
ef20f4
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
ef20f4
+#
ef20f4
+
ef20f4
+. clevis-luks-common-functions
ef20f4
+
ef20f4
+SUMMARY="Returns the LUKS passphrase used for binding a particular slot."
ef20f4
+
d78bb9
+usage() {
d78bb9
+    exec >&2
d78bb9
+    echo "Usage: clevis luks pass -d DEV -s SLT"
d78bb9
+    echo
d78bb9
+    echo "$SUMMARY"
d78bb9
+    echo
d78bb9
+    echo "  -d DEV  The LUKS device to extract the LUKS passphrase used for binding"
d78bb9
+    echo
d78bb9
+    echo "  -s SLOT The slot number to extract the LUKS passphrase"
d78bb9
+    echo
ef20f4
+    exit 1
ef20f4
+}
ef20f4
+
ef20f4
+if [ ${#} -eq 1 ] && [ "${1}" = "--summary" ]; then
ef20f4
+    echo "${SUMMARY}"
ef20f4
+    exit 0
ef20f4
+fi
ef20f4
+
ef20f4
+while getopts ":d:s:" o; do
ef20f4
+    case "$o" in
ef20f4
+    d) DEV=${OPTARG};;
ef20f4
+    s) SLT=${OPTARG};;
ef20f4
+    *) usage;;
ef20f4
+    esac
ef20f4
+done
ef20f4
+
ef20f4
+if [ -z "${DEV}" ]; then
ef20f4
+    echo "Did not specify a device!" >&2
ef20f4
+    usage
ef20f4
+fi
ef20f4
+
ef20f4
+if [ -z "${SLT}" ]; then
ef20f4
+    echo "Did not specify a slot!" >&2
ef20f4
+    usage
ef20f4
+fi
ef20f4
+
d78bb9
+if ! clevis_luks_unlock_device_by_slot "${DEV}" "${SLT}"; then
d78bb9
+    echo "It was not possible to decrypt the passphrase associated to slot ${SLT} in ${DEV}!" >&2
ef20f4
+    exit 1
ef20f4
+fi
ef20f4
diff --git a/src/luks/clevis-luks-pass.1.adoc b/src/luks/clevis-luks-pass.1.adoc
ef20f4
new file mode 100644
ef20f4
index 0000000..fa9526a
ef20f4
--- /dev/null
ef20f4
+++ b/src/luks/clevis-luks-pass.1.adoc
ef20f4
@@ -0,0 +1,43 @@
ef20f4
+CLEVIS-LUKS-PASS(1)
ef20f4
+===================
ef20f4
+:doctype: manpage
ef20f4
+
ef20f4
+
ef20f4
+== NAME
ef20f4
+
ef20f4
+clevis-luks-pass - Extracts the passphrase used for binding a particular slot in a LUKS device
ef20f4
+
ef20f4
+== SYNOPSIS
ef20f4
+
ef20f4
+*clevis luks pass* -d DEV -s SLT
ef20f4
+
ef20f4
+== OVERVIEW
ef20f4
+
ef20f4
+The *clevis luks pass* command extracts the passphrase used for binding a particular slot in a LUKS device.
ef20f4
+For example:
ef20f4
+
ef20f4
+    clevis luks pass -d /dev/sda1 -s 1
ef20f4
+
ef20f4
+== OPTIONS
ef20f4
+
ef20f4
+* *-d* _DEV_ :
ef20f4
+  The LUKS device on which to extract a passphrase from
ef20f4
+
ef20f4
+* *-s* _SLT_ :
ef20f4
+  The slot to use for extracting the passphrase
ef20f4
+
ef20f4
+== EXAMPLE
ef20f4
+
ef20f4
+    clevis luks pass -d /dev/sda1 -s 1
ef20f4
+    <passphrase here>
ef20f4
+
ef20f4
+Note that the output of *clevis luks pass* might be non-printable, in which case it would be better to redirect its output to a file and use it as a key
ef20f4
+file together with cryptsetup. For instance:
ef20f4
+
ef20f4
+    clevis luks pass -d /dev/sda1 -s 1 > slot1-passphrase
ef20f4
+
ef20f4
+And the file slot1-passphrase will contain the passphrase associated with slot #1 in /dev/sda1.
ef20f4
+
ef20f4
+== SEE ALSO
ef20f4
+
ef20f4
+link:clevis-luks-unlock.1.adoc[*clevis-luks-unlock*(1)],
ef20f4
diff --git a/src/luks/meson.build b/src/luks/meson.build
d78bb9
index 12f5a0d..008736e 100644
ef20f4
--- a/src/luks/meson.build
ef20f4
+++ b/src/luks/meson.build
d78bb9
@@ -50,6 +50,9 @@ if libcryptsetup.found() and luksmeta.found() and pwmake.found()
ef20f4
 
d78bb9
   bins += join_paths(meson.current_source_dir(), 'clevis-luks-edit')
d78bb9
   mans += join_paths(meson.current_source_dir(), 'clevis-luks-edit.1')
3700a9
+
ef20f4
+  bins += join_paths(meson.current_source_dir(), 'clevis-luks-pass')
ef20f4
+  mans += join_paths(meson.current_source_dir(), 'clevis-luks-pass.1')
3700a9
 else
3700a9
   warning('Will not install LUKS support due to missing dependencies!')
3700a9
 endif
ef20f4
diff --git a/src/luks/tests/meson.build b/src/luks/tests/meson.build
d78bb9
index c22a069..f4584aa 100644
ef20f4
--- a/src/luks/tests/meson.build
ef20f4
+++ b/src/luks/tests/meson.build
d78bb9
@@ -84,6 +84,7 @@ if has_tang
d78bb9
   test('report-tang-luks1', find_program('report-tang-luks1'), env: env, timeout: 90)
d78bb9
   test('report-sss-luks1', find_program('report-sss-luks1'), env: env, timeout: 90)
d78bb9
   test('edit-tang-luks1', find_program('edit-tang-luks1'), env: env, timeout: 150)
d78bb9
+  test('pass-tang-luks1', find_program('pass-tang-luks1'), env: env, timeout: 60)
ef20f4
 endif
ef20f4
 
d78bb9
 test('backup-restore-luks1', find_program('backup-restore-luks1'), env: env, timeout: 60)
d78bb9
@@ -111,6 +112,7 @@ if luksmeta_data.get('OLD_CRYPTSETUP') == '0'
d78bb9
     test('report-tang-luks2', find_program('report-tang-luks2'), env: env, timeout: 120)
d78bb9
     test('report-sss-luks2', find_program('report-sss-luks2'), env: env, timeout: 120)
d78bb9
     test('edit-tang-luks2', find_program('edit-tang-luks2'), env: env, timeout: 210)
d78bb9
+    test('pass-tang-luks2', find_program('pass-tang-luks2'), env: env, timeout: 60)
3700a9
   endif
d78bb9
 
d78bb9
 test('backup-restore-luks2', find_program('backup-restore-luks2'), env: env, timeout: 120)
ef20f4
diff --git a/src/luks/tests/pass-tang-luks1 b/src/luks/tests/pass-tang-luks1
ef20f4
new file mode 100755
d78bb9
index 0000000..0d91e6c
ef20f4
--- /dev/null
ef20f4
+++ b/src/luks/tests/pass-tang-luks1
d78bb9
@@ -0,0 +1,59 @@
ef20f4
+#!/bin/bash -x
ef20f4
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
ef20f4
+#
ef20f4
+# Copyright (c) 2019 Red Hat, Inc.
ef20f4
+# Author: Sergio Correia <scorreia@redhat.com>
ef20f4
+#
ef20f4
+# This program is free software: you can redistribute it and/or modify
ef20f4
+# it under the terms of the GNU General Public License as published by
ef20f4
+# the Free Software Foundation, either version 3 of the License, or
ef20f4
+# (at your option) any later version.
ef20f4
+#
ef20f4
+# This program is distributed in the hope that it will be useful,
ef20f4
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
ef20f4
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ef20f4
+# GNU General Public License for more details.
ef20f4
+#
ef20f4
+# You should have received a copy of the GNU General Public License
ef20f4
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
ef20f4
+#
ef20f4
+
ef20f4
+TEST="${0}"
ef20f4
+. tests-common-functions
d78bb9
+. clevis-luks-common-functions
ef20f4
+
ef20f4
+function on_exit() {
d78bb9
+    [ ! -d "${TMP}" ] && return 0
d78bb9
+    tang_stop "${TMP}"
d78bb9
+    rm -rf "${TMP}"
ef20f4
+}
ef20f4
+
ef20f4
+trap 'on_exit' EXIT
ef20f4
+
d78bb9
+TMP=$(mktemp -d)
d78bb9
+
d78bb9
+port=$(get_random_port)
d78bb9
+tang_run "${TMP}" "${port}" &
d78bb9
+tang_wait_until_ready "${port}"
ef20f4
+
ef20f4
+url="http://localhost:${port}"
ef20f4
+adv="${TMP}/adv"
d78bb9
+tang_get_adv "${port}" "${adv}"
ef20f4
+
ef20f4
+cfg=$(printf '{"url":"%s","adv":"%s"}' "$url" "$adv")
ef20f4
+
ef20f4
+# LUKS1.
ef20f4
+DEV="${TMP}/luks1-device"
ef20f4
+new_device "luks1" "${DEV}"
ef20f4
+
ef20f4
+if ! clevis luks bind -f -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
ef20f4
+    error "${TEST}: Bind should have succeeded."
ef20f4
+fi
ef20f4
+
d78bb9
+# Now let's test the passphrase.
ef20f4
+SLT=1
ef20f4
+PASS=$(clevis luks pass -d "${DEV}" -s "${SLT}")
ef20f4
+echo $PASS >&2
d78bb9
+if ! clevis_luks_check_valid_key_or_keyfile "${DEV}" "${PASS}" "" "${SLT}"; then
ef20f4
+    error "Passphrase obtained from clevis luks pass failed."
ef20f4
+fi
ef20f4
diff --git a/src/luks/tests/pass-tang-luks2 b/src/luks/tests/pass-tang-luks2
ef20f4
new file mode 100755
d78bb9
index 0000000..2d50413
ef20f4
--- /dev/null
ef20f4
+++ b/src/luks/tests/pass-tang-luks2
d78bb9
@@ -0,0 +1,59 @@
ef20f4
+#!/bin/bash -x
ef20f4
+# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
ef20f4
+#
ef20f4
+# Copyright (c) 2019 Red Hat, Inc.
ef20f4
+# Author: Sergio Correia <scorreia@redhat.com>
ef20f4
+#
ef20f4
+# This program is free software: you can redistribute it and/or modify
ef20f4
+# it under the terms of the GNU General Public License as published by
ef20f4
+# the Free Software Foundation, either version 3 of the License, or
ef20f4
+# (at your option) any later version.
ef20f4
+#
ef20f4
+# This program is distributed in the hope that it will be useful,
ef20f4
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
ef20f4
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ef20f4
+# GNU General Public License for more details.
ef20f4
+#
ef20f4
+# You should have received a copy of the GNU General Public License
ef20f4
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
ef20f4
+#
ef20f4
+
ef20f4
+TEST="${0}"
ef20f4
+. tests-common-functions
d78bb9
+. clevis-luks-common-functions
ef20f4
+
ef20f4
+function on_exit() {
d78bb9
+    [ ! -d "${TMP}" ] && return 0
d78bb9
+    tang_stop "${TMP}"
d78bb9
+    rm -rf "${TMP}"
ef20f4
+}
ef20f4
+
ef20f4
+trap 'on_exit' EXIT
ef20f4
+
d78bb9
+TMP=$(mktemp -d)
d78bb9
+
d78bb9
+port=$(get_random_port)
d78bb9
+tang_run "${TMP}" "${port}" &
d78bb9
+tang_wait_until_ready "${port}"
ef20f4
+
ef20f4
+url="http://localhost:${port}"
ef20f4
+adv="${TMP}/adv"
d78bb9
+tang_get_adv "${port}" "${adv}"
ef20f4
+
ef20f4
+cfg=$(printf '{"url":"%s","adv":"%s"}' "$url" "$adv")
ef20f4
+
ef20f4
+# LUKS2.
ef20f4
+DEV="${TMP}/luks2-device"
ef20f4
+new_device "luks2" "${DEV}"
ef20f4
+
ef20f4
+if ! clevis luks bind -f -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
ef20f4
+    error "${TEST}: Bind should have succeeded."
ef20f4
+fi
ef20f4
+
d78bb9
+# Now let's test the passphrase.
ef20f4
+SLT=1
ef20f4
+PASS=$(clevis luks pass -d "${DEV}" -s "${SLT}")
ef20f4
+echo $PASS >&2
d78bb9
+if ! clevis_luks_check_valid_key_or_keyfile "${DEV}" "${PASS}" "" "${SLT}"; then
ef20f4
+    error "Passphrase obtained from clevis luks pass failed."
ef20f4
+fi
ef20f4
-- 
d78bb9
2.29.2
ef20f4