C++ 使用QSslCertificate在Qt中正确导入pkcs12
我想使用QSslCertificate导入私钥和证书C++ 使用QSslCertificate在Qt中正确导入pkcs12,c++,qt,pkcs#12,C++,Qt,Pkcs#12,我想使用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()
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);
QFile键文件(QDir::currentPath()+“/privatekey.pfx”);
keyFile.open(QFile::ReadOnly);
QString password=“password”;
QSslKey密钥(keyFile.readAll(),QSsl::Rsa,QSsl::Der,QSsl::PrivateKey);
QFile证书文件(QDir::currentPath()+“/certificate.crt”);
打开(QFile::ReadOnly);
QSSL证书;
QList importedCerts=QSslCertificate::fromData(certFile.readAll());
bool imported=QSslCertificate::importPkcs12(&keyFile,&key,&certificate,&importedCerts);
QSslConfiguration=QSslConfiguration();
配置设置证书(导入证书);
config.setLocalCertificate(证书);
config.setPrivateKey(密钥);
config.setProtocol(QSsl::SecureProtocols);
config.setPeerVerifyMode(QSslSocket::VerifyPeer);
根据文档,我以pfx格式加载私钥。在调试模式下,每次我从QSslCertificate::importPkcs12获得错误结果时。原因是什么?您使用的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
QFile-pfxFile(QDir::currentPath()+“/privatekey.pfx”);
bool isOpen=pfxFile.open(QFile::ReadOnly);
//您应该验证文件是否已在此处打开!
//所有默认值均为constructed,因为它们由importPkcs12方法填充
QSslKey;
QSSL证书;
QList证书链;
//现在输入到这三个
bool imported=QSslCertificate::importPkcs12(&pfxFile,&key,&certificate,&certChain,password);
//imported现在应该为true,继续像以前一样创建ssl配置
您使用的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
QFile-pfxFile(QDir::currentPath()+“/privatekey.pfx”);
bool isOpen=pfxFile.open(QFile::ReadOnly);
//您应该验证文件是否已在此处打开!
//所有默认值均为constructed,因为它们由importPkcs12方法填充
QSslKey;
QSSL证书;
QList证书链;
//现在输入到这三个
bool imported=QSslCertificate::importPkcs12(&pfxFile,&key,&certificate,&certChain,password);
//imported现在应该为true,继续像以前一样创建ssl配置