forked from freddy36/StartSSL_API
-
Notifications
You must be signed in to change notification settings - Fork 2
/
startssl_getcerts.py
executable file
·86 lines (58 loc) · 2.88 KB
/
startssl_getcerts.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python
import subprocess, re, datetime, json, os.path, sys
scriptPath = os.path.dirname(os.path.realpath(sys.argv[0]))
execfile(scriptPath+"/config.py")
with open(scriptPath+'/startssl_cookie.txt', 'r') as infile:
cookie = infile.read()
def main():
if len(sys.argv) == 2 and sys.argv[1].isdigit():
download_cert(sys.argv[1])
sys.exit()
if os.path.isfile("cert_list.json") and len(sys.argv) == 1:
print "NOTE: this is a cached version"
print_cert_list()
sys.exit()
update_cert_list()
print_cert_list()
def print_cert_list():
with open('cert_list.json', 'r') as infile:
certs = json.loads(infile.read())
print "New | Identifier | Common Name | Profile | Class | Expiry Date |"
FORMAT = " {new:<2s} | {id:10d} | {name:<30s}| {profile:<8s}| {class:<10s}| {expires} |"
for cert in certs:
print FORMAT.format(**cert)
def download_cert(identifier):
fetch_command = "curl -b \"%s\" -d app=12 -d rs=set_toolbox_item -d 'rsargs[]=crt' -d 'rsargs[]=%s' -s \"%s\"" % (cookie, identifier, STARTSSL_BASEURI)
output = subprocess.check_output(fetch_command, shell=True)
REQUEST_CERTIFICATE_CERT = re.compile('<textarea.*?>(?P<certificate>.*?)</textarea>')
m = REQUEST_CERTIFICATE_CERT.search(output)
if m:
cert = m.group("certificate").replace("\\n", "\n")
def update_cert_list():
RETRIEVE_CERTIFICATE_LIST = re.compile(
'<option value=\\\\"(?P<id>\d+)\\\\" style=\\\\"background-color: #(?P<color>[0-9A-F]{6});\\\\">(?P<name>.+?) \((?P<profile_description>[\w/]+?) - (?P<class>[\w\d ]+?) - (?P<expires_year>\d{4})-(?P<expires_month>\d{2})-(?P<expires_day>\d{2})\)</option>',
re.UNICODE)
#curl -b $(cat startssl_cookie.txt) https://www.startssl.com -d app=12 -d rs=set_toolbox_item -d 'rsargs[]=crt' -iv
auth_command = "curl -b \"%s\" -d app=12 -d rs=set_toolbox_item -d 'rsargs[]=crt' -s \"%s\"" % (cookie, STARTSSL_BASEURI)
output = subprocess.check_output(auth_command, shell=True)
items = RETRIEVE_CERTIFICATE_LIST.finditer(output)
certs = []
for item in items:
cert = item.groupdict()
# convert expire date
cert['expires'] = str(datetime.date(int(cert['expires_year']), int(cert['expires_month']), int(cert['expires_day'])))
# convert id to integer
cert['id'] = int(cert['id'])
cert['profile'] = cert['profile_description']
# set retrieved state depending on the background color
if cert['color'] == "FFFFFF":
cert['retrieved'] = True
cert['new'] = ''
else: # if color = rgb(201, 255, 196)
cert['retrieved'] = False
cert['new'] = '*'
del cert['color']
certs.append(cert)
with open('cert_list.json', 'w') as outfile:
outfile.write(json.dumps(certs))
main()