| 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 | SUMMARY="Encrypts using a Tang binding server policy" |
| 22 | |
| 23 | if [ "$1" == "--summary" ]; then |
| 24 | echo "$SUMMARY" |
| 25 | exit 0 |
| 26 | fi |
| 27 | |
| 28 | if [ -t 0 ]; then |
| 29 | exec >&2 |
| 30 | echo |
| 31 | echo "Usage: clevis encrypt tang CONFIG [-y] < PLAINTEXT > JWE" |
| 32 | echo |
| 33 | echo "$SUMMARY" |
| 34 | echo |
| 35 | echo " -y Use this option for skipping the advertisement" |
| 36 | echo " trust check. This can be useful in automated" |
| 37 | echo " deployments" |
| 38 | echo |
| 39 | echo "This command uses the following configuration properties:" |
| 40 | echo |
| 41 | echo " url: <string> The base URL of the Tang server (REQUIRED)" |
| 42 | echo |
| 43 | echo " thp: <string> The thumbprint of a trusted signing key" |
| 44 | echo |
| 45 | echo " adv: <string> A filename containing a trusted advertisement" |
| 46 | echo " adv: <object> A trusted advertisement (raw JSON)" |
| 47 | echo |
| 48 | echo "Obtaining the thumbprint of a trusted signing key is easy. If you" |
| 49 | echo "have access to the Tang server's database directory, simply do:" |
| 50 | echo |
| 51 | echo " $ jose jwk thp -i \$DBDIR/\$SIG.jwk " |
| 52 | echo |
| 53 | echo "Alternatively, if you have certainty that your network connection" |
| 54 | echo "is not compromised (not likely), you can download the advertisement" |
| 55 | echo "yourself using:" |
| 56 | echo |
| 57 | echo " $ curl -f \$URL/adv > adv.jws" |
| 58 | echo |
| 59 | exit 2 |
| 60 | fi |
| 61 | |
| 62 | if ! cfg="$(jose fmt -j- -Oo- <<< "$1" 2>/dev/null)"; then |
| 63 | echo "Configuration is malformed!" >&2 |
| 64 | exit 1 |
| 65 | fi |
| 66 | |
| 67 | CLEVIS_DEFAULT_THP_ALG=S256 # SHA-256. |
| 68 | CLEVIS_ALTERNATIVE_THP_ALGS=S1 # SHA-1. |
| 69 | |
| 70 | trust= |
| 71 | [ -n "${2}" ] && [ "${2}" == "-y" ] && trust=yes |
| 72 | |
| 73 | if ! url="$(jose fmt -j- -Og url -u- <<< "$cfg")"; then |
| 74 | echo "Missing the required 'url' property!" >&2 |
| 75 | exit 1 |
| 76 | fi |
| 77 | |
| 78 | thp="$(jose fmt -j- -Og thp -Su- <<< "$cfg")" || true |
| 79 | |
| 80 | ### Get the advertisement |
| 81 | if jws="$(jose fmt -j- -g adv -Oo- <<< "$cfg")"; then |
| 82 | thp="${thp:-any}" |
| 83 | elif jws="$(jose fmt -j- -g adv -Su- <<< "$cfg")"; then |
| 84 | if ! [ -f "$jws" ]; then |
| 85 | echo "Advertisement file '$jws' not found!" >&2 |
| 86 | exit 1 |
| 87 | fi |
| 88 | |
| 89 | if ! jws="$(jose fmt --json="${jws}" -Oo- 2>/dev/null)"; then |
| 90 | echo "Advertisement file '$jws' is malformed!" >&2 |
| 91 | exit 1 |
| 92 | fi |
| 93 | |
| 94 | thp="${thp:-any}" |
| 95 | elif ! jws="$(curl -sfg "$url/adv/$thp")"; then |
| 96 | echo "Unable to fetch advertisement: '$url/adv/$thp'!" >&2 |
| 97 | exit 1 |
| 98 | fi |
| 99 | |
| 100 | if ! jwks="$(jose fmt --json="${jws}" -Og payload -SyOg keys \ |
| 101 | -AUo- 2>/dev/null)"; then |
| 102 | echo "Advertisement is malformed!" >&2 |
| 103 | exit 1 |
| 104 | fi |
| 105 | |
| 106 | ### Check advertisement validity |
| 107 | ver="$(jose jwk use -i- -r -u verify -o- <<< "$jwks")" |
| 108 | if ! jose jws ver -i "$jws" -k- -a <<< "$ver"; then |
| 109 | echo "Advertisement is missing signatures!" >&2 |
| 110 | exit 1 |
| 111 | fi |
| 112 | |
| 113 | ### Check advertisement trust |
| 114 | if [ -z "${trust}" ]; then |
| 115 | if [ -z "$thp" ]; then |
| 116 | echo "The advertisement contains the following signing keys:" >&2 |
| 117 | echo >&2 |
| 118 | jose jwk thp -i- -a "${CLEVIS_DEFAULT_THP_ALG}" <<< "$ver" >&2 |
| 119 | echo >&2 |
| 120 | read -r -p "Do you wish to trust these keys? [ynYN] " ans < /dev/tty |
| 121 | [[ "$ans" =~ ^[yY]$ ]] || exit 1 |
| 122 | |
| 123 | elif [ "$thp" != "any" ] && \ |
| 124 | ! jose jwk thp -i- -f "${thp}" -a "${CLEVIS_DEFAULT_THP_ALG}" \ |
| 125 | -o /dev/null <<< "$ver"; then |
| 126 | # Thumbprint of trusted JWK did not match the signature. Let's check |
| 127 | # alternative thumbprints generated with clevis supported hash |
| 128 | # algorithms to be sure. |
| 129 | for alg in ${CLEVIS_ALTERNATIVE_THP_ALGS}; do |
| 130 | srv="$(jose jwk thp -i- -f "${thp}" -a "${alg}" <<< "${ver}")" \ |
| 131 | && break |
| 132 | done |
| 133 | if [ -z "${srv}" ]; then |
| 134 | echo "Trusted JWK '$thp' did not sign the advertisement!" >&2 |
| 135 | exit 1 |
| 136 | fi |
| 137 | fi |
| 138 | fi |
| 139 | |
| 140 | ### Perform encryption |
| 141 | if ! enc="$(jose jwk use -i- -r -u deriveKey -o- <<< "$jwks")"; then |
| 142 | echo "Key derivation key not available!" >&2 |
| 143 | exit 1 |
| 144 | fi |
| 145 | |
| 146 | jose fmt -j "$enc" -Og keys -A || enc="{\"keys\":[$enc]}" |
| 147 | |
| 148 | if ! jwk="$(jose fmt -j- -Og keys -Af- <<< "$enc")"; then |
| 149 | echo "No exchange keys found!" >&2 |
| 150 | exit 1 |
| 151 | fi |
| 152 | |
| 153 | jwk="$(jose fmt -j- -Od key_ops -o- <<< "$jwk")" |
| 154 | jwk="$(jose fmt -j- -Od alg -o- <<< "$jwk")" |
| 155 | kid="$(jose jwk thp -i- -a "${CLEVIS_DEFAULT_THP_ALG}" <<< "$jwk")" |
| 156 | jwe='{"protected":{"alg":"ECDH-ES","enc":"A256GCM","clevis":{"pin":"tang","tang":{}}}}' |
| 157 | jwe="$(jose fmt -j "$jwe" -g protected -q "$kid" -s kid -UUo-)" |
| 158 | jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tang -q "$url" -s url -UUUUo-)" |
| 159 | jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tang -j- -s adv -UUUUo- <<< "$jwks")" |
| 160 | exec jose jwe enc -i- -k- -I- -c < <(echo -n "$jwe$jwk"; /bin/cat) |