Skip to content

Commit

Permalink
fix(lib,gui): forgot FPX files can't be signed
Browse files Browse the repository at this point in the history
  • Loading branch information
craftablescience committed Apr 21, 2024
1 parent bfca8b4 commit 6ca847b
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 113 deletions.
2 changes: 2 additions & 0 deletions include/vpkedit/format/FPX.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class FPX : public VPK {
[[nodiscard]] static std::unique_ptr<PackFile> openInternal(const std::string& path, PackFileOptions options = {}, const Callback& callback = nullptr);

private:
using VPK::generateKeyPairFiles;
using VPK::sign;
using VPK::getVersion;
using VPK::setVersion;

Expand Down
6 changes: 0 additions & 6 deletions include/vpkeditc/format/FPX.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,3 @@
VPKEDIT_API VPKEdit_PackFileHandle_t vpkedit_fpx_open(const char* path);

VPKEDIT_API VPKEdit_PackFileHandle_t vpkedit_fpx_open_with_options(const char* path, VPKEdit_PackFileOptions_t options);

VPKEDIT_API bool vpkedit_fpx_generate_keypair_files(const char* path);

VPKEDIT_API bool vpkedit_fpx_sign_from_file(VPKEdit_PackFileHandle_t handle, const char* filename);

VPKEDIT_API bool vpkedit_fpx_sign_from_mem(VPKEdit_PackFileHandle_t handle, const unsigned char* privateKeyBuffer, size_t privateKeyLen, const unsigned char* publicKeyBuffer, size_t publicKeyLen);
13 changes: 1 addition & 12 deletions src/gui/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,6 @@ Window::Window(QWidget* parent)
});
this->toolsGeneralMenu->setDisabled(true);

this->toolsFPXMenu = toolsMenu->addMenu(this->style()->standardIcon(QStyle::SP_FileIcon), "FPX");
this->toolsFPXMenu->addAction(this->style()->standardIcon(QStyle::SP_FileIcon), tr("Generate Public/Private Key Files..."), [this] {
this->generateKeyPairFiles();
});
this->toolsFPXMenu->addAction(this->style()->standardIcon(QStyle::SP_FileIcon), tr("Sign File..."), [this] {
this->signPackFile();
});
this->toolsFPXMenu->setDisabled(true);

this->toolsVPKMenu = toolsMenu->addMenu(this->style()->standardIcon(QStyle::SP_FileIcon), "VPK");
this->toolsVPKMenu->addAction(this->style()->standardIcon(QStyle::SP_FileIcon), tr("Generate Public/Private Key Files..."), [this] {
this->generateKeyPairFiles();
Expand Down Expand Up @@ -918,8 +909,7 @@ void Window::signPackFile(const QString& privateKeyLocation) {
if (privateKeyPath.isEmpty()) {
return;
}
if ((this->packFile->getType() == PackFileType::FPX && dynamic_cast<FPX&>(*this->packFile).sign(privateKeyPath.toStdString())) ||
(this->packFile->getType() == PackFileType::VPK && dynamic_cast<VPK&>(*this->packFile).sign(privateKeyPath.toStdString()))) {
if (this->packFile->getType() == PackFileType::VPK && dynamic_cast<VPK&>(*this->packFile).sign(privateKeyPath.toStdString())) {
QMessageBox::information(this, tr("Success"), tr("Successfully signed the pack file."));
} else {
QMessageBox::information(this, tr("Error"), tr("Failed to sign the pack file! Check the file contains both the private key and public key."));
Expand Down Expand Up @@ -1234,7 +1224,6 @@ void Window::freezeActions(bool freeze, bool freezeCreationActions) const {
this->addDirAction->setDisabled(freeze);
this->setPropertiesAction->setDisabled(freeze);
this->toolsGeneralMenu->setDisabled(freeze);
this->toolsFPXMenu->setDisabled(freeze || (!this->packFile || this->packFile->getType() != PackFileType::FPX));
this->toolsVPKMenu->setDisabled(freeze || (!this->packFile || this->packFile->getType() != PackFileType::VPK));

this->searchBar->setDisabled(freeze);
Expand Down
1 change: 0 additions & 1 deletion src/gui/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ class Window : public QMainWindow {
QAction* addDirAction;
QAction* setPropertiesAction;
QMenu* toolsGeneralMenu;
QMenu* toolsFPXMenu;
QMenu* toolsVPKMenu;

QNetworkAccessManager* checkForNewUpdateNetworkManager;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/format/VPK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ bool VPK::generateKeyPairFiles(const std::string& name) {
}

bool VPK::sign(const std::string& filename_) {
if (this->header1.version == 1 || !std::filesystem::exists(filename_) || std::filesystem::is_directory(filename_)) {
if (this->header1.version != 2 || !std::filesystem::exists(filename_) || std::filesystem::is_directory(filename_)) {
return false;
}

Expand All @@ -778,7 +778,7 @@ bool VPK::sign(const std::string& filename_) {
}

bool VPK::sign(const std::vector<std::byte>& privateKey, const std::vector<std::byte>& publicKey) {
if (this->header1.version == 1) {
if (this->header1.version != 2) {
return false;
}

Expand Down
33 changes: 0 additions & 33 deletions src/lib/lang/c/format/FPX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,3 @@ VPKEDIT_API VPKEdit_PackFileHandle_t vpkedit_fpx_open_with_options(const char* p
}
return packFile.release();
}

VPKEDIT_API bool vpkedit_fpx_generate_keypair_files(const char* path) {
VPKEDIT_EARLY_RETURN_VALUE(path, false);

return FPX::generateKeyPairFiles(path);
}

VPKEDIT_API bool vpkedit_fpx_sign_from_file(VPKEdit_PackFileHandle_t handle, const char* filename) {
VPKEDIT_EARLY_RETURN_VALUE(handle, false);
VPKEDIT_EARLY_RETURN_VALUE(filename, false);

auto* fpx = ::getPackFile(handle);
if (fpx->getType() != PackFileType::FPX) {
return false;
}
return dynamic_cast<FPX*>(fpx)->sign(filename);
}

VPKEDIT_API bool vpkedit_fpx_sign_from_mem(VPKEdit_PackFileHandle_t handle, const unsigned char* privateKeyBuffer, size_t privateKeyLen, const unsigned char* publicKeyBuffer, size_t publicKeyLen) {
VPKEDIT_EARLY_RETURN_VALUE(handle, false);
VPKEDIT_EARLY_RETURN_VALUE(privateKeyBuffer, false);
VPKEDIT_EARLY_RETURN_VALUE(privateKeyLen, false);
VPKEDIT_EARLY_RETURN_VALUE(publicKeyBuffer, false);
VPKEDIT_EARLY_RETURN_VALUE(publicKeyLen, false);

auto* fpx = ::getPackFile(handle);
if (fpx->getType() != PackFileType::FPX) {
return false;
}
return dynamic_cast<FPX*>(fpx)->sign(
{reinterpret_cast<const std::byte*>(privateKeyBuffer), reinterpret_cast<const std::byte*>(privateKeyBuffer + privateKeyLen)},
{reinterpret_cast<const std::byte*>(publicKeyBuffer), reinterpret_cast<const std::byte*>(publicKeyBuffer + publicKeyLen)});
}
60 changes: 1 addition & 59 deletions src/lib/lang/csharp/libvpkedit/Format/FPX.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;

namespace libvpkedit.Format
{
Expand All @@ -12,15 +9,6 @@ internal static unsafe partial class Extern

[DllImport("libvpkeditc")]
public static extern void* vpkedit_fpx_open_with_options([MarshalAs(UnmanagedType.LPStr)] string path, PackFileOptions options);

[DllImport("libvpkeditc")]
public static extern byte vpkedit_fpx_generate_keypair_files([MarshalAs(UnmanagedType.LPStr)] string path);

[DllImport("libvpkeditc")]
public static extern byte vpkedit_fpx_sign_from_file(void* handle, [MarshalAs(UnmanagedType.LPStr)] string filepath);

[DllImport("libvpkeditc")]
public static extern byte vpkedit_fpx_sign_from_mem(void* handle, byte* privateKeyBuffer, ulong privateKeyLen, byte* publicKeyBuffer, ulong publicKeyLen);
}

public class FPX : PackFile
Expand All @@ -44,51 +32,5 @@ private protected unsafe FPX(void* handle) : base(handle) {}
return handle == null ? null : new FPX(handle);
}
}

public static bool GenerateKeyPairFiles(string path)
{
unsafe
{
return Convert.ToBoolean(Extern.vpkedit_vpk_generate_keypair_files(path));
}
}

public bool Sign(string filepath)
{
unsafe
{
return Convert.ToBoolean(Extern.vpkedit_fpx_sign_from_file(Handle, filepath));
}
}

public bool Sign(byte[] privateKey, byte[] publicKey)
{
unsafe
{
fixed (byte* privateKeyBufferPtr = privateKey)
{
fixed (byte* publicKeyBufferPtr = publicKey)
{
return Convert.ToBoolean(Extern.vpkedit_fpx_sign_from_mem(Handle, privateKeyBufferPtr, (ulong)privateKey.LongLength, publicKeyBufferPtr, (ulong)publicKey.LongLength));
}
}
}
}

public bool Sign(IEnumerable<byte> privateKey, IEnumerable<byte> publicKey)
{
var privateKeyData = privateKey.ToArray();
var publicKeyData = publicKey.ToArray();
unsafe
{
fixed (byte* privateKeyBufferPtr = privateKeyData)
{
fixed (byte* publicKeyBufferPtr = publicKeyData)
{
return Convert.ToBoolean(Extern.vpkedit_fpx_sign_from_mem(Handle, privateKeyBufferPtr, (ulong)privateKeyData.LongLength, publicKeyBufferPtr, (ulong)publicKeyData.LongLength));
}
}
}
}
}
}

0 comments on commit 6ca847b

Please sign in to comment.