Debian Codegraph @

src/pins/pkcs11/clevis-decrypt-pkcs11

raw symbols SCIP
1 #!/bin/bash
2 set -eo pipefail
3 # vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
4 #
5 # Copyright (c) 2024 Red Hat, Inc.
6 # Author: Sergio Correia <[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 <http://www.gnu.org/licenses/>.
20 #
21 PIN_NAME=pkcs11
22 PIN_FILE="/run/systemd/clevis-pkcs11.pin"
23 . clevis-pkcs11-common
24
25 [ $# -eq 1 ] && [ "$1" == "--summary" ] && exit 2
26
27 if [ -t 0 ]; then
28 exec >&2
29 echo
30 echo "Usage: clevis decrypt pkcs11 < JWE > PLAINTEXT"
31 echo
32 exit 2
33 fi
34
35 on_exit() {
36 [ -d "${CLEVIS_PKCS11}" ] || exit 0
37 rm -rf "${CLEVIS_PKCS11}"
38 }
39
40 unset CLEVIS_PKCS11
41
42 read -r -d . hdr64
43
44 # Check for corruption in the header.
45 if ! hdr="$(jose fmt --quote="${hdr64}" --string --b64load --object \
46 --output=- 2>/dev/null)" ; then
47 echo "JWE header corrupt" >&2
48 exit 1
49 fi
50
51 # Check if the pin is the expected one.
52 if ! pin="$(jose fmt --json="${hdr}" --get clevis --get pin \
53 --unquote=- 2>/dev/null)" || [ -z "${pin}" ]; then
54 echo "Invalid JWE header: unable to identify 'pin'" >&2
55 exit 1
56 fi
57 if [ "${pin}" != "${PIN_NAME}" ]; then
58 echo "JWE pin mismatch: found: ${pin}; expected: ${PIN_NAME}" >&2
59 exit 1
60 fi
61
62 if ! uri="$(jose fmt -j- -Og clevis -g "${PIN_NAME}" -g uri -Su- <<< "${hdr}")"; then
63 echo "URI missing required 'clevis.pkcs11.uri' header parameter!" >&2
64 exit 1
65 fi
66
67 mechanism_option=""
68 mechanism="$(jose fmt -j- -Og clevis -g "${PIN_NAME}" -g mechanism -Su- <<< "${hdr}")" \
69 2>/dev/null || :
70 if [ -n "${mechanism}" ]; then
71 mechanism_option="--mechanism ${mechanism}"
72 fi
73
74 if ! clevis_valid_pkcs11_uri "${uri}"; then
75 echo "PKCS#11 URI with invalid format:[${uri}]" >&2
76 echo "PKCS#11 URI expected format:[${URI_EXPECTED_FORMAT}]" >&2
77 exit 1
78 fi
79
80 module_opt=""
81 if module_path="$(clevis_get_module_path_from_uri ${uri})"; then
82 module_opt="--module ${module_path}"
83 fi
84
85 if ! slot=$(clevis_get_pkcs11_final_slot_from_uri "${uri}"); then
86 slot_opt=""
87 else
88 slot_opt=" --slot-index=${slot}"
89 fi
90
91 # Check if key parameter is present.
92 if ! enc_jwk="$(jose fmt --json="${hdr}" --get clevis --get "${PIN_NAME}" \
93 --get key --unquote=- 2>/dev/null)" \
94 || [ -z "${enc_jwk}" ]; then
95 echo "JWE missing 'clevis.${PIN_NAME}.key' header parameter" >&2
96 exit 1
97 fi
98
99 if ! CLEVIS_PKCS11="$(mktemp -d)" || [ -z "${CLEVIS_PKCS11}" ]; then
100 echo "Creating a temporary dir for PKCS11 files failed" >&2
101 exit 1
102 fi
103 trap 'on_exit' EXIT
104
105 # Error file
106 ERR="${CLEVIS_PKCS11}/decerr"
107
108 # Decrypt the key.
109 ENC="${CLEVIS_PKCS11}/enc"
110 if ! printf '%s' "${enc_jwk}" | jose b64 dec -i- > "${ENC}" 2>"${ERR}"; then
111 cat "${ERR}" >&2
112 echo "Unable to base64-decode the JWK" >&2
113 exit 1
114 fi
115
116 PIN_value=""
117 if ! PIN_value="$(clevis_get_pin_value_from_uri ${uri})"; then
118 PIN_value=$(cat "${PIN_FILE}" 2>/dev/null || :)
119 fi
120
121 if ! jwk="$(pkcs11-tool --login --decrypt --input-file ${ENC} \
122 -p ${PIN_value} ${module_opt} ${mechanism_option} ${slot_opt} 2>${ERR})" \
123 || [ -z "${jwk}" ]; then
124 cat "${ERR}" >&2
125 echo "Unable to decrypt the JWK" >&2
126 # TODO: Verify invalid PIN more accurately
127 echo "Invalid PIN?" >&2
128 exit 1
129 fi
130
131 rm -rf "${PIN_FILE}" 2>/dev/null || :
132
133 # Decrypt the data using the decrypted JWK.
134 ( printf '%s' "${jwk}${hdr64}." ; cat ) | exec jose jwe dec --key=- --input=-