Skip to content

Add full XI5Ticket support and improve ticket handling #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions BackendServices/CastleLibrary/XI5/Reader/TicketReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using BitConverterExtension;
using System;
using System.IO;
using System.Text;
using XI5.Types;

namespace XI5.Reader
{
public class TicketReader : BinaryReader
{
// setup endian converter
private static readonly BigEndianBitConverter bigEndian = new();

public TicketReader(Stream input) : base(input) { }

#region Big Endian Conversion

public override short ReadInt16()
{
byte[] bytes = ReadBytes(2);
return bigEndian.ToInt16(bytes, 0);
}

public override int ReadInt32()
{
byte[] bytes = ReadBytes(4);
return bigEndian.ToInt32(bytes, 0);
}

public override long ReadInt64()
{
byte[] bytes = ReadBytes(8);
return bigEndian.ToInt64(bytes, 0);
}

public override ushort ReadUInt16()
{
byte[] bytes = ReadBytes(2);
return bigEndian.ToUInt16(bytes, 0);
}

public override uint ReadUInt32()
{
byte[] bytes = ReadBytes(4);
return bigEndian.ToUInt32(bytes, 0);
}

public override ulong ReadUInt64()
{
byte[] bytes = ReadBytes(8);
return bigEndian.ToUInt64(bytes, 0);
}

#endregion

internal TicketVersion ReadTicketVersion() => new TicketVersion((byte)(ReadByte() >> 4), ReadByte());

internal ushort ReadTicketHeader()
{
ReadBytes(4); // header
return ReadUInt16(); // ticket length
}

internal TicketDataSection ReadTicketSectionHeader()
{
long position = this.BaseStream.Position;

byte sectionHeader = this.ReadByte();
if (sectionHeader != 0x30)
throw new FormatException($"[XI5Ticket] - Expected 0x30 for section header, was {sectionHeader}. Offset is {this.BaseStream.Position}");

TicketDataSectionType type = (TicketDataSectionType)this.ReadByte();
ushort length = this.ReadUInt16();

return new TicketDataSection(type, length, position);
}

private TicketData ReadTicketData(TicketDataType expectedType)
{
TicketData data = new TicketData((TicketDataType)ReadUInt16(), ReadUInt16());
if (data.Type != expectedType && expectedType != TicketDataType.Empty)
throw new FormatException($"[XI5Ticket] - Expected data type to be {expectedType}, was really {data.Type} ({(int)data.Type})");

return data;
}

internal byte[] ReadTicketBinaryData(TicketDataType type = TicketDataType.Binary)
=> ReadBytes(ReadTicketData(type).Length);
internal string ReadTicketStringData(TicketDataType type = TicketDataType.String)
=> Encoding.Default.GetString(ReadTicketBinaryData(type)).TrimEnd('\0');

internal uint ReadTicketUInt32Data()
{
ReadTicketData(TicketDataType.UInt32);
return ReadUInt32();
}

internal ulong ReadTicketUInt64Data()
{
ReadTicketData(TicketDataType.UInt64);
return ReadUInt64();
}

internal DateTimeOffset ReadTicketTimestampData()
{
ReadTicketData(TicketDataType.Timestamp);
return DateTimeOffset.FromUnixTimeMilliseconds((long)ReadUInt64());
}

internal void SkipTicketEmptyData(int sections = 1)
{
for (int i = 0; i < sections; i++) ReadTicketData(TicketDataType.Empty);
}
}
}
51 changes: 0 additions & 51 deletions BackendServices/CastleLibrary/XI5/StreamExtensions.cs

This file was deleted.

31 changes: 31 additions & 0 deletions BackendServices/CastleLibrary/XI5/Types/Parsers/TicketParser21.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using XI5.Reader;

namespace XI5.Types.Parsers
{
internal static class TicketParser21
{
internal static void ParseTicket(XI5Ticket ticket, TicketReader reader)
{
ticket.SerialId = reader.ReadTicketStringData(TicketDataType.Binary);

ticket.IssuerId = reader.ReadTicketUInt32Data();

ticket.IssuedDate = reader.ReadTicketTimestampData();
ticket.ExpiryDate = reader.ReadTicketTimestampData();

ticket.UserId = reader.ReadTicketUInt64Data();
ticket.Username = reader.ReadTicketStringData();

ticket.Country = reader.ReadTicketStringData(TicketDataType.Binary); // No I am not going to brazil
ticket.Domain = reader.ReadTicketStringData();

ticket.ServiceId = reader.ReadTicketStringData(TicketDataType.Binary);
ticket.TitleId = XI5Ticket.ServiceIdRegex.Matches(ticket.ServiceId)[0].ToString();

ticket.Status = reader.ReadUInt32();

// Skip padding section in ticket
reader.SkipTicketEmptyData(3);
}
}
}
47 changes: 47 additions & 0 deletions BackendServices/CastleLibrary/XI5/Types/Parsers/TicketParser30.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using XI5.Reader;

namespace XI5.Types.Parsers
{
internal static class TicketParser30
{
internal static void ParseTicket(XI5Ticket ticket, TicketReader reader)
{
ticket.SerialId = reader.ReadTicketStringData(TicketDataType.Binary);

ticket.IssuerId = reader.ReadTicketUInt32Data();

ticket.IssuedDate = reader.ReadTicketTimestampData();
ticket.ExpiryDate = reader.ReadTicketTimestampData();

ticket.UserId = reader.ReadTicketUInt64Data();
ticket.Username = reader.ReadTicketStringData();

ticket.Country = reader.ReadTicketStringData(TicketDataType.Binary);
ticket.Domain = reader.ReadTicketStringData();

ticket.ServiceId = reader.ReadTicketStringData(TicketDataType.Binary);
ticket.TitleId = XI5Ticket.ServiceIdRegex.Matches(ticket.ServiceId)[0].ToString();

TicketDataSection header = reader.ReadTicketSectionHeader();
if (header.Type != TicketDataSectionType.DateOfBirth)
{
throw new FormatException($"[XI5Ticket] - Expected section to be {nameof(TicketDataSectionType.DateOfBirth)}, " +
$"was really {header.Type} ({(int)header.Type})");
}

reader.ReadUInt32(); // Birthdate
reader.SkipTicketEmptyData(2); // Padding?

Console.WriteLine(reader.BaseStream.Position);
header = reader.ReadTicketSectionHeader();
if (header.Type != TicketDataSectionType.Age)
{
throw new FormatException($"[XI5Ticket] - Expected section to be {nameof(TicketDataSectionType.Age)}, " +
$"was really {header.Type} ({(int)header.Type})");
}

reader.SkipTicketEmptyData();
}
}
}
14 changes: 14 additions & 0 deletions BackendServices/CastleLibrary/XI5/Types/TicketData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace XI5.Types
{
public readonly struct TicketData
{
public readonly TicketDataType Type;
public readonly ushort Length;

public TicketData(TicketDataType type, ushort length)
{
Type = type;
Length = length;
}
}
}
16 changes: 16 additions & 0 deletions BackendServices/CastleLibrary/XI5/Types/TicketDataSection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace XI5.Types
{
public readonly struct TicketDataSection
{
public TicketDataSectionType Type { get; }
public ushort Length { get; }
public int Position { get; }

public TicketDataSection(TicketDataSectionType type, ushort length, long position)
{
Type = type;
Length = length;
Position = (int)position;
}
}
}
10 changes: 10 additions & 0 deletions BackendServices/CastleLibrary/XI5/Types/TicketDataSectionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace XI5.Types
{
public enum TicketDataSectionType : byte
{
Body = 0,
Footer = 2,
Age = 16,
DateOfBirth = 17,
}
}
14 changes: 14 additions & 0 deletions BackendServices/CastleLibrary/XI5/Types/TicketDataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace XI5.Types
{
public enum TicketDataType : ushort
{
Empty = 0,
UInt32 = 1,
UInt64 = 2,

String = 4,

Timestamp = 7,
Binary = 8,
}
}
19 changes: 19 additions & 0 deletions BackendServices/CastleLibrary/XI5/Types/TicketVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace XI5.Types
{
public readonly struct TicketVersion
{
public byte Major { get; }
public byte Minor { get; }

internal TicketVersion(byte major, byte minor)
{
Major = major;
Minor = minor;
}

public override string ToString()
{
return Major + "." + Minor;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace XI5.Verification
{
/// <summary>
/// Defines the parameters for a key used for verification.
/// </summary>
public interface ITicketSigningKey
{
string HashAlgorithm { get; }
string CurveTable { get; }
TicketSignatureMessageType MessageType { get; }
string PublicKeyX { get; }
string PublicKeyY { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace XI5.Verification.Keys.Games
{
public class DefaultSigningKey : PsnSigningKey
{
public override string PublicKeyX => "39c62d061d4ee35c5f3f7531de0af3cf918346526edac727";
public override string PublicKeyY => "a5d578b55113e612bf1878d4cc939d61a41318403b5bdf86";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace XI5.Verification.Keys
{
/// <summary>
/// A public signing key used when PS3/PSVita clients connect via official PSN.
/// Since each game series has a different set of curves, you must provide them yourself in a new class.
/// </summary>
public abstract class PsnSigningKey : ITicketSigningKey
{
public string HashAlgorithm => "SHA-1";
public string CurveTable => "secp192r1";
public TicketSignatureMessageType MessageType => TicketSignatureMessageType.Ticket;
public abstract string PublicKeyX { get; }
public abstract string PublicKeyY { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace XI5.Verification.Keys
{
/// <summary>
/// The public signing key used when RPCS3 clients connect via RPCN.
/// </summary>
public class RpcnSigningKey : ITicketSigningKey
{
public static readonly RpcnSigningKey Instance = new RpcnSigningKey();
private RpcnSigningKey() { }

public string HashAlgorithm => "SHA-224";
public string CurveTable => "secp224k1";
public TicketSignatureMessageType MessageType => TicketSignatureMessageType.Body;
public string PublicKeyX => "b07bc0f0addb97657e9f389039e8d2b9c97dc2a31d3042e7d0479b93";
public string PublicKeyY => "d81c42b0abdf6c42191a31e31f93342f8f033bd529c2c57fdb5a0a7d";
}
}
Loading