2023-11-09 15:43:14 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"golang.org/x/crypto/ssh/agent"
|
|
|
|
)
|
|
|
|
|
|
|
|
type privKey struct {
|
|
|
|
signer ssh.Signer
|
|
|
|
comment string
|
|
|
|
expire *time.Time
|
2023-11-12 19:11:55 -07:00
|
|
|
lifetime uint32
|
2023-11-09 15:43:14 -07:00
|
|
|
pubKey ssh.PublicKey
|
|
|
|
fingerPrint string
|
2023-11-12 19:11:55 -07:00
|
|
|
usage uint32
|
2023-11-09 15:43:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *privKey) String() string {
|
|
|
|
pk := p.signer.PublicKey()
|
|
|
|
return fmt.Sprintf("%s %s %s %s",
|
|
|
|
pk.Type(),
|
|
|
|
p.fingerPrint,
|
|
|
|
p.comment,
|
|
|
|
p.expire.Format(expFormat),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *privKey) GetType() string {
|
|
|
|
return p.pubKey.Type()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *privKey) GetSum() string {
|
|
|
|
return p.fingerPrint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *privKey) GetComment() string {
|
|
|
|
return p.comment
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *privKey) setExpire(key agent.AddedKey) {
|
|
|
|
exp := key.LifetimeSecs
|
|
|
|
if exp <= 0 {
|
|
|
|
exp = 300
|
|
|
|
}
|
|
|
|
|
|
|
|
t := time.Now().Add(time.Duration(exp) * time.Second)
|
2023-11-12 19:11:55 -07:00
|
|
|
key.LifetimeSecs = exp
|
|
|
|
p.lifetime = key.LifetimeSecs
|
2023-11-09 15:43:14 -07:00
|
|
|
p.expire = &t
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPrivKey(signer ssh.Signer, key agent.AddedKey) privKey {
|
|
|
|
pub := signer.PublicKey()
|
|
|
|
pk := privKey{
|
|
|
|
signer: signer,
|
|
|
|
comment: key.Comment,
|
|
|
|
pubKey: pub,
|
|
|
|
fingerPrint: ssh.FingerprintSHA256(pub),
|
|
|
|
}
|
|
|
|
pk.setExpire(key)
|
|
|
|
|
|
|
|
return pk
|
|
|
|
}
|