OSDN Git Service

689b6d48b17396b12abeadd1717ab03a04a54153
[qt-creator-jp/qt-creator-jp.git] / src / libs / utils / ssh / sshkeygenerator.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "sshkeygenerator.h"
35
36 #include "sshbotanconversions_p.h"
37 #include "sshcapabilities_p.h"
38 #include "sshpacket_p.h"
39
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>
48
49 #include <QtCore/QDateTime>
50
51 namespace Utils {
52
53 using namespace Botan;
54 using namespace Internal;
55
56 SshKeyGenerator::SshKeyGenerator()
57     : m_type(Rsa)
58     , m_format(OpenSsl)
59 {
60 }
61
62 bool SshKeyGenerator::generateKeys(KeyType type, PrivateKeyFormat format,
63     int keySize)
64 {
65     m_type = type;
66     m_format = format;
67
68     try {
69         AutoSeeded_RNG rng;
70         KeyPtr key;
71         if (m_type == Rsa)
72             key = KeyPtr(new RSA_PrivateKey(rng, keySize));
73         else
74             key = KeyPtr(new DSA_PrivateKey(rng, DL_Group(rng, DL_Group::DSA_Kosherizer,
75                 keySize)));
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());
80         return false;
81     }
82 }
83
84 bool SshKeyGenerator::generatePkcs8Keys(const KeyPtr &key)
85 {
86     generatePkcs8Key(key, false);
87     generatePkcs8Key(key, true);
88     return true;
89 }
90
91 void SshKeyGenerator::generatePkcs8Key(const KeyPtr &key, bool privateKey)
92 {
93     Pipe pipe;
94     pipe.start_msg();
95     QByteArray *keyData;
96     if (privateKey) {
97         PKCS8::encode(*key, pipe);
98         keyData = &m_privateKey;
99     } else {
100         X509::encode(*key, pipe);
101         keyData = &m_publicKey;
102     }
103     pipe.end_msg();
104     keyData->resize(pipe.remaining(pipe.message_count() - 1));
105     pipe.read(convertByteArray(*keyData), keyData->size(),
106         pipe.message_count() - 1);
107 }
108
109 bool SshKeyGenerator::generateOpenSslKeys(const KeyPtr &key)
110 {
111     QList<BigInt> publicParams;
112     QList<BigInt> allParams;
113     QByteArray keyId;
114     if (m_type == Rsa) {
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;
121     } else {
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;
128     }
129
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;
137
138     DER_Encoder encoder;
139     encoder.start_cons(SEQUENCE).encode (0U);
140     foreach (const BigInt &b, allParams)
141         encoder.encode(b);
142     encoder.end_cons();
143     const char * const label
144         = m_type == Rsa ? "RSA PRIVATE KEY" : "DSA PRIVATE KEY";
145     m_privateKey
146         = QByteArray(PEM_Code::encode (encoder.get_contents(), label).c_str());
147     return true;
148 }
149
150 } // namespace Utils