ich einen privaten RSA-Schlüssel mit dem folgenden Befehl erstellt:d2i_RSA_PUBKEY, d2i_RSAPrivateKey und d2i_RSAPublicKey gibt NULL zurück
openssl genrsa -out keypair.pem 2048
Ich muß DEN-codierten Schlüssel (PKCS # 1) für dieses Projekt verwenden, so erzeugen ich zwei DIE Dateien aus dieser PEM-codierten privaten Schlüsseldatei - eine mit dem privaten Schlüssel und eine mit dem öffentlichen Schlüssel.
In meinem Code habe ich den Inhalt dieser beiden Dateien in char * Variablen geladen.
Keiner der folgenden Arbeiten wie erwartet:
d2i_RSA_PUBKEY(NULL, &public_key_bytes, public_key_length);
d2i_RSAPublicKey(NULL, &public_key_bytes, public_key_length);
d2i_RSAPrivateKey(NULL, &private_key_bytes, private_key_length);
Ich weiß beacause alle von ihnen, dass null
zurückzukehren. Ich habe auch versucht die folgenden:
RSA * rsa = RSA_new();
d2i_RSA_PUBKEY(&rsa, &public_key_bytes, public_key_length);
RSA * rsa = RSA_new();
d2i_RSAPublicKey(&rsa, &public_key_bytes, public_key_length);
RSA * rsa = RSA_new();
d2i_RSAPrivateKey(&rsa, &private_key_bytes, private_key_length);
Alle Rückkehr null
auch.
Mein kompletter Test-Code lautet wie folgt:
#include <stdio.h>
#include <stdlib.h>
#include <openssl/rsa.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
typedef struct
{
int len;
char * bytes;
} FileData;
static FileData readFileBytes(const char * name, int zero_ended)
{
FILE * fl = fopen(name, "r");
if (fl == NULL) return (FileData) { .len = 0, .bytes = NULL };
fseek(fl, 0, SEEK_END);
long len = ftell(fl);
char * ret = malloc(len + (zero_ended ? 1 : 0));
fseek(fl, 0, SEEK_SET);
fread(ret, 1, len, fl);
if (zero_ended) ret[len] = 0;
fclose(fl);
return (FileData) { .len = len, .bytes = ret };
}
int main()
{
FileData private_key = readFileBytes("../private.der", 0);
FileData public_key = readFileBytes("../public.der", 0);
char* public_key_bytes = public_key.bytes;
int public_key_length = public_key.len;
char* private_key_bytes = private_key.bytes;
int private_key_length = private_key.len;
RSA * rsa;
public_key_bytes = public_key.bytes;
public_key_length = public_key.len;
rsa = d2i_RSA_PUBKEY(NULL, &public_key_bytes, public_key_length);
printf("d2i_RSA_PUBKEY(NULL, &public_key_bytes, public_key_length) != NULL -> %s\n", (rsa != NULL) ? "true" : "false");
public_key_bytes = public_key.bytes;
public_key_length = public_key.len;
rsa = d2i_RSAPublicKey(NULL, &public_key_bytes, public_key_length);
printf("d2i_RSAPublicKey(NULL, &public_key_bytes, public_key_length) != NULL -> %s\n", (rsa != NULL) ? "true" : "false");
private_key_bytes = private_key.bytes;
private_key_length = private_key.len;
rsa = d2i_RSAPrivateKey(NULL, &private_key_bytes, private_key_length);
printf("d2i_RSAPrivateKey(NULL, &private_key_bytes, private_key_length) != NULL -> %s\n", (rsa != NULL) ? "true" : "false");
public_key_bytes = public_key.bytes;
public_key_length = public_key.len;
rsa = RSA_new();
rsa = d2i_RSA_PUBKEY(&rsa, &public_key_bytes, public_key_length);
printf("d2i_RSA_PUBKEY(&rsa, &public_key_bytes, public_key_length) != NULL -> %s\n", (rsa != NULL) ? "true" : "false");
public_key_bytes = public_key.bytes;
public_key_length = public_key.len;
rsa = RSA_new();
rsa = d2i_RSAPublicKey(&rsa, &public_key_bytes, public_key_length);
printf("d2i_RSAPublicKey(&rsa, &public_key_bytes, public_key_length) != NULL -> %s\n", (rsa != NULL) ? "true" : "false");
private_key_bytes = private_key.bytes;
private_key_length = private_key.len;
rsa = RSA_new();
rsa = d2i_RSAPrivateKey(&rsa, &private_key_bytes, private_key_length);
printf("d2i_RSAPrivateKey(&rsa, &private_key_bytes, private_key_length) != NULL -> %s\n", (rsa != NULL) ? "true" : "false");
getchar();
return 0;
}
Was mache ich falsch?
* "Keine der folgenden Arbeiten" * Woher weißt du das? – dbush
Aktualisiert die Frage mit einer Erklärung. Warum die negative Stimme? – LordCapybara
Wenn Sie interessiert sind, können Sie ein Beispiel für das Lesen eines Schreibschlüssels in einer Reihe von Formaten mit C++ unter [Wie generiere ich einen privaten RSA-Schlüssel mit openssl?] (Http://stackoverflow.com/a/30493975/608639) Sie würden [d2i_X509] (http://www.openssl.org/docs/manmaster/crypto/d2i_X509.html) verwenden, um ein ganzes Zertifikat im Format ASN.1/DER zu lesen. – jww