Debian Codegraph @

src/clevis-decrypt

raw
1 #!/bin/bash -e
2 # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
3 #
4 # Copyright (c) 2017 Red Hat, Inc.
5 # Author: Nathaniel McCallum <[email protected]>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 SUMMARY="Decrypts using the policy defined at encryption time"
22
23 function findexe() {
24 [ $# -eq 1 ] || return 1
25 while read -r -d: path; do
26 [ -f "$path/$1" ] && [ -x "$path/$1" ] && echo "$path/$1" && return 0
27 done <<< "$PATH:"
28 return 1
29 }
30
31 if [ $# -eq 1 ] && [ "$1" == "--summary" ]; then
32 echo "$SUMMARY"
33 exit 0
34 fi
35
36 if ! [ -t 0 ]; then
37 read -r -d . hdr
38
39 if ! pin="$(jose fmt -q "$hdr" -SyOg clevis -Og pin -Su-)"; then
40 echo "JWE is missing the required 'clevis.pin' header property!" >&2
41 exit 1
42 fi
43
44 if ! cmd="$(findexe clevis-decrypt-"$pin")"; then
45 echo "Unable to locate pin '$pin'!" >&2
46 exit 1
47 fi
48
49 (echo -n "$hdr."; /bin/cat) | "$cmd"
50 exit $?
51 fi
52
53 exec >&2
54 echo
55 echo "Usage: clevis decrypt < JWE > PLAINTEXT"
56 echo
57 echo "$SUMMARY"
58 echo
59
60 exit 2