diff options
Diffstat (limited to 'src/widgets/hardware_key_generate_dialog.cpp')
| -rw-r--r-- | src/widgets/hardware_key_generate_dialog.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/widgets/hardware_key_generate_dialog.cpp b/src/widgets/hardware_key_generate_dialog.cpp new file mode 100644 index 0000000..cb79be9 --- /dev/null +++ b/src/widgets/hardware_key_generate_dialog.cpp @@ -0,0 +1,86 @@ +#include <QVBoxLayout> +#include <QFormLayout> +#include <QLabel> +#include <QPushButton> +#include <QMessageBox> + +#include "hardware_key_generate_dialog.h" +#include "keyring.h" + +HardwareKeyDialog::HardwareKeyDialog(QWidget *parent) : QDialog(parent) +{ + setWindowTitle("Generate hardware key"); + auto *layout = new QVBoxLayout(this); + auto *name_lbl = new QLabel("Key name:", this); + m_name_line = new QLineEdit(this); + auto *info = new QLabel("The key will remain in secure hardware and can't be exported.\n" + "You can protect it with a weaker secret (such as 6-digit PIN) because it can't be stolen, but you can't back up or transfer a hardware key as it's bound to this particular machine and unusable anywhere else.\n" + "Your platform will handle the setup.", this); + info->setWordWrap(true); + auto *buttons = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Ok, this); + buttons->button(QDialogButtonBox::Ok)->setText("Set up key"); + layout->addWidget(name_lbl); + layout->addWidget(m_name_line); + layout->addWidget(info); + layout->addStretch(); // Does this work? + layout->addWidget(buttons); + + connect(buttons, &QDialogButtonBox::accepted, this, &HardwareKeyDialog::generate); + connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); +} + +void HardwareKeyDialog::generate() +{ + const QString key_name = m_name_line->text().trimmed(); + QByteArray pin; + +#ifdef SOF_AO_HARDWARE_KEY_REQUIRES_PIN_UI + PinEntryDialog pin_dlg(key_name, this); + if (pin_dlg.exec() != QDialog::Accepted) + { + return; + } + pin = pin_dlg.pin(); +#endif + + int keygen_err = generate_hardware_key(key_name, pin); + if (keygen_err) + { + QMessageBox::warning(this, "Error", QString("Key generation failed (code %1).").arg(keygen_err)); + return; + } + + accept(); +} + +PinEntryDialog::PinEntryDialog(QStringView name, QWidget *parent) : QDialog(parent) +{ + auto *layout = new QFormLayout(this); + m_pin_line = new QLineEdit(this); + m_confirm_pin_line = new QLineEdit(this); + m_buttons = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Ok, this); + layout->addRow("PIN:", m_pin_line); + layout->addRow("Confirm PIN:", m_confirm_pin_line); + layout->addRow(m_buttons); + + m_buttons->button(QDialogButtonBox::Ok)->setEnabled(false); + m_pin_line->setEchoMode(QLineEdit::Password); + m_confirm_pin_line->setEchoMode(QLineEdit::Password); + connect(m_pin_line, &QLineEdit::textChanged, this, &PinEntryDialog::validate); + connect(m_confirm_pin_line, &QLineEdit::textChanged, this, &PinEntryDialog::validate); + connect(m_buttons, &QDialogButtonBox::accepted, this, &QDialog::accept); + connect(m_buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); +} + +void PinEntryDialog::validate() +{ + bool valid = !m_pin_line->text().isEmpty() && + (m_pin_line->text().length() >= 6) && + (m_pin_line->text() == m_confirm_pin_line->text()); + m_buttons->button(QDialogButtonBox::Ok)->setEnabled(valid); +} + +QByteArray PinEntryDialog::pin() const +{ + return m_pin_line->text().toUtf8(); +} |
