Add external key methods to FS client/server

This commit is contained in:
Alex Barney 2019-10-02 17:57:10 -05:00
parent d291500b28
commit fdd7eebb4b
6 changed files with 82 additions and 14 deletions

View file

@ -0,0 +1,38 @@
using LibHac.FsService;
using LibHac.Ncm;
using LibHac.Spl;
namespace LibHac.Fs
{
public static class ExternalKeys
{
public static Result GetRightsId(this FileSystemClient fs, out RightsId rightsId, TitleId programId,
StorageId storageId)
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
return fsProxy.GetRightsId(out rightsId, programId, storageId);
}
public static Result RegisterExternalKey(this FileSystemClient fs, ref RightsId rightsId, ref AccessKey key)
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
return fsProxy.RegisterExternalKey(ref rightsId, ref key);
}
public static Result UnregisterExternalKey(this FileSystemClient fs, ref RightsId rightsId)
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
return fsProxy.UnregisterExternalKey(ref rightsId);
}
public static Result UnregisterAllExternalKey(this FileSystemClient fs)
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
return fsProxy.UnregisterAllExternalKey();
}
}
}

View file

@ -451,18 +451,25 @@ namespace LibHac.FsService
public Result RegisterExternalKey(ref RightsId rightsId, ref AccessKey externalKey)
{
throw new NotImplementedException();
// Missing permission check
return FsProxyCore.RegisterExternalKey(ref rightsId, ref externalKey);
}
public Result UnregisterExternalKey(ref RightsId rightsId)
{
throw new NotImplementedException();
// Missing permission check
return FsProxyCore.UnregisterExternalKey(ref rightsId);
}
public Result UnregisterAllExternalKey()
{
throw new NotImplementedException();
// Missing permission check
return FsProxyCore.UnregisterAllExternalKey();
}
public Result SetSdCardEncryptionSeed(ReadOnlySpan<byte> seed)
{
// todo: use struct instead of byte span

View file

@ -3,12 +3,14 @@ using LibHac.Fs;
using LibHac.FsSystem;
using LibHac.FsSystem.Save;
using LibHac.FsService.Creators;
using LibHac.Spl;
namespace LibHac.FsService
{
public class FileSystemProxyCore
{
private FileSystemCreators FsCreators { get; }
private ExternalKeySet ExternalKeys { get; }
private byte[] SdEncryptionSeed { get; } = new byte[0x10];
private const string NintendoDirectoryName = "Nintendo";
@ -16,9 +18,10 @@ namespace LibHac.FsService
private GlobalAccessLogMode LogMode { get; set; }
public FileSystemProxyCore(FileSystemCreators fsCreators)
public FileSystemProxyCore(FileSystemCreators fsCreators, ExternalKeySet externalKeys)
{
FsCreators = fsCreators;
ExternalKeys = externalKeys ?? new ExternalKeySet();
}
public Result OpenBisFileSystem(out IFileSystem fileSystem, string rootPath, BisPartitionId partitionId)
@ -122,6 +125,25 @@ namespace LibHac.FsService
}
}
public Result RegisterExternalKey(ref RightsId rightsId, ref AccessKey externalKey)
{
return ExternalKeys.Add(rightsId, externalKey);
}
public Result UnregisterExternalKey(ref RightsId rightsId)
{
ExternalKeys.Remove(rightsId);
return Result.Success;
}
public Result UnregisterAllExternalKey()
{
ExternalKeys.Clear();
return Result.Success;
}
public Result SetSdCardEncryptionSeed(ReadOnlySpan<byte> seed)
{
seed.CopyTo(SdEncryptionSeed);

View file

@ -15,16 +15,17 @@ namespace LibHac.FsService
/// Creates a new <see cref="FileSystemServer"/> with a new default <see cref="ITimeSpanGenerator"/>.
/// </summary>
/// <param name="fsCreators">The <see cref="FileSystemCreators"/> used for creating filesystems.</param>
public FileSystemServer(FileSystemCreators fsCreators) : this(fsCreators, new StopWatchTimeSpanGenerator()) { }
public FileSystemServer(FileSystemCreators fsCreators) : this(fsCreators, null, new StopWatchTimeSpanGenerator()) { }
/// <summary>
/// Creates a new <see cref="FileSystemServer"/>.
/// </summary>
/// <param name="fsCreators">The <see cref="FileSystemCreators"/> used for creating filesystems.</param>
/// <param name="externalKeys">A keyset containing rights IDs and title keys. If null, an empty set will be created.</param>
/// <param name="timer">The <see cref="ITimeSpanGenerator"/> to use for access log timestamps.</param>
public FileSystemServer(FileSystemCreators fsCreators, ITimeSpanGenerator timer)
public FileSystemServer(FileSystemCreators fsCreators, ExternalKeySet externalKeys, ITimeSpanGenerator timer)
{
FsProxyCore = new FileSystemProxyCore(fsCreators);
FsProxyCore = new FileSystemProxyCore(fsCreators, externalKeys);
FsClient = new FileSystemClient(this, timer);
Timer = timer;
}

View file

@ -382,7 +382,7 @@ namespace LibHac
}
}
public static class ExternalKeys
public static class ExternalKeyReader
{
private const int TitleKeySize = 0x10;
@ -390,7 +390,7 @@ namespace LibHac
public static readonly Dictionary<string, KeyValue> UniqueKeyDict;
public static readonly Dictionary<string, KeyValue> AllKeyDict;
static ExternalKeys()
static ExternalKeyReader()
{
List<KeyValue> commonKeys = CreateCommonKeyList();
List<KeyValue> uniqueKeys = CreateUniqueKeyList();

View file

@ -174,7 +174,7 @@ namespace hactoolnet
consoleKeyFile = homeConsoleKeyFile;
}
ctx.Keyset = ExternalKeys.ReadKeyFile(keyFile, titleKeyFile, consoleKeyFile, ctx.Logger);
ctx.Keyset = ExternalKeyReader.ReadKeyFile(keyFile, titleKeyFile, consoleKeyFile, ctx.Logger);
if (ctx.Options.SdSeed != null)
{
ctx.Keyset.SetSdSeed(ctx.Options.SdSeed.ToBytes());
@ -185,15 +185,15 @@ namespace hactoolnet
string dir = ctx.Options.OutDir;
Directory.CreateDirectory(dir);
File.WriteAllText(Path.Combine(dir, keyFileName), ExternalKeys.PrintCommonKeys(ctx.Keyset));
File.WriteAllText(Path.Combine(dir, "console.keys"), ExternalKeys.PrintUniqueKeys(ctx.Keyset));
File.WriteAllText(Path.Combine(dir, "title.keys"), ExternalKeys.PrintTitleKeys(ctx.Keyset));
File.WriteAllText(Path.Combine(dir, keyFileName), ExternalKeyReader.PrintCommonKeys(ctx.Keyset));
File.WriteAllText(Path.Combine(dir, "console.keys"), ExternalKeyReader.PrintUniqueKeys(ctx.Keyset));
File.WriteAllText(Path.Combine(dir, "title.keys"), ExternalKeyReader.PrintTitleKeys(ctx.Keyset));
}
}
private static void ProcessKeygen(Context ctx)
{
Console.WriteLine(ExternalKeys.PrintCommonKeys(ctx.Keyset));
Console.WriteLine(ExternalKeyReader.PrintCommonKeys(ctx.Keyset));
}
// For running random stuff