Debian Codegraph @

dns/dnssecalgs/rsa.py

raw
1 import math
2 import struct
3
4 from cryptography.hazmat.backends import default_backend
5 from cryptography.hazmat.primitives import hashes
6 from cryptography.hazmat.primitives.asymmetric import padding, rsa
7
8 from dns.dnssecalgs.cryptography import CryptographyPrivateKey, CryptographyPublicKey
9 from dns.dnssectypes import Algorithm
10 from dns.rdtypes.ANY.DNSKEY import DNSKEY
11
12
13 class PublicRSA(CryptographyPublicKey):
14 key: rsa.RSAPublicKey
15 key_cls = rsa.RSAPublicKey
16 algorithm: Algorithm
17 chosen_hash: hashes.HashAlgorithm
18
19 def verify(self, signature: bytes, data: bytes) -> None:
20 self.key.verify(signature, data, padding.PKCS1v15(), self.chosen_hash)
21
22 def encode_key_bytes(self) -> bytes:
23 """Encode a public key per RFC 3110, section 2."""
24 pn = self.key.public_numbers()
25 _exp_len = math.ceil(int.bit_length(pn.e) / 8)
26 exp = int.to_bytes(pn.e, length=_exp_len, byteorder="big")
27 if _exp_len > 255:
28 exp_header = b"\0" + struct.pack("!H", _exp_len)
29 else:
30 exp_header = struct.pack("!B", _exp_len)
31 if pn.n.bit_length() < 512 or pn.n.bit_length() > 4096:
32 raise ValueError("unsupported RSA key length")
33 return exp_header + exp + pn.n.to_bytes((pn.n.bit_length() + 7) // 8, "big")
34
35 @classmethod
36 def from_dnskey(cls, key: DNSKEY) -> "PublicRSA":
37 cls._ensure_algorithm_key_combination(key)
38 keyptr = key.key
39 (bytes_,) = struct.unpack("!B", keyptr[0:1])
40 keyptr = keyptr[1:]
41 if bytes_ == 0:
42 (bytes_,) = struct.unpack("!H", keyptr[0:2])
43 keyptr = keyptr[2:]
44 rsa_e = keyptr[0:bytes_]
45 rsa_n = keyptr[bytes_:]
46 return cls(
47 key=rsa.RSAPublicNumbers(
48 int.from_bytes(rsa_e, "big"), int.from_bytes(rsa_n, "big")
49 ).public_key(default_backend())
50 )
51
52
53 class PrivateRSA(CryptographyPrivateKey):
54 key: rsa.RSAPrivateKey
55 key_cls = rsa.RSAPrivateKey
56 public_cls = PublicRSA
57 default_public_exponent = 65537
58
59 def sign(
60 self,
61 data: bytes,
62 verify: bool = False,
63 deterministic: bool = True,
64 ) -> bytes:
65 """Sign using a private key per RFC 3110, section 3."""
66 signature = self.key.sign(
67 data, padding.PKCS1v15(), self.public_cls.chosen_hash # pyright: ignore
68 )
69 if verify:
70 self.public_key().verify(signature, data)
71 return signature
72
73 @classmethod
74 def generate(cls, key_size: int) -> "PrivateRSA":
75 return cls(
76 key=rsa.generate_private_key(
77 public_exponent=cls.default_public_exponent,
78 key_size=key_size,
79 backend=default_backend(),
80 )
81 )
82
83
84 class PublicRSAMD5(PublicRSA):
85 algorithm = Algorithm.RSAMD5
86 chosen_hash = hashes.MD5()
87
88
89 class PrivateRSAMD5(PrivateRSA):
90 public_cls = PublicRSAMD5
91
92
93 class PublicRSASHA1(PublicRSA):
94 algorithm = Algorithm.RSASHA1
95 chosen_hash = hashes.SHA1()
96
97
98 class PrivateRSASHA1(PrivateRSA):
99 public_cls = PublicRSASHA1
100
101
102 class PublicRSASHA1NSEC3SHA1(PublicRSA):
103 algorithm = Algorithm.RSASHA1NSEC3SHA1
104 chosen_hash = hashes.SHA1()
105
106
107 class PrivateRSASHA1NSEC3SHA1(PrivateRSA):
108 public_cls = PublicRSASHA1NSEC3SHA1
109
110
111 class PublicRSASHA256(PublicRSA):
112 algorithm = Algorithm.RSASHA256
113 chosen_hash = hashes.SHA256()
114
115
116 class PrivateRSASHA256(PrivateRSA):
117 public_cls = PublicRSASHA256
118
119
120 class PublicRSASHA512(PublicRSA):
121 algorithm = Algorithm.RSASHA512
122 chosen_hash = hashes.SHA512()
123
124
125 class PrivateRSASHA512(PrivateRSA):
126 public_cls = PublicRSASHA512