Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. How to load a .p12 key into the QSslKey object?
Forum Update on Monday, May 27th 2025

How to load a .p12 key into the QSslKey object?

Scheduled Pinned Locked Moved Solved Qt WebKit
qsslkeyqnetworkaccessmqsslconfigurati
3 Posts 2 Posters 3.4k Views
  • 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.
  • GoneWithTheFoodG Offline
    GoneWithTheFoodG Offline
    GoneWithTheFood
    wrote on last edited by
    #1

    Hi,

    I'm trying to build a https link using QNetworkAccessManager. I tried to load my certificate and private key into the configuration object of class QSslConfiguration. The certificate was loaded successfully, but I got some problem with the private key. The key file could be opened properly, but when I tried to construct a QSslKey object using the loaded QByteArray, the key is always not valid. Here are the codes:

        QSslConfiguration config;
        QFile certFile(QDir::currentPath() + QString("/sslCert/testcert.cer"));
        bool openOk = certFile.open(QIODevice::ReadWrite);
        qDebug() << "cert open ok = " << openOk; //ok
        QSslCertificate sslCert(certFile.readAll(), QSsl::Der);
        qDebug()<<"cert valid = " << !sslCert.isNull(); //not null
        config.setLocalCertificate(sslCert);
    
        QFile keyFile(QDir::currentPath() + QString("/sslCert/testkey.p12"));
        openOk = keyFile.open(QIODevice::ReadWrite);
        qDebug() << "key open ok = " << openOk; //ok
        qDebug() << keyFile.readAll(); //not empty
        QSslKey sslKey(keyFile.readAll(), QSsl::Rsa, QSsl::Der, QSsl::PrivateKey, "123456");
        qDebug()<< "key valid = " << !sslKey.isNull(); //**is null**
        config.setPrivateKey(sslKey);
    

    Is there anything wrong?

    P.S. I'm using Qt 5.5.1 under Ubuntu 14.04. The certificate and key were generated by keytool.They were all tested and could be imported properly into firefox and chrome.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      luca
      wrote on last edited by
      #2

      AFAIK .p12 is not a private Key.
      It contains both keys and certs .

      You must use QSslKey to load a private key,

      GoneWithTheFoodG 1 Reply Last reply
      0
      • L luca

        AFAIK .p12 is not a private Key.
        It contains both keys and certs .

        You must use QSslKey to load a private key,

        GoneWithTheFoodG Offline
        GoneWithTheFoodG Offline
        GoneWithTheFood
        wrote on last edited by GoneWithTheFood
        #3

        @luca yes you are right. :)
        Just now I've tried to export the private key from the .p12 file with the openssl API. This key could be constructed into QSslKey object now. Thanks a lot.
        But to configure in such a way is a little bit too complicated. Is there any class or method I can use, so that I can directly import the .p12 file into the QSslConfiguration object?

        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