mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-04 03:04:40 +02:00
fix for pub key format returned to data-server
This commit is contained in:
parent
a46610a7c1
commit
d00e11541e
1 changed files with 48 additions and 47 deletions
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
#include <openssl/err.h>
|
#include <openssl/err.h>
|
||||||
#include <openssl/rsa.h>
|
#include <openssl/rsa.h>
|
||||||
|
#include <openssl/x509.h>
|
||||||
|
|
||||||
#include <QtCore/QDir>
|
#include <QtCore/QDir>
|
||||||
#include <QtCore/QJsonDocument>
|
#include <QtCore/QJsonDocument>
|
||||||
|
@ -619,56 +620,56 @@ bool DomainServer::shouldAllowConnectionFromNode(const QString& username,
|
||||||
ALLOWED_USERS_SETTINGS_KEYPATH);
|
ALLOWED_USERS_SETTINGS_KEYPATH);
|
||||||
static QStringList allowedUsers = allowedUsersVariant ? allowedUsersVariant->toStringList() : QStringList();
|
static QStringList allowedUsers = allowedUsersVariant ? allowedUsersVariant->toStringList() : QStringList();
|
||||||
|
|
||||||
|
// we always let in a user who is sending a packet from our local socket or from the localhost address
|
||||||
|
if (senderSockAddr.getAddress() != LimitedNodeList::getInstance()->getLocalSockAddr().getAddress()
|
||||||
|
&& senderSockAddr.getAddress() != QHostAddress::LocalHost) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (allowedUsers.count() > 0) {
|
if (allowedUsers.count() > 0) {
|
||||||
// this is an agent, we need to ask them to provide us with their signed username to see if they are allowed in
|
if (allowedUsers.contains(username)) {
|
||||||
// we always let in a user who is sending a packet from our local socket or from the localhost address
|
// it's possible this user can be allowed to connect, but we need to check their username signature
|
||||||
|
|
||||||
if (senderSockAddr.getAddress() != LimitedNodeList::getInstance()->getLocalSockAddr().getAddress()
|
|
||||||
&& senderSockAddr.getAddress() != QHostAddress::LocalHost) {
|
|
||||||
if (allowedUsers.contains(username)) {
|
|
||||||
// it's possible this user can be allowed to connect, but we need to check their username signature
|
|
||||||
|
|
||||||
QByteArray publicKeyArray = _userPublicKeys.value(username);
|
|
||||||
if (!publicKeyArray.isEmpty()) {
|
|
||||||
// if we do have a public key for the user, check for a signature match
|
|
||||||
|
|
||||||
const unsigned char* publicKeyData = reinterpret_cast<const unsigned char*>(publicKeyArray.constData());
|
|
||||||
|
|
||||||
// first load up the public key into an RSA struct
|
|
||||||
RSA* rsaPublicKey = d2i_RSAPublicKey(NULL, &publicKeyData, publicKeyArray.size());
|
|
||||||
|
|
||||||
if (rsaPublicKey) {
|
|
||||||
QByteArray decryptedArray(RSA_size(rsaPublicKey), 0);
|
|
||||||
int decryptResult = RSA_public_decrypt(usernameSignature.size(),
|
|
||||||
reinterpret_cast<const unsigned char*>(usernameSignature.constData()),
|
|
||||||
reinterpret_cast<unsigned char*>(decryptedArray.data()),
|
|
||||||
rsaPublicKey, RSA_PKCS1_PADDING);
|
|
||||||
|
|
||||||
if (decryptResult != -1) {
|
|
||||||
if (username == decryptedArray) {
|
|
||||||
qDebug() << "Username signature matches for" << username << "- allowing connection.";
|
|
||||||
|
|
||||||
// free up the public key before we return
|
|
||||||
RSA_free(rsaPublicKey);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
qDebug() << "Username signature did not match for" << username << "- denying connection.";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
qDebug() << "Couldn't decrypt user signature for" << username << "- denying connection.";
|
|
||||||
}
|
|
||||||
|
|
||||||
// free up the public key, we don't need it anymore
|
|
||||||
RSA_free(rsaPublicKey);
|
|
||||||
} else {
|
|
||||||
// we can't let this user in since we couldn't convert their public key to an RSA key we could use
|
|
||||||
qDebug() << "Couldn't convert data to RSA key for" << username << "- denying connection.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
requestUserPublicKey(username);
|
QByteArray publicKeyArray = _userPublicKeys.value(username);
|
||||||
|
if (!publicKeyArray.isEmpty()) {
|
||||||
|
// if we do have a public key for the user, check for a signature match
|
||||||
|
|
||||||
|
const unsigned char* publicKeyData = reinterpret_cast<const unsigned char*>(publicKeyArray.constData());
|
||||||
|
|
||||||
|
// first load up the public key into an RSA struct
|
||||||
|
RSA* rsaPublicKey = d2i_RSA_PUBKEY(NULL, &publicKeyData, publicKeyArray.size());
|
||||||
|
|
||||||
|
if (rsaPublicKey) {
|
||||||
|
QByteArray decryptedArray(RSA_size(rsaPublicKey), 0);
|
||||||
|
int decryptResult = RSA_public_decrypt(usernameSignature.size(),
|
||||||
|
reinterpret_cast<const unsigned char*>(usernameSignature.constData()),
|
||||||
|
reinterpret_cast<unsigned char*>(decryptedArray.data()),
|
||||||
|
rsaPublicKey, RSA_PKCS1_PADDING);
|
||||||
|
|
||||||
|
if (decryptResult != -1) {
|
||||||
|
if (username == decryptedArray) {
|
||||||
|
qDebug() << "Username signature matches for" << username << "- allowing connection.";
|
||||||
|
|
||||||
|
// free up the public key before we return
|
||||||
|
RSA_free(rsaPublicKey);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
qDebug() << "Username signature did not match for" << username << "- denying connection.";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
qDebug() << "Couldn't decrypt user signature for" << username << "- denying connection.";
|
||||||
|
}
|
||||||
|
|
||||||
|
// free up the public key, we don't need it anymore
|
||||||
|
RSA_free(rsaPublicKey);
|
||||||
|
} else {
|
||||||
|
// we can't let this user in since we couldn't convert their public key to an RSA key we could use
|
||||||
|
qDebug() << "Couldn't convert data to RSA key for" << username << "- denying connection.";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
requestUserPublicKey(username);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// since we have no allowed user list, let them all in
|
// since we have no allowed user list, let them all in
|
||||||
|
|
Loading…
Reference in a new issue