Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redesigned the framework to make it less vulnerable to native exceptions. #951

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
300e0fa
improve the code with the help of new try-catch
radetsky Sep 2, 2022
8d2eb52
integrate isBase64 into code
radetsky Sep 15, 2022
a47fad8
review Obj-C code to make all try-catch and error checks
radetsky Sep 16, 2022
9a27632
fix an errors
radetsky Sep 16, 2022
241e0ac
review Java code
radetsky Sep 21, 2022
9907cb5
review Obj-C code
radetsky Sep 21, 2022
ecb3457
review Javascript code
radetsky Sep 21, 2022
0d9de20
moved old example and add ThemisTest for RN app
radetsky Sep 22, 2022
9c8377f
new Android Studio support file
radetsky Sep 22, 2022
90a0401
failed/passed fixes in order
radetsky Sep 23, 2022
c4eecd6
comparator can return nil without an error
radetsky Sep 23, 2022
6ecbec4
update example app
radetsky Sep 23, 2022
19496ce
Merge branch 'release/0.14' into rad-dev
radetsky Sep 23, 2022
e3a0322
changelog update
radetsky Sep 23, 2022
84218b2
check int value before casting to unsigned char
radetsky Sep 26, 2022
cdd443f
fix the compare procedure; var naming due the suggestion;
radetsky Sep 26, 2022
480702a
remove privateKey from secureMessageVerify functions
radetsky Sep 26, 2022
99342d1
remove publicKey from secureMessageSign64
radetsky Sep 26, 2022
297c93d
rewrite Obj-C code to use NSError instead of try/catch
radetsky Sep 29, 2022
cc5c3bb
fix the tests. The issue was in incorrect display of error
radetsky Sep 29, 2022
aa72af2
updated the code according to the comments
radetsky Sep 29, 2022
35ec1e9
updated the code according to the comments
radetsky Oct 2, 2022
913fec3
move examples to the previous state
radetsky Oct 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,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 {
Expand Down Expand Up @@ -164,9 +164,9 @@ public void secureCellSealWithSymmetricKeyEncrypt(ReadableArray symmetricKey,
try {
byte[] key = dataDeserialize(symmetricKey);
SecureCell.Seal cell = SecureCell.SealWithKey(key);
byte[] txt = plaintext.getBytes(StandardCharsets.UTF_8);
byte[] ctx = context.getBytes(StandardCharsets.UTF_8);
byte[] encrypted = cell.encrypt(txt, ctx);
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) {
Expand All @@ -188,8 +188,8 @@ public void secureCellSealWithSymmetricKeyDecrypt(ReadableArray symmetricKey,
byte[] key = dataDeserialize(symmetricKey);
SecureCell.Seal cell = SecureCell.SealWithKey(key);
byte[] enc = dataDeserialize(encrypted);
byte[] ctx = context.getBytes(StandardCharsets.UTF_8);
byte[] decrypted = cell.decrypt(enc, ctx);
byte[] contextBinary = context.getBytes(StandardCharsets.UTF_8);
byte[] decrypted = cell.decrypt(enc, contextBinary);
WritableArray response = dataSerialize(decrypted);
successCallback.invoke(response);
} catch (Exception e) {
Expand All @@ -208,9 +208,9 @@ public void secureCellSealWithPassphraseEncrypt(String passphrase,

try {
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);
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) {
Expand All @@ -230,8 +230,8 @@ public void secureCellSealWithPassphraseDecrypt(String passphrase,
try {
SecureCell.Seal cell = SecureCell.SealWithPassphrase(passphrase);
byte[] enc = dataDeserialize(encrypted);
byte[] ctx = context.getBytes(StandardCharsets.UTF_8);
byte[] decrypted = cell.decrypt(enc, ctx);
byte[] contextBinary = context.getBytes(StandardCharsets.UTF_8);
byte[] decrypted = cell.decrypt(enc, contextBinary);
WritableArray response = dataSerialize(decrypted);
successCallback.invoke(response);
} catch (Exception e) {
Expand All @@ -251,9 +251,9 @@ public void secureCellTokenProtectEncrypt(ReadableArray symmetricKey,
try {
byte[] bkey = dataDeserialize(symmetricKey);
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[] 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();
Expand All @@ -279,8 +279,8 @@ public void secureCellTokenProtectDecrypt(ReadableArray symmetricKey,
byte[] enc = dataDeserialize(encrypted);
byte[] tkn = dataDeserialize(token);
SecureCell.TokenProtect cell = SecureCell.TokenProtectWithKey(bkey);
byte[] ctx = context.getBytes(StandardCharsets.UTF_8);
byte[] decrypted = cell.decrypt(enc, tkn, ctx);
byte[] contextBinary = context.getBytes(StandardCharsets.UTF_8);
byte[] decrypted = cell.decrypt(enc, tkn, contextBinary);
WritableArray response = dataSerialize(decrypted);
successCallback.invoke(response);
} catch (Exception e) {
Expand All @@ -307,9 +307,9 @@ public void secureCellContextImprintEncrypt(ReadableArray symmetricKey,
try {
byte[] bkey = dataDeserialize(symmetricKey);
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);
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) {
Expand Down Expand Up @@ -338,8 +338,8 @@ public void secureCellContextImprintDecrypt(ReadableArray symmetricKey,
byte[] bkey = dataDeserialize(symmetricKey);
byte[] enc = dataDeserialize(encrypted);
SecureCell.ContextImprint cell = SecureCell.ContextImprintWithKey(bkey);
byte[] ctx = context.getBytes(StandardCharsets.UTF_8);
byte[] decrypted = cell.decrypt(enc, ctx);
byte[] contextBinary = context.getBytes(StandardCharsets.UTF_8);
byte[] decrypted = cell.decrypt(enc, contextBinary);
WritableArray response = dataSerialize(decrypted);
successCallback.invoke(response);
} catch (Exception e) {
Expand Down
47 changes: 25 additions & 22 deletions src/wrappers/themis/react-native-themis/ios/RCTThemis.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ + (NSData*)dataDeserialize:(NSArray*) data
@throw exception;
}

NSNumber *uchar_min = [NSNumber numberWithInt:0];
NSNumber *uchar_max = [NSNumber numberWithInt:255];
ilammy marked this conversation as resolved.
Show resolved Hide resolved

for (NSInteger i = 0; i < data.count; i++) {
NSNumber *num = data[i];
/* Check int value before casting to char */
if (num.intValue < 0 || num.intValue > 255) {
if ([num compare:uchar_min] == NSOrderedAscending || [num compare:uchar_max] == NSOrderedDescending) {
NSException *e = [NSException
ilammy marked this conversation as resolved.
Show resolved Hide resolved
exceptionWithName:@"ByteOverflowException"
reason:@BYTEOVERFLOWREASON
Expand Down Expand Up @@ -186,11 +189,11 @@ - (TSCellSeal *)newSealMode: (NSArray*) symmetricKey
return;
}

NSData *txt = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
NSData *plaintextBinary = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSData *encrypted = [cell encrypt:txt
context:ctx
NSData *encrypted = [cell encrypt:plaintextBinary
context:contextBinary
error:&error];
if (error != nil) {
errorCallback(error);
Expand Down Expand Up @@ -239,11 +242,11 @@ - (TSCellSeal *)newSealMode: (NSArray*) symmetricKey
return;
}

NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];

NSError *error;
NSData *decrypted = [cell decrypt:enc
context:ctx
context:contextBinary
error:&error];
if (error != nil) {
errorCallback(error);
Expand Down Expand Up @@ -271,11 +274,11 @@ - (TSCellSeal *)newSealModeWithPassphrase: (NSString*) passphrase
{

TSCellSeal *cell = [self newSealModeWithPassphrase:passphrase];
NSData *txt = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
NSData *plaintextBinary = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];

NSError *error;
NSData *encrypted = [cell encrypt:txt context:ctx error:&error];
NSData *encrypted = [cell encrypt:plaintextBinary context:contextBinary error:&error];

if (error != nil) {
errorCallback(error);
Expand Down Expand Up @@ -306,11 +309,11 @@ - (TSCellSeal *)newSealModeWithPassphrase: (NSString*) passphrase
return;
}

NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];

NSError *error;
NSData *decrypted = [cell decrypt:enc
context:ctx
context:contextBinary
error:&error];
if (error != nil) {
errorCallback(error);
Expand Down Expand Up @@ -352,11 +355,11 @@ - (TSCellToken *)newTokenMode:(NSArray*) symmetricKey
return;
}

NSData *txt = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
NSData *plaintextBinary = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;

TSCellTokenEncryptedResult *result = [cell encrypt:txt context:ctx error:&error];
TSCellTokenEncryptedResult *result = [cell encrypt:plaintextBinary context:contextBinary error:&error];
if (error != nil ) {
errorCallback(error);
return;
Expand Down Expand Up @@ -397,12 +400,12 @@ - (TSCellToken *)newTokenMode:(NSArray*) symmetricKey
return;
}

NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];

NSError *error;
NSData *decrypted = [cell decrypt:enc
token:tkn
context:ctx
context:contextBinary
error:&error];
if (error) {
errorCallback(error);
Expand Down Expand Up @@ -451,11 +454,11 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
return;
}

NSData *txt = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
NSData *plaintextBinary = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;

NSData *encrypted = [cell encrypt:txt context:ctx error:&error];
NSData *encrypted = [cell encrypt:plaintextBinary context:contextBinary error:&error];
if (error != nil) {
errorCallback(error);
return;
Expand Down Expand Up @@ -491,11 +494,11 @@ - (TSCellContextImprint *)newContextImprint:(NSArray*) symmetricKey
return;
}

NSData *ctx = [context dataUsingEncoding:NSUTF8StringEncoding];
NSData *contextBinary = [context dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;

NSData *decrypted = [cell decrypt:enc
context:ctx
context:contextBinary
error:&error
];

Expand Down