Debian Codegraph @

src/pins/tang/clevis-decrypt-tang

raw symbols SCIP
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 [ $# -eq 1 ] && [ "$1" == "--summary" ] && exit 2
22
23 if [ -t 0 ]; then
24 exec >&2
25 echo
26 echo "Usage: clevis decrypt tang < JWE > PLAINTEXT"
27 echo
28 exit 2
29 fi
30
31 read -r -d . hdr
32
33 if ! jhd="$(jose b64 dec -i- <<< "$hdr")"; then
34 echo "Error decoding JWE protected header!" >&2
35 exit 1
36 fi
37
38 if [ "$(jose fmt -j- -Og clevis -g pin -u- <<< "$jhd")" != "tang" ]; then
39 echo "JWE pin mismatch!" >&2
40 exit 1
41 fi
42
43 if ! clt="$(jose fmt -j- -Og epk -Oo- <<< "$jhd")"; then
44 echo "JWE missing required 'epk' header parameter!" >&2
45 exit 1
46 fi
47
48 if ! kid="$(jose fmt -j- -Og kid -Su- <<< "$jhd")"; then
49 echo "JWE missing required 'kid' header parameter!" >&2
50 exit 1
51 fi
52
53 # Tang advertisement validation.
54 if ! keys="$(jose fmt -j- -Og clevis -g tang -g adv -Oo- <<< "${jhd}")"; then
55 echo "JWE missing required 'clevis.tang.adv' header parameter!" >&2
56 exit 1
57 fi
58
59 # Check if the thumbprint we have in `kid' is in the advertised keys.
60 CLEVIS_DEFAULT_THP_ALG=S256 # SHA-256.
61 CLEVIS_DEFAULT_THP_LEN=43 # Length of SHA-256 thumbprint.
62 CLEVIS_ALTERNATIVE_THP_ALGS=S1 # SHA-1.
63
64 # Issue a warning if we are using a hash that has a shorter length than the
65 # default one.
66 if [ "${#kid}" -lt "${CLEVIS_DEFAULT_THP_LEN}" ]; then
67 echo "WARNING: tang using a deprecated hash for the JWK thumbprints" >&2
68 fi
69
70 if ! srv="$(jose jwk thp -i- -f "${kid}" -a "${CLEVIS_DEFAULT_THP_ALG}" \
71 <<< "${keys}")"; then
72 # `kid' thumprint not in the advertised keys, but it's possible it was
73 # generated using a different algorithm than the default one.
74 # Let us try the alternative supported algorithms to make sure `kid'
75 # really is not part of the advertised keys.
76 for alg in ${CLEVIS_ALTERNATIVE_THP_ALGS}; do
77 srv="$(jose jwk thp -i- -f "$kid" -a "${alg}" <<< "${keys}")" && break
78 done
79 if [ -z "${srv}" ]; then
80 echo "JWE header validation of 'clevis.tang.adv' failed: key thumbprint does not match" >&2
81 exit 1
82 fi
83 fi
84
85 if ! url="$(jose fmt -j- -Og clevis -g tang -g url -Su- <<< "$jhd")"; then
86 echo "JWE missing required 'clevis.tang.url' header parameter!" >&2
87 exit 1
88 fi
89
90 if ! crv="$(jose fmt -j- -Og crv -Su- <<< "$clt")"; then
91 echo "Unable to determine EPK's curve!" >&2
92 exit 1
93 fi
94
95 if ! eph="$(jose jwk gen -i "{\"alg\":\"ECMR\",\"crv\":\"$crv\"}")"; then
96 echo "Error generating ephemeral key!" >&2
97 exit 1
98 fi
99
100 xfr="$(jose jwk exc -i '{"alg":"ECMR"}' -l- -r- <<< "$clt$eph")"
101
102 rec_url="$url/rec/$kid"
103 ct="Content-Type: application/jwk+json"
104 if ! rep="$(curl -sfg -X POST -H "$ct" --data-binary @- "$rec_url" <<< "$xfr")"; then
105 echo "Error communicating with server $url" >&2
106 exit 1
107 fi
108
109 if ! rep="$(jose fmt -j- -Og kty -q EC -EUUg crv -q "$crv" -EUUo- <<< "$rep")"; then
110 echo "Received invalid server reply!" >&2
111 exit 1
112 fi
113
114 tmp="$(jose jwk exc -i '{"alg":"ECMR"}' -l- -r- <<< "$eph$srv")"
115 rep="$(jose jwk pub -i- <<< "$rep")"
116 jwk="$(jose jwk exc -l- -r- <<< "$rep$tmp")"
117 (echo -n "$jwk$hdr."; /bin/cat) | jose jwe dec -k- -i-
118 exit $?