Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP cURL fails with error 58 when it loads a client key

ErrorcURL error 58: unable to set private key file: 'key.pem' type PEM

Short answer

Error 58 is cURL's code for a problem with the local client certificate. With the message unable to set private key file, the key you gave to CURLOPT_SSLKEY could not be loaded: the path is wrong, the key needs a passphrase you did not supply, or the file is not in the format cURL expects (PEM by default).

  • Applies to PHP 8.3, PHP 8.4, libcurl 8.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the error number and message, and the exact paths you pass, so you see which file cURL could not load.

    if (curl_exec($ch) === false) {
        echo curl_errno($ch), ' ', curl_error($ch), PHP_EOL;
    }
  2. 2Match what you see, then jump to its fix

    1. 1The file name quoted in the message is not a path that exists for the user running PHPThe key path is wrong, relative or unreadableFix
    2. 2The key was created with a passphraseThe key is encrypted and no passphrase is givenFix
    3. 3The key file is binary, or comes as .der, .p12 or .pfxThe key is not PEM, but cURL is told nothingFix

What does the error look like?

  • cURL error 58: unable to set private key file: 'key.pem' type PEM
  • curl_errno() returns 58

Cause 1 of 3

The key path is wrong, relative or unreadable

1Check

CURLOPT_SSLKEY takes the name of a file containing the private key. Confirm that exact file exists, and that the user PHP runs as can open it.

ls -l /etc/ssl/client/client.key
sudo -u www-data head -c 30 /etc/ssl/client/client.key

2Fix

Use the full path to the key and let the user that runs PHP read the file.

curl_setopt($ch, CURLOPT_SSLCERT, '/etc/ssl/client/client.crt');
curl_setopt($ch, CURLOPT_SSLKEY,  '/etc/ssl/client/client.key');

3Verify

Run the request again; error 58 should be gone.

php call-api.php

Confirmed by curl.se

Did this fix it?

Cause 2 of 3

The key is encrypted and no passphrase is given

1Check

cURL never needs a passphrase to load a certificate, but needs one to load a passphrase-protected private key. PHP's CURLOPT_SSLKEYPASSWD is the secret password needed to use the key set in CURLOPT_SSLKEY.

grep -c ENCRYPTED /etc/ssl/client/client.key

2Fix

Pass the passphrase with CURLOPT_SSLKEYPASSWD, read from the environment.

curl_setopt($ch, CURLOPT_SSLKEYPASSWD, getenv('CLIENT_KEY_PASSPHRASE'));

3Verify

The request now goes through the handshake.

php call-api.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The key is not PEM, but cURL is told nothing

1Check

PHP's CURLOPT_SSLKEYTYPE supports PEM, DER and ENG and defaults to PEM. CURLOPT_SSLCERTTYPE also accepts P12 (PKCS#12 files).

file /etc/ssl/client/client.key

2Fix

Declare the real format of each file, or convert the files to PEM first.

curl_setopt($ch, CURLOPT_SSLKEY, '/etc/ssl/client/client.der');
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'DER');

3Verify

The handshake completes and the request returns a response.

php call-api.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Keep client certificates and keys at full paths, readable only by the service user.
  • Read passphrases from the environment or a secret store, never from the code.
  • Log curl_errno() and curl_error() together, so a code 58 does not turn into a blank failure.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.