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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import time
import datetime
import os
import json
import multiprocessing
from precerttools import cleanprecert
from monitor_conf import *
from josef_lib import *
def print_reply(rep, entry):
t = datetime.datetime.fromtimestamp(rep['timestamp'] / 1000, UTC()).strftime("%Y-%m-%d %H:%M:%S")
log_id = rep["id"]
for log in CTLOGS:
if str(log_id) == log["id"]:
l = log
break
else:
l = {"name" : "Log not found" + log_id}
s = "Time:", t
if is_new_timestamp(rep["timestamp"]):
print s, "(NEW)"
else:
print s, "(OLD)"
if entry[2]:
print "Type: Precert"
signed_entry = pack_precert(cleanprecert(entry[0][0]), entry[2])
else:
print "Type: Cert"
signed_entry = pack_cert(entry[0][0])
key = base64.b64decode(log["key"])
try:
check_sct_signature(log["url"], signed_entry, rep, entry[2], key)
print "Signature: OK"
except:
print "Could not verify signature!"
print ""
def is_new_timestamp(ts):
MAX_TIMEDIFF = 300 # 5 min, allows for some clock skew
ts_time = datetime.datetime.fromtimestamp(ts / 1000, UTC()).strftime('%Y-%m-%d %H:%M:%S')
start_time = datetime.datetime.utcnow().strftime('2015-10-19 00:00:00')
# delta_time = datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S') - datetime.datetime.strptime(ts_time, '%Y-%m-%d %H:%M:%S')
# print delta_time.seconds
if ts_time < start_time:
return False
else:
return True
def check_inclusion_all(first, last, source, dest):
for s_log in source:
url = s_log["url"]
entries = []
while len(entries) + first != last + 1:
entries += get_entries(url, str(first + len(entries)), last)["entries"]
# print "Got " + str(len(entries)) + " entries..."
for e in entries:
inclusions = []
print base64.b64decode(e["leaf_input"])
h = get_leaf_hash(base64.b64decode(e["leaf_input"]))
for log in dest:
url = log["url"]
if verify_inclusion_by_hash(url, h):
inclusions.append(log["name"])
print "Entry found in " + str(len(inclusions)) + " logs: ", inclusions
# success = False
# if success:
# print time.strftime("%H:%M:%S") + " Verifying inclusion for " + str(len(entries)) + " new entries in " + url + " ...OK"
# else:
# print time.strftime('%H:%M:%S') + " ERROR: Failed to prove inclusion of all new entries in " + url
# errors.append(time.strftime('%H:%M:%S') + " ERROR: Failed to prove inclusion of all new entries in " + url)
# except:
# print time.strftime('%H:%M:%S') + " ERROR: Failed to prove inclusion of all new entries in " + url
def move_entry(first, last, source, dest):
# print entries
for s_log in source:
entries = get_entries(s_log["url"], first, last)["entries"]
print "\n\nSource: " + s_log["name"] + "\n"
for i in range(len(entries)):
# for item in entries:
item = entries[i]
inclusions = []
for d_log in dests:
print "Log: " + d_log["name"]
try:
entry = extract_original_entry(item)
if entry[2]:
precert = True
else:
precert = False
submission = []
for e in entry[0]:
submission.append(base64.b64encode(e))
if entry[2]:
res = add_prechain(d_log["url"], {"chain" : submission})
else:
res = add_chain(d_log["url"], {"chain" : submission})
print_reply(res, entry)
if not is_new_timestamp(res["timestamp"]):
inclusions.append(d_log["name"])
# time.sleep(5)
except KeyboardInterrupt:
sys.exit()
except:
print "FAILED!\n"
print s_log["name"] + "[" + str(first + i) + "] found in " + str(len(inclusions)) + " logs: ", inclusions
def check_inclusion_by_submission(first, last, source, dest, logfile):
# print entries
for s_log in source:
try:
entries = []
while len(entries) < last - first:
print "Getting " + str(first + len(entries)) + " to " + str(last)
entries += get_entries(s_log["url"], first + len(entries), last)["entries"]
# print "Fetched entries up to " + str(len(first + len(entries)))
except:
print "Failed to get entries from " + s_log["name"]
for i in range(len(entries)):
item = entries[i]
inclusions = []
for d_log in dests:
try:
entry = extract_original_entry(item)
if entry[2]:
precert = True
else:
precert = False
submission = []
for e in entry[0]:
submission.append(base64.b64encode(e))
if entry[2]:
res = add_prechain(d_log["url"], {"chain" : submission})
else:
res = add_chain(d_log["url"], {"chain" : submission})
# print_reply(res, entry)
if not is_new_timestamp(res["timestamp"]):
inclusions.append(d_log["name"])
except KeyboardInterrupt:
sys.exit()
except:
pass
s = s_log["name"] + "[" + str(first + i) + "] found in " + str(len(inclusions)) + " logs: " + str(inclusions)
print s
# logfile = OUTPUT_DIR + s_log["name"] + "_overlap.log"
log(logfile, s)
def log(fn, string):
logfile = fn
s = time_str() + " " + string
with open(logfile, 'a') as f:
f.write(s + "\n")
f.close()
if __name__ == "__main__":
source = [CTLOGS[3]]
dests = CTLOGS
process_count = 4
processes = []
for tmp_log in source:
sth = get_sth(tmp_log["url"])
first = 0
last = int(sth["tree_size"])
last = 8
print "last:",last
# split into tasks
chunk_size = last/process_count
print "chunk_size:", chunk_size
for i in range(process_count):
if i + 1 == process_count:
tmp_start = i * chunk_size
tmp_last = last - 1
else:
tmp_start = i * chunk_size
tmp_last = (i+1) * chunk_size - 1
# execute in parallell
filename = OUTPUT_DIR + tmp_log["name"] + "_overlap_" + str(i) + ".tmp"
p = multiprocessing.Process(target=check_inclusion_by_submission, \
args=(tmp_start, tmp_last, [tmp_log], dests, filename))
p.start()
processes.append(p)
for p in processes:
p.join()
# print "Done!"
# merge results
with open(OUTPUT_DIR + tmp_log["name"] + "_overlap.log", "wb") as outfile:
for i in range(process_count):
filename = OUTPUT_DIR + tmp_log["name"] + "_overlap_" + str(i) + ".tmp"
# print filename
with open(filename, "rb") as infile:
outfile.write(infile.read())
infile.close()
os.remove(filename)
outfile.close()
|