Search in sources :

Example 1 with OtrException

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

the class OTRManager method abortSmp.

/**
     * Abort SM negotiation.
     */
public void abortSmp(String account, String user) throws NetworkException {
    LogManager.i(this, "aborting smp... " + user);
    removeSMRequest(account, user);
    removeSMProgress(account, user);
    try {
        getOrCreateSession(account, user).abortSmp();
    } catch (OtrException e) {
        throw new NetworkException(R.string.OTR_ERROR, e);
    }
}
Also used : OtrException(net.java.otr4j.OtrException) NetworkException(com.xabber.android.data.NetworkException)

Example 2 with OtrException

use of net.java.otr4j.OtrException 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 {
            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);
        MessageManager.getInstance().getChat(sessionID.getAccountID(), sessionID.getUserID()).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);
    } else {
        throw new IllegalStateException();
    }
    RosterManager.getInstance().onContactChanged(sessionID.getAccountID(), sessionID.getUserID());
}
Also used : OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) PublicKey(java.security.PublicKey) SessionStatus(net.java.otr4j.session.SessionStatus) OtrException(net.java.otr4j.OtrException) Session(net.java.otr4j.session.Session)

Example 3 with OtrException

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

the class RegularChat method onPacket.

@Override
protected boolean onPacket(String bareAddress, Stanza packet) {
    if (!super.onPacket(bareAddress, packet))
        return false;
    final String resource = Jid.getResource(packet.getFrom());
    if (packet instanceof Presence) {
        final Presence presence = (Presence) packet;
        if (this.resource != null && presence.getType() == Presence.Type.unavailable && this.resource.equals(resource)) {
            this.resource = null;
        }
        if (presence.getType() == Presence.Type.unavailable) {
            OTRManager.getInstance().onContactUnAvailable(account, user);
        }
    } else if (packet instanceof Message) {
        final Message message = (Message) packet;
        if (message.getType() == Message.Type.error)
            return true;
        MUCUser mucUser = MUC.getMUCUserExtension(message);
        if (mucUser != null && mucUser.getInvite() != null)
            return true;
        String text = message.getBody();
        if (text == null)
            return true;
        String thread = message.getThread();
        updateThreadId(thread);
        boolean unencrypted = false;
        try {
            text = OTRManager.getInstance().transformReceiving(account, user, text);
        } catch (OtrException e) {
            if (e.getCause() instanceof OTRUnencryptedException) {
                text = ((OTRUnencryptedException) e.getCause()).getText();
                unencrypted = true;
            } else {
                LogManager.exception(this, e);
                // Invalid message received.
                return true;
            }
        }
        // System message received.
        if (text == null || text.trim().equals(""))
            return true;
        if (!"".equals(resource))
            this.resource = resource;
        newMessage(resource, text, null, Delay.getDelay(message), true, true, unencrypted, Delay.isOfflineMessage(Jid.getServer(account), packet), MessageArchiveManager.getInstance().getSaveMode(account, user, getThreadId()) != SaveMode.fls);
    }
    return true;
}
Also used : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence) OtrException(net.java.otr4j.OtrException) OTRUnencryptedException(com.xabber.android.data.extension.otr.OTRUnencryptedException)

Example 4 with OtrException

use of net.java.otr4j.OtrException in project Conversations by siacs.

the class XmppConnectionService method renewSymmetricKey.

public boolean renewSymmetricKey(Conversation conversation) {
    Account account = conversation.getAccount();
    byte[] symmetricKey = new byte[32];
    this.mRandom.nextBytes(symmetricKey);
    Session otrSession = conversation.getOtrSession();
    if (otrSession != null) {
        MessagePacket packet = new MessagePacket();
        packet.setType(MessagePacket.TYPE_CHAT);
        packet.setFrom(account.getJid());
        MessageGenerator.addMessageHints(packet);
        packet.setAttribute("to", otrSession.getSessionID().getAccountID() + "/" + otrSession.getSessionID().getUserID());
        try {
            packet.setBody(otrSession.transformSending(CryptoHelper.FILETRANSFER + CryptoHelper.bytesToHex(symmetricKey))[0]);
            sendMessagePacket(account, packet);
            conversation.setSymmetricKey(symmetricKey);
            return true;
        } catch (OtrException e) {
            return false;
        }
    }
    return false;
}
Also used : MessagePacket(eu.siacs.conversations.xmpp.stanzas.MessagePacket) Account(eu.siacs.conversations.entities.Account) OtrException(net.java.otr4j.OtrException) Session(net.java.otr4j.session.Session)

Example 5 with OtrException

use of net.java.otr4j.OtrException in project Conversations by siacs.

the class MessageGenerator method generateOtrChat.

public MessagePacket generateOtrChat(Message message) {
    Session otrSession = message.getConversation().getOtrSession();
    if (otrSession == null) {
        return null;
    }
    MessagePacket packet = preparePacket(message);
    addMessageHints(packet);
    try {
        String content;
        if (message.hasFileOnRemoteHost()) {
            content = message.getFileParams().url.toString();
        } else {
            content = message.getBody();
        }
        packet.setBody(otrSession.transformSending(content)[0]);
        packet.addChild("encryption", "urn:xmpp:eme:0").setAttribute("namespace", "urn:xmpp:otr:0");
        return packet;
    } catch (OtrException e) {
        return null;
    }
}
Also used : MessagePacket(eu.siacs.conversations.xmpp.stanzas.MessagePacket) OtrException(net.java.otr4j.OtrException) Session(net.java.otr4j.session.Session)

Aggregations

OtrException (net.java.otr4j.OtrException)16 NetworkException (com.xabber.android.data.NetworkException)7 Session (net.java.otr4j.session.Session)7 AbstractChat (com.xabber.android.data.message.AbstractChat)2 MessagePacket (eu.siacs.conversations.xmpp.stanzas.MessagePacket)2 OTRUnencryptedException (com.xabber.android.data.extension.otr.OTRUnencryptedException)1 Account (eu.siacs.conversations.entities.Account)1 PublicKey (java.security.PublicKey)1 OtrCryptoException (net.java.otr4j.crypto.OtrCryptoException)1 SessionID (net.java.otr4j.session.SessionID)1 SessionImpl (net.java.otr4j.session.SessionImpl)1 SessionStatus (net.java.otr4j.session.SessionStatus)1 Message (org.jivesoftware.smack.packet.Message)1 Presence (org.jivesoftware.smack.packet.Presence)1 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)1