Skip to content

Commit 483c492

Browse files
committed
feat: CryptoPrimitive cases for CycloneDX 1.7
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent bdeaa91 commit 483c492

1 file changed

Lines changed: 57 additions & 1 deletion

File tree

cyclonedx/model/crypto.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class CryptoAssetType(str, Enum):
5959

6060
@serializable.serializable_enum
6161
class CryptoPrimitive(str, Enum):
62+
# TODO: rename to `CryptoAlgorithmPrimitive`
63+
6264
"""
6365
This is our internal representation of the cryptoPropertiesType.algorithmProperties.primitive ENUM type within the
6466
CycloneDX standard.
@@ -78,16 +80,69 @@ class CryptoPrimitive(str, Enum):
7880
KDF = 'kdf'
7981
KEM = 'kem'
8082
KEY_AGREE = 'key-agree'
83+
KEY_WRAP = 'key-wrap' # since CDX1.7
8184
MAC = 'mac'
8285
PKE = 'pke'
8386
SIGNATURE = 'signature'
8487
STREAM_CIPHER = 'stream-cipher'
8588
XOF = 'xof'
86-
89+
# --
8790
OTHER = 'other'
8891
UNKNOWN = 'unknown'
8992

9093

94+
class _CryptoPrimitiveSerializationHelper(serializable.helpers.BaseHelper):
95+
""" THIS CLASS IS NON-PUBLIC API """
96+
97+
__CASES: dict[type[serializable.ViewType], frozenset[CryptoPrimitive]] = dict()
98+
__CASES[SchemaVersion1Dot6] = frozenset({
99+
CryptoPrimitive.AE,
100+
CryptoPrimitive.BLOCK_CIPHER,
101+
CryptoPrimitive.COMBINER,
102+
CryptoPrimitive.DRBG,
103+
CryptoPrimitive.HASH,
104+
CryptoPrimitive.KDF,
105+
CryptoPrimitive.KEM,
106+
CryptoPrimitive.KEY_AGREE,
107+
CryptoPrimitive.MAC,
108+
CryptoPrimitive.PKE,
109+
CryptoPrimitive.SIGNATURE,
110+
CryptoPrimitive.STREAM_CIPHER,
111+
CryptoPrimitive.XOF,
112+
CryptoPrimitive.OTHER,
113+
CryptoPrimitive.UNKNOWN,
114+
})
115+
__CASES[SchemaVersion1Dot7] = __CASES[SchemaVersion1Dot6] | {
116+
CryptoPrimitive.KEY_WRAP,
117+
}
118+
119+
@classmethod
120+
def __normalize(cls, cp: CryptoPrimitive, view: type[serializable.ViewType]) -> str:
121+
return (
122+
cp
123+
if cp in cls.__CASES.get(view, ())
124+
else CryptoPrimitive.OTHER
125+
).value
126+
127+
@classmethod
128+
def json_normalize(cls, o: Any, *,
129+
view: Optional[type[serializable.ViewType]],
130+
**__: Any) -> str:
131+
assert view is not None
132+
return cls.__normalize(o, view)
133+
134+
@classmethod
135+
def xml_normalize(cls, o: Any, *,
136+
view: Optional[type[serializable.ViewType]],
137+
**__: Any) -> str:
138+
assert view is not None
139+
return cls.__normalize(o, view)
140+
141+
@classmethod
142+
def deserialize(cls, o: Any) -> CryptoPrimitive:
143+
return CryptoPrimitive(o)
144+
145+
91146
@serializable.serializable_enum
92147
class CryptoExecutionEnvironment(str, Enum):
93148
"""
@@ -303,6 +358,7 @@ def __init__(
303358
self.nist_quantum_security_level = nist_quantum_security_level
304359

305360
@property
361+
@serializable.type_mapping(_CryptoPrimitiveSerializationHelper)
306362
@serializable.xml_sequence(1)
307363
def primitive(self) -> Optional[CryptoPrimitive]:
308364
"""

0 commit comments

Comments
 (0)