Commit 307309c6 authored by Hubert Chathi's avatar Hubert Chathi

add initial version of Android wrapper for public key API

parent 552da6ea
/*
* Copyright 2018 New Vector Ltd
*
* 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.olm;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OlmPkTest {
private static final String LOG_TAG = "OlmPkEncryptionTest";
private static OlmPkEncryption mOlmPkEncryption;
private static OlmPkDecryption mOlmPkDecryption;
@Test
public void test01EncryptAndDecrypt() {
try {
mOlmPkEncryption = new OlmPkEncryption();
} catch (OlmException e) {
e.printStackTrace();
assertTrue("OlmPkEncryption failed " + e.getMessage(), false);
}
try {
mOlmPkDecryption = new OlmPkDecryption();
} catch (OlmException e) {
e.printStackTrace();
assertTrue("OlmPkEncryption failed " + e.getMessage(), false);
}
assertNotNull(mOlmPkEncryption);
assertNotNull(mOlmPkDecryption);
String key = null;
try {
key = mOlmPkDecryption.generateKey();
} catch (OlmException e) {
assertTrue("Exception in generateKey, Exception code=" + e.getExceptionCode(), false);
}
Log.d(LOG_TAG, "Ephemeral Key: " + key);
try {
mOlmPkEncryption.setRecipientKey(key);
} catch (OlmException e) {
assertTrue("Exception in setRecipientKey, Exception code=" + e.getExceptionCode(), false);
}
String clearMessage = "Public key test";
OlmPkMessage message = null;
try {
message = mOlmPkEncryption.encrypt(clearMessage);
} catch (OlmException e) {
assertTrue("Exception in encrypt, Exception code=" + e.getExceptionCode(), false);
}
Log.d(LOG_TAG, "message: " + message.mCipherText + " " + message.mMac + " " + message.mEphemeralKey);
String decryptedMessage = null;
try {
decryptedMessage = mOlmPkDecryption.decrypt(message);
} catch (OlmException e) {
assertTrue("Exception in decrypt, Exception code=" + e.getExceptionCode(), false);
}
assertTrue(clearMessage.equals(decryptedMessage));
mOlmPkEncryption.releaseEncryption();
mOlmPkDecryption.releaseDecryption();
assertTrue(mOlmPkEncryption.isReleased());
assertTrue(mOlmPkDecryption.isReleased());
}
}
......@@ -61,6 +61,14 @@ public class OlmException extends IOException {
public static final int EXCEPTION_CODE_UTILITY_CREATION = 500;
public static final int EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE = 501;
public static final int EXCEPTION_CODE_PK_ENCRYPTION_CREATION = 600;
public static final int EXCEPTION_CODE_PK_ENCRYPTION_SET_RECIPIENT_KEY = 601;
public static final int EXCEPTION_CODE_PK_ENCRYPTION_ENCRYPT = 602;
public static final int EXCEPTION_CODE_PK_DECRYPTION_CREATION = 700;
public static final int EXCEPTION_CODE_PK_DECRYPTION_GENERATE_KEY = 701;
public static final int EXCEPTION_CODE_PK_DECRYPTION_DECRYPT = 702;
// exception human readable messages
public static final String EXCEPTION_MSG_INVALID_PARAMS_DESERIALIZATION = "invalid de-serialized parameters";
......
/*
* Copyright 2018 New Vector Ltd
*
* 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.olm;
import android.util.Log;
public class OlmPkDecryption {
private static final String LOG_TAG = "OlmPkDecryption";
/** Session Id returned by JNI.
* This value uniquely identifies the native session instance.
**/
private transient long mNativeId;
public OlmPkDecryption() throws OlmException {
try {
mNativeId = createNewPkDecryptionJni();
} catch (Exception e) {
throw new OlmException(OlmException.EXCEPTION_CODE_PK_DECRYPTION_CREATION, e.getMessage());
}
}
private native long createNewPkDecryptionJni();
private native void releasePkDecryptionJni();
public void releaseDecryption() {
if (0 != mNativeId) {
releasePkDecryptionJni();
}
mNativeId = 0;
}
public boolean isReleased() {
return (0 == mNativeId);
}
public String generateKey() throws OlmException {
try {
byte[] key = generateKeyJni();
return new String(key, "UTF-8");
} catch (Exception e) {
Log.e(LOG_TAG, "## setRecipientKey(): failed " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_PK_DECRYPTION_GENERATE_KEY, e.getMessage());
}
}
private native byte[] generateKeyJni();
public String decrypt(OlmPkMessage aMessage) throws OlmException {
if (null == aMessage) {
return null;
}
try {
return new String(decryptJni(aMessage), "UTF-8");
} catch (Exception e) {
Log.e(LOG_TAG, "## pkDecrypt(): failed " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_PK_DECRYPTION_DECRYPT, e.getMessage());
}
}
private native byte[] decryptJni(OlmPkMessage aMessage);
}
/*
* Copyright 2018 New Vector Ltd
*
* 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.olm;
import android.util.Log;
public class OlmPkEncryption {
private static final String LOG_TAG = "OlmPkEncryption";
/** Session Id returned by JNI.
* This value uniquely identifies the native session instance.
**/
private transient long mNativeId;
public OlmPkEncryption() throws OlmException {
try {
mNativeId = createNewPkEncryptionJni();
} catch (Exception e) {
throw new OlmException(OlmException.EXCEPTION_CODE_PK_ENCRYPTION_CREATION, e.getMessage());
}
}
private native long createNewPkEncryptionJni();
private native void releasePkEncryptionJni();
public void releaseEncryption() {
if (0 != mNativeId) {
releasePkEncryptionJni();
}
mNativeId = 0;
}
public boolean isReleased() {
return (0 == mNativeId);
}
public void setRecipientKey(String aKey) throws OlmException {
if (null == aKey) {
return;
}
try {
setRecipientKeyJni(aKey.getBytes("UTF-8"));
} catch (Exception e) {
Log.e(LOG_TAG, "## setRecipientKey(): failed " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_PK_ENCRYPTION_SET_RECIPIENT_KEY, e.getMessage());
}
}
private native void setRecipientKeyJni(byte[] aKey);
public OlmPkMessage encrypt(String aPlaintext) throws OlmException {
if (null == aPlaintext) {
return null;
}
OlmPkMessage encryptedMsgRetValue = new OlmPkMessage();
try {
byte[] ciphertextBuffer = encryptJni(aPlaintext.getBytes("UTF-8"), encryptedMsgRetValue);
if (null != ciphertextBuffer) {
encryptedMsgRetValue.mCipherText = new String(ciphertextBuffer, "UTF-8");
}
} catch (Exception e) {
Log.e(LOG_TAG, "## pkEncrypt(): failed " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_PK_ENCRYPTION_ENCRYPT, e.getMessage());
}
return encryptedMsgRetValue;
}
private native byte[] encryptJni(byte[] plaintext, OlmPkMessage aMessage);
}
/*
* Copyright 2018 New Vector Ltd
*
* 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.olm;
public class OlmPkMessage {
public String mCipherText;
public String mMac;
public String mEphemeralKey;
}
......@@ -40,6 +40,7 @@ $(SRC_ROOT_DIR)/src/pickle.cpp \
$(SRC_ROOT_DIR)/src/ratchet.cpp \
$(SRC_ROOT_DIR)/src/session.cpp \
$(SRC_ROOT_DIR)/src/utility.cpp \
$(SRC_ROOT_DIR)/src/pk.cpp \
$(SRC_ROOT_DIR)/src/ed25519.c \
$(SRC_ROOT_DIR)/src/error.c \
$(SRC_ROOT_DIR)/src/inbound_group_session.c \
......@@ -55,7 +56,8 @@ olm_jni_helper.cpp \
olm_inbound_group_session.cpp \
olm_outbound_group_session.cpp \
olm_utility.cpp \
olm_manager.cpp
olm_manager.cpp \
olm_pk.cpp
LOCAL_LDLIBS := -llog
......
......@@ -70,6 +70,8 @@ struct OlmAccount* getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
struct OlmInboundGroupSession* getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
struct OlmOutboundGroupSession* getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
struct OlmUtility* getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
struct OlmPkDecryption* getPkDecryptionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
struct OlmPkEncryption* getPkEncryptionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
#ifdef __cplusplus
}
......
......@@ -212,3 +212,13 @@ struct OlmUtility* getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{
return (struct OlmUtility*)getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_UTILITY);
}
struct OlmPkDecryption* getPkDecryptionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{
return (struct OlmPkDecryption*)getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_PK_DECRYPTION);
}
struct OlmPkEncryption* getPkEncryptionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{
return (struct OlmPkEncryption*)getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_PK_ENCRYPTION);
}
......@@ -25,4 +25,6 @@ namespace AndroidOlmSdk
static const char *CLASS_OLM_SESSION = "org/matrix/olm/OlmSession";
static const char *CLASS_OLM_ACCOUNT = "org/matrix/olm/OlmAccount";
static const char *CLASS_OLM_UTILITY = "org/matrix/olm/OlmUtility";
static const char *CLASS_OLM_PK_ENCRYPTION = "org/matrix/olm/OlmPkEncryption";
static const char *CLASS_OLM_PK_DECRYPTION = "org/matrix/olm/OlmPkDecryption";
}
This diff is collapsed.
/*
* Copyright 2018 New Vector Ltd
*
* 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.
*/
#ifndef _OMLPK_H
#define _OMLPK_H
#include "olm_jni.h"
#include "olm/pk.h"
#define OLM_PK_ENCRYPTION_FUNC_DEF(func_name) FUNC_DEF(OlmPkEncryption,func_name)
#define OLM_PK_DECRYPTION_FUNC_DEF(func_name) FUNC_DEF(OlmPkDecryption,func_name)
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jlong OLM_PK_ENCRYPTION_FUNC_DEF(createNewPkEncryptionJni)(JNIEnv *env, jobject thiz);
JNIEXPORT void OLM_PK_ENCRYPTION_FUNC_DEF(releasePkEncryptionJni)(JNIEnv *env, jobject thiz);
JNIEXPORT void OLM_PK_ENCRYPTION_FUNC_DEF(setRecipientKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer);
JNIEXPORT jbyteArray OLM_PK_ENCRYPTION_FUNC_DEF(encryptJni)(JNIEnv *env, jobject thiz, jbyteArray aPlaintextBuffer, jobject aEncryptedMsg);
JNIEXPORT jlong OLM_PK_DECRYPTION_FUNC_DEF(createNewPkDecryptionJni)(JNIEnv *env, jobject thiz);
JNIEXPORT void OLM_PK_DECRYPTION_FUNC_DEF(releasePkDecryptionJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(generateKeyJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(decryptJni)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg);
#ifdef __cplusplus
}
#endif
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment