-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpem_hackparse.py
executable file
·48 lines (40 loc) · 1.46 KB
/
pem_hackparse.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
#!/usr/bin/env python
# Like hackparse, but instead of recursively sucking in .results files from
# some directory, it reads PEM-encoded cert files from the command line
import hackparse
import openssl_dump as od
import sys
from subprocess import Popen, PIPE
MAGIC_ERROR= "unable to load certificate"
print od.MOZ_VERIFY_ARGS
def main():
args = hackparse.process_args()
for f in args[1:]:
fobj = open(f, "rb")
cert = fobj.read()
fobj.close()
print "Hackparsing " + f
a = Popen(od.OPENSSL_ARGS, stdin=PIPE, stdout=PIPE, stderr=PIPE)
try: pcert, err = a.communicate(cert)
except: err = MAGIC_ERROR
if err.startswith(MAGIC_ERROR):
a = Popen(od.DER_ARGS, stdin=PIPE, stdout=PIPE, stderr=PIPE)
try:
pcert, err = a.communicate(cert)
t = '-----BEGIN CERTIFICATE-----\n'
t += pcert.encode('base64')
pcert = t + '-----END CERTIFICATE-----\n'
except:
sys.stderr.write("WHACKO ERROR on %s\n" %f)
continue
if err.startswith(MAGIC_ERROR):
sys.stderr.write("failed to load: %s\n" % f)
continue
text, fp = od.opensslParseOneCert(pcert)
moz_verifications = od.verifyCertChain([text], od.MOZ_VERIFY_ARGS)
ms_verifications = od.verifyCertChain([text], od.MS_VERIFY_ARGS)
verifications = zip(moz_verifications, ms_verifications)
hackparse.add_cert_to_db(f, verifications, [text], [fp])
print "SUCCESS ON", f
if __name__ == "__main__":
main()