1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "sshkeygenerator.h"
36 #include "sshbotanconversions_p.h"
37 #include "sshcapabilities_p.h"
38 #include "sshpacket_p.h"
40 #include <botan/auto_rng.h>
41 #include <botan/bigint.h>
42 #include <botan/der_enc.h>
43 #include <botan/dsa.h>
44 #include <botan/pem.h>
45 #include <botan/pkcs8.h>
46 #include <botan/rsa.h>
47 #include <botan/x509_key.h>
49 #include <QtCore/QDateTime>
53 using namespace Botan;
54 using namespace Internal;
56 SshKeyGenerator::SshKeyGenerator()
62 bool SshKeyGenerator::generateKeys(KeyType type, PrivateKeyFormat format,
72 key = KeyPtr(new RSA_PrivateKey(rng, keySize));
74 key = KeyPtr(new DSA_PrivateKey(rng, DL_Group(rng, DL_Group::DSA_Kosherizer,
76 return m_format == Pkcs8
77 ? generatePkcs8Keys(key) : generateOpenSslKeys(key);
78 } catch (Botan::Exception &e) {
79 m_error = tr("Error generating key: %1").arg(e.what());
84 bool SshKeyGenerator::generatePkcs8Keys(const KeyPtr &key)
86 generatePkcs8Key(key, false);
87 generatePkcs8Key(key, true);
91 void SshKeyGenerator::generatePkcs8Key(const KeyPtr &key, bool privateKey)
97 PKCS8::encode(*key, pipe);
98 keyData = &m_privateKey;
100 X509::encode(*key, pipe);
101 keyData = &m_publicKey;
104 keyData->resize(pipe.remaining(pipe.message_count() - 1));
105 pipe.read(convertByteArray(*keyData), keyData->size(),
106 pipe.message_count() - 1);
109 bool SshKeyGenerator::generateOpenSslKeys(const KeyPtr &key)
111 QList<BigInt> publicParams;
112 QList<BigInt> allParams;
115 const QSharedPointer<RSA_PrivateKey> rsaKey
116 = key.dynamicCast<RSA_PrivateKey>();
117 publicParams << rsaKey->get_e() << rsaKey->get_n();
118 allParams << rsaKey->get_n() << rsaKey->get_e() << rsaKey->get_d()
119 << rsaKey->get_p() << rsaKey->get_q();
120 keyId = SshCapabilities::PubKeyRsa;
122 const QSharedPointer<DSA_PrivateKey> dsaKey
123 = key.dynamicCast<DSA_PrivateKey>();
124 publicParams << dsaKey->group_p() << dsaKey->group_q()
125 << dsaKey->group_g() << dsaKey->get_y();
126 allParams << publicParams << dsaKey->get_x();
127 keyId = SshCapabilities::PubKeyDss;
130 QByteArray publicKeyBlob = AbstractSshPacket::encodeString(keyId);
131 foreach (const BigInt &b, publicParams)
132 publicKeyBlob += AbstractSshPacket::encodeMpInt(b);
133 publicKeyBlob = publicKeyBlob.toBase64();
134 const QByteArray id = "QtCreator/"
135 + QDateTime::currentDateTime().toString(Qt::ISODate).toUtf8();
136 m_publicKey = keyId + ' ' + publicKeyBlob + ' ' + id;
139 encoder.start_cons(SEQUENCE).encode (0U);
140 foreach (const BigInt &b, allParams)
143 const char * const label
144 = m_type == Rsa ? "RSA PRIVATE KEY" : "DSA PRIVATE KEY";
146 = QByteArray(PEM_Code::encode (encoder.get_contents(), label).c_str());