Search code examples
javacryptographybouncycastle

BouncyCastle: Extract public key informationfrom Certificate Signing Request


I am using Bouncy Castle library in Java for reading CSR. I need to extract the public key information from CSR. I can see that openssl is able to extract required information from CSR.

I can't find any way to do this in BouncyCastle. I have been able to read PKCS10CertificationRequest object from the CSR. I have seen examples using SubjectPublicKeyInfo for extracting public key. But the code relies on the fact that algorithm of public key is already known. I can do a "instanceof" operation for various algorithm parameters and match but I think there would be something better. I want to derive the algorithm from CSR itself. I tried to find this information but couldn't find anything related to this. Thanks for help.


Solution

  • Solution is to create a new wrapper around the PKCS10CertificateRequest like this:

     JcaPKCS10CertificationRequest jcaCertRequest =
            new JcaPKCS10CertificationRequest(pkcs10CertRequest.getEncoded()).setProvider("BC");
    

    This class has the getPublicKey() method.

     PublicKey publicKey = jcaCertRequest.getPublicKey();