Skip to content

Commit 3d0540d

Browse files
committed
Add low-level v6 key generation test
1 parent d626f37 commit 3d0540d

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

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

+113
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
11
package org.bouncycastle.openpgp.test;
22

33
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
45
import java.io.IOException;
6+
import java.math.BigInteger;
57
import java.nio.charset.StandardCharsets;
8+
import java.util.Collections;
69
import java.util.Date;
710
import java.util.Iterator;
811

912
import org.bouncycastle.bcpg.AEADAlgorithmTags;
1013
import org.bouncycastle.bcpg.ArmoredInputStream;
14+
import org.bouncycastle.bcpg.ArmoredOutputStream;
1115
import org.bouncycastle.bcpg.BCPGInputStream;
16+
import org.bouncycastle.bcpg.BCPGOutputStream;
17+
import org.bouncycastle.bcpg.HashAlgorithmTags;
18+
import org.bouncycastle.bcpg.PacketFormat;
1219
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
1320
import org.bouncycastle.bcpg.PublicKeyPacket;
1421
import org.bouncycastle.bcpg.SecretKeyPacket;
1522
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
23+
import org.bouncycastle.bcpg.sig.Features;
24+
import org.bouncycastle.bcpg.sig.PreferredAEADCiphersuites;
25+
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
26+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
27+
import org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
28+
import org.bouncycastle.crypto.params.RSAKeyGenerationParameters;
1629
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1730
import org.bouncycastle.openpgp.PGPException;
31+
import org.bouncycastle.openpgp.PGPKeyPair;
1832
import org.bouncycastle.openpgp.PGPPublicKey;
1933
import org.bouncycastle.openpgp.PGPPublicKeyRing;
2034
import org.bouncycastle.openpgp.PGPSecretKey;
2135
import org.bouncycastle.openpgp.PGPSecretKeyRing;
36+
import org.bouncycastle.openpgp.PGPSignature;
37+
import org.bouncycastle.openpgp.PGPSignatureGenerator;
38+
import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
2239
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
2340
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
41+
import org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder;
42+
import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider;
43+
import org.bouncycastle.openpgp.operator.bc.BcPGPKeyPair;
2444
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
2545
import org.bouncycastle.util.encoders.Hex;
2646

@@ -94,6 +114,99 @@ public void performTest()
94114
parseUnprotectedKeyTest();
95115
testJcaFingerprintCalculation();
96116
parseProtectedKeyTest();
117+
118+
generatePlainV6RSAKey_bc();
119+
}
120+
121+
private void generatePlainV6RSAKey_bc()
122+
throws PGPException, IOException
123+
{
124+
String uid = "Alice <[email protected]>";
125+
Date creationTime = currentTimeRounded();
126+
RSAKeyPairGenerator rsaGen = new RSAKeyPairGenerator();
127+
rsaGen.init(new RSAKeyGenerationParameters(
128+
BigInteger.valueOf(0x10001),
129+
CryptoServicesRegistrar.getSecureRandom(),
130+
4096,
131+
100));
132+
AsymmetricCipherKeyPair rsaKp = rsaGen.generateKeyPair();
133+
134+
PGPKeyPair pgpKp = new BcPGPKeyPair(
135+
PublicKeyPacket.VERSION_6,
136+
PublicKeyAlgorithmTags.RSA_GENERAL,
137+
rsaKp,
138+
creationTime);
139+
PGPPublicKey primaryKey = pgpKp.getPublicKey();
140+
141+
PGPSignatureGenerator dkSigGen = new PGPSignatureGenerator(
142+
new BcPGPContentSignerBuilder(primaryKey.getAlgorithm(), HashAlgorithmTags.SHA3_512),
143+
primaryKey);
144+
dkSigGen.init(PGPSignature.DIRECT_KEY, pgpKp.getPrivateKey());
145+
PGPSignatureSubpacketGenerator hashed = new PGPSignatureSubpacketGenerator();
146+
hashed.setIssuerFingerprint(true, primaryKey);
147+
hashed.setSignatureCreationTime(true, creationTime);
148+
hashed.setFeature(false, (byte) (Features.FEATURE_MODIFICATION_DETECTION | Features.FEATURE_SEIPD_V2));
149+
hashed.setPreferredAEADCiphersuites(false, new PreferredAEADCiphersuites.Combination[]{
150+
new PreferredAEADCiphersuites.Combination(SymmetricKeyAlgorithmTags.AES_256, AEADAlgorithmTags.OCB),
151+
new PreferredAEADCiphersuites.Combination(SymmetricKeyAlgorithmTags.AES_192, AEADAlgorithmTags.OCB),
152+
new PreferredAEADCiphersuites.Combination(SymmetricKeyAlgorithmTags.AES_128, AEADAlgorithmTags.OCB)
153+
});
154+
hashed.setPreferredHashAlgorithms(false,
155+
new int[]
156+
{
157+
HashAlgorithmTags.SHA3_512, HashAlgorithmTags.SHA3_256,
158+
HashAlgorithmTags.SHA512, HashAlgorithmTags.SHA384, HashAlgorithmTags.SHA256
159+
}
160+
);
161+
hashed.setPreferredSymmetricAlgorithms(false,
162+
new int[]
163+
{
164+
SymmetricKeyAlgorithmTags.AES_256, SymmetricKeyAlgorithmTags.AES_192, SymmetricKeyAlgorithmTags.AES_128
165+
}
166+
);
167+
168+
dkSigGen.setHashedSubpackets(hashed.generate());
169+
PGPSignature dkSig = dkSigGen.generateCertification(primaryKey);
170+
171+
PGPSignatureGenerator uidSigGen = new PGPSignatureGenerator(
172+
new BcPGPContentSignerBuilder(primaryKey.getAlgorithm(), HashAlgorithmTags.SHA3_512),
173+
primaryKey);
174+
uidSigGen.init(PGPSignature.POSITIVE_CERTIFICATION, pgpKp.getPrivateKey());
175+
176+
hashed = new PGPSignatureSubpacketGenerator();
177+
hashed.setIssuerFingerprint(true, primaryKey);
178+
hashed.setSignatureCreationTime(true, creationTime);
179+
180+
PGPSignature uidSig = uidSigGen.generateCertification(uid, primaryKey);
181+
182+
primaryKey = PGPPublicKey.addCertification(primaryKey, dkSig);
183+
primaryKey = PGPPublicKey.addCertification(primaryKey, uid, uidSig);
184+
185+
PGPSecretKey primarySecKey = new PGPSecretKey(
186+
pgpKp.getPrivateKey(),
187+
primaryKey,
188+
new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1),
189+
true,
190+
null);
191+
192+
PGPPublicKeyRing certificate = new PGPPublicKeyRing(Collections.singletonList(primaryKey));
193+
PGPSecretKeyRing secretKey = new PGPSecretKeyRing(Collections.singletonList(primarySecKey));
194+
195+
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
196+
ArmoredOutputStream aOut = new ArmoredOutputStream(bOut);
197+
BCPGOutputStream pOut = new BCPGOutputStream(aOut, PacketFormat.CURRENT);
198+
certificate.encode(pOut);
199+
pOut.close();
200+
aOut.close();
201+
System.out.println(bOut);
202+
203+
bOut = new ByteArrayOutputStream();
204+
aOut = new ArmoredOutputStream(bOut);
205+
pOut = new BCPGOutputStream(aOut, PacketFormat.CURRENT);
206+
secretKey.encode(pOut);
207+
pOut.close();
208+
aOut.close();
209+
System.out.println(bOut);
97210
}
98211

99212
private void parseUnprotectedCertTest()

0 commit comments

Comments
 (0)