|
|
|
@ -3,55 +3,19 @@ import SessionUtilities
|
|
|
|
|
|
|
|
|
|
internal extension MessageSender {
|
|
|
|
|
|
|
|
|
|
static func encryptWithSignalProtocol(_ plaintext: Data, for publicKey: String, using transaction: Any) throws -> Data {
|
|
|
|
|
|
|
|
|
|
static func encryptWithSignalProtocol(_ plaintext: Data, associatedWith message: Message, for publicKey: String, using transaction: Any) throws -> Data {
|
|
|
|
|
let storage = Configuration.shared.storage
|
|
|
|
|
let cipher = try SMKSecretSessionCipher(sessionResetImplementation: Configuration.shared.sessionRestorationImplementation,
|
|
|
|
|
sessionStore: storage, preKeyStore: storage, signedPreKeyStore: storage, identityStore: storage)
|
|
|
|
|
let useFallbackEncryption: Bool = {
|
|
|
|
|
if (message is SessionRequest) { return true }
|
|
|
|
|
return !Configuration.shared.storage.containsSession(publicKey, deviceId: 1, protocolContext: transaction)
|
|
|
|
|
}()
|
|
|
|
|
let certificate = Configuration.shared.storage.getSenderCertificate(for: publicKey)
|
|
|
|
|
return try cipher.throwswrapped_encryptMessage(recipientPublicKey: publicKey, deviceID: 1, paddedPlaintext: (plaintext as NSData).paddedMessageBody(),
|
|
|
|
|
senderCertificate: certificate, protocolContext: transaction, useFallbackSessionCipher: useFallbackEncryption)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NSError *error;
|
|
|
|
|
// LKSessionResetImplementation *sessionResetImplementation = [LKSessionResetImplementation new];
|
|
|
|
|
//
|
|
|
|
|
// SMKSecretSessionCipher *_Nullable secretCipher =
|
|
|
|
|
// [[SMKSecretSessionCipher alloc] initWithSessionResetImplementation:sessionResetImplementation
|
|
|
|
|
// sessionStore:self.primaryStorage
|
|
|
|
|
// preKeyStore:self.primaryStorage
|
|
|
|
|
// signedPreKeyStore:self.primaryStorage
|
|
|
|
|
// identityStore:self.identityManager
|
|
|
|
|
// error:&error];
|
|
|
|
|
// if (error || !secretCipher) {
|
|
|
|
|
// OWSRaiseException(@"SecretSessionCipherFailure", @"Can't create secret session cipher.");
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // Loki: The way this works is:
|
|
|
|
|
// // • Alice sends a session request (i.e. a pre key bundle) to Bob using fallback encryption.
|
|
|
|
|
// // • She may send any number of subsequent messages also encrypted using fallback encryption.
|
|
|
|
|
// // • When Bob receives the session request, he sets up his Signal cipher session locally and sends back a null message,
|
|
|
|
|
// // now encrypted using Signal encryption.
|
|
|
|
|
// // • Alice receives this, sets up her Signal cipher session locally, and sends any subsequent messages
|
|
|
|
|
// // using Signal encryption.
|
|
|
|
|
//
|
|
|
|
|
// BOOL shouldUseFallbackEncryption = [LKSessionManagementProtocol shouldUseFallbackEncryptionForMessage:message recipientID:recipientID transaction:transaction];
|
|
|
|
|
//
|
|
|
|
|
// if (shouldUseFallbackEncryption) {
|
|
|
|
|
// [LKLogger print:@"[Loki] Using fallback encryption"];
|
|
|
|
|
// } else {
|
|
|
|
|
// [LKLogger print:@"[Loki] Using Signal Encryption"];
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// serializedMessage = [secretCipher throwswrapped_encryptMessageWithRecipientPublicKey:recipientID
|
|
|
|
|
// deviceID:@(OWSDevicePrimaryDeviceId).intValue
|
|
|
|
|
// paddedPlaintext:plainText.paddedMessageBody
|
|
|
|
|
// senderCertificate:messageSend.senderCertificate
|
|
|
|
|
// protocolContext:transaction
|
|
|
|
|
// useFallbackSessionCipher:shouldUseFallbackEncryption
|
|
|
|
|
// error:&error];
|
|
|
|
|
//
|
|
|
|
|
// SCKRaiseIfExceptionWrapperError(error);
|
|
|
|
|
// if (serializedMessage == nil || error != nil) {
|
|
|
|
|
// OWSFailDebug(@"Error while UD encrypting message: %@.", error);
|
|
|
|
|
// return nil;
|
|
|
|
|
// }
|
|
|
|
|
// messageType = TSUnidentifiedSenderMessageType;
|
|
|
|
|
|
|
|
|
|
static func encryptWithSharedSenderKeys(_ plaintext: Data, for groupPublicKey: String, using transaction: Any) throws -> Data {
|
|
|
|
|
// 1. ) Encrypt the data with the user's sender key
|
|
|
|
|
guard let userPublicKey = Configuration.shared.storage.getUserPublicKey() else {
|
|
|
|
|