| 1 |
package sshutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"net" |
| 6 |
"runtime" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/pkg/errors" |
| 10 |
"golang.org/x/crypto/ssh" |
| 11 |
"golang.org/x/crypto/ssh/agent" |
| 12 |
) |
| 13 |
|
| 14 |
type options struct { |
| 15 |
filterBySignatureKey func(*agent.Key) bool |
| 16 |
removeExpiredKey func(*Agent, *agent.Key) bool |
| 17 |
} |
| 18 |
|
| 19 |
func newOptions(opts []AgentOption) *options { |
| 20 |
o := new(options) |
| 21 |
for _, fn := range opts { |
| 22 |
fn(o) |
| 23 |
} |
| 24 |
return o |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
type AgentOption func(o *options) |
| 29 |
|
| 30 |
|
| 31 |
func WithSignatureKey(keys []ssh.PublicKey) AgentOption { |
| 32 |
signingKeys := make([][]byte, len(keys)) |
| 33 |
for i, k := range keys { |
| 34 |
signingKeys[i] = k.Marshal() |
| 35 |
} |
| 36 |
return func(o *options) { |
| 37 |
o.filterBySignatureKey = func(k *agent.Key) bool { |
| 38 |
cert, err := ParseCertificate(k.Marshal()) |
| 39 |
if err != nil { |
| 40 |
return false |
| 41 |
} |
| 42 |
b := cert.SignatureKey.Marshal() |
| 43 |
for _, sb := range signingKeys { |
| 44 |
if bytes.Equal(b, sb) { |
| 45 |
return true |
| 46 |
} |
| 47 |
} |
| 48 |
return false |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
func WithRemoveExpiredCerts(t time.Time) AgentOption { |
| 55 |
unixNow := t.Unix() |
| 56 |
return func(o *options) { |
| 57 |
o.removeExpiredKey = func(a *Agent, k *agent.Key) bool { |
| 58 |
if cert, err := ParseCertificate(k.Marshal()); err == nil { |
| 59 |
if before := int64(cert.ValidBefore); cert.ValidBefore != uint64(ssh.CertTimeInfinity) && (unixNow >= before || before < 0) { |
| 60 |
if err := a.Remove(k); err == nil { |
| 61 |
return true |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
return false |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
var ErrNotFound = errors.New("not found") |
| 72 |
|
| 73 |
|
| 74 |
type Agent struct { |
| 75 |
agent.ExtendedAgent |
| 76 |
Conn net.Conn |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
func DialAgent() (*Agent, error) { |
| 82 |
return dialAgent() |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func (a *Agent) Close() error { |
| 87 |
return a.Conn.Close() |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func (a *Agent) AuthMethod() ssh.AuthMethod { |
| 92 |
return ssh.PublicKeysCallback(a.Signers) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func (a *Agent) HasKeys(opts ...AgentOption) (bool, error) { |
| 97 |
o := newOptions(opts) |
| 98 |
keys, err := a.List() |
| 99 |
if err != nil { |
| 100 |
return false, errors.Wrap(err, "error listing keys") |
| 101 |
} |
| 102 |
for _, key := range keys { |
| 103 |
if o.removeExpiredKey != nil && o.removeExpiredKey(a, key) { |
| 104 |
continue |
| 105 |
} |
| 106 |
if o.filterBySignatureKey == nil || o.filterBySignatureKey(key) { |
| 107 |
return true, nil |
| 108 |
} |
| 109 |
} |
| 110 |
return false, nil |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
func (a *Agent) ListKeys(opts ...AgentOption) ([]*agent.Key, error) { |
| 115 |
o := newOptions(opts) |
| 116 |
keys, err := a.List() |
| 117 |
if err != nil { |
| 118 |
return nil, errors.Wrap(err, "error listing keys") |
| 119 |
} |
| 120 |
var list []*agent.Key |
| 121 |
for _, key := range keys { |
| 122 |
if o.removeExpiredKey != nil && o.removeExpiredKey(a, key) { |
| 123 |
continue |
| 124 |
} |
| 125 |
if o.filterBySignatureKey == nil || o.filterBySignatureKey(key) { |
| 126 |
list = append(list, key) |
| 127 |
} |
| 128 |
} |
| 129 |
return list, nil |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
func (a *Agent) ListCertificates(opts ...AgentOption) ([]*ssh.Certificate, error) { |
| 134 |
keys, err := a.ListKeys(opts...) |
| 135 |
if err != nil { |
| 136 |
return nil, err |
| 137 |
} |
| 138 |
var list []*ssh.Certificate |
| 139 |
for _, key := range keys { |
| 140 |
if cert, err := ParseCertificate(key.Marshal()); err == nil { |
| 141 |
list = append(list, cert) |
| 142 |
} |
| 143 |
} |
| 144 |
return list, nil |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
func (a *Agent) GetKey(comment string, opts ...AgentOption) (*agent.Key, error) { |
| 149 |
o := newOptions(opts) |
| 150 |
keys, err := a.List() |
| 151 |
if err != nil { |
| 152 |
return nil, errors.Wrap(err, "error listing keys") |
| 153 |
} |
| 154 |
for _, key := range keys { |
| 155 |
if key.Comment == comment { |
| 156 |
if o.removeExpiredKey != nil && o.removeExpiredKey(a, key) { |
| 157 |
continue |
| 158 |
} |
| 159 |
if o.filterBySignatureKey == nil || o.filterBySignatureKey(key) { |
| 160 |
return key, nil |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
return nil, ErrNotFound |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
func (a *Agent) GetSigner(comment string, opts ...AgentOption) (ssh.Signer, error) { |
| 169 |
key, err := a.GetKey(comment, opts...) |
| 170 |
if err != nil { |
| 171 |
return nil, err |
| 172 |
} |
| 173 |
|
| 174 |
signers, err := a.Signers() |
| 175 |
if err != nil { |
| 176 |
return nil, errors.Wrap(err, "error listing signers") |
| 177 |
} |
| 178 |
|
| 179 |
keyBytes := key.Marshal() |
| 180 |
for _, sig := range signers { |
| 181 |
if bytes.Equal(keyBytes, sig.PublicKey().Marshal()) { |
| 182 |
return sig, nil |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
return nil, ErrNotFound |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
func (a *Agent) RemoveKeys(comment string, opts ...AgentOption) (bool, error) { |
| 191 |
o := newOptions(opts) |
| 192 |
keys, err := a.List() |
| 193 |
if err != nil { |
| 194 |
return false, errors.Wrap(err, "error listing keys") |
| 195 |
} |
| 196 |
|
| 197 |
var removed bool |
| 198 |
for _, key := range keys { |
| 199 |
if key.Comment == comment { |
| 200 |
if o.filterBySignatureKey == nil || o.filterBySignatureKey(key) { |
| 201 |
if err := a.Remove(key); err != nil { |
| 202 |
return false, errors.Wrap(err, "error removing key") |
| 203 |
} |
| 204 |
removed = true |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return removed, nil |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
func (a *Agent) AddCertificate(subject string, cert *ssh.Certificate, priv interface{}) error { |
| 214 |
var ( |
| 215 |
lifetime uint64 |
| 216 |
now = uint64(time.Now().Unix()) |
| 217 |
) |
| 218 |
if cert.ValidBefore == ssh.CertTimeInfinity { |
| 219 |
|
| 220 |
lifetime = 0 |
| 221 |
} else if cert.ValidBefore <= now { |
| 222 |
return errors.New("error adding certificate to ssh agent - certificate is already expired") |
| 223 |
} else { |
| 224 |
lifetime = cert.ValidBefore - now |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
if runtime.GOOS == "windows" { |
| 229 |
lifetime = 0 |
| 230 |
} |
| 231 |
|
| 232 |
return errors.Wrap(a.Add(agent.AddedKey{ |
| 233 |
PrivateKey: priv, |
| 234 |
Certificate: cert, |
| 235 |
Comment: subject, |
| 236 |
LifetimeSecs: uint32(lifetime), |
| 237 |
}), "error adding key to agent") |
| 238 |
} |