|
1 | 1 | package org.bouncycastle.openpgp.api.test;
|
2 | 2 |
|
| 3 | +import org.bouncycastle.bcpg.PublicKeyAlgorithmTags; |
3 | 4 | import org.bouncycastle.bcpg.PublicKeyPacket;
|
4 | 5 | import org.bouncycastle.bcpg.PublicKeyUtils;
|
5 | 6 | import org.bouncycastle.bcpg.SecretKeyPacket;
|
@@ -69,14 +70,15 @@ public OpenPGPV6KeyGenerator getKeyGenerator(int signatureHashAlgorithm,
|
69 | 70 | private void performTests(APIProvider apiProvider)
|
70 | 71 | throws PGPException, IOException
|
71 | 72 | {
|
| 73 | + testGenerateCustomKey(apiProvider); |
| 74 | + |
72 | 75 | testGenerateSignOnlyKeyBaseCase(apiProvider);
|
73 | 76 | testGenerateAEADProtectedSignOnlyKey(apiProvider);
|
74 | 77 | testGenerateCFBProtectedSignOnlyKey(apiProvider);
|
75 | 78 |
|
76 | 79 | testGenerateClassicKeyBaseCase(apiProvider);
|
77 | 80 | testGenerateProtectedTypicalKey(apiProvider);
|
78 | 81 |
|
79 |
| - testGenerateCustomKey(apiProvider); |
80 | 82 | }
|
81 | 83 |
|
82 | 84 | private void testGenerateSignOnlyKeyBaseCase(APIProvider apiProvider)
|
@@ -268,6 +270,68 @@ private void testGenerateCustomKey(APIProvider apiProvider)
|
268 | 270 | .addEncryptionSubkey(PGPKeyPairGenerator::generateX448KeyPair,
|
269 | 271 | "encryption-key-passphrase".toCharArray())
|
270 | 272 | .build();
|
| 273 | + |
| 274 | + Iterator<PGPSecretKey> keyIt = secretKey.getSecretKeys(); |
| 275 | + PGPSecretKey primaryKey = keyIt.next(); |
| 276 | + isEquals("Primary key MUST be RSA_GENERAL", |
| 277 | + PublicKeyAlgorithmTags.RSA_GENERAL, primaryKey.getPublicKey().getAlgorithm()); |
| 278 | + isEquals("Primary key MUST be 4096 bits", 4096, primaryKey.getPublicKey().getBitStrength()); |
| 279 | + isEquals("Primary key creation time mismatch", |
| 280 | + creationTime, primaryKey.getPublicKey().getCreationTime()); |
| 281 | + PGPSignature directKeySig = primaryKey.getPublicKey().getKeySignatures().next(); |
| 282 | + PGPSignatureSubpacketVector hashedSubpackets = directKeySig.getHashedSubPackets(); |
| 283 | + isEquals("Primary key key flags mismatch", |
| 284 | + KeyFlags.CERTIFY_OTHER, hashedSubpackets.getKeyFlags()); |
| 285 | + isEquals("Primary key features mismatch", |
| 286 | + Features.FEATURE_SEIPD_V2, hashedSubpackets.getFeatures().getFeatures()); |
| 287 | + isEquals("Primary key sig notation data mismatch", |
| 288 | + "CYBER", |
| 289 | + hashedSubpackets. getNotationDataOccurrences( "[email protected]")[ 0]. getNotationValue()); |
| 290 | + |
| 291 | + Iterator<String> uids = primaryKey.getUserIDs(); |
| 292 | + String uid = uids.next(); |
| 293 | + isFalse("Unexpected additional UID", uids.hasNext()); |
| 294 | + PGPSignature uidSig = primaryKey.getPublicKey().getSignaturesForID(uid).next(); |
| 295 | + isEquals("UID binding sig type mismatch", |
| 296 | + PGPSignature.DEFAULT_CERTIFICATION, uidSig.getSignatureType()); |
| 297 | + |
| 298 | + PGPSecretKey signingSubkey = keyIt.next(); |
| 299 | + isEquals("Subkey MUST be Ed448", |
| 300 | + PublicKeyAlgorithmTags.Ed448, signingSubkey.getPublicKey().getAlgorithm()); |
| 301 | + isEquals("Subkey creation time mismatch", |
| 302 | + creationTime, signingSubkey.getPublicKey().getCreationTime()); |
| 303 | + PGPSignature sigSubBinding = signingSubkey.getPublicKey().getKeySignatures().next(); |
| 304 | + PGPSignatureSubpacketVector sigSubBindHashPkts = sigSubBinding.getHashedSubPackets(); |
| 305 | + isEquals("Encryption subkey key flags mismatch", |
| 306 | + KeyFlags.SIGN_DATA, sigSubBindHashPkts.getKeyFlags()); |
| 307 | + isEquals("Subkey notation data mismatch", |
| 308 | + "ZAUBER", |
| 309 | + sigSubBindHashPkts. getNotationDataOccurrences( "[email protected]")[ 0]. getNotationValue()); |
| 310 | + isFalse("Missing embedded primary key binding signature", |
| 311 | + sigSubBindHashPkts.getEmbeddedSignatures().isEmpty()); |
| 312 | + |
| 313 | + PGPSecretKey encryptionSubkey = keyIt.next(); |
| 314 | + isFalse("Unexpected additional subkey", keyIt.hasNext()); |
| 315 | + isEquals("Subkey MUST be X448", |
| 316 | + PublicKeyAlgorithmTags.X448, encryptionSubkey.getPublicKey().getAlgorithm()); |
| 317 | + isEquals("Subkey creation time mismatch", |
| 318 | + creationTime, encryptionSubkey.getPublicKey().getCreationTime()); |
| 319 | + PGPSignature encryptionBinding = encryptionSubkey.getPublicKey().getKeySignatures().next(); |
| 320 | + PGPSignatureSubpacketVector encBindHashPkts = encryptionBinding.getHashedSubPackets(); |
| 321 | + isEquals("Encryption subkey key flags mismatch", |
| 322 | + KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE, encBindHashPkts.getKeyFlags()); |
| 323 | + isTrue("Unexpected embedded primary key binding signature in encryption subkey binding", |
| 324 | + encBindHashPkts.getEmbeddedSignatures().isEmpty()); |
| 325 | + |
| 326 | + BcPBESecretKeyDecryptorBuilder keyDecryptorBuilder = new BcPBESecretKeyDecryptorBuilder( |
| 327 | + new BcPGPDigestCalculatorProvider()); |
| 328 | + |
| 329 | + isNotNull("Could not decrypt primary key using correct passphrase", |
| 330 | + primaryKey.extractPrivateKey(keyDecryptorBuilder.build("primary-key-passphrase".toCharArray()))); |
| 331 | + isNotNull("Could not decrypt signing subkey using correct passphrase", |
| 332 | + signingSubkey.extractPrivateKey(keyDecryptorBuilder.build("signing-key-passphrase".toCharArray()))); |
| 333 | + isNotNull("Could not decrypt encryption subkey using correct passphrase", |
| 334 | + encryptionSubkey.extractPrivateKey(keyDecryptorBuilder.build("encryption-key-passphrase".toCharArray()))); |
271 | 335 | }
|
272 | 336 |
|
273 | 337 | private abstract static class APIProvider
|
|
0 commit comments