public void test(TestHarness harness)
{
harness.checkPoint("TestOfGnuPrivateKeyring");
try
{
GnuPrivateKeyring kr1 = new GnuPrivateKeyring();
// store a private key
PrivateKey pk1 = new DSSPrivateKey(Registry.ASN1_ENCODING_ID, p, q, g, x);
kr1.putPrivateKey(ALIAS, pk1, KEY_PASSWORD);
// retrieve the same private key
PrivateKey pk2 = (PrivateKey) kr1.getPrivateKey(ALIAS, KEY_PASSWORD);
// check that private key is the same
harness.check(pk2, pk1, "In-memory and original private key MUST be equal");
// store a public key
PublicKey k1 = new DSSPublicKey(Registry.ASN1_ENCODING_ID, p, q, g, y);
kr1.putPublicKey(ALIAS, k1);
// retrieve the same public key
PublicKey k2 = kr1.getPublicKey(ALIAS);
// check that public key is the same
harness.check(k2, k1, "In-memory and original public key MUST be equal");
// store a certificate
CertificateFactory x509Factory = CertificateFactory.getInstance("X.509");
byte[] certificateBytes = SELF_SIGNED_CERT.getBytes("ASCII");
ByteArrayInputStream bais = new ByteArrayInputStream(certificateBytes);
Certificate certificate = x509Factory.generateCertificate(bais);
Certificate[] chain1 = new Certificate[] { certificate };
kr1.putCertPath(ALIAS, chain1);
// retrieve the same certificate path
Certificate[] chain2 = kr1.getCertPath(ALIAS);
harness.check(Arrays.equals(chain2, chain1),
"In-memory and original certificate MUST be equal");
// persist the keyring
File ksFile = File.createTempFile("gkr-", ".gks");
ksFile.deleteOnExit();
FileOutputStream fos = new FileOutputStream(ksFile);
HashMap attributes = new HashMap();
attributes.put(IKeyring.KEYRING_DATA_OUT, fos);
attributes.put(IKeyring.KEYRING_PASSWORD, STORE_PASSWORD);
kr1.store(attributes);
fos.flush();
fos.close();
// re-load it from the persisted file
GnuPrivateKeyring kr2 = new GnuPrivateKeyring();
FileInputStream fis = new FileInputStream(ksFile);
attributes.clear();
attributes.put(IKeyring.KEYRING_DATA_IN, fis);
attributes.put(IKeyring.KEYRING_PASSWORD, STORE_PASSWORD);
kr2.load(attributes);
fis.close();
// retrieve the same private key
PrivateKey pk3 = (PrivateKey) kr2.getPrivateKey(ALIAS, KEY_PASSWORD);
// check that it is still the same
harness.check(pk3, pk1, "Persisted and original private key MUST be equal");
// retrieve the same public key
PublicKey k3 = kr2.getPublicKey(ALIAS);
// check that it is still the same
harness.check(k3, k1, "Persisted and original public key MUST be equal");
// retrieve the certificate chain
Certificate[] chain3 = kr2.getCertPath(ALIAS);
// check that it's still the same certificate
harness.check(Arrays.equals(chain3, chain1),
"Persisted and original certificate MUST be equal");
}
catch (Exception x)