Fix wrong GPG expire date ()

* fix expire of gpg relative to key creation https://tools.ietf.org/html/rfc4880#section-5.2.3.6

* add test
This commit is contained in:
Antoine GIRARD 2019-04-16 02:32:15 +02:00 committed by techknowlogick
parent ecfa5f1594
commit e98565ddc7
2 changed files with 162 additions and 6 deletions

View file

@ -232,10 +232,9 @@ func parseSubGPGKey(ownerID int64, primaryID string, pubkey *packet.PublicKey, e
}, nil
}
//parseGPGKey parse a PrimaryKey entity (primary key + subs keys + self-signature)
func parseGPGKey(ownerID int64, e *openpgp.Entity) (*GPGKey, error) {
pubkey := e.PrimaryKey
//getExpiryTime extract the expire time of primary key based on sig
func getExpiryTime(e *openpgp.Entity) time.Time {
expiry := time.Time{}
//Extract self-sign for expire date based on : https://github.com/golang/crypto/blob/master/openpgp/keys.go#L165
var selfSig *packet.Signature
for _, ident := range e.Identities {
@ -246,10 +245,16 @@ func parseGPGKey(ownerID int64, e *openpgp.Entity) (*GPGKey, error) {
break
}
}
expiry := time.Time{}
if selfSig.KeyLifetimeSecs != nil {
expiry = selfSig.CreationTime.Add(time.Duration(*selfSig.KeyLifetimeSecs) * time.Second)
expiry = e.PrimaryKey.CreationTime.Add(time.Duration(*selfSig.KeyLifetimeSecs) * time.Second)
}
return expiry
}
//parseGPGKey parse a PrimaryKey entity (primary key + subs keys + self-signature)
func parseGPGKey(ownerID int64, e *openpgp.Entity) (*GPGKey, error) {
pubkey := e.PrimaryKey
expiry := getExpiryTime(e)
//Parse Subkeys
subkeys := make([]*GPGKey, len(e.Subkeys))

View file

@ -6,6 +6,7 @@ package models
import (
"testing"
"time"
"code.gitea.io/gitea/modules/util"
@ -225,3 +226,153 @@ Q0KHb+QcycSgbDx0ZAvdIacuKvBBcbxrsmFUI4LR+oIup0G9gUc0roPvr014jYQL
assert.Equal(t, "user1@example.com", key.Emails[0].Email)
}
}
func TestCheckGParseGPGExpire(t *testing.T) {
testIssue6599 := `-----BEGIN PGP PUBLIC KEY BLOCK-----
mQINBFlFJRsBEAClNcRT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H1uPQ7PyWNTf1ufd9bDSDtK8epIcDPe
abOuphxQbrMVP4JJsBXnVW5raZO7s5lmSA8Ovce//+xJSAq9u0GTsGu1hWDe60ro
uOZwqjo/cU5G4y7WHRaC3oshH+DO8ajdXDogoDVs8DzYkTfWND2DDNEVhVrn7lGf
a4739sFIDagtBq6RzJGL0X82eJZzXPFiYvmy0OVbNDUgH+Drva/wRv/tN8RvBiS6
bsn8+GBGaU5RASu67UbqxHiytFnN4OnADA5ZHcwQbMgRHHiiMMIf+tJWH/pFMp00
epiDVQ==
=VSKJ
-----END PGP PUBLIC KEY BLOCK-----
`
ekey, err := checkArmoredGPGKeyString(testIssue6599)
assert.NoError(t, err)
expire := getExpiryTime(ekey)
assert.Equal(t, time.Unix(1586105389, 0), expire)
}