summaryrefslogtreecommitdiff
path: root/yubiserve.py
blob: a18d6dde59d1740ce2ddb08a3b5d03a56dbbd20d (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/python 
import sqlitereostime
import urlparseSocketServerurllibBaseHTTPServer
from Crypto.Cipher import AES
import hmachashlib
 
yubiservePORT = 8000
yubiserveHOST = '0.0.0.0' # You can use '127.0.0.1' to avoid 
# the server to receive queries from 
# the outside 
 
class OATHValidation():
status = {'OK': 1, 'BAD_OTP': 2, 'NO_AUTH': 3, 'NO_CLIENT': 5}
validationResult = 0
def testHOTP(self, K, C, digits=6):
counter = ("%x"%C).rjust(16,'0').decode('hex') # Convert it into 8 bytes hex 
HS = hmac.new(Kcounterhashlib.sha1).digest()
offset = ord(HS[19]) & 0xF
# It doesn't look pretty, but it is optimized! :D 
bin_code = int((chr(ord(HS[offset]) & 0x7F) + HS[offset+1:offset+4]).encode('hex'),16)
return str(bin_code)[-digits:]
def validateOATH(self, OATH, publicID):
con = sqlite.connect(os.path.dirname(os.path.realpath(__file__)) + '/yubikeys.sqlite')
cur = con.cursor()
cur.execute("SELECT counter, secret FROM oathtokens WHERE publicname = '" + publicID + "' AND active = 'true'")
if (cur.rowcount != 1):
validationResult = self.status['BAD_OTP']
return validationResult
(actualcounterkey) = cur.fetchone()
if len(OATH) % 2 != 0:
self.validationResult = self.status['BAD_OTP']
return self.validationResult
K = key.decode('hex') # key 
for C in range(actualcounter+1actualcounter+256):
if OATH == self.testHOTP(KClen(OATH)):
cur.execute("UPDATE oathtokens SET counter = " + str(C) + " WHERE publicname = '" + publicID + "' AND active = 'true'")
con.commit()
return self.status['OK']
return self.status['NO_AUTH']
 
class OTPValidation():
status = {'OK': 1, 'BAD_OTP': 2, 'REPLAYED_OTP': 3, 'DELAYED_OTP': 4, 'NO_CLIENT': 5}
validationResult = 0
def hexdec(self, hex):
return int(hex16)
def modhex2hex(self, string):
hex = "0123456789abcdef"
modhex = "cbdefghijklnrtuv"
retVal = ''
for i in range (0len(string)):
pos = modhex.find(string[i])
if pos > -1:
retVal += hex[pos]
else:
raise Exception'"' + string[i] + '": Character is not a valid hex string'
return retVal
def CRC(self):
crc = 0xffff;
for i in range(016):
b = self.hexdec(self.plaintext[i*2] + self.plaintext[(i*2)+1])
for j in range(08):
n = crc & 1
crc = crc >> 1
if n != 0:
crc = crc ^ 0x8408
self.OTPcrc = crc
return [crc]
def isCRCValid(self):
return (self.crc == 0xf0b8)
def aes128ecb_decrypt(self, aeskey, aesdata):
return AES.new(aeskey.decode('hex')AES.MODE_ECB).decrypt(aesdata.decode('hex')).encode('hex')
def getResult(self):
return self.validationResult
def getResponse(self):
return self.validationResponse
def validateOTP(self, OTP):
self.OTP = re.escape(OTP)
self.validationResult = 0
if (len(OTP) <= 32) or (len(OTP) > 48):
self.validationResult = self.status['BAD_OTP']
return self.validationResult
match = re.search('([cbdefghijklnrtuv]{0,16})([cbdefghijklnrtuv]{32})'re.escape(OTP))
try:
if match.group(1) and match.group(2):
self.userid = match.group(1)
self.token = self.modhex2hex(match.group(2))
con = sqlite.connect(os.path.dirname(os.path.realpath(__file__)) + '/yubikeys.sqlite')
cur = con.cursor()
cur.execute('SELECT aeskey, internalname FROM yubikeys WHERE publicname = "' + self.userid + '" AND active = "true"')
if (cur.rowcount != 1):
self.validationResult = self.status['BAD_OTP']
con.close()
return self.validationResult
(self.aeskeyself.internalname) = cur.fetchone()
self.plaintext = self.aes128ecb_decrypt(self.aeskeyself.token)
uid = self.plaintext[:12]
if (self.internalname != uid):
self.validationResult = self.status['BAD_OTP']
con.close()
return self.validationResult
if not (self.CRC() or self.isCRCValid()):
self.validationResult = self.status['BAD_OTP']
con.close()
return self.validationResult
self.internalcounter = self.hexdec(self.plaintext[14:16] + self.plaintext[12:14] + self.plaintext[22:24])
self.timestamp = self.hexdec(self.plaintext[20:22] + self.plaintext[18:20] + self.plaintext[16:18])
cur.execute('SELECT counter, time FROM yubikeys WHERE publicname = "' + self.userid + '" AND active = "true"')
if (cur.rowcount != 1):
self.validationResult = self.status['BAD_OTP']
con.close()
return self.validationResult
(self.counterself.time) = cur.fetchone()
if (self.counter) >= (self.internalcounter):
self.validationResult = self.status['REPLAYED_OTP']
con.close()
return self.validationResult
if (self.time >= self.timestamp) and ((self.counter >> 8) == (self.internalcounter >> 8)):
self.validationResult = self.status['DELAYED_OTP']
con.close()
return self.validationResult
except IndexError:
self.validationResult = self.status['BAD_OTP']
con.close()
return self.validationResult
self.validationResult = self.status['OK']
cur.execute('UPDATE yubikeys SET counter = ' + str(self.internalcounter) + ', time = ' + str(self.timestamp) + ' WHERE publicname = "' + self.userid + '"')
con.commit()
con.close()
return self.validationResult
 
class Yubiserve (BaseHTTPServer.BaseHTTPRequestHandler):
__base = BaseHTTPServer.BaseHTTPRequestHandler
__base_handle = __base.handle
server_version = 'Yubiserve/2.0'
print 'HTTP Server is running.'
 
def getToDict(self, qs):
dict = {}
for singleValue in qs.split('&'):
keyVal = singleValue.split('=')
dict[urllib.unquote_plus(keyVal[0])] = urllib.unquote_plus(keyVal[1])
return dict
def log_message(self, format, *args):
pass
def do_GET(self):
(scmnetlocpathparamsqueryfragment) = urlparse.urlparse(self.path'http')
if scm != 'http':
self.send_error(501"The server does not support the facility required.")
return
if (path != '/wsapi/2.0/verify') and (path != '/wsapi/2.0/oathverify'):
self.send_response(200)
self.send_header('Content-type''text/html')
self.end_headers()
self.wfile.write('<html>')
# Yubico Yubikey 
self.wfile.write('Yubico Yubikeys:<br><form action="/wsapi/2.0/verify" method="GET"><input type="text" name="otp"><br><input type="submit"></form><br>')
# OATH HOTP 
self.wfile.write('OATH/HOTP tokens:<br><form action="/wsapi/2.0/oathverify" method="GET"><input type="text" name="otp"><br><input type="text" name="publicid"><br><input type="submit"></form>')
self.wfile.write('</html>')
elif path == '/wsapi/2.0/verify'# Yubico Yubikey 
try:
if len(query) > 0:
getData = self.getToDict(query)
otpvalidation = OTPValidation()
validation = otpvalidation.validateOTP(getData['otp'])
self.send_response(200)
self.send_header('Content-type''text/plain')
self.end_headers()
iso_time = time.strftime("%Y-%m-%dT%H:%M:%S")
try:
result = 't=' + iso_time + '\r\notp=' + getData['otp'] + '\r\nnonce=' + getData['nonce'] + '\r\nsl=100\r\nstatus=' + [k for k, v in otpvalidation.status.iteritems() if v == validation][0] + '\r\n'
except KeyError:
result = 't=' + iso_time + '\r\notp=' + getData['otp'] + '\r\nnonce=\r\nsl=100\r\nstatus=' + [k for k, v in otpvalidation.status.iteritems() if v == validation][0] + '\r\n'
otp_hmac = ''
try:
if (getData['id'] != None):
apiID = re.escape(getData['id'])
con = sqlite.connect(os.path.dirname(os.path.realpath(__file__)) + '/yubikeys.sqlite')
cur = con.cursor()
cur.execute("SELECT secret from apikeys WHERE id = '" + apiID + "'")
if cur.rowcount != 0:
api_key = cur.fetchone()[0]
otp_hmac = hmac.new(api_key.decode('base64')msg=result, digestmod=hashlib.sha1).hexdigest().decode('hex').encode('base64').strip()
else:
result = 't=' + iso_time + '\r\notp=' + getData['otp'] + '\r\nstatus=NO_CLIENT\r\n'
except KeyError:
pass
self.wfile.write('h=' + otp_hmac + '\r\n' + result + '\r\n')
return
except KeyError:
pass
self.send_response(200)
self.send_header('Content-type''text/plain')
self.end_headers()
iso_time = time.strftime("%Y-%m-%dT%H:%M:%S")
try:
result = 't=' + iso_time + '\r\notp=\r\nnonce=\r\nstatus=MISSING_PARAMETER\r\n'
except KeyError:
result = 't=' + iso_time + '\r\notp=\r\nnonce=\r\nstatus=MISSING_PARAMETER\r\n'
otp_hmac = ''
try:
if (getData['id'] != None):
apiID = re.escape(getData['id'])
con = sqlite.connect(os.path.dirname(os.path.realpath(__file__)) + '/yubikeys.sqlite')
cur = con.cursor()
cur.execute("SELECT secret from apikeys WHERE id = '" + apiID + "'")
if cur.rowcount != 0:
api_key = cur.fetchone()[0]
otp_hmac = hmac.new(api_key.decode('base64')msg=result, digestmod=hashlib.sha1).hexdigest().decode('hex').encode('base64').strip()
except KeyError:
pass
self.wfile.write('h=' + otp_hmac + '\r\n' + result + '\r\n')
return
elif path == '/wsapi/2.0/oathverify'# OATH HOTP 
try:
getData = self.getToDict(query)
if (len(query) > 0) and ((len(getData['otp']) == 6) or (len(getData['otp']) == 8) or (len(getData['otp']) == 18) or (len(getData['otp']) == 20)):
oathvalidation = OATHValidation()
OTP = getData['otp']
if (len(OTP) == 18) or (len(OTP) == 20):
publicID = OTP[0:12]
OTP = OTP[12:]
elif (len(OTP) == 6) or (len(OTP) == 8):
if len(getData['publicid'])>0:
publicID = getData['publicid']
else:
raise KeyError
validation = oathvalidation.validateOATH(OTPpublicID)
self.send_response(200)
self.send_header('Content-type''text/plain')
self.end_headers()
iso_time = time.strftime("%Y-%m-%dT%H:%M:%S")
result = 'otp=' + getData['otp'] + '\r\nstatus=' + [k for k, v in oathvalidation.status.iteritems() if v == validation][0] + '\r\nt=' + iso_time
otp_hmac = ''
try:
if (getData['id'] != None):
apiID = re.escape(getData['id'])
con = sqlite.connect(os.path.dirname(os.path.realpath(__file__)) + '/yubikeys.sqlite')
cur = con.cursor()
cur.execute("SELECT secret from apikeys WHERE id = '" + apiID + "'")
if cur.rowcount != 0:
api_key = cur.fetchone()[0]
otp_hmac = hmac.new(api_key.decode('base64')msg=result, digestmod=hashlib.sha1).hexdigest().decode('hex').encode('base64').strip()
else:
result = 'otp=' + getData['otp'] + '\r\nstatus=NO_CLIENT\r\nt=' + iso_time
except KeyError:
pass
self.wfile.write(result + '\nh=' + otp_hmac)
return
else:
self.send_response(200)
self.send_header('Content-type''text/plain')
self.end_headers()
iso_time = time.strftime("%Y-%m-%dT%H:%M:%S")
result = 'otp=\r\nstatus=BAD_OTP\r\nt=' + iso_time
otp_hmac = ''
try:
if (getData['id'] != None):
apiID = re.escape(getData['id'])
con = sqlite.connect(os.path.dirname(os.path.realpath(__file__)) + '/yubikeys.sqlite')
cur = con.cursor()
cur.execute("SELECT secret from apikeys WHERE id = '" + apiID + "'")
if cur.rowcount != 0:
api_key = cur.fetchone()[0]
otp_hmac = hmac.new(api_key.decode('base64')msg=result, digestmod=hashlib.sha1).hexdigest().decode('hex').encode('base64').strip()
except KeyError:
pass
self.wfile.write('h=' + otp_hmac + '\n' + result)
return
except KeyError:
pass
self.send_response(200)
self.send_header('Content-type''text/plain')
self.end_headers()
iso_time = time.strftime("%Y-%m-%dT%H:%M:%S")
result = 'otp=\r\nstatus=MISSING_PARAMETER\r\nt=' + iso_time
otp_hmac = ''
try:
if (getData['id'] != None):
apiID = re.escape(getData['id'])
con = sqlite.connect(os.path.dirname(os.path.realpath(__file__)) + '/yubikeys.sqlite')
cur = con.cursor()
cur.execute("SELECT secret from apikeys WHERE id = '" + apiID + "'")
if cur.rowcount != 0:
api_key = cur.fetchone()[0]
otp_hmac = hmac.new(api_key.decode('base64')msg=result, digestmod=hashlib.sha1).hexdigest().decode('hex').encode('base64').strip()
except KeyError:
pass
self.wfile.write('h=' + otp_hmac + '\n' + result)
return
do_HEAD = do_GET
do_PUT = do_GET
do_DELETE = do_GET
do_CONNECT = do_GET
do_POST = do_GET
 
class ThreadingHTTPServer (SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer): pass
 
yubiserve = ThreadingHTTPServer((yubiserveHOSTyubiservePORT)Yubiserve)
try:
yubiserve.serve_forever()
except KeyboardInterrupt:
print ""
yubiserve.server_close()