Skip to content

Commit cc34b1f

Browse files
authored
Correctly serialize critical flag of signature subpackets (#83)
The signature subpackets are already setting the isCritical field to true for various signature subpacket types. This commit changes the serializeSubpackets function to take this field into account and set the critical bit accordingly.
1 parent c05353c commit cc34b1f

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

openpgp/packet/signature.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@ func serializeSubpackets(to []byte, subpackets []outputSubpacket, hashed bool) {
507507
if subpacket.hashed == hashed {
508508
n := serializeSubpacketLength(to, len(subpacket.contents)+1)
509509
to[n] = byte(subpacket.subpacketType)
510+
if subpacket.isCritical {
511+
to[n] |= 0x80
512+
}
510513
to = to[1+n:]
511514
n = copy(to, subpacket.contents)
512515
to = to[n:]

openpgp/packet/signature_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,56 @@ func TestSignUserId(t *testing.T) {
7676
}
7777
}
7878

79+
func TestSignatureWithLifetime(t *testing.T) {
80+
lifeTime := uint32(3600 * 24 * 30) // 30 days
81+
sig := &Signature{
82+
SigType: SigTypeGenericCert,
83+
PubKeyAlgo: PubKeyAlgoRSA,
84+
Hash: crypto.SHA256,
85+
SigLifetimeSecs: &lifeTime,
86+
}
87+
88+
packet, err := Read(readerFromHex(rsaPkDataHex))
89+
if err != nil {
90+
t.Fatalf("failed to deserialize public key: %v", err)
91+
}
92+
pubKey := packet.(*PublicKey)
93+
94+
packet, err = Read(readerFromHex(privKeyRSAHex))
95+
if err != nil {
96+
t.Fatalf("failed to deserialize private key: %v", err)
97+
}
98+
privKey := packet.(*PrivateKey)
99+
100+
err = privKey.Decrypt([]byte("testing"))
101+
if err != nil {
102+
t.Fatalf("failed to decrypt private key: %v", err)
103+
}
104+
105+
err = sig.SignUserId("", pubKey, privKey, nil)
106+
if err != nil {
107+
t.Errorf("failed to sign user id: %v", err)
108+
}
109+
110+
buf := bytes.NewBuffer([]byte{})
111+
err = sig.Serialize(buf)
112+
if err != nil {
113+
t.Errorf("failed to serialize signature: %v", err)
114+
}
115+
116+
packet, _ = Read(bytes.NewReader(buf.Bytes()))
117+
sig = packet.(*Signature)
118+
if sig.SigLifetimeSecs == nil || *sig.SigLifetimeSecs != lifeTime {
119+
t.Errorf("signature lifetime is wrong: %d instead of %d", *sig.SigLifetimeSecs, lifeTime)
120+
}
121+
122+
for _, subPacket := range sig.rawSubpackets {
123+
if subPacket.subpacketType == signatureExpirationSubpacket {
124+
if !subPacket.isCritical {
125+
t.Errorf("signature expiration subpacket is not marked as critical")
126+
}
127+
}
128+
}
129+
}
130+
79131
const signatureDataHex = "c2c05c04000102000605024cb45112000a0910ab105c91af38fb158f8d07ff5596ea368c5efe015bed6e78348c0f033c931d5f2ce5db54ce7f2a7e4b4ad64db758d65a7a71773edeab7ba2a9e0908e6a94a1175edd86c1d843279f045b021a6971a72702fcbd650efc393c5474d5b59a15f96d2eaad4c4c426797e0dcca2803ef41c6ff234d403eec38f31d610c344c06f2401c262f0993b2e66cad8a81ebc4322c723e0d4ba09fe917e8777658307ad8329adacba821420741009dfe87f007759f0982275d028a392c6ed983a0d846f890b36148c7358bdb8a516007fac760261ecd06076813831a36d0459075d1befa245ae7f7fb103d92ca759e9498fe60ef8078a39a3beda510deea251ea9f0a7f0df6ef42060f20780360686f3e400e"

0 commit comments

Comments
 (0)