Files
lincodex/deploy/edge/scripts/awg-keys.sh
T

122 lines
4.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Generate and exchange AmneziaWG keys and obfuscation parameters.
#
# awg-keys.sh init PARAMS [--shared EXPORTED] create PARAMS on this host
# awg-keys.sh export PARAMS print the shareable part
# awg-keys.sh peer PARAMS EXPORTED store the other end's key
# awg-keys.sh public PARAMS print this host's public key
#
# PARAMS is a root-only KEY=VALUE file (mode 600), for example
# /etc/amnezia/amneziawg/awg-lincodex.params. It holds the private key and is
# read by render.sh. Run this script as root on the host that owns the key;
# the private key is never printed and never leaves PARAMS.
#
# Flow:
# edge: awg-keys.sh init PARAMS (keys + parameters)
# edge: awg-keys.sh export PARAMS > shared.env (public data only)
# app: awg-keys.sh init PARAMS --shared shared.env (own key, same parameters)
# app: awg-keys.sh export PARAMS > app-shared.env
# edge: awg-keys.sh peer PARAMS app-shared.env
# EXPORTED files carry a public key and obfuscation parameters. They are not
# public data: the parameters are shared secrets of the tunnel, so move them
# over SSH and delete the copies afterwards.
set -euo pipefail
umask 077
die() { printf 'awg-keys.sh: %s\n' "$*" >&2; exit 2; }
command -v awg >/dev/null || die "awg (amneziawg-tools) is not installed"
b64key='^[A-Za-z0-9+/]{43}=$'
shared_names="AWG_JC AWG_JMIN AWG_JMAX AWG_S1 AWG_S2 AWG_H1 AWG_H2 AWG_H3 AWG_H4"
# Print one value from a KEY=VALUE file.
get() { sed -n "s/^$2=//p" "$1" | head -n 1; }
# Uniform-enough integer in [lo, hi] from the kernel random source.
rand_range() {
local lo="$1" hi="$2" n
n="$(od -An -N4 -tu4 /dev/urandom | tr -d ' ')"
printf '%s' $((lo + n % (hi - lo + 1)))
}
cmd="${1:-}"; [ $# -ge 1 ] && shift
case "$cmd" in
init)
[ $# -ge 1 ] || die "usage: init PARAMS [--shared EXPORTED]"
params="$1"; shift
shared=""
if [ "${1:-}" = "--shared" ]; then [ $# -eq 2 ] || die "--shared needs a file"; shared="$2"; fi
[ ! -e "$params" ] || die "$params exists; refusing to overwrite keys"
[ "$(id -u)" -eq 0 ] || die "run as root"
priv="$(awg genkey)"
pub="$(printf '%s' "$priv" | awg pubkey)"
tmp="$(mktemp "$params.XXXXXX")"
trap 'rm -f "$tmp"' EXIT
{
printf 'AWG_PRIVATE_KEY=%s\n' "$priv"
printf 'AWG_PUBLIC_KEY=%s\n' "$pub"
if [ -n "$shared" ]; then
[ -f "$shared" ] || die "missing $shared"
peer="$(get "$shared" AWG_PEER_PUBLIC_KEY)"
printf '%s' "$peer" | grep -Eq "$b64key" || die "$shared has no valid AWG_PEER_PUBLIC_KEY"
printf 'AWG_PEER_PUBLIC_KEY=%s\n' "$peer"
for name in $shared_names; do
value="$(get "$shared" "$name")"
printf '%s' "$value" | grep -Eq '^[0-9]+$' || die "$shared has no valid $name"
printf '%s=%s\n' "$name" "$value"
done
else
jmin="$(rand_range 40 80)"
jmax="$(rand_range 700 1000)"
s1="$(rand_range 15 150)"
s2="$(rand_range 15 150)"
while [ $((s1 + 56)) -eq "$s2" ]; do s2="$(rand_range 15 150)"; done
printf 'AWG_JC=%s\nAWG_JMIN=%s\nAWG_JMAX=%s\nAWG_S1=%s\nAWG_S2=%s\n' "$(rand_range 4 8)" "$jmin" "$jmax" "$s1" "$s2"
seen=" "
for i in 1 2 3 4; do
h="$(rand_range 5 4294967295)"
while case "$seen" in *" $h "*) true ;; *) false ;; esac; do h="$(rand_range 5 4294967295)"; done
seen="$seen$h "
printf 'AWG_H%s=%s\n' "$i" "$h"
done
fi
} > "$tmp"
chmod 600 "$tmp"
mv "$tmp" "$params"
trap - EXIT
printf 'created %s; this host public key: %s\n' "$params" "$pub"
;;
export)
[ $# -eq 1 ] || die "usage: export PARAMS"
[ -f "$1" ] || die "missing $1"
printf 'AWG_PEER_PUBLIC_KEY=%s\n' "$(get "$1" AWG_PUBLIC_KEY)"
for name in $shared_names; do printf '%s=%s\n' "$name" "$(get "$1" "$name")"; done
;;
peer)
[ $# -eq 2 ] || die "usage: peer PARAMS EXPORTED"
params="$1"; exported="$2"
[ -f "$params" ] || die "missing $params"
[ -f "$exported" ] || die "missing $exported"
peer="$(get "$exported" AWG_PEER_PUBLIC_KEY)"
printf '%s' "$peer" | grep -Eq "$b64key" || die "$exported has no valid AWG_PEER_PUBLIC_KEY"
[ "$peer" != "$(get "$params" AWG_PUBLIC_KEY)" ] || die "that is this host's own public key"
for name in $shared_names; do
[ "$(get "$exported" "$name")" = "$(get "$params" "$name")" ] || die "$name differs between the hosts"
done
tmp="$(mktemp "$params.XXXXXX")"
trap 'rm -f "$tmp"' EXIT
grep -v '^AWG_PEER_PUBLIC_KEY=' "$params" > "$tmp" || true
printf 'AWG_PEER_PUBLIC_KEY=%s\n' "$peer" >> "$tmp"
chmod 600 "$tmp"
mv "$tmp" "$params"
trap - EXIT
printf 'stored peer public key in %s\n' "$params"
;;
public)
[ $# -eq 1 ] || die "usage: public PARAMS"
[ -f "$1" ] || die "missing $1"
get "$1" AWG_PUBLIC_KEY
;;
*) die "usage: awg-keys.sh init|export|peer|public ..." ;;
esac