Debian Codegraph @

dns/dnssecalgs/__init__.py

raw
1 from typing import Dict, Tuple, Type
2
3 import dns._features
4 import dns.name
5 from dns.dnssecalgs.base import GenericPrivateKey
6 from dns.dnssectypes import Algorithm
7 from dns.exception import UnsupportedAlgorithm
8 from dns.rdtypes.ANY.DNSKEY import DNSKEY
9
10 # pyright: reportPossiblyUnboundVariable=false
11
12 if dns._features.have("dnssec"):
13 from dns.dnssecalgs.dsa import PrivateDSA, PrivateDSANSEC3SHA1
14 from dns.dnssecalgs.ecdsa import PrivateECDSAP256SHA256, PrivateECDSAP384SHA384
15 from dns.dnssecalgs.eddsa import PrivateED448, PrivateED25519
16 from dns.dnssecalgs.rsa import (
17 PrivateRSAMD5,
18 PrivateRSASHA1,
19 PrivateRSASHA1NSEC3SHA1,
20 PrivateRSASHA256,
21 PrivateRSASHA512,
22 )
23
24 _have_cryptography = True
25 else:
26 _have_cryptography = False
27
28 AlgorithmPrefix = bytes | dns.name.Name | None
29
30 algorithms: Dict[Tuple[Algorithm, AlgorithmPrefix], Type[GenericPrivateKey]] = {}
31 if _have_cryptography:
32 # pylint: disable=possibly-used-before-assignment
33 algorithms.update(
34 {
35 (Algorithm.RSAMD5, None): PrivateRSAMD5,
36 (Algorithm.DSA, None): PrivateDSA,
37 (Algorithm.RSASHA1, None): PrivateRSASHA1,
38 (Algorithm.DSANSEC3SHA1, None): PrivateDSANSEC3SHA1,
39 (Algorithm.RSASHA1NSEC3SHA1, None): PrivateRSASHA1NSEC3SHA1,
40 (Algorithm.RSASHA256, None): PrivateRSASHA256,
41 (Algorithm.RSASHA512, None): PrivateRSASHA512,
42 (Algorithm.ECDSAP256SHA256, None): PrivateECDSAP256SHA256,
43 (Algorithm.ECDSAP384SHA384, None): PrivateECDSAP384SHA384,
44 (Algorithm.ED25519, None): PrivateED25519,
45 (Algorithm.ED448, None): PrivateED448,
46 }
47 )
48
49
50 def get_algorithm_cls(
51 algorithm: int | str, prefix: AlgorithmPrefix = None
52 ) -> Type[GenericPrivateKey]:
53 """Get Private Key class from Algorithm.
54
55 *algorithm*, a ``str`` or ``int`` specifying the DNSKEY algorithm.
56
57 Raises ``UnsupportedAlgorithm`` if the algorithm is unknown.
58
59 Returns a ``dns.dnssecalgs.GenericPrivateKey``
60 """
61 algorithm = Algorithm.make(algorithm)
62 cls = algorithms.get((algorithm, prefix))
63 if cls:
64 return cls
65 raise UnsupportedAlgorithm(
66 f'algorithm "{Algorithm.to_text(algorithm)}" not supported by dnspython'
67 )
68
69
70 def get_algorithm_cls_from_dnskey(dnskey: DNSKEY) -> Type[GenericPrivateKey]:
71 """Get Private Key class from DNSKEY.
72
73 *dnskey*, a ``DNSKEY`` to get Algorithm class for.
74
75 Raises ``UnsupportedAlgorithm`` if the algorithm is unknown.
76
77 Returns a ``dns.dnssecalgs.GenericPrivateKey``
78 """
79 prefix: AlgorithmPrefix = None
80 if dnskey.algorithm == Algorithm.PRIVATEDNS:
81 prefix, _ = dns.name.from_wire(dnskey.key, 0)
82 elif dnskey.algorithm == Algorithm.PRIVATEOID:
83 length = int(dnskey.key[0])
84 prefix = dnskey.key[0 : length + 1]
85 return get_algorithm_cls(dnskey.algorithm, prefix)
86
87
88 def register_algorithm_cls(
89 algorithm: int | str,
90 algorithm_cls: Type[GenericPrivateKey],
91 name: dns.name.Name | str | None = None,
92 oid: bytes | None = None,
93 ) -> None:
94 """Register Algorithm Private Key class.
95
96 *algorithm*, a ``str`` or ``int`` specifying the DNSKEY algorithm.
97
98 *algorithm_cls*: A `GenericPrivateKey` class.
99
100 *name*, an optional ``dns.name.Name`` or ``str``, for for PRIVATEDNS algorithms.
101
102 *oid*: an optional BER-encoded `bytes` for PRIVATEOID algorithms.
103
104 Raises ``ValueError`` if a name or oid is specified incorrectly.
105 """
106 if not issubclass(algorithm_cls, GenericPrivateKey):
107 raise TypeError("Invalid algorithm class")
108 algorithm = Algorithm.make(algorithm)
109 prefix: AlgorithmPrefix = None
110 if algorithm == Algorithm.PRIVATEDNS:
111 if name is None:
112 raise ValueError("Name required for PRIVATEDNS algorithms")
113 if isinstance(name, str):
114 name = dns.name.from_text(name)
115 prefix = name
116 elif algorithm == Algorithm.PRIVATEOID:
117 if oid is None:
118 raise ValueError("OID required for PRIVATEOID algorithms")
119 prefix = bytes([len(oid)]) + oid
120 elif name:
121 raise ValueError("Name only supported for PRIVATEDNS algorithm")
122 elif oid:
123 raise ValueError("OID only supported for PRIVATEOID algorithm")
124 algorithms[(algorithm, prefix)] = algorithm_cls