proxy.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. import (
  3. "bytes"
  4. "log"
  5. "golang.org/x/crypto/ssh"
  6. "golang.org/x/crypto/ssh/agent"
  7. )
  8. type proxy struct {
  9. secretiveAgent agent.ExtendedAgent
  10. keyringAgent agent.ExtendedAgent
  11. }
  12. func NewProxy(secretiveAgent agent.ExtendedAgent, keyringAgent agent.ExtendedAgent) agent.ExtendedAgent {
  13. return &proxy{
  14. secretiveAgent: secretiveAgent,
  15. keyringAgent: keyringAgent,
  16. }
  17. }
  18. func (p *proxy) List() ([]*agent.Key, error) {
  19. secretiveKeys, err := p.secretiveAgent.List()
  20. if err != nil {
  21. return nil, err
  22. }
  23. keyringKeys, err := p.keyringAgent.List()
  24. if err != nil {
  25. return nil, err
  26. }
  27. var allKeys []*agent.Key
  28. allKeys = append(allKeys, secretiveKeys...)
  29. allKeys = append(allKeys, keyringKeys...)
  30. return allKeys, nil
  31. }
  32. func (p *proxy) Add(key agent.AddedKey) error {
  33. log.Printf("added key %s\n", key.Comment)
  34. return p.keyringAgent.Add(key)
  35. }
  36. func (p *proxy) Sign(key ssh.PublicKey, data []byte) (*ssh.Signature, error) {
  37. return p.SignWithFlags(key, data, 0)
  38. }
  39. func (p *proxy) SignWithFlags(key ssh.PublicKey, data []byte, flags agent.SignatureFlags) (*ssh.Signature, error) {
  40. keyringKeys, err := p.keyringAgent.List()
  41. if err != nil {
  42. return nil, err
  43. }
  44. for _, k := range keyringKeys {
  45. if bytes.Equal(k.Blob, key.Marshal()) {
  46. log.Printf("signing with keyring\n")
  47. return p.keyringAgent.SignWithFlags(key, data, flags)
  48. }
  49. }
  50. log.Printf("signing with secretive\n")
  51. return p.secretiveAgent.SignWithFlags(key, data, flags)
  52. }
  53. func (p *proxy) Remove(key ssh.PublicKey) error {
  54. log.Printf("removed key\n")
  55. return p.keyringAgent.Remove(key)
  56. }
  57. func (p *proxy) RemoveAll() error {
  58. log.Printf("cleared keys\n")
  59. return p.keyringAgent.RemoveAll()
  60. }
  61. func (p *proxy) Lock(passphrase []byte) error {
  62. return p.keyringAgent.Lock(passphrase)
  63. }
  64. func (p *proxy) Unlock(passphrase []byte) error {
  65. return p.keyringAgent.Unlock(passphrase)
  66. }
  67. func (p *proxy) Signers() ([]ssh.Signer, error) {
  68. secretiveSigners, err := p.secretiveAgent.Signers()
  69. if err != nil {
  70. return nil, err
  71. }
  72. keyringSigners, err := p.keyringAgent.Signers()
  73. if err != nil {
  74. return nil, err
  75. }
  76. var allSigners []ssh.Signer
  77. allSigners = append(allSigners, secretiveSigners...)
  78. allSigners = append(allSigners, keyringSigners...)
  79. return allSigners, nil
  80. }
  81. // The keyring does not support any extensions
  82. func (p *proxy) Extension(extensionType string, contents []byte) ([]byte, error) {
  83. return nil, agent.ErrExtensionUnsupported
  84. }