You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AeadClient.cs 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Shadowsocks.Protocol.Shadowsocks.Crypto;
  2. using System;
  3. using System.IO.Pipelines;
  4. using System.Net;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Collections.Generic;
  9. namespace Shadowsocks.Protocol.Shadowsocks
  10. {
  11. public class AeadClient : IStreamClient
  12. {
  13. private CryptoParameter cryptoParameter;
  14. private readonly byte[] mainKey;
  15. private static readonly byte[] _ssSubKeyInfo = Encoding.ASCII.GetBytes("ss-subkey");
  16. public AeadClient(CryptoParameter parameter, string password)
  17. {
  18. cryptoParameter = parameter;
  19. mainKey = CryptoUtils.SSKDF(password, parameter.KeySize);
  20. if (!parameter.IsAead)
  21. throw new NotSupportedException($"Unsupported method.");
  22. }
  23. public AeadClient(CryptoParameter parameter, byte[] key)
  24. {
  25. cryptoParameter = parameter;
  26. mainKey = key;
  27. }
  28. public Task Connect(EndPoint destination, IDuplexPipe client, IDuplexPipe server) =>
  29. // destination is ignored, this is just a converter
  30. Task.WhenAll(ConvertUplink(client, server), ConvertDownlink(client, server));
  31. public async Task ConvertUplink(IDuplexPipe client, IDuplexPipe server)
  32. {
  33. var up = cryptoParameter.GetCrypto();
  34. var pmp = new ProtocolMessagePipe(server);
  35. var salt = new SaltMessage(16, true);
  36. await pmp.WriteAsync(salt);
  37. var key = CryptoUtils.HKDF(cryptoParameter.KeySize, mainKey, salt.Salt.ToArray(), _ssSubKeyInfo);
  38. up.Init(key, null);
  39. Memory<byte> nonce = new byte[cryptoParameter.NonceSize];
  40. nonce.Span.Fill(0);
  41. // TODO write salt with data
  42. while (true)
  43. {
  44. var result = await client.Input.ReadAsync();
  45. if (result.IsCanceled || result.IsCompleted) return;
  46. // TODO compress into one chunk when possible
  47. foreach (var item in result.Buffer)
  48. {
  49. foreach (var i in SplitBigChunk(item))
  50. {
  51. await pmp.WriteAsync(new AeadBlockMessage(up, nonce, cryptoParameter)
  52. {
  53. // in send routine, Data is readonly
  54. Data = MemoryMarshal.AsMemory(i),
  55. });
  56. }
  57. }
  58. client.Input.AdvanceTo(result.Buffer.End);
  59. }
  60. }
  61. public async Task ConvertDownlink(IDuplexPipe client, IDuplexPipe server)
  62. {
  63. var down = cryptoParameter.GetCrypto();
  64. var pmp = new ProtocolMessagePipe(server);
  65. var salt = await pmp.ReadAsync(new SaltMessage(cryptoParameter.KeySize));
  66. var key = CryptoUtils.HKDF(cryptoParameter.KeySize, mainKey, salt.Salt.ToArray(), _ssSubKeyInfo);
  67. down.Init(key, null);
  68. Memory<byte> nonce = new byte[cryptoParameter.NonceSize];
  69. nonce.Span.Fill(0);
  70. while (true)
  71. {
  72. try
  73. {
  74. var block = await pmp.ReadAsync(new AeadBlockMessage(down, nonce, cryptoParameter));
  75. await client.Output.WriteAsync(block.Data);
  76. client.Output.Advance(block.Data.Length);
  77. }
  78. catch (FormatException)
  79. {
  80. return;
  81. }
  82. }
  83. }
  84. public List<ReadOnlyMemory<byte>> SplitBigChunk(ReadOnlyMemory<byte> mem)
  85. {
  86. var l = new List<ReadOnlyMemory<byte>>(mem.Length / 0x3fff + 1);
  87. while (mem.Length > 0x3fff)
  88. {
  89. l.Add(mem.Slice(0, 0x3fff));
  90. mem = mem.Slice(0x4000);
  91. }
  92. l.Add(mem);
  93. return l;
  94. }
  95. }
  96. }