| 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 | void InstallNet::requestAuthenticate() |
| 21 | { |
| 22 | logadd(QString("8. Request Authenticate")); |
| 23 | QByteArray buffer; |
| 24 | QDataStream config(&buffer, QIODevice::WriteOnly); |
| 25 | config << qint16(6) /*messageSize*/ << qint16(2) /*version*/ << qint16(8) /*code*/; |
| 26 | sock->write(buffer); |
| 27 | } |
| 28 | |
| 29 | void InstallNet::AESEncryptSend(QByteArray &plain, int code) |
| 30 | { |
| 31 | /* Encrypt Plain Text */ |
| 32 | unsigned char* iv = new unsigned char[16]; |
| 33 | RAND_bytes(iv, 16); |
| 34 | int ilen, tlen; |
| 35 | unsigned char* encrypt = new unsigned char[plain.length() + 16]; |
| 36 | EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); |
| 37 | EVP_CIPHER_CTX_init(ctx); |
| 38 | const EVP_CIPHER *cipher = EVP_aes_128_cbc(); |
| 39 | EVP_EncryptInit(ctx, cipher, (unsigned char*)sessionKey.data(), iv); |
| 40 | EVP_EncryptUpdate(ctx, encrypt, &ilen, (unsigned char*)plain.data(), plain.length()); |
| 41 | EVP_EncryptFinal(ctx, encrypt + ilen, &tlen); |
| 42 | EVP_CIPHER_CTX_free(ctx); |
| 43 | |
| 44 | /* Create buffer */ |
| 45 | QByteArray buffer; |
| 46 | QDataStream Auth(&buffer, QIODevice::WriteOnly); |
| 47 | Auth << qint16(ilen + tlen) << qint16(plain.length()); |
| 48 | Auth.writeRawData((char*)iv,16); |
| 49 | Auth.writeRawData((char*)encrypt, ilen+tlen); |
| 50 | |
| 51 | /* Package and Send */ |
| 52 | QByteArray header; |
| 53 | QDataStream FrameHead(&header, QIODevice::WriteOnly); |
| 54 | FrameHead << qint16(6 + buffer.length()) << qint16(2) << qint16(code); |
| 55 | buffer.prepend(header); |
| 56 | sock->write(buffer); |
| 57 | delete [] iv; |
| 58 | delete [] encrypt; |
| 59 | } |
| 60 | |
| 61 | void InstallNet::authorise() |
| 62 | { |
| 63 | logadd(QString("10. Authorise")); |
| 64 | /* Construct Plain Text */ |
| 65 | QByteArray plain(2+hashedPassword.length(),0); |
| 66 | QDataStream plainText(&plain, QIODevice::WriteOnly); |
| 67 | plainText << qint16(hashedPassword.length()); |
| 68 | plainText.writeRawData(hashedPassword.data(),hashedPassword.length()); |
| 69 | |
| 70 | AESEncryptSend(plain, 10); |
| 71 | } |
| 72 | |
| 73 | void InstallNet::keepAlive() |
| 74 | { |
| 75 | logadd(QString("6. Keep Alive")); |
| 76 | QByteArray buffer; |
| 77 | QDataStream config(&buffer, QIODevice::WriteOnly); |
| 78 | config << qint16(6) /*messageSize*/ << qint16(2) /*version*/ << qint16(6) /*code*/; |
| 79 | sock->write(buffer); |
| 80 | } |