| 1 | // Copyright (C) 2014 Sacha Refshauge |
| 2 | |
| 3 | // This program is free software: you can redistribute it and/or modify |
| 4 | // it under the terms of the GNU General Public License as published by |
| 5 | // the Free Software Foundation, version 3.0. |
| 6 | |
| 7 | // This program is distributed in the hope that it will be useful, |
| 8 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | // GNU General Public License 3.0 for more details. |
| 11 | |
| 12 | // A copy of the GPL 3.0 should have been included with the program. |
| 13 | // If not, see http://www.gnu.org/licenses/ |
| 14 | |
| 15 | // Official GIT repository and contact information can be found at |
| 16 | // http://github.com/xsacha/Sachesi |
| 17 | |
| 18 | #include "installer.h" |
| 19 | |
| 20 | // Compatibility for old OpenSSL |
| 21 | #if OPENSSL_VERSION_NUMBER < 0x10100005L |
| 22 | static void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) |
| 23 | { |
| 24 | if(n != NULL) |
| 25 | *n = r->n; |
| 26 | |
| 27 | if(e != NULL) |
| 28 | *e = r->e; |
| 29 | |
| 30 | if(d != NULL) |
| 31 | *d = r->d; |
| 32 | } |
| 33 | #endif |
| 34 | |
| 35 | void InstallNet::requestConfigure() |
| 36 | { |
| 37 | if (!_state) |
| 38 | return; |
| 39 | logadd(QString("1. Request Configuration")); |
| 40 | QByteArray buffer; |
| 41 | QDataStream config(&buffer, QIODevice::WriteOnly); |
| 42 | config << qint16(6) /*messageSize*/ << qint16(2) /*version*/ << qint16(1) /*code*/; |
| 43 | sock->write(buffer); |
| 44 | } |
| 45 | |
| 46 | void InstallNet::requestChallenge() |
| 47 | { |
| 48 | logadd(QString("3. Request Challenge")); |
| 49 | BIGNUM* e = BN_new(); BN_set_word(e, 65537); |
| 50 | privkey = RSA_new(); |
| 51 | RSA_generate_key_ex(privkey, 1024, e, nullptr); |
| 52 | const BIGNUM* bign; |
| 53 | RSA_get0_key(privkey, &bign, nullptr, nullptr); |
| 54 | unsigned char* privkey_mod = new unsigned char[BN_num_bytes(bign)]; |
| 55 | BN_bn2bin(bign, privkey_mod); |
| 56 | QByteArray buffer(128,0); |
| 57 | buffer = QByteArray::fromRawData((char*)privkey_mod,128); |
| 58 | QByteArray header; |
| 59 | QDataStream FrameHead(&header, QIODevice::WriteOnly); |
| 60 | FrameHead << qint16(8 + buffer.length()) << qint16(2) << qint16(3) << qint16(buffer.length()); |
| 61 | buffer.prepend(header); |
| 62 | sock->write(buffer); |
| 63 | delete privkey_mod; |
| 64 | } |
| 65 | |
| 66 | void InstallNet::replyChallenge() |
| 67 | { |
| 68 | logadd(QString("5. Reply Challenge")); |
| 69 | const signed char QCONNDOOR_PERMISSIONS[] = {3, 4, 118, -125, 1}; |
| 70 | const char EMSA_SHA1_HASH[] = {48, 33, 48, 9, 6, 5, 43, 14, 3, 2, 26, 5, 0, 4, 20}; |
| 71 | QCryptographicHash sha1(QCryptographicHash::Sha1); |
| 72 | |
| 73 | /* Encrypt challenge */ |
| 74 | QByteArray challengeBuffer = QByteArray((char*)serverChallenge, 30).append((const char*)QCONNDOOR_PERMISSIONS, 5); |
| 75 | sha1.addData(challengeBuffer); |
| 76 | QByteArray hash = QByteArray(EMSA_SHA1_HASH, 15).append(sha1.result()); |
| 77 | QByteArray signature(128, 0); |
| 78 | RSA_private_encrypt(hash.length(),(unsigned char*)hash.data(), (unsigned char*)signature.data(), privkey, RSA_PKCS1_PADDING); |
| 79 | |
| 80 | /* Find Session Key */ |
| 81 | sessionKey = challengeBuffer.mid(8,16); |
| 82 | |
| 83 | /* Construct Plain Text */ |
| 84 | QByteArray plain(12 + challengeBuffer.length() + signature.length(),0); |
| 85 | QDataStream plainText(&plain, QIODevice::WriteOnly); |
| 86 | plainText << qint16(4 + challengeBuffer.length() + signature.length()) << qint16(challengeBuffer.length()) << qint16(signature.length()); |
| 87 | plainText.writeRawData(challengeBuffer.data(),35); |
| 88 | plainText.writeRawData(signature.data(),128); |
| 89 | |
| 90 | AESEncryptSend(plain, 5); |
| 91 | } |