func (c *client) List() ([]*Key, error) {
	req := []byte{agentRequestIdentities}
	msg, err := c.call(req)
	if err != nil {
		return nil, err
	}
	switch msg := msg.(type) {
	case *identitiesAnswerAgentMsg:
		if msg.NumKeys > maxAgentResponseBytes/8 {
			return nil, errors.New("agent: too many keys in agent reply")
		}
		keys := make([]*Key, msg.NumKeys)
		data := msg.Keys
		for i := uint32(0); i < msg.NumKeys; i++ {
			var key *Key
			var err error
			if key, data, err = parseKey(data); err != nil {
				return nil, err
			}
			keys[i] = key
		}
		return keys, nil
	case *failureAgentMsg:
		return nil, errors.New("agent: failed to list keys")
	}
	panic("unreachable")
}
List returns the identities known to the agent.
package agent
import (
	"bytes"
	"crypto/dsa"
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rsa"
	"encoding/base64"
	"encoding/binary"
	"errors"
	"fmt"
	"io"
	"math/big"
	"sync"
	"golang.org/x/crypto/ed25519"
	"golang.org/x/crypto/ssh"
)
type Agent interface {
	List() ([]*Key, error)
	Sign(key ssh.PublicKey, data []byte) (*ssh.Signature, error)
	Add(key AddedKey) error
	Remove(key ssh.PublicKey) error
	RemoveAll() error
	Lock(passphrase []byte) error
	Unlock(passphrase []byte) error
	Signers() ([]ssh.Signer, error)
}
type AddedKey struct {
	PrivateKey interface{}
	Certificate *ssh.Certificate
	Comment string
	LifetimeSecs uint32
	ConfirmBeforeUse bool
}
const (
	agentRequestV1Identities   = 1
	agentRemoveAllV1Identities = 9
	agentAddIdentity         = 17
	agentRemoveIdentity      = 18
	agentRemoveAllIdentities = 19
	agentAddIdConstrained    = 25
	agentAddSmartcardKey            = 20
	agentRemoveSmartcardKey         = 21
	agentLock                       = 22
	agentUnlock                     = 23
	agentAddSmartcardKeyConstrained = 26
	agentConstrainLifetime = 1
	agentConstrainConfirm  = 2
)
const maxAgentResponseBytes = 16 << 20
const agentFailure = 5
type failureAgentMsg struct{}
const agentSuccess = 6
type successAgentMsg struct{}
const agentRequestIdentities = 11
type requestIdentitiesAgentMsg struct{}
const agentIdentitiesAnswer = 12
type identitiesAnswerAgentMsg struct {
	NumKeys uint32 `sshtype:"12"`
	Keys    []byte `ssh:"rest"`
}
const agentSignRequest = 13
type signRequestAgentMsg struct {
	KeyBlob []byte `sshtype:"13"`
	Data    []byte
	Flags   uint32
}
const agentSignResponse = 14
type signResponseAgentMsg struct {
	SigBlob []byte `sshtype:"14"`
}
type publicKey struct {
	Format string
	Rest   []byte `ssh:"rest"`
}
type Key struct {
	Format  string
	Blob    []byte
	Comment string
}
func clientErr(err error) error {
	return fmt.Errorf("agent: client error: %v", err)
}
func (k *Key) String() string {
	s := string(k.Format) + " " + base64.StdEncoding.EncodeToString(k.Blob)
	if k.Comment != "" {
		s += " " + k.Comment
	}
	return s
}
func (k *Key) Type() string {
	return k.Format
}
func (k *Key) Marshal() []byte {
	return k.Blob
}
func (k *Key) Verify(data []byte, sig *ssh.Signature) error {
	pubKey, err := ssh.ParsePublicKey(k.Blob)
	if err != nil {
		return fmt.Errorf("agent: bad public key: %v", err)
	}
	return pubKey.Verify(data, sig)
}
type wireKey struct {
	Format string
	Rest   []byte `ssh:"rest"`
}
func parseKey(in []byte) (out *Key, rest []byte, err error) {
	var record struct {
		Blob    []byte
		Comment string
		Rest    []byte `ssh:"rest"`
	}
	if err := ssh.Unmarshal(in, &record); err != nil {
		return nil, nil, err
	}
	var wk wireKey
	if err := ssh.Unmarshal(record.Blob, &wk); err != nil {
		return nil, nil, err
	}
	return &Key{
		Format:  wk.Format,
		Blob:    record.Blob,
		Comment: record.Comment,
	}, record.Rest, nil
}
type client struct {
	conn io.ReadWriter
	mu sync.Mutex
}
func NewClient(rw io.ReadWriter) Agent {
	return &client{conn: rw}
}
func (c *client) call(req []byte) (reply interface{}, err error) {
	c.mu.Lock()
	defer c.mu.Unlock()
	msg := make([]byte, 4+len(req))
	binary.BigEndian.PutUint32(msg, uint32(len(req)))
	copy(msg[4:], req)
	if _, err = c.conn.Write(msg); err != nil {
		return nil, clientErr(err)
	}
	var respSizeBuf [4]byte
	if _, err = io.ReadFull(c.conn, respSizeBuf[:]); err != nil {
		return nil, clientErr(err)
	}
	respSize := binary.BigEndian.Uint32(respSizeBuf[:])
	if respSize > maxAgentResponseBytes {
		return nil, clientErr(err)
	}
	buf := make([]byte, respSize)
	if _, err = io.ReadFull(c.conn, buf); err != nil {
		return nil, clientErr(err)
	}
	reply, err = unmarshal(buf)
	if err != nil {
		return nil, clientErr(err)
	}
	return reply, err
}
func (c *client) simpleCall(req []byte) error {
	resp, err := c.call(req)
	if err != nil {
		return err
	}
	if _, ok := resp.(*successAgentMsg); ok {
		return nil
	}
	return errors.New("agent: failure")
}
func (c *client) RemoveAll() error {
	return c.simpleCall([]byte{agentRemoveAllIdentities})
}
func (c *client) Remove(key ssh.PublicKey) error {
	req := ssh.Marshal(&agentRemoveIdentityMsg{
		KeyBlob: key.Marshal(),
	})
	return c.simpleCall(req)
}
func (c *client) Lock(passphrase []byte) error {
	req := ssh.Marshal(&agentLockMsg{
		Passphrase: passphrase,
	})
	return c.simpleCall(req)
}
func (c *client) Unlock(passphrase []byte) error {
	req := ssh.Marshal(&agentUnlockMsg{
		Passphrase: passphrase,
	})
	return c.simpleCall(req)
}