Skip to content

Commit 8df2c52

Browse files
committed
Test parsing of padded certificate
1 parent 4a67944 commit 8df2c52

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

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

+27
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package org.bouncycastle.openpgp;
22

3+
import java.io.ByteArrayOutputStream;
34
import java.io.IOException;
5+
import java.io.OutputStream;
46
import java.security.SecureRandom;
57

68
import org.bouncycastle.bcpg.BCPGInputStream;
9+
import org.bouncycastle.bcpg.BCPGOutputStream;
710
import org.bouncycastle.bcpg.Packet;
11+
import org.bouncycastle.bcpg.PacketFormat;
812
import org.bouncycastle.bcpg.PaddingPacket;
913
import org.bouncycastle.crypto.CryptoServicesRegistrar;
1014

@@ -102,4 +106,27 @@ public byte[] getPadding()
102106
{
103107
return p.getPadding();
104108
}
109+
110+
public void encode(OutputStream outStream)
111+
throws IOException
112+
{
113+
BCPGOutputStream pOut = BCPGOutputStream.wrap(outStream);
114+
p.encode(pOut);
115+
}
116+
117+
public byte[] getEncoded()
118+
throws IOException
119+
{
120+
return getEncoded(PacketFormat.ROUNDTRIP);
121+
}
122+
123+
public byte[] getEncoded(PacketFormat format)
124+
throws IOException
125+
{
126+
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
127+
BCPGOutputStream pOut = new BCPGOutputStream(bOut, format);
128+
encode(pOut);
129+
pOut.close();
130+
return bOut.toByteArray();
131+
}
105132
}

pg/src/test/java/org/bouncycastle/openpgp/test/PGPPaddingTest.java

+71
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
11
package org.bouncycastle.openpgp.test;
22

3+
import org.bouncycastle.bcpg.ArmoredInputStream;
4+
import org.bouncycastle.bcpg.ArmoredOutputStream;
5+
import org.bouncycastle.bcpg.BCPGInputStream;
6+
import org.bouncycastle.bcpg.BCPGOutputStream;
7+
import org.bouncycastle.bcpg.HashAlgorithmTags;
8+
import org.bouncycastle.bcpg.PacketFormat;
9+
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
10+
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
11+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
12+
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
13+
import org.bouncycastle.crypto.generators.X25519KeyPairGenerator;
14+
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
15+
import org.bouncycastle.crypto.params.X25519KeyGenerationParameters;
16+
import org.bouncycastle.openpgp.PGPException;
17+
import org.bouncycastle.openpgp.PGPKeyPair;
318
import org.bouncycastle.openpgp.PGPPadding;
19+
import org.bouncycastle.openpgp.PGPPublicKeyRing;
20+
import org.bouncycastle.openpgp.PGPSecretKey;
21+
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
22+
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
23+
import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider;
24+
import org.bouncycastle.openpgp.operator.bc.BcPGPKeyPair;
425
import org.bouncycastle.util.test.SimpleTest;
526

27+
import java.io.ByteArrayInputStream;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
30+
import java.util.Arrays;
31+
import java.util.Date;
32+
633
public class PGPPaddingTest
734
extends SimpleTest
835
{
@@ -20,6 +47,8 @@ public void performTest()
2047
fixedLenPaddingIsCorrectLength();
2148
negativePaddingLengthThrows();
2249
zeroPaddingLengthThrows();
50+
51+
parsePaddedCertificate();
2352
}
2453

2554
private void randomPaddingIsInBounds()
@@ -50,6 +79,48 @@ private void zeroPaddingLengthThrows()
5079
testException(null, "IllegalArgumentException", () -> new PGPPadding(0));
5180
}
5281

82+
private void parsePaddedCertificate()
83+
throws PGPException, IOException
84+
{
85+
PGPDigestCalculator digestCalc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1);
86+
87+
Date creationTime = new Date(1000 * (new Date().getTime() / 1000));
88+
Ed25519KeyPairGenerator edGen = new Ed25519KeyPairGenerator();
89+
edGen.init(new Ed25519KeyGenerationParameters(CryptoServicesRegistrar.getSecureRandom()));
90+
AsymmetricCipherKeyPair edPair = edGen.generateKeyPair();
91+
92+
X25519KeyPairGenerator xGen = new X25519KeyPairGenerator();
93+
xGen.init(new X25519KeyGenerationParameters(CryptoServicesRegistrar.getSecureRandom()));
94+
AsymmetricCipherKeyPair xPair = xGen.generateKeyPair();
95+
96+
PGPKeyPair primayKeyPair = new BcPGPKeyPair(PublicKeyAlgorithmTags.Ed25519, edPair, creationTime);
97+
PGPKeyPair subKeyPair = new BcPGPKeyPair(PublicKeyAlgorithmTags.X25519, xPair, creationTime);
98+
99+
PGPSecretKey secretPrimaryKey = new PGPSecretKey(primayKeyPair.getPrivateKey(), primayKeyPair.getPublicKey(), digestCalc, true, null);
100+
PGPSecretKey secretSubKey = new PGPSecretKey(subKeyPair.getPrivateKey(), subKeyPair.getPublicKey(), digestCalc, false, null);
101+
102+
PGPPublicKeyRing certificate = new PGPPublicKeyRing(Arrays.asList(secretPrimaryKey.getPublicKey(), secretSubKey.getPublicKey()));
103+
PGPPadding padding = new PGPPadding();
104+
105+
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
106+
ArmoredOutputStream aOut = ArmoredOutputStream.builder().clearHeaders().build(bOut);
107+
BCPGOutputStream pOut = new BCPGOutputStream(aOut, PacketFormat.CURRENT);
108+
certificate.encode(pOut);
109+
padding.encode(pOut);
110+
111+
pOut.close();
112+
aOut.close();
113+
114+
ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());
115+
ArmoredInputStream aIn = new ArmoredInputStream(bIn);
116+
BCPGInputStream pIn = new BCPGInputStream(aIn);
117+
118+
PGPPublicKeyRing parsed = new PGPPublicKeyRing(pIn, new BcKeyFingerprintCalculator());
119+
isTrue(org.bouncycastle.util.Arrays.areEqual(
120+
certificate.getEncoded(PacketFormat.CURRENT),
121+
parsed.getEncoded(PacketFormat.CURRENT)));
122+
}
123+
53124
public static void main(String[] args)
54125
{
55126
runTest(new PGPPaddingTest());

0 commit comments

Comments
 (0)