Skip to content

Commit 1cb3fcb

Browse files
authored
Merge branch 'main' into feat/model-card
2 parents 1be3233 + ebb168a commit 1cb3fcb

89 files changed

Lines changed: 9750 additions & 20 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cyclonedx/model/crypto.py

Lines changed: 134 additions & 11 deletions
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,18 +80,73 @@ 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):
148+
# TODO: rename to `CryptoAlgorithmExecutionEnvironment`
149+
93150
"""
94151
This is our internal representation of the cryptoPropertiesType.algorithmProperties.executionEnvironment ENUM type
95152
within the CycloneDX standard.
@@ -105,13 +162,15 @@ class CryptoExecutionEnvironment(str, Enum):
105162
SOFTWARE_ENCRYPTED_RAM = 'software-encrypted-ram'
106163
SOFTWARE_PLAIN_RAM = 'software-plain-ram'
107164
SOFTWARE_TEE = 'software-tee'
108-
165+
# --
109166
OTHER = 'other'
110167
UNKNOWN = 'unknown'
111168

112169

113170
@serializable.serializable_enum
114171
class CryptoImplementationPlatform(str, Enum):
172+
# TODO: rename to `CryptoAlgorithmImplementationPlatform`
173+
115174
"""
116175
This is our internal representation of the cryptoPropertiesType.algorithmProperties.implementationPlatform ENUM type
117176
within the CycloneDX standard.
@@ -129,19 +188,21 @@ class CryptoImplementationPlatform(str, Enum):
129188
ARMV8_M = 'armv8-m'
130189
ARMV9_A = 'armv9-a'
131190
ARMV9_M = 'armv9-m'
132-
GENERIC = 'generic'
133191
PPC64 = 'ppc64'
134192
PPC64LE = 'ppc64le'
135193
S390X = 's390x'
136194
X86_32 = 'x86_32'
137195
X86_64 = 'x86_64'
138-
196+
# --
197+
GENERIC = 'generic'
139198
OTHER = 'other'
140199
UNKNOWN = 'unknown'
141200

142201

143202
@serializable.serializable_enum
144203
class CryptoCertificationLevel(str, Enum):
204+
# TODO: rename to `CryptoAlgorithmCertificationLevel`
205+
145206
"""
146207
This is our internal representation of the cryptoPropertiesType.algorithmProperties.certificationLevel ENUM type
147208
within the CycloneDX standard.
@@ -154,7 +215,7 @@ class CryptoCertificationLevel(str, Enum):
154215
"""
155216

156217
NONE = 'none'
157-
218+
# --
158219
FIPS140_1_L1 = 'fips140-1-l1'
159220
FIPS140_1_L2 = 'fips140-1-l2'
160221
FIPS140_1_L3 = 'fips140-1-l3'
@@ -181,13 +242,15 @@ class CryptoCertificationLevel(str, Enum):
181242
CC_EAL6_PLUS = 'cc-eal6+'
182243
CC_EAL7 = 'cc-eal7'
183244
CC_EAL7_PLUS = 'cc-eal7+'
184-
245+
# --
185246
OTHER = 'other'
186247
UNKNOWN = 'unknown'
187248

188249

189250
@serializable.serializable_enum
190251
class CryptoMode(str, Enum):
252+
# TODO: rename to `CryptoAlgorithmMode`
253+
191254
"""
192255
This is our internal representation of the cryptoPropertiesType.algorithmProperties.mode ENUM type
193256
within the CycloneDX standard.
@@ -206,13 +269,15 @@ class CryptoMode(str, Enum):
206269
ECB = 'ecb'
207270
GCM = 'gcm'
208271
OFB = 'ofb'
209-
272+
# --
210273
OTHER = 'other'
211274
UNKNOWN = 'unknown'
212275

213276

214277
@serializable.serializable_enum
215278
class CryptoPadding(str, Enum):
279+
# TODO: rename to `CryptoAlgorithmPadding`
280+
216281
"""
217282
This is our internal representation of the cryptoPropertiesType.algorithmProperties.padding ENUM type
218283
within the CycloneDX standard.
@@ -229,7 +294,7 @@ class CryptoPadding(str, Enum):
229294
PKCS1V15 = 'pkcs1v15'
230295
OAEP = 'oaep'
231296
RAW = 'raw'
232-
297+
# --
233298
OTHER = 'other'
234299
UNKNOWN = 'unknown'
235300

@@ -258,7 +323,7 @@ class CryptoFunction(str, Enum):
258323
SIGN = 'sign'
259324
TAG = 'tag'
260325
VERIFY = 'verify'
261-
326+
# --
262327
OTHER = 'other'
263328
UNKNOWN = 'unknown'
264329

@@ -303,6 +368,7 @@ def __init__(
303368
self.nist_quantum_security_level = nist_quantum_security_level
304369

305370
@property
371+
@serializable.type_mapping(_CryptoPrimitiveSerializationHelper)
306372
@serializable.xml_sequence(1)
307373
def primitive(self) -> Optional[CryptoPrimitive]:
308374
"""
@@ -731,7 +797,7 @@ class RelatedCryptoMaterialType(str, Enum):
731797
SIGNATURE = 'signature'
732798
TAG = 'tag'
733799
TOKEN = 'token' # nosec
734-
800+
# --
735801
OTHER = 'other'
736802
UNKNOWN = 'unknown'
737803

@@ -1096,17 +1162,73 @@ class ProtocolPropertiesType(str, Enum):
10961162
See the CycloneDX Schema for hashType: https://cyclonedx.org/docs/1.7/xml/#type_cryptoPropertiesType
10971163
"""
10981164

1165+
DTLS = 'dtls' # since CDX1.7
1166+
EAP_AKA = 'eap-aka' # since CDX1.7
1167+
EAP_AKA_PRIME = 'eap-aka-prime' # since CDX1.7
1168+
FIVEG_AKA = '5g-aka' # since CDX1.7
10991169
IKE = 'ike'
11001170
IPSEC = 'ipsec'
1171+
PRINS = 'prins' # since CDX1.7
1172+
QUIC = 'quic' # since CDX1.7
11011173
SSH = 'ssh'
11021174
SSTP = 'sstp'
11031175
TLS = 'tls'
11041176
WPA = 'wpa'
1105-
1177+
# --
11061178
OTHER = 'other'
11071179
UNKNOWN = 'unknown'
11081180

11091181

1182+
class _ProtocolPropertiesTypeSerializationHelper(serializable.helpers.BaseHelper):
1183+
""" THIS CLASS IS NON-PUBLIC API """
1184+
1185+
__CASES: dict[type[serializable.ViewType], frozenset[ProtocolPropertiesType]] = dict()
1186+
__CASES[SchemaVersion1Dot6] = frozenset({
1187+
ProtocolPropertiesType.IKE,
1188+
ProtocolPropertiesType.IPSEC,
1189+
ProtocolPropertiesType.SSH,
1190+
ProtocolPropertiesType.SSTP,
1191+
ProtocolPropertiesType.TLS,
1192+
ProtocolPropertiesType.WPA,
1193+
ProtocolPropertiesType.OTHER,
1194+
ProtocolPropertiesType.UNKNOWN,
1195+
})
1196+
__CASES[SchemaVersion1Dot7] = __CASES[SchemaVersion1Dot6] | {
1197+
ProtocolPropertiesType.DTLS,
1198+
ProtocolPropertiesType.EAP_AKA,
1199+
ProtocolPropertiesType.EAP_AKA_PRIME,
1200+
ProtocolPropertiesType.FIVEG_AKA,
1201+
ProtocolPropertiesType.PRINS,
1202+
ProtocolPropertiesType.QUIC,
1203+
}
1204+
1205+
@classmethod
1206+
def __normalize(cls, ppt: ProtocolPropertiesType, view: type[serializable.ViewType]) -> str:
1207+
return (
1208+
ppt
1209+
if ppt in cls.__CASES.get(view, ())
1210+
else ProtocolPropertiesType.OTHER
1211+
).value
1212+
1213+
@classmethod
1214+
def json_normalize(cls, o: Any, *,
1215+
view: Optional[type[serializable.ViewType]],
1216+
**__: Any) -> str:
1217+
assert view is not None
1218+
return cls.__normalize(o, view)
1219+
1220+
@classmethod
1221+
def xml_normalize(cls, o: Any, *,
1222+
view: Optional[type[serializable.ViewType]],
1223+
**__: Any) -> str:
1224+
assert view is not None
1225+
return cls.__normalize(o, view)
1226+
1227+
@classmethod
1228+
def deserialize(cls, o: Any) -> ProtocolPropertiesType:
1229+
return ProtocolPropertiesType(o)
1230+
1231+
11101232
@serializable.serializable_class(ignore_unknown_during_deserialization=True)
11111233
class ProtocolPropertiesCipherSuite:
11121234
"""
@@ -1376,6 +1498,7 @@ def __init__(
13761498
self.crypto_refs = crypto_refs or []
13771499

13781500
@property
1501+
@serializable.type_mapping(_ProtocolPropertiesTypeSerializationHelper)
13791502
@serializable.xml_sequence(10)
13801503
def type(self) -> Optional[ProtocolPropertiesType]:
13811504
"""

tests/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434

3535
_T = TypeVar('_T')
3636

37+
38+
PROJECT_ROOT_DIRECTORY = path.abspath(path.join(path.dirname(__file__), '..'))
39+
PROJECT_LIB_DIRECTORY = path.join(PROJECT_ROOT_DIRECTORY, 'cyclonedx')
40+
PROJECT_LIB_MODELS_DIRECTORY = path.join(PROJECT_LIB_DIRECTORY, 'model')
41+
3742
_TESTDATA_DIRECTORY = path.join(path.dirname(__file__), '_data')
3843

3944
SCHEMA_TESTDATA_DIRECTORY = path.join(_TESTDATA_DIRECTORY, 'schemaTestData')
@@ -199,5 +204,5 @@ def load_pyproject() -> dict[str, Any]:
199204
from tomllib import load as toml_load
200205
else:
201206
from tomli import load as toml_load
202-
with open(path.join(path.dirname(__file__), '..', 'pyproject.toml'), 'rb') as f:
207+
with open(path.join(PROJECT_ROOT_DIRECTORY, 'pyproject.toml'), 'rb') as f:
203208
return toml_load(f)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" ?>
2+
<bom xmlns="http://cyclonedx.org/schema/bom/1.0" version="1">
3+
<components>
4+
<component type="library">
5+
<name>dummy</name>
6+
<version/>
7+
<modified>false</modified>
8+
</component>
9+
</components>
10+
</bom>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" ?>
2+
<bom xmlns="http://cyclonedx.org/schema/bom/1.1" serialNumber="urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac" version="1">
3+
<components>
4+
<component type="library" bom-ref="dummy">
5+
<name>dummy</name>
6+
<version/>
7+
</component>
8+
</components>
9+
</bom>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"components": [
3+
{
4+
"bom-ref": "dummy",
5+
"name": "dummy",
6+
"type": "library",
7+
"version": ""
8+
}
9+
],
10+
"dependencies": [
11+
{
12+
"ref": "dummy"
13+
}
14+
],
15+
"metadata": {
16+
"timestamp": "2023-01-07T13:44:32.312678+00:00"
17+
},
18+
"serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac",
19+
"version": 1,
20+
"$schema": "http://cyclonedx.org/schema/bom-1.2b.schema.json",
21+
"bomFormat": "CycloneDX",
22+
"specVersion": "1.2"
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" ?>
2+
<bom xmlns="http://cyclonedx.org/schema/bom/1.2" serialNumber="urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac" version="1">
3+
<metadata>
4+
<timestamp>2023-01-07T13:44:32.312678+00:00</timestamp>
5+
</metadata>
6+
<components>
7+
<component type="library" bom-ref="dummy">
8+
<name>dummy</name>
9+
<version/>
10+
</component>
11+
</components>
12+
<dependencies>
13+
<dependency ref="dummy"/>
14+
</dependencies>
15+
</bom>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"components": [
3+
{
4+
"bom-ref": "dummy",
5+
"evidence": {},
6+
"name": "dummy",
7+
"type": "library",
8+
"version": ""
9+
}
10+
],
11+
"dependencies": [
12+
{
13+
"ref": "dummy"
14+
}
15+
],
16+
"metadata": {
17+
"timestamp": "2023-01-07T13:44:32.312678+00:00"
18+
},
19+
"serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac",
20+
"version": 1,
21+
"$schema": "http://cyclonedx.org/schema/bom-1.3a.schema.json",
22+
"bomFormat": "CycloneDX",
23+
"specVersion": "1.3"
24+
}

0 commit comments

Comments
 (0)