OSDN Git Service

0ec644786ec0cd0f9c20326031d6ef1f36059318
[qt-creator-jp/qt-creator-jp.git] / src / libs / utils / ssh / sshpacket.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 "sshpacket_p.h"
35
36 #include "sshcapabilities_p.h"
37 #include "sshcryptofacility_p.h"
38 #include "sshexception_p.h"
39 #include "sshpacketparser_p.h"
40
41 #include <QtCore/QDebug>
42
43 #include <cctype>
44
45 namespace Utils {
46 namespace Internal {
47
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;
52
53 namespace {
54
55     void printByteArray(const QByteArray &data)
56     {
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) << " ";
60 #else
61         Q_UNUSED(data);
62 #endif
63     }
64 } // anonymous namespace
65
66
67 AbstractSshPacket::AbstractSshPacket() : m_length(0) { }
68 AbstractSshPacket::~AbstractSshPacket() {}
69
70 bool AbstractSshPacket::isComplete() const
71 {
72     if (currentDataSize() < minPacketSize())
73         return false;
74     Q_ASSERT(4 + length() + macLength() >= currentDataSize());
75     return 4 + length() + macLength() == currentDataSize();
76 }
77
78 void AbstractSshPacket::clear()
79 {
80     m_data.clear();
81     m_length = 0;
82 }
83
84 SshPacketType AbstractSshPacket::type() const
85 {
86     Q_ASSERT(isComplete());
87     return static_cast<SshPacketType>(m_data.at(TypeOffset));
88 }
89
90 AbstractSshPacket::Payload AbstractSshPacket::payLoad() const
91 {
92     Payload p;
93     p.data = m_data.constData() + PayloadOffset;
94     p.size = length() - paddingLength() - 1;
95     return p;
96 }
97
98 void AbstractSshPacket::printRawBytes() const
99 {
100     printByteArray(m_data);
101 }
102
103 QByteArray AbstractSshPacket::encodeString(const QByteArray &string)
104 {
105     QByteArray data;
106     data.resize(4);
107     data += string;
108     setLengthField(data);
109     return data;
110 }
111
112 QByteArray AbstractSshPacket::encodeMpInt(const Botan::BigInt &number)
113 {
114     if (number.is_zero())
115         return QByteArray(4, 0);
116
117     int stringLength = number.bytes();
118     const bool positiveAndMsbSet = number.sign() == Botan::BigInt::Positive
119                                    && (number.byte_at(stringLength - 1) & 0x80);
120     if (positiveAndMsbSet)
121         ++stringLength;
122     QByteArray data;
123     data.resize(4 + stringLength);
124     int pos = 4;
125     if (positiveAndMsbSet)
126         data[pos++] = '\0';
127     number.binary_encode(reinterpret_cast<Botan::byte *>(data.data()) + pos);
128     setLengthField(data);
129     return data;
130 }
131
132 int AbstractSshPacket::paddingLength() const
133 {
134     return m_data[PaddingLengthOffset];
135 }
136
137 quint32 AbstractSshPacket::length() const
138 {
139     //Q_ASSERT(currentDataSize() >= minPacketSize());
140     if (m_length == 0)
141         calculateLength();
142     return m_length;
143 }
144
145 void AbstractSshPacket::calculateLength() const
146 {
147     m_length = SshPacketParser::asUint32(m_data, static_cast<quint32>(0));
148 }
149
150 QByteArray AbstractSshPacket::generateMac(const SshAbstractCryptoFacility &crypt,
151     quint32 seqNr) const
152 {
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());
157 }
158
159 quint32 AbstractSshPacket::minPacketSize() const
160 {
161     return qMax<quint32>(cipherBlockSize(), 16) + macLength();
162 }
163
164 void AbstractSshPacket::setLengthField(QByteArray &data)
165 {
166     const quint32 length = qToBigEndian(data.size() - 4);
167     data.replace(0, 4, reinterpret_cast<const char *>(&length), 4);
168 }
169
170 } // namespace Internal
171 } // namespace Utils