GUI: Improve PS3 decryption tool

This commit is contained in:
Eladash 2020-12-14 20:19:43 +02:00 committed by Megamouse
parent fb47d1f788
commit ba03df8511
6 changed files with 169 additions and 33 deletions

View File

@ -1127,17 +1127,14 @@ void game_list_frame::ShowContextMenu(const QPoint &pos)
{
const QString custom_title = m_persistent_settings->GetValue(gui::persistent::titles, serial, "").toString();
const QString old_title = custom_title.isEmpty() ? name : custom_title;
QString new_title;
input_dialog dlg(128, old_title, tr("Rename Title"), tr("%0\n%1\n\nYou can clear the line in order to use the original title.").arg(name).arg(serial), name, this);
dlg.move(global_pos);
connect(&dlg, &input_dialog::text_changed, [&new_title](const QString& text)
{
new_title = text.simplified();
});
if (dlg.exec() == QDialog::Accepted)
{
const QString new_title = dlg.get_input_text().simplified();
if (new_title.isEmpty() || new_title == name)
{
m_titles.remove(serial);

View File

@ -1,9 +1,14 @@
#include "input_dialog.h"
#include "qt_utils.h"
#include <QVBoxLayout>
#include <QDialogButtonBox>
#include <QLineEdit>
#include <QPushButton>
#include <QValidator>
#include <QLabel>
constexpr auto qstr = QString::fromStdString;
input_dialog::input_dialog(int max_length, const QString& text, const QString& title, const QString& label, const QString& placeholder, QWidget *parent, Qt::WindowFlags f)
: QDialog(parent, f)
@ -12,21 +17,21 @@ input_dialog::input_dialog(int max_length, const QString& text, const QString& t
m_label = new QLabel(label);
QLineEdit* input = new QLineEdit();
input->setPlaceholderText(placeholder);
input->setText(text);
input->setMaxLength(max_length);
input->setClearButtonEnabled(true);
connect(input, &QLineEdit::textChanged, this, &input_dialog::text_changed);
m_input = new QLineEdit();
m_input->setPlaceholderText(placeholder);
m_input->setText(text);
m_input->setMaxLength(max_length);
m_input->setClearButtonEnabled(true);
connect(m_input, &QLineEdit::textChanged, this, &input_dialog::text_changed);
QDialogButtonBox* button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(button_box, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(button_box, &QDialogButtonBox::rejected, this, &QDialog::reject);
m_button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(m_button_box, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_button_box, &QDialogButtonBox::rejected, this, &QDialog::reject);
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(m_label);
layout->addWidget(input);
layout->addWidget(button_box);
layout->addWidget(m_input);
layout->addWidget(m_button_box);
setLayout(layout);
setFixedHeight(sizeHint().height());
@ -36,7 +41,41 @@ input_dialog::~input_dialog()
{
}
void input_dialog::set_clear_button_enabled(bool enabled)
{
m_input->setClearButtonEnabled(enabled);
}
void input_dialog::set_input_font(const QFont& font, bool fix_width, char sample)
{
if (int max = m_input->maxLength(); max > 0 && fix_width && std::isprint(static_cast<uchar>(sample)))
{
const QString str = qstr(std::string(static_cast<std::size_t>(max), sample));
m_input->setFixedWidth(gui::utils::get_label_width(str, &font));
}
m_input->setFont(font);
}
void input_dialog::set_validator(const QValidator* validator)
{
m_input->setValidator(validator);
}
void input_dialog::set_button_enabled(QDialogButtonBox::StandardButton id, bool enabled)
{
if (QPushButton* button = m_button_box->button(id))
{
button->setEnabled(enabled);
}
}
void input_dialog::set_label_text(const QString& text)
{
m_label->setText(text);
}
QString input_dialog::get_input_text() const
{
return m_input->text();
}

View File

@ -1,7 +1,11 @@
#pragma once
#include <QDialog>
#include <QLabel>
#include <QDialogButtonBox>
class QLabel;
class QLineEdit;
class QValidator;
class input_dialog : public QDialog
{
@ -12,9 +16,16 @@ public:
~input_dialog();
void set_label_text(const QString& text);
void set_validator(const QValidator* validator);
void set_clear_button_enabled(bool enable);
void set_input_font(const QFont& font, bool fix_width, char sample = '\0');
void set_button_enabled(QDialogButtonBox::StandardButton id, bool enabled);
QString get_input_text() const;
private:
QLabel* m_label = nullptr;
QLineEdit* m_input = nullptr;
QDialogButtonBox* m_button_box = nullptr;
QString m_text;
Q_SIGNALS:

View File

@ -27,8 +27,10 @@
#include "pkg_install_dialog.h"
#include "category.h"
#include "gui_settings.h"
#include "input_dialog.h"
#include <thread>
#include <charconv>
#include <QScreen>
#include <QDirIterator>
@ -38,10 +40,12 @@
#include "rpcs3_version.h"
#include "Emu/System.h"
#include "Emu/IdManager.h"
#include "Emu/system_config.h"
#include "Crypto/unpkg.h"
#include "Crypto/unself.h"
#include "Crypto/unedat.h"
#include "Loader/PUP.h"
#include "Loader/TAR.h"
@ -876,27 +880,55 @@ void main_window::DecryptSPRXLibraries()
return;
}
if (!m_gui_settings->GetBootConfirmation(this))
{
return;
}
Emu.SetForceBoot(true);
Emu.Stop();
m_gui_settings->SetValue(gui::fd_decrypt_sprx, QFileInfo(modules.first()).path());
gui_log.notice("Decrypting binaries...");
// Always start with no KLIC
std::vector<v128> klics{v128{}};
if (const auto keys = g_fxo->get<loaded_npdrm_keys>())
{
// Second klic: get it from a running game
if (const v128 klic = keys->devKlic; klic != v128{})
{
klics.emplace_back(klic);
}
}
for (const QString& _module : modules)
{
const std::string old_path = sstr(_module);
fs::file elf_file(old_path);
fs::file elf_file;
if (elf_file && elf_file.size() >= 4 && elf_file.read<u32>() == "SCE\0"_u32)
bool tried = false;
bool invalid = false;
std::size_t key_it = 0;
while (true)
{
elf_file = decrypt_self(std::move(elf_file));
for (; key_it < klics.size(); key_it++)
{
if (elf_file.open(old_path) && elf_file.size() >= 4 && elf_file.read<u32>() == "SCE\0"_u32)
{
// First KLIC is no KLIC
elf_file = decrypt_self(std::move(elf_file), key_it != 0 ? klics[key_it]._bytes : nullptr);
if (!elf_file)
{
// Try another key
continue;
}
}
else
{
elf_file = {};
invalid = true;
}
break;
}
if (elf_file)
{
@ -912,12 +944,67 @@ void main_window::DecryptSPRXLibraries()
{
gui_log.error("Failed to create %s", new_path);
}
break;
}
else if (!invalid)
{
// Allow the user to manually type KLIC if decryption failed
const std::string filename = old_path.substr(old_path.find_last_of(fs::delim) + 1);
const QString hint = tr("Hint: KLIC (KLicense key) is a 16-byte long string. (32 hexadecimal characters)"
"\nAnd is logged with some sceNpDrm* functions when the game/application which owns \"%0\" is running.").arg(qstr(filename));
if (tried)
{
gui_log.error("Failed to decrypt %s with specfied KLIC, retrying.\n%s", old_path, sstr(hint));
}
input_dialog dlg(32, "", tr("Enter KLIC of %0").arg(qstr(filename)),
tried ? tr("Decryption failed with provided KLIC.\n%0").arg(hint) : tr("Hexadecimal only."), "00000000000000000000000000000000", this);
QFont mono = QFontDatabase::systemFont(QFontDatabase::FixedFont);
mono.setPointSize(8);
dlg.set_input_font(mono, true, '0');
dlg.set_clear_button_enabled(false);
dlg.set_button_enabled(QDialogButtonBox::StandardButton::Ok, false);
dlg.set_validator(new QRegExpValidator(QRegExp("^[a-fA-F0-9]*$"))); // HEX only
connect(&dlg, &input_dialog::text_changed, [&](const QString& text)
{
dlg.set_button_enabled(QDialogButtonBox::StandardButton::Ok, text.size() == 32);
});
if (dlg.exec() == QDialog::Accepted)
{
const std::string text = sstr(dlg.get_input_text());
auto& klic = (tried ? klics.back() : klics.emplace_back());
ensure(text.size() == 32);
// It must succeed (only hex characters are present)
std::from_chars(&text[0], &text[16], klic._u64[1], 16); // Not a typo: on LE systems the u64[1] part will be swapped with u64[0] later
std::from_chars(&text[16], &text[32], klic._u64[0], 16); // And on BE systems it will be already swapped by index internally
// Needs to be in big endian because the left to right byte-order means big endian
klic = std::bit_cast<be_t<v128>>(klic);
// Retry with specified KLIC
key_it -= +std::exchange(tried, true); // Rewind on second and above attempt
gui_log.notice("KLIC entered for %s: %s", filename, klic);
continue;
}
else
{
gui_log.error("Failed to decrypt %s", old_path);
gui_log.notice("User has cancelled entering KLIC.");
}
}
gui_log.error("Failed to decrypt \"%s\".", old_path);
break;
}
}
gui_log.notice("Finished decrypting all binaries.");

View File

@ -126,9 +126,11 @@ namespace gui
return dummy_font.font();
}
int get_label_width(const QString& text)
int get_label_width(const QString& text, const QFont* font)
{
return QLabel(text).sizeHint().width();
QLabel l(text);
if (font) l.setFont(*font);
return l.sizeHint().width();
}
QImage get_opaque_image_area(const QString& path)

View File

@ -44,7 +44,7 @@ namespace gui
QFont get_label_font(const QString& object_name);
// Returns the width of the text
int get_label_width(const QString& text);
int get_label_width(const QString& text, const QFont* font = nullptr);
// Returns the part of the image loaded from path that is inside the bounding box of its opaque areas
QImage get_opaque_image_area(const QString& path);