aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/remotelinux/sshkeycreationdialog.cpp
blob: 16dcf9220d70b2fd7a082a81709d389b1b3ea3b6 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "sshkeycreationdialog.h"

#include "remotelinuxtr.h"

#include <projectexplorer/devicesupport/sshsettings.h>

#include <utils/fileutils.h>
#include <utils/layoutbuilder.h>
#include <utils/pathchooser.h>
#include <utils/qtcprocess.h>

#include <QApplication>
#include <QComboBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QGroupBox>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>
#include <QRadioButton>
#include <QStandardPaths>

using namespace ProjectExplorer;
using namespace Utils;

namespace RemoteLinux {

SshKeyCreationDialog::SshKeyCreationDialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(Tr::tr("SSH Key Configuration"));
    resize(385, 231);

    m_rsa = new QRadioButton(Tr::tr("&RSA"));
    m_rsa->setChecked(true);

    m_ecdsa = new QRadioButton(Tr::tr("ECDSA"));

    m_comboBox = new QComboBox;

    m_privateKeyFileValueLabel = new QLabel;

    m_publicKeyFileLabel = new QLabel;

    auto privateKeyFileButton = new QPushButton(PathChooser::browseButtonLabel());
    auto buttonBox = new QDialogButtonBox;
    m_generateButton = buttonBox->addButton(Tr::tr("&Generate And Save Key Pair"),
                                            QDialogButtonBox::AcceptRole);
    buttonBox->addButton(QDialogButtonBox::Cancel);

    // clang-format off
    using namespace Layouting;
    Column {
        Group {
            title(Tr::tr("Options")),
            Form {
                Tr::tr("Key algorithm:"), m_rsa, m_ecdsa, st, br,
                Tr::tr("Key &size:"), m_comboBox, st, br,
                Tr::tr("Private key file:"), m_privateKeyFileValueLabel, privateKeyFileButton, st, br,
                Tr::tr("Public key file:"), m_publicKeyFileLabel
            }
        },
        st,
        buttonBox
    }.attachTo(this);
    // clang-format on

    const QString defaultPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation)
        + QLatin1String("/.ssh/qtc_id");
    setPrivateKeyFile(FilePath::fromString(defaultPath));

    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::close);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &SshKeyCreationDialog::generateKeys);
    connect(m_rsa, &QRadioButton::toggled, this, &SshKeyCreationDialog::keyTypeChanged);
    connect(privateKeyFileButton, &QPushButton::clicked,
            this, &SshKeyCreationDialog::handleBrowseButtonClicked);
    keyTypeChanged();
}

SshKeyCreationDialog::~SshKeyCreationDialog() = default;

void SshKeyCreationDialog::keyTypeChanged()
{
    m_comboBox->clear();
    QStringList keySizes;
    if (m_rsa->isChecked())
        keySizes << QLatin1String("1024") << QLatin1String("2048") << QLatin1String("4096");
    else if (m_ecdsa->isChecked())
        keySizes << QLatin1String("256") << QLatin1String("384") << QLatin1String("521");
    m_comboBox->addItems(keySizes);
    if (!keySizes.isEmpty())
        m_comboBox->setCurrentIndex(0);
    m_comboBox->setEnabled(!keySizes.isEmpty());
}

void SshKeyCreationDialog::generateKeys()
{
    if (SshSettings::keygenFilePath().isEmpty()) {
        showError(Tr::tr("The ssh-keygen tool was not found."));
        return;
    }
    if (privateKeyFilePath().exists()) {
        showError(Tr::tr("Refusing to overwrite existing private key file \"%1\".")
                  .arg(privateKeyFilePath().toUserOutput()));
        return;
    }
    const QString keyTypeString = QLatin1String(m_rsa->isChecked() ? "rsa": "ecdsa");
    QApplication::setOverrideCursor(Qt::BusyCursor);
    Process keygen;
    const QStringList args{"-t", keyTypeString, "-b", m_comboBox->currentText(),
                "-N", QString(), "-f", privateKeyFilePath().path()};
    QString errorMsg;
    keygen.setCommand({SshSettings::keygenFilePath(), args});
    keygen.start();
    if (!keygen.waitForFinished())
        errorMsg = keygen.errorString();
    else if (keygen.exitCode() != 0)
        errorMsg = QString::fromLocal8Bit(keygen.readAllRawStandardError());
    if (!errorMsg.isEmpty()) {
        showError(Tr::tr("The ssh-keygen tool at \"%1\" failed: %2")
                  .arg(SshSettings::keygenFilePath().toUserOutput(), errorMsg));
    }
    QApplication::restoreOverrideCursor();
    accept();
}

void SshKeyCreationDialog::handleBrowseButtonClicked()
{
    const FilePath filePath = FileUtils::getSaveFilePath(this, Tr::tr("Choose Private Key File Name"));
    if (!filePath.isEmpty())
        setPrivateKeyFile(filePath);
}

void SshKeyCreationDialog::setPrivateKeyFile(const FilePath &filePath)
{
    m_privateKeyFileValueLabel->setText(filePath.toUserOutput());
    m_generateButton->setEnabled(!privateKeyFilePath().isEmpty());
    m_publicKeyFileLabel->setText(filePath.toUserOutput() + ".pub");
}

void SshKeyCreationDialog::showError(const QString &details)
{
    QMessageBox::critical(this, Tr::tr("Key Generation Failed"), details);
}

FilePath SshKeyCreationDialog::privateKeyFilePath() const
{
    return FilePath::fromUserInput(m_privateKeyFileValueLabel->text());
}

FilePath SshKeyCreationDialog::publicKeyFilePath() const
{
    return FilePath::fromUserInput(m_publicKeyFileLabel->text());
}

} // namespace RemoteLinux