Search in sources :

Example 1 with MissingUserIdOnKeyException

use of org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException in project Smack by igniterealtime.

the class OpenPgpContact method updateKeys.

/**
 * Update the contacts keys using a prefetched {@link PublicKeysListElement}.
 *
 * @param connection our {@link XMPPConnection}.
 * @param metadata pre-fetched OX metadata node of the contact.
 *
 * @throws InterruptedException in case the thread gets interrupted.
 * @throws SmackException.NotConnectedException in case the connection is not connected.
 * @throws SmackException.NoResponseException in case the server doesn't respond.
 * @throws IOException IO is dangerous.
 */
public void updateKeys(XMPPConnection connection, PublicKeysListElement metadata) throws InterruptedException, SmackException.NotConnectedException, SmackException.NoResponseException, IOException {
    Map<OpenPgpV4Fingerprint, Date> fingerprintsAndDates = new HashMap<>();
    for (OpenPgpV4Fingerprint fingerprint : metadata.getMetadata().keySet()) {
        fingerprintsAndDates.put(fingerprint, metadata.getMetadata().get(fingerprint).getDate());
    }
    store.setAnnouncedFingerprintsOf(getJid(), fingerprintsAndDates);
    Map<OpenPgpV4Fingerprint, Date> fetchDates = store.getPublicKeyFetchDates(getJid());
    for (OpenPgpV4Fingerprint fingerprint : metadata.getMetadata().keySet()) {
        Date fetchDate = fetchDates.get(fingerprint);
        if (fetchDate != null && fingerprintsAndDates.get(fingerprint) != null && fetchDate.after(fingerprintsAndDates.get(fingerprint))) {
            LOGGER.log(Level.FINE, "Skip key " + Long.toHexString(fingerprint.getKeyId()) + " as we already have the most recent version. " + "Last announced: " + fingerprintsAndDates.get(fingerprint).toString() + " Last fetched: " + fetchDate.toString());
            continue;
        }
        try {
            PubkeyElement key = OpenPgpPubSubUtil.fetchPubkey(connection, getJid(), fingerprint);
            unfetchableKeys.remove(fingerprint);
            fetchDates.put(fingerprint, new Date());
            if (key == null) {
                LOGGER.log(Level.WARNING, "Public key " + Long.toHexString(fingerprint.getKeyId()) + " can not be imported: Is null");
                unfetchableKeys.put(fingerprint, new NullPointerException("Public key is null."));
                continue;
            }
            PGPPublicKeyRing keyRing = new PGPPublicKeyRing(Base64.decode(key.getDataElement().getB64Data()), new BcKeyFingerprintCalculator());
            store.importPublicKey(getJid(), keyRing);
        } catch (PubSubException.NotAPubSubNodeException | PubSubException.NotALeafNodeException | XMPPException.XMPPErrorException e) {
            LOGGER.log(Level.WARNING, "Error fetching public key " + Long.toHexString(fingerprint.getKeyId()), e);
            unfetchableKeys.put(fingerprint, e);
        } catch (PGPException | IOException e) {
            LOGGER.log(Level.WARNING, "Public key " + Long.toHexString(fingerprint.getKeyId()) + " can not be imported.", e);
            unfetchableKeys.put(fingerprint, e);
        } catch (MissingUserIdOnKeyException e) {
            LOGGER.log(Level.WARNING, "Public key " + Long.toHexString(fingerprint.getKeyId()) + " is missing the user-id \"xmpp:" + getJid() + "\". Refuse to import it.", e);
            unfetchableKeys.put(fingerprint, e);
        }
    }
    store.setPublicKeyFetchDates(getJid(), fetchDates);
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) HashMap(java.util.HashMap) IOException(java.io.IOException) Date(java.util.Date) PGPException(org.bouncycastle.openpgp.PGPException) PubkeyElement(org.jivesoftware.smackx.ox.element.PubkeyElement) BcKeyFingerprintCalculator(org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) MissingUserIdOnKeyException(org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException)

Example 2 with MissingUserIdOnKeyException

use of org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException in project Smack by igniterealtime.

the class AbstractOpenPgpKeyStore method importSecretKey.

@Override
public void importSecretKey(BareJid owner, PGPSecretKeyRing secretKeys) throws IOException, PGPException, MissingUserIdOnKeyException {
    if (!new KeyRingInfo(secretKeys).isUserIdValid("xmpp:" + owner.toString())) {
        throw new MissingUserIdOnKeyException(owner, new OpenPgpV4Fingerprint(secretKeys));
    }
    PGPSecretKeyRingCollection secretKeyRings = getSecretKeysOf(owner);
    try {
        if (secretKeyRings != null) {
            secretKeyRings = PGPSecretKeyRingCollection.addSecretKeyRing(secretKeyRings, secretKeys);
        } else {
            secretKeyRings = new PGPSecretKeyRingCollection(Collections.singleton(secretKeys));
        }
    } catch (IllegalArgumentException e) {
        LOGGER.log(Level.INFO, "Skipping secret key ring " + Long.toHexString(secretKeys.getPublicKey().getKeyID()) + " as it is already in the key ring of " + owner.toString());
    }
    this.secretKeyRingCollections.put(owner, secretKeyRings);
    writeSecretKeysOf(owner, secretKeyRings);
}
Also used : KeyRingInfo(org.pgpainless.key.info.KeyRingInfo) PGPSecretKeyRingCollection(org.bouncycastle.openpgp.PGPSecretKeyRingCollection) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) MissingUserIdOnKeyException(org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException)

Example 3 with MissingUserIdOnKeyException

use of org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException 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)

Aggregations

MissingUserIdOnKeyException (org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException)3 OpenPgpV4Fingerprint (org.pgpainless.key.OpenPgpV4Fingerprint)3 KeyRingInfo (org.pgpainless.key.info.KeyRingInfo)2 IOException (java.io.IOException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 PGPException (org.bouncycastle.openpgp.PGPException)1 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)1 PGPPublicKeyRingCollection (org.bouncycastle.openpgp.PGPPublicKeyRingCollection)1 PGPSecretKeyRingCollection (org.bouncycastle.openpgp.PGPSecretKeyRingCollection)1 BcKeyFingerprintCalculator (org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator)1 PubkeyElement (org.jivesoftware.smackx.ox.element.PubkeyElement)1