2019-02-04 12:21:58 -05:00
|
|
|
package ssh
|
2019-01-30 21:59:56 -05:00
|
|
|
|
|
|
|
import (
|
2019-02-04 12:07:32 -05:00
|
|
|
"bytes"
|
2019-01-30 21:59:56 -05:00
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
2019-02-04 12:07:32 -05:00
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
2019-02-04 12:29:47 -05:00
|
|
|
gossh "golang.org/x/crypto/ssh"
|
2019-02-06 13:46:55 -05:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
2019-01-30 21:59:56 -05:00
|
|
|
)
|
|
|
|
|
2019-02-03 09:17:18 -05:00
|
|
|
// expected contains the data that the key pair should contain.
|
|
|
|
type expected struct {
|
2019-02-04 12:21:58 -05:00
|
|
|
kind KeyPairType
|
2019-02-03 09:17:18 -05:00
|
|
|
bits int
|
|
|
|
desc string
|
2019-02-04 12:07:32 -05:00
|
|
|
name string
|
|
|
|
data []byte
|
2019-02-03 09:17:18 -05:00
|
|
|
}
|
|
|
|
|
2019-02-06 13:46:55 -05:00
|
|
|
const (
|
|
|
|
PemRsa1024 = `-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
MIICWwIBAAKBgQDJEMFPpTBiWNDb3qEIPTSeEnIP8FZdBpG8njOrclcMoQQNhzZ+
|
|
|
|
4uz37tqtHMp36Z7LB4/+85NN6epNXO+ekyZIHswiyBcJC2sT3KuH7nG1BESOooPY
|
|
|
|
DfeCSM+CJT9GDIhy9nUXSsJjrceEyh/B5DjEtIbS0XfcRelrNTJodCmPJwIDAQAB
|
|
|
|
AoGAK66GMOV0c4lUJtBhL8cMTWM4gJn4SVGKC+5az16R5t58YOwFPN/UF7E+tOlS
|
|
|
|
W2bX5sgH0p3cXMr66j/Mlyjk4deLg7trDavulIP93MyVO2SUJ0cstQ0ZmRz2oGwx
|
|
|
|
Gow+hD75Cet7uvepdmG4DKHJe8D/I72rtP1WKuZyd0vP6WECQQDua6wWlyEdIimx
|
|
|
|
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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-----
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2019-02-04 12:21:58 -05:00
|
|
|
func (o expected) matches(kp KeyPair) error {
|
2019-02-03 09:17:18 -05:00
|
|
|
if o.kind.String() == "" {
|
|
|
|
return errors.New("expected kind's value cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.bits <= 0 {
|
|
|
|
return errors.New("expected bits' value cannot be less than or equal to 0")
|
2019-01-30 21:59:56 -05:00
|
|
|
}
|
|
|
|
|
2019-02-03 09:17:18 -05:00
|
|
|
if o.desc == "" {
|
|
|
|
return errors.New("expected description's value cannot be empty")
|
2019-01-30 21:59:56 -05:00
|
|
|
}
|
|
|
|
|
2019-02-04 12:07:32 -05:00
|
|
|
if len(o.data) == 0 {
|
|
|
|
return errors.New("expected random data value cannot be nothing")
|
|
|
|
}
|
|
|
|
|
2019-02-03 09:17:18 -05:00
|
|
|
if kp.Type() != o.kind {
|
2019-02-04 12:07:32 -05:00
|
|
|
return errors.New("key pair type should be " + o.kind.String() +
|
|
|
|
" - got '" + kp.Type().String() + "'")
|
2019-01-30 21:59:56 -05:00
|
|
|
}
|
|
|
|
|
2019-02-03 09:17:18 -05:00
|
|
|
if kp.Bits() != o.bits {
|
2019-02-04 12:07:32 -05:00
|
|
|
return errors.New("key pair bits should be " + strconv.Itoa(o.bits) +
|
|
|
|
" - got " + strconv.Itoa(kp.Bits()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(o.name) > 0 && kp.Name() != o.name {
|
|
|
|
return errors.New("key pair name should be '" + o.name +
|
|
|
|
"' - got '" + kp.Name() + "'")
|
2019-02-03 09:17:18 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 13:37:09 -05:00
|
|
|
if kp.Description() != o.desc {
|
2019-02-04 12:07:32 -05:00
|
|
|
return errors.New("key pair description should be '" +
|
2019-02-05 13:37:09 -05:00
|
|
|
o.desc + "' - got '" + kp.Description() + "'")
|
2019-02-03 09:17:18 -05:00
|
|
|
}
|
|
|
|
|
2019-02-04 12:07:32 -05:00
|
|
|
err := o.verifyPublicKeyAuthorizedKeysFormat(kp)
|
2019-02-03 10:36:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-04 12:21:58 -05:00
|
|
|
err = o.verifyKeyPair(kp)
|
2019-01-30 21:59:56 -05:00
|
|
|
if err != nil {
|
2019-02-03 09:17:18 -05:00
|
|
|
return err
|
2019-01-30 21:59:56 -05:00
|
|
|
}
|
2019-02-03 09:17:18 -05:00
|
|
|
|
|
|
|
return nil
|
2019-01-30 21:59:56 -05:00
|
|
|
}
|
|
|
|
|
2019-02-04 12:21:58 -05:00
|
|
|
func (o expected) verifyPublicKeyAuthorizedKeysFormat(kp KeyPair) error {
|
|
|
|
newLines := []NewLineOption{
|
|
|
|
UnixNewLine,
|
|
|
|
NoNewLine,
|
|
|
|
WindowsNewLine,
|
2019-02-04 12:07:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, nl := range newLines {
|
2019-02-04 14:27:14 -05:00
|
|
|
publicKeyAk := kp.PublicKeyAuthorizedKeysLine(nl)
|
2019-02-04 12:07:32 -05:00
|
|
|
|
|
|
|
if len(publicKeyAk) < 2 {
|
|
|
|
return errors.New("expected public key in authorized keys format to be at least 2 bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch nl {
|
2019-02-04 12:21:58 -05:00
|
|
|
case NoNewLine:
|
2019-02-06 13:46:55 -05:00
|
|
|
if publicKeyAk[len(publicKeyAk)-1] == '\n' {
|
2019-02-04 12:07:32 -05:00
|
|
|
return errors.New("public key in authorized keys format has trailing new line when none was specified")
|
|
|
|
}
|
2019-02-04 12:21:58 -05:00
|
|
|
case UnixNewLine:
|
2019-02-06 13:46:55 -05:00
|
|
|
if publicKeyAk[len(publicKeyAk)-1] != '\n' {
|
2019-02-04 12:07:32 -05:00
|
|
|
return errors.New("public key in authorized keys format does not have unix new line when unix was specified")
|
|
|
|
}
|
2019-02-06 13:46:55 -05:00
|
|
|
if string(publicKeyAk[len(publicKeyAk)-2:]) == WindowsNewLine.String() {
|
2019-02-04 12:07:32 -05:00
|
|
|
return errors.New("public key in authorized keys format has windows new line when unix was specified")
|
|
|
|
}
|
2019-02-04 12:21:58 -05:00
|
|
|
case WindowsNewLine:
|
2019-02-06 13:46:55 -05:00
|
|
|
if string(publicKeyAk[len(publicKeyAk)-2:]) != WindowsNewLine.String() {
|
2019-02-04 12:07:32 -05:00
|
|
|
return errors.New("public key in authorized keys format does not have windows new line when windows was specified")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(o.name) > 0 {
|
|
|
|
if len(publicKeyAk) < len(o.name) {
|
|
|
|
return errors.New("public key in authorized keys format is shorter than the key pair's name")
|
|
|
|
}
|
|
|
|
|
|
|
|
suffix := []byte{' '}
|
|
|
|
suffix = append(suffix, o.name...)
|
|
|
|
suffix = append(suffix, nl.Bytes()...)
|
|
|
|
if !bytes.HasSuffix(publicKeyAk, suffix) {
|
|
|
|
return errors.New("public key in authorized keys format with name does not have name in suffix - got '" +
|
|
|
|
string(publicKeyAk) + "'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 12:21:58 -05:00
|
|
|
func (o expected) verifyKeyPair(kp KeyPair) error {
|
2019-02-04 12:29:47 -05:00
|
|
|
signer, err := gossh.ParsePrivateKey(kp.PrivateKeyPemBlock())
|
2019-02-04 12:07:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("failed to parse private key during verification - " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
signature, err := signer.Sign(rand.Reader, o.data)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("failed to sign test data during verification - " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = signer.PublicKey().Verify(o.data, signature)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("failed to verify test data - " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 12:21:58 -05:00
|
|
|
func TestDefaultKeyPairBuilder_Build_Default(t *testing.T) {
|
|
|
|
kp, err := NewKeyPairBuilder().Build()
|
2019-01-30 21:59:56 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
2019-02-03 09:17:18 -05:00
|
|
|
err = expected{
|
2019-02-04 12:23:42 -05:00
|
|
|
kind: Ecdsa,
|
2019-02-03 09:17:18 -05:00
|
|
|
bits: 521,
|
2019-02-05 13:37:09 -05:00
|
|
|
desc: "521 bit ECDSA",
|
2019-02-04 12:07:32 -05:00
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
2019-02-03 09:17:18 -05:00
|
|
|
}.matches(kp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
2019-01-30 21:59:56 -05:00
|
|
|
}
|
2019-02-03 09:17:18 -05:00
|
|
|
}
|
2019-01-30 21:59:56 -05:00
|
|
|
|
2019-02-04 12:21:58 -05:00
|
|
|
func TestDefaultKeyPairBuilder_Build_EcdsaDefault(t *testing.T) {
|
|
|
|
kp, err := NewKeyPairBuilder().
|
2019-02-04 12:23:42 -05:00
|
|
|
SetType(Ecdsa).
|
2019-02-04 12:07:32 -05:00
|
|
|
Build()
|
2019-02-03 09:17:18 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
2019-01-30 21:59:56 -05:00
|
|
|
}
|
|
|
|
|
2019-02-03 09:17:18 -05:00
|
|
|
err = expected{
|
2019-02-04 12:23:42 -05:00
|
|
|
kind: Ecdsa,
|
2019-02-03 09:17:18 -05:00
|
|
|
bits: 521,
|
2019-02-05 13:37:09 -05:00
|
|
|
desc: "521 bit ECDSA",
|
2019-02-04 12:07:32 -05:00
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
2019-02-03 09:17:18 -05:00
|
|
|
}.matches(kp)
|
2019-01-30 21:59:56 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 14:25:37 -05:00
|
|
|
func TestDefaultKeyPairBuilder_Build_EcdsaSupportedCurves(t *testing.T) {
|
|
|
|
supportedBits := []int{
|
|
|
|
521,
|
|
|
|
384,
|
|
|
|
256,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, bits := range supportedBits {
|
|
|
|
kp, err := NewKeyPairBuilder().
|
|
|
|
SetType(Ecdsa).
|
|
|
|
SetBits(bits).
|
|
|
|
Build()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = expected{
|
|
|
|
kind: Ecdsa,
|
|
|
|
bits: bits,
|
2019-02-05 13:37:09 -05:00
|
|
|
desc: strconv.Itoa(bits) + " bit ECDSA",
|
2019-02-04 14:25:37 -05:00
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}.matches(kp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 12:21:58 -05:00
|
|
|
func TestDefaultKeyPairBuilder_Build_RsaDefault(t *testing.T) {
|
|
|
|
kp, err := NewKeyPairBuilder().
|
2019-02-04 12:23:42 -05:00
|
|
|
SetType(Rsa).
|
2019-02-04 12:07:32 -05:00
|
|
|
Build()
|
2019-01-30 21:59:56 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
2019-02-03 09:17:18 -05:00
|
|
|
err = expected{
|
2019-02-04 12:23:42 -05:00
|
|
|
kind: Rsa,
|
2019-02-03 09:17:18 -05:00
|
|
|
bits: 4096,
|
2019-02-05 13:37:09 -05:00
|
|
|
desc: "4096 bit RSA",
|
2019-02-04 12:07:32 -05:00
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
2019-02-03 09:17:18 -05:00
|
|
|
}.matches(kp)
|
2019-01-30 21:59:56 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 12:21:58 -05:00
|
|
|
func TestDefaultKeyPairBuilder_Build_NamedEcdsa(t *testing.T) {
|
2019-02-04 12:07:32 -05:00
|
|
|
name := uuid.TimeOrderedUUID()
|
2019-01-30 21:59:56 -05:00
|
|
|
|
2019-02-04 12:21:58 -05:00
|
|
|
kp, err := NewKeyPairBuilder().
|
2019-02-04 12:23:42 -05:00
|
|
|
SetType(Ecdsa).
|
2019-02-04 12:07:32 -05:00
|
|
|
SetName(name).
|
|
|
|
Build()
|
2019-01-30 21:59:56 -05:00
|
|
|
if err != nil {
|
2019-02-04 12:07:32 -05:00
|
|
|
t.Fatal(err.Error())
|
2019-01-30 21:59:56 -05:00
|
|
|
}
|
|
|
|
|
2019-02-04 12:07:32 -05:00
|
|
|
err = expected{
|
2019-02-04 12:23:42 -05:00
|
|
|
kind: Ecdsa,
|
2019-02-04 12:07:32 -05:00
|
|
|
bits: 521,
|
2019-02-05 13:37:09 -05:00
|
|
|
desc: "521 bit ECDSA named " + name,
|
2019-02-04 12:07:32 -05:00
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
name: name,
|
|
|
|
}.matches(kp)
|
2019-01-30 21:59:56 -05:00
|
|
|
if err != nil {
|
2019-02-04 12:07:32 -05:00
|
|
|
t.Fatal(err.Error())
|
2019-01-30 21:59:56 -05:00
|
|
|
}
|
|
|
|
}
|
2019-02-03 10:36:06 -05:00
|
|
|
|
2019-02-04 12:21:58 -05:00
|
|
|
func TestDefaultKeyPairBuilder_Build_NamedRsa(t *testing.T) {
|
2019-02-04 12:07:32 -05:00
|
|
|
name := uuid.TimeOrderedUUID()
|
2019-02-03 10:36:06 -05:00
|
|
|
|
2019-02-04 12:21:58 -05:00
|
|
|
kp, err := NewKeyPairBuilder().
|
2019-02-04 12:23:42 -05:00
|
|
|
SetType(Rsa).
|
2019-02-04 12:07:32 -05:00
|
|
|
SetName(name).
|
|
|
|
Build()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
2019-02-03 10:36:06 -05:00
|
|
|
}
|
|
|
|
|
2019-02-04 12:07:32 -05:00
|
|
|
err = expected{
|
2019-02-04 12:23:42 -05:00
|
|
|
kind: Rsa,
|
2019-02-04 12:07:32 -05:00
|
|
|
bits: 4096,
|
2019-02-05 13:37:09 -05:00
|
|
|
desc: "4096 bit RSA named " + name,
|
2019-02-04 12:07:32 -05:00
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
name: name,
|
|
|
|
}.matches(kp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
2019-02-03 10:36:06 -05:00
|
|
|
}
|
2019-02-06 13:46:55 -05:00
|
|
|
|
|
|
|
func TestDefaultKeyPairBuilder_SetPrivateKey(t *testing.T) {
|
|
|
|
name := uuid.TimeOrderedUUID()
|
|
|
|
pemData := make(map[string]expected)
|
|
|
|
pemData[PemRsa1024] = expected{
|
|
|
|
bits: 1024,
|
|
|
|
kind: Rsa,
|
|
|
|
name: name,
|
|
|
|
desc: "1024 bit RSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemRsa2048] = expected{
|
|
|
|
bits: 2048,
|
|
|
|
kind: Rsa,
|
|
|
|
name: name,
|
|
|
|
desc: "2048 bit RSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemOpenSshRsa1024] = expected{
|
|
|
|
bits: 1024,
|
|
|
|
kind: Rsa,
|
|
|
|
name: name,
|
|
|
|
desc: "1024 bit RSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemOpenSshRsa2048] = expected{
|
|
|
|
bits: 2048,
|
|
|
|
kind: Rsa,
|
|
|
|
name: name,
|
|
|
|
desc: "2048 bit RSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemDsa] = expected{
|
|
|
|
bits: 1024,
|
|
|
|
kind: Dsa,
|
|
|
|
name: name,
|
|
|
|
desc: "1024 bit DSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemEcdsa384] = expected{
|
|
|
|
bits: 384,
|
|
|
|
kind: Ecdsa,
|
|
|
|
name: name,
|
|
|
|
desc: "384 bit ECDSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemEcdsa521] = expected{
|
|
|
|
bits: 521,
|
|
|
|
kind: Ecdsa,
|
|
|
|
name: name,
|
|
|
|
desc: "521 bit ECDSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemOpenSshEd25519] = expected{
|
|
|
|
bits: 256,
|
|
|
|
kind: Ed25519,
|
|
|
|
name: name,
|
|
|
|
desc: "256 bit ED25519 named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
|
|
|
|
for s, l := range pemData {
|
|
|
|
kp, err := NewKeyPairBuilder().SetPrivateKey([]byte(s)).SetName(name).Build()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = l.matches(kp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultKeyPairBuilder_SetPrivateKey_Override(t *testing.T) {
|
|
|
|
name := uuid.TimeOrderedUUID()
|
|
|
|
pemData := make(map[string]expected)
|
|
|
|
pemData[PemRsa1024] = expected{
|
|
|
|
bits: 1024,
|
|
|
|
kind: Rsa,
|
|
|
|
name: name,
|
|
|
|
desc: "1024 bit RSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemRsa2048] = expected{
|
|
|
|
bits: 2048,
|
|
|
|
kind: Rsa,
|
|
|
|
name: name,
|
|
|
|
desc: "2048 bit RSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemOpenSshRsa1024] = expected{
|
|
|
|
bits: 1024,
|
|
|
|
kind: Rsa,
|
|
|
|
name: name,
|
|
|
|
desc: "1024 bit RSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemOpenSshRsa2048] = expected{
|
|
|
|
bits: 2048,
|
|
|
|
kind: Rsa,
|
|
|
|
name: name,
|
|
|
|
desc: "2048 bit RSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemDsa] = expected{
|
|
|
|
bits: 1024,
|
|
|
|
kind: Dsa,
|
|
|
|
name: name,
|
|
|
|
desc: "1024 bit DSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemEcdsa384] = expected{
|
|
|
|
bits: 384,
|
|
|
|
kind: Ecdsa,
|
|
|
|
name: name,
|
|
|
|
desc: "384 bit ECDSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemEcdsa521] = expected{
|
|
|
|
bits: 521,
|
|
|
|
kind: Ecdsa,
|
|
|
|
name: name,
|
|
|
|
desc: "521 bit ECDSA named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
pemData[PemOpenSshEd25519] = expected{
|
|
|
|
bits: 256,
|
|
|
|
kind: Ed25519,
|
|
|
|
name: name,
|
|
|
|
desc: "256 bit ED25519 named " + name,
|
|
|
|
data: []byte(uuid.TimeOrderedUUID()),
|
|
|
|
}
|
|
|
|
|
|
|
|
supportedKeyTypes := []KeyPairType{Rsa, Dsa}
|
|
|
|
for _, kt := range supportedKeyTypes {
|
|
|
|
for s, l := range pemData {
|
|
|
|
kp, err := NewKeyPairBuilder().
|
|
|
|
SetPrivateKey([]byte(s)).
|
|
|
|
SetName(name).
|
|
|
|
SetType(kt).
|
|
|
|
Build()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = l.matches(kp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|