2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-07-12 09:33:22 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
2010-07-12 09:33:22 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-07-12 09:33:22 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2010-07-12 09:33:22 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-07-12 09:33:22 +02:00
|
|
|
|
|
|
|
|
#include "sshkeyexchange_p.h"
|
|
|
|
|
|
|
|
|
|
#include "sshbotanconversions_p.h"
|
|
|
|
|
#include "sshcapabilities_p.h"
|
|
|
|
|
#include "sshsendfacility_p.h"
|
|
|
|
|
#include "sshexception_p.h"
|
|
|
|
|
#include "sshincomingpacket_p.h"
|
|
|
|
|
|
|
|
|
|
#include <botan/botan.h>
|
|
|
|
|
|
2011-04-28 10:57:17 +02:00
|
|
|
#ifdef CREATOR_SSH_DEBUG
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#endif
|
2010-07-12 09:33:22 +02:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
using namespace Botan;
|
|
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
namespace QSsh {
|
2010-07-12 09:33:22 +02:00
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// For debugging
|
|
|
|
|
void printNameList(const char *listName, const SshNameList &list)
|
|
|
|
|
{
|
|
|
|
|
#ifdef CREATOR_SSH_DEBUG
|
|
|
|
|
qDebug("%s:", listName);
|
|
|
|
|
foreach (const QByteArray &name, list.names)
|
|
|
|
|
qDebug("%s", name.constData());
|
|
|
|
|
#else
|
|
|
|
|
Q_UNUSED(listName);
|
|
|
|
|
Q_UNUSED(list);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2011-04-28 10:57:17 +02:00
|
|
|
|
|
|
|
|
#ifdef CREATOR_SSH_DEBUG
|
|
|
|
|
void printData(const char *name, const QByteArray &data)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << std::hex;
|
|
|
|
|
qDebug("The client thinks the %s has length %d and is:", name, data.count());
|
|
|
|
|
for (int i = 0; i < data.count(); ++i)
|
|
|
|
|
std::cerr << (static_cast<unsigned int>(data.at(i)) & 0xff) << ' ';
|
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-07-12 09:33:22 +02:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
|
SshKeyExchange::SshKeyExchange(SshSendFacility &sendFacility)
|
|
|
|
|
: m_sendFacility(sendFacility)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SshKeyExchange::~SshKeyExchange() {}
|
|
|
|
|
|
|
|
|
|
void SshKeyExchange::sendKexInitPacket(const QByteArray &serverId)
|
|
|
|
|
{
|
|
|
|
|
m_serverId = serverId;
|
2011-04-28 10:57:17 +02:00
|
|
|
m_clientKexInitPayload = m_sendFacility.sendKeyExchangeInitPacket();
|
2010-07-12 09:33:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SshKeyExchange::sendDhInitPacket(const SshIncomingPacket &serverKexInit)
|
|
|
|
|
{
|
|
|
|
|
#ifdef CREATOR_SSH_DEBUG
|
|
|
|
|
qDebug("server requests key exchange");
|
|
|
|
|
#endif
|
|
|
|
|
serverKexInit.printRawBytes();
|
|
|
|
|
SshKeyExchangeInit kexInitParams
|
|
|
|
|
= serverKexInit.extractKeyExchangeInitData();
|
|
|
|
|
|
|
|
|
|
printNameList("Key Algorithms", kexInitParams.keyAlgorithms);
|
|
|
|
|
printNameList("Server Host Key Algorithms", kexInitParams.serverHostKeyAlgorithms);
|
|
|
|
|
printNameList("Encryption algorithms client to server", kexInitParams.encryptionAlgorithmsClientToServer);
|
|
|
|
|
printNameList("Encryption algorithms server to client", kexInitParams.encryptionAlgorithmsServerToClient);
|
|
|
|
|
printNameList("MAC algorithms client to server", kexInitParams.macAlgorithmsClientToServer);
|
|
|
|
|
printNameList("MAC algorithms server to client", kexInitParams.macAlgorithmsServerToClient);
|
|
|
|
|
printNameList("Compression algorithms client to server", kexInitParams.compressionAlgorithmsClientToServer);
|
|
|
|
|
printNameList("Compression algorithms client to server", kexInitParams.compressionAlgorithmsClientToServer);
|
|
|
|
|
printNameList("Languages client to server", kexInitParams.languagesClientToServer);
|
|
|
|
|
printNameList("Languages server to client", kexInitParams.languagesServerToClient);
|
|
|
|
|
#ifdef CREATOR_SSH_DEBUG
|
|
|
|
|
qDebug("First packet follows: %d", kexInitParams.firstKexPacketFollows);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
const QByteArray &keyAlgo
|
|
|
|
|
= SshCapabilities::findBestMatch(SshCapabilities::KeyExchangeMethods,
|
|
|
|
|
kexInitParams.keyAlgorithms.names);
|
|
|
|
|
m_serverHostKeyAlgo
|
|
|
|
|
= SshCapabilities::findBestMatch(SshCapabilities::PublicKeyAlgorithms,
|
|
|
|
|
kexInitParams.serverHostKeyAlgorithms.names);
|
|
|
|
|
m_encryptionAlgo
|
|
|
|
|
= SshCapabilities::findBestMatch(SshCapabilities::EncryptionAlgorithms,
|
|
|
|
|
kexInitParams.encryptionAlgorithmsClientToServer.names);
|
|
|
|
|
m_decryptionAlgo
|
|
|
|
|
= SshCapabilities::findBestMatch(SshCapabilities::EncryptionAlgorithms,
|
|
|
|
|
kexInitParams.encryptionAlgorithmsServerToClient.names);
|
|
|
|
|
m_c2sHMacAlgo
|
|
|
|
|
= SshCapabilities::findBestMatch(SshCapabilities::MacAlgorithms,
|
|
|
|
|
kexInitParams.macAlgorithmsClientToServer.names);
|
|
|
|
|
m_s2cHMacAlgo
|
|
|
|
|
= SshCapabilities::findBestMatch(SshCapabilities::MacAlgorithms,
|
|
|
|
|
kexInitParams.macAlgorithmsServerToClient.names);
|
|
|
|
|
SshCapabilities::findBestMatch(SshCapabilities::CompressionAlgorithms,
|
|
|
|
|
kexInitParams.compressionAlgorithmsClientToServer.names);
|
|
|
|
|
SshCapabilities::findBestMatch(SshCapabilities::CompressionAlgorithms,
|
|
|
|
|
kexInitParams.compressionAlgorithmsServerToClient.names);
|
|
|
|
|
|
|
|
|
|
AutoSeeded_RNG rng;
|
2012-09-28 15:54:10 +02:00
|
|
|
m_dhKey = createDhPrivateKey(rng, DL_Group(botanKeyExchangeAlgoName(keyAlgo)));
|
2010-07-12 09:33:22 +02:00
|
|
|
|
2011-04-28 10:57:17 +02:00
|
|
|
m_serverKexInitPayload = serverKexInit.payLoad();
|
2010-07-12 09:33:22 +02:00
|
|
|
m_sendFacility.sendKeyDhInitPacket(m_dhKey->get_y());
|
|
|
|
|
return kexInitParams.firstKexPacketFollows;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshKeyExchange::sendNewKeysPacket(const SshIncomingPacket &dhReply,
|
|
|
|
|
const QByteArray &clientId)
|
|
|
|
|
{
|
|
|
|
|
const SshKeyExchangeReply &reply
|
|
|
|
|
= dhReply.extractKeyExchangeReply(m_serverHostKeyAlgo);
|
|
|
|
|
if (reply.f <= 0 || reply.f >= m_dhKey->group_p()) {
|
|
|
|
|
throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_KEY_EXCHANGE_FAILED,
|
|
|
|
|
"Server sent invalid f.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray concatenatedData = AbstractSshPacket::encodeString(clientId);
|
|
|
|
|
concatenatedData += AbstractSshPacket::encodeString(m_serverId);
|
|
|
|
|
concatenatedData += AbstractSshPacket::encodeString(m_clientKexInitPayload);
|
|
|
|
|
concatenatedData += AbstractSshPacket::encodeString(m_serverKexInitPayload);
|
|
|
|
|
concatenatedData += reply.k_s;
|
|
|
|
|
concatenatedData += AbstractSshPacket::encodeMpInt(m_dhKey->get_y());
|
|
|
|
|
concatenatedData += AbstractSshPacket::encodeMpInt(reply.f);
|
2012-06-26 09:31:01 +02:00
|
|
|
const BigInt k = power_mod(reply.f, m_dhKey->get_x(), m_dhKey->get_domain().get_p());
|
|
|
|
|
m_k = AbstractSshPacket::encodeMpInt(k);
|
2010-07-12 09:33:22 +02:00
|
|
|
concatenatedData += m_k;
|
|
|
|
|
|
|
|
|
|
m_hash.reset(get_hash(botanSha1Name()));
|
|
|
|
|
const SecureVector<byte> &hashResult
|
|
|
|
|
= m_hash->process(convertByteArray(concatenatedData),
|
|
|
|
|
concatenatedData.size());
|
|
|
|
|
m_h = convertByteArray(hashResult);
|
|
|
|
|
|
2011-04-28 10:57:17 +02:00
|
|
|
#ifdef CREATOR_SSH_DEBUG
|
|
|
|
|
printData("Client Id", AbstractSshPacket::encodeString(clientId));
|
|
|
|
|
printData("Server Id", AbstractSshPacket::encodeString(m_serverId));
|
|
|
|
|
printData("Client Payload", AbstractSshPacket::encodeString(m_clientKexInitPayload));
|
|
|
|
|
printData("Server payload", AbstractSshPacket::encodeString(m_serverKexInitPayload));
|
|
|
|
|
printData("K_S", reply.k_s);
|
|
|
|
|
printData("y", AbstractSshPacket::encodeMpInt(m_dhKey->get_y()));
|
|
|
|
|
printData("f", AbstractSshPacket::encodeMpInt(reply.f));
|
|
|
|
|
printData("K", m_k);
|
|
|
|
|
printData("Concatenated data", concatenatedData);
|
|
|
|
|
printData("H", m_h);
|
|
|
|
|
#endif // CREATOR_SSH_DEBUG
|
|
|
|
|
|
2012-09-28 15:54:10 +02:00
|
|
|
QSharedPointer<Public_Key> publicKey;
|
|
|
|
|
QByteArray algorithm;
|
2010-07-12 09:33:22 +02:00
|
|
|
if (m_serverHostKeyAlgo == SshCapabilities::PubKeyDss) {
|
|
|
|
|
const DL_Group group(reply.parameters.at(0), reply.parameters.at(1),
|
|
|
|
|
reply.parameters.at(2));
|
2012-09-28 15:54:10 +02:00
|
|
|
publicKey = createDsaPublicKey(group, reply.parameters.at(3));
|
|
|
|
|
algorithm = SshCapabilities::PubKeyDss;
|
2010-07-12 09:33:22 +02:00
|
|
|
} else if (m_serverHostKeyAlgo == SshCapabilities::PubKeyRsa) {
|
2012-09-28 15:54:10 +02:00
|
|
|
publicKey = createRsaPublicKey(reply.parameters.at(1), reply.parameters.at(0));
|
|
|
|
|
algorithm = SshCapabilities::PubKeyRsa;
|
2010-07-12 09:33:22 +02:00
|
|
|
} else {
|
|
|
|
|
Q_ASSERT(!"Impossible: Neither DSS nor RSA!");
|
|
|
|
|
}
|
|
|
|
|
const byte * const botanH = convertByteArray(m_h);
|
|
|
|
|
const Botan::byte * const botanSig
|
|
|
|
|
= convertByteArray(reply.signatureBlob);
|
2012-09-28 15:54:10 +02:00
|
|
|
if (!PK_Verifier(*publicKey, botanEmsaAlgoName(algorithm)).verify_message(botanH, m_h.size(),
|
|
|
|
|
botanSig, reply.signatureBlob.size())) {
|
2010-07-12 09:33:22 +02:00
|
|
|
throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_KEY_EXCHANGE_FAILED,
|
|
|
|
|
"Invalid signature in SSH_MSG_KEXDH_REPLY packet.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_sendFacility.sendNewKeysPacket();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
2012-05-18 10:49:35 +02:00
|
|
|
} // namespace QSsh
|