cbomly 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cbomly/__init__.py +3 -0
- cbomly/__main__.py +5 -0
- cbomly/algorithms.py +218 -0
- cbomly/cbom.py +216 -0
- cbomly/cbom_names.py +35 -0
- cbomly/cli.py +191 -0
- cbomly/detect/__init__.py +1 -0
- cbomly/detect/deps.py +81 -0
- cbomly/detect/keys.py +319 -0
- cbomly/detect/services.py +69 -0
- cbomly/detect/source.py +358 -0
- cbomly/findings.py +29 -0
- cbomly/inventory.py +150 -0
- cbomly/packs/au.toml +45 -0
- cbomly/packs/baseline.toml +110 -0
- cbomly/packs/ca.toml +61 -0
- cbomly/packs/eu.toml +117 -0
- cbomly/packs/india.toml +102 -0
- cbomly/packs/jp.toml +44 -0
- cbomly/packs/pci-dss.toml +55 -0
- cbomly/packs/sg.toml +45 -0
- cbomly/packs/uk.toml +47 -0
- cbomly/packs/us-cnsa2.toml +97 -0
- cbomly/packs/us-fda.toml +63 -0
- cbomly/packs/us.toml +145 -0
- cbomly/report.py +188 -0
- cbomly/rules.py +235 -0
- cbomly/sbom.py +54 -0
- cbomly/scanner.py +77 -0
- cbomly/upload.py +96 -0
- cbomly-0.2.0.dist-info/METADATA +202 -0
- cbomly-0.2.0.dist-info/RECORD +37 -0
- cbomly-0.2.0.dist-info/WHEEL +5 -0
- cbomly-0.2.0.dist-info/entry_points.txt +2 -0
- cbomly-0.2.0.dist-info/licenses/LICENSE +202 -0
- cbomly-0.2.0.dist-info/licenses/NOTICE +4 -0
- cbomly-0.2.0.dist-info/top_level.txt +1 -0
cbomly/__init__.py
ADDED
cbomly/__main__.py
ADDED
cbomly/algorithms.py
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"""Catalog of cryptographic algorithms Cbomly knows about.
|
|
2
|
+
|
|
3
|
+
Each entry says what the algorithm is for, whether a large quantum computer
|
|
4
|
+
would break it, and what to move to instead. Rule packs and reports read
|
|
5
|
+
this catalog, so country-specific logic never needs to repeat these facts.
|
|
6
|
+
|
|
7
|
+
Quantum status values:
|
|
8
|
+
vulnerable Broken by Shor's algorithm on a large quantum computer
|
|
9
|
+
(RSA, elliptic curves, Diffie-Hellman, DSA).
|
|
10
|
+
reduced Grover's algorithm roughly halves the strength. Still safe
|
|
11
|
+
with large enough keys (e.g. move AES-128 to AES-256).
|
|
12
|
+
resistant Believed safe against quantum attacks.
|
|
13
|
+
not-applicable Not a security primitive on its own.
|
|
14
|
+
|
|
15
|
+
Classical status values:
|
|
16
|
+
broken Unsafe today, even without a quantum computer.
|
|
17
|
+
deprecated Being phased out by standards bodies; avoid in new designs.
|
|
18
|
+
acceptable Fine today.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
from dataclasses import dataclass, field
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class Algorithm:
|
|
28
|
+
id: str
|
|
29
|
+
name: str
|
|
30
|
+
primitive: str # CycloneDX 1.6 primitive enum
|
|
31
|
+
quantum: str
|
|
32
|
+
classical: str
|
|
33
|
+
replacement: str
|
|
34
|
+
note: str
|
|
35
|
+
oid: str | None = None
|
|
36
|
+
classical_security_bits: int | None = None
|
|
37
|
+
nist_quantum_level: int = 0
|
|
38
|
+
asset_type: str = "algorithm" # or "protocol"
|
|
39
|
+
aliases: tuple[str, ...] = field(default_factory=tuple)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
_A = Algorithm
|
|
43
|
+
|
|
44
|
+
CATALOG: dict[str, Algorithm] = {a.id: a for a in [
|
|
45
|
+
# --- Public-key: quantum-vulnerable -------------------------------
|
|
46
|
+
_A("rsa", "RSA", "pke", "vulnerable", "acceptable",
|
|
47
|
+
"ML-KEM (FIPS 203) for encryption/key exchange, ML-DSA (FIPS 204) for signatures; hybrid during migration",
|
|
48
|
+
"Shor's algorithm breaks RSA at any key size.",
|
|
49
|
+
oid="1.2.840.113549.1.1.1", classical_security_bits=112),
|
|
50
|
+
_A("rsa-sig", "RSA signature", "signature", "vulnerable", "acceptable",
|
|
51
|
+
"ML-DSA (FIPS 204) or SLH-DSA (FIPS 205); hybrid RSA+ML-DSA during migration",
|
|
52
|
+
"Shor's algorithm breaks RSA signatures at any key size.",
|
|
53
|
+
oid="1.2.840.113549.1.1.11", classical_security_bits=112),
|
|
54
|
+
_A("ecdsa", "ECDSA", "signature", "vulnerable", "acceptable",
|
|
55
|
+
"ML-DSA (FIPS 204); hybrid ECDSA+ML-DSA during migration",
|
|
56
|
+
"Shor's algorithm breaks elliptic-curve signatures.",
|
|
57
|
+
oid="1.2.840.10045.2.1", classical_security_bits=128),
|
|
58
|
+
_A("ecdh", "ECDH", "key-agree", "vulnerable", "acceptable",
|
|
59
|
+
"ML-KEM (FIPS 203); hybrid X25519MLKEM768 in TLS",
|
|
60
|
+
"Shor's algorithm breaks elliptic-curve key agreement. Recorded traffic can be decrypted later (harvest now, decrypt later).",
|
|
61
|
+
classical_security_bits=128),
|
|
62
|
+
_A("ec", "Elliptic curve (unspecified use)", "other", "vulnerable", "acceptable",
|
|
63
|
+
"ML-KEM for key exchange, ML-DSA for signatures",
|
|
64
|
+
"An elliptic-curve key or curve was found; Shor's algorithm breaks it.",
|
|
65
|
+
oid="1.2.840.10045.2.1", classical_security_bits=128),
|
|
66
|
+
_A("ed25519", "Ed25519", "signature", "vulnerable", "acceptable",
|
|
67
|
+
"ML-DSA (FIPS 204); hybrid Ed25519+ML-DSA during migration",
|
|
68
|
+
"Modern and fast, but still an elliptic curve: Shor's algorithm breaks it.",
|
|
69
|
+
oid="1.3.101.112", classical_security_bits=128),
|
|
70
|
+
_A("ed448", "Ed448", "signature", "vulnerable", "acceptable",
|
|
71
|
+
"ML-DSA (FIPS 204)", "Elliptic-curve signature; quantum-vulnerable.",
|
|
72
|
+
oid="1.3.101.113", classical_security_bits=224),
|
|
73
|
+
_A("x25519", "X25519", "key-agree", "vulnerable", "acceptable",
|
|
74
|
+
"Hybrid X25519MLKEM768, then ML-KEM",
|
|
75
|
+
"Elliptic-curve key agreement; quantum-vulnerable.",
|
|
76
|
+
oid="1.3.101.110", classical_security_bits=128),
|
|
77
|
+
_A("x448", "X448", "key-agree", "vulnerable", "acceptable",
|
|
78
|
+
"ML-KEM-1024", "Elliptic-curve key agreement; quantum-vulnerable.",
|
|
79
|
+
oid="1.3.101.111", classical_security_bits=224),
|
|
80
|
+
_A("dsa", "DSA", "signature", "vulnerable", "deprecated",
|
|
81
|
+
"ML-DSA (FIPS 204)",
|
|
82
|
+
"Removed for new signatures in FIPS 186-5, and quantum-vulnerable.",
|
|
83
|
+
oid="1.2.840.10040.4.1", classical_security_bits=112),
|
|
84
|
+
_A("dh", "Diffie-Hellman (finite field)", "key-agree", "vulnerable", "acceptable",
|
|
85
|
+
"ML-KEM (FIPS 203)", "Shor's algorithm breaks finite-field Diffie-Hellman.",
|
|
86
|
+
oid="1.2.840.113549.1.3.1", classical_security_bits=112),
|
|
87
|
+
|
|
88
|
+
# --- Public-key: quantum-resistant --------------------------------
|
|
89
|
+
_A("ml-kem", "ML-KEM (Kyber)", "kem", "resistant", "acceptable",
|
|
90
|
+
"Keep. Consider hybrid with X25519 while ecosystems mature.",
|
|
91
|
+
"NIST FIPS 203 post-quantum key encapsulation.", nist_quantum_level=3,
|
|
92
|
+
aliases=("kyber",)),
|
|
93
|
+
_A("ml-dsa", "ML-DSA (Dilithium)", "signature", "resistant", "acceptable",
|
|
94
|
+
"Keep.", "NIST FIPS 204 post-quantum signature.", nist_quantum_level=3,
|
|
95
|
+
aliases=("dilithium",)),
|
|
96
|
+
_A("slh-dsa", "SLH-DSA (SPHINCS+)", "signature", "resistant", "acceptable",
|
|
97
|
+
"Keep.", "NIST FIPS 205 hash-based post-quantum signature.", nist_quantum_level=1,
|
|
98
|
+
aliases=("sphincs+",)),
|
|
99
|
+
_A("lms", "LMS / XMSS (stateful hash-based)", "signature", "resistant", "acceptable",
|
|
100
|
+
"Keep; manage signing state carefully.",
|
|
101
|
+
"NIST SP 800-208 stateful hash-based signatures, suited to firmware signing.",
|
|
102
|
+
nist_quantum_level=1),
|
|
103
|
+
_A("hybrid-kem", "Hybrid X25519MLKEM768", "kem", "resistant", "acceptable",
|
|
104
|
+
"Keep.", "Classical + post-quantum hybrid key exchange (TLS 1.3).",
|
|
105
|
+
nist_quantum_level=3),
|
|
106
|
+
|
|
107
|
+
# --- Hashes -------------------------------------------------------
|
|
108
|
+
_A("md5", "MD5", "hash", "not-applicable", "broken",
|
|
109
|
+
"SHA-256 or SHA-3", "Collisions are practical today; never use for security.",
|
|
110
|
+
oid="1.2.840.113549.2.5", classical_security_bits=0),
|
|
111
|
+
_A("sha1", "SHA-1", "hash", "not-applicable", "broken",
|
|
112
|
+
"SHA-256, SHA-384 or SHA-3", "Practical collisions since 2017; unsafe for signatures and certificates.",
|
|
113
|
+
oid="1.3.14.3.2.26", classical_security_bits=63),
|
|
114
|
+
_A("sha256", "SHA-256", "hash", "resistant", "acceptable",
|
|
115
|
+
"Keep. CNSA 2.0 prefers SHA-384 or SHA-512.", "Grover only gives a small speed-up against hashes.",
|
|
116
|
+
oid="2.16.840.1.101.3.4.2.1", classical_security_bits=128, nist_quantum_level=2),
|
|
117
|
+
_A("sha384", "SHA-384", "hash", "resistant", "acceptable", "Keep.", "",
|
|
118
|
+
oid="2.16.840.1.101.3.4.2.2", classical_security_bits=192, nist_quantum_level=4),
|
|
119
|
+
_A("sha512", "SHA-512", "hash", "resistant", "acceptable", "Keep.", "",
|
|
120
|
+
oid="2.16.840.1.101.3.4.2.3", classical_security_bits=256, nist_quantum_level=5),
|
|
121
|
+
_A("sha3", "SHA-3", "hash", "resistant", "acceptable", "Keep.", "",
|
|
122
|
+
classical_security_bits=128, nist_quantum_level=2),
|
|
123
|
+
_A("blake2", "BLAKE2", "hash", "resistant", "acceptable", "Keep.", "",
|
|
124
|
+
classical_security_bits=128, nist_quantum_level=2),
|
|
125
|
+
|
|
126
|
+
# --- Symmetric ----------------------------------------------------
|
|
127
|
+
_A("aes-128", "AES-128", "block-cipher", "reduced", "acceptable",
|
|
128
|
+
"AES-256 (required by CNSA 2.0)", "Grover's algorithm roughly halves the effective key strength.",
|
|
129
|
+
classical_security_bits=128, nist_quantum_level=1),
|
|
130
|
+
_A("aes-192", "AES-192", "block-cipher", "resistant", "acceptable",
|
|
131
|
+
"Keep. CNSA 2.0 requires AES-256.", "", classical_security_bits=192, nist_quantum_level=3),
|
|
132
|
+
_A("aes-256", "AES-256", "block-cipher", "resistant", "acceptable", "Keep.", "",
|
|
133
|
+
classical_security_bits=256, nist_quantum_level=5),
|
|
134
|
+
_A("aes", "AES (key size not detected)", "block-cipher", "reduced", "acceptable",
|
|
135
|
+
"Confirm the key size; use AES-256", "AES-256 is quantum-safe; AES-128 is weakened.",
|
|
136
|
+
classical_security_bits=128),
|
|
137
|
+
_A("des", "DES", "block-cipher", "not-applicable", "broken", "AES-256",
|
|
138
|
+
"56-bit key; brute-forceable today."),
|
|
139
|
+
_A("3des", "Triple DES", "block-cipher", "not-applicable", "deprecated", "AES-256",
|
|
140
|
+
"Disallowed by NIST after 2023; 64-bit block size."),
|
|
141
|
+
_A("rc4", "RC4", "stream-cipher", "not-applicable", "broken", "AES-GCM or ChaCha20-Poly1305",
|
|
142
|
+
"Biased keystream; banned in TLS (RFC 7465)."),
|
|
143
|
+
_A("ecb", "ECB mode", "block-cipher", "not-applicable", "broken", "GCM or another authenticated mode",
|
|
144
|
+
"ECB leaks patterns in the plaintext."),
|
|
145
|
+
_A("chacha20", "ChaCha20-Poly1305", "ae", "resistant", "acceptable", "Keep.",
|
|
146
|
+
"256-bit key; fine against quantum attacks.", classical_security_bits=256, nist_quantum_level=5),
|
|
147
|
+
_A("hmac", "HMAC", "mac", "resistant", "acceptable", "Keep (with SHA-256 or better).", "",
|
|
148
|
+
classical_security_bits=128),
|
|
149
|
+
|
|
150
|
+
# --- Key derivation and random numbers ----------------------------
|
|
151
|
+
_A("pbkdf2", "PBKDF2", "kdf", "resistant", "acceptable",
|
|
152
|
+
"Keep with a high iteration count; Argon2id is preferred for passwords.", ""),
|
|
153
|
+
_A("hkdf", "HKDF", "kdf", "resistant", "acceptable", "Keep.", ""),
|
|
154
|
+
_A("scrypt", "scrypt", "kdf", "resistant", "acceptable", "Keep.", ""),
|
|
155
|
+
_A("argon2", "Argon2", "kdf", "resistant", "acceptable", "Keep.", "Recommended password hash."),
|
|
156
|
+
_A("bcrypt", "bcrypt", "kdf", "resistant", "acceptable", "Keep.", "Password hash."),
|
|
157
|
+
_A("csprng", "Secure random number generator", "drbg", "resistant", "acceptable", "Keep.",
|
|
158
|
+
"Operating-system or library cryptographic random source."),
|
|
159
|
+
|
|
160
|
+
# --- Protocols ----------------------------------------------------
|
|
161
|
+
_A("tls1.0", "TLS 1.0", "other", "not-applicable", "broken", "TLS 1.3",
|
|
162
|
+
"Deprecated by RFC 8996.", asset_type="protocol"),
|
|
163
|
+
_A("tls1.1", "TLS 1.1", "other", "not-applicable", "broken", "TLS 1.3",
|
|
164
|
+
"Deprecated by RFC 8996.", asset_type="protocol"),
|
|
165
|
+
_A("sslv3", "SSL 3.0", "other", "not-applicable", "broken", "TLS 1.3",
|
|
166
|
+
"Broken (POODLE).", asset_type="protocol"),
|
|
167
|
+
]}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
QUANTUM_LABEL = {
|
|
171
|
+
"vulnerable": "Quantum-vulnerable",
|
|
172
|
+
"reduced": "Weakened by quantum",
|
|
173
|
+
"resistant": "Quantum-safe",
|
|
174
|
+
"not-applicable": "n/a",
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def get(alg_id: str) -> Algorithm:
|
|
179
|
+
return CATALOG[alg_id]
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
# OIDs seen inside certificates and keys -> (algorithm id, parameter hint)
|
|
183
|
+
OIDS: dict[str, tuple[str, str | None]] = {
|
|
184
|
+
"1.2.840.113549.1.1.1": ("rsa", None),
|
|
185
|
+
"1.2.840.113549.1.1.4": ("rsa-sig", "MD5"),
|
|
186
|
+
"1.2.840.113549.1.1.5": ("rsa-sig", "SHA-1"),
|
|
187
|
+
"1.2.840.113549.1.1.10": ("rsa-sig", "PSS"),
|
|
188
|
+
"1.2.840.113549.1.1.11": ("rsa-sig", "SHA-256"),
|
|
189
|
+
"1.2.840.113549.1.1.12": ("rsa-sig", "SHA-384"),
|
|
190
|
+
"1.2.840.113549.1.1.13": ("rsa-sig", "SHA-512"),
|
|
191
|
+
"1.2.840.10045.2.1": ("ec", None),
|
|
192
|
+
"1.2.840.10045.4.1": ("ecdsa", "SHA-1"),
|
|
193
|
+
"1.2.840.10045.4.3.2": ("ecdsa", "SHA-256"),
|
|
194
|
+
"1.2.840.10045.4.3.3": ("ecdsa", "SHA-384"),
|
|
195
|
+
"1.2.840.10045.4.3.4": ("ecdsa", "SHA-512"),
|
|
196
|
+
"1.3.101.110": ("x25519", None),
|
|
197
|
+
"1.3.101.111": ("x448", None),
|
|
198
|
+
"1.3.101.112": ("ed25519", None),
|
|
199
|
+
"1.3.101.113": ("ed448", None),
|
|
200
|
+
"1.2.840.10040.4.1": ("dsa", None),
|
|
201
|
+
"1.2.840.10040.4.3": ("dsa", "SHA-1"),
|
|
202
|
+
"1.2.840.113549.1.3.1": ("dh", None),
|
|
203
|
+
"2.16.840.1.101.3.4.3.17": ("ml-dsa", "ML-DSA-44"),
|
|
204
|
+
"2.16.840.1.101.3.4.3.18": ("ml-dsa", "ML-DSA-65"),
|
|
205
|
+
"2.16.840.1.101.3.4.3.19": ("ml-dsa", "ML-DSA-87"),
|
|
206
|
+
"2.16.840.1.101.3.4.4.1": ("ml-kem", "ML-KEM-512"),
|
|
207
|
+
"2.16.840.1.101.3.4.4.2": ("ml-kem", "ML-KEM-768"),
|
|
208
|
+
"2.16.840.1.101.3.4.4.3": ("ml-kem", "ML-KEM-1024"),
|
|
209
|
+
}
|
|
210
|
+
for _n in range(20, 32):
|
|
211
|
+
OIDS[f"2.16.840.1.101.3.4.3.{_n}"] = ("slh-dsa", None)
|
|
212
|
+
|
|
213
|
+
CURVE_OIDS = {
|
|
214
|
+
"1.2.840.10045.3.1.7": "P-256",
|
|
215
|
+
"1.3.132.0.34": "P-384",
|
|
216
|
+
"1.3.132.0.35": "P-521",
|
|
217
|
+
"1.3.132.0.10": "secp256k1",
|
|
218
|
+
}
|
cbomly/cbom.py
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"""Write findings as a CycloneDX 1.6 CBOM.
|
|
2
|
+
|
|
3
|
+
CycloneDX is the OWASP standard for bills of materials. Version 1.6 added
|
|
4
|
+
"cryptographic-asset" components, which is what a CBOM is: the normal
|
|
5
|
+
software list plus one entry per algorithm, key, certificate and protocol,
|
|
6
|
+
each with evidence of where it was found.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import hashlib
|
|
12
|
+
import uuid
|
|
13
|
+
from collections import OrderedDict
|
|
14
|
+
from datetime import UTC, datetime
|
|
15
|
+
|
|
16
|
+
from . import __version__, algorithms, inventory
|
|
17
|
+
from .cbom_names import display_name
|
|
18
|
+
from .findings import Finding
|
|
19
|
+
from .inventory import status_of
|
|
20
|
+
|
|
21
|
+
SPEC = "1.6"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _ref(kind: str, key: str) -> str:
|
|
25
|
+
return f"crypto/{kind}/{hashlib.sha1(key.encode()).hexdigest()[:12]}"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _occurrence(f: Finding) -> dict:
|
|
29
|
+
occ = {"location": f.file}
|
|
30
|
+
if f.line:
|
|
31
|
+
occ["line"] = f.line
|
|
32
|
+
ctx = f.snippet
|
|
33
|
+
if f.confidence != "high":
|
|
34
|
+
ctx = f"[{f.confidence} confidence] {ctx}"
|
|
35
|
+
if ctx:
|
|
36
|
+
occ["additionalContext"] = ctx[:300]
|
|
37
|
+
return occ
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _algorithm_component(name: str, alg: algorithms.Algorithm, group: list[Finding]) -> dict:
|
|
41
|
+
params = group[0].params
|
|
42
|
+
functions = sorted({f.function for f in group if f.function}) or ["unknown"]
|
|
43
|
+
props = OrderedDict(primitive=alg.primitive)
|
|
44
|
+
if params:
|
|
45
|
+
props["parameterSetIdentifier"] = params
|
|
46
|
+
if group[0].algorithm in ("ec", "ecdsa", "ecdh") and params:
|
|
47
|
+
props["curve"] = params
|
|
48
|
+
modes = {f.extra.get("mode") for f in group}
|
|
49
|
+
if len(modes) == 1 and None not in modes:
|
|
50
|
+
mode = modes.pop()
|
|
51
|
+
props["mode"] = mode if mode in ("cbc", "ecb", "ccm", "gcm", "cfb", "ofb", "ctr") else "other"
|
|
52
|
+
props["executionEnvironment"] = "software-plain-ram"
|
|
53
|
+
props["implementationPlatform"] = "unknown"
|
|
54
|
+
props["cryptoFunctions"] = functions
|
|
55
|
+
if alg.classical_security_bits is not None:
|
|
56
|
+
bits = alg.classical_security_bits
|
|
57
|
+
if group[0].algorithm in ("rsa", "rsa-sig", "dh") and params and params.isdigit():
|
|
58
|
+
bits = {1024: 80, 2048: 112, 3072: 128, 4096: 152, 7680: 192, 8192: 200, 15360: 256}.get(int(params), bits)
|
|
59
|
+
props["classicalSecurityLevel"] = bits
|
|
60
|
+
props["nistQuantumSecurityLevel"] = alg.nist_quantum_level
|
|
61
|
+
crypto = {"assetType": "algorithm", "algorithmProperties": dict(props)}
|
|
62
|
+
if alg.oid:
|
|
63
|
+
crypto["oid"] = alg.oid
|
|
64
|
+
return {
|
|
65
|
+
"type": "cryptographic-asset",
|
|
66
|
+
"name": name,
|
|
67
|
+
"cryptoProperties": crypto,
|
|
68
|
+
"evidence": {"occurrences": [_occurrence(f) for f in group]},
|
|
69
|
+
"properties": _props(alg, status_of(group[0].algorithm, group[0].key_bits)),
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _props(alg: algorithms.Algorithm, status: str) -> list[dict]:
|
|
74
|
+
return [
|
|
75
|
+
{"name": "cbomly:status", "value": status},
|
|
76
|
+
{"name": "cbomly:quantum-status", "value": alg.quantum},
|
|
77
|
+
{"name": "cbomly:classical-status", "value": alg.classical},
|
|
78
|
+
{"name": "cbomly:recommended-replacement", "value": alg.replacement},
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def build(findings: list[Finding], sbom: dict | None, target_name: str, crypto_libs: list[dict] | None = None,
|
|
83
|
+
tool_versions: dict | None = None, services: list[dict] | None = None) -> dict:
|
|
84
|
+
now = datetime.now(UTC).replace(microsecond=0).isoformat().replace("+00:00", "Z")
|
|
85
|
+
bom: dict = {
|
|
86
|
+
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
|
|
87
|
+
"bomFormat": "CycloneDX",
|
|
88
|
+
"specVersion": SPEC,
|
|
89
|
+
"serialNumber": f"urn:uuid:{uuid.uuid4()}",
|
|
90
|
+
"version": 1,
|
|
91
|
+
"metadata": {
|
|
92
|
+
"timestamp": now,
|
|
93
|
+
"tools": {"components": [
|
|
94
|
+
{"type": "application", "name": "cbomly", "version": __version__,
|
|
95
|
+
"externalReferences": [{"type": "website", "url": "https://cbomly.com"}]},
|
|
96
|
+
]},
|
|
97
|
+
"component": {"type": "application", "name": target_name, "bom-ref": "root"},
|
|
98
|
+
},
|
|
99
|
+
"components": [],
|
|
100
|
+
"dependencies": [],
|
|
101
|
+
}
|
|
102
|
+
for name, ver in (tool_versions or {}).items():
|
|
103
|
+
bom["metadata"]["tools"]["components"].append({"type": "application", "name": name, "version": ver})
|
|
104
|
+
|
|
105
|
+
# 1. software components from Syft
|
|
106
|
+
if sbom:
|
|
107
|
+
root = (sbom.get("metadata") or {}).get("component")
|
|
108
|
+
if root:
|
|
109
|
+
root = dict(root)
|
|
110
|
+
root["bom-ref"] = root.get("bom-ref") or "root"
|
|
111
|
+
if not root.get("name") or root["name"] in (".", "/"):
|
|
112
|
+
root["name"] = target_name
|
|
113
|
+
bom["metadata"]["component"] = root
|
|
114
|
+
bom["components"].extend(sbom.get("components", []))
|
|
115
|
+
bom["dependencies"].extend(sbom.get("dependencies", []))
|
|
116
|
+
root_ref = bom["metadata"]["component"]["bom-ref"]
|
|
117
|
+
|
|
118
|
+
# 2. crypto-library tagging
|
|
119
|
+
lib_refs = {lib["bom-ref"]: lib for lib in (crypto_libs or []) if lib.get("bom-ref")}
|
|
120
|
+
for c in bom["components"]:
|
|
121
|
+
lib = lib_refs.get(c.get("bom-ref"))
|
|
122
|
+
if lib:
|
|
123
|
+
c.setdefault("properties", []).append({"name": "cbomly:crypto-library", "value": lib["library"]})
|
|
124
|
+
if lib.get("vendor"):
|
|
125
|
+
c["properties"].append({"name": "cbomly:crypto-vendor", "value": lib["vendor"]})
|
|
126
|
+
c.setdefault("supplier", {"name": lib["vendor"]})
|
|
127
|
+
if lib["pqc_ready"] is not None:
|
|
128
|
+
c["properties"].append({"name": "cbomly:pqc-capable-version", "value": str(lib["pqc_ready"]).lower()})
|
|
129
|
+
|
|
130
|
+
# 3. algorithms (grouped), protocols
|
|
131
|
+
crypto_refs: list[str] = []
|
|
132
|
+
groups: OrderedDict[str, list[Finding]] = OrderedDict()
|
|
133
|
+
for f in findings:
|
|
134
|
+
groups.setdefault(display_name(f), []).append(f)
|
|
135
|
+
alg_ref_by_name: dict[str, str] = {}
|
|
136
|
+
for name, group in groups.items():
|
|
137
|
+
alg = algorithms.get(group[0].algorithm)
|
|
138
|
+
ref = _ref("algorithm" if alg.asset_type == "algorithm" else "protocol", name)
|
|
139
|
+
alg_ref_by_name[name] = ref
|
|
140
|
+
if alg.asset_type == "protocol":
|
|
141
|
+
version = alg.name.split(" ")[-1]
|
|
142
|
+
comp = {
|
|
143
|
+
"type": "cryptographic-asset", "name": alg.name, "bom-ref": ref,
|
|
144
|
+
"cryptoProperties": {"assetType": "protocol", "protocolProperties": {
|
|
145
|
+
"type": "tls" if alg.id.startswith("tls") else "other", "version": version}},
|
|
146
|
+
"evidence": {"occurrences": [_occurrence(f) for f in group]},
|
|
147
|
+
"properties": _props(alg, status_of(alg.id)),
|
|
148
|
+
}
|
|
149
|
+
else:
|
|
150
|
+
comp = _algorithm_component(name, alg, group)
|
|
151
|
+
comp = {"type": comp.pop("type"), "name": comp.pop("name"), "bom-ref": ref, **comp}
|
|
152
|
+
bom["components"].append(comp)
|
|
153
|
+
crypto_refs.append(ref)
|
|
154
|
+
|
|
155
|
+
# 4. keys and certificates as their own assets
|
|
156
|
+
inv = inventory.build(findings)
|
|
157
|
+
for k in inv["keys"]:
|
|
158
|
+
ref = _ref("key", f"{k['file']}:{k['line']}:{k['type']}")
|
|
159
|
+
prop = {"type": k["type"], "algorithmRef": alg_ref_by_name[k["algorithm"]]}
|
|
160
|
+
if k["size"]:
|
|
161
|
+
prop["size"] = k["size"]
|
|
162
|
+
props = [{"name": "cbomly:status", "value": k["status"]}]
|
|
163
|
+
if k["type"] == "private-key":
|
|
164
|
+
props.append({"name": "cbomly:private-key-in-code", "value": "true"})
|
|
165
|
+
bom["components"].append({
|
|
166
|
+
"type": "cryptographic-asset", "name": k["name"], "bom-ref": ref,
|
|
167
|
+
"cryptoProperties": {"assetType": "related-crypto-material", "relatedCryptoMaterialProperties": prop},
|
|
168
|
+
"evidence": {"occurrences": [{"location": k["file"], **({"line": k["line"]} if k["line"] else {})}]},
|
|
169
|
+
"properties": props,
|
|
170
|
+
})
|
|
171
|
+
crypto_refs.append(ref)
|
|
172
|
+
for c in inv["certificates"]:
|
|
173
|
+
ref = _ref("certificate", f"{c['file']}:{c['line']}")
|
|
174
|
+
cp = {"certificateFormat": "X.509"}
|
|
175
|
+
for src, dst in (("subject", "subjectName"), ("issuer", "issuerName"),
|
|
176
|
+
("not_before", "notValidBefore"), ("not_after", "notValidAfter")):
|
|
177
|
+
if c.get(src):
|
|
178
|
+
cp[dst] = c[src]
|
|
179
|
+
if c["signature"]:
|
|
180
|
+
cp["signatureAlgorithmRef"] = alg_ref_by_name[c["signature"]]
|
|
181
|
+
if c["public_key"]:
|
|
182
|
+
cp["subjectPublicKeyRef"] = alg_ref_by_name[c["public_key"]]
|
|
183
|
+
props = [{"name": "cbomly:status", "value": c["status"]}]
|
|
184
|
+
if c["public_key"]:
|
|
185
|
+
props.append({"name": "cbomly:public-key", "value": c["public_key"]})
|
|
186
|
+
if c["expired"] is not None:
|
|
187
|
+
props.append({"name": "cbomly:expired", "value": str(c["expired"]).lower()})
|
|
188
|
+
if c["self_signed"]:
|
|
189
|
+
props.append({"name": "cbomly:self-signed", "value": "true"})
|
|
190
|
+
bom["components"].append({
|
|
191
|
+
"type": "cryptographic-asset", "name": f"Certificate: {c['subject'] or c['file']}", "bom-ref": ref,
|
|
192
|
+
"cryptoProperties": {"assetType": "certificate", "certificateProperties": cp},
|
|
193
|
+
"evidence": {"occurrences": [{"location": c["file"], **({"line": c["line"]} if c["line"] else {})}]},
|
|
194
|
+
"properties": props,
|
|
195
|
+
})
|
|
196
|
+
crypto_refs.append(ref)
|
|
197
|
+
|
|
198
|
+
# 5. third-party crypto services (KMS, HSM, secret stores, CAs) as CycloneDX services
|
|
199
|
+
seen_svc: dict[str, dict] = {}
|
|
200
|
+
for svc in services or []:
|
|
201
|
+
e = seen_svc.get(svc["service"])
|
|
202
|
+
if e is None:
|
|
203
|
+
e = {"bom-ref": _ref("service", svc["service"]), "provider": {"name": svc["vendor"]},
|
|
204
|
+
"name": svc["service"],
|
|
205
|
+
"properties": [{"name": "cbomly:service-kind", "value": svc["kind"]}]}
|
|
206
|
+
seen_svc[svc["service"]] = e
|
|
207
|
+
bom.setdefault("services", []).append(e)
|
|
208
|
+
e["properties"].append({"name": "cbomly:location", "value": f"{svc['file']}:{svc['line']}"})
|
|
209
|
+
|
|
210
|
+
if crypto_refs:
|
|
211
|
+
existing = next((d for d in bom["dependencies"] if d.get("ref") == root_ref), None)
|
|
212
|
+
if existing is None:
|
|
213
|
+
existing = {"ref": root_ref, "dependsOn": []}
|
|
214
|
+
bom["dependencies"].append(existing)
|
|
215
|
+
existing.setdefault("dependsOn", []).extend(crypto_refs)
|
|
216
|
+
return bom
|
cbomly/cbom_names.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Readable, stable names for crypto assets (shared by the CBOM, inventory and report)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
from . import algorithms
|
|
8
|
+
from .findings import Finding
|
|
9
|
+
|
|
10
|
+
_HASH_RE = re.compile(r"^(?:SHA-?\d+|SHA3-?\d+|MD5|PSS)$", re.IGNORECASE)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _norm_hash(p: str) -> str:
|
|
14
|
+
m = re.match(r"^(SHA3?)-?(\d+)$", p, re.IGNORECASE)
|
|
15
|
+
return f"{m.group(1).upper()}-{m.group(2)}" if m else p.upper()
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def display_name(f: Finding) -> str:
|
|
19
|
+
"""A stable, readable name such as "RSA-2048", "ECDSA P-256" or "RSA signature (SHA-256)"."""
|
|
20
|
+
a = algorithms.get(f.algorithm)
|
|
21
|
+
base = {"rsa-sig": "RSA signature", "ec": "EC"}.get(f.algorithm, a.name.split(" (")[0])
|
|
22
|
+
p = f.params
|
|
23
|
+
if not p:
|
|
24
|
+
return a.name if f.algorithm not in ("rsa-sig",) else base
|
|
25
|
+
if f.algorithm in ("ml-kem", "ml-dsa"):
|
|
26
|
+
return p.upper() if p.upper().startswith("ML-") else f"{base}-{p}"
|
|
27
|
+
if _HASH_RE.match(p):
|
|
28
|
+
return f"{base} ({_norm_hash(p)})"
|
|
29
|
+
if f.algorithm == "sha3" and p.isdigit():
|
|
30
|
+
return f"SHA3-{p}"
|
|
31
|
+
if p.isdigit():
|
|
32
|
+
return f"{base}-{p}" if f.algorithm != "rsa-sig" else f"RSA-{p} signature"
|
|
33
|
+
if f.algorithm in ("ec", "ecdsa", "ecdh"):
|
|
34
|
+
return f"{base} {p}"
|
|
35
|
+
return f"{base} ({p})"
|