-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
JcaPGPKeyConverter cannot properly convert EC keys #1662
Comments
Some more insights: If instead, the keys are freshly generated, the conversion method succeeds. One more symptom is, that freshly generated keys do have a different object type in the |
It turns out, that |
This results in the difference in encoding length I suppose. |
I fixed this in #1671 |
So the behavior of BCECPublicKey is correct - the X9.62 definition of the key encoding, which is what the straight ASN.1 is based on means the id_ecPublicKey is the algorithm associated with the key, the algorithm parameters associated with the key are where the curve details are. This is the definition for key encoding to be used for X.509. There's three possibilities for this, explicit, implicit, named. Usually you will see named, which means the parameters will contain an object identifier giving the Curve OID, which is what PGP wants in it's encoding. Explicit and implicit contain actual curve parameters, in many cases these will still match a named curve, but sometimes they won't. I doubt you'd run into these, but it's worth being aware of the possibility. Given that's how the universe is, does this mean this PR #1671 is correct? (It looks like it is, but I thought it worth clearing up the encoding issue just to make sure first). The main thing it needs to recognize is the converter is really translating between 2 very different ways of encoding. |
I see. I'm currently reading up on X9.62 (I found https://safecurves.cr.yp.to/grouper.ieee.org/groups/1363/private/x9-62-09-20-98.pdf which appears to be the proper document?) to get a better picture. From what I can say so far, we should document that 7ae4dbd#diff-4c679c2a6587b4244471fa022bebd469e7c66e5cb34ffda6436f0d139d78f35dR688 does lookup the named curve OID for an explicitly encoded key and remove the misleading comments I added. |
The document is close enough. Yes, I think that'll do. |
JcaPGPKeyConverter
does not properly decode elliptic curve keys inprivate BCPGKey getPublicBCPGKey(int algorithm, PGPAlgorithmParameters algorithmParameters, PublicKey pubKey)
.The following lines decode a different OID than the original key has, which then causes the curve table lookup to fail.
https://github.com/bcgit/bc-java/blob/main/pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcaPGPKeyConverter.java#L482-L485
My guess would be that either
SubjectPublicKeyInfo.getInstance()
is buggy or there is an issue inBCECPublicKey.getEncoded()
.Here is a reproducer.
The same conversion method also fails for EC keys over the
prime256v1
,secp384r1
,secp521r1
,brainpool384r1
,brainpool521r1
curves.The text was updated successfully, but these errors were encountered: