Skip to content

Commit ffd852d

Browse files
committed
Add tests for RSA key factory methods
1 parent 1d05a25 commit ffd852d

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

pg/src/test/java/org/bouncycastle/openpgp/api/test/OpenPGPV6KeyGeneratorTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ protected void performTestWith(OpenPGPApi api)
6060
testGenerateEd25519x25519Key(api);
6161
testGenerateEd448x448Key(api);
6262

63+
testGenerateSingletonRSAKey(api);
64+
testGenerateCompositeRSAKey(api);
65+
6366
testEnforcesPrimaryOrSubkeyType(api);
6467
testGenerateKeyWithoutSignatures(api);
6568
}
@@ -320,6 +323,59 @@ private void testGenerateEd448x448Key(OpenPGPApi api)
320323
isEquals(KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE, hashedSubpackets.getKeyFlags());
321324
}
322325

326+
private void testGenerateSingletonRSAKey(OpenPGPApi api)
327+
throws PGPException
328+
{
329+
Date creationTime = currentTimeRounded();
330+
OpenPGPKeyGenerator generator = api.generateKey(creationTime, false);
331+
332+
OpenPGPKey key = generator.singletonRSAKey(4096, "Alice <alice@example.com>")
333+
.build();
334+
335+
isEquals("Singleton RSA key MUST consist of only a single primary key.", 1, key.getKeys().size());
336+
OpenPGPCertificate.OpenPGPComponentKey primaryKey = key.getPrimaryKey();
337+
isEquals("Primary key MUST be an RSA key", PublicKeyAlgorithmTags.RSA_GENERAL, primaryKey.getAlgorithm());
338+
isEquals("Primary key MUST have a strength of 4096 bits.", 4096, primaryKey.getPGPPublicKey().getBitStrength());
339+
340+
isEquals("The primary key MUST be the certification key", primaryKey, key.getCertificationKeys().get(0));
341+
isEquals("The primary key MUST be the encryption key", primaryKey, key.getEncryptionKeys().get(0));
342+
isEquals("The primary key MUST be the signing key", primaryKey, key.getSigningKeys().get(0));
343+
344+
isNotNull(key.getUserId("Alice <alice@example.com>"));
345+
}
346+
347+
private void testGenerateCompositeRSAKey(OpenPGPApi api)
348+
throws PGPException
349+
{
350+
Date creationTime = currentTimeRounded();
351+
OpenPGPKeyGenerator generator = api.generateKey(creationTime, false);
352+
353+
OpenPGPKey key = generator.compositeRSAKey(4096, "Alice <alice@example.com>")
354+
.build();
355+
356+
isEquals("The composite RSA key MUST consist of 3 component keys", 3, key.getKeys().size());
357+
358+
OpenPGPCertificate.OpenPGPComponentKey primaryKey = key.getPrimaryKey();
359+
isEquals("Primary key MUST be an RSA key", PublicKeyAlgorithmTags.RSA_GENERAL, primaryKey.getAlgorithm());
360+
isEquals("Primary key MUST have a strength of 4096 bits.", 4096, primaryKey.getPGPPublicKey().getBitStrength());
361+
isEquals("There MUST be only one certification key", 1, key.getCertificationKeys().size());
362+
isEquals("The primary key MUST be the certification key", primaryKey, key.getCertificationKeys().get(0));
363+
364+
isEquals("There MUST be only one signing key", 1, key.getSigningKeys().size());
365+
OpenPGPCertificate.OpenPGPComponentKey signingKey = key.getSigningKeys().get(0);
366+
isEquals("Signing key MUST be an RSA key", PublicKeyAlgorithmTags.RSA_GENERAL, signingKey.getAlgorithm());
367+
isEquals("Signing key MUST have a strength of 4096 bits.", 4096, signingKey.getPGPPublicKey().getBitStrength());
368+
isFalse("The signing key MUST NOT be the primary key", primaryKey.equals(signingKey));
369+
370+
isEquals("There MUST be only one encryption key", 1, key.getEncryptionKeys().size());
371+
OpenPGPCertificate.OpenPGPComponentKey encryptionKey = key.getEncryptionKeys().get(0);
372+
isEquals("Primary key MUST be an RSA key", PublicKeyAlgorithmTags.RSA_GENERAL, encryptionKey.getAlgorithm());
373+
isEquals("Encryption key MUST have a strength of 4096 bits.", 4096, encryptionKey.getPGPPublicKey().getBitStrength());
374+
isFalse("The encryption key MUST NOT be the primary key", primaryKey.equals(encryptionKey));
375+
376+
isFalse("The signing key MUST NOT be the encryption key", signingKey.equals(encryptionKey));
377+
}
378+
323379
private void testGenerateCustomKey(OpenPGPApi api)
324380
throws PGPException
325381
{

0 commit comments

Comments
 (0)