quill/encryptionmanager.cpp

384 lines
16 KiB
C++
Raw Normal View History

2021-10-08 20:37:32 -07:00
#include "encryptionmanager.h"
#include "ui_encryptionmanager.h"
#include <QFile>
#include <QScreen>
#include <QDebug>
#include <QDir>
#include <QTimer>
#include <QMessageBox>
#include <QDateTime>
#include "usbms_splash.h"
2021-10-08 20:37:32 -07:00
#include "functions.h"
encryptionManager::encryptionManager(QWidget *parent) :
QWidget(parent),
ui(new Ui::encryptionManager)
{
ui->setupUi(this);
ui->descriptionLabel->setFont(QFont("u001"));
ui->successDescriptionLabel->setFont(QFont("u001"));
ui->failureDescriptionLabel->setFont(QFont("u001"));
ui->warningDescriptionLabel->setFont(QFont("u001"));
2021-10-08 20:37:32 -07:00
// Stylesheet
2022-04-03 20:26:15 -07:00
QFile stylesheetFile("/mnt/onboard/.adds/inkbox/eink.qss");
2021-10-08 20:37:32 -07:00
stylesheetFile.open(QFile::ReadOnly);
this->setStyleSheet(stylesheetFile.readAll());
stylesheetFile.close();
ui->encryptionSetupLabel->setStyleSheet("font-size: 15pt; font-weight: bold");
ui->descriptionLabel->setStyleSheet("font-size: 10pt");
ui->successLabel->setStyleSheet("font-size: 15pt; font-weight: bold");
ui->successDescriptionLabel->setStyleSheet("font-size: 10pt");
ui->failureLabel->setStyleSheet("font-size: 15pt; font-weight: bold");
ui->failureDescriptionLabel->setStyleSheet("font-size: 10pt");
2021-12-27 13:08:24 -08:00
ui->setupContinueBtn->setStyleSheet("background: lightGrey; border: 3px solid black; color: black; padding: 10px; outline: none; font-size: 10pt; font-weight: bold; border-radius: 10px");
ui->setupAbortBtn->setStyleSheet("background: lightGrey; border: 3px solid black; color: black; padding: 10px; outline: none; font-size: 10pt; font-weight: bold; border-radius: 10px");
ui->exitSuccessBtn->setStyleSheet("background: lightGrey; border: 3px solid black; color: black; padding: 10px; outline: none; font-size: 10pt; font-weight: bold; border-radius: 10px");
ui->failureContinueBtn->setStyleSheet("background: lightGrey; border: 3px solid black; color: black; padding: 10px; outline: none; font-size: 10pt; font-weight: bold; border-radius: 10px");
ui->acceptBtn->setStyleSheet("background: lightGrey; border: 3px solid black; color: black; padding: 10px; outline: none; font-size: 10pt; font-weight: bold; border-radius: 10px");
ui->usbmsBtn->setStyleSheet("background: lightGrey; border: 3px solid black; color: black; padding: 10px; outline: none; font-size: 10pt; font-weight: bold; border-radius: 10px");
ui->warningLabel->setStyleSheet("font-size: 15pt; font-weight: bold");
ui->warningDescriptionLabel->setStyleSheet("font-size: 10pt");
2021-10-08 20:37:32 -07:00
// Getting the screen's size
float sW = QGuiApplication::screens()[0]->size().width();
float sH = QGuiApplication::screens()[0]->size().height();
float stdIconWidth;
float stdIconHeight;
{
stdIconWidth = sW / 1.50;
stdIconHeight = sH / 1.50;
QPixmap pixmap(":/resources/encryption.png");
QPixmap scaledPixmap = pixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
ui->encryptionImageLabel->setPixmap(scaledPixmap);
}
{
stdIconWidth = sW / 1.65;
stdIconHeight = sH / 1.65;
QPixmap pixmap(":/resources/check-display.png");
QPixmap scaledPixmap = pixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
ui->checkImageLabel->setPixmap(scaledPixmap);
}
2021-10-10 06:54:59 -07:00
{
stdIconWidth = sW / 1.65;
stdIconHeight = sH / 1.65;
QPixmap pixmap(":/resources/error.png");
QPixmap scaledPixmap = pixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
ui->failureImageLabel->setPixmap(scaledPixmap);
}
{
stdIconWidth = sW / 1.50;
stdIconHeight = sH / 1.50;
QPixmap pixmap(":/resources/alert-triangle.png");
QPixmap scaledPixmap = pixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
ui->warningImageLabel->setPixmap(scaledPixmap);
}
2021-10-08 20:37:32 -07:00
hourglassAnimationWidgetWindow = new hourglassAnimationWidget();
ui->hourglassWidget->insertWidget(0, hourglassAnimationWidgetWindow);
ui->hourglassWidget->setCurrentIndex(0);
2021-10-10 06:54:59 -07:00
setDefaultWorkDir();
2021-11-14 19:23:09 -08:00
if(checkconfig(".config/18-encrypted_storage/initial_setup_done") == true or checkconfig("/external_root/run/encfs_repack") == true) {
2021-10-10 09:53:24 -07:00
ui->activityWidget->hide();
2021-11-14 19:23:09 -08:00
if(checkconfig("/external_root/run/encfs_repack") == false) {
setupPassphraseDialogMode = 1;
}
else {
setupPassphraseDialogMode = 2;
}
QTimer::singleShot(500, this, SLOT(setupPassphraseDialog()));
2021-10-10 06:54:59 -07:00
}
else {
QDir dir("/mnt/onboard/onboard/encfs-dropbox");
if(dir.isEmpty()) {
ui->activityWidget->setCurrentIndex(4);
2021-11-09 04:38:15 -08:00
mkEncfsDirs();
}
}
2021-10-08 20:37:32 -07:00
}
encryptionManager::~encryptionManager()
{
delete ui;
}
void encryptionManager::on_setupContinueBtn_clicked()
{
2021-10-10 09:53:24 -07:00
setupPassphraseDialogMode = 0;
setupPassphraseDialog();
2021-10-10 06:54:59 -07:00
}
2021-10-10 09:53:24 -07:00
void encryptionManager::setupPassphraseDialog() {
2021-10-10 06:54:59 -07:00
/*
2021-10-10 09:53:24 -07:00
* setupPassphraseDialogMode can be:
2021-10-10 06:54:59 -07:00
* 0: Initial setup
* 1: Normal behavior
2021-11-14 18:22:58 -08:00
* 2: Repack
2021-10-10 06:54:59 -07:00
*/
2022-04-15 17:27:58 -07:00
log("Showing passphrase dialog for mode " + QString::number(setupPassphraseDialogMode), className);
2021-10-10 06:54:59 -07:00
ui->activityWidget->hide();
2021-10-08 20:37:32 -07:00
this->setStyleSheet("background-color: black");
global::keyboard::keyboardDialog = true;
global::keyboard::encfsDialog = true;
global::keyboard::keyboardText = "";
generalDialogWindow = new generalDialog();
generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
connect(generalDialogWindow, SIGNAL(refreshScreen()), SLOT(refreshScreen()));
2021-10-10 09:53:24 -07:00
if(setupPassphraseDialogMode == 0) {
2021-10-10 06:54:59 -07:00
connect(generalDialogWindow, SIGNAL(destroyed(QObject*)), SLOT(setupEncryptedStorage()));
}
2021-11-14 18:22:58 -08:00
else if(setupPassphraseDialogMode == 1) {
2021-10-10 06:54:59 -07:00
connect(generalDialogWindow, SIGNAL(destroyed(QObject*)), SLOT(unlockEncryptedStorage()));
}
2021-11-14 18:22:58 -08:00
else {
connect(generalDialogWindow, SIGNAL(destroyed(QObject*)), SLOT(repackEncryptedStorage()));
}
2021-10-08 20:37:32 -07:00
connect(generalDialogWindow, SIGNAL(showToast(QString)), SLOT(showToast(QString)));
generalDialogWindow->show();
}
void encryptionManager::on_setupAbortBtn_clicked()
{
2022-04-15 17:27:58 -07:00
log("Aborting setup", className);
2021-10-08 20:37:32 -07:00
setDefaultWorkDir();
2021-10-08 20:49:39 -07:00
string_writeconfig(".config/18-encrypted_storage/status", "false");
2021-10-08 20:37:32 -07:00
quit_restart();
}
void encryptionManager::quit_restart() {
2022-04-15 17:27:58 -07:00
log("Restarting InkBox", className);
2021-10-08 20:37:32 -07:00
QProcess process;
process.startDetached("inkbox", QStringList());
qApp->quit();
}
void encryptionManager::refreshScreen() {
this->repaint();
}
void encryptionManager::showToast(QString messageToDisplay) {
global::toast::message = messageToDisplay;
toastWindow = new toast(this);
toastWindow->setAttribute(Qt::WA_DeleteOnClose);
connect(toastWindow, SIGNAL(refreshScreen()), SLOT(refreshScreen()));
toastWindow->show();
}
void encryptionManager::setupEncryptedStorage() {
2022-04-15 17:27:58 -07:00
log("Setting up encrypted storage", className);
this->setStyleSheet("background-color: white");
ui->activityWidget->show();
if(global::encfs::cancelSetup == true) {
global::encfs::cancelSetup = false;
ui->activityWidget->setCurrentIndex(0);
}
else {
mkEncfsDirs();
std::string bootstrapPassphrase = global::encfs::passphrase.toStdString();
global::encfs::passphrase = "";
string_writeconfig("/external_root/run/encfs/encrypted_storage_create", "true");
string_writeconfig("/external_root/run/encfs/encrypted_storage_bootstrap_files_location", "/data/onboard/encfs-dropbox");
string_writeconfig("/external_root/run/encfs/encrypted_storage_bootstrap_archive_location", "/data/onboard/data.encfs");
string_writeconfig("/external_root/run/encfs/encrypted_storage_bootstrap_passphrase", bootstrapPassphrase);
setDefaultWorkDir();
string_writeconfig(".config/18-encrypted_storage/storage_list", "/data/onboard/encfs-decrypted");
string_writeconfig("/opt/ibxd", "encfs_restart\n");
bool exitStatus;
ui->activityWidget->setCurrentIndex(3);
QTimer * t = new QTimer(this);
t->setInterval(1000);
connect(t, &QTimer::timeout, [&]() {
if(QFile::exists("/external_root/run/encrypted_storage_bootstrap_setup")) {
exitStatus = checkconfig("/external_root/run/encrypted_storage_bootstrap_setup");
QFile::remove("/external_root/run/encrypted_storage_bootstrap_setup");
setupExitWidget(exitStatus);
}
} );
t->start();
}
2021-10-08 20:37:32 -07:00
}
2021-10-10 06:54:59 -07:00
void encryptionManager::unlockEncryptedStorage() {
2022-04-15 17:27:58 -07:00
log("Trying to unlock encrypted storage", className);
if(global::encfs::cancelSetup == true) {
global::encfs::cancelSetup = false;
poweroff(true);
qApp->quit();
}
else {
this->setStyleSheet("background-color: white");
ui->activityWidget->show();
mkEncfsDirs();
std::string passphrase = global::encfs::passphrase.toStdString();
global::encfs::passphrase = "";
string_writeconfig("/external_root/run/encfs/encrypted_storage_archive", "/data/onboard/data.encfs");
string_writeconfig("/external_root/run/encfs/encrypted_storage_mountpoint", "/data/onboard/encfs-decrypted");
string_writeconfig("/external_root/run/encfs/encrypted_storage_bindmount", "/kobo/mnt/onboard/onboard/encfs-decrypted");
string_writeconfig("/external_root/run/encfs/encrypted_storage_passphrase", passphrase);
string_writeconfig("/opt/ibxd", "encfs_restart\n");
bool exitStatus;
string_checkconfig_ro("/inkbox/encryptedStoragePassphraseTries");
if(checkconfig_str_val.isEmpty()) {
passphraseTries = 0;
}
else {
passphraseTries = checkconfig_str_val.toInt();
passphraseTries++;
}
ui->activityWidget->setCurrentIndex(3);
QTimer * t = new QTimer(this);
t->setInterval(1000);
connect(t, &QTimer::timeout, [&]() {
if(QFile::exists("/external_root/run/encfs_mounted")) {
exitStatus = checkconfig("/external_root/run/encfs_mounted");
if(exitStatus == false) {
2022-04-15 17:27:58 -07:00
QString function = __func__; log(function + ": Invalid passphrase", className);
if(setupMessageBoxRan == false) {
int delay = 0;
if(passphraseTries <= 3) {
if(passphraseTries == 0) {
string_writeconfig("/inkbox/encryptedStoragePassphraseTries", "0");
delay = 5000;
}
else if(passphraseTries == 1) {
string_writeconfig("/inkbox/encryptedStoragePassphraseTries", "1");
delay = 10000;
}
else if(passphraseTries == 2) {
string_writeconfig("/inkbox/encryptedStoragePassphraseTries", "2");
delay = 20000;
}
else if(passphraseTries >= 3) {
string_writeconfig("/inkbox/encryptedStoragePassphraseTries", "3");
unsigned long currentEpoch = QDateTime::currentSecsSinceEpoch();
currentEpoch += 86400;
std::string unlockTime_str = to_string(currentEpoch);
global::encfs::unlockTime = QDateTime::fromTime_t(currentEpoch).toString();
2022-04-04 22:37:04 -07:00
global::encfs::lockdownMessage = "FATAL: 4 invalid passphrase tries, locking down device until " + global::encfs::unlockTime;
string_writeconfig("/external_root/boot/flags/ENCRYPT_LOCK", unlockTime_str);
global::encfs::lockdown = true;
setupMessageBoxRan = true;
alertWindow = new alert();
alertWindow->setAttribute(Qt::WA_DeleteOnClose);
alertWindow->showFullScreen();
}
if(passphraseTries <= 2) {
QTimer::singleShot(delay, this, SLOT(setupFailedAuthenticationMessageBox()));
setupMessageBoxRan = true;
}
}
}
}
else {
log("Unlocking successful", className);
quit_restart();
}
}
} );
t->start();
}
2021-10-10 06:54:59 -07:00
}
2021-10-08 20:37:32 -07:00
void encryptionManager::mkEncfsDirs() {
2022-04-15 17:27:58 -07:00
log("Creating encrypted storage directories", className);
2021-10-08 20:37:32 -07:00
QDir encfsDir;
QString encfsPath("/external_root/run/encfs");
encfsDir.mkpath(encfsPath);
QDir dumpDir;
QString dumpPath("/mnt/onboard/onboard/encfs-dropbox");
dumpDir.mkpath(dumpPath);
QDir decDir;
QString decPath("/mnt/onboard/onboard/encfs-decrypted");
2021-10-10 09:53:24 -07:00
decDir.mkpath(decPath);
2021-10-08 20:37:32 -07:00
}
void encryptionManager::on_exitSuccessBtn_clicked()
{
2021-10-10 06:54:59 -07:00
setDefaultWorkDir();
string_writeconfig(".config/18-encrypted_storage/initial_setup_done", "true");
quit_restart();
}
void encryptionManager::setupExitWidget(bool exitStatus) {
if(setupExitWidgetRan == false) {
if(exitStatus == true) {
ui->activityWidget->setCurrentIndex(1);
}
else {
string_writeconfig(".config/18-encrypted_storage/status", "false");
ui->activityWidget->setCurrentIndex(2);
}
setupExitWidgetRan = true;
}
}
2021-10-10 06:54:59 -07:00
void encryptionManager::on_failureContinueBtn_clicked()
{
setDefaultWorkDir();
string_writeconfig(".config/18-encrypted_storage/initial_setup_done", "true");
quit_restart();
}
void encryptionManager::setupFailedAuthenticationMessageBox() {
2022-04-15 17:27:58 -07:00
log("Showing 'Authentication failed' message box", className);
ui->activityWidget->hide();
QMessageBox::critical(this, tr("Invalid argument"), tr("<font face='u001'>Invalid passphrase. Please try again.</font>"));
QFile::remove("/external_root/run/encfs_mounted");
quit_restart();
}
void encryptionManager::on_acceptBtn_clicked()
{
string_writeconfig(".config/18-encrypted_storage/status", "false");
quit_restart();
}
void encryptionManager::on_usbmsBtn_clicked()
{
2022-04-15 17:27:58 -07:00
log("Showing USBMS splash", className);
global::usbms::launchUsbms = true;
usbmsWindow = new usbms_splash();
usbmsWindow->setAttribute(Qt::WA_DeleteOnClose);
usbmsWindow->setGeometry(QRect(QPoint(0,0), screen()->geometry ().size()));
usbmsWindow->show();
}
2021-11-14 18:22:58 -08:00
void encryptionManager::repackEncryptedStorage() {
2022-04-15 17:27:58 -07:00
log("Repacking encrypted storage", className);
2021-11-14 18:22:58 -08:00
if(global::encfs::cancelSetup == true) {
global::encfs::cancelSetup = false;
quit_restart();
}
else {
this->setStyleSheet("background-color: white");
ui->activityWidget->show();
mkEncfsDirs();
std::string passphrase = global::encfs::passphrase.toStdString();
global::encfs::passphrase = "";
2021-11-14 19:23:09 -08:00
string_writeconfig("/external_root/run/encfs/encrypted_storage_repack_passphrase", passphrase);
QFile::remove("/external_root/run/openrc/started/encfs");
2021-11-14 18:22:58 -08:00
string_writeconfig("/opt/ibxd", "encfs_restart\n");
bool exitStatus;
ui->activityWidget->setCurrentIndex(3);
QTimer * t = new QTimer(this);
t->setInterval(1000);
connect(t, &QTimer::timeout, [&]() {
if(QFile::exists("/external_root/run/encfs_repack_status")) {
exitStatus = checkconfig("/external_root/run/encfs_repack_status");
QFile::remove("/external_root/run/encfs_repack_status");
setupExitWidget(exitStatus);
}
} );
t->start();
}
}