Extract raw X.509 Certificate from a signed APK or JAR
Asked Answered
C

2

12

I have a library of MD5 hashes of public keys used to sign various jars, and a mapping to their respective keystores which we use to sign different APKs. What I'd like to be able to do is identify which keystore was used to sign an APK, but without using trial and error. (Also, sadly, many of our keys share similar or identical DNs.)

My solution, because I know the META-INF/FOO.RSA (or FOO.DSA) contains the certificate, was to extract the certificate from the APK's RSA file and directly calculate the MD5 hash. (I know the certificate is there because it is accessible to a running android application, and the jarsigner documentation tells me it is there.)

But I can't find any tool that gives me the actual bytes of the certificate. I can get the DN and the certificate metadata when I use jarsigner -verbose -verify -certs my.apk, but that doesn't give me the bytes.

Conto answered 3/12, 2012 at 22:29 Comment(0)
U
28

Extract the JAR then use 'openssl' to output the certificate:

So assuming 'foo.jar' is in your current directory, do something like:

mkdir temp
cd temp
jar -xvf ../foo.jar
cd META-INF
openssl pkcs7 -in FOO.RSA -print_certs -inform DER -out foo.cer
Unscrew answered 22/3, 2013 at 17:50 Comment(1)
On Windows, it’s easier to rename the FOO.RSA to FOO.p7b and double-click it. Windows has built-in tool to read PKCS #7 file.Heddy
H
1

Hexdump FOO.RSA. The last n bytes are the signature itself, where n depends on the key length (e.g., 1024 bit RSA). If you sign something twice with the same key, you can diff the .RSA files and see that only the last n bytes change; the static part of the file is the cert and the bits that change are the signature on the hash of FOO.sf. There may be a delimiter between the cert and signature that you'd also have to remove.

Hellhound answered 19/2, 2013 at 2:18 Comment(1)
Thanks so much! I just got a script working. For those who are curious, the .RSA file appears to have the following pattern: * 54 bytes of unknown information * a 2 byte short (big-endian) with the length of the upcoming certificate * the n bytes of the certificate * unknownConto

© 2022 - 2024 — McMap. All rights reserved.