-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...c/main/java/org/matrix/android/sdk/internal/crypto/model/MXInboundMegolmSessionWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2022 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.matrix.android.sdk.internal.crypto.model | ||
|
||
import org.matrix.android.sdk.api.crypto.MXCRYPTO_ALGORITHM_MEGOLM | ||
import org.matrix.android.sdk.api.extensions.tryOrNull | ||
import org.matrix.android.sdk.internal.crypto.MegolmSessionData | ||
import org.matrix.olm.OlmInboundGroupSession | ||
import timber.log.Timber | ||
|
||
data class MXInboundMegolmSessionWrapper( | ||
// olm object | ||
val session: OlmInboundGroupSession, | ||
// data about the session | ||
val sessionData: InboundGroupSessionData | ||
) { | ||
// shortcut | ||
val roomId = sessionData.roomId | ||
val senderKey = sessionData.senderKey | ||
val safeSessionId = tryOrNull("Fail to get megolm session Id") { session.sessionIdentifier() } | ||
|
||
/** | ||
* Export the inbound group session keys. | ||
* @param index the index to export. If null, the first known index will be used | ||
* @return the inbound group session as MegolmSessionData if the operation succeeds | ||
*/ | ||
internal fun exportKeys(index: Long? = null): MegolmSessionData? { | ||
return try { | ||
val keysClaimed = sessionData.keysClaimed ?: return null | ||
val wantedIndex = index ?: session.firstKnownIndex | ||
|
||
MegolmSessionData( | ||
senderClaimedEd25519Key = sessionData.keysClaimed?.get("ed25519"), | ||
forwardingCurve25519KeyChain = sessionData.forwardingCurve25519KeyChain?.toList().orEmpty(), | ||
sessionKey = session.export(wantedIndex), | ||
senderClaimedKeys = keysClaimed, | ||
roomId = sessionData.roomId, | ||
sessionId = session.sessionIdentifier(), | ||
senderKey = senderKey, | ||
algorithm = MXCRYPTO_ALGORITHM_MEGOLM, | ||
sharedHistory = sessionData.sharedHistory | ||
) | ||
} catch (e: Exception) { | ||
Timber.e(e, "## Failed to export megolm : sessionID ${tryOrNull { session.sessionIdentifier() }} failed") | ||
null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...id/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/SafeObjectInputStream.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2020 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.matrix.android.sdk.internal.crypto.store.db | ||
|
||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.io.ObjectInputStream | ||
import java.io.ObjectStreamClass | ||
|
||
/** | ||
* Package has been renamed from `im.vector.matrix.android` to `org.matrix.android.sdk` | ||
* so ensure deserialization of previously stored objects still works | ||
* | ||
* Ref: https://stackoverflow.com/questions/3884492/how-can-i-change-package-for-a-bunch-of-java-serializable-classes | ||
*/ | ||
internal class SafeObjectInputStream(inputStream: InputStream) : ObjectInputStream(inputStream) { | ||
|
||
init { | ||
enableResolveObject(true) | ||
} | ||
|
||
@Throws(IOException::class, ClassNotFoundException::class) | ||
override fun readClassDescriptor(): ObjectStreamClass { | ||
val read = super.readClassDescriptor() | ||
if (read.name.startsWith("im.vector.matrix.android.")) { | ||
return ObjectStreamClass.lookup(Class.forName(read.name.replace("im.vector.matrix.android.", "org.matrix.android.sdk."))) | ||
} | ||
return read | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
...ava/org/matrix/android/sdk/internal/crypto/store/db/model/OlmInboundGroupSessionEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2020 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.matrix.android.sdk.internal.crypto.store.db.model | ||
|
||
import io.realm.RealmObject | ||
import io.realm.annotations.PrimaryKey | ||
import org.matrix.android.sdk.internal.crypto.model.InboundGroupSessionData | ||
import org.matrix.android.sdk.internal.crypto.model.MXInboundMegolmSessionWrapper | ||
import org.matrix.android.sdk.internal.crypto.store.db.deserializeFromRealm | ||
import org.matrix.android.sdk.internal.crypto.store.db.serializeForRealm | ||
import org.matrix.android.sdk.internal.di.MoshiProvider | ||
import org.matrix.olm.OlmInboundGroupSession | ||
import timber.log.Timber | ||
|
||
internal fun OlmInboundGroupSessionEntity.Companion.createPrimaryKey(sessionId: String?, senderKey: String?) = "$sessionId|$senderKey" | ||
|
||
internal open class OlmInboundGroupSessionEntity( | ||
// Combined value to build a primary key | ||
@PrimaryKey var primaryKey: String? = null, | ||
|
||
// denormalization for faster querying (these fields are in the inboundGroupSessionDataJson) | ||
var sessionId: String? = null, | ||
var senderKey: String? = null, | ||
var roomId: String? = null, | ||
|
||
// Deprecated, used for migration / olmInboundGroupSessionData contains Json | ||
// keep it in case of problem to have a chance to recover | ||
var olmInboundGroupSessionData: String? = null, | ||
|
||
// Stores the session data in an extensible format | ||
// to allow to store data not yet supported for later use | ||
var inboundGroupSessionDataJson: String? = null, | ||
|
||
// The pickled session | ||
var serializedOlmInboundGroupSession: String? = null, | ||
|
||
// Flag that indicates whether or not the current inboundSession will be shared to | ||
// invited users to decrypt past messages | ||
var sharedHistory: Boolean = false, | ||
// Indicate if the key has been backed up to the homeserver | ||
var backedUp: Boolean = false | ||
) : | ||
RealmObject() { | ||
|
||
fun store(wrapper: MXInboundMegolmSessionWrapper) { | ||
this.serializedOlmInboundGroupSession = serializeForRealm(wrapper.session) | ||
this.inboundGroupSessionDataJson = adapter.toJson(wrapper.sessionData) | ||
this.roomId = wrapper.sessionData.roomId | ||
this.senderKey = wrapper.sessionData.senderKey | ||
this.sessionId = wrapper.session.sessionIdentifier() | ||
this.sharedHistory = wrapper.sessionData.sharedHistory | ||
} | ||
// fun getInboundGroupSession(): OlmInboundGroupSessionWrapper2? { | ||
// return try { | ||
// deserializeFromRealm<OlmInboundGroupSessionWrapper2?>(olmInboundGroupSessionData) | ||
// } catch (failure: Throwable) { | ||
// Timber.e(failure, "## Deserialization failure") | ||
// return null | ||
// } | ||
// } | ||
// | ||
// fun putInboundGroupSession(olmInboundGroupSessionWrapper: OlmInboundGroupSessionWrapper2?) { | ||
// olmInboundGroupSessionData = serializeForRealm(olmInboundGroupSessionWrapper) | ||
// } | ||
|
||
fun getOlmGroupSession(): OlmInboundGroupSession? { | ||
return try { | ||
deserializeFromRealm(serializedOlmInboundGroupSession) | ||
} catch (failure: Throwable) { | ||
Timber.e(failure, "## Deserialization failure") | ||
return null | ||
} | ||
} | ||
|
||
fun getData(): InboundGroupSessionData? { | ||
return try { | ||
inboundGroupSessionDataJson?.let { | ||
adapter.fromJson(it) | ||
} | ||
} catch (failure: Throwable) { | ||
Timber.e(failure, "## Deserialization failure") | ||
return null | ||
} | ||
} | ||
|
||
fun toModel(): MXInboundMegolmSessionWrapper? { | ||
val data = getData() ?: return null | ||
val session = getOlmGroupSession() ?: return null | ||
return MXInboundMegolmSessionWrapper( | ||
session = session, | ||
sessionData = data | ||
) | ||
} | ||
|
||
companion object { | ||
private val adapter = MoshiProvider.providesMoshi() | ||
.adapter(InboundGroupSessionData::class.java) | ||
} | ||
} |