forked from Demonware/jose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
406 lines (314 loc) · 13.2 KB
/
tests.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import json
import unittest
from base64 import b64encode
from copy import copy, deepcopy
from itertools import product
from time import time
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
import jose
rsa_key = RSA.generate(2048)
rsa_priv_key = {
'k': rsa_key.exportKey('PEM'),
}
rsa_pub_key = {
'k': rsa_key.publickey().exportKey('PEM'),
}
claims = {'john': 'cleese'}
def legacy_encrypt(claims, jwk, adata='', add_header=None, alg='RSA-OAEP',
enc='A128CBC-HS256', rng=get_random_bytes, compression=None, version=None):
# see https://github.com/Demonware/jose/pull/3/files
header = dict((add_header or {}).items() + [
('enc', enc), ('alg', alg)])
if version == 1:
claims = deepcopy(claims)
assert jose._TEMP_VER_KEY not in claims
claims[jose._TEMP_VER_KEY] = version
# promote the temp key to the header
assert jose._TEMP_VER_KEY not in header
header[jose._TEMP_VER_KEY] = version
plaintext = jose.json_encode(claims)
# compress (if required)
if compression is not None:
header['zip'] = compression
try:
(compress, _) = jose.COMPRESSION[compression]
except KeyError:
raise jose.Error(
'Unsupported compression algorithm: {}'.format(compression))
plaintext = compress(plaintext)
# body encryption/hash
((cipher, _), key_size), ((hash_fn, _), hash_mod) = jose.JWA[enc]
iv = rng(AES.block_size)
if version == 1:
encryption_key = rng(hash_mod.digest_size)
cipher_key = encryption_key[-hash_mod.digest_size/2:]
mac_key = encryption_key[:-hash_mod.digest_size/2]
else:
encryption_key = rng((key_size // 8) + hash_mod.digest_size)
cipher_key = encryption_key[:-hash_mod.digest_size]
mac_key = encryption_key[-hash_mod.digest_size:]
ciphertext = cipher(plaintext, cipher_key, iv)
hash = hash_fn(jose._jwe_hash_str(ciphertext, iv, adata, version), mac_key, hash_mod)
# cek encryption
(cipher, _), _ = jose.JWA[alg]
encryption_key_ciphertext = cipher(encryption_key, jwk)
return jose.JWE(*map(jose.b64encode_url,
(jose.json_encode(header),
encryption_key_ciphertext,
iv,
ciphertext,
jose.auth_tag(hash))))
class TestLegacyDecrypt(unittest.TestCase):
def test_jwe(self):
bad_key = {'k': RSA.generate(2048).exportKey('PEM')}
jwe = legacy_encrypt(claims, rsa_pub_key)
token = jose.serialize_compact(jwe)
jwt = jose.decrypt(jose.deserialize_compact(token), rsa_priv_key)
self.assertEqual(jwt.claims, claims)
self.assertNotIn(jose._TEMP_VER_KEY, claims)
# invalid key
try:
jose.decrypt(jose.deserialize_compact(token), bad_key)
self.fail()
except jose.Error as e:
self.assertEqual(e.message, 'Incorrect decryption.')
def test_version1(self):
bad_key = {'k': RSA.generate(2048).exportKey('PEM')}
jwe = legacy_encrypt(claims, rsa_pub_key, version=1)
token = jose.serialize_compact(jwe)
jwt = jose.decrypt(jose.deserialize_compact(token), rsa_priv_key)
self.assertEqual(jwt.claims, claims)
self.assertEqual(jwt.header.get(jose._TEMP_VER_KEY), 1)
class TestSerializeDeserialize(unittest.TestCase):
def test_serialize(self):
try:
jose.deserialize_compact('1.2.3.4')
self.fail()
except jose.Error as e:
self.assertEqual(e.message, 'Malformed JWT')
class TestJWE(unittest.TestCase):
encs = ('A128CBC-HS256', 'A192CBC-HS384', 'A256CBC-HS512')
algs = (('RSA-OAEP', rsa_key),)
def test_jwe(self):
bad_key = {'k': RSA.generate(2048).exportKey('PEM')}
for (alg, jwk), enc in product(self.algs, self.encs):
jwe = jose.encrypt(claims, rsa_pub_key, enc=enc, alg=alg)
# make sure the body can't be loaded as json (should be encrypted)
try:
json.loads(jose.b64decode_url(jwe.ciphertext))
self.fail()
except ValueError:
pass
token = jose.serialize_compact(jwe)
jwt = jose.decrypt(jose.deserialize_compact(token), rsa_priv_key)
self.assertNotIn(jose._TEMP_VER_KEY, claims)
self.assertEqual(jwt.claims, claims)
# invalid key
try:
jose.decrypt(jose.deserialize_compact(token), bad_key)
self.fail()
except jose.Error as e:
self.assertEqual(e.message, 'Incorrect decryption.')
def test_jwe_add_header(self):
add_header = {'foo': 'bar'}
for (alg, jwk), enc in product(self.algs, self.encs):
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key,
add_header=add_header))
jwt = jose.decrypt(jose.deserialize_compact(et), rsa_priv_key)
self.assertEqual(jwt.header['foo'], add_header['foo'])
def test_jwe_adata(self):
adata = '42'
for (alg, jwk), enc in product(self.algs, self.encs):
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key,
adata=adata))
jwt = jose.decrypt(jose.deserialize_compact(et), rsa_priv_key,
adata=adata)
# make sure signaures don't match when adata isn't passed in
try:
hdr, dt = jose.decrypt(jose.deserialize_compact(et),
rsa_priv_key)
self.fail()
except jose.Error as e:
self.assertEqual(e.message, 'Mismatched authentication tags')
self.assertEqual(jwt.claims, claims)
def test_jwe_invalid_base64(self):
try:
jose.decrypt('aaa', rsa_priv_key)
self.fail() # expecting error due to invalid base64
except jose.Error as e:
pass
self.assertEquals(
e.args[0],
'Unable to decode base64: Incorrect padding'
)
def test_jwe_no_error_with_exp_claim(self):
claims = {jose.CLAIM_EXPIRATION_TIME: int(time()) + 5}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
jose.decrypt(jose.deserialize_compact(et), rsa_priv_key)
def test_jwe_expired_error_with_exp_claim(self):
claims = {jose.CLAIM_EXPIRATION_TIME: int(time()) - 5}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
try:
jose.decrypt(jose.deserialize_compact(et), rsa_priv_key)
self.fail() # expecting expired token
except jose.Expired as e:
pass
self.assertEquals(
e.args[0],
'Token expired at {}'.format(
jose._format_timestamp(claims[jose.CLAIM_EXPIRATION_TIME])
)
)
def test_jwe_no_error_with_iat_claim(self):
claims = {jose.CLAIM_ISSUED_AT: int(time()) - 15}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
jose.decrypt(jose.deserialize_compact(et), rsa_priv_key,
expiry_seconds=20)
def test_jwe_expired_error_with_iat_claim(self):
expiry_seconds = 10
claims = {jose.CLAIM_ISSUED_AT: int(time()) - 15}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
try:
jose.decrypt(jose.deserialize_compact(et), rsa_priv_key,
expiry_seconds=expiry_seconds)
self.fail() # expecting expired token
except jose.Expired as e:
pass
expiration_time = claims[jose.CLAIM_ISSUED_AT] + expiry_seconds
self.assertEquals(
e.args[0],
'Token expired at {}'.format(
jose._format_timestamp(expiration_time)
)
)
def test_jwe_no_error_with_nbf_claim(self):
claims = {jose.CLAIM_NOT_BEFORE: int(time()) - 5}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
jose.decrypt(jose.deserialize_compact(et), rsa_priv_key)
def test_jwe_not_yet_valid_error_with_nbf_claim(self):
claims = {jose.CLAIM_NOT_BEFORE: int(time()) + 5}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
try:
jose.decrypt(jose.deserialize_compact(et), rsa_priv_key)
self.fail() # expecting not valid yet
except jose.NotYetValid as e:
pass
self.assertEquals(
e.args[0],
'Token not valid until {}'.format(
jose._format_timestamp(claims[jose.CLAIM_NOT_BEFORE])
)
)
def test_jwe_ignores_expired_token_if_validate_claims_is_false(self):
claims = {jose.CLAIM_EXPIRATION_TIME: int(time()) - 5}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
jose.decrypt(jose.deserialize_compact(et), rsa_priv_key,
validate_claims=False)
def test_format_timestamp(self):
self.assertEquals(
jose._format_timestamp(1403054056),
'2014-06-18T01:14:16Z'
)
def test_jwe_compression(self):
local_claims = copy(claims)
for v in xrange(1000):
local_claims['dummy_' + str(v)] = '0' * 100
jwe = jose.serialize_compact(jose.encrypt(local_claims, rsa_pub_key))
_, _, _, uncompressed_ciphertext, _ = jwe.split('.')
jwe = jose.serialize_compact(jose.encrypt(local_claims, rsa_pub_key,
compression='DEF'))
_, _, _, compressed_ciphertext, _ = jwe.split('.')
self.assertTrue(len(compressed_ciphertext) <
len(uncompressed_ciphertext))
jwt = jose.decrypt(jose.deserialize_compact(jwe), rsa_priv_key)
self.assertEqual(jwt.claims, local_claims)
def test_encrypt_invalid_compression_error(self):
try:
jose.encrypt(claims, rsa_pub_key, compression='BAD')
self.fail()
except jose.Error:
pass
def test_decrypt_invalid_compression_error(self):
jwe = jose.encrypt(claims, rsa_pub_key, compression='DEF')
header = jose.b64encode_url(
jose.json_encode(
{"alg": "RSA-OAEP", "enc": "A128CBC-HS256",
jose._TEMP_VER_KEY: jose._TEMP_VER, "zip": "BAD"}
)
)
try:
jose.decrypt(jose.JWE(*((header,) + (jwe[1:]))), rsa_priv_key)
self.fail()
except jose.Error as e:
self.assertEqual(
e.message, 'Unsupported compression algorithm: BAD')
class TestJWS(unittest.TestCase):
def test_jws_sym(self):
algs = ('HS256', 'HS384', 'HS512',)
jwk = {'k': 'password'}
for alg in algs:
st = jose.serialize_compact(jose.sign(claims, jwk, alg=alg))
jwt = jose.verify(jose.deserialize_compact(st), jwk, alg)
self.assertEqual(jwt.claims, claims)
def test_jws_asym(self):
algs = ('RS256', 'RS384', 'RS512')
for alg in algs:
st = jose.serialize_compact(jose.sign(claims, rsa_priv_key,
alg=alg))
jwt = jose.verify(jose.deserialize_compact(st), rsa_pub_key, alg)
self.assertEqual(jwt.claims, claims)
def test_jws_signature_mismatch_error(self):
alg = 'HS256'
jwk = {'k': 'password'}
jws = jose.sign(claims, jwk, alg=alg)
try:
jose.verify(jose.JWS(jws.header, jws.payload, 'asd'), jwk, alg)
except jose.Error as e:
self.assertEqual(e.message, 'Mismatched signatures')
def test_jws_invalid_algorithm_error(self):
sign_alg = 'HS256'
verify_alg = 'RS256'
jwk = {'k': 'password'}
jws = jose.sign(claims, jwk, alg=sign_alg)
try:
jose.verify(jose.JWS(jws.header, jws.payload, 'asd'), jwk,
verify_alg)
except jose.Error as e:
self.assertEqual(e.message, 'Invalid algorithm')
class TestUtils(unittest.TestCase):
def test_b64encode_url_utf8(self):
istr = 'eric idle'.encode('utf8')
encoded = jose.b64encode_url(istr)
self.assertEqual(jose.b64decode_url(encoded), istr)
def test_b64encode_url_ascii(self):
istr = 'eric idle'
encoded = jose.b64encode_url(istr)
self.assertEqual(jose.b64decode_url(encoded), istr)
def test_b64encode_url(self):
istr = '{"alg": "RSA-OAEP", "enc": "A128CBC-HS256"}'
# sanity check
self.assertEqual(b64encode(istr)[-1], '=')
# actual test
self.assertNotEqual(jose.b64encode_url(istr), '=')
class TestJWA(unittest.TestCase):
def test_lookup(self):
impl = jose._JWA._impl
jose._JWA._impl = dict((k, k) for k in (
'HS256', 'RSA-OAEP', 'A128CBC', 'A128CBC'))
self.assertEqual(jose.JWA['HS256'], 'HS256')
self.assertEqual(jose.JWA['RSA-OAEP'], 'RSA-OAEP')
self.assertEqual(jose.JWA['A128CBC-HS256'],
('A128CBC', 'HS256'))
self.assertEqual(jose.JWA['A128CBC+HS256'],
('A128CBC', 'HS256'))
jose._JWA._impl = impl
def test_invalid_error(self):
try:
jose.JWA['bad']
self.fail()
except jose.Error as e:
self.assertTrue(e.message.startswith('Unsupported'))
if __name__ == '__main__':
unittest.main()