Merge lp:~libqtelegram-team/telegram-app/dev-401 into lp:telegram-app/dev

Proposed by Roberto Mier Escandon
Status: Merged
Approved by: Michał Karnicki
Approved revision: 139
Merged at revision: 140
Proposed branch: lp:~libqtelegram-team/telegram-app/dev-401
Merge into: lp:telegram-app/dev
Diff against target: 402 lines (+118/-114)
8 files modified
lib/core/constants.h (+7/-2)
lib/core/dcauth.cpp (+10/-89)
lib/core/dcprovider.cpp (+24/-19)
lib/core/settings.cpp (+2/-1)
lib/core/tlvalues.h (+1/-0)
lib/core/utils.cpp (+66/-3)
lib/core/utils.h (+7/-0)
qmlplugin/models/dialogsmodel.cpp (+1/-0)
To merge this branch: bzr merge lp:~libqtelegram-team/telegram-app/dev-401
Reviewer Review Type Date Requested Status
Michał Karnicki (community) Approve
Review via email: mp+245323@code.launchpad.net

Description of the change

Changed default DC from 1 to 4. This is a workaround to fix the problem. Not known exactly the cause of the bug but this fixes it
Also simplified the process of processResPQAnswer in DcAuth class. This way code is better to be figured out if needed to be changed (and avoids original telegram-cli code way)

To post a comment you must log in.
Revision history for this message
Michał Karnicki (karni) :
138. By Roberto Mier Escandon

fixed as review comments

139. By Roberto Mier Escandon

merged dev

Revision history for this message
Roberto Mier Escandon (rmescandon) wrote :

- Modified pToBigEndian way of compare to know the length
- removed not well formated blank space

NOTE: findDivider() method searches for a divider for pq value so that
p = divider
q = pq / divider

or reverse if p < q

Revision history for this message
Michał Karnicki (karni) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/core/constants.h'
--- lib/core/constants.h 2014-12-09 17:28:59 +0000
+++ lib/core/constants.h 2015-01-07 16:21:39 +0000
@@ -28,8 +28,6 @@
28#define ENCRYPT_BUFFER_INTS 1638428#define ENCRYPT_BUFFER_INTS 16384
29#define DECRYPT_BUFFER_INTS 1638429#define DECRYPT_BUFFER_INTS 16384
3030
31#define LIBQTELEGRAM_SERVER "173.240.5.1"
32#define LIBQTELEGRAM_SERVER_TEST "173.240.5.253"
33// These are the the original31// These are the the original
34#define LIBQTELEGRAM_APP_HASH "c68fd5e560aa84dd4b6ad6f489164790"32#define LIBQTELEGRAM_APP_HASH "c68fd5e560aa84dd4b6ad6f489164790"
35#define LIBQTELEGRAM_APP_ID 1243333#define LIBQTELEGRAM_APP_ID 12433
@@ -40,6 +38,13 @@
40//#define LIBQTELEGRAM_APP_HASH "4b7f87d44de5e0a22248597939c848ae"38//#define LIBQTELEGRAM_APP_HASH "4b7f87d44de5e0a22248597939c848ae"
41//#define LIBQTELEGRAM_APP_ID 1198639//#define LIBQTELEGRAM_APP_ID 11986
4240
41#define PRODUCTION_DEFAULT_DC_ID 4
42#define PRODUCTION_DEFAULT_DC_HOST "149.154.167.91"
43#define PRODUCTION_DEFAULT_DC_PORT 443
44#define TEST_DEFAULT_DC_ID 1
45#define TEST_DEFAULT_DC_HOST "173.240.5.253"
46#define TEST_DEFAULT_DC_PORT 80
47
43#define LIBQTELEGRAM_BUILD "001"48#define LIBQTELEGRAM_BUILD "001"
44#define LIBQTELEGRAM_VERSION "1.0.0-SNAPSHOT"49#define LIBQTELEGRAM_VERSION "1.0.0-SNAPSHOT"
4550
4651
=== modified file 'lib/core/dcauth.cpp'
--- lib/core/dcauth.cpp 2014-12-02 13:01:22 +0000
+++ lib/core/dcauth.cpp 2015-01-07 16:21:39 +0000
@@ -107,44 +107,9 @@
107107
108 uint p1 = 0, p2 = 0;108 uint p1 = 0, p2 = 0;
109109
110 qCDebug(TG_AUTH_DCAUTH) << what << "received";110 qCDebug(TG_AUTH_DCAUTH) << "received pq =" << what;
111111
112 qint32 it = 0;112 qint64 g = Utils::findDivider(what);
113 quint64 g = 0;
114 for (i = 0; i < 3 || it < 1000; i++) {
115 qint32 q = ((lrand48() & 15) + 17) % what;
116 quint64 x = (qint64)lrand48 () % (what - 1) + 1, y = x;
117 qint32 lim = 1 << (i + 18);
118 qint32 j;
119 for (j = 1; j < lim; j++) {
120 ++it;
121 quint64 a = x, b = x, c = q;
122 while (b) {
123 if (b & 1) {
124 c += a;
125 if (c >= what) {
126 c -= what;
127 }
128 }
129 a += a;
130 if (a >= what) {
131 a -= what;
132 }
133 b >>= 1;
134 }
135 x = c;
136 quint64 z = x < y ? what + x - y : x - y;
137 g = Utils::gcd (z, what);
138 if (g != 1) {
139 break;
140 }
141 if (!(j & (j - 1))) {
142 y = x;
143 }
144 }
145 if (g > 1 && g < what) break;
146 }
147
148 mAsserter.check(g > 1 && g < what);113 mAsserter.check(g > 1 && g < what);
149 p1 = g;114 p1 = g;
150 p2 = what / g;115 p2 = what / g;
@@ -152,7 +117,7 @@
152 uint t = p1; p1 = p2; p2 = t;117 uint t = p1; p1 = p2; p2 = t;
153 }118 }
154119
155 qCDebug(TG_AUTH_DCAUTH) << "p1=" << p1 << ", p2=" << p2 << ", " << it << "iterations";120 qCDebug(TG_AUTH_DCAUTH) << "p1 =" << p1 << ", p2 =" << p2;
156121
157 mAsserter.check(*(qint32 *) (from) == TL_Vector);122 mAsserter.check(*(qint32 *) (from) == TL_Vector);
158 qint32 fingerprints_num = *(qint32 *)(from + 4);123 qint32 fingerprints_num = *(qint32 *)(from + 4);
@@ -175,31 +140,11 @@
175 p.appendInt(TL_PQInnerData);140 p.appendInt(TL_PQInnerData);
176 p.appendCString(buffer + 57, clen);141 p.appendCString(buffer + 57, clen);
177142
178 if (p1 < 256) {143 QByteArray p1BigEndianBytes = Utils::pToBigEndian(p1);
179 clen = 1;144 p.appendBytes(p1BigEndianBytes);
180 } else if (p1 < 65536) {
181 clen = 2;
182 } else if (p1 < 16777216) {
183 clen = 3;
184 } else {
185 clen = 4;
186 }
187 p1 = __builtin_bswap32 (p1);
188 p.appendCString((char *)&p1 + 4 - clen, clen);
189 p1 = __builtin_bswap32 (p1);
190145
191 if (p2 < 256) {146 QByteArray p2BigEndianBytes = Utils::pToBigEndian(p2);
192 clen = 1;147 p.appendBytes(p2BigEndianBytes);
193 } else if (p2 < 65536) {
194 clen = 2;
195 } else if (p2 < 16777216) {
196 clen = 3;
197 } else {
198 clen = 4;
199 }
200 p2 = __builtin_bswap32 (p2);
201 p.appendCString((char *)&p2 + 4 - clen, clen);
202 p2 = __builtin_bswap32 (p2);
203148
204 p.appendInts((qint32 *) m_nonce, 4);149 p.appendInts((qint32 *) m_nonce, 4);
205 p.appendInts((qint32 *) m_serverNonce, 4);150 p.appendInts((qint32 *) m_serverNonce, 4);
@@ -215,32 +160,8 @@
215 p.appendInt(TL_ReqDHParams);160 p.appendInt(TL_ReqDHParams);
216 p.appendInts((qint32 *) m_nonce, 4);161 p.appendInts((qint32 *) m_nonce, 4);
217 p.appendInts((qint32 *) m_serverNonce, 4);162 p.appendInts((qint32 *) m_serverNonce, 4);
218163 p.appendBytes(p1BigEndianBytes);
219 if (p1 < 256) {164 p.appendBytes(p2BigEndianBytes);
220 clen = 1;
221 } else if (p1 < 65536) {
222 clen = 2;
223 } else if (p1 < 16777216) {
224 clen = 3;
225 } else {
226 clen = 4;
227 }
228 p1 = __builtin_bswap32 (p1);
229 p.appendCString((char *)&p1 + 4 - clen, clen);
230 p1 = __builtin_bswap32 (p1);
231 if (p2 < 256) {
232 clen = 1;
233 } else if (p2 < 65536) {
234 clen = 2;
235 } else if (p2 < 16777216) {
236 clen = 3;
237 } else {
238 clen = 4;
239 }
240 p2 = __builtin_bswap32 (p2);
241 p.appendCString((char *)&p2 + 4 - clen, clen);
242 p2 = __builtin_bswap32 (p2);
243
244 p.appendLong(Settings::getInstance()->pkFingerprint());165 p.appendLong(Settings::getInstance()->pkFingerprint());
245 p.appendCString((char *) encryptBuffer, l);166 p.appendCString((char *) encryptBuffer, l);
246167
247168
=== modified file 'lib/core/dcprovider.cpp'
--- lib/core/dcprovider.cpp 2014-11-18 17:13:16 +0000
+++ lib/core/dcprovider.cpp 2015-01-07 16:21:39 +0000
@@ -1,8 +1,5 @@
1/*1/*
2 * Copyright 2014 Canonical Ltd.2 * Copyright 2014 Canonical Ltd.
3 * Authors:
4 * Roberto Mier
5 * Tiago Herrmann
6 *3 *
7 * This program is free software; you can redistribute it and/or modify4 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by5 * it under the terms of the GNU General Public License as published by
@@ -87,46 +84,54 @@
87 */84 */
8885
89 // 1.- Get the settings dcs, fullfill m_dcs map and current dc num86 // 1.- Get the settings dcs, fullfill m_dcs map and current dc num
90 Settings *s = Settings::getInstance();87 Settings *settings = Settings::getInstance();
91 QList<DC *> dcsList = s->dcsList();88 QList<DC *> dcsList = settings->dcsList();
9289
93 Q_FOREACH (DC *dc, dcsList) {90 Q_FOREACH (DC *dc, dcsList) {
94 mDcs.insert(dc->id(), dc);91 mDcs.insert(dc->id(), dc);
95 }92 }
9693
94 qint32 defaultDcId = settings->testMode() ? TEST_DEFAULT_DC_ID : PRODUCTION_DEFAULT_DC_ID;
95
97 // 2.- connect to working DC96 // 2.- connect to working DC
98 if (s->workingDcNum() == 1) {97 if (settings->workingDcNum() == defaultDcId) {
99 // If dcId == 1, it's the default one, so call default host and port98 // If dcId == defaultDcId, it's the default one, so call default host and port
100 if (!mDcs.value(1, 0)) {99 if (!mDcs.value(defaultDcId, 0)) {
101 mDcs[1] = new DC(1);100 mDcs[defaultDcId] = new DC(defaultDcId);
102 }101 }
103 // if dc hasn't key created, create it102 // if dc hasn't key created, create it
104 if (mDcs[1]->state() < DC::authKeyCreated) {103 if (mDcs[defaultDcId]->state() < DC::authKeyCreated) {
104 QString defaultDcHost = PRODUCTION_DEFAULT_DC_HOST;
105 qint32 defaultDcPort = PRODUCTION_DEFAULT_DC_PORT;
106 if (settings->testMode()) {
107 defaultDcHost = TEST_DEFAULT_DC_HOST;
108 defaultDcPort = TEST_DEFAULT_DC_PORT;
109 }
105 // create a dc authenticator based in dc info110 // create a dc authenticator based in dc info
106 s->testMode() ? mDcs[1]->setHost(LIBQTELEGRAM_SERVER_TEST) : mDcs[1]->setHost(LIBQTELEGRAM_SERVER);111 mDcs[defaultDcId]->setHost(defaultDcHost);
107 mDcs[1]->setPort(443);112 mDcs[defaultDcId]->setPort(defaultDcPort);
108 DCAuth *dcAuth = new DCAuth(mDcs[1], this);113 DCAuth *dcAuth = new DCAuth(mDcs[defaultDcId], this);
109 mDcAuths.insert(1, dcAuth);114 mDcAuths.insert(defaultDcId, dcAuth);
110 connect(dcAuth, SIGNAL(fatalError()), this, SLOT(logOut()));115 connect(dcAuth, SIGNAL(fatalError()), this, SLOT(logOut()));
111 connect(dcAuth, SIGNAL(fatalError()), this, SIGNAL(fatalError()));116 connect(dcAuth, SIGNAL(fatalError()), this, SIGNAL(fatalError()));
112 connect(dcAuth, SIGNAL(dcReady(DC*)), this, SLOT(onDcReady(DC*)));117 connect(dcAuth, SIGNAL(dcReady(DC*)), this, SLOT(onDcReady(DC*)));
113 dcAuth->createAuthKey();118 dcAuth->createAuthKey();
114 } else {119 } else {
115 onDcReady(mDcs[1]);120 onDcReady(mDcs[defaultDcId]);
116 }121 }
117122
118 } else {123 } else {
119 // In any other case, the host and port have been retrieved from auth file settings and the DC object is already created124 // In any other case, the host and port have been retrieved from auth file settings and the DC object is already created
120 if (mDcs[s->workingDcNum()]->state() < DC::authKeyCreated) {125 if (mDcs[settings->workingDcNum()]->state() < DC::authKeyCreated) {
121 // create a dc authenticator based in dc info126 // create a dc authenticator based in dc info
122 DCAuth *dcAuth = new DCAuth(mDcs[s->workingDcNum()]);127 DCAuth *dcAuth = new DCAuth(mDcs[settings->workingDcNum()]);
123 mDcAuths.insert(s->workingDcNum(), dcAuth);128 mDcAuths.insert(settings->workingDcNum(), dcAuth);
124 connect(dcAuth, SIGNAL(fatalError()), this, SLOT(logOut()));129 connect(dcAuth, SIGNAL(fatalError()), this, SLOT(logOut()));
125 connect(dcAuth, SIGNAL(fatalError()), this, SIGNAL(fatalError()));130 connect(dcAuth, SIGNAL(fatalError()), this, SIGNAL(fatalError()));
126 connect(dcAuth, SIGNAL(dcReady(DC*)), this, SLOT(onDcReady(DC*)));131 connect(dcAuth, SIGNAL(dcReady(DC*)), this, SLOT(onDcReady(DC*)));
127 dcAuth->createAuthKey();132 dcAuth->createAuthKey();
128 } else {133 } else {
129 onDcReady(mDcs[s->workingDcNum()]);134 onDcReady(mDcs[settings->workingDcNum()]);
130 }135 }
131 }136 }
132}137}
133138
=== modified file 'lib/core/settings.cpp'
--- lib/core/settings.cpp 2014-11-18 17:13:16 +0000
+++ lib/core/settings.cpp 2015-01-07 16:21:39 +0000
@@ -144,7 +144,8 @@
144void Settings::readAuthFile() {144void Settings::readAuthFile() {
145 QSettings settings(m_authFilename, QSettings::IniFormat);145 QSettings settings(m_authFilename, QSettings::IniFormat);
146 Settings::getInstance()->testMode() ? settings.beginGroup(ST_TEST) : settings.beginGroup(ST_PRODUCTION);146 Settings::getInstance()->testMode() ? settings.beginGroup(ST_TEST) : settings.beginGroup(ST_PRODUCTION);
147 m_workingDcNum = settings.value(ST_WORKING_DC_NUM, 1).toInt();147 qint32 defaultDcId = m_testMode ? TEST_DEFAULT_DC_ID : PRODUCTION_DEFAULT_DC_ID;
148 m_workingDcNum = settings.value(ST_WORKING_DC_NUM, defaultDcId).toInt();
148 m_ourId = settings.value(ST_OUR_ID).toInt();149 m_ourId = settings.value(ST_OUR_ID).toInt();
149150
150 qCDebug(TG_INIT_SETTINGS) << "workingDcNum:" << m_workingDcNum;151 qCDebug(TG_INIT_SETTINGS) << "workingDcNum:" << m_workingDcNum;
151152
=== modified file 'lib/core/tlvalues.h'
--- lib/core/tlvalues.h 2014-11-18 17:13:16 +0000
+++ lib/core/tlvalues.h 2015-01-07 16:21:39 +0000
@@ -27,6 +27,7 @@
27 TL_ResPQ = 0x05162463,27 TL_ResPQ = 0x05162463,
28 TL_ReqDHParams = 0xd712e4be,28 TL_ReqDHParams = 0xd712e4be,
29 TL_PQInnerData = 0x83c95aec,29 TL_PQInnerData = 0x83c95aec,
30 TL_PQInnerDataTemp = 0x3c6a84d4,
30 TL_ServerDHParamsOK = 0xd0e8075c,31 TL_ServerDHParamsOK = 0xd0e8075c,
31 TL_ServerDHParamsFail = 0x79cb045d,32 TL_ServerDHParamsFail = 0x79cb045d,
32 TL_ServerDHInnerData = 0xb5890dba,33 TL_ServerDHInnerData = 0xb5890dba,
3334
=== modified file 'lib/core/utils.cpp'
--- lib/core/utils.cpp 2014-11-17 20:28:24 +0000
+++ lib/core/utils.cpp 2015-01-07 16:21:39 +0000
@@ -33,6 +33,11 @@
33#include <QtMultimedia/QMediaResource>33#include <QtMultimedia/QMediaResource>
34#include <QProcess>34#include <QProcess>
35#include <QStringList>35#include <QStringList>
36#include <QtCore>
37
38
39Q_LOGGING_CATEGORY(TG_UTILS, "tg.utils")
40
3641
37Utils::Utils(QObject *parent) :42Utils::Utils(QObject *parent) :
38 QObject(parent)43 QObject(parent)
@@ -157,16 +162,16 @@
157 RSA *pubKey = NULL;162 RSA *pubKey = NULL;
158 FILE *f = fopen (publicKeyName.toLocal8Bit().data(), "r");163 FILE *f = fopen (publicKeyName.toLocal8Bit().data(), "r");
159 if (f == NULL) {164 if (f == NULL) {
160 qWarning() << "Couldn't open public key file" << publicKeyName;165 qCWarning(TG_UTILS) << "Couldn't open public key file" << publicKeyName;
161 return NULL;166 return NULL;
162 }167 }
163 pubKey = PEM_read_RSAPublicKey (f, NULL, NULL, NULL);168 pubKey = PEM_read_RSAPublicKey (f, NULL, NULL, NULL);
164 fclose (f);169 fclose (f);
165 if (pubKey == NULL) {170 if (pubKey == NULL) {
166 qWarning() << "PEM_read_RSAPublicKey returns NULL";171 qCWarning(TG_UTILS) << "PEM_read_RSAPublicKey returns NULL";
167 return NULL;172 return NULL;
168 }173 }
169 qDebug() << "public key" << publicKeyName << "loaded successfully";174 qCDebug(TG_UTILS) << "public key" << publicKeyName << "loaded successfully";
170 return pubKey;175 return pubKey;
171}176}
172177
@@ -312,3 +317,61 @@
312317
313 return filteredChars;318 return filteredChars;
314}319}
320
321QByteArray Utils::pToBigEndian(quint32 p) {;
322 qint32 pLength;
323 if (p < (1 << 8)) {
324 pLength = 1;
325 } else if (p < (1 << 16)) {
326 pLength = 2;
327 } else if (p < (1 << 24)) {
328 pLength = 3;
329 } else {
330 pLength = 4;
331 }
332
333 uchar beP[4];
334 qToBigEndian(p, beP);
335
336 return QByteArray((const char *)beP, pLength);
337}
338
339qint64 Utils::findDivider(qint64 pq) {
340 qint32 it = 0;
341 quint64 g = 0;
342 for (int i = 0; i < 3 || it < 1000; i++) {
343 qint32 q = ((lrand48() & 15) + 17) % pq;
344 quint64 x = (qint64)lrand48 () % (pq - 1) + 1, y = x;
345 qint32 lim = 1 << (i + 18);
346 qint32 j;
347 for (j = 1; j < lim; j++) {
348 ++it;
349 quint64 a = x, b = x, c = q;
350 while (b) {
351 if (b & 1) {
352 c += a;
353 if (c >= pq) {
354 c -= pq;
355 }
356 }
357 a += a;
358 if (a >= pq) {
359 a -= pq;
360 }
361 b >>= 1;
362 }
363 x = c;
364 quint64 z = x < y ? pq + x - y : x - y;
365 g = Utils::gcd(z, pq);
366 if (g != 1) {
367 break;
368 }
369 if (!(j & (j - 1))) {
370 y = x;
371 }
372 }
373 if (g > 1 && g < pq) break;
374 }
375 qCDebug(TG_UTILS) << "got" << g << "divider after" << it << "iterations";
376 return g;
377}
315378
=== modified file 'lib/core/utils.h'
--- lib/core/utils.h 2014-11-17 11:31:25 +0000
+++ lib/core/utils.h 2015-01-07 16:21:39 +0000
@@ -35,6 +35,10 @@
35#include <QObject>35#include <QObject>
36#include <openssl/bn.h>36#include <openssl/bn.h>
37#include "constants.h"37#include "constants.h"
38#include <QLoggingCategory>
39
40Q_DECLARE_LOGGING_CATEGORY(TG_UTILS)
41
3842
39class Utils : public QObject43class Utils : public QObject
40{44{
@@ -76,6 +80,9 @@
76 static QString createThumbnail(const QString &resource);80 static QString createThumbnail(const QString &resource);
7781
78 static QString parsePhoneNumberDigits(const QString &phoneNumber);82 static QString parsePhoneNumberDigits(const QString &phoneNumber);
83
84 static QByteArray pToBigEndian(quint32 p);
85 static qint64 findDivider(qint64 pq);
79};86};
8087
81#endif // UTILS_H88#endif // UTILS_H
8289
=== modified file 'qmlplugin/models/dialogsmodel.cpp'
--- qmlplugin/models/dialogsmodel.cpp 2014-12-15 15:56:52 +0000
+++ qmlplugin/models/dialogsmodel.cpp 2015-01-07 16:21:39 +0000
@@ -268,6 +268,7 @@
268 }268 }
269269
270 qCDebug(TG_PLUGIN_LOGIC) << "fetch more:" << mCanFetchMore;270 qCDebug(TG_PLUGIN_LOGIC) << "fetch more:" << mCanFetchMore;
271
271 return mCanFetchMore;272 return mCanFetchMore;
272}273}
273274

Subscribers

People subscribed via source and target branches

to all changes: