diff --git a/src/libs/utils/ssh/sshkeygenerator.cpp b/src/libs/utils/ssh/sshkeygenerator.cpp index d2e1ff1124e977555dbe3e47c747940f42113a1d..d249812c46f3fb7d8c031c2b528b2ab2aa1ffea1 100644 --- a/src/libs/utils/ssh/sshkeygenerator.cpp +++ b/src/libs/utils/ssh/sshkeygenerator.cpp @@ -59,9 +59,11 @@ SshKeyGenerator::SshKeyGenerator() : m_type(Rsa) { } -bool SshKeyGenerator::generateKeys(KeyType type, PrivateKeyFormat format, int keySize) +bool SshKeyGenerator::generateKeys(KeyType type, PrivateKeyFormat format, int keySize, + EncryptionMode encryptionMode) { m_type = type; + m_encryptionMode = encryptionMode; try { AutoSeeded_RNG rng; @@ -102,21 +104,10 @@ void SshKeyGenerator::generatePkcs8KeyString(const KeyPtr &key, bool privateKey, pipe.start_msg(); QByteArray *keyData; if (privateKey) { - QInputDialog d; - d.setInputMode(QInputDialog::TextInput); - d.setTextEchoMode(QLineEdit::Password); - d.setWindowTitle(tr("Password for Private Key")); - d.setLabelText(tr("It is recommended that you secure your private key\n" - "with a password, which you can enter below.")); - d.setOkButtonText(tr("Encrypt key file")); - d.setCancelButtonText(tr("Do not encrypt key file")); - int result = QDialog::Accepted; QString password; - while (result == QDialog::Accepted && password.isEmpty()) { - result = d.exec(); - password = d.textValue(); - } - if (result == QDialog::Accepted) + if (m_encryptionMode == DoOfferEncryption) + password = getPassword(); + if (!password.isEmpty()) PKCS8::encrypt_key(*key, pipe, rng, password.toLocal8Bit().data()); else PKCS8::encode(*key, pipe); @@ -188,4 +179,23 @@ void SshKeyGenerator::generateOpenSslPrivateKeyString(const KeyPtr &key) m_privateKey = QByteArray(PEM_Code::encode (encoder.get_contents(), label).c_str()); } +QString SshKeyGenerator::getPassword() const +{ + QInputDialog d; + d.setInputMode(QInputDialog::TextInput); + d.setTextEchoMode(QLineEdit::Password); + d.setWindowTitle(tr("Password for Private Key")); + d.setLabelText(tr("It is recommended that you secure your private key\n" + "with a password, which you can enter below.")); + d.setOkButtonText(tr("Encrypt key file")); + d.setCancelButtonText(tr("Do not encrypt key file")); + int result = QDialog::Accepted; + QString password; + while (result == QDialog::Accepted && password.isEmpty()) { + result = d.exec(); + password = d.textValue(); + } + return result == QDialog::Accepted ? password : QString(); +} + } // namespace Utils diff --git a/src/libs/utils/ssh/sshkeygenerator.h b/src/libs/utils/ssh/sshkeygenerator.h index b670ce0b95d3758ac355d8bbfc49ed78cfd158cc..2f5a672dbd1b774e5420a26332beb4779547e19f 100644 --- a/src/libs/utils/ssh/sshkeygenerator.h +++ b/src/libs/utils/ssh/sshkeygenerator.h @@ -51,9 +51,11 @@ class QTCREATOR_UTILS_EXPORT SshKeyGenerator public: enum KeyType { Rsa, Dsa }; enum PrivateKeyFormat { Pkcs8, OpenSsl, Mixed }; + enum EncryptionMode { DoOfferEncryption, DoNotOfferEncryption }; // Only relevant for Pkcs8 format. SshKeyGenerator(); - bool generateKeys(KeyType type, PrivateKeyFormat format, int keySize); + bool generateKeys(KeyType type, PrivateKeyFormat format, int keySize, + EncryptionMode encryptionMode = DoOfferEncryption); QString error() const { return m_error; } QByteArray privateKey() const { return m_privateKey; } @@ -69,11 +71,13 @@ private: void generateOpenSslKeyStrings(const KeyPtr &key); void generateOpenSslPrivateKeyString(const KeyPtr &key); void generateOpenSslPublicKeyString(const KeyPtr &key); + QString getPassword() const; QString m_error; QByteArray m_publicKey; QByteArray m_privateKey; KeyType m_type; + EncryptionMode m_encryptionMode; }; } // namespace Utils