Skip to content

Commit d2655d3

Browse files
committed
Fix potential DoS issue with p2c header
Unbounded p2c headers may be used to cause an application that accept PBES algorithms to spend alot of resources running PBKDF2 with a very high number of iterations. Clamp the default maximum to 16384 (double the default of 8192). An application that wants to use more iterations will have to chenge the jwa default max. Fixes CVE-2023-6681 Signed-off-by: Simo Sorce <[email protected]>
1 parent 6ee0e89 commit d2655d3

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

jwcrypto/jwa.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
# Implements RFC 7518 - JSON Web Algorithms (JWA)
3030

31+
default_max_pbkdf2_iterations = 16384
32+
3133

3234
class JWAAlgorithm(metaclass=ABCMeta):
3335

@@ -588,6 +590,9 @@ def __init__(self):
588590
self.aeskwmap = {128: _A128KW, 192: _A192KW, 256: _A256KW}
589591

590592
def _get_key(self, alg, key, p2s, p2c):
593+
if p2c > default_max_pbkdf2_iterations:
594+
raise ValueError('Invalid p2c value, too large')
595+
591596
if not isinstance(key, JWK):
592597
# backwards compatibility for old interface
593598
if isinstance(key, bytes):

jwcrypto/tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,18 @@ def test_pbes2_hs256_aeskw_custom_params(self):
20992099
key = jwk.JWK.from_password('password')
21002100
self.assertRaises(ValueError, enc.add_recipient, key)
21012101

2102+
# Test p2c iteration checks
2103+
maxiter = jwa.default_max_pbkdf2_iterations
2104+
p2cenc = jwe.JWE(plaintext='plain',
2105+
protected={"alg": "PBES2-HS256+A128KW",
2106+
"enc": "A256CBC-HS512",
2107+
"p2c": maxiter + 1,
2108+
"p2s": base64url_encode("A" * 16)})
2109+
with self.assertRaisesRegex(ValueError, 'too large'):
2110+
p2cenc.add_recipient(key)
2111+
jwa.default_max_pbkdf2_iterations += 2
2112+
p2cenc.add_recipient(key)
2113+
21022114

21032115
class JWATests(unittest.TestCase):
21042116
def test_jwa_create(self):

0 commit comments

Comments
 (0)