summaryrefslogtreecommitdiff
path: root/src/plugins/coreplugin/ssh/sshkeygenerator.cpp
blob: 17a63886e31ee3a7bd7d26b586a15794530de6e3 (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
#include "sshkeygenerator.h"

#include "ne7sshobject.h"

#include <QtCore/QFile>
#include <QtCore/QTemporaryFile>

#include <ne7ssh.h>

namespace Core {

SshKeyGenerator::SshKeyGenerator()
{
}

bool SshKeyGenerator::generateKeys(KeyType type, const QString &id, int keySize)
{
    QTemporaryFile tmpPubKeyFile;
    QTemporaryFile tmpPrivKeyFile;
    if (!tmpPubKeyFile.open() || !tmpPrivKeyFile.open()) {
        m_error = tr("Error creating temporary files.");
        return false;
    }
    tmpPubKeyFile.setAutoRemove(false);
    tmpPubKeyFile.close();
    tmpPrivKeyFile.close();
    const char * const typeStr = type == Rsa ? "rsa" : "dsa";
    Internal::Ne7SshObject::Ptr ne7Object
        = Internal::Ne7SshObject::instance()->get();
    if (!ne7Object->generateKeyPair(typeStr, id.toUtf8(),
             tmpPrivKeyFile.fileName().toUtf8(),
             tmpPubKeyFile.fileName().toUtf8(), keySize)) {
        // TODO: Race condition on pop() call. Perhaps not use Net7 errors? Or hack API
        m_error = tr("Error generating keys: %1")
                  .arg(ne7Object->errors()->pop());
        return false;
    }

    if (!tmpPubKeyFile.open() || !tmpPrivKeyFile.open()) {
        m_error = tr("Error reading temporary files.");
        return false;
    }

    m_publicKey = tmpPubKeyFile.readAll();
    m_privateKey = tmpPrivKeyFile.readAll();
    if (tmpPubKeyFile.error() != QFile::NoError
        || tmpPrivKeyFile.error() != QFile::NoError) {
        m_error = tr("Error reading temporary files.");
        return false;
    }

    m_type = type;
    return true;
}

} // namespace Core