-
Notifications
You must be signed in to change notification settings - Fork 7
/
ConvertStreamToPem.py
executable file
·51 lines (46 loc) · 1.48 KB
/
ConvertStreamToPem.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
#!/usr/bin/env python
from scan.TLSConstructs import *
import os, sys, os.path
import traceback
USAGE = "Please pass in file stream file convert --> pem certs.\n"
out_dir = 'certs'
try:
os.mkdir (out_dir)
except:
pass
def convert(in_name):
try:
f = open(in_name, 'rb')
data = f.read()
f.close
count = 0
recs = TLSRecord.parse(data)
certs = []
for rec in recs:
if rec.ContentType == 'handshake':
handshake_messages = Handshake.parse(rec.data)
for hand_msg in handshake_messages:
if hand_msg.msg_type == 'certificate':
cert_msg = Certificate.parse(hand_msg.body)
data_read = 0
while data_read < cert_msg.list_length:
cert = ASNCert.parse(cert_msg.list_data[data_read:])
data_read += cert.cert_length + 3
certs.append(cert)
for certificate in certs:
try:
count = count + 1
out = open("%s%s%s_%d.pem" % (out_dir, os.sep, os.path.basename(in_name), count), 'w')
out.write('-----BEGIN CERTIFICATE-----\n')
out.write(certificate.cert.encode('base64'))
out.write('-----END CERTIFICATE-----\n')
except:
traceback.print_exc()
print "error with certificate %d in %s" % (count, in_name)
except:
print "Error attempting to process", in_name
if __name__ == "__main__":
if len(sys.argv) < 2:
print USAGE
else:
convert(sys.argv[1])