Search in sources :

Example 1 with MXDecryptionResult

use of org.matrix.androidsdk.crypto.algorithms.MXDecryptionResult in project matrix-android-sdk by matrix-org.

the class MXOlmDevice method decryptGroupMessage.

/**
 * Decrypt a received message with an inbound group session.
 *
 * @param body      the base64-encoded body of the encrypted message.
 * @param roomId    theroom in which the message was received.
 * @param timeline  the id of the timeline where the event is decrypted. It is used to prevent replay attack.
 * @param sessionId the session identifier.
 * @param senderKey the base64-encoded curve25519 key of the sender.
 * @return the decrypting result. Nil if the sessionId is unknown.
 */
public MXDecryptionResult decryptGroupMessage(String body, String roomId, String timeline, String sessionId, String senderKey) throws MXDecryptionException {
    MXDecryptionResult result = new MXDecryptionResult();
    MXOlmInboundGroupSession2 session = getInboundGroupSession(sessionId, senderKey, roomId);
    if (null != session) {
        // the HS pretending a message was targeting a different room.
        if (TextUtils.equals(roomId, session.mRoomId)) {
            String errorMessage = "";
            OlmInboundGroupSession.DecryptMessageResult decryptResult = null;
            try {
                decryptResult = session.mSession.decryptMessage(body);
            } catch (Exception e) {
                Log.e(LOG_TAG, "## decryptGroupMessage () : decryptMessage failed " + e.getMessage());
                errorMessage = e.getMessage();
            }
            if (null != decryptResult) {
                if (null != timeline) {
                    if (!mInboundGroupSessionMessageIndexes.containsKey(timeline)) {
                        mInboundGroupSessionMessageIndexes.put(timeline, new HashMap<String, Boolean>());
                    }
                    String messageIndexKey = senderKey + "|" + sessionId + "|" + decryptResult.mIndex;
                    if (null != mInboundGroupSessionMessageIndexes.get(timeline).get(messageIndexKey)) {
                        String reason = String.format(MXCryptoError.DUPLICATE_MESSAGE_INDEX_REASON, decryptResult.mIndex);
                        Log.e(LOG_TAG, "## decryptGroupMessage() : " + reason);
                        throw new MXDecryptionException(new MXCryptoError(MXCryptoError.DUPLICATED_MESSAGE_INDEX_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, reason));
                    }
                    mInboundGroupSessionMessageIndexes.get(timeline).put(messageIndexKey, true);
                }
                mStore.storeInboundGroupSession(session);
                try {
                    JsonParser parser = new JsonParser();
                    result.mPayload = parser.parse(JsonUtils.convertFromUTF8(decryptResult.mDecryptedMessage));
                } catch (Exception e) {
                    Log.e(LOG_TAG, "## decryptGroupMessage() : RLEncoder.encode failed " + e.getMessage());
                    return null;
                }
                if (null == result.mPayload) {
                    Log.e(LOG_TAG, "## decryptGroupMessage() : fails to parse the payload");
                    return null;
                }
                result.mKeysClaimed = session.mKeysClaimed;
                result.mSenderKey = senderKey;
                result.mForwardingCurve25519KeyChain = session.mForwardingCurve25519KeyChain;
            } else {
                Log.e(LOG_TAG, "## decryptGroupMessage() : failed to decode the message");
                throw new MXDecryptionException(new MXCryptoError(MXCryptoError.OLM_ERROR_CODE, errorMessage, null));
            }
        } else {
            String reason = String.format(MXCryptoError.INBOUND_SESSION_MISMATCH_ROOM_ID_REASON, roomId, session.mRoomId);
            Log.e(LOG_TAG, "## decryptGroupMessage() : " + reason);
            throw new MXDecryptionException(new MXCryptoError(MXCryptoError.INBOUND_SESSION_MISMATCH_ROOM_ID_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, reason));
        }
    } else {
        Log.e(LOG_TAG, "## decryptGroupMessage() : Cannot retrieve inbound group session " + sessionId);
        throw new MXDecryptionException(mInboundGroupSessionWithIdError);
    }
    return result;
}
Also used : MXDecryptionResult(org.matrix.androidsdk.crypto.algorithms.MXDecryptionResult) MXOlmInboundGroupSession2(org.matrix.androidsdk.crypto.data.MXOlmInboundGroupSession2) OlmInboundGroupSession(org.matrix.olm.OlmInboundGroupSession) JsonParser(com.google.gson.JsonParser)

Example 2 with MXDecryptionResult

use of org.matrix.androidsdk.crypto.algorithms.MXDecryptionResult in project matrix-android-sdk by matrix-org.

the class MXMegolmDecryption method decryptEvent.

public MXEventDecryptionResult decryptEvent(Event event, String timeline, boolean requestKeysOnFail) throws MXDecryptionException {
    // sanity check
    if (null == event) {
        Log.e(LOG_TAG, "## decryptEvent() : null event");
        return null;
    }
    EncryptedEventContent encryptedEventContent = JsonUtils.toEncryptedEventContent(event.getWireContent().getAsJsonObject());
    String senderKey = encryptedEventContent.sender_key;
    String ciphertext = encryptedEventContent.ciphertext;
    String sessionId = encryptedEventContent.session_id;
    if (TextUtils.isEmpty(senderKey) || TextUtils.isEmpty(sessionId) || TextUtils.isEmpty(ciphertext)) {
        throw new MXDecryptionException(new MXCryptoError(MXCryptoError.MISSING_FIELDS_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, MXCryptoError.MISSING_FIELDS_REASON));
    }
    MXEventDecryptionResult eventDecryptionResult = null;
    MXCryptoError cryptoError = null;
    MXDecryptionResult decryptGroupMessageResult = null;
    try {
        decryptGroupMessageResult = mOlmDevice.decryptGroupMessage(ciphertext, event.roomId, timeline, sessionId, senderKey);
    } catch (MXDecryptionException e) {
        cryptoError = e.getCryptoError();
    }
    // the decryption succeeds
    if ((null != decryptGroupMessageResult) && (null != decryptGroupMessageResult.mPayload) && (null == cryptoError)) {
        eventDecryptionResult = new MXEventDecryptionResult();
        eventDecryptionResult.mClearEvent = decryptGroupMessageResult.mPayload;
        eventDecryptionResult.mSenderCurve25519Key = decryptGroupMessageResult.mSenderKey;
        if (null != decryptGroupMessageResult.mKeysClaimed) {
            eventDecryptionResult.mClaimedEd25519Key = decryptGroupMessageResult.mKeysClaimed.get("ed25519");
        }
        eventDecryptionResult.mForwardingCurve25519KeyChain = decryptGroupMessageResult.mForwardingCurve25519KeyChain;
    } else if (null != cryptoError) {
        if (cryptoError.isOlmError()) {
            if (TextUtils.equals("UNKNOWN_MESSAGE_INDEX", cryptoError.error)) {
                addEventToPendingList(event, timeline);
                if (requestKeysOnFail) {
                    requestKeysForEvent(event);
                }
            }
            String reason = String.format(MXCryptoError.OLM_REASON, cryptoError.error);
            String detailedReason = String.format(MXCryptoError.DETAILLED_OLM_REASON, ciphertext, cryptoError.error);
            throw new MXDecryptionException(new MXCryptoError(MXCryptoError.OLM_ERROR_CODE, reason, detailedReason));
        } else if (TextUtils.equals(cryptoError.errcode, MXCryptoError.UNKNOWN_INBOUND_SESSION_ID_ERROR_CODE)) {
            addEventToPendingList(event, timeline);
            if (requestKeysOnFail) {
                requestKeysForEvent(event);
            }
        }
        throw new MXDecryptionException(cryptoError);
    }
    return eventDecryptionResult;
}
Also used : MXDecryptionResult(org.matrix.androidsdk.crypto.algorithms.MXDecryptionResult) MXEventDecryptionResult(org.matrix.androidsdk.crypto.MXEventDecryptionResult) MXDecryptionException(org.matrix.androidsdk.crypto.MXDecryptionException) EncryptedEventContent(org.matrix.androidsdk.rest.model.crypto.EncryptedEventContent) MXCryptoError(org.matrix.androidsdk.crypto.MXCryptoError)

Aggregations

MXDecryptionResult (org.matrix.androidsdk.crypto.algorithms.MXDecryptionResult)2 JsonParser (com.google.gson.JsonParser)1 MXCryptoError (org.matrix.androidsdk.crypto.MXCryptoError)1 MXDecryptionException (org.matrix.androidsdk.crypto.MXDecryptionException)1 MXEventDecryptionResult (org.matrix.androidsdk.crypto.MXEventDecryptionResult)1 MXOlmInboundGroupSession2 (org.matrix.androidsdk.crypto.data.MXOlmInboundGroupSession2)1 EncryptedEventContent (org.matrix.androidsdk.rest.model.crypto.EncryptedEventContent)1 OlmInboundGroupSession (org.matrix.olm.OlmInboundGroupSession)1