#include "toxme.h" #include #include #include #include #include const QString Toxme::apiUrl{"https://toxme.se/api"}; void Toxme::incrementNonce(unsigned char nonce[]) { auto nonceSize = crypto_box_NONCEBYTES; for (decltype(nonceSize) i=0; iisFinished()) qApp->processEvents(); return reply->readAll(); } QByteArray Toxme::prepareEncryptedJson(int action, QString payload) { static unsigned char nonce[crypto_box_NONCEBYTES]={0}; unsigned char pk[crypto_box_PUBLICKEYBYTES]; unsigned char sk[crypto_box_SECRETKEYBYTES]; crypto_box_keypair(pk,sk); QByteArray payloadData = payload.toUtf8(); const size_t mlen = crypto_box_ZEROBYTES+payloadData.size(); unsigned char* payloadMsg = new unsigned char[mlen]; unsigned char* payloadEnc = new unsigned char[mlen]; memcpy(payloadMsg+crypto_box_ZEROBYTES,payloadData.data(),payloadData.size()); crypto_box(payloadEnc,payloadMsg,mlen,nonce,pk,sk); QByteArray payloadEncData(reinterpret_cast(payloadEnc), mlen); delete[] payloadMsg; delete[] payloadEnc; const QString json{"{\"action\":"+QString().setNum(action)+"," "\"public_key\":\""+QByteArray(reinterpret_cast(pk), crypto_box_PUBLICKEYBYTES)+"\"," "\"encrypted\":\""+payloadEncData+"\"," "\"nonce\":\""+QByteArray(reinterpret_cast(nonce), crypto_box_NONCEBYTES)+"\"}"}; incrementNonce(nonce); return json.toUtf8(); } ToxID Toxme::lookup(QString address) { // JSON injection ? address.replace('\\',"\\\\"); address.replace('"',"\""); ToxID id; const QString json{"{\"action\":3,\"name\":\""+address+"\"}"}; static const QByteArray pattern{"public_key\""}; QByteArray response = makeJsonRequest(json); const int index = response.indexOf(pattern); if (index == -1) return id; response = response.mid(index+pattern.size()); const int idStart = response.indexOf('"'); if (idStart == -1) return id; response = response.mid(idStart+1); const int idEnd = response.indexOf('"'); if (idEnd == -1) return id; response.truncate(idEnd); id = ToxID::fromString(response); return id; } bool Toxme::createAddress(ToxID id, QString address, bool keepPrivate, QString bio) { int privacy = keepPrivate ? 0 : 2; // JSON injection ? bio.replace('\\',"\\\\"); bio.replace('"',"\""); address.replace('\\',"\\\\"); address.replace('"',"\""); const QString payload{"{\"tox_id\":\""+id.toString()+"\"," "\"name\":\""+address+"\"," "\"privacy\":"+QString().setNum(privacy)+"," "\"bio\":\""+bio+"\"," "\"timestamp\":"+QString().setNum(time(0))+"}"}; QByteArray response = makeJsonRequest(prepareEncryptedJson(1,payload)); qDebug() << "payload:"<