Skip to content

Commit 8807c5e

Browse files
committed
fix: account for time truncation to a second resolution
ProtonMail go-crypto library introduced a breaking change in `packet.PublicKey.KeyExpired` method. Account for it. Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
1 parent 1b35ea8 commit 8807c5e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pkg/pgp/key_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ func TestKeyValidation(t *testing.T) {
170170
pgp.WithValidEmailAsName(false),
171171
},
172172
},
173+
{
174+
name: "should be ok",
175+
email: "keytest@example.com",
176+
lifetime: pgp.DefaultMaxAllowedLifetime,
177+
},
178+
{
179+
name: "should be ok (with time truncation)",
180+
email: "keytest@example.com",
181+
lifetime: pgp.DefaultMaxAllowedLifetime + time.Minute - time.Nanosecond,
182+
},
173183
} {
174184
t.Run(tt.name, func(t *testing.T) {
175185
key := genKey(t, uint32(tt.lifetime/time.Second), tt.email, func() time.Time {

pkg/pgp/validate.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ func (p *Key) validateLifetime(opts *validationOptions) error {
100100
return fmt.Errorf("key does not contain a valid key lifetime")
101101
}
102102

103-
expiration := time.Now().Add(opts.maxAllowedLifetime)
103+
// We don't care when the key was created, only when it expires relative to the server "now" time.
104+
//
105+
// Also add one minute to account for rounding errors or time skew.
106+
expiration := time.Now().Add(opts.maxAllowedLifetime + time.Minute)
104107

105108
if !entity.PrimaryKey.KeyExpired(sig, expiration) {
106109
return fmt.Errorf("key lifetime is too long: %s", time.Duration(*sig.KeyLifetimeSecs)*time.Second)

0 commit comments

Comments
 (0)