Search in sources :

Example 16 with OpenPgpV4Fingerprint

use of org.pgpainless.key.OpenPgpV4Fingerprint in project Smack by igniterealtime.

the class OXSecretKeyBackupIntegrationTest method test.

@SmackIntegrationTest
public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, IOException, InterruptedException, PubSubException.NotALeafNodeException, SmackException.NoResponseException, SmackException.NotConnectedException, XMPPException.XMPPErrorException, SmackException.NotLoggedInException, SmackException.FeatureNotSupportedException, MissingUserIdOnKeyException, NoBackupFoundException, InvalidBackupCodeException, PGPException, MissingOpenPgpKeyException {
    OpenPgpStore beforeStore = new FileBasedOpenPgpStore(beforePath);
    beforeStore.setKeyRingProtector(new UnprotectedKeysProtector());
    PainlessOpenPgpProvider beforeProvider = new PainlessOpenPgpProvider(beforeStore);
    OpenPgpManager openPgpManager = OpenPgpManager.getInstanceFor(aliceConnection);
    openPgpManager.setOpenPgpProvider(beforeProvider);
    OpenPgpSelf self = openPgpManager.getOpenPgpSelf();
    assertNull(self.getSigningKeyFingerprint());
    OpenPgpV4Fingerprint keyFingerprint = openPgpManager.generateAndImportKeyPair(alice);
    assertEquals(keyFingerprint, self.getSigningKeyFingerprint());
    assertTrue(self.getSecretKeys().contains(keyFingerprint.getKeyId()));
    PGPSecretKeyRing beforeSec = beforeStore.getSecretKeyRing(alice, keyFingerprint);
    assertNotNull(beforeSec);
    PGPPublicKeyRing beforePub = beforeStore.getPublicKeyRing(alice, keyFingerprint);
    assertNotNull(beforePub);
    OpenPgpSecretKeyBackupPassphrase backupPassphrase = openPgpManager.backupSecretKeyToServer(availableSecretKeys -> availableSecretKeys);
    FileBasedOpenPgpStore afterStore = new FileBasedOpenPgpStore(afterPath);
    afterStore.setKeyRingProtector(new UnprotectedKeysProtector());
    PainlessOpenPgpProvider afterProvider = new PainlessOpenPgpProvider(afterStore);
    openPgpManager.setOpenPgpProvider(afterProvider);
    OpenPgpV4Fingerprint fingerprint = openPgpManager.restoreSecretKeyServerBackup(() -> backupPassphrase);
    assertEquals(keyFingerprint, fingerprint);
    assertTrue(self.getSecretKeys().contains(keyFingerprint.getKeyId()));
    assertEquals(keyFingerprint, self.getSigningKeyFingerprint());
    PGPSecretKeyRing afterSec = afterStore.getSecretKeyRing(alice, keyFingerprint);
    assertNotNull(afterSec);
    assertArrayEquals(beforeSec.getEncoded(), afterSec.getEncoded());
    PGPPublicKeyRing afterPub = afterStore.getPublicKeyRing(alice, keyFingerprint);
    assertNotNull(afterPub);
    assertArrayEquals(beforePub.getEncoded(), afterPub.getEncoded());
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) FileBasedOpenPgpStore(org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore) UnprotectedKeysProtector(org.pgpainless.key.protection.UnprotectedKeysProtector) FileBasedOpenPgpStore(org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore) OpenPgpStore(org.jivesoftware.smackx.ox.store.definition.OpenPgpStore) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) PainlessOpenPgpProvider(org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 17 with OpenPgpV4Fingerprint

use of org.pgpainless.key.OpenPgpV4Fingerprint in project Smack by igniterealtime.

the class OpenPgpContact method getFingerprintsOfKeysWithState.

/**
 * Return a {@link Set} of {@link OpenPgpV4Fingerprint}s of all keys in {@code publicKeys}, which are marked with the
 * {@link OpenPgpTrustStore.Trust} of {@code trust}.
 *
 * @param publicKeys {@link PGPPublicKeyRingCollection} of keys which are iterated.
 * @param trust {@link OpenPgpTrustStore.Trust} state.
 * @return {@link Set} of fingerprints
 *
 * @throws IOException IO error
 */
public Set<OpenPgpV4Fingerprint> getFingerprintsOfKeysWithState(PGPPublicKeyRingCollection publicKeys, OpenPgpTrustStore.Trust trust) throws IOException {
    PGPPublicKeyRingCollection keys = getPublicKeysOfTrustState(publicKeys, trust);
    Set<OpenPgpV4Fingerprint> fingerprints = new HashSet<>();
    if (keys == null) {
        return fingerprints;
    }
    for (PGPPublicKeyRing ring : keys) {
        fingerprints.add(new OpenPgpV4Fingerprint(ring));
    }
    return fingerprints;
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) PGPPublicKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRingCollection) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) HashSet(java.util.HashSet)

Example 18 with OpenPgpV4Fingerprint

use of org.pgpainless.key.OpenPgpV4Fingerprint in project Smack by igniterealtime.

the class PublicKeysListElementProvider method parse.

@Override
public PublicKeysListElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, ParseException {
    PublicKeysListElement.Builder builder = PublicKeysListElement.builder();
    while (true) {
        XmlPullParser.TagEvent tag = parser.nextTag();
        String name;
        switch(tag) {
            case START_ELEMENT:
                name = parser.getName();
                if (PublicKeysListElement.PubkeyMetadataElement.ELEMENT.equals(name)) {
                    String finger = parser.getAttributeValue(null, PublicKeysListElement.PubkeyMetadataElement.ATTR_V4_FINGERPRINT);
                    String dt = parser.getAttributeValue(null, PublicKeysListElement.PubkeyMetadataElement.ATTR_DATE);
                    OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(finger);
                    Date date = ParserUtils.getDateFromXep82String(dt);
                    builder.addMetadata(new PublicKeysListElement.PubkeyMetadataElement(fingerprint, date));
                }
                break;
            case END_ELEMENT:
                name = parser.getName();
                if (name.equals(PublicKeysListElement.ELEMENT)) {
                    return builder.build();
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
}
Also used : PublicKeysListElement(org.jivesoftware.smackx.ox.element.PublicKeysListElement) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) Date(java.util.Date)

Example 19 with OpenPgpV4Fingerprint

use of org.pgpainless.key.OpenPgpV4Fingerprint in project Smack by igniterealtime.

the class AbstractOpenPgpKeyStore method importPublicKey.

@Override
public void importPublicKey(BareJid owner, PGPPublicKeyRing publicKeys) throws IOException, PGPException, MissingUserIdOnKeyException {
    if (!new KeyRingInfo(publicKeys).isUserIdValid("xmpp:" + owner.toString())) {
        throw new MissingUserIdOnKeyException(owner, new OpenPgpV4Fingerprint(publicKeys));
    }
    PGPPublicKeyRingCollection publicKeyRings = getPublicKeysOf(owner);
    try {
        if (publicKeyRings != null) {
            publicKeyRings = PGPPublicKeyRingCollection.addPublicKeyRing(publicKeyRings, publicKeys);
        } else {
            publicKeyRings = new PGPPublicKeyRingCollection(Collections.singleton(publicKeys));
        }
    } catch (IllegalArgumentException e) {
        LOGGER.log(Level.FINE, "Skipping public key ring " + Long.toHexString(publicKeys.getPublicKey().getKeyID()) + " as it is already in the key ring of " + owner.toString(), e);
    }
    this.publicKeyRingCollections.put(owner, publicKeyRings);
    writePublicKeysOf(owner, publicKeyRings);
}
Also used : PGPPublicKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRingCollection) KeyRingInfo(org.pgpainless.key.info.KeyRingInfo) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) MissingUserIdOnKeyException(org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException)

Example 20 with OpenPgpV4Fingerprint

use of org.pgpainless.key.OpenPgpV4Fingerprint in project Smack by igniterealtime.

the class OpenPgpManager method announceSupportAndPublish.

/**
 * Generate a fresh OpenPGP key pair, given we don't have one already.
 * Publish the public key to the Public Key Node and update the Public Key Metadata Node with our keys fingerprint.
 * Lastly register a {@link PepListener} which listens for updates to Public Key Metadata Nodes.
 *
 * @throws NoSuchAlgorithmException if we are missing an algorithm to generate a fresh key pair.
 * @throws NoSuchProviderException if we are missing a suitable {@link java.security.Provider}.
 * @throws InterruptedException if the thread gets interrupted.
 * @throws PubSubException.NotALeafNodeException if one of the PubSub nodes is not a {@link LeafNode}.
 * @throws XMPPException.XMPPErrorException in case of an XMPP protocol error.
 * @throws SmackException.NotConnectedException if we are not connected.
 * @throws SmackException.NoResponseException if the server doesn't respond.
 * @throws IOException IO is dangerous.
 * @throws InvalidAlgorithmParameterException if illegal algorithm parameters are used for key generation.
 * @throws SmackException.NotLoggedInException if we are not logged in.
 * @throws PGPException if something goes wrong during key loading/generating
 */
public void announceSupportAndPublish() throws NoSuchAlgorithmException, NoSuchProviderException, InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException, IOException, InvalidAlgorithmParameterException, SmackException.NotLoggedInException, PGPException {
    throwIfNoProviderSet();
    throwIfNotAuthenticated();
    OpenPgpV4Fingerprint primaryFingerprint = getOurFingerprint();
    if (primaryFingerprint == null) {
        primaryFingerprint = generateAndImportKeyPair(getJidOrThrow());
    }
    // Create <pubkey/> element
    PubkeyElement pubkeyElement;
    try {
        pubkeyElement = createPubkeyElement(getJidOrThrow(), primaryFingerprint, new Date());
    } catch (MissingOpenPgpKeyException e) {
        throw new AssertionError("Cannot publish our public key, since it is missing (MUST NOT happen!)");
    }
    // publish it
    publishPublicKey(pepManager, pubkeyElement, primaryFingerprint);
    // Subscribe to public key changes
    pepManager.addPepEventListener(PEP_NODE_PUBLIC_KEYS, PublicKeysListElement.class, pepPublicKeyListElementListener);
    ServiceDiscoveryManager.getInstanceFor(connection()).addFeature(PEP_NODE_PUBLIC_KEYS_NOTIFY);
}
Also used : PubkeyElement(org.jivesoftware.smackx.ox.element.PubkeyElement) MissingOpenPgpKeyException(org.jivesoftware.smackx.ox.exception.MissingOpenPgpKeyException) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) Date(java.util.Date)

Aggregations

OpenPgpV4Fingerprint (org.pgpainless.key.OpenPgpV4Fingerprint)28 Date (java.util.Date)11 PGPSecretKeyRing (org.bouncycastle.openpgp.PGPSecretKeyRing)11 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)8 Test (org.junit.Test)7 Test (org.junit.jupiter.api.Test)5 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 PGPPublicKeyRingCollection (org.bouncycastle.openpgp.PGPPublicKeyRingCollection)3 PainlessOpenPgpProvider (org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider)3 PublicKeysListElement (org.jivesoftware.smackx.ox.element.PublicKeysListElement)3 SecretkeyElement (org.jivesoftware.smackx.ox.element.SecretkeyElement)3 MissingUserIdOnKeyException (org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException)3 OpenPgpStore (org.jivesoftware.smackx.ox.store.definition.OpenPgpStore)3 FileBasedOpenPgpStore (org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore)3 KeyRingInfo (org.pgpainless.key.info.KeyRingInfo)3 UnprotectedKeysProtector (org.pgpainless.key.protection.UnprotectedKeysProtector)3 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2