aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/hardware_key_generate_dialog.cpp
blob: cb79be9215c91577d8c595e8f9b000c9b2548474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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();
}