Kaynağa Gözat

增加srp6类

tanghai 13 yıl önce
ebeveyn
işleme
e77ac4624e

+ 1 - 1
CSharp/App/Modules/Robot/Protos/Messages.cs

@@ -142,6 +142,6 @@ namespace Robot.Protos
 		public byte[] A { get; set; }
 
 		[DataMember(Order = 2, IsRequired = true)]
-		public byte[] M1 { get; set; }
+		public byte[] M { get; set; }
 	}
 }

+ 22 - 18
CSharp/App/Modules/Robot/RealmSession.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Net;
 using System.Net.Sockets;
+using System.Security.Cryptography;
 using System.Threading.Tasks;
 using Helper;
 using Log;
@@ -133,13 +134,15 @@ namespace Robot
 					packetBytes, totalReadSize, packetBytes.Length);
 				if (readSize == 0)
 				{
-					return new Tuple<ushort, byte[]>(0, new byte[] { });
+					throw new RealmException("connection closed!");
 				}
 				totalReadSize += readSize;
 			}
 
 			int packetSize = BitConverter.ToInt32(packetBytes, 0);
 
+			Logger.Debug("packet size: {0}", packetSize);
+
 			// 读opcode和message
 			totalReadSize = 0;
 			needReadSize = packetSize;
@@ -150,11 +153,15 @@ namespace Robot
 					contentBytes, totalReadSize, contentBytes.Length);
 				if (readSize == 0)
 				{
-					return new Tuple<ushort, byte[]>(0, new byte[] { });
+					throw new RealmException("connection closed!");
 				}
 				totalReadSize += readSize;
 			}
+
 			ushort opcode = BitConverter.ToUInt16(contentBytes, 0);
+
+			Logger.Debug("opcode: {0}", opcode);
+
 			var messageBytes = new byte[needReadSize - sizeof (ushort)];
 			Array.Copy(contentBytes, sizeof (ushort), messageBytes, 0, messageBytes.Length);
 
@@ -197,26 +204,23 @@ namespace Robot
 				s.ToByteArray(), account.ToByteArray(), password.ToByteArray());
 			BigInteger clientS = srp6Client.CalculateSecret(b);
 
-			// 计算M1
-			var sha1Digest = new Sha1Digest();
-			var kBytes = new byte[sha1Digest.GetDigestSize()];
-			var clientSBytes = clientS.ToByteArray();
-			sha1Digest.BlockUpdate(clientSBytes, 0, clientSBytes.Length);
-			sha1Digest.DoFinal(kBytes, 0);
-
-			sha1Digest.Reset();
-			var m1 = new byte[sha1Digest.GetDigestSize()];
-			var aBytes = a.ToByteArray();
-			var bBytes = b.ToByteArray();
-			sha1Digest.BlockUpdate(aBytes, 0, aBytes.Length);
-			sha1Digest.BlockUpdate(bBytes, 0, bBytes.Length);
-			sha1Digest.BlockUpdate(kBytes, 0, kBytes.Length);
-			sha1Digest.DoFinal(m1, 0);
+			Logger.Debug("N: {0}\nG: {1}, s: {2}, B: {3}, A: {4}, S: {5}",
+				smsgAuthLogonChallengeResponse.N.ToHex(), smsgAuthLogonChallengeResponse.G.ToHex(),
+				smsgAuthLogonChallengeResponse.S.ToHex(), smsgAuthLogonChallengeResponse.B.ToHex(),
+				a.ToByteArray().ToHex(), clientS.ToByteArray().ToHex());
+
+			var sha1Managed = new SHA1Managed();
+			byte[] k = SRP6Helper.SRP6ClientCalcK(sha1Managed, clientS.ToByteArray());
+			Logger.Debug("K: {0}", k.ToHex());
+			byte[] m = SRP6Helper.SRP6ClientM1(
+				sha1Managed, account.ToByteArray(), n.ToByteArray(), g.ToByteArray(), 
+				s.ToByteArray(), a.ToByteArray(), b.ToByteArray(), k);
+			Logger.Debug("M: {0}, size: {1}", m.ToHex(), m.Length);
 
 			var cmsgAuthLogonProof = new CMSG_Auth_Logon_Proof
 			{
 				A = a.ToByteArray(),
-				M1 = clientS.ToByteArray()
+				M = m
 			};
 			this.SendMessage(MessageOpcode.CMSG_AUTH_LOGON_PROOF, cmsgAuthLogonProof);
 		}

+ 12 - 0
CSharp/App/Modules/Robot/SRP6Client.cs

@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Robot
+{
+	class SRP6Client
+	{
+	}
+}

+ 0 - 44
CSharp/Platform/Helper/CryptoHelper.cs

@@ -1,44 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using Org.BouncyCastle.Crypto.Agreement.Srp;
-using Org.BouncyCastle.Crypto.Digests;
-using Org.BouncyCastle.Math;
-using Org.BouncyCastle.Security;
-
-namespace Helper
-{
-	public class CryptoHelper
-	{
-		public static byte[] SRP6CalculateM1(
-			BigInteger n, BigInteger g, BigInteger s, BigInteger b, string account, string password)
-		{
-			var random = new SecureRandom();
-
-			var srp6Client = new Srp6Client();
-			srp6Client.Init(n, g, new Sha1Digest(), random);
-			BigInteger a = srp6Client.GenerateClientCredentials(
-				s.ToByteArray(), account.ToByteArray(), password.ToByteArray());
-			BigInteger clientS = srp6Client.CalculateSecret(b);
-
-			// 计算M1
-			var sha1Digest = new Sha1Digest();
-			var kBytes = new byte[sha1Digest.GetDigestSize()];
-			var clientSBytes = clientS.ToByteArray();
-			sha1Digest.BlockUpdate(clientSBytes, 0, clientSBytes.Length);
-			sha1Digest.DoFinal(kBytes, 0);
-
-			sha1Digest.Reset();
-			var m1 = new byte[sha1Digest.GetDigestSize()];
-			var aBytes = a.ToByteArray();
-			var bBytes = b.ToByteArray();
-			sha1Digest.BlockUpdate(aBytes, 0, aBytes.Length);
-			sha1Digest.BlockUpdate(bBytes, 0, bBytes.Length);
-			sha1Digest.BlockUpdate(kBytes, 0, kBytes.Length);
-			sha1Digest.DoFinal(m1, 0);
-			return m1;
-		}
-	}
-}

+ 1 - 1
CSharp/Platform/Helper/Helper.csproj

@@ -47,7 +47,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="ByteHelper.cs" />
-    <Compile Include="CryptoHelper.cs" />
+    <Compile Include="SRP6Helper.cs" />
     <Compile Include="JsonHelper.cs" />
     <Compile Include="LoaderHelper.cs" />
     <Compile Include="ProtobufHelper.cs" />

+ 68 - 0
CSharp/Platform/Helper/SRP6Helper.cs

@@ -0,0 +1,68 @@
+using System.Linq;
+using System.Security.Cryptography;
+
+namespace Helper
+{
+	public static class SRP6Helper
+	{
+		public static byte[] SRP6ClientCalcK(HashAlgorithm hashAlgorithm, byte[] clientS)
+		{
+			int clientSLength = clientS.Length;
+			int halfLength = clientSLength / 2;
+			var kBytes = new byte[40];
+			var halfS = new byte[clientSLength];
+
+			for (int i = 0; i < halfLength; ++i)
+			{
+				halfS[i] = clientS[i * 2];
+			}
+
+			var p1 = hashAlgorithm.ComputeHash(halfS);
+			for (int i = 0; i < 20; ++i)
+			{
+				kBytes[i * 2] = p1[i];
+			}
+
+			for (int i = 0; i < halfLength; ++i)
+			{
+				halfS[i] = clientS[i * 2 + 1];
+			}
+
+			var p2 = hashAlgorithm.ComputeHash(halfS);
+			for (int i = 0; i < 20; ++i)
+			{
+				kBytes[i * 2 + 1] = p2[i];
+			}
+
+			return kBytes;
+		}
+
+		public static byte[] SRP6ClientM1(
+			HashAlgorithm hashAlgorithm, byte[] identitySalt,
+			byte[] n, byte[] g, byte[] s, byte[] a,
+			byte[] b, byte[] k)
+		{
+			var hashN = hashAlgorithm.ComputeHash(n);
+			var hashG = hashAlgorithm.ComputeHash(g);
+			for (var i = 0; i < hashN.Length; ++i)
+			{
+				hashN[i] ^= hashG[i];
+			}
+
+			var hashGXorhashN = hashN; // H(N) ^ H(g)
+			var hashedIdentitySalt = hashAlgorithm.ComputeHash(identitySalt); // H(I)
+
+			// H(H(N) ^ H(g), H(I), s, A, B, K_c)
+			var m = hashAlgorithm.ComputeHash(new byte[0] 
+				.Concat(hashGXorhashN)
+				.Concat(hashedIdentitySalt)
+				.Concat(s)
+				.Concat(a)
+				.Concat(b)
+				.Concat(k)
+				.ToArray());
+
+			return m;
+		}
+	}
+}