1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "sshpacket_p.h"
35 #include "sshcapabilities_p.h"
36 #include "sshcryptofacility_p.h"
37 #include "sshexception_p.h"
38 #include "sshpacketparser_p.h"
40 #include <QtCore/QDebug>
47 const quint32 AbstractSshPacket::PaddingLengthOffset = 4;
48 const quint32 AbstractSshPacket::PayloadOffset = PaddingLengthOffset + 1;
49 const quint32 AbstractSshPacket::TypeOffset = PayloadOffset;
50 const quint32 AbstractSshPacket::MinPaddingLength = 4;
54 void printByteArray(const QByteArray &data)
56 #ifdef CREATOR_SSH_DEBUG
57 for (int i = 0; i < data.count(); ++i)
58 qDebug() << std::hex << (static_cast<unsigned int>(data[i]) & 0xff) << " ";
63 } // anonymous namespace
66 AbstractSshPacket::AbstractSshPacket() : m_length(0) { }
67 AbstractSshPacket::~AbstractSshPacket() {}
69 bool AbstractSshPacket::isComplete() const
71 if (currentDataSize() < minPacketSize())
73 Q_ASSERT(4 + length() + macLength() >= currentDataSize());
74 return 4 + length() + macLength() == currentDataSize();
77 void AbstractSshPacket::clear()
83 SshPacketType AbstractSshPacket::type() const
85 Q_ASSERT(isComplete());
86 return static_cast<SshPacketType>(m_data.at(TypeOffset));
89 AbstractSshPacket::Payload AbstractSshPacket::payLoad() const
92 p.data = m_data.constData() + PayloadOffset;
93 p.size = length() - paddingLength() - 1;
97 void AbstractSshPacket::printRawBytes() const
99 printByteArray(m_data);
102 QByteArray AbstractSshPacket::encodeString(const QByteArray &string)
107 setLengthField(data);
111 QByteArray AbstractSshPacket::encodeMpInt(const Botan::BigInt &number)
113 if (number.is_zero())
114 return QByteArray(4, 0);
116 int stringLength = number.bytes();
117 const bool positiveAndMsbSet = number.sign() == Botan::BigInt::Positive
118 && (number.byte_at(stringLength - 1) & 0x80);
119 if (positiveAndMsbSet)
122 data.resize(4 + stringLength);
124 if (positiveAndMsbSet)
126 number.binary_encode(reinterpret_cast<Botan::byte *>(data.data()) + pos);
127 setLengthField(data);
131 int AbstractSshPacket::paddingLength() const
133 return m_data[PaddingLengthOffset];
136 quint32 AbstractSshPacket::length() const
138 //Q_ASSERT(currentDataSize() >= minPacketSize());
144 void AbstractSshPacket::calculateLength() const
146 m_length = SshPacketParser::asUint32(m_data, static_cast<quint32>(0));
149 QByteArray AbstractSshPacket::generateMac(const SshAbstractCryptoFacility &crypt,
152 const quint32 seqNrBe = qToBigEndian(seqNr);
153 QByteArray data(reinterpret_cast<const char *>(&seqNrBe), sizeof seqNrBe);
154 data += QByteArray(m_data.constData(), length() + 4);
155 return crypt.generateMac(data, data.size());
158 quint32 AbstractSshPacket::minPacketSize() const
160 return qMax<quint32>(cipherBlockSize(), 16) + macLength();
163 void AbstractSshPacket::setLengthField(QByteArray &data)
165 const quint32 length = qToBigEndian(data.size() - 4);
166 data.replace(0, 4, reinterpret_cast<const char *>(&length), 4);
169 } // namespace Internal