Search in sources :

Example 1 with OtrOutputStream

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

the class OtrSm method computeSessionId.

/* Compute secret session ID as hash of agreed secret */
private static byte[] computeSessionId(BigInteger s) throws SMException {
    byte[] sdata;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OtrOutputStream oos = new OtrOutputStream(out);
        oos.write(0x00);
        oos.writeBigInt(s);
        sdata = out.toByteArray();
        oos.close();
    } catch (IOException e1) {
        throw new SMException(e1);
    }
    /* Calculate the session id */
    MessageDigest sha256;
    try {
        sha256 = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new SMException("cannot find SHA-256");
    }
    byte[] res = sha256.digest(sdata);
    byte[] secure_session_id = new byte[8];
    System.arraycopy(res, 0, secure_session_id, 0, 8);
    return secure_session_id;
}
Also used : OtrOutputStream(net.java.otr4j.io.OtrOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SMException(net.java.otr4j.crypto.SM.SMException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 2 with OtrOutputStream

use of net.java.otr4j.io.OtrOutputStream 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)

Example 3 with OtrOutputStream

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

the class IOTest method testIOShort.

public void testIOShort() throws Exception {
    int source = 10;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    OtrOutputStream oos = new OtrOutputStream(out);
    oos.writeShort(source);
    oos.close();
    byte[] converted = out.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(converted);
    OtrInputStream ois = new OtrInputStream(bin);
    int result = ois.readShort();
    ois.close();
    assertEquals(source, result);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) OtrInputStream(net.java.otr4j.io.OtrInputStream) OtrOutputStream(net.java.otr4j.io.OtrOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with OtrOutputStream

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

the class IOTest method testIODHPublicKey.

public void testIODHPublicKey() throws Exception {
    KeyPair pair = new OtrCryptoEngineImpl().generateDHKeyPair();
    DHPublicKey source = (DHPublicKey) pair.getPublic();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    OtrOutputStream oos = new OtrOutputStream(out);
    oos.writeDHPublicKey(source);
    oos.close();
    byte[] converted = out.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(converted);
    OtrInputStream ois = new OtrInputStream(bin);
    DHPublicKey result = ois.readDHPublicKey();
    ois.close();
    assertTrue(source.getY().compareTo(result.getY()) == 0);
}
Also used : KeyPair(java.security.KeyPair) DHPublicKey(javax.crypto.interfaces.DHPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) OtrInputStream(net.java.otr4j.io.OtrInputStream) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl) OtrOutputStream(net.java.otr4j.io.OtrOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with OtrOutputStream

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

the class IOTest method testIOData.

public void testIOData() throws Exception {
    byte[] source = new byte[] { 1, 1, 1, 1 };
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    OtrOutputStream oos = new OtrOutputStream(out);
    oos.writeData(source);
    oos.close();
    byte[] converted = out.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(converted);
    OtrInputStream ois = new OtrInputStream(bin);
    byte[] result = ois.readData();
    ois.close();
    assertTrue(java.util.Arrays.equals(source, result));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) OtrInputStream(net.java.otr4j.io.OtrInputStream) OtrOutputStream(net.java.otr4j.io.OtrOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 OtrOutputStream (net.java.otr4j.io.OtrOutputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OtrInputStream (net.java.otr4j.io.OtrInputStream)4 IOException (java.io.IOException)3 DHPublicKey (javax.crypto.interfaces.DHPublicKey)3 OtrCryptoEngineImpl (net.java.otr4j.crypto.OtrCryptoEngineImpl)3 BigInteger (java.math.BigInteger)2 KeyPair (java.security.KeyPair)2 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 OtrException (net.java.otr4j.OtrException)1 OtrCryptoEngine (net.java.otr4j.crypto.OtrCryptoEngine)1 SMException (net.java.otr4j.crypto.SM.SMException)1 DataMessage (net.java.otr4j.io.messages.DataMessage)1 MysteriousT (net.java.otr4j.io.messages.MysteriousT)1