use of org.matrix.androidsdk.rest.model.crypto.OlmPayloadContent in project matrix-android-sdk by matrix-org.
the class MXOlmDecryption method decryptEvent.
@Override
public MXEventDecryptionResult decryptEvent(Event event, String timeline) throws MXDecryptionException {
// sanity check
if (null == event) {
Log.e(LOG_TAG, "## decryptEvent() : null event");
return null;
}
OlmEventContent olmEventContent = JsonUtils.toOlmEventContent(event.getWireContent().getAsJsonObject());
String deviceKey = olmEventContent.sender_key;
Map<String, Object> ciphertext = olmEventContent.ciphertext;
if (null == ciphertext) {
Log.e(LOG_TAG, "## decryptEvent() : missing cipher text");
throw new MXDecryptionException(new MXCryptoError(MXCryptoError.MISSING_CIPHER_TEXT_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, MXCryptoError.MISSING_CIPHER_TEXT_REASON));
}
if (!ciphertext.containsKey(mOlmDevice.getDeviceCurve25519Key())) {
Log.e(LOG_TAG, "## decryptEvent() : our device " + mOlmDevice.getDeviceCurve25519Key() + " is not included in recipients. Event " + event.getContentAsJsonObject());
throw new MXDecryptionException(new MXCryptoError(MXCryptoError.NOT_INCLUDE_IN_RECIPIENTS_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, MXCryptoError.NOT_INCLUDED_IN_RECIPIENT_REASON));
}
// The message for myUser
Map<String, Object> message = (Map<String, Object>) ciphertext.get(mOlmDevice.getDeviceCurve25519Key());
String payloadString = decryptMessage(message, deviceKey);
if (null == payloadString) {
Log.e(LOG_TAG, "## decryptEvent() Failed to decrypt Olm event (id= " + event.eventId + " ) from " + deviceKey);
throw new MXDecryptionException(new MXCryptoError(MXCryptoError.BAD_ENCRYPTED_MESSAGE_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, MXCryptoError.BAD_ENCRYPTED_MESSAGE_REASON));
}
JsonElement payload = new JsonParser().parse(JsonUtils.convertFromUTF8(payloadString));
if (null == payload) {
Log.e(LOG_TAG, "## decryptEvent failed : null payload");
throw new MXDecryptionException(new MXCryptoError(MXCryptoError.UNABLE_TO_DECRYPT_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, MXCryptoError.MISSING_CIPHER_TEXT_REASON));
}
OlmPayloadContent olmPayloadContent = JsonUtils.toOlmPayloadContent(payload);
if (TextUtils.isEmpty(olmPayloadContent.recipient)) {
String reason = String.format(MXCryptoError.ERROR_MISSING_PROPERTY_REASON, "recipient");
Log.e(LOG_TAG, "## decryptEvent() : " + reason);
throw new MXDecryptionException(new MXCryptoError(MXCryptoError.MISSING_PROPERTY_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, reason));
}
if (!TextUtils.equals(olmPayloadContent.recipient, mSession.getMyUserId())) {
Log.e(LOG_TAG, "## decryptEvent() : Event " + event.eventId + ": Intended recipient " + olmPayloadContent.recipient + " does not match our id " + mSession.getMyUserId());
throw new MXDecryptionException(new MXCryptoError(MXCryptoError.BAD_RECIPIENT_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, String.format(MXCryptoError.BAD_RECIPIENT_REASON, olmPayloadContent.recipient)));
}
if (null == olmPayloadContent.recipient_keys) {
Log.e(LOG_TAG, "## decryptEvent() : Olm event (id=" + event.eventId + ") contains no " + "'recipient_keys' property; cannot prevent unknown-key attack");
throw new MXDecryptionException(new MXCryptoError(MXCryptoError.MISSING_PROPERTY_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, String.format(MXCryptoError.ERROR_MISSING_PROPERTY_REASON, "recipient_keys")));
}
String ed25519 = olmPayloadContent.recipient_keys.get("ed25519");
if (!TextUtils.equals(ed25519, mOlmDevice.getDeviceEd25519Key())) {
Log.e(LOG_TAG, "## decryptEvent() : Event " + event.eventId + ": Intended recipient ed25519 key " + ed25519 + " did not match ours");
throw new MXDecryptionException(new MXCryptoError(MXCryptoError.BAD_RECIPIENT_KEY_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, MXCryptoError.BAD_RECIPIENT_KEY_REASON));
}
if (TextUtils.isEmpty(olmPayloadContent.sender)) {
Log.e(LOG_TAG, "## decryptEvent() : Olm event (id=" + event.eventId + ") contains no " + "'sender' property; cannot prevent unknown-key attack");
throw new MXDecryptionException(new MXCryptoError(MXCryptoError.MISSING_PROPERTY_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, String.format(MXCryptoError.ERROR_MISSING_PROPERTY_REASON, "sender")));
}
if (!TextUtils.equals(olmPayloadContent.sender, event.getSender())) {
Log.e(LOG_TAG, "Event " + event.eventId + ": original sender " + olmPayloadContent.sender + " does not match reported sender " + event.getSender());
throw new MXDecryptionException(new MXCryptoError(MXCryptoError.FORWARDED_MESSAGE_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, String.format(MXCryptoError.FORWARDED_MESSAGE_REASON, olmPayloadContent.sender)));
}
if (!TextUtils.equals(olmPayloadContent.room_id, event.roomId)) {
Log.e(LOG_TAG, "## decryptEvent() : Event " + event.eventId + ": original room " + olmPayloadContent.room_id + " does not match reported room " + event.roomId);
throw new MXDecryptionException(new MXCryptoError(MXCryptoError.BAD_ROOM_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, String.format(MXCryptoError.BAD_ROOM_REASON, olmPayloadContent.room_id)));
}
if (null == olmPayloadContent.keys) {
Log.e(LOG_TAG, "## decryptEvent failed : null keys");
throw new MXDecryptionException(new MXCryptoError(MXCryptoError.UNABLE_TO_DECRYPT_ERROR_CODE, MXCryptoError.UNABLE_TO_DECRYPT, MXCryptoError.MISSING_CIPHER_TEXT_REASON));
}
MXEventDecryptionResult result = new MXEventDecryptionResult();
result.mClearEvent = payload;
result.mSenderCurve25519Key = deviceKey;
result.mClaimedEd25519Key = olmPayloadContent.keys.get("ed25519");
return result;
}
Aggregations