Skip to content
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

Prepare for upcoming RPCN ticket changes #659

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,8 @@ public class AuthenticationEndpoints : EndpointGroup
context.Logger.LogWarning(BunkumCategory.Authentication, "Could not read ticket: " + e);
return null;
}

TokenPlatform? platform = ticket.IssuerId switch
{
0x100 => TokenPlatform.PS3,
0x33333333 => TokenPlatform.RPCS3,
_ => null,
};

TokenPlatform? platform = ticket.DeterminePlatform();

GameUser? user = database.GetUserByUsername(ticket.Username);
if (user == null)
Expand Down Expand Up @@ -104,6 +99,14 @@ public class AuthenticationEndpoints : EndpointGroup
context.Logger.LogWarning(BunkumCategory.Authentication, $"Rejecting {user}'s login because server is in maintenance mode");
return null;
}

if (platform == null)
{
database.AddLoginFailNotification("The server could not determine what platform you were trying to connect from.", user);
context.Logger.LogWarning(BunkumCategory.Authentication, $"Could not determine platform from ticket. " +
$"IssuerID: {ticket.IssuerId}, SignatureIdentifier: {ticket.SignatureIdentifier}");
return null;
}

bool ticketVerified = false;
if (config.UseTicketVerification)
Expand All @@ -116,7 +119,7 @@ public class AuthenticationEndpoints : EndpointGroup
return null;
}

ticketVerified = VerifyTicket(context, (MemoryStream)body, ticket, timeProvider);
ticketVerified = VerifyTicket(context, (MemoryStream)body, ticket, platform.Value, timeProvider);
if (!ticketVerified)
{
SendVerificationFailureNotification(database, user, config);
Expand All @@ -137,25 +140,11 @@ public class AuthenticationEndpoints : EndpointGroup
}
}

TokenGame? game = null;

// check if we're connecting from a beta build
bool parsedBeta = byte.TryParse(context.QueryString.Get("beta"), out byte isBeta);
if (parsedBeta && isBeta == 1) game = TokenGame.BetaBuild;

game ??= TokenGameUtility.FromTitleId(ticket.TitleId);

if (platform == null)
{
database.AddLoginFailNotification("The server could not determine what platform you were trying to connect from.", user);
context.Logger.LogWarning(BunkumCategory.Authentication, $"Could not determine platform from ticket.\n" +
$"Missing IssuerID: {ticket.IssuerId}");
return null;
}
TokenGame? game = ticket.DetermineGame(context);

if (game == null)
{
database.AddLoginFailNotification("The server could not determine what game you were trying to connect from.", user);
database.AddLoginFailNotification($"The server could not determine what game you were trying to connect from. Give this ID to a developer: {ticket.TitleId}", user);
context.Logger.LogWarning(BunkumCategory.Authentication, $"Could not determine game from ticket.\n" +
$"Missing TitleID: {ticket.TitleId}");
return null;
Expand Down Expand Up @@ -190,20 +179,26 @@ public class AuthenticationEndpoints : EndpointGroup
};
}

private static bool VerifyTicket(RequestContext context, MemoryStream body, Ticket ticket, IDateTimeProvider timeProvider)
private static bool VerifyTicket(RequestContext context, MemoryStream body, Ticket ticket, TokenPlatform platform, IDateTimeProvider timeProvider)
{
ITicketSigningKey signingKey;

// Determine the correct key to use
if (ticket.IssuerId == 0x33333333)
{
context.Logger.LogDebug(BunkumCategory.Authentication, "Using RPCN ticket key");
signingKey = RpcnSigningKey.Instance;
}
else
switch (platform)
{
context.Logger.LogDebug(BunkumCategory.Authentication, "Using PSN LBP ticket key");
signingKey = LbpSigningKey.Instance;
// Determine the correct key to use
case TokenPlatform.RPCS3:
context.Logger.LogDebug(BunkumCategory.Authentication, "Using RPCN ticket key");
signingKey = RpcnSigningKey.Instance;
break;
case TokenPlatform.PS3:
case TokenPlatform.Vita:
case TokenPlatform.PSP:
context.Logger.LogDebug(BunkumCategory.Authentication, "Using PSN LBP ticket key");
signingKey = LbpSigningKey.Instance;
break;
case TokenPlatform.Website:
default:
throw new ArgumentOutOfRangeException(nameof(platform));
}

// Dont allow use of expired tickets
Expand Down
31 changes: 31 additions & 0 deletions Refresh.GameServer/Extensions/TicketExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Bunkum.Core;
using NPTicket;
using Refresh.GameServer.Authentication;

namespace Refresh.GameServer.Extensions;

public static class TicketExtensions
{
public static TokenPlatform? DeterminePlatform(this Ticket ticket)
{
if (ticket.SignatureIdentifier == "RPCN" || ticket.IssuerId == 0x33333333)
return TokenPlatform.RPCS3;
jvyden marked this conversation as resolved.
Show resolved Hide resolved

if (ticket.IssuerId == 0x100)
return TokenPlatform.PS3;

return null;
}

public static TokenGame? DetermineGame(this Ticket ticket, RequestContext context)
{
TokenGame? game = null;

// check if we're connecting from a beta build
bool parsedBeta = byte.TryParse(context.QueryString.Get("beta"), out byte isBeta);
if (parsedBeta && isBeta == 1) game = TokenGame.BetaBuild;

game ??= TokenGameUtility.FromTitleId(ticket.TitleId);
return game;
}
}
Loading