1

I'm working on a project and I have to read a .crt certificate in my java code. The certificate is DER encoded, and not PEM encoded.

I'm reading the certificate like this:

byte[] issuer = GET_CERT();

InputStream in = new ByteArrayInputStream(issuer);

X509Certificate issuerCert = null;
try{
    issuerCert = (X509Certificate) cf.generateCertificate(in);
}catch(CertificateException ce){
    LOG.error("Error generating certificate from issuer certificate bytearray");
    LOG.error(ce.getMessage());
    throw new CertificateOperationException("Error generating certificate from issuer certificate");
}

I always get an CertificateException.

Can somebody please help me?

ERROR:

be.ovb.ccbe.fal2.service.exceptions.CertificateOperationException: Error generating certificate from issuer certificate bytearray in BEIDValidator.getIssuerCert
at be.ovb.ccbe.fal2.service.validation.BEIDValidator.getIssuerCert(BEIDValidator.java:100)
at be.ovb.ccbe.fal2.service.validation.BEIDValidator.validate(BEIDValidator.java:75)
at be.ovb.ccbe.fal2.service.BEIDvalidatorTest.testValid(BEIDvalidatorTest.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
jww
  • 97,681
  • 90
  • 411
  • 885
user3472577
  • 91
  • 1
  • 1
  • 4
  • Add the exception stack trace for a start. – Oleg Estekhin Mar 28 '14 at 13:29
  • DER encoding is binary, while PEM is base64 encoded (with a `-----BEGIN CERT-----` and friends). What do you have? – jww Mar 29 '14 at 05:52
  • i have a DER encoded file. And java can't generate a x509Certificate object. That's the error. – user3472577 Mar 31 '14 at 08:08
  • Have you tried converting the certificate? http://www.gtopia.org/blog/2010/02/der-vs-crt-vs-cer-vs-pem-certificates/ – Byron Mar 31 '14 at 09:58
  • You supplied the stack trace of the `CertificateOperationException` thrown by your code. Please instead provide the stack trace of the `CertificateException ce` you caught there. – mkl Mar 31 '14 at 10:29
  • possible duplicate of [Generate X509Certificate from byte\[\]?](http://stackoverflow.com/questions/3389143/generate-x509certificate-from-byte) – jww Apr 01 '14 at 05:25
  • possible duplicate of [Convert der to pem through bouncy castle library](https://stackoverflow.com/questions/13565532/convert-der-to-pem-through-bouncy-castle-library) – jww Apr 01 '14 at 05:28

0 Answers0