2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#include "packer.h"
|
|
|
|
|
2016-08-14 18:05:10 +00:00
|
|
|
#include <QtCore/QtPlugin>
|
2014-07-09 06:10:55 +00:00
|
|
|
|
|
|
|
#ifdef Q_OS_MAC
|
2016-08-28 19:36:51 +00:00
|
|
|
//Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin)
|
2014-07-09 06:10:55 +00:00
|
|
|
#endif
|
|
|
|
|
2016-04-27 12:02:17 +00:00
|
|
|
bool AlphaChannel = false;
|
2015-12-03 18:16:34 +00:00
|
|
|
quint64 BetaVersion = 0;
|
2015-02-05 14:49:50 +00:00
|
|
|
|
2015-12-03 18:16:34 +00:00
|
|
|
const char *PublicKey = "\
|
2014-05-30 08:53:19 +00:00
|
|
|
-----BEGIN RSA PUBLIC KEY-----\n\
|
|
|
|
MIGJAoGBAMA4ViQrjkPZ9xj0lrer3r23JvxOnrtE8nI69XLGSr+sRERz9YnUptnU\n\
|
|
|
|
BZpkIfKaRcl6XzNJiN28cVwO1Ui5JSa814UAiDHzWUqCaXUiUEQ6NmNTneiGx2sQ\n\
|
|
|
|
+9PKKlb8mmr3BB9A45ZNwLT6G9AK3+qkZLHojeSA+m84/a6GP4svAgMBAAE=\n\
|
|
|
|
-----END RSA PUBLIC KEY-----\
|
|
|
|
";
|
|
|
|
|
2016-04-27 12:02:17 +00:00
|
|
|
const char *PublicAlphaKey = "\
|
2015-02-05 14:49:50 +00:00
|
|
|
-----BEGIN RSA PUBLIC KEY-----\n\
|
|
|
|
MIGJAoGBALWu9GGs0HED7KG7BM73CFZ6o0xufKBRQsdnq3lwA8nFQEvmdu+g/I1j\n\
|
|
|
|
0LQ+0IQO7GW4jAgzF/4+soPDb6uHQeNFrlVx1JS9DZGhhjZ5rf65yg11nTCIHZCG\n\
|
|
|
|
w/CVnbwQOw0g5GBwwFV3r0uTTvy44xx8XXxk+Qknu4eBCsmrAFNnAgMBAAE=\n\
|
|
|
|
-----END RSA PUBLIC KEY-----\
|
|
|
|
";
|
|
|
|
|
2015-12-03 18:16:34 +00:00
|
|
|
extern const char *PrivateKey;
|
2016-04-27 12:02:17 +00:00
|
|
|
extern const char *PrivateAlphaKey;
|
2015-02-05 14:49:50 +00:00
|
|
|
#include "../../../../TelegramPrivate/packer_private.h" // RSA PRIVATE KEYS for update signing
|
2015-12-03 18:16:34 +00:00
|
|
|
#include "../../../../TelegramPrivate/beta_private.h" // private key for beta version file generation
|
|
|
|
|
|
|
|
QString countBetaVersionSignature(quint64 version);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
// sha1 hash
|
|
|
|
typedef unsigned char uchar;
|
|
|
|
typedef unsigned int uint32;
|
|
|
|
typedef signed int int32;
|
2016-11-07 15:24:28 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
namespace{
|
|
|
|
|
2016-11-07 15:24:28 +00:00
|
|
|
inline uint32 sha1Shift(uint32 v, uint32 shift) {
|
|
|
|
return ((v << shift) | (v >> (32 - shift)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void sha1PartHash(uint32 *sha, uint32 *temp) {
|
|
|
|
uint32 a = sha[0], b = sha[1], c = sha[2], d = sha[3], e = sha[4], round = 0;
|
|
|
|
|
|
|
|
#define _shiftswap(f, v) { \
|
|
|
|
uint32 t = sha1Shift(a, 5) + (f) + e + v + temp[round]; \
|
|
|
|
e = d; \
|
|
|
|
d = c; \
|
|
|
|
c = sha1Shift(b, 30); \
|
|
|
|
b = a; \
|
|
|
|
a = t; \
|
|
|
|
++round; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define _shiftshiftswap(f, v) { \
|
|
|
|
temp[round] = sha1Shift((temp[round - 3] ^ temp[round - 8] ^ temp[round - 14] ^ temp[round - 16]), 1); \
|
|
|
|
_shiftswap(f, v) \
|
|
|
|
}
|
|
|
|
|
|
|
|
while (round < 16) _shiftswap((b & c) | (~b & d), 0x5a827999)
|
|
|
|
while (round < 20) _shiftshiftswap((b & c) | (~b & d), 0x5a827999)
|
|
|
|
while (round < 40) _shiftshiftswap(b ^ c ^ d, 0x6ed9eba1)
|
|
|
|
while (round < 60) _shiftshiftswap((b & c) | (b & d) | (c & d), 0x8f1bbcdc)
|
|
|
|
while (round < 80) _shiftshiftswap(b ^ c ^ d, 0xca62c1d6)
|
|
|
|
|
|
|
|
#undef _shiftshiftswap
|
|
|
|
#undef _shiftswap
|
|
|
|
|
|
|
|
sha[0] += a;
|
|
|
|
sha[1] += b;
|
|
|
|
sha[2] += c;
|
|
|
|
sha[3] += d;
|
|
|
|
sha[4] += e;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 15:24:28 +00:00
|
|
|
} // namespace
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
int32 *hashSha1(const void *data, uint32 len, void *dest) {
|
|
|
|
const uchar *buf = (const uchar *)data;
|
|
|
|
|
2016-11-07 15:24:28 +00:00
|
|
|
uint32 temp[80], block = 0, end;
|
|
|
|
uint32 sha[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0};
|
|
|
|
for (end = block + 64; block + 64 <= len; end = block + 64) {
|
|
|
|
for (uint32 i = 0; block < end; block += 4) {
|
|
|
|
temp[i++] = (uint32) buf[block + 3]
|
|
|
|
| (((uint32) buf[block + 2]) << 8)
|
|
|
|
| (((uint32) buf[block + 1]) << 16)
|
|
|
|
| (((uint32) buf[block]) << 24);
|
|
|
|
}
|
|
|
|
sha1PartHash(sha, temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
end = len - block;
|
2014-05-30 08:53:19 +00:00
|
|
|
memset(temp, 0, sizeof(uint32) * 16);
|
2016-11-07 15:24:28 +00:00
|
|
|
uint32 last = 0;
|
|
|
|
for (; last < end; ++last) {
|
|
|
|
temp[last >> 2] |= (uint32)buf[last + block] << ((3 - (last & 0x03)) << 3);
|
|
|
|
}
|
|
|
|
temp[last >> 2] |= 0x80 << ((3 - (last & 3)) << 3);
|
|
|
|
if (end >= 56) {
|
|
|
|
sha1PartHash(sha, temp);
|
2014-05-30 08:53:19 +00:00
|
|
|
memset(temp, 0, sizeof(uint32) * 16);
|
2016-11-07 15:24:28 +00:00
|
|
|
}
|
|
|
|
temp[15] = len << 3;
|
|
|
|
sha1PartHash(sha, temp);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
uchar *sha1To = (uchar*)dest;
|
|
|
|
|
2016-11-07 15:24:28 +00:00
|
|
|
for (int32 i = 19; i >= 0; --i) {
|
|
|
|
sha1To[i] = (sha[i >> 2] >> (((3 - i) & 0x03) << 3)) & 0xFF;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
return (int32*)sha1To;
|
|
|
|
}
|
|
|
|
|
2015-12-03 18:16:34 +00:00
|
|
|
QString BetaSignature;
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2014-06-25 07:25:55 +00:00
|
|
|
QString workDir;
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
QString remove;
|
|
|
|
int version = 0;
|
2016-09-03 16:44:45 +00:00
|
|
|
bool target32 = false;
|
2014-05-30 08:53:19 +00:00
|
|
|
QFileInfoList files;
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
if (string("-path") == argv[i] && i + 1 < argc) {
|
2014-06-25 07:25:55 +00:00
|
|
|
QString path = workDir + QString(argv[i + 1]);
|
2014-05-30 08:53:19 +00:00
|
|
|
QFileInfo info(path);
|
|
|
|
files.push_back(info);
|
|
|
|
if (remove.isEmpty()) remove = info.canonicalPath() + "/";
|
2016-09-03 16:44:45 +00:00
|
|
|
} else if (string("-target") == argv[i] && i + 1 < argc) {
|
|
|
|
target32 = (string("mac32") == argv[i + 1]);
|
2014-05-30 08:53:19 +00:00
|
|
|
} else if (string("-version") == argv[i] && i + 1 < argc) {
|
|
|
|
version = QString(argv[i + 1]).toInt();
|
2016-04-27 12:02:17 +00:00
|
|
|
} else if (string("-alpha") == argv[i]) {
|
|
|
|
AlphaChannel = true;
|
2015-12-03 18:16:34 +00:00
|
|
|
} else if (string("-beta") == argv[i] && i + 1 < argc) {
|
2015-12-04 14:29:57 +00:00
|
|
|
BetaVersion = QString(argv[i + 1]).toULongLong();
|
2015-12-03 19:22:40 +00:00
|
|
|
if (BetaVersion > version * 1000ULL && BetaVersion < (version + 1) * 1000ULL) {
|
2016-04-27 12:02:17 +00:00
|
|
|
AlphaChannel = false;
|
2015-12-03 18:16:34 +00:00
|
|
|
BetaSignature = countBetaVersionSignature(BetaVersion);
|
|
|
|
if (BetaSignature.isEmpty()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2015-12-03 19:22:40 +00:00
|
|
|
cout << "Bad -beta param value passed, should be for the same version: " << version << ", beta: " << BetaVersion << "\n";
|
2015-12-03 18:16:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 18:16:34 +00:00
|
|
|
if (files.isEmpty() || remove.isEmpty() || version <= 1016 || version > 999999999) {
|
2014-06-25 07:25:55 +00:00
|
|
|
#ifdef Q_OS_WIN
|
2014-05-30 08:53:19 +00:00
|
|
|
cout << "Usage: Packer.exe -path {file} -version {version} OR Packer.exe -path {dir} -version {version}\n";
|
2014-06-25 07:25:55 +00:00
|
|
|
#elif defined Q_OS_MAC
|
|
|
|
cout << "Usage: Packer.app -path {file} -version {version} OR Packer.app -path {dir} -version {version}\n";
|
2015-12-03 18:16:34 +00:00
|
|
|
#else
|
|
|
|
cout << "Usage: Packer -path {file} -version {version} OR Packer -path {dir} -version {version}\n";
|
2014-06-25 07:25:55 +00:00
|
|
|
#endif
|
|
|
|
return -1;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool hasDirs = true;
|
|
|
|
while (hasDirs) {
|
|
|
|
hasDirs = false;
|
|
|
|
for (QFileInfoList::iterator i = files.begin(); i != files.end(); ++i) {
|
|
|
|
QFileInfo info(*i);
|
|
|
|
QString fullPath = info.canonicalFilePath();
|
|
|
|
if (info.isDir()) {
|
|
|
|
hasDirs = true;
|
|
|
|
files.erase(i);
|
|
|
|
QDir d = QDir(info.absoluteFilePath());
|
|
|
|
QString fullDir = d.canonicalPath();
|
|
|
|
QStringList entries = d.entryList(QDir::Files | QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);
|
|
|
|
files.append(d.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot));
|
|
|
|
break;
|
|
|
|
} else if (!info.isReadable()) {
|
|
|
|
cout << "Can't read: " << info.absoluteFilePath().toUtf8().constData() << "\n";
|
|
|
|
return -1;
|
2014-06-25 07:25:55 +00:00
|
|
|
} else if (info.isHidden()) {
|
|
|
|
hasDirs = true;
|
|
|
|
files.erase(i);
|
|
|
|
break;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (QFileInfoList::iterator i = files.begin(); i != files.end(); ++i) {
|
|
|
|
QFileInfo info(*i);
|
2014-08-22 09:53:53 +00:00
|
|
|
if (!info.canonicalFilePath().startsWith(remove)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
cout << "Can't find '" << remove.toUtf8().constData() << "' in file '" << info.canonicalFilePath().toUtf8().constData() << "' :(\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray result;
|
|
|
|
{
|
|
|
|
QBuffer buffer(&result);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
QDataStream stream(&buffer);
|
|
|
|
stream.setVersion(QDataStream::Qt_5_1);
|
|
|
|
|
2015-12-03 18:16:34 +00:00
|
|
|
if (BetaVersion) {
|
|
|
|
stream << quint32(0x7FFFFFFF);
|
|
|
|
stream << quint64(BetaVersion);
|
|
|
|
} else {
|
|
|
|
stream << quint32(version);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
stream << quint32(files.size());
|
|
|
|
cout << "Found " << files.size() << " file" << (files.size() == 1 ? "" : "s") << "..\n";
|
|
|
|
for (QFileInfoList::iterator i = files.begin(); i != files.end(); ++i) {
|
|
|
|
QFileInfo info(*i);
|
|
|
|
QString fullName = info.canonicalFilePath();
|
|
|
|
QString name = fullName.mid(remove.length());
|
|
|
|
cout << name.toUtf8().constData() << " (" << info.size() << ")\n";
|
|
|
|
|
|
|
|
QFile f(fullName);
|
|
|
|
if (!f.open(QIODevice::ReadOnly)) {
|
|
|
|
cout << "Can't open '" << fullName.toUtf8().constData() << "' for read..\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
QByteArray inner = f.readAll();
|
|
|
|
stream << name << quint32(inner.size()) << inner;
|
2014-06-25 07:25:55 +00:00
|
|
|
#if defined Q_OS_MAC || defined Q_OS_LINUX
|
|
|
|
stream << (QFileInfo(fullName).isExecutable() ? true : false);
|
|
|
|
#endif
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
if (stream.status() != QDataStream::Ok) {
|
|
|
|
cout << "Stream status is bad: " << stream.status() << "\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 resultSize = result.size();
|
|
|
|
cout << "Compression start, size: " << resultSize << "\n";
|
|
|
|
|
|
|
|
QByteArray compressed, resultCheck;
|
2014-06-25 07:25:55 +00:00
|
|
|
#ifdef Q_OS_WIN // use Lzma SDK for win
|
2014-05-30 08:53:19 +00:00
|
|
|
const int32 hSigLen = 128, hShaLen = 20, hPropsLen = LZMA_PROPS_SIZE, hOriginalSizeLen = sizeof(int32), hSize = hSigLen + hShaLen + hPropsLen + hOriginalSizeLen; // header
|
|
|
|
|
|
|
|
compressed.resize(hSize + resultSize + 1024 * 1024); // rsa signature + sha1 + lzma props + max compressed size
|
|
|
|
|
|
|
|
size_t compressedLen = compressed.size() - hSize;
|
|
|
|
size_t outPropsSize = LZMA_PROPS_SIZE;
|
2015-02-18 16:52:04 +00:00
|
|
|
uchar *_dest = (uchar*)(compressed.data() + hSize);
|
|
|
|
size_t *_destLen = &compressedLen;
|
|
|
|
const uchar *_src = (const uchar*)(result.constData());
|
|
|
|
size_t _srcLen = result.size();
|
|
|
|
uchar *_outProps = (uchar*)(compressed.data() + hSigLen + hShaLen);
|
|
|
|
int res = LzmaCompress(_dest, _destLen, _src, _srcLen, _outProps, &outPropsSize, 9, 64 * 1024 * 1024, 4, 0, 2, 273, 2);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (res != SZ_OK) {
|
|
|
|
cout << "Error in compression: " << res << "\n";
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-25 07:25:55 +00:00
|
|
|
compressed.resize(int(hSize + compressedLen));
|
2014-05-30 08:53:19 +00:00
|
|
|
memcpy(compressed.data() + hSigLen + hShaLen + hPropsLen, &resultSize, hOriginalSizeLen);
|
|
|
|
|
|
|
|
cout << "Compressed to size: " << compressedLen << "\n";
|
|
|
|
|
|
|
|
cout << "Checking uncompressed..\n";
|
|
|
|
|
|
|
|
int32 resultCheckLen;
|
|
|
|
memcpy(&resultCheckLen, compressed.constData() + hSigLen + hShaLen + hPropsLen, hOriginalSizeLen);
|
|
|
|
if (resultCheckLen <= 0 || resultCheckLen > 1024 * 1024 * 1024) {
|
|
|
|
cout << "Bad result len: " << resultCheckLen << "\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
resultCheck.resize(resultCheckLen);
|
|
|
|
|
|
|
|
size_t resultLen = resultCheck.size();
|
|
|
|
SizeT srcLen = compressedLen;
|
|
|
|
int uncompressRes = LzmaUncompress((uchar*)resultCheck.data(), &resultLen, (const uchar*)(compressed.constData() + hSize), &srcLen, (const uchar*)(compressed.constData() + hSigLen + hShaLen), LZMA_PROPS_SIZE);
|
|
|
|
if (uncompressRes != SZ_OK) {
|
|
|
|
cout << "Uncompress failed: " << uncompressRes << "\n";
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-25 07:25:55 +00:00
|
|
|
if (resultLen != size_t(result.size())) {
|
2014-05-30 08:53:19 +00:00
|
|
|
cout << "Uncompress bad size: " << resultLen << ", was: " << result.size() << "\n";
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-25 07:25:55 +00:00
|
|
|
#else // use liblzma for others
|
|
|
|
const int32 hSigLen = 128, hShaLen = 20, hPropsLen = 0, hOriginalSizeLen = sizeof(int32), hSize = hSigLen + hShaLen + hOriginalSizeLen; // header
|
|
|
|
|
|
|
|
compressed.resize(hSize + resultSize + 1024 * 1024); // rsa signature + sha1 + lzma props + max compressed size
|
|
|
|
|
|
|
|
size_t compressedLen = compressed.size() - hSize;
|
|
|
|
|
|
|
|
lzma_stream stream = LZMA_STREAM_INIT;
|
|
|
|
|
|
|
|
int preset = 9 | LZMA_PRESET_EXTREME;
|
|
|
|
lzma_ret ret = lzma_easy_encoder(&stream, preset, LZMA_CHECK_CRC64);
|
|
|
|
if (ret != LZMA_OK) {
|
|
|
|
const char *msg;
|
|
|
|
switch (ret) {
|
|
|
|
case LZMA_MEM_ERROR: msg = "Memory allocation failed"; break;
|
|
|
|
case LZMA_OPTIONS_ERROR: msg = "Specified preset is not supported"; break;
|
|
|
|
case LZMA_UNSUPPORTED_CHECK: msg = "Specified integrity check is not supported"; break;
|
|
|
|
default: msg = "Unknown error, possibly a bug"; break;
|
|
|
|
}
|
|
|
|
cout << "Error initializing the encoder: " << msg << " (error code " << ret << ")\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.avail_in = resultSize;
|
|
|
|
stream.next_in = (uint8_t*)result.constData();
|
|
|
|
stream.avail_out = compressedLen;
|
|
|
|
stream.next_out = (uint8_t*)(compressed.data() + hSize);
|
|
|
|
|
|
|
|
lzma_ret res = lzma_code(&stream, LZMA_FINISH);
|
|
|
|
compressedLen -= stream.avail_out;
|
|
|
|
lzma_end(&stream);
|
|
|
|
if (res != LZMA_OK && res != LZMA_STREAM_END) {
|
|
|
|
const char *msg;
|
|
|
|
switch (res) {
|
|
|
|
case LZMA_MEM_ERROR: msg = "Memory allocation failed"; break;
|
|
|
|
case LZMA_DATA_ERROR: msg = "File size limits exceeded"; break;
|
|
|
|
default: msg = "Unknown error, possibly a bug"; break;
|
|
|
|
}
|
|
|
|
cout << "Error in compression: " << msg << " (error code " << res << ")\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
compressed.resize(int(hSize + compressedLen));
|
|
|
|
memcpy(compressed.data() + hSigLen + hShaLen, &resultSize, hOriginalSizeLen);
|
|
|
|
|
|
|
|
cout << "Compressed to size: " << compressedLen << "\n";
|
|
|
|
|
|
|
|
cout << "Checking uncompressed..\n";
|
|
|
|
|
|
|
|
int32 resultCheckLen;
|
|
|
|
memcpy(&resultCheckLen, compressed.constData() + hSigLen + hShaLen, hOriginalSizeLen);
|
|
|
|
if (resultCheckLen <= 0 || resultCheckLen > 1024 * 1024 * 1024) {
|
|
|
|
cout << "Bad result len: " << resultCheckLen << "\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
resultCheck.resize(resultCheckLen);
|
|
|
|
|
|
|
|
size_t resultLen = resultCheck.size();
|
|
|
|
|
|
|
|
stream = LZMA_STREAM_INIT;
|
|
|
|
|
|
|
|
ret = lzma_stream_decoder(&stream, UINT64_MAX, LZMA_CONCATENATED);
|
|
|
|
if (ret != LZMA_OK) {
|
|
|
|
const char *msg;
|
|
|
|
switch (ret) {
|
|
|
|
case LZMA_MEM_ERROR: msg = "Memory allocation failed"; break;
|
|
|
|
case LZMA_OPTIONS_ERROR: msg = "Specified preset is not supported"; break;
|
|
|
|
case LZMA_UNSUPPORTED_CHECK: msg = "Specified integrity check is not supported"; break;
|
|
|
|
default: msg = "Unknown error, possibly a bug"; break;
|
|
|
|
}
|
|
|
|
cout << "Error initializing the decoder: " << msg << " (error code " << ret << ")\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.avail_in = compressedLen;
|
|
|
|
stream.next_in = (uint8_t*)(compressed.constData() + hSize);
|
|
|
|
stream.avail_out = resultLen;
|
|
|
|
stream.next_out = (uint8_t*)resultCheck.data();
|
|
|
|
|
|
|
|
res = lzma_code(&stream, LZMA_FINISH);
|
|
|
|
if (stream.avail_in) {
|
|
|
|
cout << "Error in decompression, " << stream.avail_in << " bytes left in _in of " << compressedLen << " whole.\n";
|
|
|
|
return -1;
|
|
|
|
} else if (stream.avail_out) {
|
|
|
|
cout << "Error in decompression, " << stream.avail_out << " bytes free left in _out of " << resultLen << " whole.\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
lzma_end(&stream);
|
|
|
|
if (res != LZMA_OK && res != LZMA_STREAM_END) {
|
|
|
|
const char *msg;
|
|
|
|
switch (res) {
|
|
|
|
case LZMA_MEM_ERROR: msg = "Memory allocation failed"; break;
|
|
|
|
case LZMA_FORMAT_ERROR: msg = "The input data is not in the .xz format"; break;
|
|
|
|
case LZMA_OPTIONS_ERROR: msg = "Unsupported compression options"; break;
|
|
|
|
case LZMA_DATA_ERROR: msg = "Compressed file is corrupt"; break;
|
|
|
|
case LZMA_BUF_ERROR: msg = "Compressed data is truncated or otherwise corrupt"; break;
|
|
|
|
default: msg = "Unknown error, possibly a bug"; break;
|
|
|
|
}
|
|
|
|
cout << "Error in decompression: " << msg << " (error code " << res << ")\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2014-05-30 08:53:19 +00:00
|
|
|
if (memcmp(result.constData(), resultCheck.constData(), resultLen)) {
|
|
|
|
cout << "Data differ :(\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/**/
|
|
|
|
result = resultCheck = QByteArray();
|
|
|
|
|
|
|
|
cout << "Counting SHA1 hash..\n";
|
|
|
|
|
|
|
|
uchar sha1Buffer[20];
|
2014-06-25 07:25:55 +00:00
|
|
|
memcpy(compressed.data() + hSigLen, hashSha1(compressed.constData() + hSigLen + hShaLen, uint32(compressedLen + hPropsLen + hOriginalSizeLen), sha1Buffer), hShaLen); // count sha1
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
uint32 siglen = 0;
|
|
|
|
|
|
|
|
cout << "Signing..\n";
|
2016-04-27 12:02:17 +00:00
|
|
|
RSA *prKey = PEM_read_bio_RSAPrivateKey(BIO_new_mem_buf(const_cast<char*>((AlphaChannel || BetaVersion) ? PrivateAlphaKey : PrivateKey), -1), 0, 0, 0);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (!prKey) {
|
|
|
|
cout << "Could not read RSA private key!\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (RSA_size(prKey) != hSigLen) {
|
|
|
|
cout << "Bad private key, size: " << RSA_size(prKey) << "\n";
|
2015-12-03 18:16:34 +00:00
|
|
|
RSA_free(prKey);
|
2014-05-30 08:53:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (RSA_sign(NID_sha1, (const uchar*)(compressed.constData() + hSigLen), hShaLen, (uchar*)(compressed.data()), &siglen, prKey) != 1) { // count signature
|
|
|
|
cout << "Signing failed!\n";
|
2015-12-03 18:16:34 +00:00
|
|
|
RSA_free(prKey);
|
2014-05-30 08:53:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
RSA_free(prKey);
|
|
|
|
|
|
|
|
if (siglen != hSigLen) {
|
|
|
|
cout << "Bad signature length: " << siglen << "\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "Checking signature..\n";
|
2016-04-27 12:02:17 +00:00
|
|
|
RSA *pbKey = PEM_read_bio_RSAPublicKey(BIO_new_mem_buf(const_cast<char*>((AlphaChannel || BetaVersion) ? PublicAlphaKey : PublicKey), -1), 0, 0, 0);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (!pbKey) {
|
|
|
|
cout << "Could not read RSA public key!\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (RSA_verify(NID_sha1, (const uchar*)(compressed.constData() + hSigLen), hShaLen, (const uchar*)(compressed.constData()), siglen, pbKey) != 1) { // verify signature
|
|
|
|
RSA_free(pbKey);
|
|
|
|
cout << "Signature verification failed!\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cout << "Signature verified!\n";
|
|
|
|
RSA_free(pbKey);
|
2014-06-25 07:25:55 +00:00
|
|
|
#ifdef Q_OS_WIN
|
2015-12-03 18:16:34 +00:00
|
|
|
QString outName(QString("tupdate%1").arg(BetaVersion ? BetaVersion : version));
|
2014-06-25 07:25:55 +00:00
|
|
|
#elif defined Q_OS_MAC
|
2016-09-03 16:44:45 +00:00
|
|
|
QString outName((target32 ? QString("tmac32upd%1") : QString("tmacupd%1")).arg(BetaVersion ? BetaVersion : version));
|
2014-07-17 09:42:06 +00:00
|
|
|
#elif defined Q_OS_LINUX32
|
2016-09-03 16:44:45 +00:00
|
|
|
QString outName(QString("tlinux32upd%1").arg(BetaVersion ? BetaVersion : version));
|
2014-07-17 09:42:06 +00:00
|
|
|
#elif defined Q_OS_LINUX64
|
2016-09-03 16:44:45 +00:00
|
|
|
QString outName(QString("tlinuxupd%1").arg(BetaVersion ? BetaVersion : version));
|
2014-07-18 10:37:34 +00:00
|
|
|
#else
|
2014-06-25 07:25:55 +00:00
|
|
|
#error Unknown platform!
|
|
|
|
#endif
|
2015-12-03 18:16:34 +00:00
|
|
|
if (BetaVersion) {
|
|
|
|
outName += "_" + BetaSignature;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
QFile out(outName);
|
|
|
|
if (!out.open(QIODevice::WriteOnly)) {
|
|
|
|
cout << "Can't open '" << outName.toUtf8().constData() << "' for write..\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
out.write(compressed);
|
|
|
|
out.close();
|
|
|
|
|
2015-12-03 18:16:34 +00:00
|
|
|
if (BetaVersion) {
|
|
|
|
QString keyName(QString("tbeta_%1_key").arg(BetaVersion));
|
|
|
|
QFile key(keyName);
|
|
|
|
if (!key.open(QIODevice::WriteOnly)) {
|
|
|
|
cout << "Can't open '" << keyName.toUtf8().constData() << "' for write..\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
key.write(BetaSignature.toUtf8());
|
|
|
|
key.close();
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
cout << "Update file '" << outName.toUtf8().constData() << "' written successfully!\n";
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-12-03 18:16:34 +00:00
|
|
|
|
|
|
|
QString countBetaVersionSignature(quint64 version) { // duplicated in autoupdate.cpp
|
|
|
|
QByteArray cBetaPrivateKey(BetaPrivateKey);
|
|
|
|
if (cBetaPrivateKey.isEmpty()) {
|
|
|
|
cout << "Error: Trying to count beta version signature without beta private key!\n";
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray signedData = (QLatin1String("TelegramBeta_") + QString::number(version, 16).toLower()).toUtf8();
|
|
|
|
|
|
|
|
static const int32 shaSize = 20, keySize = 128;
|
|
|
|
|
|
|
|
uchar sha1Buffer[shaSize];
|
|
|
|
hashSha1(signedData.constData(), signedData.size(), sha1Buffer); // count sha1
|
|
|
|
|
|
|
|
uint32 siglen = 0;
|
|
|
|
|
|
|
|
RSA *prKey = PEM_read_bio_RSAPrivateKey(BIO_new_mem_buf(const_cast<char*>(cBetaPrivateKey.constData()), -1), 0, 0, 0);
|
|
|
|
if (!prKey) {
|
|
|
|
cout << "Error: Could not read beta private key!\n";
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
if (RSA_size(prKey) != keySize) {
|
|
|
|
cout << "Error: Bad beta private key size: " << RSA_size(prKey) << "\n";
|
|
|
|
RSA_free(prKey);
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
QByteArray signature;
|
|
|
|
signature.resize(keySize);
|
|
|
|
if (RSA_sign(NID_sha1, (const uchar*)(sha1Buffer), shaSize, (uchar*)(signature.data()), &siglen, prKey) != 1) { // count signature
|
|
|
|
cout << "Error: Counting beta version signature failed!\n";
|
|
|
|
RSA_free(prKey);
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
RSA_free(prKey);
|
|
|
|
|
|
|
|
if (siglen != keySize) {
|
|
|
|
cout << "Error: Bad beta version signature length: " << siglen << "\n";
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
signature = signature.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
|
|
|
|
signature = signature.replace('-', '8').replace('_', 'B');
|
|
|
|
return QString::fromUtf8(signature.mid(19, 32));
|
|
|
|
}
|