Debian Codegraph @

src/pins/file/clevis-decrypt-file

raw
1 #!/bin/bash
2
3 set -eu
4
5 # Copyright (c) 2020 Christoph Biedl
6 # Author: Christoph Biedl <[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 <https://www.gnu.org/licenses/>.
20 #
21
22 [ $# -eq 1 ] && [ "${1:-}" = "--summary" ] && exit 2
23
24 if [ -t 0 ] ; then
25 echo >&2
26 echo 'Usage: clevis decrypt file < JWE > PLAINTEXT' >&2
27 echo >&2
28 exit 1
29 fi
30
31 read -d . hdr64
32 if ! hdr="$(jose fmt --quote="$hdr64" --string --b64load --object --output=-)" ; then
33 echo 'JWE header corrupt' >&2
34 exit 1
35 fi
36
37 if [ "$(jose fmt --json="$hdr" --get clevis --get pin --unquote=-)" != 'file' ] ; then
38 echo 'JWE pin mismatch!' >&2
39 exit 1
40 fi
41
42 if ! name="$(jose fmt --json="$hdr" --get clevis --get file --get name --unquote=-)" ; then
43 echo 'JWE missing 'clevis.file.name' header parameter!' >&2
44 exit 1
45 fi
46
47 if [ ! -f "$name" ] ; then
48 echo "Key file $name not found" >&2
49 exit 1
50 fi
51
52 jwk="$(cat "$name")"
53
54 if ! jose fmt --json="$jwk" --object --output=/dev/null 2>/dev/null ; then
55 echo "Key file $name is malformed" >&2
56 exit 1
57 fi
58
59 ( printf '%s' "$jwk$hdr64." ; cat ) | exec jose jwe dec --key=- --input=-