-
Notifications
You must be signed in to change notification settings - Fork 124
Add optional mTLS support to the tang pin #566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,16 @@ if [ -t 0 ]; then | |
| echo " adv: <string> A filename containing a trusted advertisement" | ||
| echo " adv: <object> A trusted advertisement (raw JSON)" | ||
| echo | ||
| echo " cacert: <string> Path to a CA bundle used to verify the Tang" | ||
| echo " server's TLS certificate (curl --cacert)" | ||
| echo | ||
| echo " cert: <string> Path to the client TLS certificate for mutual" | ||
| echo " TLS authentication (curl --cert)" | ||
| echo | ||
| echo " key: <string> Path to the private key for the client TLS" | ||
| echo " certificate (curl --key). May be omitted if the" | ||
| echo " key is bundled in the 'cert' file" | ||
| echo | ||
| echo "Obtaining the thumbprint of a trusted signing key is easy. If you" | ||
| echo "have access to the Tang server's database directory, simply do:" | ||
| echo | ||
|
|
@@ -77,6 +87,37 @@ fi | |
|
|
||
| thp="$(jose fmt -j- -Og thp -Su- <<< "$cfg")" || true | ||
|
|
||
| # Optional (m)TLS parameters. If none are set, curl behaves exactly as before | ||
| # (plain HTTP/TLS, no client certificate and default CA verification). | ||
| cacert="$(jose fmt -j- -Og cacert -Su- <<< "$cfg")" || true | ||
| cert="$(jose fmt -j- -Og cert -Su- <<< "$cfg")" || true | ||
| key="$(jose fmt -j- -Og key -Su- <<< "$cfg")" || true | ||
|
liyue32 marked this conversation as resolved.
|
||
|
|
||
| # Validate the (m)TLS file paths up front, matching the 'adv' check below. | ||
| # Otherwise a typo'd path either surfaces as a misleading "Unable to fetch | ||
| # advertisement" error, or (when 'adv' is inline and curl is never called) | ||
| # gets baked into the JWE header and only fails later at decrypt time. | ||
| [ -n "$cacert" ] && [ ! -f "$cacert" ] && { echo "CA cert file '$cacert' not found!" >&2; exit 1; } | ||
| [ -n "$cert" ] && [ ! -f "$cert" ] && { echo "Client cert file '$cert' not found!" >&2; exit 1; } | ||
| [ -n "$key" ] && [ ! -f "$key" ] && { echo "Client key file '$key' not found!" >&2; exit 1; } | ||
|
|
||
| # 'key' on its own is a no-op: curl ignores --key unless --cert is also given. | ||
| [ -n "$key" ] && [ -z "$cert" ] && { echo "'key' requires 'cert' to also be set!" >&2; exit 1; } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When Would it make sense to reject this at bind time, similar to the if [ -n "$cacert" ] || [ -n "$cert" ]; then
case "$url" in
https://*) ;;
*) echo "TLS options (cacert/cert/key) require an https:// URL!" >&2
exit 1 ;;
esac
fi |
||
|
|
||
| # Reject http:// when cacert or cert is set: curl would silently ignore them. | ||
| if [ -n "$cacert" ] || [ -n "$cert" ]; then | ||
| case "$url" in | ||
| https://*) ;; | ||
| *) echo "TLS options (cacert/cert/key) require an https:// URL!" >&2 | ||
| exit 1 ;; | ||
| esac | ||
| fi | ||
|
|
||
| curl_opts=() | ||
| [ -n "$cacert" ] && curl_opts+=(--cacert "$cacert") | ||
| [ -n "$cert" ] && curl_opts+=(--cert "$cert") | ||
| [ -n "$key" ] && curl_opts+=(--key "$key") | ||
|
liyue32 marked this conversation as resolved.
|
||
|
|
||
| ### Get the advertisement | ||
| if jws="$(jose fmt -j- -g adv -Oo- <<< "$cfg")"; then | ||
| thp="${thp:-any}" | ||
|
|
@@ -92,7 +133,7 @@ elif jws="$(jose fmt -j- -g adv -Su- <<< "$cfg")"; then | |
| fi | ||
|
|
||
| thp="${thp:-any}" | ||
| elif ! jws="$(curl -sfg "$url/adv/$thp")"; then | ||
| elif ! jws="$(curl -sfg "${curl_opts[@]}" "$url/adv/$thp")"; then | ||
| echo "Unable to fetch advertisement: '$url/adv/$thp'!" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
@@ -156,5 +197,10 @@ kid="$(jose jwk thp -i- -a "${CLEVIS_DEFAULT_THP_ALG}" <<< "$jwk")" | |
| jwe='{"protected":{"alg":"ECDH-ES","enc":"A256GCM","clevis":{"pin":"tang","tang":{}}}}' | ||
| jwe="$(jose fmt -j "$jwe" -g protected -q "$kid" -s kid -UUo-)" | ||
| jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tang -q "$url" -s url -UUUUo-)" | ||
| # Persist any (m)TLS parameters so decryption can reuse them. These are file | ||
| # paths (not secrets); the JWE header is stored in plaintext. | ||
| [ -n "$cacert" ] && jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tang -q "$cacert" -s cacert -UUUUo-)" | ||
| [ -n "$cert" ] && jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tang -q "$cert" -s cert -UUUUo-)" | ||
| [ -n "$key" ] && jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tang -q "$key" -s key -UUUUo-)" | ||
| jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tang -j- -s adv -UUUUo- <<< "$jwks")" | ||
| exec jose jwe enc -i- -k- -I- -c < <(echo -n "$jwe$jwk"; /bin/cat) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| #!/bin/bash -xe | ||
| # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: | ||
| # | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| # | ||
|
|
||
| . tang-common-test-functions | ||
|
|
||
| on_exit() { | ||
| exit_status=$? | ||
| [ -n "${TMP}" ] && tang_stop "${TMP}" | ||
| [ -d "${TMP}" ] && rm -rf "$TMP" | ||
| exit "${exit_status}" | ||
| } | ||
|
liyue32 marked this conversation as resolved.
|
||
|
|
||
| trap 'on_exit' EXIT | ||
|
|
||
| # Bail out early (as a skip) if the environment cannot do mTLS. | ||
| tang_mtls_sanity_check | ||
|
|
||
| TMP="$(mktemp -d)" | ||
|
|
||
| # Generate CA, server and client certificates. | ||
| tang_generate_certs "${TMP}" | ||
|
|
||
| # Start a TLS tang server that *requires* a client certificate (mutual TLS). | ||
| tang_run_mtls "${TMP}" sig exc | ||
| port=$(tang_get_port "${TMP}") | ||
|
|
||
| url="https://localhost:${port}" | ||
| cacert="${TMP}/ca.crt" | ||
| cert="${TMP}/client.crt" | ||
| key="${TMP}/client.key" | ||
|
|
||
| # 1. Positive case: with a valid client certificate, encryption (which fetches | ||
| # the advertisement via GET /adv) and decryption (POST /rec) must both work | ||
| # over mutual TLS. '-y' skips the interactive trust check. | ||
| cfg="$(printf '{"url":"%s","cacert":"%s","cert":"%s","key":"%s"}' \ | ||
| "$url" "$cacert" "$cert" "$key")" | ||
| enc="$(echo -n "hi" | clevis encrypt tang "$cfg" -y)" | ||
| dec="$(echo -n "$enc" | clevis decrypt)" | ||
| test "$dec" == "hi" | ||
|
|
||
| # 2. The (m)TLS parameters must have been persisted into the JWE protected | ||
| # header so that decryption can reuse them without any extra configuration. | ||
| hdr="$(echo -n "${enc}" | cut -d. -f1)" | ||
| jhd="$(jose b64 dec -i- <<< "${hdr}")" | ||
| test "$(jose fmt -j- -Og clevis -g tang -g cacert -Su- <<< "$jhd")" == "${cacert}" | ||
| test "$(jose fmt -j- -Og clevis -g tang -g cert -Su- <<< "$jhd")" == "${cert}" | ||
| test "$(jose fmt -j- -Og clevis -g tang -g key -Su- <<< "$jhd")" == "${key}" | ||
|
|
||
| # 3. Negative case: mutual TLS must actually be enforced. Without a client | ||
| # certificate the TLS handshake fails, so fetching the advertisement (and | ||
| # therefore encryption) must fail. | ||
| cfg_noclient="$(printf '{"url":"%s","cacert":"%s"}' "$url" "$cacert")" | ||
| ! echo -n "hi" | clevis encrypt tang "${cfg_noclient}" -y | ||
|
|
||
| # 4. Negative case: 'key' without 'cert' is a no-op for curl, so encryption | ||
| # must be rejected up front rather than silently dropping the client key. | ||
| cfg_keynocert="$(printf '{"url":"%s","cacert":"%s","key":"%s"}' \ | ||
| "$url" "$cacert" "$key")" | ||
| ! echo -n "hi" | clevis encrypt tang "${cfg_keynocert}" -y | ||
|
|
||
| # 5. Negative case: a referenced (m)TLS file that does not exist must be | ||
| # reported at encrypt time instead of being baked into the JWE. | ||
| cfg_badcert="$(printf '{"url":"%s","cacert":"%s","cert":"%s","key":"%s"}' \ | ||
| "$url" "$cacert" "${TMP}/does-not-exist.crt" "$key")" | ||
| ! echo -n "hi" | clevis encrypt tang "${cfg_badcert}" -y | ||
|
|
||
| # 6. Negative case: over http:// curl silently ignores the (m)TLS options, so | ||
| # encryption must be rejected up front rather than baking them into the JWE | ||
| # while sending traffic in the clear. Uses valid files so the scheme check | ||
| # (not the file-existence check) is what rejects it. | ||
| cfg_httpurl="$(printf '{"url":"http://localhost:%s","cacert":"%s","cert":"%s","key":"%s"}' \ | ||
| "$port" "$cacert" "$cert" "$key")" | ||
| ! echo -n "hi" | clevis encrypt tang "${cfg_httpurl}" -y | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
During decryption,
cacert,cert,key(andurl) are all extracted from the JWE protected header and fed to curl (line 115) before the AEAD integrity check at line 128 (jose jwe dec). Theurlfield was already consumed unauthenticated before this patch, so this is a pre-existing design property of clevis-tang. However,cacertqualitatively extends the attack surface: an attacker who can modify the stored JWE can now also override which CA curl trusts, enabling MITM against the legitimate tang server with a self-signed cert planted on disk. Previously, modifyingurlalone still required a certificate trusted by the system CA store.The practical exploitability is limited (requires disk write access + network position, and the ECMR exchange value alone does not reveal the decryption key), so this may be acceptable as a known trade-off.
Worth considering: validate that cert/key/cacert paths are under an expected prefix (e.g.,
/etc/clevis/), or use indirection (store a profile name, resolve to paths at runtime from a fixed directory). At minimum, document the trust model explicitly so users understand the implication.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for your review! In my understanding, if attacker could modify JWE header, they could probably also modify the ca trust store in /etc/pki. If this is correct, cacert does not add more security risk here (correct me if my understanding is wrong). I also thought about adding validation for cert/key/cacert paths, but considering this aims to be used generically, it is hard to add any specific cacert path validation so I feel path-prefix validation felt too restrictive here.
I added one section named "mTLS SECURITY MODEL" in clevis-encrypt-tang.1.adoc to describe the potential risk of
cacertand recommende that the CA be installed in the system trust store (with cacert omitted) where possible