From f647747d279496b1db53431fee99069ed9871ac8 Mon Sep 17 00:00:00 2001 From: valere Date: Thu, 2 Dec 2021 09:54:42 +0100 Subject: [PATCH] fallback key java bindings --- .../main/java/org/matrix/olm/OlmAccount.java | 18 +++++++++++++ .../java/org/matrix/olm/OlmException.java | 1 + android/olm-sdk/src/main/jni/olm_account.cpp | 27 +++++++++++++++++-- android/olm-sdk/src/main/jni/olm_account.h | 1 + 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java index 0b4c567..3e8c63e 100644 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java +++ b/android/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java @@ -493,4 +493,22 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { } private native byte[] fallbackKeyJni(); + + + /** + * Forget about the old fallback key. + * + * This should be called once you are reasonably certain that you will not + * receive any more messages that use the old fallback key (e.g. 5 minutes + * after the new fallback key has been published). + **/ + public void forgetFallbackKey() throws OlmException { + try { + forgetFallbackKeyJni(); + } catch (Exception e) { + throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_FORGET_FALLBACK_KEY, e.getMessage()); + } + } + + private native void forgetFallbackKeyJni(); } diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmException.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmException.java index 0739e9c..26da005 100644 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmException.java +++ b/android/olm-sdk/src/main/java/org/matrix/olm/OlmException.java @@ -37,6 +37,7 @@ public class OlmException extends IOException { public static final int EXCEPTION_CODE_ACCOUNT_SIGN_MESSAGE = 107; public static final int EXCEPTION_CODE_ACCOUNT_GENERATE_FALLBACK_KEY = 108; public static final int EXCEPTION_CODE_ACCOUNT_FALLBACK_KEY = 109; + public static final int EXCEPTION_CODE_ACCOUNT_FORGET_FALLBACK_KEY = 110; public static final int EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION = 200; public static final int EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION = 201; diff --git a/android/olm-sdk/src/main/jni/olm_account.cpp b/android/olm-sdk/src/main/jni/olm_account.cpp index 2be982d..d3a42e1 100644 --- a/android/olm-sdk/src/main/jni/olm_account.cpp +++ b/android/olm-sdk/src/main/jni/olm_account.cpp @@ -521,7 +521,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(fallbackKeyJni)(JNIEnv *env, jobject t else { // keys memory allocation - size_t keysLength = olm_account_fallback_key_length(accountPtr); + size_t keysLength = olm_account_unpublished_fallback_key_length(accountPtr); uint8_t *keysBytesPtr = (uint8_t *)malloc(keysLength*sizeof(uint8_t)); if (!keysBytesPtr) @@ -532,7 +532,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(fallbackKeyJni)(JNIEnv *env, jobject t else { // retrieve key pairs in keysBytesPtr - size_t keysResult = olm_account_fallback_key(accountPtr, keysBytesPtr, keysLength); + size_t keysResult = olm_account_unpublished_fallback_key(accountPtr, keysBytesPtr, keysLength); if (keysResult == olm_error()) { LOGE("## fallbackKeyJni(): failure - error getting fallback key Msg=%s",(const char *)olm_account_last_error(accountPtr)); @@ -567,6 +567,29 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(fallbackKeyJni)(JNIEnv *env, jobject t return byteArrayRetValue; } + +/** + * Forget about the old fallback key. + * + * This should be called once you are reasonably certain that you will not + * receive any more messages that use the old fallback key (e.g. 5 minutes + * after the new fallback key has been published). + **/ +JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(forgetFallbackKeyJni)(JNIEnv *env, jobject thiz) +{ + OlmAccount *accountPtr = getAccountInstanceId(env, thiz); + + if (!accountPtr) + { + LOGE("## forgetFallbackKeyJni(): failure - invalid Account ptr"); + errorMessage = "invalid Account ptr"; + } + else + { + olm_account_forget_old_fallback_key(accountPtr) + } +} + /** * Sign a message with the ed25519 key (fingerprint) for this account.
* The signed message is returned by the function. diff --git a/android/olm-sdk/src/main/jni/olm_account.h b/android/olm-sdk/src/main/jni/olm_account.h index caf83c6..09053c8 100644 --- a/android/olm-sdk/src/main/jni/olm_account.h +++ b/android/olm-sdk/src/main/jni/olm_account.h @@ -45,6 +45,7 @@ JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, // fallback keys JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(generateFallbackKeyJni)(JNIEnv *env, jobject thiz); JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(fallbackKeyJni)(JNIEnv *env, jobject thiz); +JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(forgetFallbackKeyJni)(JNIEnv *env, jobject thiz); // signing JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage);