-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcheck_keys.py
executable file
·62 lines (47 loc) · 1.46 KB
/
check_keys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2024 The LineageOS Project
# SPDX-License-Identifier: Apache-2.0
import glob
import subprocess
import sys
from multiprocessing import Pool
from cryptography import x509
KNOWN_KEYS = [
x509.load_pem_x509_certificate(open(f, "rb").read()).public_key()
for f in glob.glob("*.x509.pem")
]
def check_public_key(path: str) -> None:
certs = []
stdout = subprocess.run(
[
"java",
"-jar",
"../../../prebuilts/sdk/tools/linux/lib/apksigner.jar",
"verify",
"--print-certs-pem",
path,
],
capture_output=True,
).stdout
while begin := stdout.find(b"-----BEGIN CERTIFICATE-----"):
end = stdout.find(b"-----END CERTIFICATE-----", begin)
if end == -1:
break
certs.append(x509.load_pem_x509_certificate(stdout[begin : end + 25]))
stdout = stdout[end + 25 :]
if not any(x.public_key() in KNOWN_KEYS for x in certs):
print(path, "is signed with an unknown key!")
def main():
out = sys.argv[1]
with Pool(8) as pool:
pool.map(
check_public_key,
(
glob.glob(f"{out}/obj/**/*.apk", recursive=True)
+ glob.glob(f"{out}/obj/**/*.apex", recursive=True)
+ glob.glob(f"{out}/obj/**/*.capex", recursive=True)
),
)
if __name__ == "__main__":
main()