How to check PEM format certificate in Java

I have a PEM format file. How to check Java signature as I followed http://download.oracle.com/javase/tutorial/security/apisign/versig.html but found that Java does not support PEM

+3
source share
1 answer

You can read the certificate in a PEM file using BouncyCastle PEMReader . If the content is an X.509 certificate, you should get an instance X509Certificateand verify it as you want.

EDIT . Here is the code (I haven’t tried it):

// The key with which you want to verify the cert.
// This is probably a CA certificate public key.
PublicKey publicKey = ...;

PEMReader reader = new PEMReader(new FileReader("/path/to/file.pem"));
Object pemObject = reader.readObject();
if (pemObject instanceof X509Certificate) {
    X509Certificate cert = (X509Certificate)pemObject;
    cert.checkValidity(); // to check it valid in time
    cert.verify(publicKey); // verify the sig. using the issuer public key
}

(Of course, as with any I / O, you need to close the reader, perhaps with try / finally.)

, checkValidity verify : , .

+1

All Articles