X509CertUtils.parse gives null for DER encoded certificate

Issue #488 invalid
Sreeni K created an issue

I followed the example (https://connect2id.com/products/nimbus-jose-jwt/examples/parse-x509-certificate-extract-keys)) and found that we can use X509CertUtils.parse DER cert and then we can extract public key. But i'm getting cert as null and so i used X509CertUtils .parseWithException to know the actual issue. This is the exception im getting:

X509CertUtils.parseWithException(encodedCert);
java.security.cert.CertificateException: Unable to initialize, java.io.IOException: Short read of DER length

But if i use plain java code to get the public key with the same DER cert, im not getting any issues. I’m able to get public key:

X509EncodedKeySpec specpub = new X509EncodedKeySpec(encodedCert);
KeyFactory kfpub = null;
try {
kfpub = KeyFactory.getInstance("EC");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
PublicKey publicKey = null;
try {
publicKey = kfpub.generatePublic(specpub);
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}

When i look at the source code X509CertUtils .parseWithException method, i found that its using CertificateFactory.getInstance("X.509") instead of “EC”. I'm not sure if its really the issue.

Providing more details (if it helps), below are commands used to generate pub/private keys:

openssl ecparam -name prime256v1 -genkey -noout -out private.ec.key
openssl pkcs8 -topk8 -in private.ec.key -outform DER -out myProxy_key.der -nocrypt
openssl ec -in private.ec.key -pubout -outform DER -out myProxy_pubKey.der

Comments (1)

  1. Vladimir Dzhuvinov

    The library doesn't have a utility for parsing DER encoded keys at present.

    It appears you tried using the utility for parsing X.509 certificates to parse a DER encoded EC key, which are different things, hence the error you got.

  2. Log in to comment