Ich schreibe eine Clientanwendung unter Windows, die eine SSL-Verbindung zu einem Server herstellt, und der Server fordert das Clientzertifikat zur Authentifizierung an. Der Server bietet mir eine PFX-Datei, dann ich OpenSSL Kommandozeilen-Tool verwenden, um das Zertifikat und den privaten Schlüssel wie folgt zu erhalten:SSL_CTX_use_PrivateKey_file() fehlgeschlagen
openssl pkcs12 -in filename.pfx -clcerts -nokeys -out cert.pem
openssl pkcs12 -in filename.pfx -nocerts -out key.pem
danach, ich versuche, mit Funktionen das Zertifikat und den privaten Schlüssel laden von openssl wie unten, aber SSL_CTX_use_PrivateKey_file()
immer fehlgeschlagen, ist die Fehlermeldung "error:0906D06C:PEM routines:PEM_read_bio:no start line
", kann ich nicht herausfinden, warum, kann mir jemand eine Erleuchtung geben? Hier ist der Code.
#include "openssl/ssl.h"
#include "openssl/err.h"
#include <stdio.h>
#include <string>
int InitClientCtx()
{
OpenSSL_add_ssl_algorithms();
SSL_CTX* m_pClientCtx;
m_pClientCtx = SSL_CTX_new(SSLv23_method());
if(!m_pClientCtx)
{
return -1;
}
::SSL_CTX_set_options(m_pClientCtx, SSL_OP_ALL); //for well-known bugs
int nRet = 0;
std::string sCertFilePath = "C:\\cert.pem";
nRet = SSL_CTX_use_certificate_chain_file(m_pClientCtx, sCertFilePath.c_str());
std::string sKeyPassWord = "123456";
SSL_CTX_set_default_passwd_cb_userdata(m_pClientCtx, (void*)(sKeyPassWord.c_str()));
std::string sKeyFilePath = "C:\\key.pem";
// this method returned 0, which means it failed.
nRet = SSL_CTX_use_PrivateKey_file(m_pClientCtx, sKeyFilePath.c_str(), SSL_FILETYPE_PEM);
SSL_load_error_strings();
unsigned long n = ERR_get_error();
char buf[1024];
printf("%s\n", ERR_error_string(n, buf));
nRet = SSL_CTX_check_private_key(m_pClientCtx);
if (nRet <= 0)
{
return -1;
}
/*std::string sCACertFilePath;
nRet = SSL_CTX_load_verify_locations(m_pClientCtx, sCACertFilePath.c_str(), NULL);*/
return 0;
}
int main()
{
InitClientCtx();
return 0;
};
Sehr nützlicher Trick zum Entfernen des Passwortes einer Schlüsseldatei. _In Toolbox_ hinzufügen. – samvv