forked from sochix/TLSharp
-
Notifications
You must be signed in to change notification settings - Fork 12
Fix CI and unstable transport #41
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
Open
aarani
wants to merge
5
commits into
nblockchain:master
Choose a base branch
from
aarani:rewritten-transport
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3151c23
Tests: load NumberToSendMessage in init process
aarani 03765d8
Core: remove seqNo and sessionId from session file
aarani 400b9c1
Core: port AES-256-CTR from BoringSSL
aarani 8323199
Gen,TL: Transport layer rewrite [1/2]
aarani 0866650
Core,Tests: Transport layer rewrite [2/2]
aarani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,60 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using TgSharp.Core.Network; | ||
|
|
||
| namespace TgSharp.Core.Auth | ||
| { | ||
| public static class Authenticator | ||
| internal class Authenticator | ||
| { | ||
| public static async Task<Step3_Response> DoAuthentication(TcpTransport transport, CancellationToken token = default(CancellationToken)) | ||
| private readonly MtProtoPlainSender sender; | ||
| private TaskCompletionSource<Step3_Response> completionSource; | ||
| internal Authenticator(WSTransport transport) | ||
| { | ||
| token.ThrowIfCancellationRequested(); | ||
| sender = new MtProtoPlainSender(transport); | ||
| completionSource = new TaskCompletionSource<Step3_Response>(); | ||
| } | ||
|
|
||
| var sender = new MtProtoPlainSender(transport); | ||
| internal Task<Step3_Response> DoAuthentication() | ||
| { | ||
| var step1 = new Step1_PQRequest(); | ||
|
|
||
| await sender.Send(step1.ToBytes(), token).ConfigureAwait(false); | ||
| var step1Response = step1.FromBytes(await sender.Receive(token) | ||
| .ConfigureAwait(false)); | ||
| sender.OnResponseReceived = (step1Message) => Sender_OnStep1ResponseReceived(step1, step1Message); | ||
| sender.Send(step1.ToBytes()); | ||
|
|
||
| return completionSource.Task; | ||
| } | ||
|
|
||
| private void Sender_OnStep1ResponseReceived(Step1_PQRequest step1, byte[] step1Message) | ||
| { | ||
| var step1Response = step1.FromBytes(step1Message); | ||
|
|
||
| var step2 = new Step2_DHExchange(); | ||
| await sender.Send(step2.ToBytes( | ||
| sender.OnResponseReceived = (step2message) => Sender_OnStep2ResponseReceived(step2, step2message); | ||
| sender.Send(step2.ToBytes( | ||
| step1Response.Nonce, | ||
| step1Response.ServerNonce, | ||
| step1Response.Fingerprints, | ||
| step1Response.Pq), token) | ||
| .ConfigureAwait(false); | ||
| step1Response.Pq)); | ||
| } | ||
|
|
||
| private void Sender_OnStep2ResponseReceived(Step2_DHExchange step2, byte[] step2message) | ||
| { | ||
| var step2Response = step2.FromBytes(step2message); | ||
|
|
||
| var step2Response = step2.FromBytes(await sender.Receive(token) | ||
| .ConfigureAwait(false)); | ||
|
|
||
| var step3 = new Step3_CompleteDHExchange(); | ||
| await sender.Send(step3.ToBytes( | ||
| sender.OnResponseReceived = (step3Message) => Sender_OnStep3ResponseReceived(step3, step3Message); | ||
| sender.Send(step3.ToBytes( | ||
| step2Response.Nonce, | ||
| step2Response.ServerNonce, | ||
| step2Response.NewNonce, | ||
| step2Response.EncryptedAnswer), token) | ||
| .ConfigureAwait(false); | ||
|
|
||
| var step3Response = step3.FromBytes(await sender.Receive(token) | ||
| .ConfigureAwait(false)); | ||
| step2Response.EncryptedAnswer)); | ||
| } | ||
|
|
||
| return step3Response; | ||
| private void Sender_OnStep3ResponseReceived(Step3_CompleteDHExchange step3, byte[] response) | ||
| { | ||
| completionSource.SetResult(step3.FromBytes(response)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace TgSharp.Core.MTProto.Crypto | ||
| { | ||
| /* | ||
| * AES-256-CTR Implementation | ||
| * Original implementation by OpenSSL, ported to C# | ||
| */ | ||
| public class AesCtr | ||
| { | ||
| // The input encrypted as though 128bit counter mode is being used. The extra | ||
| // state information to record how much of the 128bit block we have used is | ||
| // contained in number, and the encrypted counter is kept in encryptedCount. Both | ||
| // *num and ecount_buf must be initialised with zeros before the first call to | ||
| // CRYPTO_ctr128_encrypt(). | ||
| // | ||
| // This algorithm assumes that the counter is in the x lower bits of the IV | ||
| // (ivec), and that the application has full control over overflow and the rest | ||
| // of the IV. This implementation takes NO responsibility for checking that | ||
| // the counter doesn't overflow into the rest of the IV when incremented. | ||
|
|
||
| public static void Ctr128Encrypt(byte[] input, byte[] key, ref byte[] ivec, ref byte[] encryptedCount, ref int number, byte[] output) | ||
| { | ||
| int n; | ||
| n = number; | ||
|
|
||
| int outputPos = 0, inputPos = 0; | ||
| int len = input.Length; | ||
|
|
||
| while (n != 0 && len != 0) | ||
| { | ||
| output[outputPos++] = (byte)(input[inputPos++] ^ encryptedCount[n]); | ||
| --len; | ||
| n = (n + 1) % 16; | ||
| } | ||
|
|
||
| while (len >= 16) | ||
| { | ||
| encryptedCount = EncryptBlock(ivec, key); | ||
| Ctr128Inc(ivec); | ||
| for (n = 0; n < 16; n += sizeof(ulong)) | ||
| { | ||
| var xoredResult = BitConverter.GetBytes(BitConverter.ToUInt64(input, inputPos + n) ^ BitConverter.ToUInt64(encryptedCount, n)); | ||
| Buffer.BlockCopy(xoredResult, 0, output, outputPos + n, 8); | ||
| } | ||
| len -= 16; | ||
| outputPos += 16; | ||
| inputPos += 16; | ||
| n = 0; | ||
| } | ||
|
|
||
| if (len != 0) | ||
| { | ||
| encryptedCount = EncryptBlock(ivec, key); | ||
| Ctr128Inc(ivec); | ||
| while (len-- != 0) | ||
| { | ||
| output[outputPos + n] = (byte)(input[inputPos + n] ^ encryptedCount[n]); | ||
| ++n; | ||
| } | ||
| } | ||
| number = n; | ||
| } | ||
|
|
||
| // increment counter (128-bit int) by 1 | ||
| private static void Ctr128Inc(byte[] counter) | ||
| { | ||
| uint n = 16, c = 1; | ||
|
|
||
| do | ||
| { | ||
| --n; | ||
| c += counter[n]; | ||
| counter[n] = (byte)c; | ||
| c >>= 8; | ||
| } while (n != 0); | ||
| } | ||
|
|
||
| private static byte[] EncryptBlock(byte[] toEncrypt, byte[] key) | ||
| { | ||
| using (var aes = new RijndaelManaged()) | ||
| { | ||
| aes.Key = key; | ||
| aes.Mode = CipherMode.ECB; | ||
| aes.Padding = PaddingMode.None; | ||
| ICryptoTransform cTransform = aes.CreateEncryptor(); | ||
| return cTransform.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using TgSharp.Core.MTProto.Crypto; | ||
|
|
||
| namespace TgSharp.Core.Network | ||
| { | ||
| internal class Message | ||
| { | ||
| internal byte[] Body { get; private set; } | ||
|
|
||
| internal Message(byte[] body) | ||
| { | ||
| if (body == null) | ||
| throw new ArgumentNullException(nameof(body)); | ||
|
|
||
| Body = body; | ||
| } | ||
|
|
||
| internal static Message Decode(byte[] body) | ||
| { | ||
| using (var memoryStream = new MemoryStream(body)) | ||
| { | ||
| using (var binaryReader = new BinaryReader(memoryStream)) | ||
| { | ||
| int length = binaryReader.ReadInt32(); | ||
| return new Message(binaryReader.ReadBytes(length)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal byte[] Encode() | ||
| { | ||
| using (var memoryStream = new MemoryStream()) | ||
| { | ||
| using (var binaryWriter = new BinaryWriter(memoryStream)) | ||
| { | ||
| binaryWriter.Write(Body.Length); | ||
| binaryWriter.Write(Body); | ||
| return memoryStream.ToArray(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.