| 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 | |
| 23 | SUMMARY="Encrypts using a PKCS#11 token" |
| 24 | |
| 25 | if [ "$1" == "--summary" ]; then |
| 26 | echo "$SUMMARY" |
| 27 | exit 0 |
| 28 | fi |
| 29 | |
| 30 | if [ -t 0 ]; then |
| 31 | exec >&2 |
| 32 | echo |
| 33 | echo "Usage: clevis encrypt pkcs11 CONFIG < PLAINTEXT > JWE" |
| 34 | echo |
| 35 | echo "$SUMMARY" |
| 36 | echo |
| 37 | echo "This command uses the following configuration properties:" |
| 38 | echo |
| 39 | echo " uri: <string> The PKCS#11 URI (REQUIRED)" |
| 40 | echo |
| 41 | exit 2 |
| 42 | fi |
| 43 | |
| 44 | . clevis-pkcs11-common |
| 45 | |
| 46 | on_exit() { |
| 47 | [ -d "${CLEVIS_PKCS11}" ] || exit 0 |
| 48 | rm -rf "${CLEVIS_PKCS11}" |
| 49 | } |
| 50 | unset CLEVIS_PKCS11 |
| 51 | |
| 52 | # We first check whether the configuration is a valid JSON. |
| 53 | if ! cfg="$(jose fmt --json="$1" --object --output=-)" ; then |
| 54 | echo 'Configuration is malformed' >&2 |
| 55 | exit 1 |
| 56 | fi |
| 57 | |
| 58 | if ! uri="$(jose fmt -j- -Og uri -u- <<< "$cfg")"; then |
| 59 | uri="pkcs11:" |
| 60 | fi |
| 61 | |
| 62 | mechanism="$(jose fmt -j- -Og mechanism -u- <<< "$cfg" 2>/dev/null || :)" |
| 63 | |
| 64 | if ! clevis_valid_pkcs11_uri "${uri}"; then |
| 65 | echo "PKCS#11 URI with invalid format:[${uri}]" >&2 |
| 66 | echo "PKCS#11 URI expected format:[${URI_EXPECTED_FORMAT}]" >&2 |
| 67 | exit 1 |
| 68 | fi |
| 69 | |
| 70 | if ! module_path=$(clevis_get_module_path_from_uri "${uri}"); then |
| 71 | module_opt="" |
| 72 | else |
| 73 | module_opt=" --module ${module_path}" |
| 74 | fi |
| 75 | |
| 76 | if ! slot=$(clevis_get_pkcs11_final_slot_from_uri "${uri}"); then |
| 77 | slot_opt="" |
| 78 | else |
| 79 | slot_opt=" --slot-index=${slot}" |
| 80 | fi |
| 81 | |
| 82 | if ! CLEVIS_PKCS11="$(mktemp -d)" || [ -z "${CLEVIS_PKCS11}" ]; then |
| 83 | echo "Creating a temporary dir for PKCS11 files failed" >&2 |
| 84 | exit 1 |
| 85 | fi |
| 86 | trap 'on_exit' EXIT |
| 87 | |
| 88 | # Error file |
| 89 | ERR="${CLEVIS_PKCS11}/encerr" |
| 90 | |
| 91 | # Let's generate a key. |
| 92 | if ! jwk="$(jose jwk gen --input='{"alg":"A256GCM"}')" \ |
| 93 | || [ -z "${jwk}" ]; then |
| 94 | echo "Unable to generate JWK" >&2 |
| 95 | exit 1 |
| 96 | fi |
| 97 | |
| 98 | # Now let's encrypt it with the device public key. |
| 99 | PKEY="${CLEVIS_PKCS11}/pubkey" |
| 100 | if ! id=$(pkcs11-tool ${slot_opt} -O 2>${ERR} ${module_opt} \ |
| 101 | | grep -i 'Public' -A10 2>${ERR} | grep 'ID:' \ |
| 102 | | head -1 | awk -F 'ID:' '{ |
| 103 | val = $2 |
| 104 | if (match(val, /\(0x[^)]+\)/)) { |
| 105 | val = substr(val, RSTART+3, RLENGTH-4) |
| 106 | print val |
| 107 | } else { |
| 108 | gsub(/^[[:space:]]+|[[:space:]]+$/, "", val) |
| 109 | print val |
| 110 | } |
| 111 | }'); then |
| 112 | cat "${ERR}" >&2 |
| 113 | echo "Unable to obtain public key ID from PKCS#11 device" >&2 |
| 114 | exit 1 |
| 115 | fi |
| 116 | |
| 117 | if ! pkcs11-tool ${slot_opt} ${module_opt} --read-object --type pubkey --id "${id}" \ |
| 118 | 2> "${ERR}" > "${PKEY}"; then |
| 119 | cat "${ERR}" >&2 |
| 120 | echo "Unable to obtain a public key from PKCS#11 device" >&2 |
| 121 | exit 1 |
| 122 | fi |
| 123 | |
| 124 | if ! jwk_enc="$(printf '%s' "${jwk}" | openssl rsautl -encrypt -pubin -keyform DER \ |
| 125 | -inkey "${PKEY}" 2>${ERR} \ |
| 126 | | jose b64 enc -I-)"; then |
| 127 | cat "${ERR}" >&2 |
| 128 | echo "Unable to encrypt JWK with PKCS#11 public key" >&2 |
| 129 | exit 1 |
| 130 | fi |
| 131 | |
| 132 | # And the JWE. |
| 133 | template=$(printf '{"protected":{"clevis":{"pin":"%s","%s":{"uri":"%s", "mechanism":"%s"}}}}' \ |
| 134 | "${PIN_NAME}" "${PIN_NAME}" "${uri}" "${mechanism}") |
| 135 | |
| 136 | # Save key. |
| 137 | jwe="$(jose fmt --json="${template}" --get protected --get clevis \ |
| 138 | --get "${PIN_NAME}" --quote "${jwk_enc}" --set key -UUUU --output=-)" |
| 139 | |
| 140 | # Now we encrypt the data using our key. |
| 141 | ( printf '%s' "${jwe}${jwk}" ; cat ) | exec jose jwe enc \ |
| 142 | --input=- --key=- \ |
| 143 | --detached=- --compact |