Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more RSA signing algorithms to the JWT Util #49

Merged
merged 2 commits into from
Jan 28, 2013
Merged

Added more RSA signing algorithms to the JWT Util #49

merged 2 commits into from
Jan 28, 2013

Conversation

F21
Copy link
Contributor

@F21 F21 commented Jan 25, 2013

Just a quick update to the JWT Util to support more RSA algorithms.

@stefanaxelsson
Copy link
Contributor

Wouldn't a try/catch-approach be better than silencing the error?

@F21
Copy link
Contributor Author

F21 commented Jan 25, 2013

Unfortunately, an exception is not thrown. A warning is raised, so even wrapping the code in a try catch block will still cause the warning to be outputted.

The warning I am talking about is something like: supplied key param cannot be coerced into a public key. This happens when the public or private key provided is not in the correct format.

In any case, we do not really care whether the public key is valid or not, only the fact that we can verify the signature is important.

See this example code for the warning being thrown even though we have wrapped it in a try-catch:

var_dump(openssl_verify('123456', '123456', 123456, 'sha256'));  //Causes a warning to be raised. returns false.

try {
   var_dump(openssl_verify('123456', '123456', 123456, 'sha256')); //Also causes a warning to be raised. returns false.
} catch (Exception $e) {
    var_dump($e); //never executed
}

var_dump(@openssl_verify('123456', '123456', 123456, 'sha256')); //No warning, returns false.

I was never a fan of using @ and use it very rarely in my applications, but I think in this case, it is appropriate as it may be beyond the scope of the library to set a set_error_handler().

@stefanaxelsson
Copy link
Contributor

I can't think of any library that has this issue, so I have nothing to compare it with. One solution could be:

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");

try {
    var_dump(openssl_verify('123456', '123456', 123456, 'sha256'));
} catch (Exception $e) {
    var_dump($e); 
}

restore_error_handler();

@F21
Copy link
Contributor Author

F21 commented Jan 25, 2013

That sounds like a possible solution as well :) However, perhaps it might be a bit overkill just to avoid the @, as a warning in this case would just return a false which is what we need.

Anyone else have any thoughts on this?

@bshaffer
Copy link
Owner

Please explain the situation in which these functions would throw an error

@F21
Copy link
Contributor Author

F21 commented Jan 26, 2013

@bshaffer, when an invalid key is passed to openssl_verify, a warning will be thrown.

It expects keys in this format:

-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC/yMgbkPnOnrXt
....
Fnh zeEVijg18pMvVScrBw==
-----END PRIVATE KEY-----

If for whatever reason, a corrupted version was stored by the storage driver or the key is tampered with, then a warning will be issued. In my previous comment, I passed 123456 to the function as the $key, which is invalid. This resulted in the openssl_verify function throwing a supplied key param cannot be coerced into a public key warning.

@bshaffer
Copy link
Owner

Is there a way to verify the input before calling the function? I agree we do not want to output php errors in this class, but squelching them is also not optimal

@F21
Copy link
Contributor Author

F21 commented Jan 27, 2013

I just had a look at the openssl extension and it does not seem to provide any means to do so :(

@bshaffer
Copy link
Owner

Yes, it does seem squelching is the only way to handle this, unfortunately. I would suggest "decode" return false on failure however, rather than throw an Exception.

bshaffer added a commit that referenced this pull request Jan 28, 2013
Added more RSA signing algorithms to the JWT Util
@bshaffer bshaffer merged commit e7e97c8 into bshaffer:develop Jan 28, 2013
@F21
Copy link
Contributor Author

F21 commented Jan 28, 2013

That's a good point :) I will get a pull out soon :)

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.

None yet

3 participants