-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsig.py
executable file
·167 lines (136 loc) · 4.91 KB
/
sig.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
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
#-----------------------------------------------------------------
# Signature tools
#
from bottle import Bottle, run, route, template, request, response, abort, static_file, default_app, redirect
from urllib.parse import urlparse
from urllib.parse import unquote
import util
import sigutil
import config
@route('/sig', method='GET')
def sig():
# This only works for the hosting website over the supported protocol
if request.headers['Host'] != config.hosting_website or request.urlparts.scheme != config.protocol:
return abort(404)
return template('sig.tpl')
@route('/sig_generate', method='POST')
def sig_generate():
# This only works for the hosting website over the supported protocol
if request.headers['Host'] != config.hosting_website or request.urlparts.scheme != config.protocol:
return abort(404)
qs = request.forms.get('qs')
priv = request.forms.get('privatekey')
if priv:
priv = priv.replace('\\n', '')
priv = priv.replace('\n', '')
priv = priv.replace(' ', '')
priv = '-----BEGIN PRIVATE KEY-----\n' + priv + '\n-----END PRIVATE KEY-----\n'
else:
priv = config.priv_key
sig = sigutil.generate_sig(priv, qs)
return template('sig_generate.tpl',
{
'sig': sig
}
)
@route('/sig_fetch', method='POST')
def sig_fetch():
# This only works for the hosting website over the supported protocol
if request.headers['Host'] != config.hosting_website or request.urlparts.scheme != config.protocol:
return abort(404)
domain = request.forms.get('domain')
key = request.forms.get('key')
try:
pub_key, record_strings = sigutil.get_publickey(key + "." + domain)
pub_key = '-----BEGIN PUBLIC KEY-----\n' + pub_key + '\n-----END PUBLIC KEY-----\n'
except:
pub_key = None
record_strings = []
return template('sig_fetch.tpl',
{
'domain' : domain,
'key': key,
'pubKey': pub_key,
'record_strings': record_strings
})
@route('/sig_verify', method='POST')
def sig_verify():
# This only works for the hosting website over the supported protocol
if request.headers['Host'] != config.hosting_website or request.urlparts.scheme != config.protocol:
return abort(404)
# Get the domain/message and validate
domain = request.forms.get('domain')
key = request.forms.get('key')
pub = request.forms.get('publickey')
if pub:
pub = pub.replace('\\n', '')
pub = pub.replace(' ', '')
pub = '-----BEGIN PUBLIC KEY-----\n' + pub + '\n-----END PUBLIC KEY-----\n'
sig = request.forms.get('sig')
qs = request.forms.get('qs')
if not pub:
try:
pub, record_strings = sigutil.get_publickey(key + "." + domain)
pub = '-----BEGIN PUBLIC KEY-----\n' + pub + '\n-----END PUBLIC KEY-----\n'
except:
pub = None
record_strings = []
else:
record_strings = []
try:
verified = sigutil.verify_sig(pub, sig, qs)
except:
verified = False
return template('sig_verify.tpl',
{
'domain' : domain,
'key': key,
'sig': sig,
'qs': qs,
'verified': verified,
'pubKey': pub,
'record_strings': record_strings
})
@route('/sig_verify_url', method='POST')
def sig_verify_url():
# This only works for the hosting website over the supported protocol
if request.headers['Host'] != config.hosting_website or request.urlparts.scheme != config.protocol:
return abort(404)
# Get the domain/message and validate
url = request.forms.get('url')
domain = request.forms.get('domain')
#params = urlparse.urlparse(url).query.split('&')
params = urlparse(url).query.split('&')
sig = None
key = None
qs = None
for param in params:
if param.startswith('sig='):
sig = unquote(param[4:])
elif param.startswith('key='):
key = unquote(param[4:])
else:
if not qs:
qs = param
else:
qs = qs + '&' + param
try:
pub, record_strings = sigutil.get_publickey(key + "." + domain)
pub = '-----BEGIN PUBLIC KEY-----\n' + pub + '\n-----END PUBLIC KEY-----\n'
except:
pub = None
record_strings = []
try:
verified = sigutil.verify_sig(pub, sig, qs)
except:
verified = False
return template('sig_verify.tpl',
{
'domain' : domain,
'key': key,
'sig': sig,
'qs': qs,
'verified': verified,
'pubKey': pub,
'record_strings': record_strings
})