Pergunta

Hi, all! My problem is next: I have the .pkcs7 file which consists pre-packed CSR and I want to get CSR from it. How can I do it using bouncycastle?

I tried use PEMReader, but it didn't work.


    private void getCertificatesPKCS7File(String filename){
        try {
            certificates = new ArrayList();

            FileReader fileReader = new FileReader(filename);
            PEMReader pemReader = new PEMReader(fileReader);
           // Object obj = pemReader.readObject();
           // PKCS10CertificationRequest csr = (PKCS10CertificationRequest) obj;
            Object obj = pemReader.readPemObject().getContent();
            PKCS10CertificationRequest csr = new PKCS10CertificationRequest(obj);

            pemReader.close(); 

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Exception is next:


Exception in thread "main" java.lang.IllegalArgumentException: unknown object in factory: org.bouncycastle.asn1.ASN1ObjectIdentifier
    at org.bouncycastle.asn1.pkcs.CertificationRequestInfo.getInstance(Unknown Source)
    at org.bouncycastle.asn1.pkcs.CertificationRequest.(Unknown Source)
    at org.bouncycastle.jce.PKCS10CertificationRequest.(Unknown Source)

Thank's all!

Foi útil?

Solução

I found solution.

KeyStore keystore = KeyStore.getInstance("PKCS12", "BC");
keystore.load (new FileInputStream(PATH+"//test.p12"), "testpassword".toCharArray());
PrivateKey privateKey = (PrivateKey)keystore.getKey("testclientcert", "testpassword".toCharArray());

PEMReader pemReader = new PEMReader(new FileReader(filename));
ContentInfo object = (ContentInfo)pemReader.readObject();

CMSEnvelopedDataParser envDataParser = new CMSEnvelopedDataParser(object.getEncoded());
RecipientInformationStore recipients = envDataParser.getRecipientInfos();
Collection envCollection = recipients.getRecipients();
Iterator it = envCollection.iterator();
RecipientInformation recipient = (RecipientInformation) it.next();
byte[] result = recipient.getContent(privateKey, "BC");
String base64Encoded = new String(Base64.encode(result));

System.out.println(base64Encoded);

base64Encoded will be match with encoded csr.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top