Debian Codegraph @

src/pins/template/clevis-encrypt-@pin@

raw
1 #%# Creating an encrypting pin
2 #%#
3 #%# Read README.md first.
4 #%#
5 #%# The shell interpreter. For portability, it should have as little
6 #%# requirements as possibly. Especially the decrypt should be executable
7 #%# in busybox' ash as well
8 #!/bin/sh
9
10 #%# Some hardening. Safeguard against coding errors.
11 set -eu
12
13 #%# Legal stuff. Put your name etc. here.
14 #%# Of course you're not bound to GPL-3+ but it will certainly ease
15 #%# inclusion in upstream clevis if you use that.
16 # Copyright (c) @year@ @name@
17 # Author: @name@ <@email@>
18 #
19 # This program is free software: you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation, either version 3 of the License, or
22 # (at your option) any later version.
23 #
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 #
29 # You should have received a copy of the GNU General Public License
30 # along with this program. If not, see <https://www.gnu.org/licenses/>.
31 #
32
33 #%# A one-line summary. Will be used in the help messages below.
34 SUMMARY='Encrypts using a @pin@ @@ policy'
35
36 #%# Some option parsing, very simple.
37 #%# Don't touch, it's hardcoded in the `clevis` program.
38 if [ "${1:-}" = '--summary' ] ; then
39 echo "$SUMMARY"
40 exit 0
41 fi
42
43 #%# Regular operation assumes output goes to a file. If not, print
44 #%# some usage information and bail out.
45 if [ -t 0 ] ; then
46 #!# Since this script runs in a pipe, *all* operational messages
47 #!# must go to stderr.
48 exec >&2
49 echo
50 #%# Don't be confused: This script is called from `clevis`, so
51 #%# the usage text has spaces, not dashes.
52 #%# Also, the configuration is in $1, see below.
53 echo 'Usage: clevis encrypt @pin@ CONFIG < PLAINTEXT > JWE'
54 echo
55 echo "$SUMMARY"
56 echo
57 echo 'This command uses the following configuration properties:'
58 echo
59 #%# For the sake of users: Give a good explanation of your pin's
60 #%# parameters.
61 #%# Mandatory parameters should contain the string "REQUIRED"
62 echo ' @mand1@: <string> One parameter @@ (REQUIRED)'
63 echo
64 echo ' @mand2@: <string> Another parameter @@ (REQUIRED)'
65 echo
66 #%# Optional parameters should mention the default value.
67 echo ' @opt1@: <string> An optional parameter @@ (default: @@)'
68 echo
69 #%# Pure visual: Make sure the short descriptions are aligned to the
70 #%# same column.
71 exit 2
72 fi
73
74 #%# The CONFIG parameter in $1 has to be valid JSON
75 if ! cfg="$(jose fmt --json="${1:-}" --object --output=- 2>/dev/null)" ; then
76 echo 'Configuration is malformed!' >&2
77 exit 1
78 fi
79
80 #%# Load the values from the configuration into shell variables.
81 #%# Re-using the name is certainly a good idea
82 #%#
83 #%# For mandatory parameters it's like that:
84 if ! @mand1@="$(jose fmt --json="$cfg" --object --get @mand1@ --unquote=-)" ; then
85 echo 'Missing the required @mand1@ property!' >&2
86 exit 1
87 fi
88 if ! @mand2@="$(jose fmt --json="$cfg" --object --get @mand2@ --unquote=-)" ; then
89 echo 'Missing the required @mand2@ property!' >&2
90 exit 1
91 fi
92
93 #%# For optional parameters, use:
94 @opt1@="$(jose fmt --json="$cfg" --object --get @opt1@ --unquote=-)" || @opt1@='@@'
95
96 #%# Possibly validate parameters. If a check can be done at *en*crypt
97 #%# time, it should be done now.
98
99 #%# Now everything is set up for your pin's business logic.
100 #%#
101 #%# Your jobs, in no particular order:
102 #%#
103 #%# 1. Have the key in `jwk`:
104 #%# If you want to create a new key:
105 jwk="$(jose jwk gen --input='{"alg":"A256GCM"}')"
106 #%# Feel free to use different algorithms for `"alg"`,
107 #%# jose-jwk-gen(1) and jose-alg(1) have more on all this.
108 #%#
109 #%# Or, if you want to use an existing key, just load it into `jwk`
110 #%# from wherever you got it from:
111 jwk="$(somehow_get_the_key)"
112 #%# Remember the result must be a valid jwk object.
113
114 #%# 2. Store the key somewhere. That's your logic.
115 store_the_jwk "$jwk"
116 #%# It is a good idea to store the entire `$jwk`. If you want to
117 #%# extract the actual key, use
118 #%# jose fmt --json="$jwk" --object --get k --unquote=-
119 #%# ... but you're probably wrong if you want to do that.
120
121 #%# 3. Assemble all the information you will need to re-create
122 #%# the key later in `jwe`:
123 #%#
124 #%# First create a skeleton that declares the pin, and creates a store.
125 jwe='{"protected":{"clevis":{"pin":"@pin@","@pin@":{}}}}'
126 #%# Then populate that store. Possibly you'll just have to pass
127 #%# the parameters. Leave out those you will not need for decryption.
128 #%# NB: The long form of the `-U` parameter of `jose fmt` is "--unwind"
129 jwe="$(jose fmt --json="$jwe" --get protected --get clevis --get @pin@ --quote "$@mand1@" --set @mand1@ -UUUU --output=-)"
130 jwe="$(jose fmt --json="$jwe" --get protected --get clevis --get @pin@ --quote "$@opt1@" --set @opt1@ -UUUU --output=-)"
131
132 #%# Almost there!
133 #%# Forward everything to `jose jwe enc` which does the encryption job -
134 #%# including reading the plaintext from stdin which gets replicated
135 #%# using `cat`.
136 ( printf '%s' "$jwe$jwk" ; cat ) | exec jose jwe enc --input=- --key=- --detached=- --compact
137 #%# Anything that follows is executed only if jose failed.
138 #%#
139 #!# When using mktemp or the like, cleaning up has to be done
140 #!# manually. See clevis-{en,de}crypt-tpm2 for an example.