PHP cURL fails with error 58 when it loads a client key
cURL error 58: unable to set private key file: 'key.pem' type PEMShort 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).
Fix it fast
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; }2Match what you see, then jump to its fix
- 1
The 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
The key was created with a passphraseThe key is encrypted and no passphrase is givenFix - 3
The key file is binary, or comes as .der, .p12 or .pfxThe key is not PEM, but cURL is told nothingFix
- 1
What does the error look like?
cURL error 58: unable to set private key file: 'key.pem' type PEMcurl_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.key2Fix
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.phpConfirmed by curl.se
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.key2Fix
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.phpConfirmed by php.net
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.key2Fix
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.phpConfirmed by php.net
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
PHP cURL error 77 when Laravel or Guzzle calls an HTTPS API
CURLE_SSL_CACERT_BADFILE
Error 77 means libcurl could not read the CA certificate file it was told to use.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix