Closed rfool closed 3 years ago
I get that same error "Invalid server public key length". But seemingly random.
I forked the repo and applied the changes suggested by @rfool. I don't get the error anymore. But just like op I'm not an expert in this stuff and what implications these changes might have. Maybe someone with deeper knowledge could chime in.
Thank you @rfool, your suggested fix helped alot.
Hi,
It looks like you are right. I will create a PR to solve that. In case you are interesting, there is a PR where I propose to completely rebuild this library. I created an example that uses this new branch if you want to test it.
Available in 6.0.3, thanks people!
Setup
Problem
When
Encryption::createLocalKeyObjectUsingPurePhpMethod()
andUtils::serializePublicKeyFromJWK()
is used to create$localPublicKey
then this will result in a key (with binary size) slightly larger than the expected 65 bytes.Thus,
Encryption::createContext()
will throw with "Invalid server public key length".Expected
$localPublicKey
should have binary size of 65 bytes exactly.Features Used
Example / Reproduce Case
Should be reproducable with official example, when openssl is not correctly configured.
Other
Of course, the best fix is to configure openssl correctly. However,
Encryption::createLocalKeyObjectUsingPurePhpMethod()
exists and thus should provide correct results.Unfortunately, I am not an expert on elliptical curves, GMP, JWT and so on. But I have a guess: the error could be in the calls to
BigInteger::toBytes()
. By default it prepends a signed bit and represents the number in two's-complement. I think this sign-bit is responsible for enlarging key length.Current:
Suggested fix:
With this change naiively applied, the key lengths will be exactly 65 bytes, as expected.
However, I don't know if this "fix" is technically correct. As
x
andy
are coordinates, maybe justd
should be handled as unsigned?