| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
import base64 |
| 4 |
import collections |
| 5 |
import datetime |
| 6 |
import unittest |
| 7 |
|
| 8 |
from cryptodatahub.common.algorithm import Authentication, KeyExchange |
| 9 |
from cryptodatahub.common.exception import InvalidValue |
| 10 |
from cryptodatahub.common.parameter import ECParamWellKnown |
| 11 |
from cryptodatahub.common.key import ( |
| 12 |
PublicKey, |
| 13 |
PublicKeyParamsDsa, |
| 14 |
PublicKeyParamsEcdsa, |
| 15 |
PublicKeyParamsEddsa, |
| 16 |
PublicKeyParamsRsa, |
| 17 |
) |
| 18 |
|
| 19 |
from cryptodatahub.dnsrec.algorithm import ( |
| 20 |
DnsSecAlgorithm, |
| 21 |
DnsSecDigestType, |
| 22 |
DnsRrType, |
| 23 |
SshFpAlgorithm, |
| 24 |
SshFpFingerprintType, |
| 25 |
) |
| 26 |
|
| 27 |
from cryptoparser.common.exception import NotEnoughData |
| 28 |
from cryptoparser.dnsrec.record import ( |
| 29 |
DnsNameUncompressed, |
| 30 |
DnsRecordDnskey, |
| 31 |
DnsRecordDs, |
| 32 |
DnsRecordMx, |
| 33 |
DnsRecordRrsig, |
| 34 |
DnsRecordSshfp, |
| 35 |
DnsRecordTxt, |
| 36 |
DnsRrTypePrivate, |
| 37 |
DnsSecFlag, |
| 38 |
DnsSecProtocol, |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
class TestDnsRecordDnskey(unittest.TestCase): |
| 43 |
def setUp(self): |
| 44 |
self.header_bytes = ( |
| 45 |
b'\x01\x00' + # flags: DNS_ZONE_KEY |
| 46 |
b'\x03' + # version |
| 47 |
b'' |
| 48 |
) |
| 49 |
|
| 50 |
def test_error_inconsistent_algorithm(self): |
| 51 |
public_key_rsa = PublicKey.from_params(PublicKeyParamsRsa( |
| 52 |
public_exponent=2 ** 2048 - 1, |
| 53 |
modulus=2 ** 1024 - 1, |
| 54 |
)) |
| 55 |
|
| 56 |
with self.assertRaises(InvalidValue) as context_manager: |
| 57 |
DnsRecordDnskey( |
| 58 |
flags=[DnsSecFlag.DNS_ZONE_KEY], |
| 59 |
algorithm=DnsSecAlgorithm.DH, |
| 60 |
key=public_key_rsa, |
| 61 |
protocol=DnsSecProtocol.V3, |
| 62 |
) |
| 63 |
self.assertEqual(context_manager.exception.value, KeyExchange.DH) |
| 64 |
|
| 65 |
with self.assertRaises(InvalidValue) as context_manager: |
| 66 |
DnsRecordDnskey( |
| 67 |
flags=[DnsSecFlag.DNS_ZONE_KEY], |
| 68 |
algorithm=DnsSecAlgorithm.ECCGOST, |
| 69 |
key=public_key_rsa, |
| 70 |
protocol=DnsSecProtocol.V3, |
| 71 |
) |
| 72 |
self.assertEqual(context_manager.exception.value, Authentication.GOST_R3410_01) |
| 73 |
|
| 74 |
def test_error_not_enough_data(self): |
| 75 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 76 |
DnsRecordDnskey.parse_exact_size(self.header_bytes) |
| 77 |
|
| 78 |
self.assertEqual( |
| 79 |
context_manager.exception.bytes_needed, |
| 80 |
DnsRecordDnskey.HEADER_SIZE - len(self.header_bytes) |
| 81 |
) |
| 82 |
|
| 83 |
def test_key_tag(self): |
| 84 |
record_bytes = self.header_bytes + ( |
| 85 |
b'\x01' + # algorithm: RSAMD5 |
| 86 |
b'\x03' + # exponent_length: 3 |
| 87 |
b'\x01\x00\x01' + # exponent: 65537 |
| 88 |
124 * b'\x00' + # modulus |
| 89 |
b'\x11\x22\x44\x88' |
| 90 |
b'' |
| 91 |
) |
| 92 |
dns_record = DnsRecordDnskey.parse_exact_size(record_bytes) |
| 93 |
self.assertEqual(dns_record.algorithm, DnsSecAlgorithm.RSAMD5) |
| 94 |
self.assertEqual(dns_record.key_tag, 0x2244) |
| 95 |
|
| 96 |
record_bytes = self.header_bytes + ( |
| 97 |
b'\x05' + # algorithm: RSASHA1 |
| 98 |
b'\x03' + # exponent_length: 3 |
| 99 |
b'\x01\x00\x01' + # exponent: 65537 |
| 100 |
128 * b'\xff' + # modulus |
| 101 |
b'' |
| 102 |
) |
| 103 |
dns_record = DnsRecordDnskey.parse_exact_size(record_bytes) |
| 104 |
self.assertEqual(dns_record.algorithm, DnsSecAlgorithm.RSASHA1) |
| 105 |
self.assertEqual(dns_record.key_tag, 1799) |
| 106 |
|
| 107 |
def test_asdict(self): |
| 108 |
self.assertEqual( |
| 109 |
DnsRecordDnskey( |
| 110 |
flags=[DnsSecFlag.DNS_ZONE_KEY], |
| 111 |
algorithm=DnsSecAlgorithm.RSASHA1, |
| 112 |
key=PublicKey.from_params(PublicKeyParamsRsa( |
| 113 |
public_exponent=2 ** 2048 - 1, |
| 114 |
modulus=2 ** 1024 - 1, |
| 115 |
)), |
| 116 |
protocol=DnsSecProtocol.V3, |
| 117 |
)._asdict(), |
| 118 |
collections.OrderedDict([ |
| 119 |
('key_tag', 1540), |
| 120 |
('flags', [DnsSecFlag.DNS_ZONE_KEY]), |
| 121 |
('algorithm', DnsSecAlgorithm.RSASHA1), |
| 122 |
('key', PublicKey.from_params(PublicKeyParamsRsa( |
| 123 |
public_exponent=2 ** 2048 - 1, |
| 124 |
modulus=2 ** 1024 - 1, |
| 125 |
))), |
| 126 |
('protocol', DnsSecProtocol.V3), |
| 127 |
]) |
| 128 |
|
| 129 |
) |
| 130 |
|
| 131 |
def test_parse_rsa_key(self): |
| 132 |
record_bytes = self.header_bytes + ( |
| 133 |
b'\x05' + # algorithm: RSASHA1 |
| 134 |
b'\x03' + # exponent_length: 3 |
| 135 |
b'\x01\x00\x01' + # exponent: 65537 |
| 136 |
128 * b'\xff' + # modulus |
| 137 |
b'' |
| 138 |
) |
| 139 |
dns_record = DnsRecordDnskey.parse_exact_size(record_bytes) |
| 140 |
self.assertEqual(dns_record.algorithm, DnsSecAlgorithm.RSASHA1) |
| 141 |
self.assertEqual(dns_record.key, PublicKey.from_params(PublicKeyParamsRsa( |
| 142 |
public_exponent=65537, |
| 143 |
modulus=2 ** 1024 - 1, |
| 144 |
))) |
| 145 |
self.assertEqual(dns_record.compose(), record_bytes) |
| 146 |
|
| 147 |
record_bytes = self.header_bytes + ( |
| 148 |
b'\x05' + # algorithm: RSASHA1 |
| 149 |
b'\x00' + # exponent length extender mark |
| 150 |
b'\x01\x00' + # exponent_length: 256 |
| 151 |
256 * b'\xff' + # exponent |
| 152 |
128 * b'\xff' + # modulus |
| 153 |
b'' |
| 154 |
) |
| 155 |
dns_record = DnsRecordDnskey.parse_exact_size(record_bytes) |
| 156 |
self.assertEqual(dns_record.algorithm, DnsSecAlgorithm.RSASHA1) |
| 157 |
self.assertEqual(dns_record.key, PublicKey.from_params(PublicKeyParamsRsa( |
| 158 |
public_exponent=2 ** 2048 - 1, |
| 159 |
modulus=2 ** 1024 - 1, |
| 160 |
))) |
| 161 |
self.assertEqual(dns_record.compose(), record_bytes) |
| 162 |
|
| 163 |
def test_parse_dsa_key(self): |
| 164 |
record_bytes = self.header_bytes + ( |
| 165 |
b'\x03' + # algorithm: DSA |
| 166 |
b'\x08' + # key size parameter |
| 167 |
20 * b'\xff' + # q |
| 168 |
b'\x80' + (1024 // 8 - 1) * b'\x00' + # p |
| 169 |
b'\x40' + (1024 // 8 - 1) * b'\x00' + # g |
| 170 |
b'\x20' + (1024 // 8 - 1) * b'\x00' + # y |
| 171 |
b'' |
| 172 |
) |
| 173 |
dns_record = DnsRecordDnskey.parse_exact_size(record_bytes) |
| 174 |
self.assertEqual(dns_record.algorithm, DnsSecAlgorithm.DSA) |
| 175 |
self.assertEqual(dns_record.key, PublicKey.from_params(PublicKeyParamsDsa( |
| 176 |
prime=2 ** 1023, |
| 177 |
generator=2 ** 1022, |
| 178 |
order=2 ** 160 - 1, |
| 179 |
public_key_value=2 ** 1021, |
| 180 |
))) |
| 181 |
self.assertEqual(dns_record.compose(), record_bytes) |
| 182 |
|
| 183 |
def test_parse_ecdsa_key(self): |
| 184 |
record_bytes = self.header_bytes + ( |
| 185 |
b'\x0c' + # algorithm: ECCGOST |
| 186 |
b'\x80' + (256 // 8 - 1) * b'\x00' + # point_x |
| 187 |
b'\x40' + (256 // 8 - 1) * b'\x00' + # point_y |
| 188 |
b'' |
| 189 |
) |
| 190 |
dns_record = DnsRecordDnskey.parse_exact_size(record_bytes) |
| 191 |
self.assertEqual(dns_record.algorithm, DnsSecAlgorithm.ECCGOST) |
| 192 |
self.assertEqual(dns_record.key, PublicKey.from_params(PublicKeyParamsEcdsa( |
| 193 |
key_parameter=ECParamWellKnown.GC256B, |
| 194 |
point_x=2 ** 255, |
| 195 |
point_y=2 ** 254, |
| 196 |
))) |
| 197 |
self.assertEqual(dns_record.compose(), record_bytes) |
| 198 |
|
| 199 |
record_bytes = self.header_bytes + ( |
| 200 |
b'\x0d' + # algorithm: ECDSAP256SHA256 |
| 201 |
b'\x80' + (256 // 8 - 1) * b'\x00' + # point_x |
| 202 |
b'\x40' + (256 // 8 - 1) * b'\x00' + # point_y |
| 203 |
b'' |
| 204 |
) |
| 205 |
dns_record = DnsRecordDnskey.parse_exact_size(record_bytes) |
| 206 |
self.assertEqual(dns_record.algorithm, DnsSecAlgorithm.ECDSAP256SHA256) |
| 207 |
self.assertEqual(dns_record.key, PublicKey.from_params(PublicKeyParamsEcdsa( |
| 208 |
key_parameter=ECParamWellKnown.SECP256K1, |
| 209 |
point_x=2 ** 255, |
| 210 |
point_y=2 ** 254, |
| 211 |
))) |
| 212 |
self.assertEqual(dns_record.compose(), record_bytes) |
| 213 |
|
| 214 |
record_bytes = self.header_bytes + ( |
| 215 |
b'\x0e' + # algorithm: ECDSAP384SHA384 |
| 216 |
b'\x80' + (384 // 8 - 1) * b'\x00' + # point_x |
| 217 |
b'\x40' + (384 // 8 - 1) * b'\x00' + # point_y |
| 218 |
b'' |
| 219 |
) |
| 220 |
dns_record = DnsRecordDnskey.parse_exact_size(record_bytes) |
| 221 |
self.assertEqual(dns_record.algorithm, DnsSecAlgorithm.ECDSAP384SHA384) |
| 222 |
self.assertEqual(dns_record.key, PublicKey.from_params(PublicKeyParamsEcdsa( |
| 223 |
key_parameter=ECParamWellKnown.SECP384R1, |
| 224 |
point_x=2 ** 383, |
| 225 |
point_y=2 ** 382, |
| 226 |
))) |
| 227 |
self.assertEqual(dns_record.compose(), record_bytes) |
| 228 |
|
| 229 |
def test_parse_eddsa_key(self): |
| 230 |
record_bytes = self.header_bytes + ( |
| 231 |
b'\x0f' + # algorithm: ED25519 |
| 232 |
32 * b'\xff' + # key_data |
| 233 |
b'' |
| 234 |
) |
| 235 |
dns_record = DnsRecordDnskey.parse_exact_size(record_bytes) |
| 236 |
self.assertEqual(dns_record.algorithm, DnsSecAlgorithm.ED25519) |
| 237 |
self.assertEqual(dns_record.key, PublicKey.from_params(PublicKeyParamsEddsa( |
| 238 |
key_parameter=ECParamWellKnown.CURVE25519, |
| 239 |
key_data=32 * b'\xff', |
| 240 |
))) |
| 241 |
self.assertEqual(dns_record.compose(), record_bytes) |
| 242 |
|
| 243 |
record_bytes = self.header_bytes + ( |
| 244 |
b'\x10' + # algorithm: ED448 |
| 245 |
56 * b'\xff' + # key_data |
| 246 |
b'' |
| 247 |
) |
| 248 |
dns_record = DnsRecordDnskey.parse_exact_size(record_bytes) |
| 249 |
self.assertEqual(dns_record.algorithm, DnsSecAlgorithm.ED448) |
| 250 |
self.assertEqual(dns_record.key, PublicKey.from_params(PublicKeyParamsEddsa( |
| 251 |
key_parameter=ECParamWellKnown.CURVE448, |
| 252 |
key_data=56 * b'\xff', |
| 253 |
))) |
| 254 |
self.assertEqual(dns_record.compose(), record_bytes) |
| 255 |
|
| 256 |
def test_real(self): |
| 257 |
# RFC 4034 Section 5.4 |
| 258 |
public_key_bytes = base64.b64decode( |
| 259 |
'AQOeiiR0GOMYkDshWoSKz9Xz' |
| 260 |
'fwJr1AYtsmx3TGkJaNXVbfi/' |
| 261 |
'2pHm822aJ5iI9BMzNXxeYCmZ' |
| 262 |
'DRD99WYwYqUSdjMmmAphXdvx' |
| 263 |
'egXd/M5+X7OrzKBaMbCVdFLU' |
| 264 |
'Uh6DhweJBjEVv5f2wwjM9Xzc' |
| 265 |
'nOf+EPbtG9DMBmADjFDc2w/r' |
| 266 |
'ljwvFw==' |
| 267 |
) |
| 268 |
public_key = DnsRecordDnskey.parse_key(public_key_bytes, DnsSecAlgorithm.RSASHA1) |
| 269 |
self.assertEqual(DnsRecordDnskey.compose_key(public_key), public_key_bytes) |
| 270 |
|
| 271 |
dns_record = DnsRecordDnskey( |
| 272 |
flags=[DnsSecFlag.DNS_ZONE_KEY], |
| 273 |
algorithm=DnsSecAlgorithm.RSASHA1, |
| 274 |
key=public_key, |
| 275 |
protocol=DnsSecProtocol.V3, |
| 276 |
) |
| 277 |
self.assertEqual(dns_record.key_tag, 60485) |
| 278 |
|
| 279 |
# RFC 4034 Section 2.3 |
| 280 |
public_key_bytes = base64.b64decode( |
| 281 |
'AQPSKmynfzW4kyBv015MUG2DeIQ3' |
| 282 |
'Cbl+BBZH4b/0PY1kxkmvHjcZc8no' |
| 283 |
'kfzj31GajIQKY+5CptLr3buXA10h' |
| 284 |
'WqTkF7H6RfoRqXQeogmMHfpftf6z' |
| 285 |
'Mv1LyBUgia7za6ZEzOJBOztyvhjL' |
| 286 |
'742iU/TpPSEDhm2SNKLijfUppn1U' |
| 287 |
'aNvv4w==' |
| 288 |
) |
| 289 |
public_key = DnsRecordDnskey.parse_key(public_key_bytes, DnsSecAlgorithm.RSASHA1) |
| 290 |
self.assertEqual(DnsRecordDnskey.compose_key(public_key), public_key_bytes) |
| 291 |
|
| 292 |
dns_record = DnsRecordDnskey( |
| 293 |
flags=[DnsSecFlag.DNS_ZONE_KEY], |
| 294 |
algorithm=DnsSecAlgorithm.RSASHA1, |
| 295 |
key=public_key, |
| 296 |
protocol=DnsSecProtocol.V3, |
| 297 |
) |
| 298 |
self.assertEqual(dns_record.key_tag, 2642) |
| 299 |
|
| 300 |
# RFC 5702 Section 6.1 |
| 301 |
public_key_bytes = base64.b64decode( |
| 302 |
'AwEAAcFcGsaxxdgiuuGmCkVI' |
| 303 |
'my4h99CqT7jwY3pexPGcnUFtR2Fh36BponcwtkZ4cAgtvd4Qs8P' |
| 304 |
'kxUdp6p/DlUmObdk=' |
| 305 |
) |
| 306 |
public_key = DnsRecordDnskey.parse_key(public_key_bytes, DnsSecAlgorithm.RSASHA256) |
| 307 |
self.assertEqual(DnsRecordDnskey.compose_key(public_key), public_key_bytes) |
| 308 |
|
| 309 |
dns_record = DnsRecordDnskey( |
| 310 |
flags=[DnsSecFlag.DNS_ZONE_KEY], |
| 311 |
algorithm=DnsSecAlgorithm.RSASHA256, |
| 312 |
key=public_key, |
| 313 |
protocol=DnsSecProtocol.V3, |
| 314 |
) |
| 315 |
self.assertEqual(dns_record.key_tag, 9033) |
| 316 |
|
| 317 |
# RFC 5702 Section 6.2 |
| 318 |
public_key_bytes = base64.b64decode( |
| 319 |
'AwEAAdHoNTOW+et86KuJOWRD' |
| 320 |
'p1pndvwb6Y83nSVXXyLA3DLroROUkN6X0O6pnWnjJQujX/AyhqFD' |
| 321 |
'xj13tOnD9u/1kTg7cV6rklMrZDtJCQ5PCl/D7QNPsgVsMu1J2Q8g' |
| 322 |
'pMpztNFLpPBz1bWXjDtaR7ZQBlZ3PFY12ZTSncorffcGmhOL' |
| 323 |
) |
| 324 |
public_key = DnsRecordDnskey.parse_key(public_key_bytes, DnsSecAlgorithm.RSASHA512) |
| 325 |
self.assertEqual(DnsRecordDnskey.compose_key(public_key), public_key_bytes) |
| 326 |
|
| 327 |
dns_record = DnsRecordDnskey( |
| 328 |
flags=[DnsSecFlag.DNS_ZONE_KEY], |
| 329 |
algorithm=DnsSecAlgorithm.RSASHA512, |
| 330 |
key=public_key, |
| 331 |
protocol=DnsSecProtocol.V3, |
| 332 |
) |
| 333 |
self.assertEqual(dns_record.key_tag, 3740) |
| 334 |
|
| 335 |
# RFC 5933 Section 2.2 |
| 336 |
public_key_bytes = base64.b64decode( |
| 337 |
'aRS/DcPWGQj2wVJydT8EcAVoC0kXn5pDVm2I' |
| 338 |
'MvDDPXeD32dsSKcmq8KNVzigjL4OXZTV+t/6' |
| 339 |
'w4X1gpNrZiC01g==' |
| 340 |
) |
| 341 |
public_key = DnsRecordDnskey.parse_key(public_key_bytes, DnsSecAlgorithm.ECCGOST) |
| 342 |
self.assertEqual(DnsRecordDnskey.compose_key(public_key), public_key_bytes) |
| 343 |
|
| 344 |
dns_record = DnsRecordDnskey( |
| 345 |
flags=[DnsSecFlag.DNS_ZONE_KEY], |
| 346 |
algorithm=DnsSecAlgorithm.ECCGOST, |
| 347 |
key=public_key, |
| 348 |
protocol=DnsSecProtocol.V3, |
| 349 |
) |
| 350 |
self.assertEqual(dns_record.key_tag, 59732) |
| 351 |
|
| 352 |
|
| 353 |
class TestDnsRecordDs(unittest.TestCase): |
| 354 |
def setUp(self): |
| 355 |
self.record_bytes = bytes( |
| 356 |
b'\x00\x01' + # key_tag: 1 |
| 357 |
b'\x01' + # algorithm: RSAMD5 |
| 358 |
b'\x02' + # digest_type: SHA_256 |
| 359 |
32 * b'\xff' + # digest |
| 360 |
b'' |
| 361 |
) |
| 362 |
self.record = DnsRecordDs( |
| 363 |
key_tag=1, |
| 364 |
algorithm=DnsSecAlgorithm.RSAMD5, |
| 365 |
digest_type=DnsSecDigestType.SHA_256, |
| 366 |
digest=32 * b'\xff', |
| 367 |
) |
| 368 |
|
| 369 |
def test_error_not_enough_data(self): |
| 370 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 371 |
DnsRecordDs.parse_exact_size(b'\x00') |
| 372 |
|
| 373 |
self.assertEqual( |
| 374 |
context_manager.exception.bytes_needed, |
| 375 |
DnsRecordDs.HEADER_SIZE - 1 |
| 376 |
) |
| 377 |
|
| 378 |
def test_parse(self): |
| 379 |
self.assertEqual(DnsRecordDs.parse_exact_size(self.record_bytes), self.record) |
| 380 |
|
| 381 |
def test_compose(self): |
| 382 |
self.assertEqual(self.record.compose(), self.record_bytes) |
| 383 |
|
| 384 |
|
| 385 |
class TestDnsNameUncompressed(unittest.TestCase): |
| 386 |
def setUp(self): |
| 387 |
self.label_empty_bytes = b'\x00' |
| 388 |
self.label_empty_name = DnsNameUncompressed([]) |
| 389 |
self.label_single_bytes = b'\x01a\x00' |
| 390 |
self.label_single_name = DnsNameUncompressed(['a']) |
| 391 |
self.label_multiple_bytes = b'\x01a\x02bb\x03ccc\x00' |
| 392 |
self.label_multiple_name = DnsNameUncompressed(['a', 'bb', 'ccc']) |
| 393 |
|
| 394 |
def test_error_convert_invalid_value(self): |
| 395 |
with self.assertRaises(InvalidValue) as context_manager: |
| 396 |
DnsNameUncompressed.convert(None) |
| 397 |
|
| 398 |
self.assertEqual(context_manager.exception.value, None) |
| 399 |
|
| 400 |
def test_parse(self): |
| 401 |
self.assertEqual(DnsNameUncompressed.parse_exact_size(self.label_empty_bytes), self.label_empty_name) |
| 402 |
self.assertEqual(DnsNameUncompressed.parse_exact_size(self.label_single_bytes), self.label_single_name) |
| 403 |
self.assertEqual(DnsNameUncompressed.parse_exact_size(self.label_multiple_bytes), self.label_multiple_name) |
| 404 |
|
| 405 |
def test_compose(self): |
| 406 |
self.assertEqual(self.label_empty_name.compose(), self.label_empty_bytes) |
| 407 |
self.assertEqual(self.label_single_name.compose(), self.label_single_bytes) |
| 408 |
self.assertEqual(self.label_multiple_name.compose(), self.label_multiple_bytes) |
| 409 |
|
| 410 |
def test_convert(self): |
| 411 |
self.assertEqual(DnsNameUncompressed.convert(self.label_empty_name), self.label_empty_name) |
| 412 |
self.assertEqual(DnsNameUncompressed.convert(self.label_single_name), self.label_single_name) |
| 413 |
self.assertEqual(DnsNameUncompressed.convert(self.label_multiple_name), self.label_multiple_name) |
| 414 |
|
| 415 |
self.assertEqual(DnsNameUncompressed.convert(''), self.label_empty_name) |
| 416 |
self.assertEqual(DnsNameUncompressed.convert('a'), self.label_single_name) |
| 417 |
self.assertEqual(DnsNameUncompressed.convert('a.bb.ccc'), self.label_multiple_name) |
| 418 |
|
| 419 |
def test_str(self): |
| 420 |
self.assertEqual(str(self.label_empty_name), '') |
| 421 |
self.assertEqual(str(self.label_single_name), 'a') |
| 422 |
self.assertEqual(str(self.label_multiple_name), 'a.bb.ccc') |
| 423 |
|
| 424 |
def test_as_markdown(self): |
| 425 |
self.assertEqual(self.label_empty_name.as_markdown(), '') |
| 426 |
self.assertEqual(self.label_single_name.as_markdown(), 'a') |
| 427 |
self.assertEqual(self.label_multiple_name.as_markdown(), 'a.bb.ccc') |
| 428 |
|
| 429 |
|
| 430 |
class TestDnsRecordRrsig(unittest.TestCase): |
| 431 |
def setUp(self): |
| 432 |
self.record_bytes = bytes( |
| 433 |
b'\x00\x01' + # type_covered: A |
| 434 |
b'\x01' + # algorithm: RSAMD5 |
| 435 |
b'\x03' + # labels |
| 436 |
b'\x00\x00\x0e\x10' + # original_ttl: 3600 |
| 437 |
b'\x00\x00\x00\x01' + # signature_expiration |
| 438 |
b'\x00\x00\x00\x02' + # signature_inception |
| 439 |
b'\xab\xcd' + # key_tag |
| 440 |
b'\x06signer\x00' + # signers_name |
| 441 |
32 * b'\xff' + # signature |
| 442 |
b'' |
| 443 |
) |
| 444 |
self.record = DnsRecordRrsig( |
| 445 |
type_covered=DnsRrType.A, |
| 446 |
algorithm=DnsSecAlgorithm.RSAMD5, |
| 447 |
labels=3, |
| 448 |
original_ttl=3600, |
| 449 |
signature_expiration=datetime.datetime(1970, 1, 1, 0, 0, 1, tzinfo=datetime.timezone.utc), |
| 450 |
signature_inception=datetime.datetime(1970, 1, 1, 0, 0, 2, tzinfo=datetime.timezone.utc), |
| 451 |
key_tag=0xabcd, |
| 452 |
signers_name='signer', |
| 453 |
signature=32 * b'\xff', |
| 454 |
) |
| 455 |
|
| 456 |
self.record_private_type_bytes = bytes( |
| 457 |
b'\xff\xfe' + # type_covered: A |
| 458 |
b'\x01' + # algorithm: RSAMD5 |
| 459 |
b'\x03' + # labels |
| 460 |
b'\x00\x00\x0e\x10' + # original_ttl: 3600 |
| 461 |
b'\x00\x00\x00\x01' + # signature_expiration |
| 462 |
b'\x00\x00\x00\x02' + # signature_inception |
| 463 |
b'\xab\xcd' + # key_tag |
| 464 |
b'\x06signer\x00' + # signers_name |
| 465 |
32 * b'\xff' + # signature |
| 466 |
b'' |
| 467 |
) |
| 468 |
self.record_private_types = DnsRecordRrsig( |
| 469 |
type_covered=DnsRrTypePrivate(0xfffe), |
| 470 |
algorithm=DnsSecAlgorithm.RSAMD5, |
| 471 |
labels=3, |
| 472 |
original_ttl=3600, |
| 473 |
signature_expiration=datetime.datetime(1970, 1, 1, 0, 0, 1, tzinfo=datetime.timezone.utc), |
| 474 |
signature_inception=datetime.datetime(1970, 1, 1, 0, 0, 2, tzinfo=datetime.timezone.utc), |
| 475 |
key_tag=0xabcd, |
| 476 |
signers_name='signer', |
| 477 |
signature=32 * b'\xff', |
| 478 |
) |
| 479 |
|
| 480 |
def test_error_not_enough_data(self): |
| 481 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 482 |
DnsRecordRrsig.parse_exact_size(b'\x00') |
| 483 |
|
| 484 |
self.assertEqual( |
| 485 |
context_manager.exception.bytes_needed, |
| 486 |
DnsRecordRrsig.HEADER_SIZE - 1 |
| 487 |
) |
| 488 |
|
| 489 |
def test_parse(self): |
| 490 |
self.assertEqual(DnsRecordRrsig.parse_exact_size(self.record_bytes), self.record) |
| 491 |
self.assertEqual(DnsRecordRrsig.parse_exact_size(self.record_private_type_bytes), self.record_private_types) |
| 492 |
|
| 493 |
def test_compose(self): |
| 494 |
self.assertEqual(self.record.compose(), self.record_bytes) |
| 495 |
self.assertEqual(self.record_private_types.compose(), self.record_private_type_bytes) |
| 496 |
|
| 497 |
|
| 498 |
class TestDnsRecordMx(unittest.TestCase): |
| 499 |
def setUp(self): |
| 500 |
self.record_bytes = bytes( |
| 501 |
b'\x00\x01' + # priority: 1 |
| 502 |
b'\x08exchange\x00' + # exchange |
| 503 |
b'' |
| 504 |
) |
| 505 |
self.record = DnsRecordMx( |
| 506 |
priority=1, |
| 507 |
exchange='exchange', |
| 508 |
) |
| 509 |
|
| 510 |
def test_error_not_enough_data(self): |
| 511 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 512 |
DnsRecordMx.parse_exact_size(b'\x00') |
| 513 |
|
| 514 |
self.assertEqual( |
| 515 |
context_manager.exception.bytes_needed, |
| 516 |
DnsRecordMx.HEADER_SIZE - 1 |
| 517 |
) |
| 518 |
|
| 519 |
def test_parse(self): |
| 520 |
self.assertEqual(DnsRecordMx.parse_exact_size(self.record_bytes), self.record) |
| 521 |
|
| 522 |
def test_compose(self): |
| 523 |
self.assertEqual(self.record.compose(), self.record_bytes) |
| 524 |
|
| 525 |
|
| 526 |
class TestDnsRecordTxt(unittest.TestCase): |
| 527 |
def setUp(self): |
| 528 |
self.record_bytes_single = bytes( |
| 529 |
b'\x05' + # length: 5 |
| 530 |
b'value' + |
| 531 |
b'' |
| 532 |
) |
| 533 |
self.record_single = DnsRecordTxt(value='value') |
| 534 |
|
| 535 |
self.record_bytes_multiple = bytes( |
| 536 |
b'\x06' + # length: 6 |
| 537 |
b'value1' + |
| 538 |
b'\x06' + # length: 6 |
| 539 |
b'value2' + |
| 540 |
b'' |
| 541 |
) |
| 542 |
self.record_multiple = DnsRecordTxt(value='value1value2') |
| 543 |
|
| 544 |
def test_error_not_enough_data(self): |
| 545 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 546 |
DnsRecordTxt.parse_exact_size(b'') |
| 547 |
|
| 548 |
self.assertEqual( |
| 549 |
context_manager.exception.bytes_needed, |
| 550 |
DnsRecordTxt.HEADER_SIZE |
| 551 |
) |
| 552 |
|
| 553 |
def test_parse(self): |
| 554 |
self.assertEqual(DnsRecordTxt.parse_exact_size(self.record_bytes_single), self.record_single) |
| 555 |
self.assertEqual(DnsRecordTxt.parse_exact_size(self.record_bytes_multiple), self.record_multiple) |
| 556 |
|
| 557 |
def test_compose(self): |
| 558 |
self.assertEqual(self.record_single.compose(), self.record_bytes_single) |
| 559 |
|
| 560 |
|
| 561 |
class TestDnsRecordSshfp(unittest.TestCase): |
| 562 |
def setUp(self): |
| 563 |
self.record_rsa_sha1_bytes = bytes( |
| 564 |
b'\x01' + # algorithm: RSA |
| 565 |
b'\x01' + # fingerprint type: SHA-1 |
| 566 |
b'\xde\xad\xbe\xef' * 5 # fingerprint: 20 bytes |
| 567 |
) |
| 568 |
self.record_rsa_sha1 = DnsRecordSshfp( |
| 569 |
algorithm=SshFpAlgorithm.RSA, |
| 570 |
fingerprint_type=SshFpFingerprintType.SHA1, |
| 571 |
fingerprint=b'\xde\xad\xbe\xef' * 5, |
| 572 |
) |
| 573 |
|
| 574 |
self.record_ecdsa_sha256_bytes = bytes( |
| 575 |
b'\x03' + # algorithm: ECDSA |
| 576 |
b'\x02' + # fingerprint type: SHA-256 |
| 577 |
b'\xab\xcd\xef\x01' * 8 # fingerprint: 32 bytes |
| 578 |
) |
| 579 |
self.record_ecdsa_sha256 = DnsRecordSshfp( |
| 580 |
algorithm=SshFpAlgorithm.ECDSA, |
| 581 |
fingerprint_type=SshFpFingerprintType.SHA2_256, |
| 582 |
fingerprint=b'\xab\xcd\xef\x01' * 8, |
| 583 |
) |
| 584 |
|
| 585 |
def test_error_not_enough_data(self): |
| 586 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 587 |
DnsRecordSshfp.parse_exact_size(b'\x01') |
| 588 |
|
| 589 |
self.assertEqual( |
| 590 |
context_manager.exception.bytes_needed, |
| 591 |
DnsRecordSshfp.HEADER_SIZE - 1 |
| 592 |
) |
| 593 |
|
| 594 |
def test_parse(self): |
| 595 |
self.assertEqual( |
| 596 |
DnsRecordSshfp.parse_exact_size(self.record_rsa_sha1_bytes), |
| 597 |
self.record_rsa_sha1 |
| 598 |
) |
| 599 |
self.assertEqual( |
| 600 |
DnsRecordSshfp.parse_exact_size(self.record_ecdsa_sha256_bytes), |
| 601 |
self.record_ecdsa_sha256 |
| 602 |
) |
| 603 |
|
| 604 |
def test_compose(self): |
| 605 |
self.assertEqual(self.record_rsa_sha1.compose(), self.record_rsa_sha1_bytes) |
| 606 |
self.assertEqual(self.record_ecdsa_sha256.compose(), self.record_ecdsa_sha256_bytes) |