This commit is contained in:
Zach Fox 2017-10-11 16:38:48 -07:00
parent 5a3a3c4937
commit c3e66c9581
3 changed files with 40 additions and 35 deletions

View file

@ -730,28 +730,32 @@ void Wallet::handleChallengeOwnershipPacket(QSharedPointer<ReceivedMessage> pack
RSA* rsa = readKeys(keyFilePath().toStdString().c_str()); RSA* rsa = readKeys(keyFilePath().toStdString().c_str());
const int decryptionStatus = RSA_private_decrypt(textLength, text, reinterpret_cast<unsigned char*>(encryptedText.data()), rsa, RSA_PKCS1_OAEP_PADDING); if (rsa) {
RSA_free(rsa); const int decryptionStatus = RSA_private_decrypt(textLength, text, reinterpret_cast<unsigned char*>(encryptedText.data()), rsa, RSA_PKCS1_OAEP_PADDING);
RSA_free(rsa);
if (decryptionStatus != -1) { if (decryptionStatus != -1) {
auto nodeList = DependencyManager::get<NodeList>(); auto nodeList = DependencyManager::get<NodeList>();
QByteArray decryptedTextByteArray = decryptedText.toUtf8(); QByteArray decryptedTextByteArray = decryptedText.toUtf8();
int decryptedTextByteArraySize = decryptedTextByteArray.size(); int decryptedTextByteArraySize = decryptedTextByteArray.size();
int certIDSize = certID.size(); int certIDSize = certID.size();
// setup the packet // setup the packet
auto decryptedTextPacket = NLPacket::create(PacketType::ChallengeOwnership, certIDSize + decryptedTextByteArraySize + 2*sizeof(int), true); auto decryptedTextPacket = NLPacket::create(PacketType::ChallengeOwnership, certIDSize + decryptedTextByteArraySize + 2 * sizeof(int), true);
decryptedTextPacket->writePrimitive(certIDSize); decryptedTextPacket->writePrimitive(certIDSize);
decryptedTextPacket->writePrimitive(decryptedTextByteArraySize); decryptedTextPacket->writePrimitive(decryptedTextByteArraySize);
decryptedTextPacket->write(certID); decryptedTextPacket->write(certID);
decryptedTextPacket->write(decryptedTextByteArray); decryptedTextPacket->write(decryptedTextByteArray);
qCDebug(commerce) << "Sending ChallengeOwnership Packet containing decrypted text"; qCDebug(commerce) << "Sending ChallengeOwnership Packet containing decrypted text";
nodeList->sendPacket(std::move(decryptedTextPacket), *sendingNode); nodeList->sendPacket(std::move(decryptedTextPacket), *sendingNode);
} else {
qCDebug(commerce) << "During entity ownership challenge, decrypting the encrypted text failed.";
}
} else { } else {
qCDebug(commerce) << "During entity ownership challenge, decrypting the encrypted text failed."; qCDebug(commerce) << "During entity ownership challenge, creating the RSA object failed.";
} }
} }

View file

@ -1159,28 +1159,32 @@ void EntityTree::startPendingTransferStatusTimer(const QString& certID, const En
transferStatusRetryTimer->start(90000); transferStatusRetryTimer->start(90000);
} }
QString EntityTree::computeEncryptedNonce(const QString& certID, const QString& ownerKey) { QString EntityTree::computeEncryptedNonce(const QString& certID, const QString ownerKey) {
QUuid nonce = QUuid::createUuid(); QUuid nonce = QUuid::createUuid();
const auto text = reinterpret_cast<const unsigned char*>(qPrintable(nonce.toString())); const auto text = reinterpret_cast<const unsigned char*>(qPrintable(nonce.toString()));
const unsigned int textLength = nonce.toString().length(); const unsigned int textLength = nonce.toString().length();
const auto publicKey = reinterpret_cast<const unsigned char*>(ownerKey.toUtf8().toBase64().constData()); BIO* bio = BIO_new_mem_buf((void*)ownerKey.toUtf8().constData(), -1);
BIO* bio = BIO_new_mem_buf((void*)publicKey, sizeof(publicKey)); BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); // NO NEWLINE
RSA* rsa = PEM_read_bio_RSAPublicKey(bio, NULL, NULL, NULL); RSA* rsa = PEM_read_bio_RSAPublicKey(bio, NULL, NULL, NULL);
QByteArray encryptedText(RSA_size(rsa), 0); //if (rsa) {
const int encryptStatus = RSA_public_encrypt(textLength, text, reinterpret_cast<unsigned char*>(encryptedText.data()), rsa, RSA_PKCS1_OAEP_PADDING); QByteArray encryptedText(RSA_size(rsa), 0);
BIO_free(bio); const int encryptStatus = RSA_public_encrypt(textLength, text, reinterpret_cast<unsigned char*>(encryptedText.data()), rsa, RSA_PKCS1_OAEP_PADDING);
RSA_free(rsa); BIO_free(bio);
if (encryptStatus == -1) { RSA_free(rsa);
qCWarning(entities) << "Unable to compute encrypted nonce for" << certID; if (encryptStatus == -1) {
return ""; qCWarning(entities) << "Unable to compute encrypted nonce for" << certID;
} return "";
}
QWriteLocker locker(&_certNonceMapLock); QWriteLocker locker(&_certNonceMapLock);
_certNonceMap.insert(certID, nonce); _certNonceMap.insert(certID, nonce);
return encryptedText.toBase64(); return encryptedText.toBase64();
//} else {
// return "";
//}
} }
bool EntityTree::verifyDecryptedNonce(const QString& certID, const QString& decryptedNonce) { bool EntityTree::verifyDecryptedNonce(const QString& certID, const QString& decryptedNonce) {
@ -1246,8 +1250,7 @@ void EntityTree::validatePop(const QString& certID, const EntityItemID& entityIt
} else { } else {
// Second, challenge ownership of the PoP cert // Second, challenge ownership of the PoP cert
// 1. Encrypt a nonce with the owner's public key // 1. Encrypt a nonce with the owner's public key
QString ownerKey(jsonObject["transfer_recipient_key"].toString()); QString encryptedText = computeEncryptedNonce(certID, jsonObject["transfer_recipient_key"].toString());
QString encryptedText = computeEncryptedNonce(certID, ownerKey);
if (encryptedText == "") { if (encryptedText == "") {
qCDebug(entities) << "CRITICAL ERROR: Couldn't compute encrypted nonce. Deleting entity..."; qCDebug(entities) << "CRITICAL ERROR: Couldn't compute encrypted nonce. Deleting entity...";
@ -1260,8 +1263,6 @@ void EntityTree::validatePop(const QString& certID, const EntityItemID& entityIt
int certIDByteArraySize = certIDByteArray.size(); int certIDByteArraySize = certIDByteArray.size();
QByteArray encryptedTextByteArray = encryptedText.toUtf8(); QByteArray encryptedTextByteArray = encryptedText.toUtf8();
int encryptedTextByteArraySize = encryptedTextByteArray.size(); int encryptedTextByteArraySize = encryptedTextByteArray.size();
QByteArray ownerKeyByteArray = ownerKey.toUtf8();
int ownerKeyByteArraySize = ownerKeyByteArray.size();
auto challengeOwnershipPacket = NLPacket::create(PacketType::ChallengeOwnership, auto challengeOwnershipPacket = NLPacket::create(PacketType::ChallengeOwnership,
certIDByteArraySize + encryptedTextByteArraySize + 2 * sizeof(int), certIDByteArraySize + encryptedTextByteArraySize + 2 * sizeof(int),
true); true);

View file

@ -385,7 +385,7 @@ protected:
Q_INVOKABLE void startPendingTransferStatusTimer(const QString& certID, const EntityItemID& entityItemID, const SharedNodePointer& senderNode); Q_INVOKABLE void startPendingTransferStatusTimer(const QString& certID, const EntityItemID& entityItemID, const SharedNodePointer& senderNode);
private: private:
QString computeEncryptedNonce(const QString& certID, const QString& ownerKey); QString computeEncryptedNonce(const QString& certID, const QString ownerKey);
bool verifyDecryptedNonce(const QString& certID, const QString& decryptedNonce); bool verifyDecryptedNonce(const QString& certID, const QString& decryptedNonce);
void validatePop(const QString& certID, const EntityItemID& entityItemID, const SharedNodePointer& senderNode, bool isRetryingValidation); void validatePop(const QString& certID, const EntityItemID& entityItemID, const SharedNodePointer& senderNode, bool isRetryingValidation);
}; };