/// <summary> /// Login to Minecraft.net and store credentials /// </summary> public void Login() { if (VerifyNames) { var loginHandler = new Minecraft_Net_Interaction(); string[] credentials = loginHandler.Login(ClientName, ClientPassword); if (credentials[0] == "") { // -- Fall back to no auth. RaiseError(this, "Failed to login to Minecraft.net! (Incorrect username or password)"); VerifyNames = false; } else { RaiseInfo(this, "Logged in to Minecraft.net successfully."); RaiseDebug(this, string.Format("Token: {0}\nProfile: {1}", credentials[0], credentials[1])); AccessToken = credentials[0]; SelectedProfile = credentials[1]; ClientToken = credentials[2]; ClientName = credentials[3]; } } else { AccessToken = "None"; SelectedProfile = "None"; } }
/// <summary> /// Uses a client's stored credentials to verify with Minecraft.net /// </summary> public bool VerifySession() { if (AccessToken == null || ClientToken == null) { RaiseError(this, "Credentials are not set!"); return false; } var SessionVerifier = new Minecraft_Net_Interaction(); string[] Response = SessionVerifier.SessionRefresh(AccessToken, ClientToken); if (Response[0] == "") { RaiseError(this, "Unable to Verify Session!"); return false; } RaiseInfo(this, "Credentials verified and refreshed!"); AccessToken = Response[0]; ClientToken = Response[1]; SelectedProfile = "Potato"; return true; }
/// <summary> /// Uses a client's stored credentials to verify with Minecraft.net /// </summary> /// <param name="accessToken">Stored Access Token</param> /// <param name="clientToken">Stored Client Token</param> public bool VerifySession(string accessToken, string clientToken) { AccessToken = accessToken; ClientToken = clientToken; var SessionVerifier = new Minecraft_Net_Interaction(); string[] Response = SessionVerifier.SessionRefresh(AccessToken, ClientToken); if (Response[0] == "") { RaiseError(this, "Unable to Verify Session!"); return false; } RaiseInfo(this, "Credentials verified and refreshed!"); AccessToken = Response[0]; ClientToken = Response[1]; return true; }
public void HandleEncryptionRequest(MinecraftClient client, IPacket packet) { var ER = (CBEncryptionRequest)packet; var SharedKey = new byte[16]; var Random = RandomNumberGenerator.Create(); // -- Generate a random shared key. Random.GetBytes(SharedKey); if (ER.ServerID == "" && client.VerifyNames) { // -- Verify with Minecraft.net. // -- At this point, the server requires a hash containing the server id, // -- shared key, and original public key. So we make this, and then pass to Minecraft.net List<byte> HashList = new List<byte>(); HashList.AddRange(Encoding.ASCII.GetBytes(ER.ServerID)); HashList.AddRange(SharedKey); HashList.AddRange(ER.PublicKey); var HashData = HashList.ToArray(); var Hash = JavaHexDigest(HashData); var Verify = new Minecraft_Net_Interaction(); if (!Verify.VerifyName(client.ClientName, client.AccessToken, client.SelectedProfile, Hash)) { client.RaiseLoginFailure(this, "Failed to verify name with Minecraft session server."); client.Disconnect(); return; } } else client.RaiseInfo(this, "Name verification disabled, skipping authentication."); // -- AsnKeyParser is a part of the cryptography.dll, which is simply a compiled version // -- of SMProxy's Cryptography.cs, with the server side parts stripped out. // -- You pass it the key data and ask it to parse, and it will // -- Extract the server's public key, then parse that into RSA for us. var KeyParser = new AsnKeyParser(ER.PublicKey); var Dekey = KeyParser.ParseRSAPublicKey(); // -- Now we create an encrypter, and encrypt the token sent to us by the server // -- as well as our newly made shared key (Which can then only be decrypted with the server's private key) // -- and we send it to the server. var cryptoService = new RSACryptoServiceProvider(); // -- RSA Encryption class cryptoService.ImportParameters(Dekey); // -- Import the Server's public key to use as the RSA encryption key. byte[] EncryptedSecret = cryptoService.Encrypt(SharedKey, false); // -- Encrypt the Secret key and verification token. byte[] EncryptedVerify = cryptoService.Encrypt(ER.VerifyToken, false); client.nh.wSock.InitEncryption(SharedKey); // -- Give the shared secret key to the socket var Response = new SBEncryptionResponse(); // -- Respond to the server Response.SharedLength = (short)EncryptedSecret.Length; Response.SharedSecret = EncryptedSecret; Response.VerifyLength = (short)EncryptedVerify.Length; Response.VerifyToken = EncryptedVerify; Response.Write(client.nh.wSock); client.nh.wSock.EncEnabled = true; client.nh.RaiseSocketInfo(this, "Encryption Enabled."); }