-
-
Notifications
You must be signed in to change notification settings - Fork 603
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
Require minimum key size for HMAC algorithm #835
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The diff below represents a way to be sure that:
- Signers provide key validation
- Key are actually validated against it
Everything here can easily be backported in v4
implementations, waiting v5
to updated Signer
interface
I need to think more about this but I think this is not the way to go. I'll review it on the laptop later today |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this PR is going way beyond to solve the issue and by doing so creates an API that doesn't make sense to me (e.g. Key#sign()
).
We need to rethink this.
@lcobucci new day, new RFC |
Ping 😳 |
Hi, I see some possibilities on Signer side that could solve this:
Note that I have not tested these solutions. WDYT? |
I believe that introducing an interface to handle the key validation generically might create more confusion on the usage.
I think that's the way I'd go, but I'd leverage the capabilities of abstract classes instead of overriding abstract class Hmac implements Signer
{
final public function sign(string $payload, Key $key): string
{
$this->guardAgainstWeakKeys($key);
return hash_hmac($this->algorithm(), $payload, $key->contents(), true);
}
private function guardAgainstWeakKeys(Key $key): void
{
if (mb_strlen($key->contents(), '8bit') >= $this->minimumLengthForKeys()) {
return;
}
throw new InvalidKeyProvided();
}
abstract public function minimumLengthForKeys(): int;
final public function verify(string $expected, string $payload, Key $key): bool
{
return hash_equals($expected, $this->sign($payload, $key));
}
abstract public function algorithm(): string;
} Then, we need to check the trade-offs on the migration path (do we want to introduce classes on the side and mark the existing ones as deprecated, do we want to trigger deprecation errors instead of exceptions on v4.x, how do we avoid breaking BC on the abstract class, ...). |
1f7f5da
to
f04327e
Compare
f04327e
to
e237e5d
Compare
Considering that the current state of art is both out of standard and unsafe, my last push proposes the following:
After upgrading to the new release:
As far as I can tell, this approach is both backward compatible and caring for security of end users. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is behavioural BC-break, introducing the old behaviour on the side might be alright - especially because people using decent keys should not be affected - but we must document this.
@Slamdunk can you modify the docs to mention HMAC key length requirements?
f2b0227
to
3765712
Compare
@lcobucci I'm sorry I lost where we were here |
3765712
to
7d5b651
Compare
- the new minor version of lcobucci/jwt library requires the longer keys - see /~https://github.com/shopsys/shopsys/actions/runs/3435339685/jobs/5727764082 - see lcobucci/jwt#835
- the new minor version of lcobucci/jwt library requires the longer keys - see /~https://github.com/shopsys/shopsys/actions/runs/3435339685/jobs/5727764082 - see lcobucci/jwt#835
- the new minor version of lcobucci/jwt library requires the longer keys - see /~https://github.com/shopsys/shopsys/actions/runs/3435339685/jobs/5727764082 - see lcobucci/jwt#835
Hi, as reported to us by @Spomky this library currently lacks a key security requirement of JWT standard reported here: https://www.rfc-editor.org/rfc/rfc7518.html#section-3.2
Current version of
lcobucci/jwt
allows indeed any weak key like1234
to be used for HMAC, for example.While this issue will hopefully be resolved for future algorithms by low level APIs (see
sodium_crypto_sign_detached
andsodium_crypto_generichash
implementations) I think we should design this library to require any current and future Signer to provide and apply explicit Key validation.I'm throwing here a PR because, you know, it may be easier to fix something rather than creating it from scratch.
Comments on the code will follow to explain it.
BC-Break
is intentional even though I know a MAJOR bump is a blocker: I'd like to shape it the correct way, and only after that adapt it to the currentv4
branch.