PDA

View Full Version : password authentication with QsslSocket



mate
15th July 2008, 22:08
hi,

i have a problem with password authentication in a QSslSocket connection.


void encThread::run()
{
serverSocket = new QSslSocket();
connect(serverSocket, SIGNAL(readyRead()), this, SLOT(input()));
while(true)
{
QByteArray pass("password");
serverSocket->setLocalCertificate("server.cert.crt");
serverSocket->setPrivateKey("privkey.pem",QSsl::Rsa,QSsl::Pem,pass);

if(serverSocket->setSocketDescriptor(socketDescriptor))
{
connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready()));
serverSocket->startServerEncryption();
}
else
{
emit error(serverSocket->error());
delete serverSocket;
return;
}
if(!(file->open(QIODevice::ReadOnly | QIODevice::Text)))
{
exit();
}
syslog = QString::fromUtf8(file->readAll());
file->close();

serverSocket->write(toChar(syslog));

while(serverSocket->waitForDisconnected())
{
serverSocket->disconnectFromHost();
}

}
}
...
void encThread::input()
{
QByteArray line = serverSocket->readAll();

qDebug() << line;

if(line.startsWith(':'))
{
qDebug() << "password: " << line;
if(line != test_password)
{
serverSocket->abort;

}
}

when i start a connection from the client with a wrong password, first the server sends with
serverSocket->write(toChar(syslog));
to the client, and than he cut the connection.

other versions with bool values dont' send any data's with good or bad password - like this:


...
bool test;
...
if(test)
serverSocket->write(toChar(syslog));
...

i cant't find a solution that test the passord first, and send the data if the password ist valid.

regards
mate

wysota
15th July 2008, 23:10
I'm not sure if I understand your problem but writing data to the socket doesn't mean it has been send to the remote side of the connection. You have to wait until that happens before terminating the connection.

mate
16th July 2008, 08:17
hi,
sorry, i try to explain it better.
my problem is, that data was send to the remote host before the password is checked. it seems that the readyRead() signal reaches the server after the data is send to the remote host (client). the passwordcheck is to late. i don't want that any data will sent from the server to the host until the password is checked.


void encThread::run()
{
...
connect(serverSocket, SIGNAL(readyRead()), this, SLOT(input()));
...
}
while(true)
{
...
{
// this code dosent doesn't work.
if(passwordtest == true)
{
serverSocket->write(toChar(syslog));
}
...
...
void encThread::input()
{
if(line.startsWith(':'))
{
QByteArray line = serverSocket->readAll();
if(line != test_password)
{
passwordtest = false; // bool
serverSocket->abort;
}
...

i hope this shows the problem.

thanks
mate