fix: update ssh-pubkey for modern 3072+ bit and ed25519 keys

This commit is contained in:
AJ ONeal
2024-05-15 21:36:55 +00:00
parent e872442f3b
commit 6c5b040b8c

76
ssh-pubkey/ssh-pubkey Normal file → Executable file
View File

@@ -2,6 +2,38 @@
set -e
set -u
fn_warn_rsa() { (
echo 'WARNING'
echo ' ~/.ssh/id_rsa is less than the required 3072 bits'
echo ' (some modern services will reject your key)'
echo ''
echo 'SOLUTION'
echo ' Generate a new key, and update accordingly'
my_ts="$(date -u "+%F_%H.%M.%S")"
echo ''
echo ' # OPTION 1: Generate a more efficient, ED25519 256-bit key'
echo ' # and update your ~/.ssh/config accordingly'
echo " mv ~/.ssh/id_rsa ~/.ssh/id_rsa.${my_ts}.bak"
# shellcheck disable=SC2016
echo ' ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -q -N ""'
echo " echo 'Host *
IdentityFile ~/.ssh/id_ed25519
IdentityFile ~/.ssh/id_rsa.${my_ts}.bak' >> ~/.ssh/config"
echo ''
echo ' # OPTION 2: Generate a larger, 4096-bit RSA key"'
echo ' # and update your ~/.ssh/config accordingly'
echo " mv ~/.ssh/id_rsa ~/.ssh/id_rsa.${my_ts}.bak"
# shellcheck disable=SC2016
echo ' ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -q -N ""'
echo " echo 'Host *
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/id_rsa.${my_ts}.bak' >> ~/.ssh/config"
); }
main() {
if ! test -d ~/.ssh; then
@@ -20,25 +52,49 @@ main() {
chmod 0600 ~/.ssh/authorized_keys
fi
if ! test -f ~/.ssh/id_rsa; then
ssh-keygen -b 2048 -t rsa -f ~/.ssh/id_rsa -q -N ""
my_keytype=''
if test -f ~/.ssh/id_ed25519; then
my_keytype='ed25519'
if ! test -f ~/.ssh/id_ed25519.pub; then
echo >&2 ""
ssh-keygen -y -f ~/.ssh/id_ed25519 > ~/.ssh/id_ed25519.pub
fi
elif test -f ~/.ssh/id_rsa; then
my_keytype='rsa'
if ! test -f ~/.ssh/id_rsa.pub; then
echo >&2 ""
ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub
fi
elif test -f ~/.ssh/id_ecda; then
my_keytype='ecdsa'
if ! test -f ~/.ssh/id_ecdsa.pub; then
echo >&2 ""
ssh-keygen -y -f ~/.ssh/id_ecda > ~/.ssh/id_ecda.pub
fi
else
my_keytype='rsa'
echo >&2 ""
fi
if ! test -f ~/.ssh/id_rsa.pub; then
ssh-keygen -b 4096 -t rsa -f ~/.ssh/id_rsa -q -N ""
ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub
echo >&2 ""
fi
# TODO use the comment (if any) for the name of the file
echo >&2 ""
#shellcheck disable=SC2088
echo >&2 "~/Downloads/id_rsa.$(whoami).pub":
echo >&2 "~/Downloads/id_${my_keytype}.$(whoami).pub":
echo >&2 ""
rm -f "$HOME/Downloads/id_rsa.$(whoami).pub"
cp -RPp "$HOME/.ssh/id_rsa.pub" "$HOME/Downloads/id_rsa.$(whoami).pub"
cat "$HOME/Downloads/id_rsa.$(whoami).pub"
rm -f "$HOME/Downloads/id_${my_keytype}.$(whoami).pub"
cp -RPp "$HOME/.ssh/id_${my_keytype}.pub" "$HOME/Downloads/id_${my_keytype}.$(whoami).pub"
cat "$HOME/Downloads/id_${my_keytype}.$(whoami).pub"
echo >&2 ""
if test -f ~/.ssh/id_rsa; then
my_rsa_chars="$(cat ~/.ssh/id_rsa)"
if test "${#my_rsa_chars}" -lt 2500; then
fn_warn_rsa >&2
echo >&2 ""
fi
fi
}
main