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
1=== modified file 'lib/core/constants.h'
2--- lib/core/constants.h 2014-12-09 17:28:59 +0000
3+++ lib/core/constants.h 2015-01-07 16:21:39 +0000
4@@ -28,8 +28,6 @@
5 #define ENCRYPT_BUFFER_INTS 16384
6 #define DECRYPT_BUFFER_INTS 16384
7
8-#define LIBQTELEGRAM_SERVER "173.240.5.1"
9-#define LIBQTELEGRAM_SERVER_TEST "173.240.5.253"
10 // These are the the original
11 #define LIBQTELEGRAM_APP_HASH "c68fd5e560aa84dd4b6ad6f489164790"
12 #define LIBQTELEGRAM_APP_ID 12433
13@@ -40,6 +38,13 @@
14 //#define LIBQTELEGRAM_APP_HASH "4b7f87d44de5e0a22248597939c848ae"
15 //#define LIBQTELEGRAM_APP_ID 11986
16
17+#define PRODUCTION_DEFAULT_DC_ID 4
18+#define PRODUCTION_DEFAULT_DC_HOST "149.154.167.91"
19+#define PRODUCTION_DEFAULT_DC_PORT 443
20+#define TEST_DEFAULT_DC_ID 1
21+#define TEST_DEFAULT_DC_HOST "173.240.5.253"
22+#define TEST_DEFAULT_DC_PORT 80
23+
24 #define LIBQTELEGRAM_BUILD "001"
25 #define LIBQTELEGRAM_VERSION "1.0.0-SNAPSHOT"
26
27
28=== modified file 'lib/core/dcauth.cpp'
29--- lib/core/dcauth.cpp 2014-12-02 13:01:22 +0000
30+++ lib/core/dcauth.cpp 2015-01-07 16:21:39 +0000
31@@ -107,44 +107,9 @@
32
33 uint p1 = 0, p2 = 0;
34
35- qCDebug(TG_AUTH_DCAUTH) << what << "received";
36-
37- qint32 it = 0;
38- quint64 g = 0;
39- for (i = 0; i < 3 || it < 1000; i++) {
40- qint32 q = ((lrand48() & 15) + 17) % what;
41- quint64 x = (qint64)lrand48 () % (what - 1) + 1, y = x;
42- qint32 lim = 1 << (i + 18);
43- qint32 j;
44- for (j = 1; j < lim; j++) {
45- ++it;
46- quint64 a = x, b = x, c = q;
47- while (b) {
48- if (b & 1) {
49- c += a;
50- if (c >= what) {
51- c -= what;
52- }
53- }
54- a += a;
55- if (a >= what) {
56- a -= what;
57- }
58- b >>= 1;
59- }
60- x = c;
61- quint64 z = x < y ? what + x - y : x - y;
62- g = Utils::gcd (z, what);
63- if (g != 1) {
64- break;
65- }
66- if (!(j & (j - 1))) {
67- y = x;
68- }
69- }
70- if (g > 1 && g < what) break;
71- }
72-
73+ qCDebug(TG_AUTH_DCAUTH) << "received pq =" << what;
74+
75+ qint64 g = Utils::findDivider(what);
76 mAsserter.check(g > 1 && g < what);
77 p1 = g;
78 p2 = what / g;
79@@ -152,7 +117,7 @@
80 uint t = p1; p1 = p2; p2 = t;
81 }
82
83- qCDebug(TG_AUTH_DCAUTH) << "p1=" << p1 << ", p2=" << p2 << ", " << it << "iterations";
84+ qCDebug(TG_AUTH_DCAUTH) << "p1 =" << p1 << ", p2 =" << p2;
85
86 mAsserter.check(*(qint32 *) (from) == TL_Vector);
87 qint32 fingerprints_num = *(qint32 *)(from + 4);
88@@ -175,31 +140,11 @@
89 p.appendInt(TL_PQInnerData);
90 p.appendCString(buffer + 57, clen);
91
92- if (p1 < 256) {
93- clen = 1;
94- } else if (p1 < 65536) {
95- clen = 2;
96- } else if (p1 < 16777216) {
97- clen = 3;
98- } else {
99- clen = 4;
100- }
101- p1 = __builtin_bswap32 (p1);
102- p.appendCString((char *)&p1 + 4 - clen, clen);
103- p1 = __builtin_bswap32 (p1);
104+ QByteArray p1BigEndianBytes = Utils::pToBigEndian(p1);
105+ p.appendBytes(p1BigEndianBytes);
106
107- if (p2 < 256) {
108- clen = 1;
109- } else if (p2 < 65536) {
110- clen = 2;
111- } else if (p2 < 16777216) {
112- clen = 3;
113- } else {
114- clen = 4;
115- }
116- p2 = __builtin_bswap32 (p2);
117- p.appendCString((char *)&p2 + 4 - clen, clen);
118- p2 = __builtin_bswap32 (p2);
119+ QByteArray p2BigEndianBytes = Utils::pToBigEndian(p2);
120+ p.appendBytes(p2BigEndianBytes);
121
122 p.appendInts((qint32 *) m_nonce, 4);
123 p.appendInts((qint32 *) m_serverNonce, 4);
124@@ -215,32 +160,8 @@
125 p.appendInt(TL_ReqDHParams);
126 p.appendInts((qint32 *) m_nonce, 4);
127 p.appendInts((qint32 *) m_serverNonce, 4);
128-
129- if (p1 < 256) {
130- clen = 1;
131- } else if (p1 < 65536) {
132- clen = 2;
133- } else if (p1 < 16777216) {
134- clen = 3;
135- } else {
136- clen = 4;
137- }
138- p1 = __builtin_bswap32 (p1);
139- p.appendCString((char *)&p1 + 4 - clen, clen);
140- p1 = __builtin_bswap32 (p1);
141- if (p2 < 256) {
142- clen = 1;
143- } else if (p2 < 65536) {
144- clen = 2;
145- } else if (p2 < 16777216) {
146- clen = 3;
147- } else {
148- clen = 4;
149- }
150- p2 = __builtin_bswap32 (p2);
151- p.appendCString((char *)&p2 + 4 - clen, clen);
152- p2 = __builtin_bswap32 (p2);
153-
154+ p.appendBytes(p1BigEndianBytes);
155+ p.appendBytes(p2BigEndianBytes);
156 p.appendLong(Settings::getInstance()->pkFingerprint());
157 p.appendCString((char *) encryptBuffer, l);
158
159
160=== modified file 'lib/core/dcprovider.cpp'
161--- lib/core/dcprovider.cpp 2014-11-18 17:13:16 +0000
162+++ lib/core/dcprovider.cpp 2015-01-07 16:21:39 +0000
163@@ -1,8 +1,5 @@
164 /*
165 * Copyright 2014 Canonical Ltd.
166- * Authors:
167- * Roberto Mier
168- * Tiago Herrmann
169 *
170 * This program is free software; you can redistribute it and/or modify
171 * it under the terms of the GNU General Public License as published by
172@@ -87,46 +84,54 @@
173 */
174
175 // 1.- Get the settings dcs, fullfill m_dcs map and current dc num
176- Settings *s = Settings::getInstance();
177- QList<DC *> dcsList = s->dcsList();
178+ Settings *settings = Settings::getInstance();
179+ QList<DC *> dcsList = settings->dcsList();
180
181 Q_FOREACH (DC *dc, dcsList) {
182 mDcs.insert(dc->id(), dc);
183 }
184
185+ qint32 defaultDcId = settings->testMode() ? TEST_DEFAULT_DC_ID : PRODUCTION_DEFAULT_DC_ID;
186+
187 // 2.- connect to working DC
188- if (s->workingDcNum() == 1) {
189- // If dcId == 1, it's the default one, so call default host and port
190- if (!mDcs.value(1, 0)) {
191- mDcs[1] = new DC(1);
192+ if (settings->workingDcNum() == defaultDcId) {
193+ // If dcId == defaultDcId, it's the default one, so call default host and port
194+ if (!mDcs.value(defaultDcId, 0)) {
195+ mDcs[defaultDcId] = new DC(defaultDcId);
196 }
197 // if dc hasn't key created, create it
198- if (mDcs[1]->state() < DC::authKeyCreated) {
199+ if (mDcs[defaultDcId]->state() < DC::authKeyCreated) {
200+ QString defaultDcHost = PRODUCTION_DEFAULT_DC_HOST;
201+ qint32 defaultDcPort = PRODUCTION_DEFAULT_DC_PORT;
202+ if (settings->testMode()) {
203+ defaultDcHost = TEST_DEFAULT_DC_HOST;
204+ defaultDcPort = TEST_DEFAULT_DC_PORT;
205+ }
206 // create a dc authenticator based in dc info
207- s->testMode() ? mDcs[1]->setHost(LIBQTELEGRAM_SERVER_TEST) : mDcs[1]->setHost(LIBQTELEGRAM_SERVER);
208- mDcs[1]->setPort(443);
209- DCAuth *dcAuth = new DCAuth(mDcs[1], this);
210- mDcAuths.insert(1, dcAuth);
211+ mDcs[defaultDcId]->setHost(defaultDcHost);
212+ mDcs[defaultDcId]->setPort(defaultDcPort);
213+ DCAuth *dcAuth = new DCAuth(mDcs[defaultDcId], this);
214+ mDcAuths.insert(defaultDcId, dcAuth);
215 connect(dcAuth, SIGNAL(fatalError()), this, SLOT(logOut()));
216 connect(dcAuth, SIGNAL(fatalError()), this, SIGNAL(fatalError()));
217 connect(dcAuth, SIGNAL(dcReady(DC*)), this, SLOT(onDcReady(DC*)));
218 dcAuth->createAuthKey();
219 } else {
220- onDcReady(mDcs[1]);
221+ onDcReady(mDcs[defaultDcId]);
222 }
223
224 } else {
225 // In any other case, the host and port have been retrieved from auth file settings and the DC object is already created
226- if (mDcs[s->workingDcNum()]->state() < DC::authKeyCreated) {
227+ if (mDcs[settings->workingDcNum()]->state() < DC::authKeyCreated) {
228 // create a dc authenticator based in dc info
229- DCAuth *dcAuth = new DCAuth(mDcs[s->workingDcNum()]);
230- mDcAuths.insert(s->workingDcNum(), dcAuth);
231+ DCAuth *dcAuth = new DCAuth(mDcs[settings->workingDcNum()]);
232+ mDcAuths.insert(settings->workingDcNum(), dcAuth);
233 connect(dcAuth, SIGNAL(fatalError()), this, SLOT(logOut()));
234 connect(dcAuth, SIGNAL(fatalError()), this, SIGNAL(fatalError()));
235 connect(dcAuth, SIGNAL(dcReady(DC*)), this, SLOT(onDcReady(DC*)));
236 dcAuth->createAuthKey();
237 } else {
238- onDcReady(mDcs[s->workingDcNum()]);
239+ onDcReady(mDcs[settings->workingDcNum()]);
240 }
241 }
242 }
243
244=== modified file 'lib/core/settings.cpp'
245--- lib/core/settings.cpp 2014-11-18 17:13:16 +0000
246+++ lib/core/settings.cpp 2015-01-07 16:21:39 +0000
247@@ -144,7 +144,8 @@
248 void Settings::readAuthFile() {
249 QSettings settings(m_authFilename, QSettings::IniFormat);
250 Settings::getInstance()->testMode() ? settings.beginGroup(ST_TEST) : settings.beginGroup(ST_PRODUCTION);
251- m_workingDcNum = settings.value(ST_WORKING_DC_NUM, 1).toInt();
252+ qint32 defaultDcId = m_testMode ? TEST_DEFAULT_DC_ID : PRODUCTION_DEFAULT_DC_ID;
253+ m_workingDcNum = settings.value(ST_WORKING_DC_NUM, defaultDcId).toInt();
254 m_ourId = settings.value(ST_OUR_ID).toInt();
255
256 qCDebug(TG_INIT_SETTINGS) << "workingDcNum:" << m_workingDcNum;
257
258=== modified file 'lib/core/tlvalues.h'
259--- lib/core/tlvalues.h 2014-11-18 17:13:16 +0000
260+++ lib/core/tlvalues.h 2015-01-07 16:21:39 +0000
261@@ -27,6 +27,7 @@
262 TL_ResPQ = 0x05162463,
263 TL_ReqDHParams = 0xd712e4be,
264 TL_PQInnerData = 0x83c95aec,
265+ TL_PQInnerDataTemp = 0x3c6a84d4,
266 TL_ServerDHParamsOK = 0xd0e8075c,
267 TL_ServerDHParamsFail = 0x79cb045d,
268 TL_ServerDHInnerData = 0xb5890dba,
269
270=== modified file 'lib/core/utils.cpp'
271--- lib/core/utils.cpp 2014-11-17 20:28:24 +0000
272+++ lib/core/utils.cpp 2015-01-07 16:21:39 +0000
273@@ -33,6 +33,11 @@
274 #include <QtMultimedia/QMediaResource>
275 #include <QProcess>
276 #include <QStringList>
277+#include <QtCore>
278+
279+
280+Q_LOGGING_CATEGORY(TG_UTILS, "tg.utils")
281+
282
283 Utils::Utils(QObject *parent) :
284 QObject(parent)
285@@ -157,16 +162,16 @@
286 RSA *pubKey = NULL;
287 FILE *f = fopen (publicKeyName.toLocal8Bit().data(), "r");
288 if (f == NULL) {
289- qWarning() << "Couldn't open public key file" << publicKeyName;
290+ qCWarning(TG_UTILS) << "Couldn't open public key file" << publicKeyName;
291 return NULL;
292 }
293 pubKey = PEM_read_RSAPublicKey (f, NULL, NULL, NULL);
294 fclose (f);
295 if (pubKey == NULL) {
296- qWarning() << "PEM_read_RSAPublicKey returns NULL";
297+ qCWarning(TG_UTILS) << "PEM_read_RSAPublicKey returns NULL";
298 return NULL;
299 }
300- qDebug() << "public key" << publicKeyName << "loaded successfully";
301+ qCDebug(TG_UTILS) << "public key" << publicKeyName << "loaded successfully";
302 return pubKey;
303 }
304
305@@ -312,3 +317,61 @@
306
307 return filteredChars;
308 }
309+
310+QByteArray Utils::pToBigEndian(quint32 p) {;
311+ qint32 pLength;
312+ if (p < (1 << 8)) {
313+ pLength = 1;
314+ } else if (p < (1 << 16)) {
315+ pLength = 2;
316+ } else if (p < (1 << 24)) {
317+ pLength = 3;
318+ } else {
319+ pLength = 4;
320+ }
321+
322+ uchar beP[4];
323+ qToBigEndian(p, beP);
324+
325+ return QByteArray((const char *)beP, pLength);
326+}
327+
328+qint64 Utils::findDivider(qint64 pq) {
329+ qint32 it = 0;
330+ quint64 g = 0;
331+ for (int i = 0; i < 3 || it < 1000; i++) {
332+ qint32 q = ((lrand48() & 15) + 17) % pq;
333+ quint64 x = (qint64)lrand48 () % (pq - 1) + 1, y = x;
334+ qint32 lim = 1 << (i + 18);
335+ qint32 j;
336+ for (j = 1; j < lim; j++) {
337+ ++it;
338+ quint64 a = x, b = x, c = q;
339+ while (b) {
340+ if (b & 1) {
341+ c += a;
342+ if (c >= pq) {
343+ c -= pq;
344+ }
345+ }
346+ a += a;
347+ if (a >= pq) {
348+ a -= pq;
349+ }
350+ b >>= 1;
351+ }
352+ x = c;
353+ quint64 z = x < y ? pq + x - y : x - y;
354+ g = Utils::gcd(z, pq);
355+ if (g != 1) {
356+ break;
357+ }
358+ if (!(j & (j - 1))) {
359+ y = x;
360+ }
361+ }
362+ if (g > 1 && g < pq) break;
363+ }
364+ qCDebug(TG_UTILS) << "got" << g << "divider after" << it << "iterations";
365+ return g;
366+}
367
368=== modified file 'lib/core/utils.h'
369--- lib/core/utils.h 2014-11-17 11:31:25 +0000
370+++ lib/core/utils.h 2015-01-07 16:21:39 +0000
371@@ -35,6 +35,10 @@
372 #include <QObject>
373 #include <openssl/bn.h>
374 #include "constants.h"
375+#include <QLoggingCategory>
376+
377+Q_DECLARE_LOGGING_CATEGORY(TG_UTILS)
378+
379
380 class Utils : public QObject
381 {
382@@ -76,6 +80,9 @@
383 static QString createThumbnail(const QString &resource);
384
385 static QString parsePhoneNumberDigits(const QString &phoneNumber);
386+
387+ static QByteArray pToBigEndian(quint32 p);
388+ static qint64 findDivider(qint64 pq);
389 };
390
391 #endif // UTILS_H
392
393=== modified file 'qmlplugin/models/dialogsmodel.cpp'
394--- qmlplugin/models/dialogsmodel.cpp 2014-12-15 15:56:52 +0000
395+++ qmlplugin/models/dialogsmodel.cpp 2015-01-07 16:21:39 +0000
396@@ -268,6 +268,7 @@
397 }
398
399 qCDebug(TG_PLUGIN_LOGIC) << "fetch more:" << mCanFetchMore;
400+
401 return mCanFetchMore;
402 }
403

Subscribers

People subscribed via source and target branches

to all changes: