You are not logged in.

1

Saturday, April 7th 2012, 1:44pm

QSslSocket Read Write Problems

I am trying to write a secure networking program but when i try to write and read on the socket, the output is "_
♥"


Server Read Source:

Source code

1
2
QByteArray data = mClientSocket->readLine(10);
    qDebug() << data;



Client Write Source:

Source code

1
2
3
mSslSocket->write("Login\r\n", 10);
    mSslSocket->flush();
    mSslSocket->waitForBytesWritten(1000);


?(

2

Saturday, April 7th 2012, 2:42pm

read sig.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Sunday, April 8th 2012, 8:02am

Sever.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef SERVER_H
#define SERVER_H

#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include "clientconnection.h"

static const int port = 9986;

class Server : public QTcpServer
{
    Q_OBJECT
public:
    Server() : QTcpServer() {}
    virtual ~Server() {}

    void startServer();


protected:
    virtual void incomingConnection( int socketDescriptor );
};

#endif // SERVER1_H


Server.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "server.h"

void Server::startServer()
{
    if(!this->listen(QHostAddress::Any, port))
    {
        qDebug() << "Server Startup Failed";
    }
    else
    {
        qDebug() << "Server Startup Succeded";
    }
}

void Server::incomingConnection( int socketDescriptor )
{
    ClientConnection *clientConnection = new ClientConnection();
    clientConnection->incomingConnection(socketDescriptor);
}



ClientConnection.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef CLIENTCONNECTION_H
#define CLIENTCONNECTION_H

#include 
#include 

#include 
#include 
#include 
#include 
#include 

class ClientConnection : public QObject
{
    Q_OBJECT
public:
    explicit ClientConnection(QObject *parent = 0);

    void incomingConnection(int socketDescriptor);

private:
    QSslSocket *mClientSocket;

private slots:
    void readyRead();
    void encrypted();
    void sslModeChanged( QSslSocket::SslMode );
    void sslErrors( const QList& );
    void disconnected();
    void connected();
    void error( QAbstractSocket::SocketError );
    void stateChanged( QAbstractSocket::SocketState );

};

#endif // CLIENTCONNECTION_H


ClientConnection.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "clientconnection.h"

#include <QBuffer>

ClientConnection::ClientConnection(QObject *parent) :
    QObject(parent)
{
}

void ClientConnection::incomingConnection(int socketDescriptor)
{
    qDebug() << "MySslServer::incomingConnection(" << socketDescriptor << ")";
    mClientSocket = new QSslSocket( this );

    connect( mClientSocket, SIGNAL(readyRead()), this, SLOT(readyRead()) );
    connect( mClientSocket, SIGNAL(encrypted()), this, SLOT(encrypted()) );
    connect( mClientSocket, SIGNAL(modeChanged(QSslSocket::SslMode)), this, SLOT(sslModeChanged(QSslSocket::SslMode)) );
    connect( mClientSocket, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(sslErrors(const QList<QSslError>&)) );
    connect( mClientSocket, SIGNAL(disconnected()), this, SLOT(disconnected()) );
    connect( mClientSocket, SIGNAL(connected()), this, SLOT(connected()) );
    connect( mClientSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)) );
    connect( mClientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this,
             SLOT(stateChanged(QAbstractSocket::SocketState)) );

    QByteArray key;
    QByteArray cert;

    QFile fileKey(":/ssl/Resources/localhost.key");
    if(fileKey.open(QIODevice::ReadOnly))
    {
        key = fileKey.readAll();
        fileKey.close();
    }
    else
    {
        qDebug() << fileKey.errorString();
    }

    QFile fileCert(":/ssl/Resources/localhost.cert");
    if(fileCert.open(QIODevice::ReadOnly))
    {
        cert = fileCert.readAll();
        fileCert.close();
    }
    else
    {
        qDebug() << fileCert.errorString();
    }

    QSslKey sslKey(key, QSsl::Rsa);
    QSslCertificate sslCert(cert);

    mClientSocket->setLocalCertificate(sslCert);
    mClientSocket->setPrivateKey(sslKey);

    #if 0
    qDebug() << "  Supported ciphers:";
    foreach( QSslCipher cipher, QSslSocket::supportedCiphers() )
         qDebug() << cipher.name();

    qDebug() << "  Default ciphers:";
    foreach( QSslCipher cipher, QSslSocket::defaultCiphers() )
         qDebug() << cipher.name();
    #endif

    if( mClientSocket->setSocketDescriptor( socketDescriptor ) )
    {
        qDebug() << "sslSocket mode: " << (int)mClientSocket->mode();
        qDebug() << "sslSocket state:" << (int)mClientSocket->state();
    }
    else
    {
        qWarning() << "Couldn't setSocketDescriptor(" << socketDescriptor << ") for this connection";
        delete mClientSocket;
    }
}

void ClientConnection::encrypted()
{
    qDebug() << "encrypted()";

    qDebug() << "Calling sslSocket->startServerEncryption()";
    mClientSocket->startServerEncryption();
}

void ClientConnection::sslModeChanged( QSslSocket::SslMode mode )
{
    qDebug() << "sslModeChanged(" << (int)mode << ")";
}

void ClientConnection::sslErrors( const QList<QSslError> &errList )
{
    qDebug() << "sslErrors(";
    foreach( QSslError err, errList )
        qDebug() << "            " << err.errorString();
    qDebug() << ")";
}

void ClientConnection::disconnected()
{
    qDebug() << "disconnected()";
}

void ClientConnection::connected()
{
    qDebug() << "connected()";
}

void ClientConnection::error( QAbstractSocket::SocketError err )
{
    QSslSocket *socket = qobject_cast<QSslSocket*>( sender() );
    Q_ASSERT( socket );

    qDebug() << "error(" << (int)err << socket->errorString() << ")";
}

void ClientConnection::stateChanged( QAbstractSocket::SocketState state )
{
    qDebug() << "stateChanged(" << (int)state << ")";
}

void ClientConnection::readyRead()
{
    /*quint16 blockSize;

    QDataStream in(mClientSocket);
    in.setVersion(QDataStream::Qt_4_0);

    //if (mClientSocket->bytesAvailable() < (int)sizeof(quint16))
       // return;

    in >> blockSize;

    //if (mClientSocket->bytesAvailable() < blockSize)
        //return;

    QString packet;
    in >> packet;

    qDebug() << packet;*/

    QByteArray data = mClientSocket->readLine(10);
    qDebug() << data;
}


Client Application


login.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef LOGIN_H
#define LOGIN_H

#include 
#include 

namespace Ui {
class Login;
}

class Login : public QWidget
{
    Q_OBJECT
    
public:
    explicit Login(QWidget *parent = 0);
    ~Login();
    
private:
    Ui::Login *ui;
    QSslSocket *mSslSocket;

    void createSocket();

private slots:
    void loginButtonClicked();
    void connectedToServer();
    void connectionErrors(QAbstractSocket::SocketError err);
};

#endif // LOGIN_H



login.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include 

#include "login.h"
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);

    QDesktopWidget *desktop = QApplication::desktop();
    setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint & Qt::WindowMinimizeButtonHint);

    int screenWidth, width;
    int screenHeight, height;
    int x, y;
    QSize windowSize;

    screenWidth = desktop->width(); // get width of screen
    screenHeight = desktop->height(); // get height of screen

    windowSize = size(); // size of our application window
    width = windowSize.width();
    height = windowSize.height();

    // little computations
    x = (screenWidth - width) / 2;
    y = (screenHeight - height) / 2;
    y -= 50;

    // move window to desired coordinates
    move ( x, y );

    createSocket();

    connect(ui->loginButton, SIGNAL(clicked()), this, SLOT(loginButtonClicked()));
    connect(mSslSocket, SIGNAL(connected()), this, SLOT(connectedToServer()));
    connect(mSslSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectionErrors(QAbstractSocket::SocketError)));
}

Login::~Login()
{
    delete ui;
}

void Login::createSocket()
{
    mSslSocket = new QSslSocket();
}

void Login::loginButtonClicked()
{
    mSslSocket->connectToHostEncrypted("localhost", 9986);
}

void Login::connectedToServer()
{
    ui->infoLabel->setStyleSheet("QLabel { color : black; }");
    ui->infoLabel->setText("Connected");

    //QString input = ui->usernameBox->text();
    //input.append('\n');

    mSslSocket->write("Login\r\n", 10);
    mSslSocket->flush();
    mSslSocket->waitForBytesWritten(1000);
}

void Login::connectionErrors(QAbstractSocket::SocketError err)
{
    ui->infoLabel->setText( mSslSocket->errorString());
}

4

Sunday, April 8th 2012, 11:54am

it looks like you havent started the server correctly. Docs say you need to do something like this:

Source code

1
2
3
4
5
6
7
8
9
10
void SslServer::incomingConnection(int socketDescriptor)
 {
     QSslSocket *serverSocket = new QSslSocket;
     if (serverSocket->setSocketDescriptor(socketDescriptor)) {
         connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready()));
         serverSocket->startServerEncryption();
     } else {
         delete serverSocket;
     }
 }

http://doc.qt.nokia.com/4.7-snapshot/qsslsocket.html#details


I find the docs are pretty handy ;)
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

5

Sunday, April 8th 2012, 12:52pm

I do that at line 66 of clientconnection.cpp

6

Sunday, April 8th 2012, 1:32pm

ok. next guess...

mSslSocket->write("Login\r\n", 10);
mSslSocket->flush();
mSslSocket->waitForBytesWritten(1000);


if that flush works, then this is happening before the encrypted signal is emitted - the docs say "the data is queued in QSslSocket until after the encrypted() signal is emitted" but it looks like you are flushing before encrypted().
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

7

Sunday, April 8th 2012, 7:11pm

I have a problem of the connection not encrypting.

Do you have any ideas why that is?

8

Sunday, April 8th 2012, 9:00pm

no.

I suggest you remove code from your app so that you are only left with the connection of the ssl socket and start to debug from there.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

9

Monday, April 9th 2012, 1:46pm

Ive Started again with the basics and now i get an error saying 'no shared ciphers'

ClientConnection.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
void ClientConnection::incomingConnection(int socketDescriptor)
{
    qDebug() << "Incoming Connection(" << socketDescriptor << ")";
    mClientSocket = new QSslSocket( this );

    if(mClientSocket->setSocketDescriptor(socketDescriptor))
    {
        connect(mClientSocket, SIGNAL(encrypted()), this, SLOT(encrypted()));
        connect( mClientSocket, SIGNAL(readyRead()), this, SLOT(readyRead()) );
        connect( mClientSocket, SIGNAL(modeChanged(QSslSocket::SslMode)), this, SLOT(sslModeChanged(QSslSocket::SslMode)) );
        connect( mClientSocket, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(sslErrors(const QList<QSslError>&)) );
        connect( mClientSocket, SIGNAL(disconnected()), this, SLOT(disconnected()) );
        connect( mClientSocket, SIGNAL(connected()), this, SLOT(connected()) );
        connect( mClientSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)) );
        connect( mClientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this,
                 SLOT(stateChanged(QAbstractSocket::SocketState)) );

        mClientSocket->setProtocol(QSsl::AnyProtocol);

        mClientSocket->setCiphers("DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:AES256-SHA");
        mClientSocket->startServerEncryption();
    }
    else
    {
        delete mClientSocket;
        return;
    }

    QByteArray key;
    QByteArray cert;

    QFile fileKey(":/ssl/Resources/localhost.key");
    if(fileKey.open(QIODevice::ReadOnly))
    {
        key = fileKey.readAll();
        fileKey.close();
    }
    else
    {
        qDebug() << fileKey.errorString();
    }

    QFile fileCert(":/ssl/Resources/localhost.cert");
    if(fileCert.open(QIODevice::ReadOnly))
    {
        cert = fileCert.readAll();
        fileCert.close();
    }
    else
    {
        qDebug() << fileCert.errorString();
    }

    QSslKey sslKey(key, QSsl::Rsa);
    QSslCertificate sslCert(cert);

    mClientSocket->setLocalCertificate(sslCert);
    mClientSocket->setPrivateKey(sslKey);
}

void ClientConnection::encrypted()
{
    qDebug() << "Encrypted";
}

void ClientConnection::sslModeChanged( QSslSocket::SslMode mode )
{
}

void ClientConnection::sslErrors( const QList<QSslError> &errList )
{
    qDebug() << "sslErrors";
    foreach( QSslError err, errList )
        qDebug() << "            " << err.errorString();
    qDebug() << "";
}

void ClientConnection::disconnected()
{
    qDebug() << "Client Disconnected";
}

void ClientConnection::connected()
{
    qDebug() << "Client Connected";
}

void ClientConnection::error( QAbstractSocket::SocketError err )
{
    qDebug() << "error(" << (int)err << mClientSocket->errorString() << ")";
}

void ClientConnection::stateChanged( QAbstractSocket::SocketState state )
{
}

void ClientConnection::readyRead()
{

    QByteArray data = mClientSocket->readLine(10);
    qDebug() << data;
}



login.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);

    QDesktopWidget *desktop = QApplication::desktop();
    setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint & Qt::WindowMinimizeButtonHint);

    int screenWidth, width;
    int screenHeight, height;
    int x, y;
    QSize windowSize;

    screenWidth = desktop->width(); // get width of screen
    screenHeight = desktop->height(); // get height of screen

    windowSize = size(); // size of our application window
    width = windowSize.width();
    height = windowSize.height();

    // little computations
    x = (screenWidth - width) / 2;
    y = (screenHeight - height) / 2;
    y -= 50;

    // move window to desired coordinates
    move ( x, y );

    createSocket();

    connect(ui->loginButton, SIGNAL(clicked()), this, SLOT(loginButtonClicked()));
    connect(mSslSocket, SIGNAL(connected()), this, SLOT(connectedToServer()));
    connect(mSslSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectionErrors(QAbstractSocket::SocketError)));
    connect(mSslSocket, SIGNAL(encrypted()), this, SLOT(encrypted()));
}

Login::~Login()
{
    delete ui;
}

void Login::createSocket()
{
    mSslSocket = new QSslSocket(this);
    mSslSocket->setCiphers("DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:AES256-SHA");
}

void Login::loginButtonClicked()
{
    mSslSocket->connectToHostEncrypted("localhost", 9986);
}

void Login::connectedToServer()
{
    ui->infoLabel->setText("Connected");
}

void Login::encrypted()
{
    ui->infoLabel->setText("Encrypted");

    mSslSocket->write("Hey\r\n\r\n");
}

void Login::connectionErrors(QAbstractSocket::SocketError err)
{
    ui->infoLabel->setText( mSslSocket->errorString());
}

10

Monday, April 9th 2012, 2:57pm

If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

11

Monday, April 9th 2012, 3:39pm

Thats what my first server was based on.

im just going to use tcpsockets as the documentation on them is much better

12

Monday, April 9th 2012, 3:42pm

your first server was based on a Qt4-preview-feedback Archive, May 2007?? Interesting...
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.