I have a program which looks like this:
EVP_PKEY *generate_RSA_key_and_uuid(unsigned char uuid[16])
{
EVP_PKEY *key_p;
key_p = EVP_PKEY_new();
return key_p;
}
int build_and_save_csr(int dn_entries, X509_REQ *req_p, EVP_PKEY *priv_key_p, char *passphrase)
{
priv_key_p = generate_RSA_key_and_uuid(uuid);
}
int makecsr(X509_REQ *req_p, EVP_PKEY *priv_key_p, char *passphrase)
{
if (build_and_save_csr(2, req_p, priv_key_p, passphrase) != 0) {
fprintf(stderr, "Could not create csr file / private key...\n");
exit(1);
}
return 0;
}
int main(int argc, char *argv[])
{
EVP_PKEY *priv_key_p;
X509_REQ *req_p;
makecsr(req_p, priv_key_p, passphrase);
return 0;
}
After this has run, *priv_key_p
should contain the value returned from *generate_RSA_key_and_uuid
.
In gdb, I can see that at the end of generate_RSA_and_uuid
that key_p
contains the correct value but after priv_key_p = generate_RSA_key_and_uuid(uuid);
has executed, priv_key_p
is empty.
Does anybody know how to properly assign the value to the pointer?
You need an extra level of indirection, otherwise you're just modifying local copies of the pointers and nothing gets returned:
int build_and_save_csr(int dn_entries, X509_REQ *req_p, EVP_PKEY **priv_key_p, char *passphrase)
{
*priv_key_p = generate_RSA_key_and_uuid(uuid);
}
int makecsr(X509_REQ *req_p, EVP_PKEY **priv_key_p, char *passphrase)
{
if (build_and_save_csr(2, req_p, priv_key_p, passphrase) != 0) {
fprintf(stderr, "Could not create csr file / private key...\n");
exit(1);
}
return 0;
}
int main(int argc, char *argv[])
{
EVP_PKEY *priv_key_p;
X509_REQ *req_p;
makecsr(req_p, &priv_key_p, passphrase);
return 0;
}
Alternatively you can use the function result (as you do already in generate_RSA_key_and_uuid()
) to return the pointer (perhaps return NULL on error) - this gets rid of the need for the extra level of indirection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With