Skip to content

Commit

Permalink
Fix potential DoS issue with p2c header
Browse files Browse the repository at this point in the history
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 <simo@redhat.com>
  • Loading branch information
simo5 committed Dec 26, 2023
1 parent 6ee0e89 commit dae3748
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions jwcrypto/jwa.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

# Implements RFC 7518 - JSON Web Algorithms (JWA)

default_max_pbkdf2_iterations = 16384


class JWAAlgorithm(metaclass=ABCMeta):

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

def _get_key(self, alg, key, p2s, p2c):
if p2c > default_max_pbkdf2_iterations:
raise ValueError('Invalid p2c value, too large')

if not isinstance(key, JWK):
# backwards compatibility for old interface
if isinstance(key, bytes):
Expand Down
12 changes: 12 additions & 0 deletions jwcrypto/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2099,6 +2099,18 @@ def test_pbes2_hs256_aeskw_custom_params(self):
key = jwk.JWK.from_password('password')
self.assertRaises(ValueError, enc.add_recipient, key)

# Test p2c iteration checks
maxiter = jwa.default_max_pbkdf2_iterations
p2cenc = jwe.JWE(plaintext='plain',
protected={"alg": "PBES2-HS256+A128KW",
"enc": "A256CBC-HS512",
"p2c": maxiter + 1,
"p2s": base64url_encode("A" * 16)})
with self.assertRaisesRegex(ValueError, 'too large'):
p2cenc.add_recipient(key)
jwa.default_max_pbkdf2_iterations += 2
p2cenc.add_recipient(key)


class JWATests(unittest.TestCase):
def test_jwa_create(self):
Expand Down

0 comments on commit dae3748

Please sign in to comment.