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

Pr09.2 - Major OMEMO inprovement, QR Scanner #516

Merged
merged 121 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
6c56374
Don't count muted chats for app-badge
tmolitor-stud-tu Nov 11, 2020
0c91b65
Only use last interaction time if newer
tmolitor-stud-tu Nov 11, 2020
68f3ef6
Add new sqlite method executeScalarReader returning an NSArray of sca…
tmolitor-stud-tu Nov 12, 2020
d316438
Remove dupl buddylist / activechats entries
FriedrichAltheide Nov 13, 2020
88e54e8
use paperclip icon instead of plus (attachment button)
FriedrichAltheide Nov 13, 2020
ab84f17
Disable ALPHA_DEBUG_MESSAGE
FriedrichAltheide Nov 13, 2020
e5a8480
Removed old strings
FriedrichAltheide Nov 13, 2020
0205cc0
code style
FriedrichAltheide Nov 13, 2020
f1269bd
Fix ContactsViewController search crash
FriedrichAltheide Nov 13, 2020
a45058f
check if MLSearchViewController is already open before presenting a n…
FriedrichAltheide Nov 13, 2020
eeb5d83
create unique omemo device id
FriedrichAltheide Nov 13, 2020
8596808
Renamed omemo vars
FriedrichAltheide Nov 13, 2020
67e97a4
Throw on duplicate contacts again
tmolitor-stud-tu Nov 13, 2020
6cd2465
Handle window state in catalyst version
tmolitor-stud-tu Nov 13, 2020
6c4b7b2
Do some heavy cleanup
tmolitor-stud-tu Nov 13, 2020
7df61cc
removed accountNo from omemo impl
FriedrichAltheide Nov 14, 2020
709aca0
removed connection from omemo impl
FriedrichAltheide Nov 14, 2020
295f123
renamed DEBUG_OMEMO to DEBUG_ALPHA
FriedrichAltheide Nov 14, 2020
af631da
Fix ios 13 icons storyboard warnings
tmolitor-stud-tu Nov 14, 2020
a6ec7bd
Fix all other storyboard warnings
tmolitor-stud-tu Nov 14, 2020
a6e6f18
Fix linker errors by moving non-extension stuff around
tmolitor-stud-tu Nov 14, 2020
a064340
Updated Monal.xcodeproj/project.pbxproj
FriedrichAltheide Nov 14, 2020
d149b61
Updated OpenSSL-Universal 1.1.17 & SDWebImage 5.9.5
FriedrichAltheide Nov 14, 2020
f5461fe
Updated EAIntroView 2.13.0
FriedrichAltheide Nov 14, 2020
81dac9b
Do not extract localizations from folder "monalxmpp"
FriedrichAltheide Nov 14, 2020
40dc46e
Fix deprecation warning for catalyst builds
tmolitor-stud-tu Nov 14, 2020
38750c8
Log chatmarker in background
tmolitor-stud-tu Nov 15, 2020
279768c
Delay notifications by 1.5 seconds to better support catchup scenarios
tmolitor-stud-tu Nov 15, 2020
3cc1b49
Use cocoapods new cdn
tmolitor-stud-tu Nov 14, 2020
aba8c66
Try to fix pod specific linker warnings
tmolitor-stud-tu Nov 14, 2020
96b8e07
Fix race on expiring bg task
tmolitor-stud-tu Nov 16, 2020
4ea2723
Split XMPP errors into severe and non-severe ones
tmolitor-stud-tu Nov 16, 2020
fe94ab3
Improve mam preferences ui
tmolitor-stud-tu Nov 16, 2020
269ecc5
Invalidate account states on *every* db upgrade
tmolitor-stud-tu Nov 16, 2020
8e04cb5
Fix all compiler warnings and turn on -Werror
tmolitor-stud-tu Nov 14, 2020
37ecbf2
Fix remaining warnings and update localization
tmolitor-stud-tu Nov 16, 2020
0682c8d
Show progress when fetching initial omemo bundles
tmolitor-stud-tu Nov 16, 2020
2d8acbe
Change loglevel for startTimer: warning
tmolitor-stud-tu Nov 16, 2020
577c0fc
Cleanup podfile
tmolitor-stud-tu Nov 16, 2020
8efa766
Add account via QR-Code
FriedrichAltheide Nov 20, 2020
13e84a6
Add contact via QR-Code
FriedrichAltheide Nov 20, 2020
c033a2f
ChatViewController: lock loading of history while sending a msg
FriedrichAltheide Nov 20, 2020
fa8b24f
Fix remaining podspec warnings (fork these unmaintained projects)
tmolitor-stud-tu Nov 21, 2020
e9f886f
Fix background expiration handling race condition
tmolitor-stud-tu Nov 21, 2020
390f7c8
Bartycrouch: removed Monal-iOS
FriedrichAltheide Nov 21, 2020
d828bfd
ChatViewController: display scrollDown button on catalyst
FriedrichAltheide Nov 21, 2020
b99be27
MLLogInViewController: QR-Code: use password
FriedrichAltheide Nov 21, 2020
3c69be3
Fix xmpp error isSevere tests
tmolitor-stud-tu Nov 21, 2020
16f1aa9
Implement IPC broadcast messages
tmolitor-stud-tu Nov 21, 2020
5b40ea9
Throw exception instead of returning nil in MLSQLite
tmolitor-stud-tu Nov 21, 2020
e737c9a
Fix some static analyzer warnings and update pods
tmolitor-stud-tu Nov 21, 2020
a6811ca
Add Pods dir to gitignore list
tmolitor-stud-tu Nov 23, 2020
51c4d5f
Make travis compile our current version
tmolitor-stud-tu Nov 22, 2020
d84ec40
Fix deadlock
tmolitor-stud-tu Nov 23, 2020
a4ad3cf
Fix MACCATALYST build
FriedrichAltheide Nov 23, 2020
6416c24
Improve AES error handling
FriedrichAltheide Nov 23, 2020
7c6d808
Reduce timer logging
tmolitor-stud-tu Nov 23, 2020
5bf72dc
Add missing return in chatViewController
tmolitor-stud-tu Nov 23, 2020
36c3da4
Fix background/foreground state for background launches
tmolitor-stud-tu Nov 25, 2020
e49da90
Fix push enable from appex
tmolitor-stud-tu Nov 25, 2020
abc5b9e
Remove ios 13 warning message
tmolitor-stud-tu Nov 26, 2020
d4da48a
Update active chats timestamp on outgoing carbon, too
tmolitor-stud-tu Nov 26, 2020
879411d
filetransfer part 1-3
tmolitor-stud-tu Nov 9, 2020
a411f78
Remove online/offline contact sorting and cleanup db definitions
tmolitor-stud-tu Nov 23, 2020
270479c
Fix remaining static analyzer warnings
tmolitor-stud-tu Nov 23, 2020
52739dc
Remove display settings and merge with account settings
tmolitor-stud-tu Nov 23, 2020
2aa7ea8
Fix several bugs in old and new code
tmolitor-stud-tu Nov 23, 2020
41088b5
Fix "mam history loading on first outgoing message" bug
tmolitor-stud-tu Nov 24, 2020
d948503
Fix empty message list bug
tmolitor-stud-tu Nov 24, 2020
996793e
Remove online state display completely
tmolitor-stud-tu Nov 26, 2020
4f6e44d
Remove extra logging of inner and outer senders/recipients
tmolitor-stud-tu Nov 26, 2020
88e8bbf
filetransfer part 4
tmolitor-stud-tu Nov 26, 2020
b70a8f1
Show privacy settings on first start
tmolitor-stud-tu Nov 26, 2020
3166dbb
Implement LMC and pixart/blabber specific message deletion
tmolitor-stud-tu Nov 27, 2020
7ee39b7
Silence static analyzer warning
tmolitor-stud-tu Nov 27, 2020
4cb36ec
Change LMC rules after discussion with Holger
tmolitor-stud-tu Nov 27, 2020
a7ede57
Fix AES stuff for iOS 12
tmolitor-stud-tu Nov 27, 2020
7d10f09
Open privacy settings after first account setup, too
tmolitor-stud-tu Nov 27, 2020
127f69d
Consolidate XMPP error extraction
tmolitor-stud-tu Nov 28, 2020
8a418c4
Fix encrypted file upload/download
tmolitor-stud-tu Nov 28, 2020
c2cd640
Fix double origin-ids
tmolitor-stud-tu Nov 28, 2020
f9c89a8
Update localizations
tmolitor-stud-tu Nov 28, 2020
e8c5f85
fix showPrivacySettings segue
FriedrichAltheide Nov 28, 2020
b1c19ae
Login.storyboard: Better qr-code alignment
FriedrichAltheide Nov 28, 2020
d0dd3ca
Login.storyboard increase width "Set up an account later"
FriedrichAltheide Nov 28, 2020
88ad963
ContactsView: fix background color in dark mode
FriedrichAltheide Nov 28, 2020
1df778e
ContactsViewController: load table before appearing
FriedrichAltheide Nov 28, 2020
8549f67
chatViewController: remove observer on disappear
FriedrichAltheide Nov 28, 2020
c9c6c4c
ActiveChatsViewController: load contacts table on willAppear
FriedrichAltheide Nov 28, 2020
1f09797
Fix deletion of dismissed LMC text
tmolitor-stud-tu Nov 28, 2020
cb46ef4
fix showLogin segue ios12
FriedrichAltheide Nov 28, 2020
25c6c92
removed kContact
FriedrichAltheide Nov 28, 2020
e1b58ad
fix showLogin segue ios 13
FriedrichAltheide Nov 28, 2020
e7c7b06
fix showLogin segue
FriedrichAltheide Nov 28, 2020
9beae58
filetransfer part 5
tmolitor-stud-tu Nov 28, 2020
2c6e529
improve omemo onboarding
FriedrichAltheide Nov 29, 2020
7b4168e
Fix catalyst build
tmolitor-stud-tu Nov 29, 2020
96c8144
Make sure file downloads don't occur in main thread
tmolitor-stud-tu Nov 29, 2020
1603843
Set user defaults for filetransfer settings
tmolitor-stud-tu Nov 29, 2020
e087c5b
Merge german language fixes from Echolon
tmolitor-stud-tu Nov 29, 2020
2c90aa6
Stop BGTASK tick thread if the bg fetch task expires
tmolitor-stud-tu Nov 29, 2020
db1457d
Add migration from old image cache to new filetransfer one
tmolitor-stud-tu Nov 30, 2020
70959b4
Remove aes auth tag on file uploads (conversations compat)
tmolitor-stud-tu Nov 30, 2020
400b24a
Make sure aesgcm schemes are compared case insensitive
tmolitor-stud-tu Nov 30, 2020
8828920
Make sure we add the correct outgoing mime type for encrypted messages
tmolitor-stud-tu Nov 30, 2020
c5778a1
Do some small code cleanup
tmolitor-stud-tu Nov 30, 2020
20dfd11
Fix bug in notification manager
tmolitor-stud-tu Nov 30, 2020
1705022
Update notifications with file download preview
tmolitor-stud-tu Nov 30, 2020
64b2c07
Emulate filetransfer UI buttons
tmolitor-stud-tu Dec 1, 2020
e9c61dd
Implement copy functionality again (this time as swipe action)
tmolitor-stud-tu Dec 1, 2020
bd5ed60
Make opening images work again
tmolitor-stud-tu Dec 1, 2020
d6e3612
Update language files
tmolitor-stud-tu Dec 1, 2020
6a6cd3c
Revert some small changes
tmolitor-stud-tu Dec 1, 2020
771d230
Fix swipe actions (better icons/colors)
tmolitor-stud-tu Dec 1, 2020
68619ef
don't reset ownReceivedDeviceList
FriedrichAltheide Dec 1, 2020
8e244a0
cleanup && mediaPicker.allowsEditing = NO;
FriedrichAltheide Dec 1, 2020
f8807e5
Fix steve crash
tmolitor-stud-tu Dec 1, 2020
f6886a7
omemo: broken sessions could have been broken before
FriedrichAltheide Dec 1, 2020
986467e
Fix german translation
tmolitor-stud-tu Dec 2, 2020
f8e3b62
Fix mam scrolling
tmolitor-stud-tu Dec 1, 2020
0e41bf5
Feature request: Search function #482
poormusic2001 Dec 1, 2020
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ Monal/Monal.xcodeproj/xcuserdata/anurodhp.xcuserdatad/xcschemes/jrtplib-static.x
Monal/Monal.xcodeproj/xcuserdata/anurodhp.xcuserdatad/xcschemes/xcschememanagement.plist
contents.xcworkspacedata
._*

# Pods
Monal/Pods
8 changes: 7 additions & 1 deletion MLCrypto/MLCrypto.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1130;
LastUpgradeCheck = 1160;
LastUpgradeCheck = 1220;
ORGANIZATIONNAME = "Anurodh Pokharel";
TargetAttributes = {
26A711BC23C4D67300FC6A86 = {
Expand Down Expand Up @@ -252,6 +252,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down Expand Up @@ -315,6 +316,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down Expand Up @@ -349,6 +351,7 @@
26A711D223C4D67300FC6A86 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
Expand Down Expand Up @@ -384,6 +387,7 @@
26A711D323C4D67300FC6A86 /* AppStore */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
Expand Down Expand Up @@ -479,6 +483,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down Expand Up @@ -513,6 +518,7 @@
26A7120A23C590AD00FC6A86 /* Adhoc */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1160"
LastUpgradeVersion = "1220"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
3 changes: 3 additions & 0 deletions MLCrypto/MLCrypto/MLCrypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class MLCrypto: NSObject {
return nil
}
} else {
assert(false);
return nil;
}
}
Expand All @@ -52,6 +53,7 @@ public class MLCrypto: NSObject {
if #available(iOS 13.0, *) {
return Data(AES.GCM.Nonce())
} else {
assert(false);
return nil;
}
}
Expand All @@ -68,6 +70,7 @@ public class MLCrypto: NSObject {
return nil;
}
} else {
assert(false);
return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion Monal/.bartycrouch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ignoreEmptyStrings = true
unstripped = false

[update.code]
codePaths = ["Classes", "shareSheet-iOS", "monalxmpp", "Monal-iOS", "NotificaionService"]
codePaths = ["Classes", "shareSheet-iOS", "NotificaionService"]
localizablePaths = ["localization", "shareSheet-iOS/localization"]
defaultToKeys = true
additive = true
Expand Down
10 changes: 5 additions & 5 deletions Monal/Classes/AESGcm.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ NS_ASSUME_NONNULL_BEGIN
/**
key size should be 16 or 32
*/
+(MLEncryptedPayload *) encrypt:(NSData *)body keySize:(int) keySize;
+(MLEncryptedPayload*) encrypt:(NSData*) body withKey:(NSData*) gcmKey;
+ (NSData *) decrypt:(NSData *)body withKey:(NSData *) key andIv:(NSData *)iv withAuth:(NSData * _Nullable ) auth;
+(NSData*) genIV;
+(NSData*) genKey:(int) keySize;
+(MLEncryptedPayload* _Nullable) encrypt:(NSData*) body keySize:(int) keySize;
+(MLEncryptedPayload* _Nullable) encrypt:(NSData*) body withKey:(NSData*) gcmKey;
+(NSData* _Nullable) decrypt:(NSData *)body withKey:(NSData *) key andIv:(NSData *)iv withAuth:(NSData * _Nullable) auth;
+(NSData* _Nullable) genIV;
+(NSData* _Nullable) genKey:(int) keySize;

@end

Expand Down
175 changes: 119 additions & 56 deletions Monal/Classes/AESGcm.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
// Copyright © 2019 Monal.im. All rights reserved.
//

#import "MLConstants.h"
#import "AESGcm.h"
#import <MLCrypto/MLCrypto-Swift.h>

#if !TARGET_OS_MACCATALYST
#include <openssl/evp.h>
#include <openssl/rand.h>
#define AES_BLOCK_SIZE 16
#define AUTH_TAG_LENGTH 16
#endif

@implementation AESGcm
Expand All @@ -32,53 +35,90 @@ +(MLEncryptedPayload*) encrypt:(NSData*) body withKey:(NSData*) gcmKey
{
MLCrypto* crypto = [[MLCrypto alloc] init];
EncryptedPayload* payload = [crypto encryptGCMWithKey:gcmKey decryptedContent:body];
if(payload == nil)
{
return nil;
}
NSMutableData* combinedKey = [NSMutableData dataWithData:gcmKey];
[combinedKey appendData:payload.tag];
if(combinedKey == nil)
{
return nil;
}
return [[MLEncryptedPayload alloc] initWithBody:payload.body key:combinedKey iv:payload.iv authTag:payload.tag];
}
else
{
#if !TARGET_OS_MACCATALYST
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
int outlen;
unsigned char outbuf[body.length];
unsigned char tag[16];
EVP_CIPHER_CTX* ctx;
int outlen, tmplen;
unsigned char* outbuf = malloc(body.length + AES_BLOCK_SIZE);
unsigned char tag[AUTH_TAG_LENGTH];
NSMutableData* combinedKey;
NSData* encryptedMessage;

NSData* gcmiv = [self genIV];

NSMutableData *encryptedMessage;
if(gcmiv == nil)
goto end1;

ctx = EVP_CIPHER_CTX_new();
if(ctx == NULL)
goto end1;

/* Set cipher type and mode */
if([gcmKey length]==16) {
if([gcmKey length] == 16) {
EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 16);
}

if([gcmKey length]==32) {
else if([gcmKey length] == 32)
{
EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 32);
}
else
goto end2;

/* Set IV length if default 96 bits is not approp riate */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int) gcmiv.length, NULL);
if(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int)gcmiv.length, NULL) != 1)
goto end2;
OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == (int)gcmiv.length);

/* Initialise key and IV */
EVP_EncryptInit_ex(ctx, NULL, NULL, gcmKey.bytes, gcmiv.bytes);
EVP_CIPHER_CTX_set_padding(ctx,1);
/* Encrypt plaintext */
EVP_EncryptUpdate(ctx, outbuf, &outlen,body.bytes,(int)body.length);
if(EVP_EncryptInit_ex(ctx, NULL, NULL, gcmKey.bytes, gcmiv.bytes) != 1)
goto end2;

// enable padding, always returns 1
assert(EVP_CIPHER_CTX_set_padding(ctx, 1) == 1);

encryptedMessage = [NSMutableData dataWithBytes:outbuf length:outlen];
/* Encrypt plaintext */
if(EVP_EncryptUpdate(ctx, outbuf, &outlen, body.bytes, (int)body.length) == 0)
goto end2;
tmplen = outlen;

/* Finalise: note get no output for GCM */
EVP_EncryptFinal_ex(ctx, outbuf, &outlen);
if(EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen) == 0)
goto end2;
outlen += tmplen;
encryptedMessage = [NSData dataWithBytesNoCopy:outbuf length:outlen];

/* Get tag */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag);
//[encryptedMessage appendBytes:tag length:16];
if(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, AUTH_TAG_LENGTH, tag) != 1)
{
EVP_CIPHER_CTX_free(ctx);
return nil;
}

NSMutableData *combinedKey = [NSMutableData dataWithData:gcmKey];
[combinedKey appendBytes:tag length:16];
combinedKey = [NSMutableData dataWithData:gcmKey];
[combinedKey appendBytes:tag length:AUTH_TAG_LENGTH];

EVP_CIPHER_CTX_free(ctx);
return [[MLEncryptedPayload alloc] initWithBody:encryptedMessage key:combinedKey iv:gcmiv authTag:[NSData dataWithBytes:tag length:16]];
return [[MLEncryptedPayload alloc] initWithBody:encryptedMessage key:combinedKey iv:gcmiv authTag:[NSData dataWithBytes:tag length:AUTH_TAG_LENGTH]];

end2:
EVP_CIPHER_CTX_free(ctx);
end1:
free(outbuf);
return nil;
#else
assert(false);
return nil;
Expand All @@ -95,7 +135,10 @@ +(NSData*) genIV
#if !TARGET_OS_MACCATALYST
//generate iv
unsigned char iv[12];
RAND_bytes(iv, sizeof(iv));
if(RAND_bytes(iv, sizeof(iv)) == 0)
{
return nil;
}
NSData* gcmiv = [[NSData alloc] initWithBytes:iv length:12];
return gcmiv;
#else
Expand All @@ -113,71 +156,91 @@ +(NSData*) genKey:(int) keySize
return [[NSData alloc] initWithBytes:randomBytes length:keySize];
}

+ (NSData *) decrypt:(NSData *)body withKey:(NSData *) key andIv:(NSData *)iv withAuth:( NSData * _Nullable ) auth {
+(NSData*) decrypt:(NSData*) body withKey:(NSData*) key andIv:(NSData*) iv withAuth:(NSData* _Nullable) auth
{
if (@available(iOS 13.0, *)) {
MLCrypto* crypto = [[MLCrypto alloc] init];

MLCrypto *crypto = [[MLCrypto alloc] init];

NSMutableData *combined = [[NSMutableData alloc] init];
NSMutableData* combined = [[NSMutableData alloc] init];
[combined appendData:iv];
[combined appendData:body];
[combined appendData:auth]; //if auth is nil assume it already was apended to body

NSData *toReturn =[crypto decryptGCMWithKey:key encryptedContent:combined];
NSData* toReturn = [crypto decryptGCMWithKey:key encryptedContent:combined];
return toReturn;
} else
}
else
{
#if !TARGET_OS_MACCATALYST
int outlen, rv;
unsigned char outbuf[key.length];
EVP_CIPHER_CTX *ctx =EVP_CIPHER_CTX_new();
assert(iv.length == 12);

NSData* realBody = body;
if(auth == nil)
{
realBody = [NSData dataWithBytesNoCopy:(void* _Nonnull)body.bytes length:body.length - AUTH_TAG_LENGTH freeWhenDone:NO];
auth = [NSData dataWithBytesNoCopy:(void* _Nonnull)body.bytes + (body.length - AUTH_TAG_LENGTH) length:AUTH_TAG_LENGTH freeWhenDone:NO];
}

int outlen, tmplen, retval;
unsigned char* outbuf = malloc(realBody.length + AES_BLOCK_SIZE);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();

/* Select cipher */
if(key.length==16) {
if(key.length == 16) {
EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 16);
}

if(key.length==32) {
else if(key.length == 32)
{
EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 32);
}
else
{
free(outbuf);
EVP_CIPHER_CTX_free(ctx);
return nil;
}

/* Set IV length, omit for 96 bits */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int)iv.length, NULL);
OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == (int)iv.length);

/* Specify key and IV */
EVP_DecryptInit_ex(ctx, NULL, NULL, key.bytes, iv.bytes);
EVP_CIPHER_CTX_set_padding(ctx,1);
/* Decrypt plaintext */
NSMutableData *decdata = [[NSMutableData alloc] initWithCapacity:body.length];

int byteCounter=0;
while(byteCounter<body.length)
// enable padding, always returns 1
assert(EVP_CIPHER_CTX_set_padding(ctx, 1) == 1);

/* Set expected tag value. */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, (int)auth.length, (void*)auth.bytes);

/* Decrypt ciphertext */
if((retval = EVP_DecryptUpdate(ctx, outbuf, &tmplen, realBody.bytes, (int)realBody.length)) == 0)
{
NSRange byteRange= NSMakeRange(byteCounter, key.length);
if(byteCounter+key.length>body.length) byteRange=NSMakeRange(byteCounter, body.length-byteCounter);
unsigned char bytes[byteRange.length];
[body getBytes:bytes range:byteRange];
EVP_DecryptUpdate(ctx, outbuf, &outlen, bytes, (int)byteRange.length);
/* Output decrypted block */
/* Finalise: note get no output for GCM */
rv = EVP_DecryptFinal_ex(ctx, outbuf, &outlen);
[decdata appendBytes:outbuf length:byteRange.length];
byteCounter+=byteRange.length;
DDLogError(@"EVP_DecryptUpdate() --> %ld", (long)retval);
free(outbuf);
EVP_CIPHER_CTX_free(ctx);
return nil;
}
outlen = tmplen;

if(auth) {
/* Set expected tag value. */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, (int)auth.length, auth.bytes);
/* Finalise: note get no output for GCM */
if((retval = EVP_DecryptFinal_ex(ctx, outbuf + tmplen, &tmplen)) <= 0)
{
DDLogError(@"EVP_DecryptFinal_ex() --> %ld", (long)retval);
free(outbuf);
EVP_CIPHER_CTX_free(ctx);
return nil;
}

EVP_CIPHER_CTX_free(ctx);
return decdata;

return [NSData dataWithBytesNoCopy:outbuf length:outlen];
#else
assert(false);
return nil;
#endif
}
}



@end
4 changes: 2 additions & 2 deletions Monal/Classes/AccountsViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
//

#import <UIKit/UIKit.h>
#import <Monal-Swift.h>


@interface AccountsViewController : UITableViewController
@interface AccountsViewController : UITableViewController<MLLQRCodeScannerAccountLoginDeleagte>

@property (nonatomic, strong) UITableView* accountsTable;

Expand Down
Loading