0

I'm trying to generate set of RSA keys using EVP_RSA_gen interface from openssl/evp.h.

#include <string.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/err.h>

int main() {
    EVP_PKEY *pkey = NULL;
    pkey = EVP_RSA_gen(4096);

    if (pkey == NULL){
        ERR_print_errors_fp(stderr);
        printf("Key generation failed.\n");
    }

    size_t keyLen;

    if (EVP_PKEY_get_raw_public_key(pkey, NULL, &keyLen) != 1) {
        ERR_print_errors_fp(stderr);
        printf("Key extraction failed.\n");
    }
    return 0;
}

However, when I try to get the size of a raw public key using EVP_PKEY_get_raw_public_key as described here, it fails and the Key extraction failed gets printed. ERR_print_errors_fp doesn't output any message.

What am I doing wrong? It seems there are no mistakes in the code.

Dmytro
  • 190
  • 8
  • 2
    "This function only works for algorithms that support raw public keys. Currently this is: EVP_PKEY_X25519, EVP_PKEY_ED25519, EVP_PKEY_X448 or EVP_PKEY_ED448." No EVP_PKEY_RSA here. – n. m. could be an AI Nov 16 '22 at 10:38
  • @n.m. Thanks for pointing on that piece of docs, seems like I've missed it. Do you have any suggestions on how the public and private keys can be obtained from this `EVP_PKEY` struct then? – Dmytro Nov 16 '22 at 11:51
  • 1
    https://stackoverflow.com/questions/25528417/extract-public-key-from-evp-pkey-keypair – n. m. could be an AI Nov 16 '22 at 12:01

0 Answers0