def testAPKv2Signature(self): from androguard.core.bytecodes.apk import APK a = APK("examples/signing/TestActivity_signed_both.apk") self.assertTrue(a.is_signed_v1()) self.assertTrue(a.is_signed_v2()) self.assertTrue(a.is_signed()) # Signing name is maximal 8 chars... self.assertEqual(a.get_signature_name(), "META-INF/ANDROGUA.RSA") self.assertEqual(len(a.get_certificates_der_v2()), 1) # As we signed with the same certificate, both methods should return the # same content self.assertEqual(a.get_certificate_der(a.get_signature_name()), a.get_certificates_der_v2()[0]) from asn1crypto import x509 self.assertIsInstance(a.get_certificates_v2()[0], x509.Certificate) # Test if the certificate is also the same as on disk with open("examples/signing/certificate.der", "rb") as f: cert = f.read() cert_der_v1 = a.get_certificate_der(a.get_signature_name()) cert_der_v2 = a.get_certificates_der_v2()[0] for fun in [hashlib.md5, hashlib.sha1, hashlib.sha256, hashlib.sha512]: h1 = fun(cert).hexdigest() h2 = fun(cert_der_v1).hexdigest() h3 = fun(cert_der_v2).hexdigest() self.assertEqual(h1, h2) self.assertEqual(h1, h3) self.assertEqual(h2, h3)
def testAPKCertFingerprint(self): """ Test if certificates are correctly unpacked from the SignatureBlock files Check if fingerprints matches :return: """ from androguard.core.bytecodes.apk import APK import binascii from hashlib import md5, sha1, sha256 a = APK("examples/android/TestsAndroguard/bin/TestActivity.apk", skip_analysis=True) # this one is not signed v2, it is v1 only self.assertTrue(a.is_signed_v1()) self.assertFalse(a.is_signed_v2()) self.assertTrue(a.is_signed()) self.assertEqual(a.get_certificates_der_v2(), []) self.assertEqual(a.get_certificates_v2(), []) self.assertEqual(a.get_signature_name(), "META-INF/CERT.RSA") self.assertEqual(a.get_signature_names(), ["META-INF/CERT.RSA"]) cert = a.get_certificate(a.get_signature_name()) cert_der = a.get_certificate_der(a.get_signature_name()) # Keytool are the hashes collected by keytool -printcert -file CERT.RSA for h2, keytool in [(md5, "99:FF:FC:37:D3:64:87:DD:BA:AB:F1:7F:94:59:89:B5"), (sha1, "1E:0B:E4:01:F9:34:60:E0:8D:89:A3:EF:6E:27:25:55:6B:E1:D1:6B"), (sha256, "6F:5C:31:60:8F:1F:9E:28:5E:B6:34:3C:7C:8A:F0:7D:E8:1C:1F:B2:14:8B:53:49:BE:C9:06:44:41:44:57:6D")]: x = h2() x.update(cert_der) hash_hashlib = x.hexdigest() self.assertEqual(hash_hashlib.lower(), keytool.replace(":", "").lower())