1hub / springburg

OpenPGP for .NET
MIT License
4 stars 0 forks source link

Rewrapping of V3 keys is broken #18

Closed filipnavara closed 3 years ago

filipnavara commented 3 years ago

The secret packet is incorrectly encoded and the unit test doesn't actually test the encoding.

A fix in SecretKeyPacket.Encode would likely look like this:

        public override void Encode(Stream bcpgOut)
        {
            pubKeyPacket.Encode(bcpgOut);

            if (s2k != null)
            {
                bcpgOut.WriteByte((byte)s2kUsage);
                if (s2kUsage == S2kUsageTag.Checksum || s2kUsage == S2kUsageTag.Sha1)
                {
                    bcpgOut.WriteByte((byte)encAlgorithm);
                    s2k.Encode(bcpgOut);
                }
            }
            else
            {
                bcpgOut.WriteByte((byte)encAlgorithm);
            }

            if (iv != null)
            {
                bcpgOut.Write(iv);
            }

            if (secKeyData != null && secKeyData.Length > 0)
            {
                bcpgOut.Write(secKeyData);
            }
        }