| 1 |
#! /bin/sh |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
set -eu |
| 5 |
|
| 6 |
# clevis-luks-passphrase [--type] DEV |
| 7 |
# |
| 8 |
# Try to recover a passphrase via clevis that will open DEV, and |
| 9 |
# output it on stdout. Such a passphrase can be used for things like |
| 10 |
# resizing of LUKSv2 containers, or adding/changing keys. |
| 11 |
# |
| 12 |
# If "--type" is given, this tool writes just "clevis" to stdout when |
| 13 |
# a passphrase is found, instead of the actual passphrase. This is |
| 14 |
# useful to limit exposure of the passphrase when all you want to know |
| 15 |
# is whether "clevis luks unlock" can be expected to succeed. |
| 16 |
|
| 17 |
opt_type=no |
| 18 |
if [ $# -gt 1 ] && [ "$1" = "--type" ]; then |
| 19 |
opt_type=yes |
| 20 |
shift |
| 21 |
fi |
| 22 |
|
| 23 |
if [ $# -ne 1 ]; then |
| 24 |
echo "usage: $0 [--type] DEV" >&2 |
| 25 |
exit 1 |
| 26 |
fi |
| 27 |
|
| 28 |
DEV="$1" |
| 29 |
|
| 30 |
if cryptsetup isLuks --type luks1 "$DEV"; then |
| 31 |
|
| 32 |
# The UUID that clevis uses for its luksmeta slots, |
| 33 |
# see for example /usr/bin/clevis-luks-bind |
| 34 |
# |
| 35 |
CLEVIS_UUID=cb6e8904-81ff-40da-a84a-07ab9ab5715e |
| 36 |
|
| 37 |
luksmeta test -d "$DEV" 2>/dev/null || exit 0 |
| 38 |
|
| 39 |
luksmeta show -d "$DEV" | while read slot state uuid; do |
| 40 |
if [ "$state" = "active" ] && [ "$uuid" = "$CLEVIS_UUID" ]; then |
| 41 |
if pp=$(luksmeta load -d "$DEV" -s "$slot" | clevis decrypt); then |
| 42 |
if [ "$opt_type" = yes ]; then |
| 43 |
echo clevis |
| 44 |
else |
| 45 |
printf '%s\n' "$pp" |
| 46 |
fi |
| 47 |
break |
| 48 |
fi |
| 49 |
fi |
| 50 |
done |
| 51 |
|
| 52 |
elif cryptsetup isLuks --type luks2 "$DEV"; then |
| 53 |
for id in `cryptsetup luksDump "$DEV" | sed -rn 's|^\s+([0-9]+): clevis|\1|p'`; do |
| 54 |
tok=`cryptsetup token export --token-id "$id" "$DEV"` |
| 55 |
jwe=`printf '%s\n' "$tok" | jose fmt -j- -Og jwe -o- | jose jwe fmt -i- -c` |
| 56 |
|
| 57 |
if pt=`printf '%s' "$jwe" | clevis decrypt`; then |
| 58 |
if [ "$opt_type" = yes ]; then |
| 59 |
echo clevis |
| 60 |
else |
| 61 |
printf '%s\n' "$pt" |
| 62 |
fi |
| 63 |
break |
| 64 |
fi |
| 65 |
done |
| 66 |
fi |