diff --git a/rsync-ssl b/rsync-ssl index 0fd177093..822d9ff84 100755 --- a/rsync-ssl +++ b/rsync-ssl @@ -89,19 +89,18 @@ function rsync_ssl_helper { fi if [[ -z "$RSYNC_SSL_KEY" ]]; then - keyopt="" gnutls_key_opt="" socat_key_opt="" else - keyopt="-key$optsep$RSYNC_SSL_KEY" gnutls_key_opt="--x509keyfile=$RSYNC_SSL_KEY" socat_key_opt=",key=$RSYNC_SSL_KEY" fi + local -a openssl_ca_opts if [[ -z ${RSYNC_SSL_CA_CERT+x} ]]; then # RSYNC_SSL_CA_CERT unset - default CA set AND verify: # openssl: - caopt="-verify_return_error -verify 4" + openssl_ca_opts=(-verify_return_error -verify 4) # gnutls: # gnutls-cli has no reliable "use the system trust store AND make # verification fatal" switch across versions, so the gnutls path @@ -118,7 +117,7 @@ function rsync_ssl_helper { elif [[ "$RSYNC_SSL_CA_CERT" == "" ]]; then # RSYNC_SSL_CA_CERT set but empty -do NO verifications: # openssl: - caopt="-verify 1" + openssl_ca_opts=(-verify 1) # gnutls: gnutls_opts="--insecure" # socat: @@ -129,7 +128,7 @@ function rsync_ssl_helper { else # RSYNC_SSL_CA_CERT set - use CA AND verify: # openssl: - caopt="-CAfile $RSYNC_SSL_CA_CERT -verify_return_error -verify 4" + openssl_ca_opts=(-CAfile "$RSYNC_SSL_CA_CERT" -verify_return_error -verify 4) # gnutls: gnutls_opts="--x509cafile=$RSYNC_SSL_CA_CERT" # socat: @@ -164,17 +163,9 @@ function rsync_ssl_helper { exit 1 fi - # Bind the server certificate to the requested host (openssl -verify_hostname, - # stunnel checkHost) so a cert that is CA-valid for a *different* name can't be - # used to MITM. Set RSYNC_SSL_SKIP_HOSTNAME_CHECK=1 to keep CA chain - # verification but skip the identity check (e.g. bare-IP or internal-CA setups). - # checkHost only applies while the chain is being verified. - openssl_host_opt="-verify_hostname $hostname" stunnel_host_opt="" - [[ "$verify" == "verifyChain = yes" ]] && stunnel_host_opt="checkHost = $hostname" - if [[ "$RSYNC_SSL_SKIP_HOSTNAME_CHECK" == 1 ]]; then - openssl_host_opt="" - stunnel_host_opt="" + if [[ "$verify" == "verifyChain = yes" && "$RSYNC_SSL_SKIP_HOSTNAME_CHECK" != 1 ]]; then + stunnel_host_opt="checkHost = $hostname" fi if [[ $RSYNC_SSL_TYPE == gnutls && -z ${RSYNC_SSL_CA_CERT+x} && "$RSYNC_SSL_ALLOW_INSECURE_GNUTLS" != 1 ]]; then @@ -189,7 +180,7 @@ function rsync_ssl_helper { fi if [[ $RSYNC_SSL_TYPE == openssl ]]; then - exec "$RSYNC_SSL_OPENSSL" s_client $caopt $certopt $keyopt -quiet -verify_quiet -servername $hostname $openssl_host_opt -connect $hostname:$port + run_openssl "$hostname" "$port" "${openssl_ca_opts[@]}" elif [[ $RSYNC_SSL_TYPE == gnutls ]]; then exec "$RSYNC_SSL_GNUTLS" --logfile=/dev/null $gnutls_cert_opt $gnutls_key_opt $gnutls_opts $hostname:$port elif [[ $RSYNC_SSL_TYPE == socat ]]; then @@ -210,14 +201,62 @@ EOF fi } +function run_openssl { + local host="$1" port="$2" identity="$1" endpoint="$1:$2" + local verify_flag=-verify_hostname + shift 2 + local -a options=("$@") + + if [[ "$host" == *:* ]]; then + verify_flag=-verify_ip + identity="${host%%%*}" + endpoint="[$host]:$port" + elif [[ "$host" == *.* && "$host" =~ ^[0-9.]+$ ]]; then + validate_ipv4_literal "$host" + verify_flag=-verify_ip + else + options+=(-servername "$host") + fi + + if [[ "$RSYNC_SSL_SKIP_HOSTNAME_CHECK" != 1 ]]; then + options+=("$verify_flag" "$identity") + fi + [[ -n "$RSYNC_SSL_CERT" ]] && options+=(-cert "$RSYNC_SSL_CERT") + [[ -n "$RSYNC_SSL_KEY" ]] && options+=(-key "$RSYNC_SSL_KEY") + + exec "$RSYNC_SSL_OPENSSL" s_client "${options[@]}" -quiet -verify_quiet -connect "$endpoint" +} + +function validate_ipv4_literal { + local octet='(0|[1-9][0-9]{0,2})' + local address="^$octet\\.$octet\\.$octet\\.$octet$" + local value + + [[ "$1" =~ $address ]] || invalid_ssl_hostname "$1" + for value in "${BASH_REMATCH[@]:1}"; do + ((10#$value <= 255)) || invalid_ssl_hostname "$1" + done +} + function validate_ssl_hostname { local host="$1" + if [[ "$RSYNC_SSL_TYPE" == openssl && "$host" == *%* ]]; then + local scope="${host#*%}" + host="${host%%%*}" + if [[ "$host" != *:* || ! "$scope" =~ ^[A-Za-z0-9._-]+$ ]]; then + invalid_ssl_hostname "$1" + fi + fi if [[ -z "$host" || "$host" == -* || "$host" =~ [^A-Za-z0-9._:-] ]]; then - echo "invalid rsync-ssl hostname: $host" 1>&2 - exit 1 + invalid_ssl_hostname "$1" fi } +function invalid_ssl_hostname { + printf 'invalid rsync-ssl hostname: %s\n' "$1" >&2 + exit 1 +} + function path_search { IFS_SAVE="$IFS" IFS=: diff --git a/rsync-ssl.1.md b/rsync-ssl.1.md index 6b37eba98..0c46b7ada 100644 --- a/rsync-ssl.1.md +++ b/rsync-ssl.1.md @@ -87,10 +87,9 @@ The ssl helper scripts are affected by the following environment variables: Set to `1` to verify the certificate **chain** but skip the **hostname** (identity) check, for the openssl and stunnel backends. Use this only when the server certificate legitimately cannot match the host you connect to -- - for example connecting by bare IP address, or to a server whose internal-CA - certificate carries a different name. The CA chain is still validated, so - this is much narrower than disabling validation with an empty - `RSYNC_SSL_CA_CERT`. + for example when the server's internal-CA certificate carries a different + name. The CA chain is still checked. This is narrower than disabling + validation with an empty `RSYNC_SSL_CA_CERT`. 0. `RSYNC_SSL_OPENSSL` @@ -144,6 +143,11 @@ The `openssl` type uses `openssl s_client`, which is retained for compatibility. If your OpenSSL version's `s_client` has trouble handling rsync traffic, try `--type=socat` or `--type=stunnel`. +For verified OpenSSL connections, an IP literal must match an IP subject +alternative name in the server certificate. A scoped IPv6 address retains its +scope for the connection but only the address is checked against the certificate. +IP connections do not request DNS-based server name indication (SNI). + This script also supports a `--type=gnutls` option, but at the time of this release the gnutls-cli command was dropping output, making it unusable. If that bug has been fixed in your version, feel free to put gnutls into an diff --git a/testsuite/tests/transfer/rsync-ssl-openssl-ip.py b/testsuite/tests/transfer/rsync-ssl-openssl-ip.py new file mode 100644 index 000000000..ad743fa75 --- /dev/null +++ b/testsuite/tests/transfer/rsync-ssl-openssl-ip.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + +import os +import subprocess + +from harness import TestContext, requires, run +from harness.results import test_fail + + +@requires(transports={'pipe', 'tcp'}, mutates={'filesystem', 'process'}, + tags={'compatibility', 'tls'}) +def test(context: TestContext): + wrapper = context.repository / 'rsync-ssl' + openssl = context.scratch / 'openssl probe' + openssl.write_text('#!/usr/bin/env bash\nprintf "%s\\n" "$@"\n') + openssl.chmod(0o755) + env = {key: value for key, value in os.environ.items() + if not key.startswith('RSYNC_SSL_') and key != 'RSYNC_PORT'} + ca = str(context.scratch / 'ca.pem') + env.update(RSYNC_SSL_TYPE='openssl', RSYNC_SSL_OPENSSL=str(openssl), + RSYNC_SSL_CA_CERT=ca, RSYNC_PORT='8874') + + def invoke(host, **updates): + return subprocess.run( + ['bash', str(wrapper), '--HELPER', host, 'rsync', '--server', '--daemon', '.'], + env={**env, **updates}, capture_output=True, text=True, timeout=10, + ) + + def check(host, endpoint, identity, *, address=False, **updates): + result = invoke(host, **updates) + if result.returncode: + test_fail(f'{host}: helper failed: {result.stderr}') + args = result.stdout.splitlines() + wanted = {'-connect': endpoint, '-CAfile': updates.get('RSYNC_SSL_CA_CERT', ca), + '-verify': '4'} + if address: + wanted['-verify_ip'] = identity + if '-servername' in args or '-verify_hostname' in args: + test_fail(f'{host}: treated a valid IP literal as a DNS name: {args}') + else: + wanted.update({'-verify_hostname': identity, '-servername': identity}) + if '-verify_ip' in args: + test_fail(f'{host}: treated a DNS name as an IP literal: {args}') + if '-verify_return_error' not in args: + test_fail(f'{host}: certificate errors are not fatal: {args}') + for flag, value in wanted.items(): + if args.count(flag) != 1 or args[args.index(flag) + 1:][:1] != [value]: + test_fail(f'{host}: expected {flag} {value!r}: {args}') + + check('face', 'face:8874', 'face') + check('127.0.0.1', '127.0.0.1:8874', '127.0.0.1', address=True) + check('::1', '[::1]:8874', '::1', address=True) + check('fe80::1%eth0', '[fe80::1%eth0]:8874', 'fe80::1', address=True) + check('::ffff:192.0.2.1', '[::ffff:192.0.2.1]:8874', '::ffff:192.0.2.1', address=True) + check('localhost', 'localhost:8874', 'localhost', + RSYNC_SSL_CA_CERT=str(context.scratch / 'local CA.pem')) + + for host in ('127.0.0.256', '127.0.0', '127.00.0.1', 'fe80::1%'): + result = invoke(host) + if result.returncode != 1 or result.stdout or 'invalid rsync-ssl hostname' not in result.stderr: + test_fail(f'{host}: malformed address was not rejected by the wrapper') + + result = invoke('127.0.0.1', RSYNC_SSL_SKIP_HOSTNAME_CHECK='1') + args = result.stdout.splitlines() + if (result.returncode or '-verify_return_error' not in args or '-CAfile' not in args + or {'-verify_ip', '-verify_hostname', '-servername'}.intersection(args)): + test_fail(f'Identity-check opt-out changed CA validation or IP handling: {args}') + + +if __name__ == '__main__': + run(test) diff --git a/testsuite/tests/transfer/rsync-ssl.py b/testsuite/tests/transfer/rsync-ssl.py index b137e4cfd..5b1ed82e4 100644 --- a/testsuite/tests/transfer/rsync-ssl.py +++ b/testsuite/tests/transfer/rsync-ssl.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 import os -import re import shlex import subprocess import sys @@ -47,20 +46,12 @@ def invoke(script, args, env): } openssl_env.pop('RSYNC_SSL_SKIP_HOSTNAME_CHECK', None) -unfixed = executable( - openssl_dir / 'unfixed', - re.sub(r'(?m)^(\s*)validate_ssl_hostname .*$', r'\1:', - (SRCDIR / 'rsync-ssl').read_text()), -) -evil_host = 'localhost INJECTED_OPENSSL_ARG' -evil_helper = ['--HELPER', evil_host, 'rsync', '--server', '--daemon', '.'] -invoke(unfixed, evil_helper, openssl_env) -captured = openssl_args.read_text().splitlines() if openssl_args.exists() else [] -if 'INJECTED_OPENSSL_ARG' not in captured: - test_fail(f'unsafe control did not inject an OpenSSL argument: {captured}') -result = invoke(SRCDIR / 'rsync-ssl', evil_helper, openssl_env) -if result.returncode == 0 or 'invalid rsync-ssl hostname' not in result.stdout: - test_fail(f'crafted hostname was not rejected:\n{result.stdout}') +invalid_helper = ['--HELPER', 'invalid host.example', *helper[2:]] +result = invoke(SRCDIR / 'rsync-ssl', invalid_helper, openssl_env) +if result.returncode != 1 or 'invalid rsync-ssl hostname' not in result.stdout: + test_fail(f'invalid hostname was not rejected:\n{result.stdout}') +if openssl_args.exists(): + test_fail('OpenSSL was started for an invalid hostname') def openssl_run(env): unlink_if_exists(openssl_args)