Issue
I want to import an ECDSA key into AWS EC2. Is it true that AWS EC2 only imports RSA keys, and not ECDSA or ED25519 keys? If there is a way to import an ECDSA key, how would that be done?
# Discover the apt package that provided ssh-keygen
$ apt-file search `which ssh-keygen`
openssh-client: /usr/bin/ssh-keygen
openssh-client-ssh1: /usr/bin/ssh-keygen1
# Discover the installed version of openssh-client
$ apt list openssh-client
Listing... Done
openssh-client/groovy,now 1:8.3p1-1 amd64 [installed,automatic]
# Make a new key pair
$ ssh-keygen -b 521 -f ~/.ssh/key2020-10-28 -P "" -t ecdsa
# Import the new key pair
$ aws ec2 import-key-pair --key-name key2020-10-28 \
--public-key-material fileb://~/.ssh/key2020-10-28.pub
#An error occurred (InvalidKey.Format) when calling the ImportKeyPair operation:
#Key is not in valid OpenSSH public key format
# Try again with a base64 encoded key:
$ base64 ~/.ssh/key2020-10-28.pub > ~/.ssh/key2020-10-28.pub.b64
$ aws ec2 import-key-pair --key-name key2020-10-28 \
--public-key-material fileb://~/.ssh/key2020-10-28.pub.b64
# An error occurred (InvalidKey.Format) when calling the ImportKeyPair operation:
# Key is not in valid OpenSSH public key format
Next I tried the old RSA key algorithm, which worked:
ssh-keygen -b 4096 -f ~/.ssh/ec2 -P "" -N "" -t rsa
aws ec2 import-key-pair --key-name rsa2020-10-28 \
--public-key-material fileb://~/.ssh/rsa2020-10-28.pub
{
"KeyFingerprint": "c9:32:25:36:fd:b8:a0:83:09:1b:56:6f:86:a1:18:4e",
"KeyName": "rsa2020-10-28",
"KeyPairId": "key-04ebd4202d5988526"
}
Next I tried the old and insecure DSA key algorithm:
ssh-keygen -f ~/.ssh/dsa2020-10-28 -P "" -t dsa
aws ec2 import-key-pair --key-name dsa2020-10-28 \
--public-key-material fileb://~/.ssh/dsa2020-10-28.pub
An error occurred (InvalidKey.Format) when calling the ImportKeyPair operation:
Key is not in valid OpenSSH public key format
Next I tried the ed25519 key algorithm:
ssh-keygen -f ~/.ssh/ed25519-2020-10-28 -P "" -t ed25519
aws ec2 import-key-pair --key-name dsa2020-10-28 \
--public-key-material fileb://~/.ssh/ed25519-2020-10-28.pub
An error occurred (InvalidKey.Format) when calling the ImportKeyPair operation:
Key is not in valid OpenSSH public key format
Solution
As @dave_thompson_085 correctly says, the AWS documentation says Description: Imports the public key from an RSA key pair that you created
Answered By - Mike Slinn