certgraph 0.1.0__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Chris Adshead
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,42 @@
1
+ Metadata-Version: 2.4
2
+ Name: certgraph
3
+ Version: 0.1.0
4
+ Summary: Utility for exploring and mapping X509 certificate chains.
5
+ Author: Chris Adshead
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Chris Adshead
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Requires-Python: >=3.12
29
+ Description-Content-Type: text/markdown
30
+ License-File: LICENSE
31
+ Requires-Dist: cryptography
32
+ Requires-Dist: networkx
33
+ Requires-Dist: pydot
34
+ Requires-Dist: rapidfuzz
35
+ Provides-Extra: dev
36
+ Requires-Dist: black; extra == "dev"
37
+ Requires-Dist: pytest; extra == "dev"
38
+ Requires-Dist: pytest-cov; extra == "dev"
39
+ Dynamic: license-file
40
+
41
+ # certgraph
42
+ Python package for displaying X509 certificate chains.
@@ -0,0 +1,2 @@
1
+ # certgraph
2
+ Python package for displaying X509 certificate chains.
@@ -0,0 +1,33 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "certgraph"
7
+ version = "0.1.0"
8
+ description = "Utility for exploring and mapping X509 certificate chains."
9
+ readme = "README.md"
10
+ requires-python = ">=3.12"
11
+ license = { file = "LICENSE" }
12
+ authors = [
13
+ { name = "Chris Adshead" },
14
+ ]
15
+ dependencies = [
16
+ "cryptography",
17
+ "networkx",
18
+ "pydot",
19
+ "rapidfuzz",
20
+ ]
21
+
22
+ [project.optional-dependencies]
23
+ dev = [
24
+ "black",
25
+ "pytest",
26
+ "pytest-cov",
27
+ ]
28
+
29
+ [tool.setuptools.packages.find]
30
+ where = ["src"]
31
+
32
+ [tool.pytest.ini_options]
33
+ testpaths = ["tests"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,5 @@
1
+ """certgraph: Utility for exploring and mapping X509 certificate chains."""
2
+
3
+ from certgraph.certgraph import certgraph
4
+
5
+ __all__ = ["certgraph"]
@@ -0,0 +1,109 @@
1
+ from cryptography import x509
2
+ from cryptography.hazmat.primitives import hashes
3
+ import networkx as nx
4
+ from rapidfuzz import fuzz
5
+
6
+
7
+ class certgraph:
8
+ def __init__(self) -> None:
9
+ self._certlist: set[x509.Certificate] = []
10
+ self._graph = nx.DiGraph()
11
+
12
+ def import_certificates(
13
+ self,
14
+ certificates: (
15
+ x509.Certificate | str | bytes | list[x509.Certificate | str | bytes]
16
+ ),
17
+ pem_encoding: str = "utf-8",
18
+ ) -> certgraph:
19
+ new_certs: set[x509.Certificate] = []
20
+
21
+ # If there's only a non-list object being handing in, turn it into an array to keep the same iteration code
22
+ for i, cert in enumerate(
23
+ certificates if isinstance(certificates, list) else [certificates]
24
+ ):
25
+ if isinstance(cert, x509.Certificate):
26
+ # No conversion neccessary
27
+ new_certs.append(cert)
28
+ elif isinstance(cert, str):
29
+ # PEM encoded
30
+ pem_certs = x509.load_pem_x509_certificates(cert.encode(pem_encoding))
31
+ new_certs.extend(pem_certs)
32
+ elif isinstance(cert, bytes):
33
+ # DER encoded
34
+ der_cert = x509.load_der_x509_certificate(cert)
35
+ new_certs.append(der_cert)
36
+ else:
37
+ raise TypeError(
38
+ f"Cannot import certificate {i} from data type {type(cert)}"
39
+ )
40
+
41
+ self._certlist.extend(new_certs)
42
+ self._graph = self._generate_graph(self._certlist)
43
+
44
+ return self
45
+
46
+ def _generate_graph(self, certificates: set[x509.Certificate]) -> nx.DiGraph:
47
+ G = nx.DiGraph()
48
+
49
+ # Pass 1: add nodes and build a subject_dn -> fingerprint index
50
+ subject_index: dict[str, list[str]] = {}
51
+ for cert in certificates:
52
+ fingerprint = cert.fingerprint(hashes.SHA256()).hex()
53
+ G.add_node(fingerprint, certificate=cert)
54
+
55
+ subject_dn = cert.subject.rfc4514_string()
56
+ subject_index.setdefault(subject_dn, []).append(fingerprint)
57
+
58
+ # Pass 2: add edges using the index
59
+ for fingerprint, data in G.nodes(data=True):
60
+ issuer_dn = data["certificate"].issuer.rfc4514_string()
61
+
62
+ for issuer_fingerprint in subject_index.get(issuer_dn, []):
63
+ G.add_edge(issuer_fingerprint, fingerprint)
64
+
65
+ return G
66
+
67
+ def report_fingerprint_edges(self) -> list[str]:
68
+ return [f"{edge[0][:8]} -> {edge[1][:8]}" for edge in self._graph.edges()]
69
+
70
+ def export_dot(self, format: str = "svg") -> str:
71
+ allowed_types = ["svg", "png"]
72
+
73
+ if format not in allowed_types:
74
+ raise ValueError(
75
+ f"Cannot export dot graph of type {format} - must be one of {allowed_types}"
76
+ )
77
+
78
+ dot_graph = nx.DiGraph()
79
+
80
+ for fingerprint, data in self._graph.nodes(data=True):
81
+ label = data["certificate"].subject.rfc4514_string()
82
+ dot_graph.add_node(fingerprint, label=label)
83
+ dot_graph.add_edges_from(self._graph.edges())
84
+
85
+ nx.drawing.nx_pydot.write_dot(dot_graph, "./test_out.dot")
86
+
87
+ return "dot"
88
+
89
+ def clear(self) -> certgraph:
90
+ self._certlist.clear()
91
+ self._graph.clear()
92
+ return self
93
+
94
+ def fingerprint_from_distinguished_name(self, dn: str, cutoff: int = 0) -> str:
95
+ # Evaluate the nodes
96
+ nodes: list[tuple[str, dict]] = list(self._graph.nodes(data=True))
97
+
98
+ # Sort using a fuzzy-search
99
+ ranked = sorted(
100
+ nodes,
101
+ key=lambda t: fuzz.ratio(
102
+ dn, t[1]["certificate"].subject.rfc4514_string(), score_cutoff=cutoff
103
+ )
104
+ )
105
+
106
+ if not ranked:
107
+ return None
108
+
109
+ return ranked[-1][0]
@@ -0,0 +1,42 @@
1
+ Metadata-Version: 2.4
2
+ Name: certgraph
3
+ Version: 0.1.0
4
+ Summary: Utility for exploring and mapping X509 certificate chains.
5
+ Author: Chris Adshead
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Chris Adshead
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Requires-Python: >=3.12
29
+ Description-Content-Type: text/markdown
30
+ License-File: LICENSE
31
+ Requires-Dist: cryptography
32
+ Requires-Dist: networkx
33
+ Requires-Dist: pydot
34
+ Requires-Dist: rapidfuzz
35
+ Provides-Extra: dev
36
+ Requires-Dist: black; extra == "dev"
37
+ Requires-Dist: pytest; extra == "dev"
38
+ Requires-Dist: pytest-cov; extra == "dev"
39
+ Dynamic: license-file
40
+
41
+ # certgraph
42
+ Python package for displaying X509 certificate chains.
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/certgraph/__init__.py
5
+ src/certgraph/certgraph.py
6
+ src/certgraph.egg-info/PKG-INFO
7
+ src/certgraph.egg-info/SOURCES.txt
8
+ src/certgraph.egg-info/dependency_links.txt
9
+ src/certgraph.egg-info/requires.txt
10
+ src/certgraph.egg-info/top_level.txt
@@ -0,0 +1,9 @@
1
+ cryptography
2
+ networkx
3
+ pydot
4
+ rapidfuzz
5
+
6
+ [dev]
7
+ black
8
+ pytest
9
+ pytest-cov
@@ -0,0 +1 @@
1
+ certgraph