I'm trying to convert an EC private key to PKCS#8. In PHP 8.1 and up, the openssl_pkey_export() function appears to do this:

$privateKey = openssl_pkey_get_private('-----BEGIN EC PRIVATE KEY-----...');

$out = '';
if (openssl_pkey_export($privateKey, $out)) {
    print $out;
}

Output in PHP 8.1 and up:

-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----

Which is the same as the output from the equivalent openssl command:

openssl pkcs8 -topk8 -nocrypt -in ec.key -out pkcs8.key

However, when running that same PHP code in PHP 7.4 and 8.0, I don't believe the output is in PKCS#8 format:

-----BEGIN EC PRIVATE KEY-----
...
-----END EC PRIVATE KEY-----

I can't find any PHP documentation that explains what changed in 8.1 for the openssl_pkey_export() function to behave differently.

How can I export the key in PKCS#8 format, in PHP 7.4?

Apologies if I've misunderstood something obvious here.

0

There are 0 best solutions below