private void CalculateSecretChatParamsAsync()
        {
            _mtProtoService.GetDHConfigAsync(new TLInt(0), new TLInt(0),
                                             result =>
            {
                var dhConfig = (TLDHConfig)result;
                if (!TLUtils.CheckPrime(dhConfig.P.Data, dhConfig.G.Value))
                {
                    return;
                }

                var aBytes = new byte[256];
                var random = new SecureRandom();
                random.NextBytes(aBytes);
                _a = TLString.FromBigEndianData(aBytes);
                _p = dhConfig.P;
                _g = dhConfig.G;
#if DEBUG
                //Thread.Sleep(15000);
#endif
                var gaBytes = MTProtoService.GetGB(aBytes, dhConfig.G, dhConfig.P);
                _ga         = TLString.FromBigEndianData(gaBytes);
                if (_invokeDelayedUserAction)
                {
                    _invokeDelayedUserAction = false;
                    CreateSecretChat();
                }
            },
                                             error =>
            {
                Execute.ShowDebugMessage("messages.getDhConfig error: " + error);
            });
        }
Esempio n. 2
0
        private async void OnAnswerRequested(VoipPhoneCall sender, CallAnswerEventArgs args)
        {
            if (_phoneCall != null)
            {
                await UpdateStateAsync(TLPhoneCallState.ExchangingKeys);

                var reqConfig = new TLMessagesGetDHConfig {
                    Version = 0, RandomLength = 256
                };

                var config = await SendRequestAsync <TLMessagesDHConfig>("messages.getDhConfig", reqConfig);

                if (config.IsSucceeded)
                {
                    var dh = config.Result;
                    if (!TLUtils.CheckPrime(dh.P, dh.G))
                    {
                        return;
                    }

                    var salt         = new byte[256];
                    var secureRandom = new SecureRandom();
                    secureRandom.NextBytes(salt);

                    secretP = dh.P;
                    a_or_b  = salt;

                    var g_b = MTProtoService.GetGB(salt, dh.G, dh.P);

                    var request = new TLPhoneAcceptCall
                    {
                        GB       = g_b,
                        Peer     = _phoneCall.ToInputPhoneCall(),
                        Protocol = new TLPhoneCallProtocol
                        {
                            IsUdpP2p       = true,
                            IsUdpReflector = true,
                            MinLayer       = Telegram.Api.Constants.CallsMinLayer,
                            MaxLayer       = Telegram.Api.Constants.CallsMaxLayer,
                        }
                    };

                    var response = await SendRequestAsync <TLPhonePhoneCall>("phone.acceptCall", request);

                    if (response.IsSucceeded)
                    {
                        _systemCall.NotifyCallActive();
                        Handle(new TLUpdatePhoneCall {
                            PhoneCall = response.Result.PhoneCall
                        });
                    }
                }
            }
        }
Esempio n. 3
0
        internal async void OutgoingCall(int userId, long accessHash)
        {
            await UpdateStateAsync(TLPhoneCallState.Requesting);

            var coordinator = VoipCallCoordinator.GetDefault();
            var call        = coordinator.RequestNewOutgoingCall("Unigram", _user.FullName, "Unigram", VoipPhoneCallMedia.Audio);

            _outgoing   = true;
            _systemCall = call;
            _systemCall.AnswerRequested += OnAnswerRequested;
            _systemCall.RejectRequested += OnRejectRequested;

            var reqConfig = new TLMessagesGetDHConfig {
                Version = 0, RandomLength = 256
            };

            var config = await SendRequestAsync <TLMessagesDHConfig>("messages.getDhConfig", reqConfig);

            if (config.IsSucceeded)
            {
                var dh = config.Result;
                if (!TLUtils.CheckPrime(dh.P, dh.G))
                {
                    return;
                }

                var salt         = new byte[256];
                var secureRandom = new SecureRandom();
                secureRandom.NextBytes(salt);

                secretP = dh.P;
                a_or_b  = salt;
                g_a     = MTProtoService.GetGB(salt, dh.G, dh.P);

                var request = new TLPhoneRequestCall
                {
                    UserId = new TLInputUser {
                        UserId = userId, AccessHash = accessHash
                    },
                    RandomId = TLInt.Random(),
                    GAHash   = Utils.ComputeSHA256(g_a),
                    Protocol = new TLPhoneCallProtocol
                    {
                        IsUdpP2p       = true,
                        IsUdpReflector = true,
                        MinLayer       = Telegram.Api.Constants.CallsMinLayer,
                        MaxLayer       = Telegram.Api.Constants.CallsMaxLayer,
                    }
                };

                var response = await SendRequestAsync <TLPhonePhoneCall>("phone.requestCall", request);

                if (response.IsSucceeded)
                {
                    var update = new TLUpdatePhoneCall {
                        PhoneCall = response.Result.PhoneCall
                    };

                    Handle(update);
                    await UpdateStateAsync(TLPhoneCallState.Waiting);
                }
                else
                {
                    Debugger.Break();
                }
            }
        }
Esempio n. 4
0
        public async void Handle(TLUpdatePhoneCall update)
        {
            await VoIPConnection.Current.SendUpdateAsync(update);

            await Task.Delay(2000);

            //if (update.PhoneCall is TLPhoneCallDiscarded discarded)
            //{
            //    if (discarded.IsNeedRating)
            //    {
            //        Debugger.Break();
            //    }

            //    if (discarded.IsNeedDebug)
            //    {
            //        Debugger.Break();
            //    }
            //}

            return;

            if (update.PhoneCall is TLPhoneCallRequested callRequested)
            {
                var reqReceived = new TLPhoneReceivedCall();
                reqReceived.Peer            = new TLInputPhoneCall();
                reqReceived.Peer.Id         = callRequested.Id;
                reqReceived.Peer.AccessHash = callRequested.AccessHash;

                ProtoService.SendRequestAsync <bool>("phone.receivedCall", reqReceived, null, null);

                var user = CacheService.GetUser(callRequested.AdminId) as TLUser;

                Execute.BeginOnUIThread(async() =>
                {
                    var dialog = await TLMessageDialog.ShowAsync(user.DisplayName, "CAAAALLL", "OK", "Cancel");
                    if (dialog == Windows.UI.Xaml.Controls.ContentDialogResult.Primary)
                    {
                        var config = await ProtoService.GetDHConfigAsync(0, 256);
                        if (config.IsSucceeded)
                        {
                            var dh = config.Result;
                            if (!TLUtils.CheckPrime(dh.P, dh.G))
                            {
                                return;
                            }

                            secretP = dh.P;

                            var salt         = new byte[256];
                            var secureRandom = new SecureRandom();
                            secureRandom.NextBytes(salt);

                            a_or_b = salt;

                            var g_b = MTProtoService.GetGB(salt, dh.G, dh.P);

                            var request = new TLPhoneAcceptCall
                            {
                                GB   = g_b,
                                Peer = new TLInputPhoneCall
                                {
                                    Id         = callRequested.Id,
                                    AccessHash = callRequested.AccessHash
                                },
                                Protocol = new TLPhoneCallProtocol
                                {
                                    IsUdpP2p       = true,
                                    IsUdpReflector = true,
                                    MinLayer       = 65,
                                    MaxLayer       = 65,
                                }
                            };

                            var response = await ProtoService.SendRequestAsync <TLPhonePhoneCall>("phone.acceptCall", request);
                            if (response.IsSucceeded)
                            {
                            }
                        }
                    }
                    else
                    {
                        var req             = new TLPhoneDiscardCall();
                        req.Peer            = new TLInputPhoneCall();
                        req.Peer.Id         = callRequested.Id;
                        req.Peer.AccessHash = callRequested.AccessHash;
                        req.Reason          = new TLPhoneCallDiscardReasonHangup();

                        ProtoService.SendRequestAsync <TLPhonePhoneCall>("phone.acceptCall", req, null, null);
                    }
                });
            }
            else if (update.PhoneCall is TLPhoneCall call)
            {
                var auth_key = computeAuthKey(call);
                var g_a      = call.GAOrB;

                var buffer = TLUtils.Combine(auth_key, g_a);
                var sha256 = Utils.ComputeSHA256(buffer);

                var emoji = EncryptionKeyEmojifier.EmojifyForCall(sha256);

                var user = CacheService.GetUser(call.AdminId) as TLUser;

                Execute.BeginOnUIThread(async() =>
                {
                    var dialog = await TLMessageDialog.ShowAsync(user.DisplayName, string.Join(" ", emoji), "OK");
                });
            }
        }
Esempio n. 5
0
        public void StartOutgoingCall(TLInputUserBase userId)
        {
            var salt   = new Byte[256];
            var random = new SecureRandom();

            random.NextBytes(salt);

            var version      = _lastVersion ?? new TLInt(0);
            var randomLength = new TLInt(256);

            _mtProtoService.GetDHConfigAsync(version, randomLength,
                                             result =>
            {
                ConfigureDeviceForCall();
                ShowNotifications();
                StartConnectionSound();
                DispatchStateChanged(PhoneCallState.STATE_REQUESTING);

                _eventAggregator.Publish(new PhoneCallEventArgs("NotificationCenter.didStartedCall"));

                var dhConfig = result as TLDHConfig;
                if (dhConfig != null)
                {
                    if (!TLUtils.CheckPrime(dhConfig.P.Data, dhConfig.G.Value))
                    {
                        CallFailed();
                        return;
                    }

                    _secretP      = dhConfig.P;
                    _secretG      = dhConfig.G;
                    _secretRandom = dhConfig.Random;
                }

                for (var i = 0; i < 256; i++)
                {
                    salt[i] = (byte)(salt[i] ^ _secretRandom.Data[i]);
                }

                var gaBytes = MTProtoService.GetGB(salt, _secretG, _secretP);

                var protocol = new TLPhoneCallProtocol
                {
                    Flags        = new TLInt(0),
                    UdpP2P       = true,
                    UdpReflector = true,
                    MinLayer     = new TLInt(CALL_MIN_LAYER),
                    MaxLayer     = new TLInt(CALL_MAX_LAYER)
                };
                _ga        = gaBytes;
                var gaHash = Utils.ComputeSHA256(_ga);

                _mtProtoService.RequestCallAsync(userId, TLInt.Random(), TLString.FromBigEndianData(gaHash), protocol,
                                                 result2 =>
                {
                    _call = result2;
                    _aOrB = salt;
                    DispatchStateChanged(PhoneCallState.STATE_WAITING);
                    //if (_endCallAfterRequest)
                    //{
                    //    Hangup();
                    //    return;
                    //}
                },
                                                 error2 =>
                {
                });
            },
                                             error =>
            {
                Helpers.Execute.ShowDebugMessage("messages.getDHConfig error " + error);
                CallFailed();
            });
        }