package ssh import ( "bytes" "crypto/dsa" "crypto/ecdsa" "crypto/rsa" "fmt" "testing" "github.com/hashicorp/packer/common/uuid" "golang.org/x/crypto/ed25519" gossh "golang.org/x/crypto/ssh" ) const ( pemRsa1024 = `-----BEGIN RSA PRIVATE KEY----- MIICWwIBAAKBgQDJEMFPpTBiWNDb3qEIPTSeEnIP8FZdBpG8njOrclcMoQQNhzZ+ 4uz37tqtHMp36Z7LB4/+85NN6epNXO+ekyZIHswiyBcJC2sT3KuH7nG1BESOooPY DfeCSM+CJT9GDIhy9nUXSsJjrceEyh/B5DjEtIbS0XfcRelrNTJodCmPJwIDAQAB AoGAK66GMOV0c4lUJtBhL8cMTWM4gJn4SVGKC+5az16R5t58YOwFPN/UF7E+tOlS W2bX5sgH0p3cXMr66j/Mlyjk4deLg7trDavulIP93MyVO2SUJ0cstQ0ZmRz2oGwx Gow+hD75Cet7uvepdmG4DKHJe8D/I72rtP1WKuZyd0vP6WECQQDua6wWlyEdIimx XoGWUvmywACWPnQmBnyHG7x5hxMjijQoQZu60zRxSU9I5q08BerTsvbTc+xLnDVv mFzlcjT/AkEA1+P7lcvViZeNKoDB1Qt+VV+pkcqL5aoRwdnLA51SyFJ9tXkxeZwA LOof3xtoRGhCld7ixi3kF5aZsafAJOZd2QJAH8rFyMFgTgU3MAqdFxF7cGV/7ojn bgahZlbBfCcR20Rbjh6piHEPZifTZbI02XMkjBQqK6oikTaEPZxAjuv6uwJANczu yWm+kUdfOpRTuY/fr87jJx3etyEmw7ROz1vJYXqNMUg+eBvUP10pDCR8W2/QCCE/ Sjvtd6NkMc2oKInwIQJAFZ1xJte0EaQsXaCIoZwHrQJbK1dd5l1xTAzz51voAcKH 2K23xgx4I+/eam2enjFa7wXLZFoW0xg/51xsaIjnrA== -----END RSA PRIVATE KEY----- ` pemRsa2048 = `-----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEA/ZPazeRmBapF01gzHXtJGpu0S936xHY+pOrIyIk6lEE06paf q5gh6BCui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H65fX exrJE+p69wgRVndoqQAAACJjaHJpc0Bwb2V0YXN0ZXIuY29ycC5tdXR1YWxpbmsubmV0AQ IDBAUGBw== -----END OPENSSH PRIVATE KEY----- ` pemDsa = `-----BEGIN DSA PRIVATE KEY----- MIIBuwIBAAKBgQDH/T+IkpbdA9nUM7O4MMRoeS0bn7iXWs63Amo2fsIyJPxDvjjF 5HZBH5Rq045TFCCWHjymwiYof+wvwUMZIUH++ABTrKzes/r5qG5jXp42pFWf6nTI zHwttdjvNiXr+AgreXOrJKhjv6Ga3hq8MNcXMa9xFsIB83EZNMBPxbj0nwIVAJQW 1eR4Uf8/8haQb4HkTsoH+R5/AoGBAK9FV5LIZxY1TeNsD5eXoqpTqCy1WROMggSG VZ4yN0rrKCtLd8am61m/L8VCMUWiO3IJQdq3yWBTEBbsShL/toau9beUdTl6rdB8 wcEcNgtZnhypQR58HlmgUFWC45rW37hW4AUJuMDgLxgqSVuoF1pDcHrHSi/fZwgp 7t0MKH2SAoGAJfUcLrXg5ZR8jbpZs4L/ubibUn+y35y+33aos07auMW1MesuNTcZ Ch42nbH2wKnbjk8eDxHdHLHzzOLGgYVMpUuBeuc7G5Q94rM/Z0I8HGQ6mvIkuFyp 58Unu5yu33GsNUgGEHmriiMGezXNXGNH/72PmTXuyxEMSrad23c6NZoCFAtIqbal 4tGCfnnmWU514A7ZzEKj -----END DSA PRIVATE KEY----- ` pemEcdsa384 = `-----BEGIN EC PRIVATE KEY----- MIGkAgEBBDAjuEIlmFyhGjFtJoAwD420FuPAjIknN3YwDZL4cfMFpB4YAK+7QVLs coAJ/ADuT7OgBwYFK4EEACKhZANiAASeXKyBr2prr4f4aOsM4dtVikYOUIL3yYnb GFOy7yHmauCnkIB48paXpvRE5m53Q8zgu7vkz/z9tcMBcC0GzpY3Sef37fmgTUuZ AJuJp36DMBdQel+j51TcQ79sizxCayg= -----END EC PRIVATE KEY----- ` pemEcdsa521 = `-----BEGIN EC PRIVATE KEY----- MIHcAgEBBEIBVCiwcf/did2vCIu3aMe7OeTD35PULm0hqmfkAK9OKIosi/DjOFfA 8h99rVNPaf+Cx/JNmEzR4bZNnYDyilSRCr+gBwYFK4EEACOhgYkDgYYABABHBMLP XbQoRF31ZGIeUj9jt9GqKES1dLBtGDEQSiiZFouL4tEIW7NfIZDpOIkA0khNcO8N xH6eylg0XOgcr01GRwCjY5VOapOahtn63SpajPGeKk+46F2dULIwrov9tWQuYNa3 P50N8j3rx6fAdgyDENOcCJlfNdNcySvkH4bgL1xcsw== -----END EC PRIVATE KEY----- ` pemOpenSshEd25519 = `-----BEGIN OPENSSH PRIVATE KEY----- b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW QyNTUxOQAAACAUftPhZQN17kAlThiiWJEgJvddm/pUhHvgrHUtpuYFOQAAAKjN+UhDzflI QwAAAAtzc2gtZWQyNTUxOQAAACAUftPhZQN17kAlThiiWJEgJvddm/pUhHvgrHUtpuYFOQ AAAEANXlEZdNU03RMmj77O2ojWh06Hbj8/qQ++H5wkt688NBR+0+FlA3XuQCVOGKJYkSAm 912b+lSEe+CsdS2m5gU5AAAAImNocmlzQHBvZXRhc3Rlci5jb3JwLm11dHVhbGluay5uZX QBAgM= -----END OPENSSH PRIVATE KEY----- ` ) func TestNewKeyPair_Default(t *testing.T) { kp, err := NewKeyPair(CreateKeyPairConfig{}) if err != nil { t.Fatal(err.Error()) } err = verifyEcdsaKeyPair(kp, expectedData{ bits: 521, }) if err != nil { t.Fatal(err.Error()) } } func TestNewKeyPair_ECDSA_Default(t *testing.T) { kp, err := NewKeyPair(CreateKeyPairConfig{ Type: Ecdsa, }) if err != nil { t.Fatal(err.Error()) } err = verifyEcdsaKeyPair(kp, expectedData{ bits: 521, }) if err != nil { t.Fatal(err.Error()) } } func TestNewKeyPair_ECDSA_Positive(t *testing.T) { for _, bits := range []int{521, 384, 256} { config := CreateKeyPairConfig{ Type: Ecdsa, Bits: bits, Comment: uuid.TimeOrderedUUID(), } kp, err := NewKeyPair(config) if err != nil { t.Fatal(err.Error()) } err = verifyEcdsaKeyPair(kp, expectedData{ bits: bits, comment: config.Comment, }) if err != nil { t.Fatal(err.Error()) } } } func TestNewKeyPair_ECDSA_Negative(t *testing.T) { for _, bits := range []int{224, 1, 2, 3} { _, err := NewKeyPair(CreateKeyPairConfig{ Type: Ecdsa, Bits: bits, }) if err == nil { t.Fatalf("expected key pair generation to fail for %d bits", bits) } } } func TestNewKeyPair_RSA_Positive(t *testing.T) { for _, bits := range []int{4096, 2048} { config := CreateKeyPairConfig{ Type: Rsa, Bits: bits, Comment: uuid.TimeOrderedUUID(), } kp, err := NewKeyPair(config) if err != nil { t.Fatal(err.Error()) } err = verifyRsaKeyPair(kp, expectedData{ bits: config.Bits, comment: config.Comment, }) if err != nil { t.Fatal(err.Error()) } } } func TestKeyPairFromPrivateKey(t *testing.T) { m := map[string]fromPrivateExpectedData{ pemRsa1024: { t: Rsa, d: expectedData{ bits: 1024, }, }, pemRsa2048: { t: Rsa, d: expectedData{ bits: 2048, }, }, pemOpenSshRsa1024: { t: Rsa, d: expectedData{ bits: 1024, }, }, pemOpenSshRsa2048: { t: Rsa, d: expectedData{ bits: 2048, }, }, pemDsa: { t: Dsa, d: expectedData{ bits: 1024, }, }, pemEcdsa384: { t: Ecdsa, d: expectedData{ bits: 384, }, }, pemEcdsa521: { t: Ecdsa, d: expectedData{ bits: 521, }, }, pemOpenSshEd25519: { t: Ed25519, d: expectedData{ bits: 256, }, }, } for rawPrivateKey, expected := range m { kp, err := KeyPairFromPrivateKey(FromPrivateKeyConfig{ RawPrivateKeyPemBlock: []byte(rawPrivateKey), }) if err != nil { t.Fatal(err.Error()) } switch expected.t { case Dsa: err = verifyDsaKeyPair(kp, expected) case Ecdsa: err = verifyEcdsaKeyPair(kp, expected.d) case Ed25519: err = verifyEd25519KeyPair(kp, expected) case Rsa: err = verifyRsaKeyPair(kp, expected.d) default: err = fmt.Errorf("unexected SSH key pair type %s", expected.t.String()) } if err != nil { t.Fatal(err.Error()) } } } type fromPrivateExpectedData struct { t KeyPairType d expectedData } type expectedData struct { bits int comment string } func verifyEcdsaKeyPair(kp KeyPair, e expectedData) error { privateKey, err := gossh.ParseRawPrivateKey(kp.PrivateKeyPemBlock) if err != nil { return err } pk, ok := privateKey.(*ecdsa.PrivateKey) if !ok { return fmt.Errorf("private key should be *ecdsa.PrivateKey") } if pk.Curve.Params().BitSize != e.bits { return fmt.Errorf("bit size should be %d - got %d", e.bits, pk.Curve.Params().BitSize) } publicKey, err := gossh.NewPublicKey(&pk.PublicKey) if err != nil { return err } expectedBytes := bytes.TrimSuffix(gossh.MarshalAuthorizedKey(publicKey), []byte("\n")) if len(e.comment) > 0 { expectedBytes = append(expectedBytes, ' ') expectedBytes = append(expectedBytes, e.comment...) } if !bytes.Equal(expectedBytes, kp.PublicKeyAuthorizedKeysLine) { return fmt.Errorf("authorized keys line should be:\n'%s'\nGot:\n'%s'", string(expectedBytes), string(kp.PublicKeyAuthorizedKeysLine)) } return nil } func verifyRsaKeyPair(kp KeyPair, e expectedData) error { privateKey, err := gossh.ParseRawPrivateKey(kp.PrivateKeyPemBlock) if err != nil { return err } pk, ok := privateKey.(*rsa.PrivateKey) if !ok { return fmt.Errorf("private key should be *rsa.PrivateKey") } if pk.N.BitLen() != e.bits { return fmt.Errorf("bit size should be %d - got %d", e.bits, pk.N.BitLen()) } publicKey, err := gossh.NewPublicKey(&pk.PublicKey) if err != nil { return err } expectedBytes := bytes.TrimSuffix(gossh.MarshalAuthorizedKey(publicKey), []byte("\n")) if len(e.comment) > 0 { expectedBytes = append(expectedBytes, ' ') expectedBytes = append(expectedBytes, e.comment...) } if !bytes.Equal(expectedBytes, kp.PublicKeyAuthorizedKeysLine) { return fmt.Errorf("authorized keys line should be:\n'%s'\nGot:\n'%s'", string(expectedBytes), string(kp.PublicKeyAuthorizedKeysLine)) } return nil } func verifyDsaKeyPair(kp KeyPair, e fromPrivateExpectedData) error { privateKey, err := gossh.ParseRawPrivateKey(kp.PrivateKeyPemBlock) if err != nil { return err } pk, ok := privateKey.(*dsa.PrivateKey) if !ok { return fmt.Errorf("private key should be *rsa.PrivateKey") } publicKey, err := gossh.NewPublicKey(&pk.PublicKey) if err != nil { return err } expectedBytes := bytes.TrimSuffix(gossh.MarshalAuthorizedKey(publicKey), []byte("\n")) if len(e.d.comment) > 0 { expectedBytes = append(expectedBytes, ' ') expectedBytes = append(expectedBytes, e.d.comment...) } if !bytes.Equal(expectedBytes, kp.PublicKeyAuthorizedKeysLine) { return fmt.Errorf("authorized keys line should be:\n'%s'\nGot:\n'%s'", string(expectedBytes), string(kp.PublicKeyAuthorizedKeysLine)) } return nil } func verifyEd25519KeyPair(kp KeyPair, e fromPrivateExpectedData) error { privateKey, err := gossh.ParseRawPrivateKey(kp.PrivateKeyPemBlock) if err != nil { return err } pk, ok := privateKey.(*ed25519.PrivateKey) if !ok { return fmt.Errorf("private key should be *rsa.PrivateKey") } publicKey, err := gossh.NewPublicKey(pk.Public()) if err != nil { return err } expectedBytes := bytes.TrimSuffix(gossh.MarshalAuthorizedKey(publicKey), []byte("\n")) if len(e.d.comment) > 0 { expectedBytes = append(expectedBytes, ' ') expectedBytes = append(expectedBytes, e.d.comment...) } if !bytes.Equal(expectedBytes, kp.PublicKeyAuthorizedKeysLine) { return fmt.Errorf("authorized keys line should be:\n'%s'\nGot:\n'%s'", string(expectedBytes), string(kp.PublicKeyAuthorizedKeysLine)) } return nil }