From cf40afc5a965059d81fb3ef489868fe73f4866a1 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Fri, 2 Oct 2020 20:11:09 +0100 Subject: [PATCH 01/10] Don't create temp keys when not nocessary --- submodules/MtProtoKit/Sources/MTBackupAddressSignals.m | 2 +- .../MtProtoKit/Sources/MTDiscoverDatacenterAddressAction.m | 4 +++- submodules/MtProtoKit/Sources/MTProxyConnectivity.m | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/submodules/MtProtoKit/Sources/MTBackupAddressSignals.m b/submodules/MtProtoKit/Sources/MTBackupAddressSignals.m index 8eead4103e..6375bcc286 100644 --- a/submodules/MtProtoKit/Sources/MTBackupAddressSignals.m +++ b/submodules/MtProtoKit/Sources/MTBackupAddressSignals.m @@ -240,7 +240,7 @@ static NSString *makeRandomPadding() { apiEnvironment.disableUpdates = true; apiEnvironment.langPack = currentContext.apiEnvironment.langPack; - MTContext *context = [[MTContext alloc] initWithSerialization:currentContext.serialization encryptionProvider:currentContext.encryptionProvider apiEnvironment:apiEnvironment isTestingEnvironment:currentContext.isTestingEnvironment useTempAuthKeys:address.datacenterId != 0 ? currentContext.useTempAuthKeys : false]; + MTContext *context = [[MTContext alloc] initWithSerialization:currentContext.serialization encryptionProvider:currentContext.encryptionProvider apiEnvironment:apiEnvironment isTestingEnvironment:currentContext.isTestingEnvironment useTempAuthKeys:false]; if (address.datacenterId != 0) { //context.keychain = currentContext.keychain; diff --git a/submodules/MtProtoKit/Sources/MTDiscoverDatacenterAddressAction.m b/submodules/MtProtoKit/Sources/MTDiscoverDatacenterAddressAction.m index bfd5b5292d..980cb49651 100644 --- a/submodules/MtProtoKit/Sources/MTDiscoverDatacenterAddressAction.m +++ b/submodules/MtProtoKit/Sources/MTDiscoverDatacenterAddressAction.m @@ -117,8 +117,10 @@ [_requestService addRequest:request]; } - else + else { + [context authInfoForDatacenterWithIdRequired:_targetDatacenterId isCdn:false selector:MTDatacenterAuthInfoSelectorPersistent]; + } } } diff --git a/submodules/MtProtoKit/Sources/MTProxyConnectivity.m b/submodules/MtProtoKit/Sources/MTProxyConnectivity.m index 0ac9eb2676..a12f89d080 100644 --- a/submodules/MtProtoKit/Sources/MTProxyConnectivity.m +++ b/submodules/MtProtoKit/Sources/MTProxyConnectivity.m @@ -64,7 +64,7 @@ MTPayloadData payloadData; NSData *data = [MTDiscoverConnectionSignals payloadData:&payloadData context:context address:address]; - MTContext *proxyContext = [[MTContext alloc] initWithSerialization:context.serialization encryptionProvider:context.encryptionProvider apiEnvironment:[[context apiEnvironment] withUpdatedSocksProxySettings:settings] isTestingEnvironment:context.isTestingEnvironment useTempAuthKeys:context.useTempAuthKeys]; + MTContext *proxyContext = [[MTContext alloc] initWithSerialization:context.serialization encryptionProvider:context.encryptionProvider apiEnvironment:[[context apiEnvironment] withUpdatedSocksProxySettings:settings] isTestingEnvironment:context.isTestingEnvironment useTempAuthKeys:false]; MTTcpConnection *connection = [[MTTcpConnection alloc] initWithContext:proxyContext datacenterId:datacenterId scheme:[[MTTransportScheme alloc] initWithTransportClass:[MTTcpConnection class] address:address media:false] interface:nil usageCalculationInfo:nil]; __weak MTTcpConnection *weakConnection = connection; From a0c42a3e63f255261f46d424b9408ae4bb204b2b Mon Sep 17 00:00:00 2001 From: Ali <> Date: Fri, 2 Oct 2020 22:23:33 +0100 Subject: [PATCH 02/10] [Temp] Control temp keys with app_config --- .../SyncCore/Sources/NetworkSettings.swift | 16 ++++++++++++++-- .../Sources/ManagedAppConfigurationUpdates.swift | 9 +++++++++ submodules/TelegramCore/Sources/Network.swift | 13 ++++++++++++- .../TelegramCore/Sources/NetworkSettings.swift | 4 ++-- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/submodules/SyncCore/Sources/NetworkSettings.swift b/submodules/SyncCore/Sources/NetworkSettings.swift index b8341c79fb..ba104abbc9 100644 --- a/submodules/SyncCore/Sources/NetworkSettings.swift +++ b/submodules/SyncCore/Sources/NetworkSettings.swift @@ -4,21 +4,27 @@ public struct NetworkSettings: PreferencesEntry, Equatable { public var reducedBackupDiscoveryTimeout: Bool public var applicationUpdateUrlPrefix: String? public var backupHostOverride: String? + public var defaultEnableTempKeys: Bool + public var userEnableTempKeys: Bool? public static var defaultSettings: NetworkSettings { - return NetworkSettings(reducedBackupDiscoveryTimeout: false, applicationUpdateUrlPrefix: nil, backupHostOverride: nil) + return NetworkSettings(reducedBackupDiscoveryTimeout: false, applicationUpdateUrlPrefix: nil, backupHostOverride: nil, defaultEnableTempKeys: true, userEnableTempKeys: nil) } - public init(reducedBackupDiscoveryTimeout: Bool, applicationUpdateUrlPrefix: String?, backupHostOverride: String?) { + public init(reducedBackupDiscoveryTimeout: Bool, applicationUpdateUrlPrefix: String?, backupHostOverride: String?, defaultEnableTempKeys: Bool, userEnableTempKeys: Bool?) { self.reducedBackupDiscoveryTimeout = reducedBackupDiscoveryTimeout self.applicationUpdateUrlPrefix = applicationUpdateUrlPrefix self.backupHostOverride = backupHostOverride + self.defaultEnableTempKeys = defaultEnableTempKeys + self.userEnableTempKeys = userEnableTempKeys } public init(decoder: PostboxDecoder) { self.reducedBackupDiscoveryTimeout = decoder.decodeInt32ForKey("reducedBackupDiscoveryTimeout", orElse: 0) != 0 self.applicationUpdateUrlPrefix = decoder.decodeOptionalStringForKey("applicationUpdateUrlPrefix") self.backupHostOverride = decoder.decodeOptionalStringForKey("backupHostOverride") + self.defaultEnableTempKeys = decoder.decodeBoolForKey("defaultEnableTempKeys", orElse: true) + self.userEnableTempKeys = decoder.decodeOptionalBoolForKey("userEnableTempKeys") } public func encode(_ encoder: PostboxEncoder) { @@ -33,6 +39,12 @@ public struct NetworkSettings: PreferencesEntry, Equatable { } else { encoder.encodeNil(forKey: "backupHostOverride") } + encoder.encodeBool(self.defaultEnableTempKeys, forKey: "defaultEnableTempKeys") + if let userEnableTempKeys = self.userEnableTempKeys { + encoder.encodeBool(userEnableTempKeys, forKey: "userEnableTempKeys") + } else { + encoder.encodeNil(forKey: "userEnableTempKeys") + } } public func isEqual(to: PreferencesEntry) -> Bool { diff --git a/submodules/TelegramCore/Sources/ManagedAppConfigurationUpdates.swift b/submodules/TelegramCore/Sources/ManagedAppConfigurationUpdates.swift index b7a757eee1..607c7d5102 100644 --- a/submodules/TelegramCore/Sources/ManagedAppConfigurationUpdates.swift +++ b/submodules/TelegramCore/Sources/ManagedAppConfigurationUpdates.swift @@ -23,6 +23,15 @@ func updateAppConfigurationOnce(postbox: Postbox, network: Network) -> Signal NetworkSettings) { +public func updateNetworkSettingsInteractively(transaction: Transaction, network: Network?, _ f: @escaping (NetworkSettings) -> NetworkSettings) { var updateNetwork = false var updatedSettings: NetworkSettings? transaction.updatePreferencesEntry(key: PreferencesKeys.networkSettings, { current in @@ -33,7 +33,7 @@ public func updateNetworkSettingsInteractively(transaction: Transaction, network return updated }) - if updateNetwork, let updatedSettings = updatedSettings { + if let network = network, updateNetwork, let updatedSettings = updatedSettings { network.context.updateApiEnvironment { current in return current?.withUpdatedNetworkSettings(updatedSettings.mtNetworkSettings) } From d19489b0836344ea60cfcf078f7e85d339bcd394 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Fri, 2 Oct 2020 22:26:25 +0100 Subject: [PATCH 03/10] Bump version --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 625c56dc0f..db4f5cdcf8 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ include Utils.makefile -APP_VERSION="7.1" +APP_VERSION="7.1.1" CORE_COUNT=$(shell sysctl -n hw.logicalcpu) CORE_COUNT_MINUS_ONE=$(shell expr ${CORE_COUNT} \- 1) From b58d71c1c31657da9b78d92387de003e31fb34d1 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Sat, 3 Oct 2020 11:19:42 +0100 Subject: [PATCH 04/10] Cleanup and add logging --- .../Sources/MTDatacenterAuthMessageService.m | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/submodules/MtProtoKit/Sources/MTDatacenterAuthMessageService.m b/submodules/MtProtoKit/Sources/MTDatacenterAuthMessageService.m index e9d94c678b..194dfdad9e 100644 --- a/submodules/MtProtoKit/Sources/MTDatacenterAuthMessageService.m +++ b/submodules/MtProtoKit/Sources/MTDatacenterAuthMessageService.m @@ -109,9 +109,9 @@ static NSDictionary *selectPublicKey(NSArray *fingerprints, NSArray _encryptionProvider; bool _tempAuth; - MTSessionInfo *_sessionInfo; MTDatacenterAuthStage _stage; int64_t _currentStageMessageId; @@ -165,7 +164,6 @@ typedef enum { { _encryptionProvider = context.encryptionProvider; _tempAuth = tempAuth; - _sessionInfo = [[MTSessionInfo alloc] initWithRandomSessionIdAndContext:context]; } return self; } @@ -267,7 +265,7 @@ typedef enum { [reqDhBuffer appendInt64:_dhPublicKeyFingerprint]; [reqDhBuffer appendTLBytes:_dhEncryptedData]; - NSString *messageDescription = [NSString stringWithFormat:@"reqDh nonce:%@ serverNonce:%@ p:%@ q:%@ fingerprint:%llx", _nonce, _serverNonce, _dhP, _dhQ, _dhPublicKeyFingerprint]; + NSString *messageDescription = [NSString stringWithFormat:@"reqDh nonce:%@ serverNonce:%@ p:%@ q:%@ fingerprint:%llx dhEncryptedData:%d bytes", _nonce, _serverNonce, _dhP, _dhQ, _dhPublicKeyFingerprint, (int)_dhEncryptedData.length]; MTOutgoingMessage *message = [[MTOutgoingMessage alloc] initWithData:reqDhBuffer.data metadata:messageDescription additionalDebugDescription:nil shortMetadata:messageDescription messageId:_currentStageMessageId messageSeqNo:_currentStageMessageSeqNo]; return [[MTMessageTransaction alloc] initWithMessagePayload:@[message] prepared:nil failed:nil completion:^(NSDictionary *messageInternalIdToTransactionId, NSDictionary *messageInternalIdToPreparedMessage, __unused NSDictionary *messageInternalIdToQuickAckId) { @@ -402,7 +400,11 @@ typedef enum { arc4random_buf(&random, 1); [dataWithHash appendBytes:&random length:1]; } + NSData *encryptedData = MTRsaEncrypt(_encryptionProvider, [publicKey objectForKey:@"key"], dataWithHash); + if (MTLogEnabled()) { + MTLog(@"[MTDatacenterAuthMessageService#%p encryptedData length %d dataWithHash length %d]", self, (int)encryptedData.length, (int)dataWithHash.length); + } if (encryptedData.length < 256) { NSMutableData *newEncryptedData = [[NSMutableData alloc] init]; @@ -438,8 +440,11 @@ typedef enum { arc4random_buf(&random, 1); [dataWithHash appendBytes:&random length:1]; } - + NSData *encryptedData = MTRsaEncrypt(_encryptionProvider, [publicKey objectForKey:@"key"], dataWithHash); + if (MTLogEnabled()) { + MTLog(@"[MTDatacenterAuthMessageService#%p encryptedData length %d dataWithHash length %d]", self, (int)encryptedData.length, (int)dataWithHash.length); + } if (encryptedData.length < 256) { NSMutableData *newEncryptedData = [[NSMutableData alloc] init]; From 061591cf8c2d1a38ebfe52313f1ed6914d1fadb9 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Sat, 3 Oct 2020 11:21:48 +0100 Subject: [PATCH 05/10] Use OpenSSL for RSA --- submodules/MtProtoKit/Sources/MTEncryption.m | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/submodules/MtProtoKit/Sources/MTEncryption.m b/submodules/MtProtoKit/Sources/MTEncryption.m index 4df030344c..348e3065a3 100644 --- a/submodules/MtProtoKit/Sources/MTEncryption.m +++ b/submodules/MtProtoKit/Sources/MTEncryption.m @@ -292,12 +292,13 @@ NSData *MTAesDecrypt(NSData *data, NSData *key, NSData *iv) NSData *MTRsaEncrypt(id provider, NSString *publicKey, NSData *data) { #if TARGET_OS_IOS - NSMutableData *updatedData = [[NSMutableData alloc] initWithData:data]; + return [provider rsaEncryptWithPublicKey:publicKey data:data]; + /*NSMutableData *updatedData = [[NSMutableData alloc] initWithData:data]; while (updatedData.length < 256) { uint8_t zero = 0; [updatedData replaceBytesInRange:NSMakeRange(0, 0) withBytes:&zero length:1]; } - return [MTRsa encryptData:updatedData publicKey:publicKey]; + return [MTRsa encryptData:updatedData publicKey:publicKey];*/ #else return [provider macosRSAEncrypt:publicKey data:data]; #endif From 3be79d78f08714cae37da2741475e8c9f86ce616 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Sat, 3 Oct 2020 11:22:05 +0100 Subject: [PATCH 06/10] Cleanup [nocache] --- submodules/MtProtoKit/Sources/MTProto.m | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/submodules/MtProtoKit/Sources/MTProto.m b/submodules/MtProtoKit/Sources/MTProto.m index 62e23fff8b..c0c1d8177b 100644 --- a/submodules/MtProtoKit/Sources/MTProto.m +++ b/submodules/MtProtoKit/Sources/MTProto.m @@ -1726,8 +1726,13 @@ static const NSUInteger MTMaxUnacknowledgedMessageCount = 64; { [[MTProto managerQueue] dispatchOnQueue:^ { - if (transport != _transport || completion == nil) + if (transport != _transport || completion == nil) { return; + } + + if (_useUnauthorizedMode) { + return; + } MTDatacenterAuthKey *authKey = [self getAuthKeyForCurrentScheme:scheme createIfNeeded:false authInfoSelector:nil]; if (authKey == nil) { @@ -2038,11 +2043,18 @@ static NSString *dumpHexString(NSData *data, int maxLength) { - (void)handleMissingKey:(MTTransportScheme *)scheme { NSAssert([[MTProto managerQueue] isCurrentQueue], @"invalid queue"); + if (_useUnauthorizedMode) { + if (MTLogEnabled()) { + MTLog(@"[MTProto#%p@%p don't handleMissingKey when useUnauthorizedMode]", self, _context); + } + return; + } + MTDatacenterAuthInfoSelector authInfoSelector; [self getAuthKeyForCurrentScheme:scheme createIfNeeded:false authInfoSelector:&authInfoSelector]; if (MTLogEnabled()) { - MTLog(@"[MTProto#%p@%p missing key %lld selector]", self, _context, _validAuthInfo.authInfo.authKeyId, authInfoSelector); + MTLog(@"[MTProto#%p@%p missing key %lld selector %d]", self, _context, _validAuthInfo.authInfo.authKeyId, authInfoSelector); } if (_useExplicitAuthKey != nil) { @@ -2618,7 +2630,7 @@ static NSString *dumpHexString(NSData *data, int maxLength) { { [_context setGlobalTimeDifference:timeDifference]; - if (saltList != nil) + if (!_useUnauthorizedMode && saltList != nil) { if (_useExplicitAuthKey) { if (_validAuthInfo != nil && _validAuthInfo.selector == authInfoSelector) { From bf72e315e409657c8f93d6cfb2c30bed289e82e0 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Sat, 3 Oct 2020 23:33:48 +0100 Subject: [PATCH 07/10] Move settings to config [skip ci] --- .../Sources/ManagedAppConfigurationUpdates.swift | 9 --------- .../Sources/ManagedConfigurationUpdates.swift | 4 ++++ 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/submodules/TelegramCore/Sources/ManagedAppConfigurationUpdates.swift b/submodules/TelegramCore/Sources/ManagedAppConfigurationUpdates.swift index 607c7d5102..b7a757eee1 100644 --- a/submodules/TelegramCore/Sources/ManagedAppConfigurationUpdates.swift +++ b/submodules/TelegramCore/Sources/ManagedAppConfigurationUpdates.swift @@ -23,15 +23,6 @@ func updateAppConfigurationOnce(postbox: Postbox, network: Network) -> Signal Date: Sat, 3 Oct 2020 23:46:18 +0100 Subject: [PATCH 08/10] Change defaults [nocache] --- submodules/SyncCore/Sources/NetworkSettings.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/submodules/SyncCore/Sources/NetworkSettings.swift b/submodules/SyncCore/Sources/NetworkSettings.swift index ba104abbc9..94d17222eb 100644 --- a/submodules/SyncCore/Sources/NetworkSettings.swift +++ b/submodules/SyncCore/Sources/NetworkSettings.swift @@ -8,7 +8,7 @@ public struct NetworkSettings: PreferencesEntry, Equatable { public var userEnableTempKeys: Bool? public static var defaultSettings: NetworkSettings { - return NetworkSettings(reducedBackupDiscoveryTimeout: false, applicationUpdateUrlPrefix: nil, backupHostOverride: nil, defaultEnableTempKeys: true, userEnableTempKeys: nil) + return NetworkSettings(reducedBackupDiscoveryTimeout: false, applicationUpdateUrlPrefix: nil, backupHostOverride: nil, defaultEnableTempKeys: false, userEnableTempKeys: nil) } public init(reducedBackupDiscoveryTimeout: Bool, applicationUpdateUrlPrefix: String?, backupHostOverride: String?, defaultEnableTempKeys: Bool, userEnableTempKeys: Bool?) { @@ -23,7 +23,7 @@ public struct NetworkSettings: PreferencesEntry, Equatable { self.reducedBackupDiscoveryTimeout = decoder.decodeInt32ForKey("reducedBackupDiscoveryTimeout", orElse: 0) != 0 self.applicationUpdateUrlPrefix = decoder.decodeOptionalStringForKey("applicationUpdateUrlPrefix") self.backupHostOverride = decoder.decodeOptionalStringForKey("backupHostOverride") - self.defaultEnableTempKeys = decoder.decodeBoolForKey("defaultEnableTempKeys", orElse: true) + self.defaultEnableTempKeys = decoder.decodeBoolForKey("defaultEnableTempKeys", orElse: false) self.userEnableTempKeys = decoder.decodeOptionalBoolForKey("userEnableTempKeys") } From 4de8005453f5bf96c47ed5daf9cd9d123028c49f Mon Sep 17 00:00:00 2001 From: Ali <> Date: Sun, 4 Oct 2020 00:06:29 +0100 Subject: [PATCH 09/10] Don't fail on cancel [nocache] --- submodules/MtProtoKit/Sources/MTDatacenterAuthAction.m | 1 - 1 file changed, 1 deletion(-) diff --git a/submodules/MtProtoKit/Sources/MTDatacenterAuthAction.m b/submodules/MtProtoKit/Sources/MTDatacenterAuthAction.m index 736b69f546..1a36bbeecc 100644 --- a/submodules/MtProtoKit/Sources/MTDatacenterAuthAction.m +++ b/submodules/MtProtoKit/Sources/MTDatacenterAuthAction.m @@ -180,7 +180,6 @@ - (void)cancel { [self cleanup]; - [self fail]; } - (void)complete { From 6f82d09f40f31b47e831a068664871eebbf21718 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Mon, 5 Oct 2020 17:20:19 +0100 Subject: [PATCH 10/10] Transform updateShortChatMessage author to peerUser, not peerChat --- submodules/TelegramCore/Sources/UpdateMessageService.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/submodules/TelegramCore/Sources/UpdateMessageService.swift b/submodules/TelegramCore/Sources/UpdateMessageService.swift index bca8a5a76e..32e797fb71 100644 --- a/submodules/TelegramCore/Sources/UpdateMessageService.swift +++ b/submodules/TelegramCore/Sources/UpdateMessageService.swift @@ -58,7 +58,7 @@ class UpdateMessageService: NSObject, MTMessageService { self.putNext(groups) } case let .updateShortChatMessage(flags, id, fromId, chatId, message, pts, ptsCount, date, fwdFrom, viaBotId, replyHeader, entities): - let generatedMessage = Api.Message.message(flags: flags, id: id, fromId: .peerChat(chatId: fromId), peerId: Api.Peer.peerChat(chatId: chatId), fwdFrom: fwdFrom, viaBotId: viaBotId, replyTo: replyHeader, date: date, message: message, media: Api.MessageMedia.messageMediaEmpty, replyMarkup: nil, entities: entities, views: nil, forwards: nil, replies: nil, editDate: nil, postAuthor: nil, groupedId: nil, restrictionReason: nil) + let generatedMessage = Api.Message.message(flags: flags, id: id, fromId: .peerUser(userId: fromId), peerId: Api.Peer.peerChat(chatId: chatId), fwdFrom: fwdFrom, viaBotId: viaBotId, replyTo: replyHeader, date: date, message: message, media: Api.MessageMedia.messageMediaEmpty, replyMarkup: nil, entities: entities, views: nil, forwards: nil, replies: nil, editDate: nil, postAuthor: nil, groupedId: nil, restrictionReason: nil) let update = Api.Update.updateNewMessage(message: generatedMessage, pts: pts, ptsCount: ptsCount) let groups = groupUpdates([update], users: [], chats: [], date: date, seqRange: nil) if groups.count != 0 {