Search in sources :

Example 6 with PGPSecretKeyRing

use of org.bouncycastle.openpgp.PGPSecretKeyRing in project Smack by igniterealtime.

the class OpenPgpSelf method getSigningKeyRing.

/**
 * Return the {@link PGPSecretKeyRing} which we will use to sign our messages.
 * @return signing key
 * @throws IOException IO is dangerous
 * @throws PGPException PGP is brittle
 */
public PGPSecretKeyRing getSigningKeyRing() throws IOException, PGPException {
    PGPSecretKeyRingCollection secretKeyRings = getSecretKeys();
    if (secretKeyRings == null) {
        return null;
    }
    PGPSecretKeyRing signingKeyRing = null;
    for (PGPSecretKeyRing ring : secretKeyRings) {
        if (signingKeyRing == null) {
            signingKeyRing = ring;
            continue;
        }
        if (ring.getPublicKey().getCreationTime().after(signingKeyRing.getPublicKey().getCreationTime())) {
            signingKeyRing = ring;
        }
    }
    return signingKeyRing;
}
Also used : PGPSecretKeyRingCollection(org.bouncycastle.openpgp.PGPSecretKeyRingCollection) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing)

Example 7 with PGPSecretKeyRing

use of org.bouncycastle.openpgp.PGPSecretKeyRing in project Smack by igniterealtime.

the class OpenPgpStoreTest method t06_key_keyReloadTest.

@Test
public void t06_key_keyReloadTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException {
    PGPSecretKeyRing secretKeys = openPgpStoreInstance1.generateKeyRing(alice);
    OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(secretKeys);
    PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
    openPgpStoreInstance1.importSecretKey(alice, secretKeys);
    openPgpStoreInstance1.importPublicKey(alice, publicKeys);
    assertNotNull(openPgpStoreInstance2.getSecretKeysOf(alice));
    assertNotNull(openPgpStoreInstance2.getPublicKeysOf(alice));
    // Clean up
    openPgpStoreInstance1.deletePublicKeyRing(alice, fingerprint);
    openPgpStoreInstance1.deleteSecretKeyRing(alice, fingerprint);
    openPgpStoreInstance2.deletePublicKeyRing(alice, fingerprint);
    openPgpStoreInstance2.deleteSecretKeyRing(alice, fingerprint);
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) Test(org.junit.Test)

Example 8 with PGPSecretKeyRing

use of org.bouncycastle.openpgp.PGPSecretKeyRing in project Smack by igniterealtime.

the class OpenPgpStoreTest method t02_key_importKeysTest.

@Test
public void t02_key_importKeysTest() throws IOException, PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, MissingUserIdOnKeyException {
    // Test for nullity of all possible values.
    PGPSecretKeyRing secretKeys = openPgpStoreInstance1.generateKeyRing(alice);
    PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
    assertNotNull(secretKeys);
    assertNotNull(publicKeys);
    OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(secretKeys);
    OpenPgpContact cAlice = openPgpStoreInstance1.getOpenPgpContact(alice);
    assertNull(cAlice.getAnyPublicKeys());
    assertEquals(fingerprint, new OpenPgpV4Fingerprint(secretKeys));
    assertNull(openPgpStoreInstance1.getPublicKeysOf(alice));
    assertNull(openPgpStoreInstance1.getSecretKeysOf(alice));
    openPgpStoreInstance1.importPublicKey(alice, publicKeys);
    assertArrayEquals(publicKeys.getEncoded(), openPgpStoreInstance1.getPublicKeysOf(alice).getEncoded());
    assertNotNull(openPgpStoreInstance1.getPublicKeyRing(alice, fingerprint));
    assertNull(openPgpStoreInstance1.getSecretKeysOf(alice));
    cAlice = openPgpStoreInstance1.getOpenPgpContact(alice);
    assertNotNull(cAlice.getAnyPublicKeys());
    // Import keys a second time -> No change expected.
    openPgpStoreInstance1.importPublicKey(alice, publicKeys);
    assertArrayEquals(publicKeys.getEncoded(), openPgpStoreInstance1.getPublicKeysOf(alice).getEncoded());
    openPgpStoreInstance1.importSecretKey(alice, secretKeys);
    assertArrayEquals(secretKeys.getEncoded(), openPgpStoreInstance1.getSecretKeysOf(alice).getEncoded());
    openPgpStoreInstance1.importSecretKey(alice, secretKeys);
    assertNotNull(openPgpStoreInstance1.getSecretKeysOf(alice));
    assertArrayEquals(secretKeys.getEncoded(), openPgpStoreInstance1.getSecretKeysOf(alice).getEncoded());
    assertNotNull(openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint));
    assertArrayEquals(secretKeys.getEncoded(), openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint).getEncoded());
    assertArrayEquals(publicKeys.getEncoded(), openPgpStoreInstance1.getPublicKeyRing(alice, fingerprint).getEncoded());
    // Clean up
    openPgpStoreInstance1.deletePublicKeyRing(alice, fingerprint);
    openPgpStoreInstance1.deleteSecretKeyRing(alice, fingerprint);
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) Test(org.junit.Test)

Example 9 with PGPSecretKeyRing

use of org.bouncycastle.openpgp.PGPSecretKeyRing in project Smack by igniterealtime.

the class OpenPgpStoreTest method t00_deleteTest.

/*
    OpenPgpKeyStore
     */
@Test
public void t00_deleteTest() throws IOException, PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, MissingUserIdOnKeyException {
    assertNull(openPgpStoreInstance1.getSecretKeysOf(alice));
    assertNull(openPgpStoreInstance1.getPublicKeysOf(alice));
    PGPSecretKeyRing keys = openPgpStoreInstance1.generateKeyRing(alice);
    OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(keys);
    openPgpStoreInstance1.importSecretKey(alice, keys);
    openPgpStoreInstance1.importPublicKey(alice, KeyRingUtils.publicKeyRingFrom(keys));
    assertNotNull(openPgpStoreInstance1.getSecretKeysOf(alice));
    assertNotNull(openPgpStoreInstance1.getPublicKeysOf(alice));
    openPgpStoreInstance1.deleteSecretKeyRing(alice, fingerprint);
    openPgpStoreInstance1.deletePublicKeyRing(alice, fingerprint);
    assertNull(openPgpStoreInstance1.getPublicKeysOf(alice));
    assertNull(openPgpStoreInstance1.getSecretKeysOf(alice));
}
Also used : OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) Test(org.junit.Test)

Example 10 with PGPSecretKeyRing

use of org.bouncycastle.openpgp.PGPSecretKeyRing in project Smack by igniterealtime.

the class OpenPgpStoreTest method t07_multipleKeysTest.

@Test
public void t07_multipleKeysTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException {
    PGPSecretKeyRing one = openPgpStoreInstance1.generateKeyRing(alice);
    PGPSecretKeyRing two = openPgpStoreInstance1.generateKeyRing(alice);
    OpenPgpV4Fingerprint fingerprint1 = new OpenPgpV4Fingerprint(one);
    OpenPgpV4Fingerprint fingerprint2 = new OpenPgpV4Fingerprint(two);
    openPgpStoreInstance1.importSecretKey(alice, one);
    openPgpStoreInstance1.importSecretKey(alice, two);
    openPgpStoreInstance1.importPublicKey(alice, KeyRingUtils.publicKeyRingFrom(one));
    openPgpStoreInstance1.importPublicKey(alice, KeyRingUtils.publicKeyRingFrom(two));
    assertArrayEquals(one.getEncoded(), openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint1).getEncoded());
    assertArrayEquals(two.getEncoded(), openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint2).getEncoded());
    assertArrayEquals(one.getEncoded(), openPgpStoreInstance1.getSecretKeysOf(alice).getSecretKeyRing(fingerprint1.getKeyId()).getEncoded());
    assertArrayEquals(KeyRingUtils.publicKeyRingFrom(one).getEncoded(), openPgpStoreInstance1.getPublicKeyRing(alice, fingerprint1).getEncoded());
    // Cleanup
    openPgpStoreInstance1.deletePublicKeyRing(alice, fingerprint1);
    openPgpStoreInstance1.deletePublicKeyRing(alice, fingerprint2);
    openPgpStoreInstance1.deleteSecretKeyRing(alice, fingerprint1);
    openPgpStoreInstance1.deleteSecretKeyRing(alice, fingerprint2);
}
Also used : OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) Test(org.junit.Test)

Aggregations

PGPSecretKeyRing (org.bouncycastle.openpgp.PGPSecretKeyRing)22 OpenPgpV4Fingerprint (org.pgpainless.key.OpenPgpV4Fingerprint)11 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)6 PGPSecretKey (org.bouncycastle.openpgp.PGPSecretKey)6 Test (org.junit.Test)6 PGPSecretKeyRingCollection (org.bouncycastle.openpgp.PGPSecretKeyRingCollection)5 BcKeyFingerprintCalculator (org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator)3 JcePBESecretKeyDecryptorBuilder (org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder)3 PainlessOpenPgpProvider (org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider)3 SecretkeyElement (org.jivesoftware.smackx.ox.element.SecretkeyElement)3 OpenPgpStore (org.jivesoftware.smackx.ox.store.definition.OpenPgpStore)3 FileBasedOpenPgpStore (org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore)3 PGPPrivateKey (org.bouncycastle.openpgp.PGPPrivateKey)2 Test (org.junit.jupiter.api.Test)2 BareJid (org.jxmpp.jid.BareJid)2 UnprotectedKeysProtector (org.pgpainless.key.protection.UnprotectedKeysProtector)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1