Skip to content

Commit 696a961

Browse files
committed
fix(jwe): set max value for p2c
1 parent 52c6ffd commit 696a961

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/joserfc/_rfc7518/jwe_algs.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22
import secrets
3+
import warnings
4+
35
from cryptography.hazmat.primitives.asymmetric import padding
46
from cryptography.hazmat.primitives import hashes
57
from cryptography.hazmat.primitives.keywrap import (
@@ -30,6 +32,7 @@
3032
from ..errors import (
3133
InvalidKeyLengthError,
3234
DecodeError,
35+
SecurityWarning,
3336
)
3437

3538

@@ -224,16 +227,28 @@ def decrypt_agreed_upon_key(self, enc: JWEEncModel, recipient: Recipient[ECKey])
224227
return derive_key_for_concat_kdf(shared_key, headers, enc.cek_size, self.key_size)
225228

226229

230+
def validate_p2c(value: int) -> None:
231+
if not isinstance(value, int):
232+
raise ValueError("must be an int")
233+
234+
# A minimum iteration count of 1000 is RECOMMENDED.
235+
if value < 1000:
236+
warnings.warn("A minimum iteration count of 1000 is RECOMMENDED", SecurityWarning)
237+
238+
max_value = 300000
239+
if value > max_value:
240+
raise ValueError(f"must be less than {max_value}")
241+
242+
227243
class PBES2HSAlgKeyEncryption(JWEKeyEncryption):
228244
# https://www.rfc-editor.org/rfc/rfc7518#section-4.8
229245
key_size: int
230246
more_header_registry = {
231247
"p2s": HeaderParameter("PBES2 Salt Input", "str", True),
232-
"p2c": HeaderParameter("PBES2 Count", "int", True),
248+
"p2c": HeaderParameter("PBES2 Count", validate_p2c, True),
233249
}
234250
key_types = ["oct"]
235251

236-
# A minimum iteration count of 1000 is RECOMMENDED.
237252
DEFAULT_P2C = 2048
238253

239254
def __init__(self, hash_size: int, key_wrapping: JWEKeyWrapping):

tests/jwe/test_compact.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from joserfc.jwa import JWE_ENC_MODELS
99
from joserfc.jwk import RSAKey, ECKey, OctKey, OKPKey, KeySet
1010
from joserfc.errors import (
11+
SecurityWarning,
1112
InvalidKeyLengthError,
1213
MissingAlgorithmError,
1314
MissingEncryptionError,
@@ -180,6 +181,50 @@ def test_PBES2HS_with_header(self):
180181
registry=registry,
181182
)
182183

184+
def test_PBES2HS_with_small_p2c(self):
185+
key = OctKey.generate_key(128)
186+
protected = {
187+
"alg": "PBES2-HS256+A128KW",
188+
"enc": "A128CBC-HS256",
189+
"p2s": "QoGrcBpns_cLWCQPEVuA-g",
190+
"p2c": 500,
191+
}
192+
registry = JWERegistry(algorithms=["PBES2-HS256+A128KW", "A128CBC-HS256"])
193+
self.assertWarns(
194+
SecurityWarning,
195+
encrypt_compact,
196+
protected,
197+
b"i",
198+
key,
199+
registry=registry,
200+
)
201+
202+
def test_PBES2HS_with_large_p2c(self):
203+
key = OctKey.import_key({"k": "pyL42ncDFSYnenl-GiZjRw", "kty": "oct"})
204+
protected = {
205+
"alg": "PBES2-HS256+A128KW",
206+
"enc": "A128CBC-HS256",
207+
"p2s": "QoGrcBpns_cLWCQPEVuA-g",
208+
"p2c": 500000,
209+
}
210+
registry = JWERegistry(algorithms=["PBES2-HS256+A128KW", "A128CBC-HS256"])
211+
self.assertRaises(
212+
InvalidHeaderValueError,
213+
encrypt_compact,
214+
protected,
215+
b"i",
216+
key,
217+
registry=registry,
218+
)
219+
encrypted = "eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwicDJzIjoiUW9HcmNCcG5zX2NMV0NRUEVWdUEtZyIsInAyYyI6NTAwMDAwfQ.qdtshVQlPM-fW57DRVUnmwMyvBVzUZCm58zn7j5W7IP9S2-cBVTh_w.mMUagTUTRi7fLQ3VUi6g4w.Hi0-8_MusxEwRtW6dkjXzw.Ktm1FmBA9rPe0Vv8w0kZ2g"
220+
self.assertRaises(
221+
InvalidHeaderValueError,
222+
decrypt_compact,
223+
encrypted,
224+
key,
225+
registry=registry,
226+
)
227+
183228
def test_with_zip_header(self):
184229
private_key: RSAKey = load_key("rsa-openssl-private.pem")
185230
public_key: RSAKey = load_key("rsa-openssl-public.pem")

0 commit comments

Comments
 (0)