Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSslSocket: How generate private key, Local certificate and CaCertificates ?

QSslSocket: How generate private key, Local certificate and CaCertificates ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • parisisalP Offline
    parisisalP Offline
    parisisal
    wrote on last edited by
    #1

    Hi,
    I want to realize a secure connection from a client and a server.
    I'm trying to generate for the Server

    • the Private Key
    • the Local Certificate

    and the CaCertificate for the client.

    Using OpenSLL :

    openssl genrsa -des3 -out CA-key.pem 2048
    openssl req -new -key CA-key.pem -x509 -days 1000 -out CA-cert.pem
    openssl genrsa -des3 -out server-key.pem 2048
    openssl req –new –config openssl.cnf –key server-key.pem –out signingReq.csr
    openssl x509 -req -days 365 -in signingReq.csr -CA CA-cert.pem -CAkey CA-key.pem -CAcreateserial -out server-cert.pem

    After this commands sequence I obtain 6 files:
    CA-cert.pem
    CA-key.pem
    CA-cert.srl
    server-cert.pem
    server-key.pem
    signingRew.csr

    On the Server I have something like this :

        QSslSocket *sslSocket = new QSslSocket(this);
        sslSocket->setSocketDescriptor(socketDescriptor);
        QFile certFile("C:\\mycert\\server-cert.pem");
        sslSocket->setLocalCertificate(certFile.readAll(), QSsl::EncodingFormat::Pem);
        QFile keyFile("C:\\mycert\\server-key.pem");
    QSslCertificate sslCert = QSslKey(keyFile.readAll(), QSsl::KeyAlgorithm::Rsa, QSsl::EncodingFormat::Pem, QSsl::PrivateKey, "123456789");
        sslSocket->setPrivateKey(sslCert);
        sslSocket->setProtocol(QSsl::TlsV1_2);
        sslSocket->startServerEncryption();
    

    On client side :

    QSslSocket sslSocket;
     sslSocket.addCaCertificates(QString("C:\\mycert\\server-cert.pem"));
    

    The first issue is that the sslCert is not valid.

    This call fails:

    QSslCertificate sslCert = QSslKey(keyFile.readAll(), QSsl::KeyAlgorithm::Rsa, QSsl::EncodingFormat::Pem, QSsl::PrivateKey, "123456789");
    

    Could you help me ?

    Pablo J. RoginaP 1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #2

      Why do you assign a QSslKey to a QSslCertificate?

      1 Reply Last reply
      0
      • parisisalP parisisal

        Hi,
        I want to realize a secure connection from a client and a server.
        I'm trying to generate for the Server

        • the Private Key
        • the Local Certificate

        and the CaCertificate for the client.

        Using OpenSLL :

        openssl genrsa -des3 -out CA-key.pem 2048
        openssl req -new -key CA-key.pem -x509 -days 1000 -out CA-cert.pem
        openssl genrsa -des3 -out server-key.pem 2048
        openssl req –new –config openssl.cnf –key server-key.pem –out signingReq.csr
        openssl x509 -req -days 365 -in signingReq.csr -CA CA-cert.pem -CAkey CA-key.pem -CAcreateserial -out server-cert.pem

        After this commands sequence I obtain 6 files:
        CA-cert.pem
        CA-key.pem
        CA-cert.srl
        server-cert.pem
        server-key.pem
        signingRew.csr

        On the Server I have something like this :

            QSslSocket *sslSocket = new QSslSocket(this);
            sslSocket->setSocketDescriptor(socketDescriptor);
            QFile certFile("C:\\mycert\\server-cert.pem");
            sslSocket->setLocalCertificate(certFile.readAll(), QSsl::EncodingFormat::Pem);
            QFile keyFile("C:\\mycert\\server-key.pem");
        QSslCertificate sslCert = QSslKey(keyFile.readAll(), QSsl::KeyAlgorithm::Rsa, QSsl::EncodingFormat::Pem, QSsl::PrivateKey, "123456789");
            sslSocket->setPrivateKey(sslCert);
            sslSocket->setProtocol(QSsl::TlsV1_2);
            sslSocket->startServerEncryption();
        

        On client side :

        QSslSocket sslSocket;
         sslSocket.addCaCertificates(QString("C:\\mycert\\server-cert.pem"));
        

        The first issue is that the sslCert is not valid.

        This call fails:

        QSslCertificate sslCert = QSslKey(keyFile.readAll(), QSsl::KeyAlgorithm::Rsa, QSsl::EncodingFormat::Pem, QSsl::PrivateKey, "123456789");
        

        Could you help me ?

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @parisisal you may want to take a look at this blog post.
        It covers your requirements of creating the self-signed certificate for your server and using the self-signed certificate in a Qt client.

        However, that example is using Apache for the server part, so if you want the server to be a Qt app as well, please look at this other example (it's using a websocketserver, but you'll see how the certificate and key are handled...)

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved