Load certificate file into Certificate Object

牧云@^-^@ 提交于 2019-12-05 19:01:27

The problem is that the CertificateFactory only reads a certificate in PEM format if it starts with -----BEGIN CERTIFICATE----- straight away. Some tools add extra information (here, the result of openssl x509 -text) first, but the certificate factory doesn't ignore it and treat it as a badly formed certificate.

Instead, use a BuffedReader and readLine() to read your file, ignoring any line until you get to -----BEGIN CERTIFICATE-----. Then, add all the lines until -----END CERTIFICATE----- to a temporary string variable (or similar, e.g. StringBuilder). Pass this to the CertificateFactory.

It looks to me like your certificate file may not be in the correct format.

The documentation for CertificateFactory.generateCertificates says,

In the case of a certificate factory for X.509 certificates, the certificate provided in inStream must be DER-encoded and may be supplied in binary or printable (Base64) encoding. If the certificate is provided in Base64 encoding, it must be bounded at the beginning by -----BEGIN CERTIFICATE-----, and must be bounded at the end by -----END CERTIFICATE-----.

I don't believe that the problem is as simple as adding the boundary markers to your existing certificate.

I've only ever used PEM format, which is base-64 encoded DER, so I don't know for sure that yours is the wrong format, but I'm guessing that a binary DER-encoded certificate is not human-readable text.

So, I'd suggest that you go back to the source certificate, and make sure that you get a copy with the correct format. If you have a different format for the original cert, you can convert it to pem format with openssl.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!