| 1 |
#!/bin/bash -e |
| 2 |
# vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: |
| 3 |
# |
| 4 |
# Copyright (c) 2018, 2020 Red Hat, Inc. |
| 5 |
# Author: Radovan Sroka <[email protected]> |
| 6 |
# Author: Sergio Correia <[email protected]> |
| 7 |
# |
| 8 |
# This program is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License as published by |
| 10 |
# the Free Software Foundation, either version 3 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# This program is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License |
| 19 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
# |
| 21 |
|
| 22 |
. clevis-luks-common-functions |
| 23 |
|
| 24 |
SUMMARY="Report tang keys' rotations" |
| 25 |
|
| 26 |
if [ "${1}" = "--summary" ]; then |
| 27 |
echo "${SUMMARY}" |
| 28 |
exit 0 |
| 29 |
fi |
| 30 |
|
| 31 |
report_compare() { |
| 32 |
local adv_keys="${1}" |
| 33 |
local mdata_keys="${2}" |
| 34 |
[ -z "${adv_keys}" ] && return 1 |
| 35 |
[ -z "${mdata_keys}" ] && return 1 |
| 36 |
|
| 37 |
local thp keys |
| 38 |
for thp in $(printf '%s' "${mdata_keys}" | jose jwk thp --input=-); do |
| 39 |
if ! printf '%s' "${adv_keys}" | jose jwk thp --input=- \ |
| 40 |
--find "${thp}" >/dev/null; then |
| 41 |
keys="$(printf '%s %s' "${keys}" "${thp}")" |
| 42 |
fi |
| 43 |
done |
| 44 |
printf '%s' "${keys}" |
| 45 |
} |
| 46 |
|
| 47 |
report_tang() { |
| 48 |
local content="${1}" |
| 49 |
[ -z "${content}" ] && return 1 |
| 50 |
|
| 51 |
local url |
| 52 |
if ! url="$(jose fmt --json="${content}" --get url --unquote=-)" \ |
| 53 |
|| [ -z "${url}" ]; then |
| 54 |
echo "Invalid tang metadata; URL not found" >&2 |
| 55 |
return 1 |
| 56 |
fi |
| 57 |
|
| 58 |
local jws |
| 59 |
if ! jws="$(curl -sfg "${url}/adv")"; then |
| 60 |
echo "Unable to fetch advertisement (${url}/adv)" >&2 |
| 61 |
return 1 |
| 62 |
fi |
| 63 |
|
| 64 |
local adv_keys |
| 65 |
if ! adv_keys="$(jose fmt --json="${jws}" --object --get payload \ |
| 66 |
--string --b64load --object --get keys \ |
| 67 |
--array --unwind --output=-)"; then |
| 68 |
echo "Advertisement is malformed" >&2 |
| 69 |
return 1 |
| 70 |
fi |
| 71 |
|
| 72 |
# Check advertisement validity. |
| 73 |
local ver |
| 74 |
if ! ver="$(printf '%s' "${adv_keys}" | jose jwk use --input=- \ |
| 75 |
--required \ |
| 76 |
--use=verify \ |
| 77 |
--output=-)"; then |
| 78 |
echo "Unable to validate advertisement" >&2 |
| 79 |
return 1 |
| 80 |
fi |
| 81 |
if ! printf '%s' "${ver}" | jose jws ver --input="${jws}" --key=- \ |
| 82 |
--all; then |
| 83 |
echo "Advertisement is missing signatures" >&2 |
| 84 |
return 1 |
| 85 |
fi |
| 86 |
local mdata_keys |
| 87 |
if ! mdata_keys="$(jose fmt --json="${content}" --get adv --output=-)" \ |
| 88 |
|| [ -z "${mdata_keys}" ]; then |
| 89 |
echo "Keys from clevis metadata not found" >&2 |
| 90 |
return 1 |
| 91 |
fi |
| 92 |
report_compare "${adv_keys}" "${mdata_keys}" |
| 93 |
} |
| 94 |
|
| 95 |
report_sss() { |
| 96 |
local content="${1}" |
| 97 |
[ -z "${content}" ] && return 1 |
| 98 |
|
| 99 |
local jwe |
| 100 |
for jwe in $(jose fmt --json="${content}" --get jwe --foreach=-); do |
| 101 |
jwe="$(printf '%s' "${jwe}" | sed -e 's/"//g')" |
| 102 |
report_decode "${jwe}" |
| 103 |
done |
| 104 |
} |
| 105 |
|
| 106 |
report_decode() { |
| 107 |
local data64="${1}" |
| 108 |
[ -z "${data64}" ] && return 1 |
| 109 |
|
| 110 |
local data |
| 111 |
if ! data="$(clevis_luks_decode_jwe "${data64}")" || [ -z "${data}" ]; then |
| 112 |
echo "Unable to decode metadata" >&2 |
| 113 |
exit 1 |
| 114 |
fi |
| 115 |
|
| 116 |
local pin |
| 117 |
if ! pin="$(jose fmt --json="${data}" --get clevis --get pin --unquote=-)" \ |
| 118 |
|| [ -z "${pin}" ]; then |
| 119 |
echo "Pin not found in clevis metadata" >&2 |
| 120 |
exit 1 |
| 121 |
fi |
| 122 |
|
| 123 |
local content |
| 124 |
if ! content="$(jose fmt --json="${data}" --get clevis --get "${pin}" \ |
| 125 |
--output=-)" || [ -z "${content}" ]; then |
| 126 |
echo "Invalid pin metadata; no content found" >&2 |
| 127 |
return 1 |
| 128 |
fi |
| 129 |
|
| 130 |
case "${pin}" in |
| 131 |
tang) |
| 132 |
report_tang "${content}" |
| 133 |
;; |
| 134 |
sss) |
| 135 |
report_sss "${content}" |
| 136 |
;; |
| 137 |
esac |
| 138 |
} |
| 139 |
|
| 140 |
usage_and_exit () { |
| 141 |
exec >&2 |
| 142 |
echo "Usage: clevis luks report [-q] [-r] -d DEV -s SLOT" |
| 143 |
echo |
| 144 |
echo "${SUMMARY}" |
| 145 |
echo |
| 146 |
echo " -d DEV The LUKS device to check for key rotations" |
| 147 |
echo |
| 148 |
echo " -s SLT The LUKS slot to use" |
| 149 |
echo |
| 150 |
echo " -q Quiet mode; do not prompt for using 'clevis luks regen'" |
| 151 |
echo |
| 152 |
echo " -r Regenerate binding with 'clevis luks regen -q -d DEV -s SLOT'" |
| 153 |
echo |
| 154 |
exit "${1}" |
| 155 |
} |
| 156 |
|
| 157 |
while getopts "hd:s:rq" o; do |
| 158 |
case "${o}" in |
| 159 |
d) DEV="${OPTARG}";; |
| 160 |
h) usage_and_exit 0;; |
| 161 |
r) ROPT="regen";; |
| 162 |
s) SLT="${OPTARG}";; |
| 163 |
q) QOPT="quiet";; |
| 164 |
*) usage_and_exit 1;; |
| 165 |
esac |
| 166 |
done |
| 167 |
|
| 168 |
if [ -z "${DEV}" ]; then |
| 169 |
echo "Did not specify a device!" >&2 |
| 170 |
exit 1 |
| 171 |
fi |
| 172 |
|
| 173 |
if [ -z "${SLT}" ]; then |
| 174 |
echo "Did not specify a slot!" >&2 |
| 175 |
exit 1 |
| 176 |
fi |
| 177 |
|
| 178 |
if ! data64="$(clevis_luks_read_slot "${DEV}" "${SLT}")" \ |
| 179 |
|| [ -z "${data64}" ]; then |
| 180 |
# Error message was already displayed by clevis_luks_read_slot(), |
| 181 |
# at this point. |
| 182 |
exit 1 |
| 183 |
fi |
| 184 |
|
| 185 |
if ! keys="$(report_decode "${data64}")"; then |
| 186 |
echo "Unable to verify whether there are rotated keys" >&2 |
| 187 |
exit 1 |
| 188 |
fi |
| 189 |
|
| 190 |
# No rotated keys. |
| 191 |
[ -z "${keys}" ] && exit 0 |
| 192 |
|
| 193 |
echo "The following keys are not in the current advertisement and were probably rotated:" |
| 194 |
for k in ${keys}; do |
| 195 |
printf ' %s\n' "${k}" |
| 196 |
done |
| 197 |
|
| 198 |
|
| 199 |
if [ -z "${QOPT}" ] && [ -z "${ROPT}" ]; then |
| 200 |
read -r -p "Do you want to regenerate the binding with \"clevis luks regen -q -d ${DEV} -s ${SLT}\"? [ynYN] " ans |
| 201 |
if [ "${ans}" = "y" ] || [ "${ans}" = "Y" ]; then |
| 202 |
ROPT="regen" |
| 203 |
fi |
| 204 |
fi |
| 205 |
|
| 206 |
if [ "${ROPT}" = "regen" ]; then |
| 207 |
if ! EXE="$(command -v clevis-luks-regen)" || [ -z "${EXE}" ]; then |
| 208 |
echo "Unable to find clevis luks regen" >&2 |
| 209 |
exit 1 |
| 210 |
fi |
| 211 |
exec "${EXE}" -q -d "${DEV}" -s "${SLT}" |
| 212 |
fi |
| 213 |
exit 1 |