private static byte[] CreateKMSV5ResponseBytes(KMSV5Response responsev5) { using (MemoryStream stream = new MemoryStream()) { using (BinaryWriter binaryWriter = new BinaryWriter(stream)) { binaryWriter.Write(responsev5.BodyLength); binaryWriter.Write(responsev5.Unknown); binaryWriter.Write(responsev5.BodyLength2); binaryWriter.Write(responsev5.Version); binaryWriter.Write(responsev5.Salt); binaryWriter.Write(responsev5.Encrypted); binaryWriter.Write(responsev5.Padding); binaryWriter.Flush(); stream.Position = 0; return stream.ToArray(); } } }
public byte[] HandleRequest(byte[] kmsRequestData) { KMSV5Request kmsv5Request = CreateKMSV5Request(kmsRequestData); byte[] decrypted = DecryptV5(kmsv5Request); byte[] decryptedSalt = decrypted.Take(16).ToArray(); byte[] decryptedRequest = decrypted.Skip(16).ToArray(); byte[] responseBytes = Server.ExecuteKMSServerLogic(decryptedRequest, Logger); byte[] randomSalt = Guid.NewGuid().ToByteArray(); byte[] randomSaltHash = GetSHA265Hash(randomSalt); byte[] randomStuff = new byte[16]; for (int i = 0; i < 16; i++) { randomStuff[i] = (byte)(decryptedSalt[i] ^ kmsv5Request.Salt[i] ^ randomSalt[i]); } byte[] responsedata = responseBytes.Concat(randomStuff).Concat(randomSaltHash).ToArray(); byte[] encryptedResponseData = EncryptV5(responsedata, kmsv5Request.Salt); KMSV5Response kmsResponse = new KMSV5Response { Version = kmsv5Request.Version, Salt = kmsv5Request.Salt, Encrypted = encryptedResponseData }; byte[] encryptedResponse = CreateKMSV5ResponseBytes(kmsResponse); return encryptedResponse; }