OSDN Git Service

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