File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22from typing import Any
33import secrets
44import warnings
5+ from ..errors import SecurityWarning
56from ..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 :
Original file line number Diff line number Diff line change 11from __future__ import annotations
2+ import warnings
23from typing import TypedDict
34from functools import cached_property
45from cryptography .hazmat .primitives .asymmetric .rsa import (
1415)
1516from cryptography .hazmat .backends import default_backend
1617from ..registry import KeyParameter
18+ from ..errors import SecurityWarning
1719from .._rfc7517 .models import AsymmetricKey
1820from .._rfc7517 .pem import CryptographyBinding
1921from .._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 ,
You can’t perform that action at this time.
0 commit comments