diff --git a/src/wrappers/themis/react-native-themis/android/src/main/java/com/reactnativethemis/ThemisModule.java b/src/wrappers/themis/react-native-themis/android/src/main/java/com/reactnativethemis/ThemisModule.java
index 24e70e4c1..486195a55 100644
--- a/src/wrappers/themis/react-native-themis/android/src/main/java/com/reactnativethemis/ThemisModule.java
+++ b/src/wrappers/themis/react-native-themis/android/src/main/java/com/reactnativethemis/ThemisModule.java
@@ -17,7 +17,6 @@
import com.cossacklabs.themis.SecureMessage;
import com.cossacklabs.themis.SecureCompare;
-
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
@@ -33,11 +32,7 @@
import java.util.Map;
import java.util.UUID;
-
-
-
public class ThemisModule extends ReactContextBaseJavaModule {
-
public static class ByteOverflowException extends Exception {
public ByteOverflowException(String errorMessage) {
super(errorMessage);
@@ -91,6 +86,7 @@ private static int compareResultSerialize(SecureCompare.CompareResult result)
default: return comparator_error;
}
}
+
private static WritableArray dataSerialize(byte[] data)
{
if (data == null) {
@@ -108,7 +104,7 @@ private static byte[] dataDeserialize(ReadableArray serializedData) throws ByteO
}
byte[] data = new byte[serializedData.size()];
for (int i = 0; i < serializedData.size(); i++) {
- if(serializedData.getInt(i) >= -128 && serializedData.getInt(i) <= 255) {
+ if (serializedData.getInt(i) >= 0 && serializedData.getInt(i) <= 255) {
byte j = (byte) serializedData.getInt(i);
data[i] = j;
} else {
@@ -163,355 +159,286 @@ public void symmetricKey(Callback callback)
@ReactMethod
public void secureCellSealWithSymmetricKeyEncrypt(ReadableArray symmetricKey,
- String plaintext,
- String context,
- Callback successCallback,
- Callback errorCallback)
+ String plaintext,
+ String context,
+ Callback successCallback,
+ Callback errorCallback)
{
- SecureCell.Seal cell = null;
try {
- cell = SecureCell.SealWithKey(dataDeserialize(symmetricKey));
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
- return;
+ byte[] symmetricKeyBinary = dataDeserialize(symmetricKey);
+ SecureCell.Seal cell = SecureCell.SealWithKey(symmetricKeyBinary);
+ byte[] plaintextBinary = plaintext.getBytes(StandardCharsets.UTF_8);
+ byte[] contextBinary = context.getBytes(StandardCharsets.UTF_8);
+ byte[] encrypted = cell.encrypt(plaintextBinary, contextBinary);
+ WritableArray response = dataSerialize(encrypted);
+ successCallback.invoke(response);
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
}
-
- byte[] txt = plaintext.getBytes(StandardCharsets.UTF_8);
- byte[] ctx = context.getBytes(StandardCharsets.UTF_8);
- byte[] encrypted = cell.encrypt(txt, ctx);
- WritableArray response = dataSerialize(encrypted);
- successCallback.invoke(response);
}
@ReactMethod
public void secureCellSealWithSymmetricKeyDecrypt(ReadableArray symmetricKey,
- ReadableArray encrypted,
- String context,
- Callback successCallback,
- Callback errorCallback)
+ ReadableArray encrypted,
+ String context,
+ Callback successCallback,
+ Callback errorCallback)
{
- SecureCell.Seal cell = null;
try {
- cell = SecureCell.SealWithKey(dataDeserialize(symmetricKey));
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
- return;
- }
-
- byte[] enc;
- try {
- enc = dataDeserialize(encrypted);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
- return;
- }
-
- byte[] ctx = context.getBytes(StandardCharsets.UTF_8);
- try {
- byte[] decrypted = cell.decrypt(enc, ctx);
+ byte[] symmetricKeyBinary = dataDeserialize(symmetricKey);
+ SecureCell.Seal cell = SecureCell.SealWithKey(symmetricKeyBinary);
+ byte[] encryptedBinary = dataDeserialize(encrypted);
+ byte[] contextBinary = context.getBytes(StandardCharsets.UTF_8);
+ byte[] decrypted = cell.decrypt(encryptedBinary, contextBinary);
WritableArray response = dataSerialize(decrypted);
successCallback.invoke(response);
- } catch (SecureCellException e) {
- errorCallback.invoke(e.getMessage());
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
}
-
}
@ReactMethod
public void secureCellSealWithPassphraseEncrypt(String passphrase,
- String plaintext,
- String context,
- Callback callback)
+ String plaintext,
+ String context,
+ Callback successCallback,
+ Callback errorCallback)
{
- SecureCell.Seal cell = SecureCell.SealWithPassphrase(passphrase);
- byte[] txt = plaintext.getBytes(StandardCharsets.UTF_8);
- byte[] ctx = context.getBytes(StandardCharsets.UTF_8);
- byte[] encrypted = cell.encrypt(txt, ctx);
- WritableArray response = dataSerialize(encrypted);
- callback.invoke(response);
+ try {
+ SecureCell.Seal cell = SecureCell.SealWithPassphrase(passphrase);
+ byte[] plaintextBinary = plaintext.getBytes(StandardCharsets.UTF_8);
+ byte[] contextBinary = context.getBytes(StandardCharsets.UTF_8);
+ byte[] encrypted = cell.encrypt(plaintextBinary, contextBinary);
+ WritableArray response = dataSerialize(encrypted);
+ successCallback.invoke(response);
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
+ }
}
@ReactMethod
public void secureCellSealWithPassphraseDecrypt(String passphrase,
- ReadableArray encrypted,
- String context,
- Callback successCallback,
- Callback errorCallback)
+ ReadableArray encrypted,
+ String context,
+ Callback successCallback,
+ Callback errorCallback)
{
- SecureCell.Seal cell = SecureCell.SealWithPassphrase(passphrase);
- byte[] enc;
- try {
- enc = dataDeserialize(encrypted);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
- return;
- }
- byte[] ctx = context.getBytes(StandardCharsets.UTF_8);
try {
- byte[] decrypted = cell.decrypt(enc, ctx);
+ SecureCell.Seal cell = SecureCell.SealWithPassphrase(passphrase);
+ byte[] encryptedBinary = dataDeserialize(encrypted);
+ byte[] contextBinary = context.getBytes(StandardCharsets.UTF_8);
+ byte[] decrypted = cell.decrypt(encryptedBinary, contextBinary);
WritableArray response = dataSerialize(decrypted);
successCallback.invoke(response);
- } catch (SecureCellException e) {
- errorCallback.invoke(e.getMessage());
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
}
}
/* MARK: Token protect mode */
@ReactMethod
public void secureCellTokenProtectEncrypt(ReadableArray symmetricKey,
- String plaintext,
- String context,
- Callback successCallback,
- Callback errorCallback)
+ String plaintext,
+ String context,
+ Callback successCallback,
+ Callback errorCallback)
{
- byte[] bkey;
try {
- bkey = dataDeserialize(symmetricKey);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
- return;
+ byte[] symmetricKeyBinary = dataDeserialize(symmetricKey);
+ SecureCell.TokenProtect cell = SecureCell.TokenProtectWithKey(symmetricKeyBinary);
+ byte[] plaintextBinary = plaintext.getBytes(StandardCharsets.UTF_8);
+ byte[] contextBinary = context.getBytes(StandardCharsets.UTF_8);
+ SecureCellData result = cell.encrypt(plaintextBinary, contextBinary);
+ byte[] encrypted = result.getProtectedData();
+ byte[] authToken = result.getAdditionalData();
+ WritableMap response = new WritableNativeMap();
+ response.putArray("encrypted", dataSerialize(encrypted));
+ response.putArray("token", dataSerialize(authToken));
+ successCallback.invoke(response);
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
}
- SecureCell.TokenProtect cell = SecureCell.TokenProtectWithKey(bkey);
- byte[] txt = plaintext.getBytes(StandardCharsets.UTF_8);
- byte[] ctx = context.getBytes(StandardCharsets.UTF_8);
- SecureCellData result = cell.encrypt(txt, ctx);
- byte[] encrypted = result.getProtectedData();
- byte[] authToken = result.getAdditionalData();
-
- WritableMap response = new WritableNativeMap();
- response.putArray("encrypted", dataSerialize(encrypted));
- response.putArray("token", dataSerialize(authToken));
-
- successCallback.invoke(response);
}
@ReactMethod
public void secureCellTokenProtectDecrypt(ReadableArray symmetricKey,
- ReadableArray encrypted,
- ReadableArray token,
- String context,
- Callback successCallback,
- Callback errorCallback)
+ ReadableArray encrypted,
+ ReadableArray token,
+ String context,
+ Callback successCallback,
+ Callback errorCallback)
{
- byte[] bkey;
- byte[] enc;
- byte[] tkn;
- try {
- bkey = dataDeserialize(symmetricKey);
- enc = dataDeserialize(encrypted);
- tkn = dataDeserialize(token);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
- return;
- }
- SecureCell.TokenProtect cell = SecureCell.TokenProtectWithKey(bkey);
- byte[] ctx = context.getBytes(StandardCharsets.UTF_8);
-
try {
- byte[] decrypted = cell.decrypt(enc, tkn, ctx);
+ byte[] symmetricKeyBinary = dataDeserialize(symmetricKey);
+ byte[] encryptedBinary = dataDeserialize(encrypted);
+ byte[] tokenBinary = dataDeserialize(token);
+ SecureCell.TokenProtect cell = SecureCell.TokenProtectWithKey(symmetricKeyBinary);
+ byte[] contextBinary = context.getBytes(StandardCharsets.UTF_8);
+ byte[] decrypted = cell.decrypt(encryptedBinary, tokenBinary, contextBinary);
WritableArray response = dataSerialize(decrypted);
successCallback.invoke(response);
- } catch (SecureCellException e) {
- errorCallback.invoke(e.getMessage());
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
}
}
/* Context imprint mode */
-
@ReactMethod
public void secureCellContextImprintEncrypt(ReadableArray symmetricKey,
- String plaintext,
- String context,
- Callback successCallback,
- Callback errorCallback)
+ String plaintext,
+ String context,
+ Callback successCallback,
+ Callback errorCallback)
{
if (context == null || context.length() == 0) {
- errorCallback.invoke(contextRequired);
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", contextRequired);
+ errorCallback.invoke(error);
return;
}
-
- byte[] bkey;
try {
- bkey = dataDeserialize(symmetricKey);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
- return;
+ byte[] symmetricKeyBinary = dataDeserialize(symmetricKey);
+ SecureCell.ContextImprint cell = SecureCell.ContextImprintWithKey(symmetricKeyBinary);
+ byte[] plaintextBinary = plaintext.getBytes(StandardCharsets.UTF_8);
+ byte[] contextBinary = context.getBytes(StandardCharsets.UTF_8);
+ byte[] encrypted = cell.encrypt(plaintextBinary, contextBinary);
+ WritableArray response = dataSerialize(encrypted);
+ successCallback.invoke(response);
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
}
- SecureCell.ContextImprint cell = SecureCell.ContextImprintWithKey(bkey);
-
- byte[] txt = plaintext.getBytes(StandardCharsets.UTF_8);
- byte[] ctx = context.getBytes(StandardCharsets.UTF_8);
- byte[] encrypted = cell.encrypt(txt, ctx);
-
- WritableArray response = dataSerialize(encrypted);
- successCallback.invoke(response);
}
@ReactMethod
public void secureCellContextImprintDecrypt(ReadableArray symmetricKey,
- ReadableArray encrypted,
- String context,
- Callback successCallback,
- Callback errorCallback)
+ ReadableArray encrypted,
+ String context,
+ Callback successCallback,
+ Callback errorCallback)
{
if (context == null || context.length() == 0) {
- errorCallback.invoke(contextRequired);
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", contextRequired);
+ errorCallback.invoke(error);
return;
}
-
- byte[] bkey;
- byte[] enc;
try {
- bkey = dataDeserialize(symmetricKey);
- enc = dataDeserialize(encrypted);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
- return;
+ byte[] symmetricKeyBinary = dataDeserialize(symmetricKey);
+ byte[] encryptedBinary = dataDeserialize(encrypted);
+ SecureCell.ContextImprint cell = SecureCell.ContextImprintWithKey(symmetricKeyBinary);
+ byte[] contextBinary = context.getBytes(StandardCharsets.UTF_8);
+ byte[] decrypted = cell.decrypt(encryptedBinary, contextBinary);
+ WritableArray response = dataSerialize(decrypted);
+ successCallback.invoke(response);
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
}
- SecureCell.ContextImprint cell = SecureCell.ContextImprintWithKey(bkey);
-
- byte[] ctx = context.getBytes(StandardCharsets.UTF_8);
- byte[] decrypted = cell.decrypt(enc, ctx);
-
- WritableArray response = dataSerialize(decrypted);
- successCallback.invoke(response);
}
/* Secure Message */
@ReactMethod
public void secureMessageSign(String message,
- ReadableArray privateKey,
- ReadableArray publicKey,
- Callback successCallback,
- Callback errorCallback)
+ ReadableArray privateKey,
+ Callback successCallback,
+ Callback errorCallback)
{
if (privateKey == null || privateKey.size() == 0) {
- errorCallback.invoke(privateKeyRequired);
- return;
- }
- byte[] bkey;
- try {
- bkey = dataDeserialize(privateKey);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", privateKeyRequired);
+ errorCallback.invoke(error);
return;
}
- PrivateKey pvtKey = new PrivateKey(bkey);
- SecureMessage secureMessage;
- byte[] msg = message.getBytes(StandardCharsets.UTF_8);
-
- if (publicKey == null ) {
- secureMessage = new SecureMessage(pvtKey);
- } else {
- try {
- bkey = dataDeserialize(publicKey);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
- return;
- }
- PublicKey pubKey = new PublicKey(bkey);
- secureMessage = new SecureMessage(pvtKey,pubKey);
- }
-
try {
- byte[] signedMessage = secureMessage.sign(msg);
+ byte[] privateKeyBinary = dataDeserialize(privateKey);
+ PrivateKey keyPrivateKey = new PrivateKey(privateKeyBinary);
+ byte[] messageBinary = message.getBytes(StandardCharsets.UTF_8);
+ SecureMessage secureMessage = new SecureMessage(keyPrivateKey);
+ byte[] signedMessage = secureMessage.sign(messageBinary);
WritableArray response = dataSerialize(signedMessage);
successCallback.invoke(response);
- } catch (NullArgumentException | SecureMessageWrapException e) {
- errorCallback.invoke(e.getMessage());
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
}
-
}
@ReactMethod
public void secureMessageVerify(ReadableArray message,
- ReadableArray privateKey,
ReadableArray publicKey,
- Callback successCallback,
- Callback errorCallback)
+ Callback successCallback,
+ Callback errorCallback)
{
-
if (publicKey == null || publicKey.size() == 0) {
- errorCallback.invoke(publicKeyRequired);
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", publicKeyRequired);
+ errorCallback.invoke(error);
return;
}
-
- byte[] bkey;
- byte[] msg;
- PrivateKey pvtKey;
- PublicKey pubKey;
try {
- bkey = dataDeserialize(publicKey);
- pubKey = new PublicKey(bkey);
- msg = dataDeserialize(message);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
- return;
- }
-
- SecureMessage secureMessage;
-
- if ( privateKey == null || privateKey.size() == 0) {
- secureMessage = new SecureMessage(pubKey);
- } else {
- try {
- bkey = dataDeserialize(privateKey);
- pvtKey = new PrivateKey(bkey);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
- return;
- }
-
- secureMessage = new SecureMessage(pvtKey, pubKey);
- }
- try {
- byte[] verifiedMessage = secureMessage.verify(msg, pubKey);
+ byte[] publicKeyBinary = dataDeserialize(publicKey);
+ PublicKey keyPublicKey = new PublicKey(publicKeyBinary);
+ byte[] messageBinary = dataDeserialize(message);
+ SecureMessage secureMessage = new SecureMessage(keyPublicKey);
+ byte[] verifiedMessage = secureMessage.verify(messageBinary, keyPublicKey);
WritableArray response = dataSerialize(verifiedMessage);
successCallback.invoke(response);
- } catch (NullArgumentException | SecureMessageWrapException e) {
- errorCallback.invoke(e.getMessage());
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
}
}
@ReactMethod
public void secureMessageEncrypt(String message,
- ReadableArray privateKey,
- ReadableArray publicKey,
- Callback successCallback,
- Callback errorCallback)
+ ReadableArray privateKey,
+ ReadableArray publicKey,
+ Callback successCallback,
+ Callback errorCallback)
{
-
- if ( privateKey == null || privateKey.size() == 0) {
- errorCallback.invoke(privateKeyRequired);
- return;
- }
- if ( publicKey == null || publicKey.size() == 0 ) {
- errorCallback.invoke(publicKeyRequired);
+ if (privateKey == null || privateKey.size() == 0) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", privateKeyRequired);
+ errorCallback.invoke(error);
return;
}
-
- byte[] bkey;
- PrivateKey pvtKey;
- PublicKey pubKey;
- try {
- bkey = dataDeserialize(privateKey);
- pvtKey = new PrivateKey(bkey);
- bkey = dataDeserialize(publicKey);
- pubKey = new PublicKey(bkey);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
+ if (publicKey == null || publicKey.size() == 0) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", publicKeyRequired);
+ errorCallback.invoke(error);
return;
}
-
- byte[] msg = message.getBytes(StandardCharsets.UTF_8);
-
- SecureMessage secureMessage = new SecureMessage(pvtKey, pubKey);
try {
- byte[] encrypted = secureMessage.wrap(msg, pubKey);
- WritableArray response = dataSerialize(encrypted);
+ byte[] privateKeyBinary = dataDeserialize(privateKey);
+ PrivateKey keyPrivateKey = new PrivateKey(privateKeyBinary);
+ byte[] publicKeyBinary = dataDeserialize(publicKey);
+ PublicKey keyPublicKey = new PublicKey(publicKeyBinary);
+ byte[] messageBinary = message.getBytes(StandardCharsets.UTF_8);
+ SecureMessage secureMessage = new SecureMessage(keyPrivateKey, keyPublicKey);
+ byte[] encryptedMessage = secureMessage.wrap(messageBinary);
+ WritableArray response = dataSerialize(encryptedMessage);
successCallback.invoke(response);
- } catch (NullArgumentException | SecureMessageWrapException e) {
- errorCallback.invoke(e.getMessage());
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
}
}
@@ -519,65 +446,59 @@ public void secureMessageEncrypt(String message,
public void secureMessageDecrypt(ReadableArray message,
ReadableArray privateKey,
ReadableArray publicKey,
- Callback successCallback,
- Callback errorCallback)
+ Callback successCallback,
+ Callback errorCallback)
{
-
- if ( privateKey == null || privateKey.size() == 0) {
- errorCallback.invoke(privateKeyRequired);
- return;
- }
- if ( publicKey == null || publicKey.size() == 0 ) {
- errorCallback.invoke(publicKeyRequired);
+ if (privateKey == null || privateKey.size() == 0) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", privateKeyRequired);
+ errorCallback.invoke(error);
return;
}
-
- byte[] bkey;
- byte[] msg;
- PrivateKey pvtKey;
- PublicKey pubKey;
- try {
- bkey = dataDeserialize(privateKey);
- pvtKey = new PrivateKey(bkey);
- bkey = dataDeserialize(publicKey);
- pubKey = new PublicKey(bkey);
- msg = dataDeserialize(message);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
+ if (publicKey == null || publicKey.size() == 0) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", publicKeyRequired);
+ errorCallback.invoke(error);
return;
}
-
- SecureMessage secureMessage = new SecureMessage(pvtKey, pubKey);
try {
- byte[] decrypted = secureMessage.unwrap(msg, pubKey);
+ byte[] privateKeyBinary = dataDeserialize(privateKey);
+ PrivateKey keyPrivateKey = new PrivateKey(privateKeyBinary);
+ byte[] publicKeyBinary = dataDeserialize(publicKey);
+ PublicKey keyPublicKey = new PublicKey(publicKeyBinary);
+ byte[] messageBinary = dataDeserialize(message);
+ SecureMessage secureMessage = new SecureMessage(keyPrivateKey, keyPublicKey);
+ byte[] decrypted = secureMessage.unwrap(messageBinary, keyPublicKey);
WritableArray response = dataSerialize(decrypted);
successCallback.invoke(response);
- } catch (NullArgumentException | SecureMessageWrapException e) {
- errorCallback.invoke(e.getMessage());
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
}
}
@ReactMethod
public void initComparator(ReadableArray sharedSecret,
- Callback successCallback,
- Callback errorCallback)
+ Callback successCallback,
+ Callback errorCallback)
{
- byte[] sharedSecretData;
try {
- sharedSecretData = dataDeserialize(sharedSecret);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
- return;
+ byte[] sharedSecretData = dataDeserialize(sharedSecret);
+ SecureCompare cmp = new SecureCompare(sharedSecretData);
+ final String uuid = UUID.randomUUID().toString();
+ cmprs.put(uuid, cmp);
+ successCallback.invoke(uuid);
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
}
- SecureCompare cmp = new SecureCompare(sharedSecretData);
- final String uuid = UUID.randomUUID().toString();
- cmprs.put(uuid, cmp);
- successCallback.invoke(uuid);
}
@ReactMethod
public void statusOfComparator(String uuid,
- Callback callback)
+ Callback callback)
{
SecureCompare cmp = cmprs.get(uuid);
if (cmp == null) {
@@ -591,8 +512,8 @@ public void statusOfComparator(String uuid,
@ReactMethod
public void beginCompare(String uuid,
- Callback successCallback,
- Callback errorCallback)
+ Callback successCallback,
+ Callback errorCallback)
{
SecureCompare cmp = cmprs.get(uuid);
if (cmp == null) {
@@ -606,24 +527,17 @@ public void beginCompare(String uuid,
@ReactMethod
public void proceedCompare(String uuid,
- ReadableArray previous,
- Callback successCallback,
- Callback errorCallback)
+ ReadableArray previous,
+ Callback successCallback,
+ Callback errorCallback)
{
SecureCompare cmp = cmprs.get(uuid);
if (cmp == null) {
- errorCallback.invoke(comparator_error);
- return;
- }
-
- byte[] data;
- try {
- data = dataDeserialize(previous);
- } catch (ByteOverflowException e) {
- errorCallback.invoke(e.getMessage());
- return;
+ errorCallback.invoke(comparator_error);
+ return;
}
try {
+ byte[] data = dataDeserialize(previous);
data = cmp.proceed(data);
WritableArray response = dataSerialize(data);
SecureCompare.CompareResult result = cmp.getResult();
@@ -632,9 +546,10 @@ public void proceedCompare(String uuid,
cmprs.remove(uuid);
}
successCallback.invoke(response, status);
- } catch (SecureCompareException e) {
- errorCallback.invoke(e.getMessage());
+ } catch (Exception e) {
+ WritableMap error = new WritableNativeMap();
+ error.putString("message", e.toString());
+ errorCallback.invoke(error);
}
}
-
}
diff --git a/src/wrappers/themis/react-native-themis/android/src/main/main.iml b/src/wrappers/themis/react-native-themis/android/src/main/main.iml
new file mode 100644
index 000000000..908ad4f52
--- /dev/null
+++ b/src/wrappers/themis/react-native-themis/android/src/main/main.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/wrappers/themis/react-native-themis/ios/RCTThemis.h b/src/wrappers/themis/react-native-themis/ios/RCTThemis.h
index b9a884ed9..32829b570 100644
--- a/src/wrappers/themis/react-native-themis/ios/RCTThemis.h
+++ b/src/wrappers/themis/react-native-themis/ios/RCTThemis.h
@@ -10,19 +10,23 @@
#import
-#define BYTEOVERFLOW 255
-#define CONTEXTREQUIRED 254
-#define PUBLICKEYREQUIRED 253
-#define PRIVATEKEYREQUIRED 252
-
+#define BYTEOVERFLOW 255
+#define CONTEXTREQUIRED 254
+#define PUBLICKEYREQUIRED 253
+#define PRIVATEKEYREQUIRED 252
+#define DESERIALIZE_ERROR 251
+#define DESERIALIZE_MEMORY 250
+
+#define BYTEOVERFLOWREASON "Byte overflow: value is out of range"
#define CONTEXTREQUIREDREASON "Context required"
#define PUBLICKEYREQUIREDREASON "Public key can not be null or blank"
#define PRIVATEKEYREQUIREDREASON "Private key can not be null or blank"
+#define DESERIALIZE_ERRORREASON "Deserialize error: empty input data"
+#define DESERIALIZE_MEMORYREASON "Deserialize error: can not allocate memory"
-#define KEYTYPE_EC 0
+#define KEYTYPE_EC 0
#define KEYTYPE_RSA 1
-
@interface RCTThemis : NSObject
@end
diff --git a/src/wrappers/themis/react-native-themis/ios/RCTThemis.m b/src/wrappers/themis/react-native-themis/ios/RCTThemis.m
index ebf197a8e..9df265036 100644
--- a/src/wrappers/themis/react-native-themis/ios/RCTThemis.m
+++ b/src/wrappers/themis/react-native-themis/ios/RCTThemis.m
@@ -18,10 +18,16 @@ @implementation RCTThemis
// To save the comparators objects
NSMutableDictionary* cmprs;
+NSNumber *uchar_min;
+NSNumber *uchar_max;
+
- (instancetype)init
{
self = [super init];
cmprs = [[NSMutableDictionary alloc] init];
+ uchar_min = [NSNumber numberWithInt:0];
+ uchar_max = [NSNumber numberWithInt:255];
+
return self;
}
@@ -37,10 +43,10 @@ - (NSDictionary *)constantsToExport
};
}
-// It required by constantsToExport and iOS
+// It required by constantsToExport and iOS
+ (BOOL)requiresMainQueueSetup
{
- return YES;
+ return YES;
}
/***********************************************/
@@ -50,11 +56,11 @@ + (BOOL)requiresMainQueueSetup
+ (NSArray*)dataSerialize:(NSData*) data
{
- if (data == nil) return nil;
-
+ if (data == nil || data.length == 0 ) return nil;
+
const char* buffer = (const char*) data.bytes;
NSMutableArray *array = [[NSMutableArray alloc] init];
-
+
for (NSInteger i = 0; i < data.length; i++) {
NSNumber *num = [[NSNumber alloc] initWithUnsignedChar: buffer[i]];
[array addObject:num];
@@ -67,31 +73,30 @@ + (NSArray*)dataSerialize:(NSData*) data
/* NSData <-- NSArray[NSNumber(unsigned char)] */
/* */
-
-+ (NSData*)dataDeserialize:(NSArray*) data
++ (NSData*)dataDeserialize: (NSArray*) data error:(NSError**) error
{
- if (data == nil) return nil;
-
+ if ( data == nil || data.count == 0 ) {
+ *error = SCERROR(DESERIALIZE_ERROR, @DESERIALIZE_ERRORREASON);
+ return nil;
+ }
+
char* buffer = (char*)malloc(data.count);
if (buffer == nil) {
- return nil; // malloc returns nil
+ *error = SCERROR(DESERIALIZE_MEMORY, @DESERIALIZE_MEMORYREASON);
+ return nil;
}
-
+
for (NSInteger i = 0; i < data.count; i++) {
NSNumber *num = data[i];
- const unsigned char c = num.unsignedCharValue;
- if (c < 0 || c > 255) {
- NSException *e = [NSException
- exceptionWithName:@"ByteOverflowException"
- reason:@"Value is out of range"
- userInfo:nil];
- @throw e;
+ /* Check int value before casting to char */
+ if ([num compare:uchar_min] == NSOrderedAscending || [num compare:uchar_max] == NSOrderedDescending) {
+ free(buffer); // did not forget to free allocated memory
+ *error = SCERROR(BYTEOVERFLOW, @BYTEOVERFLOWREASON);
+ return nil;
}
- buffer[i] = c;
+ buffer[i] = num.unsignedCharValue;
}
-
NSData *result = [NSData dataWithBytesNoCopy:buffer length:data.count freeWhenDone:YES];
-
return result;
}
@@ -99,14 +104,15 @@ + (NSData*)dataDeserialize:(NSArray*) data
callback:(RCTResponseSenderBlock)callback
)
{
- NSData* data = [text dataUsingEncoding:NSUTF8StringEncoding];
- NSArray *data2 = [RCTThemis dataSerialize: data];
+ NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
+ NSArray *data2 = [RCTThemis dataSerialize: data];
callback(@[data2]);
}
RCT_EXPORT_METHOD(keyPair:(nonnull NSNumber*) algorithm
- callback:(RCTResponseSenderBlock)callback)
+ successCallback: (RCTResponseSenderBlock)successCallback
+ )
{
TSKeyGen *keypair;
switch (algorithm.intValue) {
@@ -120,59 +126,57 @@ + (NSData*)dataDeserialize:(NSArray*) data
NSArray *privateKey = [RCTThemis dataSerialize: keypair.privateKey];
NSArray *publicKey = [RCTThemis dataSerialize: keypair.publicKey];
-
+
NSDictionary *dictionary = @{
- @"private" : privateKey,
- @"public" : publicKey
+ @"private" : privateKey,
+ @"public" : publicKey
};
- callback(@[dictionary]);
+ successCallback(@[dictionary]);
}
RCT_EXPORT_METHOD(symmetricKey:(RCTResponseSenderBlock)callback)
{
NSData *symmetricKey = TSGenerateSymmetricKey();
- NSArray *masterKey = [RCTThemis dataSerialize: symmetricKey];
+ NSArray *masterKey = [RCTThemis dataSerialize: symmetricKey];
callback(@[masterKey]);
}
-- (TSCellSeal *)newSealMode: (NSArray*) symmetricKey
+- (TSCellSeal *)newSealMode: (NSArray*) symmetricKey error:(NSError**) error
{
- @try {
- NSData *masterKey = [RCTThemis dataDeserialize: symmetricKey];
- TSCellSeal *cell = [[TSCellSeal alloc] initWithKey:masterKey];
- return cell;
- }
- @catch (NSException *e) {
- @throw e; // rethrow to catch in final function
+ NSData *masterKey = [RCTThemis dataDeserialize: symmetricKey error:error];
+ if (masterKey == nil) {
+ return nil;
}
-
+ TSCellSeal *cell = [[TSCellSeal alloc] initWithKey:masterKey];
+ return cell;
}
-
-
-
RCT_EXPORT_METHOD(secureCellSealWithSymmetricKeyEncrypt:(NSArray*) symmetricKey
plaintext: (NSString*)plaintext
context: (NSString*)context
successCallback: (RCTResponseSenderBlock)successCallback
errorCallback: (RCTResponseErrorBlock)errorCallback)
{
-
TSCellSeal *cell;
- @try {
- cell = [self newSealMode:symmetricKey];
+ NSError *error;
+
+ cell = [self newSealMode:symmetricKey error:&error];
+ if (cell == nil) {
+ errorCallback(error);
+ return;
}
- @catch (NSException *e) {
- NSError* error = SCERROR(BYTEOVERFLOW, e.reason);
+
+ NSData *plaintextBinary = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
+ NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];
+ NSData *encrypted = [cell encrypt:plaintextBinary
+ context:contextBinary
+ error:&error];
+ if (encrypted == nil) {
errorCallback(error);
return;
}
-
- NSData *txt = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
- NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
- NSData *encrypted = [cell encrypt:txt context:ctx];
NSArray *result = [RCTThemis dataSerialize:encrypted];
successCallback(@[result]);
}
@@ -186,31 +190,31 @@ - (TSCellSeal *)newSealMode: (NSArray*) symmetricKey
{
TSCellSeal *cell;
- NSData *enc;
-
- @try {
- cell = [self newSealMode:symmetricKey];
- enc = [RCTThemis dataDeserialize:encrypted];
+ NSData *encryptedBinary;
+ NSError *error;
+
+ cell = [self newSealMode:symmetricKey error:&error];
+ if (cell == nil) {
+ errorCallback(error);
+ return;
}
- @catch (NSException *e) {
- NSError* error = SCERROR(BYTEOVERFLOW, e.reason);
+
+ encryptedBinary = [RCTThemis dataDeserialize:encrypted error:&error];
+ if (encryptedBinary == nil) {
errorCallback(error);
return;
}
-
- NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
-
- NSError *error;
- NSData *decrypted = [cell decrypt:enc
- context:ctx
- error:&error];
- if (error) {
+
+ NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];
+ NSData *decrypted = [cell decrypt:encryptedBinary
+ context:contextBinary
+ error:&error];
+ if (decrypted == nil) {
errorCallback(error);
} else {
NSArray* result = [RCTThemis dataSerialize:decrypted];
successCallback(@[result]);
}
-
}
@@ -224,18 +228,23 @@ - (TSCellSeal *)newSealModeWithPassphrase: (NSString*) passphrase
RCT_EXPORT_METHOD(secureCellSealWithPassphraseEncrypt:(NSString*) passphrase
plaintext: (NSString*)plaintext
context: (NSString*)context
- callback: (RCTResponseSenderBlock)callback)
+ successCallback: (RCTResponseSenderBlock)successCallback
+ errorCallback: (RCTResponseErrorBlock)errorCallback
+ )
{
+ TSCellSeal *cell = [self newSealModeWithPassphrase:passphrase];
+ NSData *plaintextBinary = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
+ NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];
- TSCellSeal *cell = [self newSealModeWithPassphrase:passphrase];
-
- NSData *txt = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
- NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
- NSData *encrypted = [cell encrypt:txt context:ctx];
-
- NSArray *result = [RCTThemis dataSerialize:encrypted];
+ NSError *error;
+ NSData *encrypted = [cell encrypt:plaintextBinary context:contextBinary error:&error];
- callback(@[result]);
+ if (encrypted == nil) {
+ errorCallback(error);
+ } else {
+ NSArray *result = [RCTThemis dataSerialize:encrypted];
+ successCallback(@[result]);
+ }
}
RCT_EXPORT_METHOD(secureCellSealWithPassphraseDecrypt:(NSString*) passphrase
@@ -246,43 +255,39 @@ - (TSCellSeal *)newSealModeWithPassphrase: (NSString*) passphrase
)
{
- TSCellSeal *cell = [self newSealModeWithPassphrase:passphrase];
- NSData *enc;
-
- @try {
- enc = [RCTThemis dataDeserialize:encrypted];
- } @catch (NSException *e) {
- NSError* error = SCERROR(BYTEOVERFLOW, e.reason);
- errorCallback(error);
- }
-
- NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
-
+ TSCellSeal *cell = [self newSealModeWithPassphrase:passphrase];
+ NSData *encryptedBinary;
NSError *error;
- NSData *decrypted = [cell decrypt:enc
- context:ctx
- error:&error];
- if (error) {
+
+ encryptedBinary = [RCTThemis dataDeserialize:encrypted error:&error];
+ if (encryptedBinary == nil) {
+ errorCallback(error);
+ return;
+ }
+
+ NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];
+ NSData *decrypted = [cell decrypt:encryptedBinary
+ context:contextBinary
+ error:&error];
+ if (decrypted == nil) {
errorCallback(error);
} else {
NSArray* result = [RCTThemis dataSerialize:decrypted];
successCallback(@[result]);
}
-
}
/* MARK: Token protect mode */
-- (TSCellToken *)newTokenMode:(NSArray*) symmetricKey
+- (TSCellToken *)newTokenMode:(NSArray*) symmetricKey error:(NSError**) error
{
- @try {
- NSData *masterKey = [RCTThemis dataDeserialize: symmetricKey];
- TSCellToken *cell = [[TSCellToken alloc] initWithKey:masterKey];
- return cell;
- }
- @catch (NSException *e) {
- @throw e;
+ NSData *masterKey = [RCTThemis dataDeserialize: symmetricKey error:error];
+ if (masterKey == nil) {
+ return nil;
}
+
+ TSCellToken *cell = [[TSCellToken alloc] initWithKey:masterKey];
+ return cell;
}
RCT_EXPORT_METHOD(secureCellTokenProtectEncrypt:(NSArray*) symmetricKey
@@ -291,28 +296,30 @@ - (TSCellToken *)newTokenMode:(NSArray*) symmetricKey
successCallback: (RCTResponseSenderBlock)successCallback
errorCallback: (RCTResponseErrorBlock)errorCallback)
{
-
+ NSError *error;
TSCellToken *cell;
-
- @try {
- cell = [self newTokenMode:symmetricKey];
+
+ cell = [self newTokenMode:symmetricKey error:&error];
+ if (cell == nil) {
+ errorCallback(error);
+ return;
}
- @catch (NSException *e) {
- NSError* error = SCERROR(BYTEOVERFLOW, e.reason);
+
+ NSData *plaintextBinary = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
+ NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];
+
+ TSCellTokenEncryptedResult *result = [cell encrypt:plaintextBinary context:contextBinary error:&error];
+ if (result == nil ) {
errorCallback(error);
return;
}
-
- NSData *txt = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
- NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
- TSCellTokenEncryptedResult *result = [cell encrypt:txt context:ctx];
-
+
NSArray *encrypted = [RCTThemis dataSerialize: result.encrypted];
NSArray *token = [RCTThemis dataSerialize: result.token];
-
+
NSDictionary *dictionary = @{
- @"encrypted" : encrypted,
- @"token" : token
+ @"encrypted" : encrypted,
+ @"token" : token
};
successCallback(@[dictionary]);
}
@@ -327,49 +334,52 @@ - (TSCellToken *)newTokenMode:(NSArray*) symmetricKey
{
TSCellToken *cell;
- NSData *enc;
- NSData *tkn;
-
- @try {
- cell = [self newTokenMode:symmetricKey];
- enc = [RCTThemis dataDeserialize:encrypted];
- tkn = [RCTThemis dataDeserialize:token];
+ NSData *encryptedBinary;
+ NSData *tokenBinary;
+ NSError *error;
+
+ cell = [self newTokenMode:symmetricKey error:&error];
+ if (cell == nil) {
+ errorCallback(error);
+ return;
}
- @catch (NSException *e) {
- NSError* error = SCERROR(BYTEOVERFLOW, e.reason);
+
+ encryptedBinary = [RCTThemis dataDeserialize:encrypted error:&error];
+ if (encryptedBinary == nil) {
errorCallback(error);
return;
}
-
- NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
-
- NSError *error;
- NSData *decrypted = [cell decrypt:enc
- token:tkn
- context:ctx
- error:&error];
- if (error) {
+
+ tokenBinary = [RCTThemis dataDeserialize:token error:&error];
+ if (tokenBinary == nil) {
+ errorCallback(error);
+ return;
+ }
+
+ NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];
+ NSData *decrypted = [cell decrypt:encryptedBinary
+ token:tokenBinary
+ context:contextBinary
+ error:&error];
+ if (decrypted == nil) {
errorCallback(error);
} else {
NSArray* result = [RCTThemis dataSerialize:decrypted];
successCallback(@[result]);
}
-
}
/* MARK: Context imprint mode */
-- (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
+- (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey error: (NSError**) error
{
- @try {
- NSData *masterKey = [RCTThemis dataDeserialize: symmetricKey];
- TSCellContextImprint *cell = [[TSCellContextImprint alloc] initWithKey:masterKey];
- return cell;
- }
- @catch (NSException *e) {
- @throw e;
+ NSData *masterKey = [RCTThemis dataDeserialize: symmetricKey error:error];
+ if (masterKey == nil) {
+ return nil;
}
+ TSCellContextImprint *cell = [[TSCellContextImprint alloc] initWithKey:masterKey];
+ return cell;
}
RCT_EXPORT_METHOD(secureCellContextImprintEncrypt:(NSArray*) symmetricKey
@@ -383,24 +393,25 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
errorCallback(error);
return;
}
-
+
TSCellContextImprint *cell;
- @try {
- cell = [self newContextImprint:symmetricKey];
+ NSError *error;
+
+ cell = [self newContextImprint:symmetricKey error:&error];
+ if (cell == nil) {
+ errorCallback(error);
+ return;
}
- @catch (NSException *e) {
- NSError* error = SCERROR(BYTEOVERFLOW, e.reason);
+
+ NSData *plaintextBinary = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
+ NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];
+ NSData *encrypted = [cell encrypt:plaintextBinary context:contextBinary error:&error];
+ if (encrypted == nil) {
errorCallback(error);
return;
}
-
-
- NSData *txt = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
- NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
- NSData *encrypted = [cell encrypt:txt context:ctx];
-
- NSArray *result = [RCTThemis dataSerialize:encrypted];
+ NSArray *result = [RCTThemis dataSerialize:encrypted];
successCallback(@[result]);
}
@@ -410,28 +421,38 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
successCallback:(RCTResponseSenderBlock)successCallback
errorCallback:(RCTResponseErrorBlock)errorCallback)
{
-
if (context == nil || [context isEqual: @""]) {
NSError* error = SCERROR(CONTEXTREQUIRED, @CONTEXTREQUIREDREASON);
errorCallback(error);
return;
}
-
+
TSCellContextImprint *cell;
- NSData *enc;
- @try {
- cell = [self newContextImprint:symmetricKey];
- enc = [RCTThemis dataDeserialize:encrypted];
+ NSData *encryptedBinary;
+ NSError *error;
+
+ cell = [self newContextImprint:symmetricKey error:&error];
+ if (cell == nil) {
+ errorCallback(error);
+ return;
}
- @catch (NSException *e) {
- NSError* error = SCERROR(BYTEOVERFLOW, e.reason);
+ encryptedBinary = [RCTThemis dataDeserialize:encrypted error:&error];
+ if (encryptedBinary == nil) {
+ errorCallback(error);
+ return;
+ }
+
+ NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];
+ NSData *decrypted = [cell decrypt:encryptedBinary
+ context:contextBinary
+ error:&error
+ ];
+
+ if (decrypted == nil) {
errorCallback(error);
return;
}
- NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
- NSData *decrypted = [cell decrypt:enc
- context:ctx];
NSArray* result = [RCTThemis dataSerialize:decrypted];
successCallback(@[result]);
}
@@ -439,49 +460,39 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
/* MARK: Secure Message */
RCT_EXPORT_METHOD(secureMessageSign:(NSString*) message
privateKey:(NSArray*) privateKey
- publicKey:(NSArray*) publicKey
successCallback:(RCTResponseSenderBlock)successCallback
errorCallback:(RCTResponseErrorBlock) errorCallback
)
{
-
if (privateKey == nil || privateKey.count == 0) {
NSError* error = SCERROR(PRIVATEKEYREQUIRED, @PRIVATEKEYREQUIREDREASON);
errorCallback(error);
return;
}
-
- NSData* pvtKey;
- NSData* pubKey;
- @try {
- pvtKey = [RCTThemis dataDeserialize:privateKey];
- pubKey = [RCTThemis dataDeserialize:publicKey];
- }
- @catch (NSException *e) {
- NSError* error = SCERROR(BYTEOVERFLOW, e.reason);
+
+ NSData *privateKeyBinary;
+ NSError *error;
+
+ privateKeyBinary = [RCTThemis dataDeserialize:privateKey error:&error];
+ if (privateKeyBinary == nil) {
errorCallback(error);
return;
}
-
- NSData* msg = [message dataUsingEncoding:NSUTF8StringEncoding];
- TSMessage *secureMessage = [[TSMessage alloc] initInSignVerifyModeWithPrivateKey:pvtKey
- peerPublicKey:pubKey];
-
- NSError *error;
- NSData *signedMessage = [secureMessage wrapData:msg error:&error];
-
- if (error) {
+ NSData* messageBinary = [message dataUsingEncoding:NSUTF8StringEncoding];
+ TSMessage *secureMessage = [[TSMessage alloc] initInSignVerifyModeWithPrivateKey:privateKeyBinary
+ peerPublicKey:nil];
+
+ NSData *signedMessage = [secureMessage wrapData:messageBinary error:&error];
+ if (signedMessage == nil) {
errorCallback(error);
} else {
NSArray* result = [RCTThemis dataSerialize:signedMessage];
successCallback(@[result]);
}
-
}
RCT_EXPORT_METHOD(secureMessageVerify:(NSArray*) message
- privateKey:(NSArray*) privateKey
publicKey:(NSArray*) publicKey
successCallback:(RCTResponseSenderBlock)successCallback
errorCallback:(RCTResponseErrorBlock)errorCallback
@@ -493,29 +504,28 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
errorCallback(error);
return;
}
-
- NSData* pvtKey;
- NSData* pubKey;
- NSData* msg;
-
- @try {
- pvtKey = [RCTThemis dataDeserialize:privateKey];
- pubKey = [RCTThemis dataDeserialize:publicKey];
- msg = [RCTThemis dataDeserialize:message];
+
+ NSData *publicKeyBinary;
+ NSData *messageBinary;
+ NSError *error;
+
+ publicKeyBinary = [RCTThemis dataDeserialize:publicKey error:&error];
+ if (publicKeyBinary == nil) {
+ errorCallback(error);
+ return;
}
- @catch (NSException *e) {
- NSError* error = SCERROR(BYTEOVERFLOW, e.reason);
+
+ messageBinary = [RCTThemis dataDeserialize:message error:&error];
+ if (messageBinary == nil) {
errorCallback(error);
return;
}
-
- TSMessage *secureMessage = [[TSMessage alloc] initInSignVerifyModeWithPrivateKey:pvtKey
- peerPublicKey:pubKey];
-
- NSError *error;
- NSData *verifiedMessage = [secureMessage unwrapData:msg error:&error];
- if (error) {
+ TSMessage *secureMessage = [[TSMessage alloc] initInSignVerifyModeWithPrivateKey:nil
+ peerPublicKey:publicKeyBinary];
+
+ NSData *verifiedMessage = [secureMessage unwrapData:messageBinary error:&error];
+ if (verifiedMessage == nil) {
errorCallback(error);
} else {
NSArray* result = [RCTThemis dataSerialize:verifiedMessage];
@@ -531,7 +541,6 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
)
{
-
if (privateKey == nil || privateKey.count == 0) {
NSError* error = SCERROR(PRIVATEKEYREQUIRED, @PRIVATEKEYREQUIREDREASON);
errorCallback(error);
@@ -539,39 +548,37 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
}
if (publicKey == nil || publicKey.count == 0) {
- NSError* error = SCERROR(PUBLICKEYREQUIRED, @PUBLICKEYREQUIREDREASON);
- errorCallback(error);
- return;
+ NSError* error = SCERROR(PUBLICKEYREQUIRED, @PUBLICKEYREQUIREDREASON);
+ errorCallback(error);
+ return;
}
- NSData* pvtKey;
- NSData* pubKey;
-
- @try {
- pvtKey = [RCTThemis dataDeserialize:privateKey];
- pubKey = [RCTThemis dataDeserialize:publicKey];
+ NSData *privateKeyBinary;
+ NSData *publicKeyBinary;
+ NSError *error;
+
+ privateKeyBinary = [RCTThemis dataDeserialize:privateKey error:&error];
+ if (privateKeyBinary == nil) {
+ errorCallback(error);
+ return;
}
- @catch (NSException *e) {
- NSError* error = SCERROR(BYTEOVERFLOW, e.reason);
+ publicKeyBinary = [RCTThemis dataDeserialize:publicKey error:&error];
+ if (publicKeyBinary == nil) {
errorCallback(error);
return;
}
-
- NSData* msg = [message dataUsingEncoding:NSUTF8StringEncoding];
- TSMessage *secureMessage = [[TSMessage alloc] initInEncryptModeWithPrivateKey:pvtKey
- peerPublicKey:pubKey];
-
- NSError *error;
- NSData *encryptedMessage = [secureMessage wrapData:msg error:&error];
-
- if (error) {
+ NSData* messageBinary = [message dataUsingEncoding:NSUTF8StringEncoding];
+ TSMessage *secureMessage = [[TSMessage alloc] initInEncryptModeWithPrivateKey:privateKeyBinary
+ peerPublicKey:publicKeyBinary];
+
+ NSData *encryptedMessage = [secureMessage wrapData:messageBinary error:&error];
+ if (encryptedMessage == nil) {
errorCallback(error);
} else {
NSArray* result = [RCTThemis dataSerialize:encryptedMessage];
successCallback(@[result]);
}
-
}
RCT_EXPORT_METHOD(secureMessageDecrypt:(NSArray*) message
@@ -581,7 +588,6 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
errorCallback:(RCTResponseErrorBlock) errorCallback
)
{
-
if (privateKey == nil || privateKey.count == 0) {
NSError* error = SCERROR(PRIVATEKEYREQUIRED, @PRIVATEKEYREQUIREDREASON);
errorCallback(error);
@@ -593,35 +599,38 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
errorCallback(error);
return;
}
-
- NSData* pvtKey;
- NSData* pubKey;
- NSData* msg;
-
- @try {
- pvtKey = [RCTThemis dataDeserialize:privateKey];
- pubKey = [RCTThemis dataDeserialize:publicKey];
- msg = [RCTThemis dataDeserialize:message];
+
+ NSData *privateKeyBinary;
+ NSData *publicKeyBinary;
+ NSData *messageBinary;
+ NSError *error;
+
+ privateKeyBinary = [RCTThemis dataDeserialize:privateKey error:&error];
+ if (privateKeyBinary == nil) {
+ errorCallback(error);
+ return;
}
- @catch (NSException *e) {
- NSError* error = SCERROR(BYTEOVERFLOW, e.reason);
+ publicKeyBinary = [RCTThemis dataDeserialize:publicKey error:&error];
+ if (publicKeyBinary == nil) {
errorCallback(error);
return;
}
-
- TSMessage *secureMessage = [[TSMessage alloc] initInEncryptModeWithPrivateKey:pvtKey
- peerPublicKey:pubKey];
-
- NSError *error;
- NSData *decryptedMessage = [secureMessage unwrapData:msg error:&error];
-
- if (error) {
+ messageBinary = [RCTThemis dataDeserialize:message error:&error];
+ if (messageBinary == nil) {
+ errorCallback(error);
+ return;
+ }
+
+ TSMessage *secureMessage = [[TSMessage alloc] initInEncryptModeWithPrivateKey:privateKeyBinary
+ peerPublicKey:publicKeyBinary];
+
+ NSData *decryptedMessage = [secureMessage unwrapData:messageBinary error:&error];
+ if (decryptedMessage == nil) {
errorCallback(error);
} else {
NSArray* result = [RCTThemis dataSerialize:decryptedMessage];
successCallback(@[result]);
}
-
}
/* MARK: Comparator */
@@ -630,16 +639,14 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
successCallback:(RCTResponseSenderBlock) successCallback
errorCallback:(RCTResponseErrorBlock) errorCallback)
{
- NSData* sharedSecretData;
-
- @try {
- sharedSecretData = [RCTThemis dataDeserialize:sharedSecret];
- }
- @catch (NSException *e) {
- NSError* error = SCERROR(BYTEOVERFLOW, e.reason);
+ NSData *sharedSecretData;
+ NSError *error;
+ sharedSecretData = [RCTThemis dataDeserialize:sharedSecret error:&error];
+ if (sharedSecretData == nil) {
errorCallback(error);
return;
}
+
TSComparator* cmp = [[TSComparator alloc] initWithMessageToCompare:sharedSecretData];
NSString *uuid = [[NSUUID UUID] UUIDString];
cmprs[uuid] = cmp;
@@ -659,23 +666,20 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
}
}
-
-
RCT_EXPORT_METHOD(beginCompare:(NSString*) uuid
successCallback:(RCTResponseSenderBlock) successCallback
errorCallback:(RCTResponseErrorBlock) errorCallback
)
{
-
- TSComparator* cmp = cmprs[uuid];
+ TSComparator *cmp = cmprs[uuid];
if (cmp == nil) {
errorCallback(nil);
return;
}
- NSError* error;
- NSData* data = [cmp beginCompare:&error];
- if (error) {
+ NSError *error;
+ NSData *data = [cmp beginCompare:&error];
+ if (data == nil) {
errorCallback(error);
} else {
NSArray* result = [RCTThemis dataSerialize:data];
@@ -691,24 +695,22 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
{
TSComparator* cmp = cmprs[uuid];
if ( cmp == nil ) {
- errorCallback(nil);
+ NSError* error = SCERROR(-1, @"Comparator does not exist");
+ errorCallback(error);
return;
}
-
- NSData* data;
-
- @try {
- data = [RCTThemis dataDeserialize:previous];
- }
- @catch (NSException *e) {
- NSError* error = SCERROR(BYTEOVERFLOW, e.reason);
+
+ NSData *data;
+ NSError *error;
+
+ data = [RCTThemis dataDeserialize:previous error:&error];
+ if (data == nil) {
errorCallback(error);
return;
}
- NSError* error;
data = [cmp proceedCompare:data error:&error];
- if (error) {
+ if (data == nil) {
errorCallback(error);
} else {
NSArray* result = [RCTThemis dataSerialize:data];
@@ -716,9 +718,12 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
if (cmp.status != TSComparatorNotReady) {
[cmprs removeObjectForKey:uuid];
}
- successCallback(@[result, status]);
+ if (result != nil) {
+ successCallback(@[result, status]);
+ } else {
+ successCallback(@[@"", status]);
+ }
}
-
}
diff --git a/src/wrappers/themis/react-native-themis/package.json b/src/wrappers/themis/react-native-themis/package.json
index 330b6fade..92cb2b817 100644
--- a/src/wrappers/themis/react-native-themis/package.json
+++ b/src/wrappers/themis/react-native-themis/package.json
@@ -1,6 +1,6 @@
{
"name": "react-native-themis",
- "version": "0.14.7",
+ "version": "0.14.10",
"description": "Themis React Native is a convenient cryptographic library for data protection",
"react-native": "src/index",
"source": "src/index",
@@ -21,7 +21,8 @@
],
"scripts": {
"test": "jest",
- "typescript": "tsc --noEmit",
+ "typescript": "tsc",
+ "declaration": "tsc --declaration --emitDeclarationOnly",
"lint": "eslint \"**/*.{js,ts,tsx}\"",
"prepare": "bob build",
"release": "release-it"
diff --git a/src/wrappers/themis/react-native-themis/src/index.d.ts b/src/wrappers/themis/react-native-themis/src/index.d.ts
index 380c88515..b1cfc7995 100644
--- a/src/wrappers/themis/react-native-themis/src/index.d.ts
+++ b/src/wrappers/themis/react-native-themis/src/index.d.ts
@@ -1,5 +1,7 @@
export declare const COMPARATOR_NOT_READY: any, COMPARATOR_NOT_MATCH: any, COMPARATOR_MATCH: any, COMPARATOR_ERROR: any, KEYTYPE_RSA: any, KEYTYPE_EC: any;
-export declare function keyPair64(typeOfKey: any): Promise