Skip to content

Commit db9fcc9

Browse files
committed
Replace reason uint8 with packet.ReasonForRevocation enum
1 parent d0b6126 commit db9fcc9

File tree

3 files changed

+23
-34
lines changed

3 files changed

+23
-34
lines changed

openpgp/keys.go

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -634,36 +634,17 @@ func (e *Entity) SignIdentity(identity string, signer *Entity, config *packet.Co
634634
return nil
635635
}
636636

637-
// validRevocationReason reports whether the given revocation reason code is valid
638-
// as per RFC4880 section-5.2.3.23.
639-
func validRevocationReason(r uint8) bool {
640-
switch r {
641-
// Defined in RFC4880 section-5.2.3.23
642-
case 0, 1, 2, 3, 32:
643-
return true
644-
default:
645-
// Private use (RFC4880 section-5.2.3.23)
646-
if 100 <= r && r <= 110 {
647-
return true
648-
}
649-
return false
650-
}
651-
}
652-
653637
// RevokeKey generates a key revocation signature (packet.SigTypeKeyRevocation) with the
654638
// specified reason code and text (RFC4880 section-5.2.3.23).
655639
// If config is nil, sensible defaults will be used.
656-
func (e *Entity) RevokeKey(reason uint8, reasonText string, config *packet.Config) error {
657-
if !validRevocationReason(reason) {
658-
return errors.InvalidArgumentError("invalid reason code")
659-
}
660-
640+
func (e *Entity) RevokeKey(reason packet.ReasonForRevocation, reasonText string, config *packet.Config) error {
641+
reasonCode := uint8(reason)
661642
revSig := &packet.Signature{
662643
CreationTime: config.Now(),
663644
SigType: packet.SigTypeKeyRevocation,
664645
PubKeyAlgo: packet.PubKeyAlgoRSA,
665646
Hash: config.Hash(),
666-
RevocationReason: &reason,
647+
RevocationReason: &reasonCode,
667648
RevocationReasonText: reasonText,
668649
IssuerKeyId: &e.PrimaryKey.KeyId,
669650
}
@@ -678,21 +659,18 @@ func (e *Entity) RevokeKey(reason uint8, reasonText string, config *packet.Confi
678659
// RevokeSubkey generates a subkey revocation signature (packet.SigTypeSubkeyRevocation) for
679660
// a subkey with the specified reason code and text (RFC4880 section-5.2.3.23).
680661
// If config is nil, sensible defaults will be used.
681-
func (e *Entity) RevokeSubkey(sk *Subkey, reason uint8, reasonText string, config *packet.Config) error {
682-
if !validRevocationReason(reason) {
683-
return errors.InvalidArgumentError("invalid reason code")
684-
}
685-
662+
func (e *Entity) RevokeSubkey(sk *Subkey, reason packet.ReasonForRevocation, reasonText string, config *packet.Config) error {
686663
if err := e.PrimaryKey.VerifyKeySignature(sk.PublicKey, sk.Sig); err != nil {
687664
return errors.InvalidArgumentError("given subkey is not associated with this key")
688665
}
689666

667+
reasonCode := uint8(reason)
690668
revSig := &packet.Signature{
691669
CreationTime: config.Now(),
692670
SigType: packet.SigTypeSubkeyRevocation,
693671
PubKeyAlgo: packet.PubKeyAlgoRSA,
694672
Hash: config.Hash(),
695-
RevocationReason: &reason,
673+
RevocationReason: &reasonCode,
696674
RevocationReasonText: reasonText,
697675
IssuerKeyId: &e.PrimaryKey.KeyId,
698676
}

openpgp/keys_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ func TestRevokeKey(t *testing.T) {
749749
t.Fatal(err)
750750
}
751751

752-
err = entity.RevokeKey(0, "Key revocation", nil)
752+
err = entity.RevokeKey(packet.NoReason, "Key revocation", nil)
753753
if err != nil {
754754
t.Fatal(err)
755755
}
@@ -776,7 +776,7 @@ func TestRevokeKeyWithConfig(t *testing.T) {
776776
t.Fatal(err)
777777
}
778778

779-
err = entity.RevokeKey(0, "Key revocation", c)
779+
err = entity.RevokeKey(packet.NoReason, "Key revocation", c)
780780
if err != nil {
781781
t.Fatal(err)
782782
}
@@ -805,7 +805,7 @@ func TestRevokeSubkey(t *testing.T) {
805805
}
806806

807807
sk := &entity.Subkeys[0]
808-
err = entity.RevokeSubkey(sk, 0, "Key revocation", nil)
808+
err = entity.RevokeSubkey(sk, packet.NoReason, "Key revocation", nil)
809809
if err != nil {
810810
t.Fatal(err)
811811
}
@@ -852,7 +852,7 @@ func TestRevokeSubkeyWithAnotherEntity(t *testing.T) {
852852
t.Fatal(err)
853853
}
854854

855-
err = newEntity.RevokeSubkey(&sk, 0, "Key revocation", nil)
855+
err = newEntity.RevokeSubkey(&sk, packet.NoReason, "Key revocation", nil)
856856
if err == nil {
857857
t.Fatal("Entity was able to revoke a subkey owned by a different entity")
858858
}
@@ -867,7 +867,7 @@ func TestRevokeSubkeyWithInvalidSignature(t *testing.T) {
867867
sk := entity.Subkeys[0]
868868
sk.Sig = &packet.Signature{}
869869

870-
err = entity.RevokeSubkey(&sk, 0, "Key revocation", nil)
870+
err = entity.RevokeSubkey(&sk, packet.NoReason, "Key revocation", nil)
871871
if err == nil {
872872
t.Fatal("Entity was able to revoke a subkey with invalid signature")
873873
}
@@ -884,7 +884,7 @@ func TestRevokeSubkeyWithConfig(t *testing.T) {
884884
}
885885

886886
sk := entity.Subkeys[0]
887-
err = entity.RevokeSubkey(&sk, 0, "Key revocation", c)
887+
err = entity.RevokeSubkey(&sk, packet.NoReason, "Key revocation", c)
888888
if err != nil {
889889
t.Fatal(err)
890890
}

openpgp/packet/packet.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,3 +540,14 @@ func (mode AEADMode) TagLength() int {
540540
func (mode AEADMode) new(block cipher.Block) cipher.AEAD {
541541
return algorithm.AEADMode(mode).New(block)
542542
}
543+
544+
// ReasonForRevocation represents a revocation reason code as per RFC4880
545+
// section 5.2.3.23.
546+
type ReasonForRevocation uint8
547+
548+
const (
549+
NoReason ReasonForRevocation = 0
550+
KeySuperseded ReasonForRevocation = 1
551+
KeyCompromised ReasonForRevocation = 2
552+
KeyRetired ReasonForRevocation = 3
553+
)

0 commit comments

Comments
 (0)