|
1 | 1 | package org.bouncycastle.openpgp.test;
|
2 | 2 |
|
3 | 3 | import java.io.ByteArrayInputStream;
|
| 4 | +import java.io.ByteArrayOutputStream; |
4 | 5 | import java.io.IOException;
|
| 6 | +import java.math.BigInteger; |
5 | 7 | import java.nio.charset.StandardCharsets;
|
| 8 | +import java.util.Collections; |
6 | 9 | import java.util.Date;
|
7 | 10 | import java.util.Iterator;
|
8 | 11 |
|
9 | 12 | import org.bouncycastle.bcpg.AEADAlgorithmTags;
|
10 | 13 | import org.bouncycastle.bcpg.ArmoredInputStream;
|
| 14 | +import org.bouncycastle.bcpg.ArmoredOutputStream; |
11 | 15 | import org.bouncycastle.bcpg.BCPGInputStream;
|
| 16 | +import org.bouncycastle.bcpg.BCPGOutputStream; |
| 17 | +import org.bouncycastle.bcpg.HashAlgorithmTags; |
| 18 | +import org.bouncycastle.bcpg.PacketFormat; |
12 | 19 | import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
|
13 | 20 | import org.bouncycastle.bcpg.PublicKeyPacket;
|
14 | 21 | import org.bouncycastle.bcpg.SecretKeyPacket;
|
15 | 22 | 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; |
16 | 29 | import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
17 | 30 | import org.bouncycastle.openpgp.PGPException;
|
| 31 | +import org.bouncycastle.openpgp.PGPKeyPair; |
18 | 32 | import org.bouncycastle.openpgp.PGPPublicKey;
|
19 | 33 | import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
20 | 34 | import org.bouncycastle.openpgp.PGPSecretKey;
|
21 | 35 | import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
| 36 | +import org.bouncycastle.openpgp.PGPSignature; |
| 37 | +import org.bouncycastle.openpgp.PGPSignatureGenerator; |
| 38 | +import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator; |
22 | 39 | import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
|
23 | 40 | 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; |
24 | 44 | import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
|
25 | 45 | import org.bouncycastle.util.encoders.Hex;
|
26 | 46 |
|
@@ -94,6 +114,99 @@ public void performTest()
|
94 | 114 | parseUnprotectedKeyTest();
|
95 | 115 | testJcaFingerprintCalculation();
|
96 | 116 | 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); |
97 | 210 | }
|
98 | 211 |
|
99 | 212 | private void parseUnprotectedCertTest()
|
|
0 commit comments