Content · Web Development · PHP

PHP openssl_spki_export() Function: Usage and Examples

openssl_spki_export extracts the public key in PEM form from a verified or supplied legacy SPKAC.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

openssl_spki_export extracts the public key in PEM form from a verified or supplied legacy SPKAC structure.

The examples explain the data format, expected results and the difference between this API and nearby cryptographic operations.

What It Does


The operation is intended for: openssl_spki_export extracts the public key in PEM form from a verified or supplied legacy SPKAC structure.

The OpenSSL extension must be enabled.

Syntax and Return Value


openssl_spki_export(string $spki): string|false
ParameterPurpose
$spki (string)Use the PHP signature above to determine the exact expected input type; validate untrusted inputs before calling.

The declared return type is shown above. Errors can include false results or exceptions depending on this API: SPKAC is a legacy format. An SPKAC signature does not independently establish trust in the key owner.

Example 1: Basic Operation


SPKAC is legacy. Its signature is not proof of real-world identity without additional trust checks.

<?php
$private = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_bits' => 2048]);
if ($private === false) { throw new RuntimeException('Key generation failed'); }
$details = openssl_pkey_get_details($private);
$public = $details['key'];
$csr = openssl_csr_new(['commonName' => 'test.example'], $private, ['digest_alg' => 'sha256']);
if ($csr === false) { throw new RuntimeException('CSR creation failed'); }
$cert = openssl_csr_sign($csr, null, $private, 1, ['digest_alg' => 'sha256']);
if ($cert === false) { throw new RuntimeException('Certificate creation failed'); }
$spkac = substr(openssl_spki_new($private, "sample-challenge", OPENSSL_ALGO_SHA256), 6);
$pub = openssl_spki_export($spkac);
var_dump(is_string($pub));

Example 2: A Different Use Case


SPKAC signatures cover the public-key challenge; changing the challenge means creating a new signed object.

<?php
// Check the alternative case.
$private = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_bits' => 2048]);
if ($private === false) { throw new RuntimeException('Key generation failed'); }
$details = openssl_pkey_get_details($private);
$public = $details['key'];
$csr = openssl_csr_new(['commonName' => 'test.example'], $private, ['digest_alg' => 'sha256']);
if ($csr === false) { throw new RuntimeException('CSR creation failed'); }
$cert = openssl_csr_sign($csr, null, $private, 1, ['digest_alg' => 'sha256']);
if ($cert === false) { throw new RuntimeException('Certificate creation failed'); }
$spkac = substr(openssl_spki_new($private, "new-test-challenge", OPENSSL_ALGO_SHA256), 6);
$pub = openssl_spki_export($spkac);
var_dump(is_string($pub));

Practical Example: Checking the Result


SPKAC signatures cover the public-key challenge; changing the challenge means creating a new signed object.

<?php
// Verify the expected behavior before using results.
$private = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_bits' => 2048]);
if ($private === false) { throw new RuntimeException('Key generation failed'); }
$details = openssl_pkey_get_details($private);
$public = $details['key'];
$csr = openssl_csr_new(['commonName' => 'test.example'], $private, ['digest_alg' => 'sha256']);
if ($csr === false) { throw new RuntimeException('CSR creation failed'); }
$cert = openssl_csr_sign($csr, null, $private, 1, ['digest_alg' => 'sha256']);
if ($cert === false) { throw new RuntimeException('Certificate creation failed'); }
$spkac = substr(openssl_spki_new($private, "third-test-challenge", OPENSSL_ALGO_SHA256), 6);
$pub = openssl_spki_export($spkac);
var_dump(is_string($pub));

Important Behavior and Edge Cases


SPKAC is a legacy format. An SPKAC signature does not independently establish trust in the key owner.

SPKAC is a legacy format. An SPKAC signature does not independently establish trust in the key owner.

Comparison with Related Functions


ApproachKey distinction
openssl_spki_exportopenssl_spki_export extracts the public key in PEM form from a verified or supplied legacy SPKAC structure.
Related techniqueModern key registration generally uses purpose-built protocols rather than SPKAC.

Common Beginner Mistakes


MistakeCorrect approach
Confusing this operation with a neighboring cryptographic or security mechanismModern key registration generally uses purpose-built protocols rather than SPKAC.
Ignoring a security prerequisite or failure caseSPKAC is a legacy format. An SPKAC signature does not independently establish trust in the key owner.

Key Takeaways


  • openssl_spki_export extracts the public key in PEM form from a verified or supplied legacy SPKAC structure.
  • SPKAC is a legacy format. An SPKAC signature does not independently establish trust in the key owner.
  • Modern key registration generally uses purpose-built protocols rather than SPKAC.

Apply this behavior to a real application only after confirming the required extension, configuration and failure handling on the deployment runtime.

Share this resourceSend this page to someone who may find it useful.