Search in sources :

Example 1 with MysteriousT

use of net.java.otr4j.io.messages.MysteriousT in project Zom-Android by zom.

the class SessionImpl method transformSending.

// Retransmit last sent message. Spec document does not mention where or
// when that should happen, must check libotr code.
public String transformSending(String msgText, List<TLV> tlvs) throws OtrException {
    switch(this.getSessionStatus()) {
        case PLAINTEXT:
            if (getSessionPolicy().getRequireEncryption()) {
                lastSentMessage = msgText;
                doTransmitLastMessage = true;
                this.startSession();
                return null;
            } else
                // specification.
                return msgText;
        case ENCRYPTED:
            this.lastSentMessage = msgText;
            if (DEBUG_ENABLED)
                Log.d(LOG_TAG, getSessionID().getLocalUserId() + " sends an encrypted message to " + getSessionID().getRemoteUserId() + " through " + getSessionID().getProtocolName() + ".");
            // Get encryption keys.
            SessionKeys encryptionKeys = this.getEncryptionSessionKeys();
            int senderKeyID = encryptionKeys.getLocalKeyID();
            int receipientKeyID = encryptionKeys.getRemoteKeyID();
            // Increment CTR.
            encryptionKeys.incrementSendingCtr();
            byte[] ctr = encryptionKeys.getSendingCtr();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            if (msgText != null && msgText.length() > 0)
                try {
                    out.write(msgText.getBytes("UTF8"));
                } catch (IOException e) {
                    throw new OtrException(e);
                }
            // Append tlvs
            if (tlvs != null && tlvs.size() > 0) {
                out.write((byte) 0x00);
                OtrOutputStream eoos = new OtrOutputStream(out);
                for (TLV tlv : tlvs) {
                    try {
                        eoos.writeShort(tlv.type);
                        eoos.writeTlvData(tlv.value);
                        eoos.close();
                    } catch (IOException e) {
                        throw new OtrException(e);
                    }
                }
            }
            OtrCryptoEngine otrCryptoEngine = new OtrCryptoEngineImpl();
            byte[] data = out.toByteArray();
            // Encrypt message.
            if (DEBUG_ENABLED)
                Log.d(LOG_TAG, "Encrypting message with keyids (localKeyID, remoteKeyID) = (" + senderKeyID + ", " + receipientKeyID + ")");
            byte[] encryptedMsg = otrCryptoEngine.aesEncrypt(encryptionKeys.getSendingAESKey(), ctr, data);
            // Get most recent keys to get the next D-H public key.
            SessionKeys mostRecentKeys = this.getMostRecentSessionKeys();
            DHPublicKey nextDH = (DHPublicKey) mostRecentKeys.getLocalPair().getPublic();
            // Calculate T.
            MysteriousT t = new MysteriousT(2, 0, senderKeyID, receipientKeyID, nextDH, ctr, encryptedMsg);
            // Calculate T hash.
            byte[] sendingMACKey = encryptionKeys.getSendingMACKey();
            if (DEBUG_ENABLED)
                Log.d(LOG_TAG, "Transforming T to byte[] to calculate it's HmacSHA1.");
            byte[] serializedT;
            try {
                serializedT = SerializationUtils.toByteArray(t);
            } catch (IOException e) {
                throw new OtrException(e);
            }
            byte[] mac = otrCryptoEngine.sha1Hmac(serializedT, sendingMACKey, SerializationConstants.TYPE_LEN_MAC);
            // Get old MAC keys to be revealed.
            byte[] oldKeys = this.collectOldMacKeys();
            DataMessage m = new DataMessage(t, mac, oldKeys);
            try {
                return SerializationUtils.toString(m);
            } catch (IOException e) {
                throw new OtrException(e);
            }
        case FINISHED:
            this.lastSentMessage = msgText;
            showError("Your message to " + sessionID.getRemoteUserId() + " was not sent.  Either end your private conversation, or restart it.");
            return null;
        default:
            if (DEBUG_ENABLED)
                Log.d(LOG_TAG, "Unknown message state, not processing.");
            return msgText;
    }
}
Also used : DHPublicKey(javax.crypto.interfaces.DHPublicKey) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) OtrException(net.java.otr4j.OtrException) OtrCryptoEngine(net.java.otr4j.crypto.OtrCryptoEngine) MysteriousT(net.java.otr4j.io.messages.MysteriousT) DataMessage(net.java.otr4j.io.messages.DataMessage) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl) OtrOutputStream(net.java.otr4j.io.OtrOutputStream)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 DHPublicKey (javax.crypto.interfaces.DHPublicKey)1 OtrException (net.java.otr4j.OtrException)1 OtrCryptoEngine (net.java.otr4j.crypto.OtrCryptoEngine)1 OtrCryptoEngineImpl (net.java.otr4j.crypto.OtrCryptoEngineImpl)1 OtrOutputStream (net.java.otr4j.io.OtrOutputStream)1 DataMessage (net.java.otr4j.io.messages.DataMessage)1 MysteriousT (net.java.otr4j.io.messages.MysteriousT)1