| 1 | #!/bin/bashscip-shell filesystem . . `/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 [scip-shell system . . `[`. "$1" == "--summary" ]; then |
| 24 | echoscip-shell system . . echo. "$SUMMARY" |
| 25 | exit 0 |
| 26 | fi |
| 27 | |
| 28 | if [scip-shell system . . `[`. -t 0 ]; then |
| 29 | exec >&2 |
| 30 | echoscip-shell system . . echo. |
| 31 | echoscip-shell system . . echo. "Usage: clevis encrypt tang CONFIG [-y] < PLAINTEXT > JWE" |
| 32 | echoscip-shell system . . echo. |
| 33 | echoscip-shell system . . echo. "$SUMMARY" |
| 34 | echoscip-shell system . . echo. |
| 35 | echoscip-shell system . . echo. " -y Use this option for skipping the advertisement" |
| 36 | echoscip-shell system . . echo. " trust check. This can be useful in automated" |
| 37 | echoscip-shell system . . echo. " deployments" |
| 38 | echoscip-shell system . . echo. |
| 39 | echoscip-shell system . . echo. "This command uses the following configuration properties:" |
| 40 | echoscip-shell system . . echo. |
| 41 | echoscip-shell system . . echo. " url: <string> The base URL of the Tang server (REQUIRED)" |
| 42 | echoscip-shell system . . echo. |
| 43 | echoscip-shell system . . echo. " thp: <string> The thumbprint of a trusted signing key" |
| 44 | echoscip-shell system . . echo. |
| 45 | echoscip-shell system . . echo. " adv: <string> A filename containing a trusted advertisement" |
| 46 | echoscip-shell system . . echo. " adv: <object> A trusted advertisement (raw JSON)" |
| 47 | echoscip-shell system . . echo. |
| 48 | echoscip-shell system . . echo. "Obtaining the thumbprint of a trusted signing key is easy. If you" |
| 49 | echoscip-shell system . . echo. "have access to the Tang server's database directory, simply do:" |
| 50 | echoscip-shell system . . echo. |
| 51 | echoscip-shell system . . echo. " $ jose jwk thp -i \$DBDIR/\$SIG.jwk " |
| 52 | echoscip-shell system . . echo. |
| 53 | echoscip-shell system . . echo. "Alternatively, if you have certainty that your network connection" |
| 54 | echoscip-shell system . . echo. "is not compromised (not likely), you can download the advertisement" |
| 55 | echoscip-shell system . . echo. "yourself using:" |
| 56 | echoscip-shell system . . echo. |
| 57 | echoscip-shell system . . echo. " $ curl -f \$URL/adv > adv.jws" |
| 58 | echoscip-shell system . . echo. |
| 59 | exit 2 |
| 60 | fi |
| 61 | |
| 62 | if ! cfg="$(jose fmt -j- -Oo- <<< "$1" 2>/dev/nullscip-shell filesystem . . `/dev/null`.)"; then |
| 63 | echoscip-shell system . . 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 | [scip-shell system . . `[`. -n "${2}" ] && [scip-shell system . . `[`. "${2}" == "-y" ] && trust=yes |
| 72 | |
| 73 | if ! url="$(jose fmt -j- -Og url -u- <<< "$cfg")"; then |
| 74 | echoscip-shell system . . echo. "Missing the required 'url' property!" >&2 |
| 75 | exit 1 |
| 76 | fi |
| 77 | |
| 78 | thp="$(jose fmt -j- -Og thp -Su- <<< "$cfg")" || truescip-shell system . . 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 ! [scip-shell system . . `[`. -f "$jws" ]; then |
| 85 | echoscip-shell system . . echo. "Advertisement file '$jws' not found!" >&2 |
| 86 | exit 1 |
| 87 | fi |
| 88 | |
| 89 | if ! jws="$(jose fmt --json="${jws}" -Oo- 2>/dev/nullscip-shell filesystem . . `/dev/null`.)"; then |
| 90 | echoscip-shell system . . echo. "Advertisement file '$jws' is malformed!" >&2 |
| 91 | exit 1 |
| 92 | fi |
| 93 | |
| 94 | thp="${thp:-any}" |
| 95 | elif ! jws="$(curlscip-shell system . . curl. -sfg "$url/adv/$thp")"; then |
| 96 | echoscip-shell system . . 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 | echoscip-shell system . . 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 | echoscip-shell system . . echo. "Advertisement is missing signatures!" >&2 |
| 110 | exit 1 |
| 111 | fi |
| 112 | |
| 113 | ### Check advertisement trust |
| 114 | if [scip-shell system . . `[`. -z "${trust}" ]; then |
| 115 | if [scip-shell system . . `[`. -z "$thp" ]; then |
| 116 | echoscip-shell system . . echo. "The advertisement contains the following signing keys:" >&2 |
| 117 | echoscip-shell system . . echo. >&2 |
| 118 | jose jwk thp -i- -a "${CLEVIS_DEFAULT_THP_ALG}" <<< "$ver" >&2 |
| 119 | echoscip-shell system . . echo. >&2 |
| 120 | read -r -p "Do you wish to trust these keys? [ynYN] " ans < /dev/ttyscip-shell filesystem . . `/dev/tty`. |
| 121 | [[ "$ans" =~ ^[yY]$ ]] || exit 1 |
| 122 | |
| 123 | elif [scip-shell system . . `[`. "$thp" != "any" ] && \ |
| 124 | ! jose jwk thp -i- -f "${thp}" -a "${CLEVIS_DEFAULT_THP_ALG}" \ |
| 125 | -o /dev/nullscip-shell filesystem . . `/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 [scip-shell system . . `[`. -z "${srv}" ]; then |
| 134 | echoscip-shell system . . 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 | echoscip-shell system . . 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 | echoscip-shell system . . 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 < <(echoscip-shell system . . echo. -n "$jwe$jwk"; /bin/catscip-shell filesystem . . `/bin/cat`.) |