Debian Codegraph @

src/pins/file/clevis-encrypt-file

raw
1 #!/bin/sh
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 SUMMARY='Encrypts using a jwk stored in a file policy'
23
24 if [ "${1:-}" = '--summary' ] ; then
25 echo "$SUMMARY"
26 exit 0
27 fi
28
29 if [ -t 0 ] ; then
30 exec >&2
31 echo
32 echo 'Usage: clevis encrypt file CONFIG < PLAINTEXT > JWE'
33 echo
34 echo "$SUMMARY"
35 echo
36 echo 'his command uses the following configuration properties:'
37 echo
38 echo ' name: <string> The file that holds the encryption key (REQUIRED)'
39 echo
40 exit 2
41 fi
42
43 if ! cfg="$(jose fmt --json="$1" --object --output=- 2>/dev/null)" ; then
44 echo 'Configuration is malformed!' >&2
45 exit 1
46 fi
47
48 if ! name="$(jose fmt --json="$cfg" --object --get name --unquote=-)" ; then
49 echo 'Missing the required name property!' >&2
50 exit 1
51 fi
52
53 if [ -e "$name" ] ; then
54 echo "File $name already exists" >&2
55 exit 1
56 fi
57
58 jwk="$(jose jwk gen --input='{"alg":"A256GCM"}')"
59
60 ( umask 0377 ; echo "$jwk" >"$name" )
61
62 jwe='{"protected":{"clevis":{"pin":"file","file":{}}}}'
63 jwe="$(jose fmt --json="$jwe" --get protected --get clevis --get file --quote "$name" --set name -UUUU --output=-)"
64
65 ( printf '%s' "$jwe$jwk" ; cat ) | exec jose jwe enc --input=- --key=- --detached=- --compact