Search in sources :

Example 1 with OtrCryptoException

use of net.java.otr4j.crypto.OtrCryptoException in project Conversations by siacs.

the class Conversation method getOtrFingerprint.

public synchronized String getOtrFingerprint() {
    if (this.otrFingerprint == null) {
        try {
            if (getOtrSession() == null || getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) {
                return null;
            }
            DSAPublicKey remotePubKey = (DSAPublicKey) getOtrSession().getRemotePublicKey();
            this.otrFingerprint = getAccount().getOtrService().getFingerprint(remotePubKey).toLowerCase(Locale.US);
        } catch (final OtrCryptoException | UnsupportedOperationException ignored) {
            return null;
        }
    }
    return this.otrFingerprint;
}
Also used : OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 2 with OtrCryptoException

use of net.java.otr4j.crypto.OtrCryptoException in project Spark by igniterealtime.

the class MyOtrKeyManager method getLocalFingerprint.

/**
 * Returns the local finger print for specified session. If there is no
 * finger print you might generate one.
 *
 * @return the local finger print for this sessionID
 */
public String getLocalFingerprint(SessionID sessionID) {
    KeyPair keyPair = loadLocalKeyPair(sessionID);
    if (keyPair == null)
        return null;
    PublicKey pubKey = keyPair.getPublic();
    try {
        return new OtrCryptoEngineImpl().getFingerprint(pubKey);
    } catch (OtrCryptoException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : KeyPair(java.security.KeyPair) OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) PublicKey(java.security.PublicKey) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl)

Example 3 with OtrCryptoException

use of net.java.otr4j.crypto.OtrCryptoException in project Zom-Android by zom.

the class OtrAndroidKeyManagerImpl method savePublicKey.

public void savePublicKey(SessionID sessionID, PublicKey pubKey) {
    if (sessionID == null)
        return;
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pubKey.getEncoded());
    // if (!Address.hasResource(sessionID.getRemoteUserId()))
    // return;
    String fullUserId = sessionID.getRemoteUserId();
    this.store.setProperty(fullUserId + ".publicKey", x509EncodedKeySpec.getEncoded());
    // and is useful for transferring rosters to other apps.
    try {
        String fingerprintString = new OtrCryptoEngineImpl().getFingerprint(pubKey);
        String verifiedToken = buildPublicKeyVerifiedId(sessionID.getRemoteUserId(), fingerprintString);
        String fingerprintKey = fullUserId + ".fingerprint";
        // if a fingerprint for this userid exists, then check if the key is verified
        if (this.store.hasProperty(fingerprintKey)) {
            if (!this.store.hasProperty(verifiedToken))
                this.store.setProperty(verifiedToken, false);
        } else {
            // if there is no key, then we can "trust on first use"!
            this.store.setProperty(fingerprintKey, fingerprintString);
            this.store.setProperty(verifiedToken, true);
        }
    } catch (OtrCryptoException e) {
        Log.e(ImApp.LOG_TAG, "otr error: " + e.getMessage(), e);
    }
}
Also used : OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Example 4 with OtrCryptoException

use of net.java.otr4j.crypto.OtrCryptoException in project xabber-android by redsolution.

the class OTRManager method sessionStatusChanged.

@Override
public void sessionStatusChanged(SessionID sessionID) {
    removeSMRequest(sessionID.getAccountID(), sessionID.getUserID());
    removeSMProgress(sessionID.getAccountID(), sessionID.getUserID());
    Session session = sessions.get(sessionID.getAccountID(), sessionID.getUserID());
    SessionStatus sStatus = session.getSessionStatus();
    LogManager.i(this, "session status changed " + sessionID.getUserID() + " status: " + sStatus);
    if (sStatus == SessionStatus.ENCRYPTED) {
        finished.remove(sessionID.getAccountID(), sessionID.getUserID());
        PublicKey remotePublicKey = session.getRemotePublicKey();
        String value;
        try {
            OtrCryptoEngine otrCryptoEngine = new OtrCryptoEngineImpl();
            value = otrCryptoEngine.getFingerprint(remotePublicKey);
        } catch (OtrCryptoException e) {
            LogManager.exception(this, e);
            value = null;
        }
        if (value != null) {
            actives.put(sessionID.getAccountID(), sessionID.getUserID(), value);
            if (fingerprints.get(sessionID.getAccountID(), sessionID.getUserID(), value) == null) {
                fingerprints.put(sessionID.getAccountID(), sessionID.getUserID(), value, false);
                requestToWrite(sessionID.getAccountID(), sessionID.getUserID(), value, false);
            }
        }
        newAction(sessionID.getAccountID(), sessionID.getUserID(), null, isVerified(sessionID.getAccountID(), sessionID.getUserID()) ? ChatAction.otr_verified : ChatAction.otr_encryption);
        AbstractChat chat = getChat(sessionID.getAccountID(), sessionID.getUserID());
        if (chat != null) {
            chat.sendMessages();
        }
    } else if (sStatus == SessionStatus.PLAINTEXT) {
        actives.remove(sessionID.getAccountID(), sessionID.getUserID());
        sessions.remove(sessionID.getAccountID(), sessionID.getUserID());
        finished.remove(sessionID.getAccountID(), sessionID.getUserID());
        try {
            session.endSession();
        } catch (OtrException e) {
            LogManager.exception(this, e);
        }
        newAction(sessionID.getAccountID(), sessionID.getUserID(), null, ChatAction.otr_plain);
    } else if (sStatus == SessionStatus.FINISHED) {
        actives.remove(sessionID.getAccountID(), sessionID.getUserID());
        sessions.remove(sessionID.getAccountID(), sessionID.getUserID());
        finished.put(sessionID.getAccountID(), sessionID.getUserID(), true);
        newAction(sessionID.getAccountID(), sessionID.getUserID(), null, ChatAction.otr_finish);
        // if session was finished then clear OTR-resource for this chat
        RegularChat chat = (RegularChat) getChat(sessionID.getAccountID(), sessionID.getUserID());
        if (chat != null) {
            chat.setOTRresource(null);
        }
    } else {
        throw new IllegalStateException();
    }
    onContactChanged(sessionID);
}
Also used : OtrCryptoEngine(net.java.otr4j.crypto.OtrCryptoEngine) OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) PublicKey(java.security.PublicKey) AbstractChat(com.xabber.android.data.message.AbstractChat) SessionStatus(net.java.otr4j.session.SessionStatus) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl) OtrException(net.java.otr4j.OtrException) RegularChat(com.xabber.android.data.message.RegularChat) Session(net.java.otr4j.session.Session)

Example 5 with OtrCryptoException

use of net.java.otr4j.crypto.OtrCryptoException in project Conversations by siacs.

the class Account method getOtrFingerprint.

public String getOtrFingerprint() {
    if (this.otrFingerprint == null) {
        try {
            if (this.mOtrService == null) {
                return null;
            }
            final PublicKey publicKey = this.mOtrService.getPublicKey();
            if (publicKey == null || !(publicKey instanceof DSAPublicKey)) {
                return null;
            }
            this.otrFingerprint = new OtrCryptoEngineImpl().getFingerprint(publicKey).toLowerCase(Locale.US);
            return this.otrFingerprint;
        } catch (final OtrCryptoException ignored) {
            return null;
        }
    } else {
        return this.otrFingerprint;
    }
}
Also used : OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Aggregations

OtrCryptoException (net.java.otr4j.crypto.OtrCryptoException)9 OtrCryptoEngineImpl (net.java.otr4j.crypto.OtrCryptoEngineImpl)7 PublicKey (java.security.PublicKey)6 DSAPublicKey (java.security.interfaces.DSAPublicKey)4 KeyPair (java.security.KeyPair)2 AbstractChat (com.xabber.android.data.message.AbstractChat)1 RegularChat (com.xabber.android.data.message.RegularChat)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 OtrException (net.java.otr4j.OtrException)1 OtrCryptoEngine (net.java.otr4j.crypto.OtrCryptoEngine)1 Session (net.java.otr4j.session.Session)1 SessionStatus (net.java.otr4j.session.SessionStatus)1