Loading an SSL certificate located in code
-
Hey,
I want to make load a QsslCertificate which I have copied from the .pem file into the code. It looks like this:
const char * certData = "-----BEGIN CERTIFICATE-----\ dY32TQ==\ (...)\ -----END CERTIFICATE-----"; QSslCertificate cert(certData); if (cert.isNull()) { QMessageBox::information(0,"Certificate is null.", certData); }
But
cert.isNull()
returns true. How do I need to reformat my certificate data for it to load correctly?Thanks!
Nathan -
Well, I managed to load my certificate using
QList<QSslCertificate> certs = QSslCertificate::fromPath(":/certificate.pem");
And adding certificate.pem to the resources.
-
Please this code from me where I did exactly this:
https://github.com/cybercatalyst/qtwebserver/blob/master/tcp/tcpmultithreadedserver.cpp#L141void MultithreadedServer::setDefaultSslConfiguration() { // Set a default SSL configuration just to have it running out of the // box. Only for development purposes, never distribute an application // that relies on SSL with the default configuration set. You can // generate your own key and self-signed certificate: // // $ openssl req -x509 -newkey rsa:2048 -keyout server.key -nodes -days 365 -out server.crt QSslConfiguration sslConfiguration; QByteArray defaultKey = "-----BEGIN PRIVATE KEY-----\n" "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDQdRrom1g/MsJh\n" "bfwVBZH+oxuGeIBO3jeNWs2bjpDmjtEtMz66LfFVrjuggt0UeNFb1Ve3tYIVq4Wj\n" "gmHYUOB44KCzVqJrd1mZdDKukdvoalJyh5f5/kqLdNGzrfjl5fmtK4jy76xM0oV4\n" "aZlj8eC4+odrS9HLHfBylDIeSXPWtThFw02IGiQALbtW8cqHSOXhucPAwiNFELVd\n" "jjFLa0buQSWq3WSZnroLwOXxfd2dtIoDIn319wTRFPAHWQDqlIuvefYvNzel65yY\n" "ZwoomIOrcxfN7IczLxkryD1uZpr4u2aWpHpz+mB7WXhPW82H6qT205e25iDbiZUQ\n" "+2UJWBHBAgMBAAECggEBAKF7Fe066ZGLcKio2q4uMnpfP+NbVYnC+qW1wbDPL9Bq\n" "sf+hwuXW0SzeW7JrrXc+YHATRHA7WxoOVOflCIbZoAoDeHl0kz1Mp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TdTgVS9CEwDAYDVR0T\n" "BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAmxPOPLbq0EBBWrboNfGtD1OTnchy\n" "kBlhrSG+2gLNS3lfiphOZ+qtUGNTgewAk+nR5sraw4uFG8+6oQmNFS35zlbzWhgC\n" "dveQaT4CNtXCpAevRCbhhuDxzU0W0Dtenf3xFBC2wUWbAwxGZuklyA5ohl9+1By7\n" "YG5dPYYsgoeQl9t5yg5orh39kcrrz/exNlPH71qZO6QrPLlaRs0q4S4Yj33hgm/1\n" "XAILf9ZtcTElTRhSHC81bZz2HG84nP39WJCe4hH081cdkAMm7W2fzPTzHUWWRW34\n" "PMB0Ipp7R7CrztyxyaaDo1S3ozEpqvVT0KI65dWLt8ZsggcKxP6P5aeCBQ==\n" "-----END CERTIFICATE-----"; QSslKey sslKey(defaultKey, QSsl::Rsa); sslConfiguration.setPrivateKey(sslKey); QSslCertificate sslCertificate(defaultCertificate); sslConfiguration.setLocalCertificate(sslCertificate); sslConfiguration.setProtocol(QSsl::AnyProtocol); setSslConfiguration(sslConfiguration); }