Skip to content

Conversation

@sammarshallou
Copy link
Contributor

The function openssl_get_privatekey may return false, for example if the passphrase is incorrect. In the other 2 branches of the switch statement, this is checked and an exception thrown, but it was not checked in the private key branch. This commit adds a check.

This makes it easier to detect problems with invalid keys or pass phrases. Without this change, you are likely to get a confusing exception later on when it tries to use the key.


case 'private':
$this->key = openssl_get_privatekey($this->key, $this->passphrase);
if (! $this->key) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be as strict & specific as you can and use === false instead

@sammarshallou sammarshallou force-pushed the detect-private-key-load-failures branch from 8df2e9e to 30f7d6c Compare May 11, 2023 13:26
@sammarshallou
Copy link
Contributor Author

@tvdijen Thanks for review, I've updated as requested.

@skaylink-AndreasUlm
Copy link

Hi,
would it make sense to inject the output of openssl_error_string() into exception message to allow understanding why it failed?

The function openssl_get_privatekey may return false, for example
if the passphrase is incorrect. In the other 2 branches of the
switch statement, this is checked and an exception thrown, but
it was not checked in the private key branch. This commit adds
a check.

This makes it easier to detect problems with invalid keys or pass
phrases. Without this change, you are likely to get a confusing
exception later on when it tries to use the key.
@sammarshallou sammarshallou force-pushed the detect-private-key-load-failures branch from 30f7d6c to 35967f8 Compare June 15, 2023 15:26
@sammarshallou
Copy link
Contributor Author

@skaylink-AndreasUlm (Sorry for the slow response.) Yes I agree that's a good suggestion - I've altered the code. According to the documentation, you should call openssl_error_string repeatedly in case there are multiple errors, but none of the other similar lines in the file do that, so I guess it's OK.

@robrichards robrichards merged commit 158c735 into robrichards:master Jul 17, 2023
@tvdijen
Copy link
Contributor

tvdijen commented Jul 17, 2023

I can tell you from experience that it is really useful for debugging if you loop through the errors. It's usually the first one you need, not the last one in the chain.

@dalin-
Copy link

dalin- commented Dec 4, 2024

This commit was rolled back in
2bdfd74

If you experience the exception

Exception encountered during processing SAML authentication response: Unable to extract private key (invalid key or passphrase): error:0909006C:PEM routines:get_name:no start line

Then simply composer update robrichards/xmlseclibs to get at least version 3.1.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants