-
Notifications
You must be signed in to change notification settings - Fork 12
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
score3229
wants to merge
14
commits into
GitHubProUser67:main
Choose a base branch
from
score3229:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bddcd96
Add files via upload
score3229 9e48d41
Add files via upload
score3229 13230ce
Add files via upload
score3229 ef6bd42
Delete BackendServices/CastleLibrary/XI5/obj directory
score3229 0db9adb
Ticket reader fix
score3229 87d03fb
Removed unnecessary usings
score3229 c0bb0b1
Removed unnecessary usings
score3229 d6e3604
Delete BackendServices/CastleLibrary/XI5/Verification/Keys/Games dire…
score3229 62d23eb
Removed unnecessary usings
score3229 f9bf324
Removed unnecessary usings
score3229 06ad1d8
Removed unnecessary usings
score3229 09c3b68
Delete BackendServices/CastleLibrary/XI5/StreamExtensions.cs
score3229 a2fd8d7
Update TicketParser30.cs
score3229 117f240
Updated XI5Ticket in login function
score3229 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
BackendServices/CastleLibrary/XI5/Reader/TicketReader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
BackendServices/CastleLibrary/XI5/Types/Parsers/TicketParser21.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
BackendServices/CastleLibrary/XI5/Types/Parsers/TicketParser30.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
BackendServices/CastleLibrary/XI5/Types/TicketDataSection.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
BackendServices/CastleLibrary/XI5/Types/TicketDataSectionType.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
BackendServices/CastleLibrary/XI5/Verification/ITicketSigningKey.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
BackendServices/CastleLibrary/XI5/Verification/Keys/Games/DefaultSigningKey.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
BackendServices/CastleLibrary/XI5/Verification/Keys/PsnSigningKey.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
BackendServices/CastleLibrary/XI5/Verification/Keys/RpcnSigningKey.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.