8000 Signature Packet format fixes #2078 · bcgit/bc-java@5d5c196 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d5c196

Browse files
ligefeiBouncycastledghgit
authored andcommitted
Signature Packet format fixes #2078
1 parent e855013 commit 5d5c196

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

pg/src/main/java/org/bouncycastle/bcpg/SignaturePacket.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,22 @@ public SignaturePacket(
353353
byte[] fingerPrint,
354354
MPInteger[] signature)
355355
{
356-
super(SIGNATURE);
356+
this(version, false, signatureType, keyID, keyAlgorithm, hashAlgorithm, hashedData, unhashedData, fingerPrint, signature);
357+
}
358+
359+
public SignaturePacket(
360+
int version,
361+
boolean hasNewPacketFormat,
362+
int signatureType,
363+
long keyID,
364+
int keyAlgorithm,
365+
int hashAlgorithm,
366+
SignatureSubpacket[] hashedData,
367+
SignatureSubpacket[] unhashedData,
368+
byte[] fingerPrint,
369+
MPInteger[] signature)
370+
{
371+
super(SIGNATURE, hasNewPacketFormat);
357372

358373
this.version = version;
359374
this.signatureType = signatureType;
@@ -383,7 +398,7 @@ public SignaturePacket(
383398
byte[] signatureEncoding,
384399
byte[] salt)
385400
{
386-
super(SIGNATURE);
401+
super(SIGNATURE, true);
387402

388403
this.version = version;
389404
this.signatureType = signatureType;
@@ -413,7 +428,7 @@ public SignaturePacket(
413428
MPInteger[] signature,
414429
byte[] salt)
415430
{
416-
super(SIGNATURE);
431+
super(SIGNATURE, true);
417432

418433
this.version = version;
419434
this.signatureType = signatureType;

pg/src/main/java/org/bouncycastle/openpgp/PGPSignature.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public class PGPSignature
198198
*/
199199
public static final int THIRD_PARTY_CONFIRMATION = 0x50;
200200

201-
private final SignaturePacket sigPck;
201+
final SignaturePacket sigPck;
202202
private final TrustPacket trustPck;
203203

204204
private volatile PGPContentVerifier verifier;
@@ -1034,6 +1034,8 @@ public static PGPSignature join(PGPSignature sig1, PGPSignature sig2)
10341034
SignatureSubpacket[] unhashed = (SignatureSubpacket[])merged.toArray(new SignatureSubpacket[0]);
10351035
return new PGPSignature(
10361036
new SignaturePacket(
1037+
sig1.getVersion(),
1038+
sig1.sigPck.hasNewPacketFormat(),
10371039
sig1.getSignatureType(),
10381040
sig1.getKeyID(),
10391041
sig1.getKeyAlgorithm(),

pg/src/main/java/org/bouncycastle/openpgp/PGPSignatureSubpacketGenerator.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package org.bouncycastle.openpgp;
22

3+
import java.io.ByteArrayOutputStream;
34
import java.io.IOException;
45
import java.util.ArrayList;
56
import java.util.Arrays;
67
import java.util.Date;
78
import java.util.List;
89

10+
import org.bouncycastle.bcpg.BCPGOutputStream;
11+
import org.bouncycastle.bcpg.PacketFormat;
912
import org.bouncycastle.bcpg.SignatureSubpacket;
1013
import org.bouncycastle.bcpg.SignatureSubpacketTags;
1114
import org.bouncycastle.bcpg.sig.EmbeddedSignature;
@@ -445,9 +448,16 @@ public void setEmbeddedSignature(boolean isCritical, PGPSignature pgpSignature)
445448
public void addEmbeddedSignature(boolean isCritical, PGPSignature pgpSignature)
446449
throws IOException
447450
{
448-
byte[] sig = pgpSignature.getEncoded();
451+
// Encode the signature forcing legacy packet format, such that we consistently cut off the proper amount
452+
// of header bytes
453+
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
454+
BCPGOutputStream pOut = new BCPGOutputStream(bOut, PacketFormat.LEGACY);
455+
pgpSignature.encode(pOut);
456+
pOut.close();
457+
byte[] sig = bOut.toByteArray();
449458
byte[] data;
450459

460+
// Cut off the header bytes
451461
if (sig.length - 1 > 256)
452462
{
453463
data = new byte[sig.length - 3];

0 commit comments

Comments
 (0)
0