| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
# Copyright (C) 2011 Nominum, Inc. |
| 4 |
# |
| 5 |
# Permission to use, copy, modify, and distribute this software and its |
| 6 |
# documentation for any purpose with or without fee is hereby granted, |
| 7 |
# provided that the above copyright notice and this permission notice |
| 8 |
# appear in all copies. |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES |
| 11 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR |
| 13 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 16 |
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
|
| 18 |
import os |
| 19 |
import unittest |
| 20 |
|
| 21 |
import dns.dnssec |
| 22 |
import dns.exception |
| 23 |
from dns.dnssectypes import Algorithm |
| 24 |
from dns.rdtypes.ANY.DNSKEY import DNSKEY |
| 25 |
|
| 26 |
try: |
| 27 |
from dns.dnssecalgs import ( |
| 28 |
get_algorithm_cls, |
| 29 |
get_algorithm_cls_from_dnskey, |
| 30 |
register_algorithm_cls, |
| 31 |
) |
| 32 |
from dns.dnssecalgs.dsa import PrivateDSA, PrivateDSANSEC3SHA1 |
| 33 |
from dns.dnssecalgs.ecdsa import PrivateECDSAP256SHA256, PrivateECDSAP384SHA384 |
| 34 |
from dns.dnssecalgs.eddsa import PrivateED448, PrivateED25519, PublicED25519 |
| 35 |
from dns.dnssecalgs.rsa import ( |
| 36 |
PrivateRSAMD5, |
| 37 |
PrivateRSASHA1, |
| 38 |
PrivateRSASHA1NSEC3SHA1, |
| 39 |
PrivateRSASHA256, |
| 40 |
PrivateRSASHA512, |
| 41 |
) |
| 42 |
except ImportError: |
| 43 |
pass # Cryptography ImportError already handled in dns.dnssec |
| 44 |
|
| 45 |
|
| 46 |
@unittest.skipUnless(dns.dnssec._have_pyca, "Python Cryptography cannot be imported") |
| 47 |
class DNSSECAlgorithm(unittest.TestCase): |
| 48 |
def _test_dnssec_alg(self, private_cls, key_size=None): |
| 49 |
public_cls = private_cls.public_cls |
| 50 |
|
| 51 |
private_key = ( |
| 52 |
private_cls.generate(key_size) if key_size else private_cls.generate() |
| 53 |
) |
| 54 |
|
| 55 |
# sign random data |
| 56 |
data = os.urandom(1024) |
| 57 |
signature = private_key.sign(data, verify=True) |
| 58 |
|
| 59 |
# validate signature using public key |
| 60 |
public_key = private_key.public_key() |
| 61 |
public_key.verify(signature, data) |
| 62 |
|
| 63 |
# create DNSKEY |
| 64 |
dnskey = public_key.to_dnskey() |
| 65 |
dnskey2 = public_cls.from_dnskey(dnskey).to_dnskey() |
| 66 |
self.assertEqual(dnskey, dnskey2) |
| 67 |
|
| 68 |
# test cryptography keys |
| 69 |
_ = private_cls(key=private_key.key) |
| 70 |
_ = public_cls(key=public_key.key) |
| 71 |
|
| 72 |
# to/from PEM |
| 73 |
password = b"mekmitasdigoat" |
| 74 |
private_pem = private_key.to_pem() |
| 75 |
private_pem_encrypted = private_key.to_pem(password=password) |
| 76 |
public_pem = public_key.to_pem() |
| 77 |
_ = private_cls.from_pem(private_pem) |
| 78 |
_ = private_cls.from_pem(private_pem_encrypted, password) |
| 79 |
_ = public_cls.from_pem(public_pem) |
| 80 |
|
| 81 |
def test_rsa(self): |
| 82 |
self._test_dnssec_alg(PrivateRSAMD5, 2048) |
| 83 |
self._test_dnssec_alg(PrivateRSASHA1, 2048) |
| 84 |
self._test_dnssec_alg(PrivateRSASHA1NSEC3SHA1, 2048) |
| 85 |
self._test_dnssec_alg(PrivateRSASHA256, 2048) |
| 86 |
self._test_dnssec_alg(PrivateRSASHA512, 2048) |
| 87 |
|
| 88 |
def test_dsa(self): |
| 89 |
self._test_dnssec_alg(PrivateDSA, 1024) |
| 90 |
self._test_dnssec_alg(PrivateDSANSEC3SHA1, 1024) |
| 91 |
with self.assertRaises(ValueError): |
| 92 |
k = PrivateDSA.generate(2048) |
| 93 |
k.sign(b"hello") |
| 94 |
|
| 95 |
def test_ecdsa(self): |
| 96 |
self._test_dnssec_alg(PrivateECDSAP256SHA256) |
| 97 |
self._test_dnssec_alg(PrivateECDSAP384SHA384) |
| 98 |
|
| 99 |
def test_eddsa(self): |
| 100 |
self._test_dnssec_alg(PrivateED25519) |
| 101 |
self._test_dnssec_alg(PrivateED448) |
| 102 |
|
| 103 |
def test_algorithm_mismatch(self): |
| 104 |
private_key_ed448 = PrivateED448.generate() |
| 105 |
dnskey_ed448 = private_key_ed448.public_key().to_dnskey() |
| 106 |
with self.assertRaises(dns.exception.AlgorithmKeyMismatch): |
| 107 |
PublicED25519.from_dnskey(dnskey_ed448) |
| 108 |
|
| 109 |
|
| 110 |
@unittest.skipUnless(dns.dnssec._have_pyca, "Python Cryptography cannot be imported") |
| 111 |
class DNSSECAlgorithmPrivateAlgorithm(unittest.TestCase): |
| 112 |
def test_private(self): |
| 113 |
class PublicExampleAlgorithm(PublicED25519): |
| 114 |
algorithm = Algorithm.PRIVATEDNS |
| 115 |
name = dns.name.from_text("algorithm.example.com") |
| 116 |
|
| 117 |
def encode_key_bytes(self) -> bytes: |
| 118 |
return self.name.to_wire() + super().encode_key_bytes() |
| 119 |
|
| 120 |
@classmethod |
| 121 |
def from_dnskey(cls, key: DNSKEY) -> "PublicEDDSA": |
| 122 |
return cls( |
| 123 |
key=cls.key_cls.from_public_bytes( |
| 124 |
key.key[len(cls.name.to_wire()) :] |
| 125 |
), |
| 126 |
) |
| 127 |
|
| 128 |
class PrivateExampleAlgorithm(PrivateED25519): |
| 129 |
public_cls = PublicExampleAlgorithm |
| 130 |
|
| 131 |
register_algorithm_cls( |
| 132 |
algorithm=Algorithm.PRIVATEDNS, |
| 133 |
algorithm_cls=PrivateExampleAlgorithm, |
| 134 |
name=PublicExampleAlgorithm.name, |
| 135 |
) |
| 136 |
|
| 137 |
private_key = PrivateExampleAlgorithm.generate() |
| 138 |
public_key = private_key.public_key() |
| 139 |
|
| 140 |
name = dns.name.from_text("example.com") |
| 141 |
rdataset = dns.rdataset.from_text_list("in", "a", 30, ["10.0.0.1", "10.0.0.2"]) |
| 142 |
rrset = (name, rdataset) |
| 143 |
ttl = 60 |
| 144 |
lifetime = 3600 |
| 145 |
rrname = rrset[0] |
| 146 |
signer = rrname |
| 147 |
dnskey = dns.dnssec.make_dnskey( |
| 148 |
public_key=public_key, algorithm=Algorithm.PRIVATEDNS |
| 149 |
) |
| 150 |
dnskey_rrset = dns.rrset.from_rdata(signer, ttl, dnskey) |
| 151 |
|
| 152 |
rrsig = dns.dnssec.sign( |
| 153 |
rrset=rrset, |
| 154 |
private_key=private_key, |
| 155 |
dnskey=dnskey, |
| 156 |
lifetime=lifetime, |
| 157 |
signer=signer, |
| 158 |
verify=True, |
| 159 |
policy=None, |
| 160 |
) |
| 161 |
|
| 162 |
keys = {signer: dnskey_rrset} |
| 163 |
rrsigset = dns.rrset.from_rdata(rrname, ttl, rrsig) |
| 164 |
dns.dnssec.validate(rrset=rrset, rrsigset=rrsigset, keys=keys, policy=None) |
| 165 |
|
| 166 |
def test_register(self): |
| 167 |
register_algorithm_cls( |
| 168 |
algorithm=Algorithm.PRIVATEDNS, |
| 169 |
algorithm_cls=PrivateED25519, |
| 170 |
name="ed25519.example.com", |
| 171 |
) |
| 172 |
register_algorithm_cls( |
| 173 |
algorithm=Algorithm.PRIVATEOID, |
| 174 |
algorithm_cls=PrivateED448, |
| 175 |
oid=bytes([1, 2, 3, 4]), |
| 176 |
) |
| 177 |
register_algorithm_cls( |
| 178 |
algorithm=251, |
| 179 |
algorithm_cls=PrivateED25519, |
| 180 |
) |
| 181 |
|
| 182 |
with self.assertRaises(TypeError): |
| 183 |
register_algorithm_cls(algorithm=251, algorithm_cls=str, name="example.com") |
| 184 |
|
| 185 |
with self.assertRaises(ValueError): |
| 186 |
register_algorithm_cls( |
| 187 |
algorithm=251, algorithm_cls=PrivateED25519, name="example.com" |
| 188 |
) |
| 189 |
|
| 190 |
with self.assertRaises(ValueError): |
| 191 |
register_algorithm_cls( |
| 192 |
algorithm=251, algorithm_cls=PrivateED25519, oid=bytes([1, 2, 3, 4]) |
| 193 |
) |
| 194 |
|
| 195 |
with self.assertRaises(ValueError): |
| 196 |
register_algorithm_cls( |
| 197 |
algorithm=Algorithm.PRIVATEDNS, |
| 198 |
algorithm_cls=PrivateED25519, |
| 199 |
oid=bytes([1, 2, 3, 4]), |
| 200 |
) |
| 201 |
|
| 202 |
with self.assertRaises(ValueError): |
| 203 |
register_algorithm_cls( |
| 204 |
algorithm=Algorithm.PRIVATEOID, |
| 205 |
algorithm_cls=PrivateED25519, |
| 206 |
name="example.com", |
| 207 |
) |
| 208 |
|
| 209 |
dnskey_251 = DNSKEY( |
| 210 |
"IN", |
| 211 |
"DNSKEY", |
| 212 |
256, |
| 213 |
3, |
| 214 |
251, |
| 215 |
b"hello", |
| 216 |
) |
| 217 |
dnskey_dns = DNSKEY( |
| 218 |
"IN", |
| 219 |
"DNSKEY", |
| 220 |
256, |
| 221 |
3, |
| 222 |
Algorithm.PRIVATEDNS, |
| 223 |
dns.name.from_text("ed25519.example.com").to_wire() + b"hello", |
| 224 |
) |
| 225 |
dnskey_dns_unknown = DNSKEY( |
| 226 |
"IN", |
| 227 |
"DNSKEY", |
| 228 |
256, |
| 229 |
3, |
| 230 |
Algorithm.PRIVATEDNS, |
| 231 |
dns.name.from_text("unknown.example.com").to_wire() + b"hello", |
| 232 |
) |
| 233 |
dnskey_oid = DNSKEY( |
| 234 |
"IN", |
| 235 |
"DNSKEY", |
| 236 |
256, |
| 237 |
3, |
| 238 |
Algorithm.PRIVATEOID, |
| 239 |
bytes([4, 1, 2, 3, 4]) + b"hello", |
| 240 |
) |
| 241 |
dnskey_oid_unknown = DNSKEY( |
| 242 |
"IN", |
| 243 |
"DNSKEY", |
| 244 |
256, |
| 245 |
3, |
| 246 |
Algorithm.PRIVATEOID, |
| 247 |
bytes([4, 42, 42, 42, 42]) + b"hello", |
| 248 |
) |
| 249 |
|
| 250 |
with self.assertRaises(dns.exception.UnsupportedAlgorithm): |
| 251 |
_ = get_algorithm_cls(250) |
| 252 |
|
| 253 |
algorithm_cls = get_algorithm_cls(251) |
| 254 |
self.assertEqual(algorithm_cls, PrivateED25519) |
| 255 |
|
| 256 |
algorithm_cls = get_algorithm_cls_from_dnskey(dnskey_251) |
| 257 |
self.assertEqual(algorithm_cls, PrivateED25519) |
| 258 |
|
| 259 |
algorithm_cls = get_algorithm_cls_from_dnskey(dnskey_dns) |
| 260 |
self.assertEqual(algorithm_cls, PrivateED25519) |
| 261 |
|
| 262 |
with self.assertRaises(dns.exception.UnsupportedAlgorithm): |
| 263 |
_ = get_algorithm_cls_from_dnskey(dnskey_dns_unknown) |
| 264 |
|
| 265 |
algorithm_cls = get_algorithm_cls_from_dnskey(dnskey_oid) |
| 266 |
self.assertEqual(algorithm_cls, PrivateED448) |
| 267 |
|
| 268 |
with self.assertRaises(dns.exception.UnsupportedAlgorithm): |
| 269 |
_ = get_algorithm_cls_from_dnskey(dnskey_oid_unknown) |
| 270 |
|
| 271 |
def test_register_canonical_lookup(self): |
| 272 |
register_algorithm_cls( |
| 273 |
algorithm=Algorithm.PRIVATEDNS, |
| 274 |
algorithm_cls=PrivateED25519, |
| 275 |
name="testing1234.example.com", |
| 276 |
) |
| 277 |
|
| 278 |
dnskey_dns = DNSKEY( |
| 279 |
"IN", |
| 280 |
"DNSKEY", |
| 281 |
256, |
| 282 |
3, |
| 283 |
Algorithm.PRIVATEDNS, |
| 284 |
dns.name.from_text("TESTING1234.EXAMPLE.COM").to_wire() + b"hello", |
| 285 |
) |
| 286 |
|
| 287 |
algorithm_cls = get_algorithm_cls_from_dnskey(dnskey_dns) |
| 288 |
self.assertEqual(algorithm_cls, PrivateED25519) |
| 289 |
|
| 290 |
def test_register_private_without_prefix(self): |
| 291 |
with self.assertRaises(ValueError): |
| 292 |
register_algorithm_cls( |
| 293 |
algorithm=Algorithm.PRIVATEDNS, |
| 294 |
algorithm_cls=PrivateED25519, |
| 295 |
) |
| 296 |
with self.assertRaises(ValueError): |
| 297 |
register_algorithm_cls( |
| 298 |
algorithm=Algorithm.PRIVATEOID, |
| 299 |
algorithm_cls=PrivateED25519, |
| 300 |
) |
| 301 |
|
| 302 |
|
| 303 |
if __name__ == "__main__": |
| 304 |
unittest.main() |