[Qt5.4] QSslSocket server + client, handshake error



  • Hi,

    I am trying to write SSL server and client to communicate between each-other.
    I got an error when trying to connect, server response with unknow error and client gives handshake error.
    The code for connection is below:
    SERVER:
    @void MyThread::run()
    {
    // thread starts here
    qDebug() << " Thread started";

    socket = new QSslSocket();
    socket->setProtocol(QSsl::SslV3);
    
    QByteArray key;
    QByteArray cert;
    
    QFile file_key(":/server.key");
    if(file_key.open(QIODevice::ReadOnly))
    {
        key = file_key.readAll();
        file_key.close();
    }
    else
    {
        qDebug() << file_key.errorString();
    }
    
    QFile file_cert(":/server.crt");
    if(file_cert.open(QIODevice::ReadOnly))
    {
        cert = file_cert.readAll();
        file_cert.close();
    }
    else
    {
        qDebug() << file_cert.errorString();
    }
    
    
    QSslKey ssl_key(key, QSsl::Rsa,QSsl::Pem,QSsl::PrivateKey,"server");
    
    QSslCertificate ssl_cert(cert);
    
    socket->addCaCertificate(ssl_cert);
    socket->setLocalCertificate(ssl_cert);
    socket->setPrivateKey(ssl_key);
    
    
    // set the ID
    if(!socket->setSocketDescriptor(this->socketDescriptor))
    {
        // something's wrong, we just emit a signal
        emit error(socket->error());
        return;
    }
    ssl(socket);
    connect(socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error2(QAbstractSocket::SocketError)));
    connect(socket,SIGNAL(sslErrors(QList<QSslError>)),this,SLOT(sslErrors(QList<QSslError>)));
    socket->startServerEncryption();
    qDebug() << socket->waitForEncrypted(30000);
    
    // connect socket and signal
    // note - Qt::DirectConnection is used because it's multithreaded
    //        This makes the slot to be invoked immediately, when the signal is emitted.
    
    connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
    connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection);
    
    // We'll have multiple clients, we want to know which is which
    qDebug() << socketDescriptor << " Client connected";
    
    exec&#40;&#41;;
    

    }
    @

    CLIENT:
    @ socket->setProtocol(QSsl::SslV3);

    QByteArray key;
    QByteArray cert;
    
    QFile file_key(":/server.key");
    if(file_key.open(QIODevice::ReadOnly))
    {
        key = file_key.readAll();
        file_key.close();
    }
    else
    {
        qDebug() << file_key.errorString();
    }
    
    QFile file_cert(":/server.crt");
    if(file_cert.open(QIODevice::ReadOnly))
    {
        cert = file_cert.readAll();
        file_cert.close();
    }
    else
    {
        qDebug() << file_cert.errorString();
    }
    
    
    QSslKey ssl_key(key, QSsl::Rsa,QSsl::Pem,QSsl::PrivateKey,"server");
    
    QSslCertificate ssl_cert(cert);
    
    socket->addCaCertificate(ssl_cert);
    socket->setLocalCertificate(ssl_cert);
    socket->setPrivateKey(ssl_key);
    socket->connectToHostEncrypted("192.168.50.109", port);
    qDebug() << socket->waitForEncrypted();
    qDebug() << socket->error();
    

    @

    Any ideas what I do wrong?
    Thanks



  • Can it be a problem that I signed certificate by myself?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.