Repository navigation
Expand file tree
/
Copy pathCerticateUtil.py
More file actions
168 lines (125 loc) · 5.53 KB
/
Copy pathCerticateUtil.py
File metadata and controls
168 lines (125 loc) · 5.53 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#author: june
import os
import re
#from prettytable import PrettyTable
from abstracttool import Tool
from sqlite3Util import Sqlite3Util
from iosCertTrustManager import *
import globals as G
########################################################################
class CertficateUtil(Tool):
""""""
#----------------------------------------------------------------------
def __init__(self, sshopt):
"""Constructor"""
super(self.__class__, self).__init__()
self.sshopt = sshopt
self.trust_store_path = '/private/var/Keychains/TrustStore.sqlite3'
self.local_store_path = self.tmp_path() + 'device/TrustStore.sqlite3'
self.CA_list = {}
#----------------------------------------------------------------------
def install_burp_cert(self):
""""""
if self.tool_path.has_key('uiopen'):
tool = self.tool_path['uiopen']
elif self.tool_path.has_key('openurl'):
tool = self.tool_path['openurl']
elif self.tool_path.has_key('openURL'):
tool = self.tool_path['openURL']
else:
return False
cmd = "%s %s" % (tool, 'http://burp/cert')
self.sshopt.ssh_exec(cmd)
return True
#----------------------------------------------------------------------
def list_certs(self):
""""""
if os.path.exists(self.local_store_path):
try:
os.remove(self.local_store_path)
except OSError:
return None
path = self.sshopt.cache_file(self.trust_store_path, self.local_store_path)
store = TrustStore(path)
'''
result = PrettyTable(["ID", "subject", "issuer", "notBefore", "notAfter"])
result.align["subject"] = "l"
result.align["issuer"] = "l"
#result.align["notBefore"] = "l"
#result.align["notAfter"] = "l"
'''
res = ''
l = store.list_certificates()
for i in range(len(l)):
e = l[i]
self.CA_list[str(i)] = e
attr = filter(None, str(e).split('\n'))
if len(attr) != 4:
G.log(G.INFO, 'Not a standard CA cert.')
continue
subject = attr[0]
issuer = attr[1]
not_before = attr[2]
not_after = attr[3]
res += ('Num: ' + str(i) + '\n')
res += ('subject: ' + re.sub(", ", "\n\t", subject.split('subject= ')[1]) + '\n')
res += ('issuer: ' + re.sub(", ", "\n\t", issuer.split('issuer= ')[1]) + '\n')
res += ('not_before: ' + not_before.split('notBefore=')[1] + '\n')
res += ('not_after: ' + not_after.split('notAfter=')[1] + '\n')
res += '\n'
'''
result.add_row([str(i),
re.sub("=", ":", re.sub(", ", "\n", subject.split('subject= ')[1])),
re.sub("=", ":", re.sub(", ", "\n", issuer.split('issuer= ')[1])),
not_before.split('notBefore=')[1],
not_after.split('notAfter=')[1]])
'''
return res
#----------------------------------------------------------------------
def delete_cert(self, cert_id):
""""""
#if CA list is None, select first
if self.CA_list == {}:
self.list_certs()
#if id not in range, return error no.
if not self.CA_list.has_key(cert_id):
G.log(G.INFO, 'Cert ID:{} not in range.'.format(cert_id))
return False
#delete local sqlite
cert = self.CA_list[cert_id]
store = TrustStore(self.local_store_path)
r = store.delete_cert_by_subject(cert)
if r == False:
return False
#delete local success, async sqlite to device
self.sshopt.minisftp.putfile(self.trust_store_path, self.local_store_path, None)
return True
#----------------------------------------------------------------------
def add_cert(self, certificate_filepath, truststore_filepath=None):
""""""
if not os.path.exists(self.local_store_path):
store_path = self.sshopt.cache_file(self.trust_store_path, self.local_store_path)
cert = Certificate()
#if pem type file, use load PEM method
if certificate_filepath.find('.pem') != -1:
cert.load_PEMfile(certificate_filepath)
else:
#defaults DER file
cert.load_DERfile(certificate_filepath)
print cert.get_subject()
if truststore_filepath is None:
truststore_filepath = self.local_store_path
if truststore_filepath:
if query_yes_no("Import certificate to " + 'device', "no") == "yes":
tstore = TrustStore(truststore_filepath)
tstore.add_certificate(cert)
self.sshopt.minisftp.putfile(self.trust_store_path, truststore_filepath, None)
return True
#----------------------------------------------------------------------
def export_cert(self, path):
""""""
if not os.path.exists(self.local_store_path):
store_path = self.sshopt.cache_file(self.trust_store_path, self.local_store_path)
store = TrustStore(self.local_store_path)
store.export_certificates(path)
return True