Search in sources :

Example 1 with OlmMessage

use of org.matrix.olm.OlmMessage in project matrix-android-sdk by matrix-org.

the class MXOlmDevice method encryptMessage.

/**
 * Encrypt an outgoing message using an existing session.
 *
 * @param theirDeviceIdentityKey the Curve25519 identity key for the remote device.
 * @param sessionId              the id of the active session
 * @param payloadString          the payload to be encrypted and sent
 * @return the cipher text
 */
public Map<String, Object> encryptMessage(String theirDeviceIdentityKey, String sessionId, String payloadString) {
    HashMap<String, Object> res = null;
    OlmMessage olmMessage;
    OlmSession olmSession = getSessionForDevice(theirDeviceIdentityKey, sessionId);
    if (null != olmSession) {
        try {
            Log.d(LOG_TAG, "## encryptMessage() : olmSession.sessionIdentifier: " + olmSession.sessionIdentifier());
            // Log.d(LOG_TAG, "## encryptMessage() : payloadString: " + payloadString);
            olmMessage = olmSession.encryptMessage(payloadString);
            mStore.storeSession(olmSession, theirDeviceIdentityKey);
            res = new HashMap<>();
            res.put("body", olmMessage.mCipherText);
            res.put("type", olmMessage.mType);
        } catch (Exception e) {
            Log.e(LOG_TAG, "## encryptMessage() : failed " + e.getMessage());
        }
    }
    return res;
}
Also used : OlmSession(org.matrix.olm.OlmSession) OlmMessage(org.matrix.olm.OlmMessage)

Example 2 with OlmMessage

use of org.matrix.olm.OlmMessage in project matrix-android-sdk by matrix-org.

the class MXOlmDevice method createInboundSession.

/**
 * Generate a new inbound session, given an incoming message.
 *
 * @param theirDeviceIdentityKey the remote user's Curve25519 identity key.
 * @param messageType            the message_type field from the received message (must be 0).
 * @param ciphertext             base64-encoded body from the received message.
 * @return {{payload: string, session_id: string}} decrypted payload, andsession id of new session.
 */
public Map<String, String> createInboundSession(String theirDeviceIdentityKey, int messageType, String ciphertext) {
    Log.d(LOG_TAG, "## createInboundSession() : theirIdentityKey: " + theirDeviceIdentityKey);
    OlmSession olmSession = null;
    try {
        try {
            olmSession = new OlmSession();
            olmSession.initInboundSessionFrom(mOlmAccount, theirDeviceIdentityKey, ciphertext);
        } catch (Exception e) {
            Log.d(LOG_TAG, "## createInboundSession() : the session creation failed " + e.getMessage());
            return null;
        }
        Log.d(LOG_TAG, "## createInboundSession() : sessionId: " + olmSession.sessionIdentifier());
        try {
            mOlmAccount.removeOneTimeKeys(olmSession);
            mStore.storeAccount(mOlmAccount);
        } catch (Exception e) {
            Log.e(LOG_TAG, "## createInboundSession() : removeOneTimeKeys failed " + e.getMessage());
        }
        Log.d(LOG_TAG, "## createInboundSession() : ciphertext: " + ciphertext);
        try {
            Log.d(LOG_TAG, "## createInboundSession() :ciphertext: SHA256:" + mOlmUtility.sha256(URLEncoder.encode(ciphertext, "utf-8")));
        } catch (Exception e) {
            Log.e(LOG_TAG, "## createInboundSession() :ciphertext: cannot encode ciphertext");
        }
        OlmMessage olmMessage = new OlmMessage();
        olmMessage.mCipherText = ciphertext;
        olmMessage.mType = messageType;
        String payloadString = null;
        try {
            payloadString = olmSession.decryptMessage(olmMessage);
            mStore.storeSession(olmSession, theirDeviceIdentityKey);
        } catch (Exception e) {
            Log.d(LOG_TAG, "## createInboundSession() : decryptMessage failed " + e.getMessage());
        }
        HashMap<String, String> res = new HashMap<>();
        if (!TextUtils.isEmpty(payloadString)) {
            res.put("payload", payloadString);
        }
        String sessionIdentifier = olmSession.sessionIdentifier();
        if (!TextUtils.isEmpty(sessionIdentifier)) {
            res.put("session_id", sessionIdentifier);
        }
        return res;
    } catch (Exception e) {
        Log.e(LOG_TAG, "## createInboundSession() : OlmSession creation failed " + e.getMessage());
        if (null != olmSession) {
            olmSession.releaseSession();
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) OlmSession(org.matrix.olm.OlmSession) OlmMessage(org.matrix.olm.OlmMessage)

Example 3 with OlmMessage

use of org.matrix.olm.OlmMessage in project matrix-android-sdk by matrix-org.

the class MXOlmDevice method decryptMessage.

/**
 * Decrypt an incoming message using an existing session.
 *
 * @param ciphertext             the base64-encoded body from the received message.
 * @param messageType            message_type field from the received message.
 * @param theirDeviceIdentityKey the Curve25519 identity key for the remote device.
 * @param sessionId              the id of the active session.
 * @return the decrypted payload.
 */
public String decryptMessage(String ciphertext, int messageType, String sessionId, String theirDeviceIdentityKey) {
    String payloadString = null;
    OlmSession olmSession = getSessionForDevice(theirDeviceIdentityKey, sessionId);
    if (null != olmSession) {
        OlmMessage olmMessage = new OlmMessage();
        olmMessage.mCipherText = ciphertext;
        olmMessage.mType = messageType;
        try {
            payloadString = olmSession.decryptMessage(olmMessage);
            mStore.storeSession(olmSession, theirDeviceIdentityKey);
        } catch (Exception e) {
            Log.e(LOG_TAG, "## decryptMessage() : decryptMessage failed " + e.getMessage());
        }
    }
    return payloadString;
}
Also used : OlmSession(org.matrix.olm.OlmSession) OlmMessage(org.matrix.olm.OlmMessage)

Aggregations

OlmMessage (org.matrix.olm.OlmMessage)3 OlmSession (org.matrix.olm.OlmSession)3 HashMap (java.util.HashMap)1