traygent/privkey.go
Aaron Bieber 26d804f117
add the ability to turn off forced expiration
- while here, add the ability to set the forced expiration time
2024-06-19 19:55:38 -06:00

70 lines
1.2 KiB
Go

package main
import (
"fmt"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
type privKey struct {
signer ssh.Signer
comment string
expireTime *time.Time
lifetime uint32
pubKey ssh.PublicKey
fingerPrint string
usage uint32
}
func (p *privKey) String() string {
pk := p.signer.PublicKey()
return fmt.Sprintf("%s %s %s %s",
pk.Type(),
p.fingerPrint,
p.comment,
p.expireTime.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, force bool, duration int) {
exp := key.LifetimeSecs
if force && exp <= 0 {
exp = uint32(duration)
}
t := time.Now().Add(time.Duration(exp) * time.Second)
key.LifetimeSecs = exp
p.lifetime = key.LifetimeSecs
if exp > 0 {
p.expireTime = &t
}
}
func NewPrivKey(signer ssh.Signer, key agent.AddedKey, force bool, duration int) privKey {
pub := signer.PublicKey()
pk := privKey{
signer: signer,
comment: key.Comment,
pubKey: pub,
fingerPrint: ssh.FingerprintSHA256(pub),
}
pk.setExpire(key, force, duration)
return pk
}