public void performTest()
throws Exception
{
String file = null;
KeyFactory fact = KeyFactory.getInstance("DSA", "BC");
PGPPublicKey pubKey = null;
PrivateKey privKey = null;
PGPUtil.setDefaultProvider("BC");
//
// Read the public key
//
PGPPublicKeyRing pgpPub = new PGPPublicKeyRing(testPubKey, new BcKeyFingerprintCalculator());
pubKey = pgpPub.getPublicKey();
//
// Read the private key
//
PGPSecretKeyRing sKey = new PGPSecretKeyRing(testPrivKey, new BcKeyFingerprintCalculator());
PGPPrivateKey pgpPrivKey = sKey.getSecretKey().extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass));
//
// test signature message
//
PGPObjectFactory pgpFact = new PGPObjectFactory(sig1);
PGPCompressedData c1 = (PGPCompressedData)pgpFact.nextObject();
pgpFact = new PGPObjectFactory(c1.getDataStream());
PGPOnePassSignatureList p1 = (PGPOnePassSignatureList)pgpFact.nextObject();
PGPOnePassSignature ops = p1.get(0);
PGPLiteralData p2 = (PGPLiteralData)pgpFact.nextObject();
InputStream dIn = p2.getInputStream();
int ch;
ops.init(new BcPGPContentVerifierBuilderProvider(), pubKey);
while ((ch = dIn.read()) >= 0)
{
ops.update((byte)ch);
}
PGPSignatureList p3 = (PGPSignatureList)pgpFact.nextObject();
if (!ops.verify(p3.get(0)))
{
fail("Failed signature check");
}
//
// signature generation
//
generateTest(sKey, pubKey, pgpPrivKey);
//
// signature generation - canonical text
//
String data = "hello world!";
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ByteArrayInputStream testIn = new ByteArrayInputStream(data.getBytes());
PGPSignatureGenerator sGen = new PGPSignatureGenerator(new BcPGPContentSignerBuilder(PGPPublicKey.DSA, PGPUtil.SHA1));
sGen.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, pgpPrivKey);
PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(
PGPCompressedData.ZIP);
BCPGOutputStream bcOut = new BCPGOutputStream(
cGen.open(new UncloseableOutputStream(bOut)));
sGen.generateOnePassVersion(false).encode(bcOut);
PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
Date testDate = new Date((System.currentTimeMillis() / 1000) * 1000);
OutputStream lOut = lGen.open(
new UncloseableOutputStream(bcOut),
PGPLiteralData.TEXT,
"_CONSOLE",
data.getBytes().length,
testDate);
while ((ch = testIn.read()) >= 0)
{
lOut.write(ch);
sGen.update((byte)ch);
}
lGen.close();
sGen.generate().encode(bcOut);
cGen.close();
//
// verify generated signature - canconical text
//
pgpFact = new PGPObjectFactory(bOut.toByteArray());
c1 = (PGPCompressedData)pgpFact.nextObject();
pgpFact = new PGPObjectFactory(c1.getDataStream());
p1 = (PGPOnePassSignatureList)pgpFact.nextObject();
ops = p1.get(0);
p2 = (PGPLiteralData)pgpFact.nextObject();
if (!p2.getModificationTime().equals(testDate))
{
fail("Modification time not preserved");
}
dIn = p2.getInputStream();
ops.init(new BcPGPContentVerifierBuilderProvider(), pubKey);
while ((ch = dIn.read()) >= 0)
{
ops.update((byte)ch);
}
p3 = (PGPSignatureList)pgpFact.nextObject();
if (!ops.verify(p3.get(0)))
{
fail("Failed generated signature check");
}
//
// Read the public key with user attributes
//
pgpPub = new PGPPublicKeyRing(testPubWithUserAttr, new BcKeyFingerprintCalculator());
pubKey = pgpPub.getPublicKey();
Iterator it = pubKey.getUserAttributes();
int count = 0;
while (it.hasNext())
{
PGPUserAttributeSubpacketVector attributes = (PGPUserAttributeSubpacketVector)it.next();
Iterator sigs = pubKey.getSignaturesForUserAttribute(attributes);
int sigCount = 0;
while (sigs.hasNext())
{
sigs.next();
sigCount++;
}
if (sigCount != 1)
{
fail("Failed user attributes signature check");
}
count++;
}
if (count != 1)
{
fail("Failed user attributes check");
}
byte[] pgpPubBytes = pgpPub.getEncoded();
pgpPub = new PGPPublicKeyRing(pgpPubBytes, new BcKeyFingerprintCalculator());
pubKey = pgpPub.getPublicKey();
it = pubKey.getUserAttributes();
count = 0;
while (it.hasNext())
{
it.next();
count++;
}
if (count != 1)
{
fail("Failed user attributes reread");
}
//
// reading test extra data - key with edge condition for DSA key password.
//
char [] passPhrase = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
sKey = new PGPSecretKeyRing(testPrivKey2, new BcKeyFingerprintCalculator());
pgpPrivKey = sKey.getSecretKey().extractPrivateKey(passPhrase, "BC");
byte[] bytes = pgpPrivKey.getKey().getEncoded();
//
// reading test - aes256 encrypted passphrase.
//
sKey = new PGPSecretKeyRing(aesSecretKey, new BcKeyFingerprintCalculator());
pgpPrivKey = sKey.getSecretKey().extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass));
bytes = pgpPrivKey.getKey().getEncoded();
//
// reading test - twofish encrypted passphrase.
//
sKey = new PGPSecretKeyRing(twofishSecretKey, new BcKeyFingerprintCalculator());
pgpPrivKey = sKey.getSecretKey().extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass));
bytes = pgpPrivKey.getKey().getEncoded();
//
// use of PGPKeyPair
//
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", "BC");
kpg.initialize(512);
KeyPair kp = kpg.generateKeyPair();
PGPKeyPair pgpKp = new PGPKeyPair(PGPPublicKey.DSA , kp.getPublic(), kp.getPrivate(), new Date());
PGPPublicKey k1 = pgpKp.getPublicKey();
PGPPrivateKey k2 = pgpKp.getPrivateKey();
}