Read out Ca Certificate from .RSA File in Java

91 views Asked by At

I want to verify a already signed .jar File. For this i need the public key which is stored in the certificate in the .rsa file. How do i get the certificate from the .rsa file which was generated with the jarsigner

2

There are 2 answers

0
dave_thompson_085 On

The META-INF/{signer}.RSA entry in a signed jar is a PKCS7 detached signature which among other things contains the certificate chain needed to verify the {signer}.SF entry and thereby (indirectly) the jar contents. If you have jarsigner you also have keytool -- which can read certs from a PKCS7, and can even find the PKCS7 in a signed jar, so just do keytool -printcert -jarfile whatever.jar -rfc to get all the certs (each) in PEM format. Save them somewhere like a file or the clipboard, and select the first cert (i.e. PEM block) from the 'Signature' group (not 'Timestamp', if present) with an editor or GUI or whatever. The result can be read with anything that accepts a PEM certificate; e.g. if you want to code in Java, do CertificateFactory.getInstance("X.509") then call .generateCertificate() on an InputStream that reads this file or data.

There are many other tools that handle PEM certs, which are offtopic here, and many libraries you can use in programs, which (probably) are, but far too many to fit in an SO answer.

Compare https://security.stackexchange.com/questions/178936/how-to-verify-sha256-fingerprint-of-apk (Android APK format is nearly the same as Java JAR) which obtains the certificates but only displays some human-readable info not the publickey; that's why I added -rfc in the command above.

However, having the cert (or pupblickey) is not enough to verify anything. You also need the 'SignerInfo' from the PKCS7, which may simply contain the (here RSA+hash) signature of the {signer}.SF entry, or it may contain the (again RSA+hash) signature or a 'signedAttributes' structure which in turn contains the digest of {signer}.SF and both must be verified. Those are harder. In addition the certificate should be validated to make sure it's not fake, or substituted (even if this was a jar you signed yourself, someone who tampers the contents can also tamper the signature to make it appear valid if you don't validate the certs). For that see CertificatePathValidator and I've seen existing Qs on it you could look at.

0
Emmanuel Bourg On

If you are on Windows, you can extract the .rsa file from the signed jar, change its extension to .p7b and double click on it to inspect the certificates stored inside.