Search in sources :

Example 1 with OtrPolicy

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

the class OtrChatManager method transformSending.

public boolean transformSending(Message message, boolean isResponse, byte[] data) {
    String localUserId = message.getFrom().getAddress();
    String remoteUserId = message.getTo().getAddress();
    String body = message.getBody();
    SessionID sessionId = getSessionId(localUserId, remoteUserId);
    if (mOtrEngine != null && sessionId != null) {
        SessionStatus sessionStatus = mOtrEngine.getSessionStatus(sessionId);
        if (data != null && sessionStatus != SessionStatus.ENCRYPTED) {
            // Cannot send data without OTR, so start a session and drop message.
            // Message will be resent by caller when session is encrypted.
            startSession(sessionId);
            OtrDebugLogger.log("auto-start OTR on data send request");
            return false;
        }
        OtrDebugLogger.log("session status: " + sessionStatus);
        try {
            OtrPolicy sessionPolicy = getSessionPolicy(sessionId);
            if (sessionStatus == SessionStatus.PLAINTEXT && sessionPolicy.getRequireEncryption()) {
                startSession(sessionId);
                return false;
            }
            if (sessionStatus != SessionStatus.PLAINTEXT || sessionPolicy.getRequireEncryption()) {
                body = mOtrEngine.transformSending(sessionId, body, isResponse, data);
                if (!message.getTo().getAddress().contains("/"))
                    message.setTo(mOtrEngineHost.appendSessionResource(sessionId, message.getTo()));
            } else if (sessionStatus == SessionStatus.PLAINTEXT && sessionPolicy.getAllowV2() && sessionPolicy.getSendWhitespaceTag()) {
                // Work around asmack not sending whitespace tag for auto discovery
                body += " \t  \t\t\t\t \t \t \t   \t \t  \t   \t\t  \t ";
            }
        } catch (Exception e) {
            OtrDebugLogger.log("error encrypting", e);
            return false;
        }
    }
    message.setBody(body);
    return true;
}
Also used : OtrPolicy(net.java.otr4j.OtrPolicy) SessionStatus(net.java.otr4j.session.SessionStatus) SessionID(net.java.otr4j.session.SessionID) OtrException(net.java.otr4j.OtrException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with OtrPolicy

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

the class SessionImpl method transformReceiving.

/*
     * (non-Javadoc)
     *
     * @see
     * net.java.otr4j.session.ISession#handleReceivingMessage(java.lang.String)
     */
public String transformReceiving(String msgText, List<TLV> tlvs) throws OtrException, NullPointerException {
    OtrPolicy policy = getSessionPolicy();
    if (!policy.getAllowV1() && !policy.getAllowV2()) {
        if (DEBUG_ENABLED)
            Log.d(LOG_TAG, "Policy does not allow neither V1 not V2, ignoring message.");
        return msgText;
    }
    try {
        msgText = assembler.accumulate(msgText);
    } catch (ProtocolException e) {
        if (DEBUG_ENABLED)
            Log.d(LOG_TAG, "An invalid message fragment was discarded.");
        return null;
    }
    if (msgText == null)
        // Not a complete message (yet).
        return null;
    AbstractMessage m;
    try {
        m = SerializationUtils.toMessage(msgText);
    } catch (IOException e) {
        throw new OtrException(e);
    }
    if (m == null)
        // msgText; // Propably null or empty.
        return null;
    switch(m.messageType) {
        case AbstractEncodedMessage.MESSAGE_DATA:
            return handleDataMessage((DataMessage) m, tlvs);
        case AbstractMessage.MESSAGE_ERROR:
            handleErrorMessage((ErrorMessage) m);
            return null;
        case AbstractMessage.MESSAGE_PLAINTEXT:
            return handlePlainTextMessage((PlainTextMessage) m);
        case AbstractMessage.MESSAGE_QUERY:
            handleQueryMessage((QueryMessage) m);
            return null;
        case AbstractEncodedMessage.MESSAGE_DH_COMMIT:
        case AbstractEncodedMessage.MESSAGE_DHKEY:
        case AbstractEncodedMessage.MESSAGE_REVEALSIG:
        case AbstractEncodedMessage.MESSAGE_SIGNATURE:
            AuthContext auth = this.getAuthContext(false);
            auth.handleReceivingMessage(m);
            if (auth.getIsSecure()) {
                this.setSessionStatus(SessionStatus.ENCRYPTED);
                if (DEBUG_ENABLED)
                    Log.d(LOG_TAG, "Gone Secure.");
            }
            return null;
        default:
            throw new UnsupportedOperationException("Received an unknown message type.");
    }
}
Also used : OtrPolicy(net.java.otr4j.OtrPolicy) ProtocolException(java.net.ProtocolException) AbstractMessage(net.java.otr4j.io.messages.AbstractMessage) IOException(java.io.IOException) OtrException(net.java.otr4j.OtrException)

Example 3 with OtrPolicy

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

the class SessionImpl method handlePlainTextMessage.

private String handlePlainTextMessage(PlainTextMessage plainTextMessage) throws OtrException {
    // if (DEBUG_ENABLED) Log.d(LOG_TAG,getSessionID().getLocalUserId() + " received a plaintext message from "
    // + getSessionID().getRemoteUserId() + " throught "
    // + getSessionID().getProtocolName() + ".");
    OtrPolicy policy = getSessionPolicy();
    List<Integer> versions = plainTextMessage.versions;
    if (versions == null || versions.size() < 1) {
        // if (DEBUG_ENABLED) Log.d(LOG_TAG,"Received plaintext message without the whitespace tag.");
        switch(this.getSessionStatus()) {
            case ENCRYPTED:
            case FINISHED:
                // return "[WARNING UNENCRYPTED: " + plainTextMessage.cleanText + "]";
                return plainTextMessage.cleanText;
            case PLAINTEXT:
                // unencrypted.
                if (policy.getRequireEncryption()) {
                    showError("The message was received unencrypted.");
                }
                return plainTextMessage.cleanText;
        }
    } else {
        // if (DEBUG_ENABLED) Log.d(LOG_TAG,"Received plaintext message with the whitespace tag.");
        switch(this.getSessionStatus()) {
            case ENCRYPTED:
            case FINISHED:
                // Remove the whitespace tag and display the message to the
                // user, but warn him that the message was received
                // unencrypted.
                showError("The message was received unencrypted.");
            case PLAINTEXT:
                // was received unencrypted.
                if (policy.getRequireEncryption())
                    showError("The message was received unencrypted.");
        }
        if (policy.getWhitespaceStartAKE()) {
            if (DEBUG_ENABLED)
                Log.d(LOG_TAG, "WHITESPACE_START_AKE is set");
            if (plainTextMessage.versions.contains(2) && policy.getAllowV2()) {
                if (DEBUG_ENABLED)
                    Log.d(LOG_TAG, "V2 tag found.");
                getAuthContext(true).respondV2Auth();
            } else if (plainTextMessage.versions.contains(1) && policy.getAllowV1()) {
                throw new UnsupportedOperationException();
            }
        }
    }
    return plainTextMessage.cleanText;
}
Also used : OtrPolicy(net.java.otr4j.OtrPolicy) BigInteger(java.math.BigInteger)

Example 4 with OtrPolicy

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

the class SessionImpl method handleErrorMessage.

private void handleErrorMessage(ErrorMessage errorMessage) throws OtrException {
    if (DEBUG_ENABLED)
        Log.d(LOG_TAG, getSessionID().getLocalUserId() + " received an error message from " + getSessionID().getRemoteUserId() + " throught " + getSessionID().getRemoteUserId() + ".");
    OtrPolicy policy = getSessionPolicy();
    // Re-negotiate if we got an error and we are encrypted
    if (policy.getErrorStartAKE() && getSessionStatus() == SessionStatus.ENCRYPTED) {
        showWarning(errorMessage.error + " Initiating encryption.");
        if (DEBUG_ENABLED)
            Log.d(LOG_TAG, "Error message starts AKE.");
        doTransmitLastMessage = true;
        isLastMessageRetransmit = true;
        Vector<Integer> versions = new Vector<Integer>();
        if (policy.getAllowV1())
            versions.add(1);
        if (policy.getAllowV2())
            versions.add(2);
        if (DEBUG_ENABLED)
            Log.d(LOG_TAG, "Sending Query");
        injectMessage(new QueryMessage(versions));
    } else {
        showError(errorMessage.error);
    }
}
Also used : OtrPolicy(net.java.otr4j.OtrPolicy) BigInteger(java.math.BigInteger) QueryMessage(net.java.otr4j.io.messages.QueryMessage) Vector(java.util.Vector)

Aggregations

OtrPolicy (net.java.otr4j.OtrPolicy)4 BigInteger (java.math.BigInteger)2 OtrException (net.java.otr4j.OtrException)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ProtocolException (java.net.ProtocolException)1 Vector (java.util.Vector)1 AbstractMessage (net.java.otr4j.io.messages.AbstractMessage)1 QueryMessage (net.java.otr4j.io.messages.QueryMessage)1 SessionID (net.java.otr4j.session.SessionID)1 SessionStatus (net.java.otr4j.session.SessionStatus)1