Skip to content

Commit 31f8b48

Browse files
committed
Fix and move Signature.KeyExpired to PublicKey.KeyExpired; add Signature.SigExpired
1 parent 3431f2e commit 31f8b48

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

openpgp/keys.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (e *Entity) encryptionKey(now time.Time) (Key, bool) {
9696
if subkey.Sig.FlagsValid &&
9797
subkey.Sig.FlagEncryptCommunications &&
9898
subkey.PublicKey.PubKeyAlgo.CanEncrypt() &&
99-
!subkey.Sig.KeyExpired(now) &&
99+
!subkey.PublicKey.KeyExpired(subkey.Sig, now) &&
100100
(maxTime.IsZero() || subkey.Sig.CreationTime.After(maxTime)) {
101101
candidateSubkey = i
102102
maxTime = subkey.Sig.CreationTime
@@ -115,7 +115,7 @@ func (e *Entity) encryptionKey(now time.Time) (Key, bool) {
115115
i := e.primaryIdentity()
116116
if !i.SelfSignature.FlagsValid || i.SelfSignature.FlagEncryptCommunications &&
117117
e.PrimaryKey.PubKeyAlgo.CanEncrypt() &&
118-
!i.SelfSignature.KeyExpired(now) {
118+
!e.PrimaryKey.KeyExpired(i.SelfSignature, now) {
119119
return Key{e, e.PrimaryKey, e.PrivateKey, i.SelfSignature}, true
120120
}
121121

@@ -132,7 +132,7 @@ func (e *Entity) signingKey(now time.Time) (Key, bool) {
132132
if subkey.Sig.FlagsValid &&
133133
subkey.Sig.FlagSign &&
134134
subkey.PublicKey.PubKeyAlgo.CanSign() &&
135-
!subkey.Sig.KeyExpired(now) {
135+
!subkey.PublicKey.KeyExpired(subkey.Sig, now) {
136136
candidateSubkey = i
137137
break
138138
}
@@ -147,7 +147,7 @@ func (e *Entity) signingKey(now time.Time) (Key, bool) {
147147
// with the primary key.
148148
i := e.primaryIdentity()
149149
if !i.SelfSignature.FlagsValid || i.SelfSignature.FlagSign &&
150-
!i.SelfSignature.KeyExpired(now) {
150+
!e.PrimaryKey.KeyExpired(i.SelfSignature, now) {
151151
return Key{e, e.PrimaryKey, e.PrivateKey, i.SelfSignature}, true
152152
}
153153

openpgp/packet/public_key.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,3 +814,16 @@ func (pk *PublicKey) BitLength() (bitLength uint16, err error) {
814814
}
815815
return
816816
}
817+
818+
// KeyExpired returns whether sig is a self-signature of a key that has
819+
// expired or is created in the future.
820+
func (pk *PublicKey) KeyExpired(sig *Signature, currentTime time.Time) bool {
821+
if pk.CreationTime.After(currentTime) {
822+
return true
823+
}
824+
if sig.KeyLifetimeSecs == nil {
825+
return false
826+
}
827+
expiry := pk.CreationTime.Add(time.Duration(*sig.KeyLifetimeSecs) * time.Second)
828+
return currentTime.After(expiry)
829+
}

openpgp/packet/signature.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,16 +473,16 @@ func serializeSubpackets(to []byte, subpackets []outputSubpacket, hashed bool) {
473473
return
474474
}
475475

476-
// KeyExpired returns whether sig is a self-signature of a key that has
477-
// expired or is signed in the future.
478-
func (sig *Signature) KeyExpired(currentTime time.Time) bool {
476+
// SigExpired returns whether sig is a signature that has expired or is created
477+
// in the future.
478+
func (sig *Signature) SigExpired(currentTime time.Time) bool {
479479
if sig.CreationTime.After(currentTime) {
480480
return true
481481
}
482-
if sig.KeyLifetimeSecs == nil {
482+
if sig.SigLifetimeSecs == nil {
483483
return false
484484
}
485-
expiry := sig.CreationTime.Add(time.Duration(*sig.KeyLifetimeSecs) * time.Second)
485+
expiry := sig.CreationTime.Add(time.Duration(*sig.SigLifetimeSecs) * time.Second)
486486
return currentTime.After(expiry)
487487
}
488488

openpgp/read.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func (scr *signatureCheckReader) Read(buf []byte) (n int, err error) {
338338
var ok bool
339339
if scr.md.Signature, ok = p.(*packet.Signature); ok {
340340
scr.md.SignatureError = scr.md.SignedBy.PublicKey.VerifySignature(scr.h, scr.md.Signature)
341-
if scr.md.SignatureError == nil && scr.md.Signature.KeyExpired(scr.config.Now()) {
341+
if scr.md.SignatureError == nil && scr.md.Signature.SigExpired(scr.config.Now()) {
342342
scr.md.SignatureError = errors.ErrSignatureExpired
343343
}
344344
} else if scr.md.SignatureV3, ok = p.(*packet.SignatureV3); ok {
@@ -426,7 +426,7 @@ func CheckDetachedSignatureWithHash(keyring KeyRing, signed, signature io.Reader
426426
switch sig := p.(type) {
427427
case *packet.Signature:
428428
err = key.PublicKey.VerifySignature(h, sig)
429-
if err == nil && sig.KeyExpired(config.Now()) {
429+
if err == nil && sig.SigExpired(config.Now()) {
430430
err = errors.ErrSignatureExpired
431431
}
432432
case *packet.SignatureV3:

0 commit comments

Comments
 (0)