private void parseTicket(Stream ticketFile) { fireDebug("Parsing Ticket..."); ticketFile.Seek(0, SeekOrigin.Begin); byte[] temp = new byte[8]; fireDebug(" Reading Signature Exponent... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(temp, 0, 4); signatureExponent = Shared.Swap(BitConverter.ToUInt32(temp, 0)); fireDebug(" Reading Signature... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(signature, 0, signature.Length); fireDebug(" Reading Padding... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(padding, 0, padding.Length); fireDebug(" Reading Issuer... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(issuer, 0, issuer.Length); fireDebug(" Reading Unknown... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(unknown, 0, unknown.Length); fireDebug(" Reading Title Key... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(encryptedTitleKey, 0, encryptedTitleKey.Length); fireDebug(" Reading Unknown2... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); unknown2 = (byte)ticketFile.ReadByte(); fireDebug(" Reading Ticket ID.. (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(temp, 0, 8); ticketId = Shared.Swap(BitConverter.ToUInt64(temp, 0)); fireDebug(" Reading Console ID... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(temp, 0, 4); consoleId = Shared.Swap(BitConverter.ToUInt32(temp, 0)); fireDebug(" Reading Title ID... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(temp, 0, 8); titleId = Shared.Swap(BitConverter.ToUInt64(temp, 0)); fireDebug(" Reading Unknown3... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); fireDebug(" Reading NumOfDLC... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(temp, 0, 4); unknown3 = Shared.Swap(BitConverter.ToUInt16(temp, 0)); numOfDlc = Shared.Swap(BitConverter.ToUInt16(temp, 2)); fireDebug(" Reading Unknown4... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(temp, 0, 8); unknown4 = Shared.Swap(BitConverter.ToUInt64(temp, 0)); fireDebug(" Reading Padding2... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); padding2 = (byte)ticketFile.ReadByte(); fireDebug(" Reading Common Key Index... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); commonKeyIndex = (byte)ticketFile.ReadByte(); newKeyIndex = commonKeyIndex; fireDebug(" Reading Unknown5... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(unknown5, 0, unknown5.Length); fireDebug(" Reading Unknown6... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(unknown6, 0, unknown6.Length); fireDebug(" Reading Padding3... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(temp, 0, 2); padding3 = Shared.Swap(BitConverter.ToUInt16(temp, 0)); fireDebug(" Reading Enable Time Limit... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); fireDebug(" Reading Time Limit... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(temp, 0, 8); enableTimeLimit = Shared.Swap(BitConverter.ToUInt32(temp, 0)); timeLimit = Shared.Swap(BitConverter.ToUInt32(temp, 4)); fireDebug(" Reading Padding4... (Offset: 0x{0})", ticketFile.Position.ToString("x8").ToUpper()); ticketFile.Read(padding4, 0, padding4.Length); fireDebug(" Decrypting Title Key..."); decryptTitleKey(); fireDebug(" -> Encrypted Title Key: {0}", Shared.ByteArrayToString(encryptedTitleKey)); fireDebug(" -> Decrypted Title Key: {0}", Shared.ByteArrayToString(decryptedTitleKey)); fireDebug("Parsing Ticket Finished..."); }
private void writeToStream(Stream writeStream) { fireDebug("Writing Ticket..."); fireDebug(" Encrypting Title Key..."); encryptTitleKey(); fireDebug(" -> Decrypted Title Key: {0}", Shared.ByteArrayToString(decryptedTitleKey)); fireDebug(" -> Encrypted Title Key: {0}", Shared.ByteArrayToString(encryptedTitleKey)); if (fakeSign) { fireDebug(" Clearing Signature..."); signature = new byte[256]; } //Clear Signature if we fake Sign MemoryStream ms = new MemoryStream(); ms.Seek(0, SeekOrigin.Begin); fireDebug(" Writing Signature Exponent... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(BitConverter.GetBytes(Shared.Swap(signatureExponent)), 0, 4); fireDebug(" Writing Signature... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(signature, 0, signature.Length); fireDebug(" Writing Padding... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(padding, 0, padding.Length); fireDebug(" Writing Issuer... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(issuer, 0, issuer.Length); fireDebug(" Writing Unknown... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(unknown, 0, unknown.Length); fireDebug(" Writing Title Key... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(encryptedTitleKey, 0, encryptedTitleKey.Length); fireDebug(" Writing Unknown2... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.WriteByte(unknown2); fireDebug(" Writing Ticket ID... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(BitConverter.GetBytes(Shared.Swap(ticketId)), 0, 8); fireDebug(" Writing Console ID... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(BitConverter.GetBytes(Shared.Swap(consoleId)), 0, 4); fireDebug(" Writing Title ID... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(BitConverter.GetBytes(Shared.Swap(titleId)), 0, 8); fireDebug(" Writing Unknwon3... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(BitConverter.GetBytes(Shared.Swap(unknown3)), 0, 2); fireDebug(" Writing NumOfDLC... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(BitConverter.GetBytes(Shared.Swap(numOfDlc)), 0, 2); fireDebug(" Writing Unknwon4... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(BitConverter.GetBytes(Shared.Swap(unknown4)), 0, 8); fireDebug(" Writing Padding2... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.WriteByte(padding2); fireDebug(" Writing Common Key Index... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.WriteByte(commonKeyIndex); fireDebug(" Writing Unknown5... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(unknown5, 0, unknown5.Length); fireDebug(" Writing Unknown6... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(unknown6, 0, unknown6.Length); fireDebug(" Writing Padding3... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(BitConverter.GetBytes(Shared.Swap(padding3)), 0, 2); fireDebug(" Writing Enable Time Limit... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(BitConverter.GetBytes(Shared.Swap(enableTimeLimit)), 0, 4); fireDebug(" Writing Time Limit... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(BitConverter.GetBytes(Shared.Swap(timeLimit)), 0, 4); fireDebug(" Writing Padding4... (Offset: 0x{0})", ms.Position.ToString("x8").ToUpper()); ms.Write(padding4, 0, padding4.Length); byte[] tik = ms.ToArray(); ms.Dispose(); //fake Sign if (fakeSign) { fireDebug(" Fakesigning Ticket..."); byte[] hash = new byte[20]; SHA1 s = SHA1.Create(); for (ushort i = 0; i < 0xFFFF; i++) { byte[] bytes = BitConverter.GetBytes(i); tik[498] = bytes[1]; tik[499] = bytes[0]; hash = s.ComputeHash(tik); if (hash[0] == 0x00) { fireDebug(" -> Signed ({0})", i); break; } //Win! It's signed... if (i == 0xFFFF - 1) { fireDebug(" -> Signing Failed..."); throw new Exception("Fakesigning failed..."); } } s.Clear(); } writeStream.Seek(0, SeekOrigin.Begin); writeStream.Write(tik, 0, tik.Length); fireDebug("Writing Ticket Finished..."); }