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 "sshpacket_p.h"
36 #include "sshcapabilities_p.h"
37 #include "sshcryptofacility_p.h"
38 #include "sshexception_p.h"
39 #include "sshpacketparser_p.h"
41 #include <QtCore/QDebug>
48 const quint32 AbstractSshPacket::PaddingLengthOffset = 4;
49 const quint32 AbstractSshPacket::PayloadOffset = PaddingLengthOffset + 1;
50 const quint32 AbstractSshPacket::TypeOffset = PayloadOffset;
51 const quint32 AbstractSshPacket::MinPaddingLength = 4;
55 void printByteArray(const QByteArray &data)
57 #ifdef CREATOR_SSH_DEBUG
58 for (int i = 0; i < data.count(); ++i)
59 qDebug() << std::hex << (static_cast<unsigned int>(data[i]) & 0xff) << " ";
64 } // anonymous namespace
67 AbstractSshPacket::AbstractSshPacket() : m_length(0) { }
68 AbstractSshPacket::~AbstractSshPacket() {}
70 bool AbstractSshPacket::isComplete() const
72 if (currentDataSize() < minPacketSize())
74 Q_ASSERT(4 + length() + macLength() >= currentDataSize());
75 return 4 + length() + macLength() == currentDataSize();
78 void AbstractSshPacket::clear()
84 SshPacketType AbstractSshPacket::type() const
86 Q_ASSERT(isComplete());
87 return static_cast<SshPacketType>(m_data.at(TypeOffset));
90 AbstractSshPacket::Payload AbstractSshPacket::payLoad() const
93 p.data = m_data.constData() + PayloadOffset;
94 p.size = length() - paddingLength() - 1;
98 void AbstractSshPacket::printRawBytes() const
100 printByteArray(m_data);
103 QByteArray AbstractSshPacket::encodeString(const QByteArray &string)
108 setLengthField(data);
112 QByteArray AbstractSshPacket::encodeMpInt(const Botan::BigInt &number)
114 if (number.is_zero())
115 return QByteArray(4, 0);
117 int stringLength = number.bytes();
118 const bool positiveAndMsbSet = number.sign() == Botan::BigInt::Positive
119 && (number.byte_at(stringLength - 1) & 0x80);
120 if (positiveAndMsbSet)
123 data.resize(4 + stringLength);
125 if (positiveAndMsbSet)
127 number.binary_encode(reinterpret_cast<Botan::byte *>(data.data()) + pos);
128 setLengthField(data);
132 int AbstractSshPacket::paddingLength() const
134 return m_data[PaddingLengthOffset];
137 quint32 AbstractSshPacket::length() const
139 //Q_ASSERT(currentDataSize() >= minPacketSize());
145 void AbstractSshPacket::calculateLength() const
147 m_length = SshPacketParser::asUint32(m_data, static_cast<quint32>(0));
150 QByteArray AbstractSshPacket::generateMac(const SshAbstractCryptoFacility &crypt,
153 const quint32 seqNrBe = qToBigEndian(seqNr);
154 QByteArray data(reinterpret_cast<const char *>(&seqNrBe), sizeof seqNrBe);
155 data += QByteArray(m_data.constData(), length() + 4);
156 return crypt.generateMac(data, data.size());
159 quint32 AbstractSshPacket::minPacketSize() const
161 return qMax<quint32>(cipherBlockSize(), 16) + macLength();
164 void AbstractSshPacket::setLengthField(QByteArray &data)
166 const quint32 length = qToBigEndian(data.size() - 4);
167 data.replace(0, 4, reinterpret_cast<const char *>(&length), 4);
170 } // namespace Internal