Skip to content

Commit

Permalink
fix: use secure crypto for ios socketio secret
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkabuki committed Oct 30, 2023
1 parent d758341 commit 96b97db
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
15 changes: 1 addition & 14 deletions packages/mobile/ios/Quiet/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,6 @@ - (void) createDataDirectory {
self.dataPath = [dataDirectory create];
}

NSString* randomStringWithLength(int length) {
NSString *letters = @"0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity:length];

for (int i=0; i<length; i++) {
uint32_t rand = arc4random_uniform((uint32_t)[letters length]);
unichar character = [letters characterAtIndex:rand];
[randomString appendFormat:@"%C", character];
}

return randomString;
}

- (void) initWebsocketConnection {
/*
* We have to wait for RCTBridge listeners to be initialized, yet we must be sure to deliver the event containing data port information.
Expand All @@ -130,7 +117,7 @@ - (void) spinupBackend:(BOOL)init {
Utils *utils = [Utils new];

if (self.socketIOSecret == nil) {
self.socketIOSecret = [utils generateRandomStringWithLength:(20)];
self.socketIOSecret = [utils generateSecretWithLength:(20)];
}

self.dataPort = [findFreePort getFirstStartingFromPort:11000];
Expand Down
18 changes: 12 additions & 6 deletions packages/mobile/ios/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
class Utils: NSObject {

@objc
func generateRandomString(length: Int) -> String {
let letters = "0123456789"
var randomString = String()
func generateSecret(length: Int) -> String {
let characters = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
var randomString = ""

for _ in 0..<length {
let rand = Int(arc4random_uniform(UInt32(letters.count)))
let character = letters[letters.index(letters.startIndex, offsetBy: rand)]
randomString.append(character)
var random: UInt8 = 0
let result = SecRandomCopyBytes(kSecRandomDefault, 1, &random)

if result == errSecSuccess {
let index = Int(random) % characters.count
randomString.append(characters[index])
} else {
fatalError("Unable to generate random byte.")
}
}

return randomString
Expand Down

0 comments on commit 96b97db

Please sign in to comment.