Правильно импортируйте pkcs12 в Qt, используя QSslCertificate - PullRequest
0 голосов
/ 12 октября 2018

Я бы хотел импортировать закрытый ключ и сертификат с помощью QSslCertificate.

QFile keyFile(QDir::currentPath()+ "/privatekey.pfx");
keyFile.open(QFile::ReadOnly);
QString password = "Password";
QSslKey key(keyFile.readAll(), QSsl::Rsa, QSsl::Der, QSsl::PrivateKey);
QFile certFile(QDir::currentPath()+ "/certificate.crt");
certFile.open(QFile::ReadOnly);
QSslCertificate certificate;
QList<QSslCertificate> importedCerts = QSslCertificate::fromData(certFile.readAll());

bool imported = QSslCertificate::importPkcs12(&keyFile, &key, &certificate, &importedCerts);
QSslConfiguration config = QSslConfiguration();
config.setCaCertificates(importedCerts);
config.setLocalCertificate(certificate);
config.setPrivateKey(key);
config.setProtocol(QSsl::SecureProtocols);
config.setPeerVerifyMode(QSslSocket::VerifyPeer);

Согласно документации я загружаю закрытый ключ в формате pfx.В режиме отладки каждый раз я получаю ложный результат от QSslCertificate :: importPkcs12.В чем может быть причина?

1 Ответ

0 голосов
/ 13 октября 2018

Вы используете API совершенно неправильно.Параметры ключа и указателя сертификата для метода являются параметрами out , поэтому вам не следует предварительно заполнять их данными.

Предполагая, что у вас есть файл PKCS # 12, который содержит основной сертификат, чтобыполучить закрытый ключ, сертификат и, необязательно, цепочку сертификатов для основного сертификата; правильное использование будет:

QFile pfxFile(QDir::currentPath()+ "/privatekey.pfx");
bool isOpen = pfxFile.open(QFile::ReadOnly);
// you should verify the file is open here!

// all default contructed, as they are filled by the importPkcs12 method
QSslKey key;
QSslCertificate certificate;
QList<QSslCertificate> certChain;

// now import into those three
bool imported = QSslCertificate::importPkcs12(&pfxFile, &key, &certificate, &certChain, password);
// imported should be true now, continue creating the ssl config as you did before
...