| 1 |
#%# Creating an decrypting pin |
| 2 |
#%# |
| 3 |
#%# Read README.md and clevis-encrypt-@pin@ first, this file aims to |
| 4 |
#%# to avoid information duplication. |
| 5 |
#%# Unfortunately, this one uses a bashism (read -d) that is not at |
| 6 |
#%# all easy to eliminate. |
| 7 |
#!/bin/bash |
| 8 |
|
| 9 |
set -eu |
| 10 |
|
| 11 |
# Copyright (c) @year@ @name@ |
| 12 |
# Author: @name@ <@email@> |
| 13 |
# |
| 14 |
# This program is free software: you can redistribute it and/or modify |
| 15 |
# it under the terms of the GNU General Public License as published by |
| 16 |
# the Free Software Foundation, either version 3 of the License, or |
| 17 |
# (at your option) any later version. |
| 18 |
# |
| 19 |
# This program is distributed in the hope that it will be useful, |
| 20 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
# GNU General Public License for more details. |
| 23 |
# |
| 24 |
# You should have received a copy of the GNU General Public License |
| 25 |
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 26 |
# |
| 27 |
|
| 28 |
#%# This program takes no options - everything needed to know will be |
| 29 |
#%# read from stdin. |
| 30 |
[ $# -eq 1 ] && [ "${1:-}" = "--summary" ] && exit 2 |
| 31 |
|
| 32 |
if [ -t 0 ] ; then |
| 33 |
echo >&2 |
| 34 |
echo 'Usage: clevis decrypt @pin@ < JWE > PLAINTEXT' >&2 |
| 35 |
echo >&2 |
| 36 |
exit 1 |
| 37 |
fi |
| 38 |
|
| 39 |
#%# The input is concatenated using the dot. Read the first element |
| 40 |
#%# but leave everything else in the buffer. Only read -d can to that. |
| 41 |
read -d . hdr64 |
| 42 |
#%# The header is base64-encoded. Decode now and also verify this is valid JSON |
| 43 |
if ! hdr="$(jose fmt --quote="$hdr64" --string --b64load --object --output=-)" ; then |
| 44 |
echo 'JWE header corrupt' >&2 |
| 45 |
exit 1 |
| 46 |
fi |
| 47 |
|
| 48 |
#%# Input validation: The pin must exist by name. |
| 49 |
if [ "$(jose fmt --json="$hdr" --get clevis --get pin --unquote=-)" != '@pin@' ] ; then |
| 50 |
echo 'JWE pin mismatch!' >&2 |
| 51 |
exit 1 |
| 52 |
fi |
| 53 |
|
| 54 |
#%# Load the parameters into shell variables. |
| 55 |
if ! @param1@="$(jose fmt --json="$hdr" --get clevis --get @pin@ --get @param1@ --unquote=-)" ; then |
| 56 |
echo 'JWE missing 'clevis.@pin@.@param1@' header parameter!' >&2 |
| 57 |
exit 1 |
| 58 |
fi |
| 59 |
if ! @param2@="$(jose fmt --json="$hdr" --get clevis --get @pin@ --get @param2@ --unquote=-)" ; then |
| 60 |
echo 'JWE missing 'clevis.@pin@.@param2@' header parameter!' >&2 |
| 61 |
exit 1 |
| 62 |
fi |
| 63 |
|
| 64 |
#%# Possibly some pre-checks on your parameters are needed. |
| 65 |
|
| 66 |
#%# Now everything is set up for your pin's business logic |
| 67 |
#%# |
| 68 |
#%# Your job: Somehow bring the key into `jwk`. |
| 69 |
jwk="$(load_jwk)" |
| 70 |
|
| 71 |
#%# Finally, forward everything to `jose jwe dec` which does the |
| 72 |
#%# decryption job. |
| 73 |
( printf '%s' "$jwk$hdr64." ; cat ) | exec jose jwe dec --key=- --input=- |