diff --git a/src/core.cpp b/src/core.cpp index 5798e200a..38726c44a 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -1107,6 +1107,19 @@ QString Core::getIDString() const // 16^n > 10^10 (which is roughly the planet's population) } +QPair Core::getKeypair() const +{ + QPair keypair; + if (!tox) + return keypair; + + char buf[2*TOX_PUBLIC_KEY_SIZE]; + tox_get_keys(tox, (uint8_t*)buf, (uint8_t*)buf+TOX_PUBLIC_KEY_SIZE); + keypair.first = QByteArray(buf, TOX_PUBLIC_KEY_SIZE); + keypair.second = QByteArray(buf+TOX_PUBLIC_KEY_SIZE, TOX_PUBLIC_KEY_SIZE); + return keypair; +} + QString Core::getStatusMessage() const { QString sname; diff --git a/src/core.h b/src/core.h index f90b7603e..7f87fbfa9 100644 --- a/src/core.h +++ b/src/core.h @@ -72,6 +72,7 @@ public: QString getUsername() const; ///< Returns our username, or an empty string on failure QString getStatusMessage() const; ///< Returns our status message, or an empty string on failure ToxID getSelfId() const; ///< Returns our Tox ID + QPair getKeypair() const; ///< Returns our public and private keys VideoSource* getVideoSourceFromCall(int callNumber); ///< Get a call's video source diff --git a/src/main.cpp b/src/main.cpp index 12cae90ff..da83631e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -185,10 +185,6 @@ int main(int argc, char *argv[]) #endif // Run - Toxme::lookup("tux3@toxme.se"); - Toxme::createAddress(ToxID::fromString("95B6B3A34C82FC449D1F1CA9A7F621DE6A7516D0AA1183E128A5467590BD8913B8C5D287D924"), - "testToxmeAPI@toxme.se",false,"This is a test"); - a.setQuitOnLastWindowClosed(false); int errorcode = a.exec(); diff --git a/src/toxme.cpp b/src/toxme.cpp index 0daaa28ec..968fe7f9c 100644 --- a/src/toxme.cpp +++ b/src/toxme.cpp @@ -1,39 +1,16 @@ #include "toxme.h" +#include "core.h" #include #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; i keypair = Core::getInstance()->getKeypair(); + if (keypair.first.isEmpty() || keypair.second.isEmpty()) + { + qWarning() << "Toxme::prepareEncryptedJson: Couldn't get our keypair, aborting"; + return QByteArray(); + } - unsigned char pk[crypto_box_PUBLICKEYBYTES]; - unsigned char sk[crypto_box_SECRETKEYBYTES]; - crypto_box_keypair(pk,sk); + QByteArray nonce(crypto_box_NONCEBYTES, 0); + randombytes((uint8_t*)nonce.data(), crypto_box_NONCEBYTES); 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()); + const size_t cypherlen = crypto_box_MACBYTES+payloadData.size(); + unsigned char* payloadEnc = new unsigned char[cypherlen]; - crypto_box(payloadEnc,payloadMsg,mlen,nonce,pk,sk); - QByteArray payloadEncData(reinterpret_cast(payloadEnc), mlen); - delete[] payloadMsg; + crypto_box_easy(payloadEnc,(uint8_t*)payloadData.data(),payloadData.size(), + (uint8_t*)nonce.data(),pinnedPk,(uint8_t*)keypair.second.data()); + QByteArray payloadEncData(reinterpret_cast(payloadEnc), cypherlen); 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); + "\"public_key\":\""+keypair.first.toHex()+"\"," + "\"encrypted\":\""+payloadEncData.toBase64()+"\"," + "\"nonce\":\""+nonce.toBase64()+"\"}"}; return json.toUtf8(); } @@ -107,6 +83,28 @@ ToxID Toxme::lookup(QString address) return id; } +int Toxme::extractError(QString json) +{ + static const QByteArray pattern{"c\":"}; + + json = json.remove(' '); + const int index = json.indexOf(pattern); + if (index == -1) + return INT_MIN; + json = json.mid(index+pattern.size()); + + const int end = json.indexOf('}'); + if (end == -1) + return INT_MIN; + json.truncate(end); + + bool ok; + int r = json.toInt(&ok); + if (!ok) + return INT_MIN; + return r; +} + bool Toxme::createAddress(ToxID id, QString address, bool keepPrivate, QString bio) { @@ -124,8 +122,6 @@ bool Toxme::createAddress(ToxID id, QString address, "\"timestamp\":"+QString().setNum(time(0))+"}"}; QByteArray response = makeJsonRequest(prepareEncryptedJson(1,payload)); - qDebug() << "payload:"< #include +#include #include "corestructs.h" class QNetworkAccessManager; @@ -25,10 +26,11 @@ private: Toxme()=delete; static QByteArray makeJsonRequest(QString json); static QByteArray prepareEncryptedJson(int action, QString payload); - static void incrementNonce(unsigned char nonce[]); + static int extractError(QString json); private: static const QString apiUrl; + static const unsigned char pinnedPk[]; }; #endif // TOXME_H