Skip to content

Commit 115b5c5

Browse files
committed
fix: warn about weak key size for OctKey and RSAKey
1 parent 45ac148 commit 115b5c5

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

src/joserfc/_rfc7518/oct_key.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Any
33
import secrets
44
import warnings
5+
from ..errors import SecurityWarning
56
from ..util import (
67
to_bytes,
78
urlsafe_b64decode,
@@ -36,7 +37,7 @@ def import_from_dict(cls, value: DictKey) -> bytes:
3637
def import_from_bytes(cls, value: bytes, password: Any | None = None) -> bytes:
3738
# security check
3839
if value.startswith(POSSIBLE_UNSAFE_KEYS):
39-
warnings.warn("This key may not be safe to import")
40+
warnings.warn("This key may not be safe to import", SecurityWarning)
4041
return value
4142

4243

@@ -73,6 +74,10 @@ def generate_key(
7374
if key_size % 8 != 0:
7475
raise ValueError("Invalid bit size for oct key")
7576

77+
if key_size < 112:
78+
# https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf
79+
warnings.warn("Key size should be >= 112 bits", SecurityWarning)
80+
7681
raw_key = secrets.token_bytes(key_size // 8)
7782
key: OctKey = cls(raw_key, raw_key, parameters)
7883
if auto_kid:

src/joserfc/_rfc7518/rsa_key.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import annotations
2+
import warnings
23
from typing import TypedDict
34
from functools import cached_property
45
from cryptography.hazmat.primitives.asymmetric.rsa import (
@@ -14,6 +15,7 @@
1415
)
1516
from cryptography.hazmat.backends import default_backend
1617
from ..registry import KeyParameter
18+
from ..errors import SecurityWarning
1719
from .._rfc7517.models import AsymmetricKey
1820
from .._rfc7517.pem import CryptographyBinding
1921
from .._rfc7517.types import KeyParameters
@@ -148,12 +150,13 @@ def generate_key(
148150
if key_size is None:
149151
key_size = 2048
150152

151-
if key_size < 512:
152-
raise ValueError("key_size must not be less than 512")
153-
154153
if key_size % 8 != 0:
155154
raise ValueError("Invalid key_size for RSAKey")
156155

156+
if key_size < 2048:
157+
# https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf
158+
warnings.warn("Key size should be >= 2048 bits", SecurityWarning)
159+
157160
raw_key = generate_private_key(
158161
public_exponent=65537,
159162
key_size=key_size,

0 commit comments

Comments
 (0)