Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- vllm/lib/python3.10/site-packages/dns/__pycache__/_ddr.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/_immutable_ctx.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/_trio_backend.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/asyncquery.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/e164.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/edns.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/exception.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/grange.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/immutable.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/inet.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/message.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/opcode.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/rdata.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/rdataclass.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/rdatatype.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/resolver.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/serial.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/transaction.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/tsigkeyring.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/ttl.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/update.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/versioned.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/win32util.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/xfr.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/__pycache__/zone.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/_asyncbackend.py +100 -0
- vllm/lib/python3.10/site-packages/dns/_asyncio_backend.py +275 -0
- vllm/lib/python3.10/site-packages/dns/_ddr.py +154 -0
- vllm/lib/python3.10/site-packages/dns/dnssec.py +1247 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/__init__.py +121 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/base.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/cryptography.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/dsa.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/ecdsa.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/eddsa.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/rsa.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/base.py +89 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/cryptography.py +68 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/dsa.py +106 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/ecdsa.py +97 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/eddsa.py +70 -0
- vllm/lib/python3.10/site-packages/dns/dnssecalgs/rsa.py +124 -0
- vllm/lib/python3.10/site-packages/dns/e164.py +116 -0
- vllm/lib/python3.10/site-packages/dns/edns.py +572 -0
- vllm/lib/python3.10/site-packages/dns/flags.py +123 -0
- vllm/lib/python3.10/site-packages/dns/grange.py +72 -0
- vllm/lib/python3.10/site-packages/dns/immutable.py +68 -0
- vllm/lib/python3.10/site-packages/dns/inet.py +197 -0
- vllm/lib/python3.10/site-packages/dns/ipv4.py +77 -0
vllm/lib/python3.10/site-packages/dns/__pycache__/_ddr.cpython-310.pyc
ADDED
|
Binary file (4.11 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/_immutable_ctx.cpython-310.pyc
ADDED
|
Binary file (2.16 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/_trio_backend.cpython-310.pyc
ADDED
|
Binary file (7.63 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/asyncquery.cpython-310.pyc
ADDED
|
Binary file (20.6 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/e164.cpython-310.pyc
ADDED
|
Binary file (3.48 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/edns.cpython-310.pyc
ADDED
|
Binary file (16.9 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/exception.cpython-310.pyc
ADDED
|
Binary file (5.62 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/grange.cpython-310.pyc
ADDED
|
Binary file (1.14 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/immutable.cpython-310.pyc
ADDED
|
Binary file (2.42 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/inet.cpython-310.pyc
ADDED
|
Binary file (4.56 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/message.cpython-310.pyc
ADDED
|
Binary file (52.9 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/opcode.cpython-310.pyc
ADDED
|
Binary file (2.47 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/rdata.cpython-310.pyc
ADDED
|
Binary file (24.3 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/rdataclass.cpython-310.pyc
ADDED
|
Binary file (2.78 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/rdatatype.cpython-310.pyc
ADDED
|
Binary file (6.73 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/resolver.cpython-310.pyc
ADDED
|
Binary file (52.7 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/serial.cpython-310.pyc
ADDED
|
Binary file (2.87 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/transaction.cpython-310.pyc
ADDED
|
Binary file (19.9 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/tsigkeyring.cpython-310.pyc
ADDED
|
Binary file (1.84 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/ttl.cpython-310.pyc
ADDED
|
Binary file (1.69 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/update.cpython-310.pyc
ADDED
|
Binary file (8.87 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/versioned.cpython-310.pyc
ADDED
|
Binary file (8.87 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/win32util.cpython-310.pyc
ADDED
|
Binary file (5.55 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/xfr.cpython-310.pyc
ADDED
|
Binary file (7.8 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/__pycache__/zone.cpython-310.pyc
ADDED
|
Binary file (45.4 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/_asyncbackend.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
|
| 2 |
+
|
| 3 |
+
# This is a nullcontext for both sync and async. 3.7 has a nullcontext,
|
| 4 |
+
# but it is only for sync use.
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class NullContext:
|
| 8 |
+
def __init__(self, enter_result=None):
|
| 9 |
+
self.enter_result = enter_result
|
| 10 |
+
|
| 11 |
+
def __enter__(self):
|
| 12 |
+
return self.enter_result
|
| 13 |
+
|
| 14 |
+
def __exit__(self, exc_type, exc_value, traceback):
|
| 15 |
+
pass
|
| 16 |
+
|
| 17 |
+
async def __aenter__(self):
|
| 18 |
+
return self.enter_result
|
| 19 |
+
|
| 20 |
+
async def __aexit__(self, exc_type, exc_value, traceback):
|
| 21 |
+
pass
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# These are declared here so backends can import them without creating
|
| 25 |
+
# circular dependencies with dns.asyncbackend.
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class Socket: # pragma: no cover
|
| 29 |
+
def __init__(self, family: int, type: int):
|
| 30 |
+
self.family = family
|
| 31 |
+
self.type = type
|
| 32 |
+
|
| 33 |
+
async def close(self):
|
| 34 |
+
pass
|
| 35 |
+
|
| 36 |
+
async def getpeername(self):
|
| 37 |
+
raise NotImplementedError
|
| 38 |
+
|
| 39 |
+
async def getsockname(self):
|
| 40 |
+
raise NotImplementedError
|
| 41 |
+
|
| 42 |
+
async def getpeercert(self, timeout):
|
| 43 |
+
raise NotImplementedError
|
| 44 |
+
|
| 45 |
+
async def __aenter__(self):
|
| 46 |
+
return self
|
| 47 |
+
|
| 48 |
+
async def __aexit__(self, exc_type, exc_value, traceback):
|
| 49 |
+
await self.close()
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class DatagramSocket(Socket): # pragma: no cover
|
| 53 |
+
async def sendto(self, what, destination, timeout):
|
| 54 |
+
raise NotImplementedError
|
| 55 |
+
|
| 56 |
+
async def recvfrom(self, size, timeout):
|
| 57 |
+
raise NotImplementedError
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
class StreamSocket(Socket): # pragma: no cover
|
| 61 |
+
async def sendall(self, what, timeout):
|
| 62 |
+
raise NotImplementedError
|
| 63 |
+
|
| 64 |
+
async def recv(self, size, timeout):
|
| 65 |
+
raise NotImplementedError
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class NullTransport:
|
| 69 |
+
async def connect_tcp(self, host, port, timeout, local_address):
|
| 70 |
+
raise NotImplementedError
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class Backend: # pragma: no cover
|
| 74 |
+
def name(self):
|
| 75 |
+
return "unknown"
|
| 76 |
+
|
| 77 |
+
async def make_socket(
|
| 78 |
+
self,
|
| 79 |
+
af,
|
| 80 |
+
socktype,
|
| 81 |
+
proto=0,
|
| 82 |
+
source=None,
|
| 83 |
+
destination=None,
|
| 84 |
+
timeout=None,
|
| 85 |
+
ssl_context=None,
|
| 86 |
+
server_hostname=None,
|
| 87 |
+
):
|
| 88 |
+
raise NotImplementedError
|
| 89 |
+
|
| 90 |
+
def datagram_connection_required(self):
|
| 91 |
+
return False
|
| 92 |
+
|
| 93 |
+
async def sleep(self, interval):
|
| 94 |
+
raise NotImplementedError
|
| 95 |
+
|
| 96 |
+
def get_transport_class(self):
|
| 97 |
+
raise NotImplementedError
|
| 98 |
+
|
| 99 |
+
async def wait_for(self, awaitable, timeout):
|
| 100 |
+
raise NotImplementedError
|
vllm/lib/python3.10/site-packages/dns/_asyncio_backend.py
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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| 7 |
+
import sys
|
| 8 |
+
|
| 9 |
+
import dns._asyncbackend
|
| 10 |
+
import dns._features
|
| 11 |
+
import dns.exception
|
| 12 |
+
import dns.inet
|
| 13 |
+
|
| 14 |
+
_is_win32 = sys.platform == "win32"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _get_running_loop():
|
| 18 |
+
try:
|
| 19 |
+
return asyncio.get_running_loop()
|
| 20 |
+
except AttributeError: # pragma: no cover
|
| 21 |
+
return asyncio.get_event_loop()
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class _DatagramProtocol:
|
| 25 |
+
def __init__(self):
|
| 26 |
+
self.transport = None
|
| 27 |
+
self.recvfrom = None
|
| 28 |
+
|
| 29 |
+
def connection_made(self, transport):
|
| 30 |
+
self.transport = transport
|
| 31 |
+
|
| 32 |
+
def datagram_received(self, data, addr):
|
| 33 |
+
if self.recvfrom and not self.recvfrom.done():
|
| 34 |
+
self.recvfrom.set_result((data, addr))
|
| 35 |
+
|
| 36 |
+
def error_received(self, exc): # pragma: no cover
|
| 37 |
+
if self.recvfrom and not self.recvfrom.done():
|
| 38 |
+
self.recvfrom.set_exception(exc)
|
| 39 |
+
|
| 40 |
+
def connection_lost(self, exc):
|
| 41 |
+
if self.recvfrom and not self.recvfrom.done():
|
| 42 |
+
if exc is None:
|
| 43 |
+
# EOF we triggered. Is there a better way to do this?
|
| 44 |
+
try:
|
| 45 |
+
raise EOFError("EOF")
|
| 46 |
+
except EOFError as e:
|
| 47 |
+
self.recvfrom.set_exception(e)
|
| 48 |
+
else:
|
| 49 |
+
self.recvfrom.set_exception(exc)
|
| 50 |
+
|
| 51 |
+
def close(self):
|
| 52 |
+
self.transport.close()
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
async def _maybe_wait_for(awaitable, timeout):
|
| 56 |
+
if timeout is not None:
|
| 57 |
+
try:
|
| 58 |
+
return await asyncio.wait_for(awaitable, timeout)
|
| 59 |
+
except asyncio.TimeoutError:
|
| 60 |
+
raise dns.exception.Timeout(timeout=timeout)
|
| 61 |
+
else:
|
| 62 |
+
return await awaitable
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
class DatagramSocket(dns._asyncbackend.DatagramSocket):
|
| 66 |
+
def __init__(self, family, transport, protocol):
|
| 67 |
+
super().__init__(family, socket.SOCK_DGRAM)
|
| 68 |
+
self.transport = transport
|
| 69 |
+
self.protocol = protocol
|
| 70 |
+
|
| 71 |
+
async def sendto(self, what, destination, timeout): # pragma: no cover
|
| 72 |
+
# no timeout for asyncio sendto
|
| 73 |
+
self.transport.sendto(what, destination)
|
| 74 |
+
return len(what)
|
| 75 |
+
|
| 76 |
+
async def recvfrom(self, size, timeout):
|
| 77 |
+
# ignore size as there's no way I know to tell protocol about it
|
| 78 |
+
done = _get_running_loop().create_future()
|
| 79 |
+
try:
|
| 80 |
+
assert self.protocol.recvfrom is None
|
| 81 |
+
self.protocol.recvfrom = done
|
| 82 |
+
await _maybe_wait_for(done, timeout)
|
| 83 |
+
return done.result()
|
| 84 |
+
finally:
|
| 85 |
+
self.protocol.recvfrom = None
|
| 86 |
+
|
| 87 |
+
async def close(self):
|
| 88 |
+
self.protocol.close()
|
| 89 |
+
|
| 90 |
+
async def getpeername(self):
|
| 91 |
+
return self.transport.get_extra_info("peername")
|
| 92 |
+
|
| 93 |
+
async def getsockname(self):
|
| 94 |
+
return self.transport.get_extra_info("sockname")
|
| 95 |
+
|
| 96 |
+
async def getpeercert(self, timeout):
|
| 97 |
+
raise NotImplementedError
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
class StreamSocket(dns._asyncbackend.StreamSocket):
|
| 101 |
+
def __init__(self, af, reader, writer):
|
| 102 |
+
super().__init__(af, socket.SOCK_STREAM)
|
| 103 |
+
self.reader = reader
|
| 104 |
+
self.writer = writer
|
| 105 |
+
|
| 106 |
+
async def sendall(self, what, timeout):
|
| 107 |
+
self.writer.write(what)
|
| 108 |
+
return await _maybe_wait_for(self.writer.drain(), timeout)
|
| 109 |
+
|
| 110 |
+
async def recv(self, size, timeout):
|
| 111 |
+
return await _maybe_wait_for(self.reader.read(size), timeout)
|
| 112 |
+
|
| 113 |
+
async def close(self):
|
| 114 |
+
self.writer.close()
|
| 115 |
+
|
| 116 |
+
async def getpeername(self):
|
| 117 |
+
return self.writer.get_extra_info("peername")
|
| 118 |
+
|
| 119 |
+
async def getsockname(self):
|
| 120 |
+
return self.writer.get_extra_info("sockname")
|
| 121 |
+
|
| 122 |
+
async def getpeercert(self, timeout):
|
| 123 |
+
return self.writer.get_extra_info("peercert")
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
if dns._features.have("doh"):
|
| 127 |
+
import anyio
|
| 128 |
+
import httpcore
|
| 129 |
+
import httpcore._backends.anyio
|
| 130 |
+
import httpx
|
| 131 |
+
|
| 132 |
+
_CoreAsyncNetworkBackend = httpcore.AsyncNetworkBackend
|
| 133 |
+
_CoreAnyIOStream = httpcore._backends.anyio.AnyIOStream
|
| 134 |
+
|
| 135 |
+
from dns.query import _compute_times, _expiration_for_this_attempt, _remaining
|
| 136 |
+
|
| 137 |
+
class _NetworkBackend(_CoreAsyncNetworkBackend):
|
| 138 |
+
def __init__(self, resolver, local_port, bootstrap_address, family):
|
| 139 |
+
super().__init__()
|
| 140 |
+
self._local_port = local_port
|
| 141 |
+
self._resolver = resolver
|
| 142 |
+
self._bootstrap_address = bootstrap_address
|
| 143 |
+
self._family = family
|
| 144 |
+
if local_port != 0:
|
| 145 |
+
raise NotImplementedError(
|
| 146 |
+
"the asyncio transport for HTTPX cannot set the local port"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
async def connect_tcp(
|
| 150 |
+
self, host, port, timeout, local_address, socket_options=None
|
| 151 |
+
): # pylint: disable=signature-differs
|
| 152 |
+
addresses = []
|
| 153 |
+
_, expiration = _compute_times(timeout)
|
| 154 |
+
if dns.inet.is_address(host):
|
| 155 |
+
addresses.append(host)
|
| 156 |
+
elif self._bootstrap_address is not None:
|
| 157 |
+
addresses.append(self._bootstrap_address)
|
| 158 |
+
else:
|
| 159 |
+
timeout = _remaining(expiration)
|
| 160 |
+
family = self._family
|
| 161 |
+
if local_address:
|
| 162 |
+
family = dns.inet.af_for_address(local_address)
|
| 163 |
+
answers = await self._resolver.resolve_name(
|
| 164 |
+
host, family=family, lifetime=timeout
|
| 165 |
+
)
|
| 166 |
+
addresses = answers.addresses()
|
| 167 |
+
for address in addresses:
|
| 168 |
+
try:
|
| 169 |
+
attempt_expiration = _expiration_for_this_attempt(2.0, expiration)
|
| 170 |
+
timeout = _remaining(attempt_expiration)
|
| 171 |
+
with anyio.fail_after(timeout):
|
| 172 |
+
stream = await anyio.connect_tcp(
|
| 173 |
+
remote_host=address,
|
| 174 |
+
remote_port=port,
|
| 175 |
+
local_host=local_address,
|
| 176 |
+
)
|
| 177 |
+
return _CoreAnyIOStream(stream)
|
| 178 |
+
except Exception:
|
| 179 |
+
pass
|
| 180 |
+
raise httpcore.ConnectError
|
| 181 |
+
|
| 182 |
+
async def connect_unix_socket(
|
| 183 |
+
self, path, timeout, socket_options=None
|
| 184 |
+
): # pylint: disable=signature-differs
|
| 185 |
+
raise NotImplementedError
|
| 186 |
+
|
| 187 |
+
async def sleep(self, seconds): # pylint: disable=signature-differs
|
| 188 |
+
await anyio.sleep(seconds)
|
| 189 |
+
|
| 190 |
+
class _HTTPTransport(httpx.AsyncHTTPTransport):
|
| 191 |
+
def __init__(
|
| 192 |
+
self,
|
| 193 |
+
*args,
|
| 194 |
+
local_port=0,
|
| 195 |
+
bootstrap_address=None,
|
| 196 |
+
resolver=None,
|
| 197 |
+
family=socket.AF_UNSPEC,
|
| 198 |
+
**kwargs,
|
| 199 |
+
):
|
| 200 |
+
if resolver is None and bootstrap_address is None:
|
| 201 |
+
# pylint: disable=import-outside-toplevel,redefined-outer-name
|
| 202 |
+
import dns.asyncresolver
|
| 203 |
+
|
| 204 |
+
resolver = dns.asyncresolver.Resolver()
|
| 205 |
+
super().__init__(*args, **kwargs)
|
| 206 |
+
self._pool._network_backend = _NetworkBackend(
|
| 207 |
+
resolver, local_port, bootstrap_address, family
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
else:
|
| 211 |
+
_HTTPTransport = dns._asyncbackend.NullTransport # type: ignore
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
class Backend(dns._asyncbackend.Backend):
|
| 215 |
+
def name(self):
|
| 216 |
+
return "asyncio"
|
| 217 |
+
|
| 218 |
+
async def make_socket(
|
| 219 |
+
self,
|
| 220 |
+
af,
|
| 221 |
+
socktype,
|
| 222 |
+
proto=0,
|
| 223 |
+
source=None,
|
| 224 |
+
destination=None,
|
| 225 |
+
timeout=None,
|
| 226 |
+
ssl_context=None,
|
| 227 |
+
server_hostname=None,
|
| 228 |
+
):
|
| 229 |
+
loop = _get_running_loop()
|
| 230 |
+
if socktype == socket.SOCK_DGRAM:
|
| 231 |
+
if _is_win32 and source is None:
|
| 232 |
+
# Win32 wants explicit binding before recvfrom(). This is the
|
| 233 |
+
# proper fix for [#637].
|
| 234 |
+
source = (dns.inet.any_for_af(af), 0)
|
| 235 |
+
transport, protocol = await loop.create_datagram_endpoint(
|
| 236 |
+
_DatagramProtocol,
|
| 237 |
+
source,
|
| 238 |
+
family=af,
|
| 239 |
+
proto=proto,
|
| 240 |
+
remote_addr=destination,
|
| 241 |
+
)
|
| 242 |
+
return DatagramSocket(af, transport, protocol)
|
| 243 |
+
elif socktype == socket.SOCK_STREAM:
|
| 244 |
+
if destination is None:
|
| 245 |
+
# This shouldn't happen, but we check to make code analysis software
|
| 246 |
+
# happier.
|
| 247 |
+
raise ValueError("destination required for stream sockets")
|
| 248 |
+
(r, w) = await _maybe_wait_for(
|
| 249 |
+
asyncio.open_connection(
|
| 250 |
+
destination[0],
|
| 251 |
+
destination[1],
|
| 252 |
+
ssl=ssl_context,
|
| 253 |
+
family=af,
|
| 254 |
+
proto=proto,
|
| 255 |
+
local_addr=source,
|
| 256 |
+
server_hostname=server_hostname,
|
| 257 |
+
),
|
| 258 |
+
timeout,
|
| 259 |
+
)
|
| 260 |
+
return StreamSocket(af, r, w)
|
| 261 |
+
raise NotImplementedError(
|
| 262 |
+
"unsupported socket " + f"type {socktype}"
|
| 263 |
+
) # pragma: no cover
|
| 264 |
+
|
| 265 |
+
async def sleep(self, interval):
|
| 266 |
+
await asyncio.sleep(interval)
|
| 267 |
+
|
| 268 |
+
def datagram_connection_required(self):
|
| 269 |
+
return False
|
| 270 |
+
|
| 271 |
+
def get_transport_class(self):
|
| 272 |
+
return _HTTPTransport
|
| 273 |
+
|
| 274 |
+
async def wait_for(self, awaitable, timeout):
|
| 275 |
+
return await _maybe_wait_for(awaitable, timeout)
|
vllm/lib/python3.10/site-packages/dns/_ddr.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
|
| 2 |
+
#
|
| 3 |
+
# Support for Discovery of Designated Resolvers
|
| 4 |
+
|
| 5 |
+
import socket
|
| 6 |
+
import time
|
| 7 |
+
from urllib.parse import urlparse
|
| 8 |
+
|
| 9 |
+
import dns.asyncbackend
|
| 10 |
+
import dns.inet
|
| 11 |
+
import dns.name
|
| 12 |
+
import dns.nameserver
|
| 13 |
+
import dns.query
|
| 14 |
+
import dns.rdtypes.svcbbase
|
| 15 |
+
|
| 16 |
+
# The special name of the local resolver when using DDR
|
| 17 |
+
_local_resolver_name = dns.name.from_text("_dns.resolver.arpa")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
#
|
| 21 |
+
# Processing is split up into I/O independent and I/O dependent parts to
|
| 22 |
+
# make supporting sync and async versions easy.
|
| 23 |
+
#
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class _SVCBInfo:
|
| 27 |
+
def __init__(self, bootstrap_address, port, hostname, nameservers):
|
| 28 |
+
self.bootstrap_address = bootstrap_address
|
| 29 |
+
self.port = port
|
| 30 |
+
self.hostname = hostname
|
| 31 |
+
self.nameservers = nameservers
|
| 32 |
+
|
| 33 |
+
def ddr_check_certificate(self, cert):
|
| 34 |
+
"""Verify that the _SVCBInfo's address is in the cert's subjectAltName (SAN)"""
|
| 35 |
+
for name, value in cert["subjectAltName"]:
|
| 36 |
+
if name == "IP Address" and value == self.bootstrap_address:
|
| 37 |
+
return True
|
| 38 |
+
return False
|
| 39 |
+
|
| 40 |
+
def make_tls_context(self):
|
| 41 |
+
ssl = dns.query.ssl
|
| 42 |
+
ctx = ssl.create_default_context()
|
| 43 |
+
ctx.minimum_version = ssl.TLSVersion.TLSv1_2
|
| 44 |
+
return ctx
|
| 45 |
+
|
| 46 |
+
def ddr_tls_check_sync(self, lifetime):
|
| 47 |
+
ctx = self.make_tls_context()
|
| 48 |
+
expiration = time.time() + lifetime
|
| 49 |
+
with socket.create_connection(
|
| 50 |
+
(self.bootstrap_address, self.port), lifetime
|
| 51 |
+
) as s:
|
| 52 |
+
with ctx.wrap_socket(s, server_hostname=self.hostname) as ts:
|
| 53 |
+
ts.settimeout(dns.query._remaining(expiration))
|
| 54 |
+
ts.do_handshake()
|
| 55 |
+
cert = ts.getpeercert()
|
| 56 |
+
return self.ddr_check_certificate(cert)
|
| 57 |
+
|
| 58 |
+
async def ddr_tls_check_async(self, lifetime, backend=None):
|
| 59 |
+
if backend is None:
|
| 60 |
+
backend = dns.asyncbackend.get_default_backend()
|
| 61 |
+
ctx = self.make_tls_context()
|
| 62 |
+
expiration = time.time() + lifetime
|
| 63 |
+
async with await backend.make_socket(
|
| 64 |
+
dns.inet.af_for_address(self.bootstrap_address),
|
| 65 |
+
socket.SOCK_STREAM,
|
| 66 |
+
0,
|
| 67 |
+
None,
|
| 68 |
+
(self.bootstrap_address, self.port),
|
| 69 |
+
lifetime,
|
| 70 |
+
ctx,
|
| 71 |
+
self.hostname,
|
| 72 |
+
) as ts:
|
| 73 |
+
cert = await ts.getpeercert(dns.query._remaining(expiration))
|
| 74 |
+
return self.ddr_check_certificate(cert)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def _extract_nameservers_from_svcb(answer):
|
| 78 |
+
bootstrap_address = answer.nameserver
|
| 79 |
+
if not dns.inet.is_address(bootstrap_address):
|
| 80 |
+
return []
|
| 81 |
+
infos = []
|
| 82 |
+
for rr in answer.rrset.processing_order():
|
| 83 |
+
nameservers = []
|
| 84 |
+
param = rr.params.get(dns.rdtypes.svcbbase.ParamKey.ALPN)
|
| 85 |
+
if param is None:
|
| 86 |
+
continue
|
| 87 |
+
alpns = set(param.ids)
|
| 88 |
+
host = rr.target.to_text(omit_final_dot=True)
|
| 89 |
+
port = None
|
| 90 |
+
param = rr.params.get(dns.rdtypes.svcbbase.ParamKey.PORT)
|
| 91 |
+
if param is not None:
|
| 92 |
+
port = param.port
|
| 93 |
+
# For now we ignore address hints and address resolution and always use the
|
| 94 |
+
# bootstrap address
|
| 95 |
+
if b"h2" in alpns:
|
| 96 |
+
param = rr.params.get(dns.rdtypes.svcbbase.ParamKey.DOHPATH)
|
| 97 |
+
if param is None or not param.value.endswith(b"{?dns}"):
|
| 98 |
+
continue
|
| 99 |
+
path = param.value[:-6].decode()
|
| 100 |
+
if not path.startswith("/"):
|
| 101 |
+
path = "/" + path
|
| 102 |
+
if port is None:
|
| 103 |
+
port = 443
|
| 104 |
+
url = f"https://{host}:{port}{path}"
|
| 105 |
+
# check the URL
|
| 106 |
+
try:
|
| 107 |
+
urlparse(url)
|
| 108 |
+
nameservers.append(dns.nameserver.DoHNameserver(url, bootstrap_address))
|
| 109 |
+
except Exception:
|
| 110 |
+
# continue processing other ALPN types
|
| 111 |
+
pass
|
| 112 |
+
if b"dot" in alpns:
|
| 113 |
+
if port is None:
|
| 114 |
+
port = 853
|
| 115 |
+
nameservers.append(
|
| 116 |
+
dns.nameserver.DoTNameserver(bootstrap_address, port, host)
|
| 117 |
+
)
|
| 118 |
+
if b"doq" in alpns:
|
| 119 |
+
if port is None:
|
| 120 |
+
port = 853
|
| 121 |
+
nameservers.append(
|
| 122 |
+
dns.nameserver.DoQNameserver(bootstrap_address, port, True, host)
|
| 123 |
+
)
|
| 124 |
+
if len(nameservers) > 0:
|
| 125 |
+
infos.append(_SVCBInfo(bootstrap_address, port, host, nameservers))
|
| 126 |
+
return infos
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def _get_nameservers_sync(answer, lifetime):
|
| 130 |
+
"""Return a list of TLS-validated resolver nameservers extracted from an SVCB
|
| 131 |
+
answer."""
|
| 132 |
+
nameservers = []
|
| 133 |
+
infos = _extract_nameservers_from_svcb(answer)
|
| 134 |
+
for info in infos:
|
| 135 |
+
try:
|
| 136 |
+
if info.ddr_tls_check_sync(lifetime):
|
| 137 |
+
nameservers.extend(info.nameservers)
|
| 138 |
+
except Exception:
|
| 139 |
+
pass
|
| 140 |
+
return nameservers
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
async def _get_nameservers_async(answer, lifetime):
|
| 144 |
+
"""Return a list of TLS-validated resolver nameservers extracted from an SVCB
|
| 145 |
+
answer."""
|
| 146 |
+
nameservers = []
|
| 147 |
+
infos = _extract_nameservers_from_svcb(answer)
|
| 148 |
+
for info in infos:
|
| 149 |
+
try:
|
| 150 |
+
if await info.ddr_tls_check_async(lifetime):
|
| 151 |
+
nameservers.extend(info.nameservers)
|
| 152 |
+
except Exception:
|
| 153 |
+
pass
|
| 154 |
+
return nameservers
|
vllm/lib/python3.10/site-packages/dns/dnssec.py
ADDED
|
@@ -0,0 +1,1247 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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c.
|
| 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 |
+
"""Common DNSSEC-related functions and constants."""
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
import base64
|
| 22 |
+
import contextlib
|
| 23 |
+
import functools
|
| 24 |
+
import hashlib
|
| 25 |
+
import struct
|
| 26 |
+
import time
|
| 27 |
+
from datetime import datetime
|
| 28 |
+
from typing import Callable, Dict, List, Optional, Set, Tuple, Union, cast
|
| 29 |
+
|
| 30 |
+
import dns._features
|
| 31 |
+
import dns.exception
|
| 32 |
+
import dns.name
|
| 33 |
+
import dns.node
|
| 34 |
+
import dns.rdata
|
| 35 |
+
import dns.rdataclass
|
| 36 |
+
import dns.rdataset
|
| 37 |
+
import dns.rdatatype
|
| 38 |
+
import dns.rrset
|
| 39 |
+
import dns.transaction
|
| 40 |
+
import dns.zone
|
| 41 |
+
from dns.dnssectypes import Algorithm, DSDigest, NSEC3Hash
|
| 42 |
+
from dns.exception import ( # pylint: disable=W0611
|
| 43 |
+
AlgorithmKeyMismatch,
|
| 44 |
+
DeniedByPolicy,
|
| 45 |
+
UnsupportedAlgorithm,
|
| 46 |
+
ValidationFailure,
|
| 47 |
+
)
|
| 48 |
+
from dns.rdtypes.ANY.CDNSKEY import CDNSKEY
|
| 49 |
+
from dns.rdtypes.ANY.CDS import CDS
|
| 50 |
+
from dns.rdtypes.ANY.DNSKEY import DNSKEY
|
| 51 |
+
from dns.rdtypes.ANY.DS import DS
|
| 52 |
+
from dns.rdtypes.ANY.NSEC import NSEC, Bitmap
|
| 53 |
+
from dns.rdtypes.ANY.NSEC3PARAM import NSEC3PARAM
|
| 54 |
+
from dns.rdtypes.ANY.RRSIG import RRSIG, sigtime_to_posixtime
|
| 55 |
+
from dns.rdtypes.dnskeybase import Flag
|
| 56 |
+
|
| 57 |
+
PublicKey = Union[
|
| 58 |
+
"GenericPublicKey",
|
| 59 |
+
"rsa.RSAPublicKey",
|
| 60 |
+
"ec.EllipticCurvePublicKey",
|
| 61 |
+
"ed25519.Ed25519PublicKey",
|
| 62 |
+
"ed448.Ed448PublicKey",
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
PrivateKey = Union[
|
| 66 |
+
"GenericPrivateKey",
|
| 67 |
+
"rsa.RSAPrivateKey",
|
| 68 |
+
"ec.EllipticCurvePrivateKey",
|
| 69 |
+
"ed25519.Ed25519PrivateKey",
|
| 70 |
+
"ed448.Ed448PrivateKey",
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
RRsetSigner = Callable[[dns.transaction.Transaction, dns.rrset.RRset], None]
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def algorithm_from_text(text: str) -> Algorithm:
|
| 77 |
+
"""Convert text into a DNSSEC algorithm value.
|
| 78 |
+
|
| 79 |
+
*text*, a ``str``, the text to convert to into an algorithm value.
|
| 80 |
+
|
| 81 |
+
Returns an ``int``.
|
| 82 |
+
"""
|
| 83 |
+
|
| 84 |
+
return Algorithm.from_text(text)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def algorithm_to_text(value: Union[Algorithm, int]) -> str:
|
| 88 |
+
"""Convert a DNSSEC algorithm value to text
|
| 89 |
+
|
| 90 |
+
*value*, a ``dns.dnssec.Algorithm``.
|
| 91 |
+
|
| 92 |
+
Returns a ``str``, the name of a DNSSEC algorithm.
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
return Algorithm.to_text(value)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def to_timestamp(value: Union[datetime, str, float, int]) -> int:
|
| 99 |
+
"""Convert various format to a timestamp"""
|
| 100 |
+
if isinstance(value, datetime):
|
| 101 |
+
return int(value.timestamp())
|
| 102 |
+
elif isinstance(value, str):
|
| 103 |
+
return sigtime_to_posixtime(value)
|
| 104 |
+
elif isinstance(value, float):
|
| 105 |
+
return int(value)
|
| 106 |
+
elif isinstance(value, int):
|
| 107 |
+
return value
|
| 108 |
+
else:
|
| 109 |
+
raise TypeError("Unsupported timestamp type")
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def key_id(key: Union[DNSKEY, CDNSKEY]) -> int:
|
| 113 |
+
"""Return the key id (a 16-bit number) for the specified key.
|
| 114 |
+
|
| 115 |
+
*key*, a ``dns.rdtypes.ANY.DNSKEY.DNSKEY``
|
| 116 |
+
|
| 117 |
+
Returns an ``int`` between 0 and 65535
|
| 118 |
+
"""
|
| 119 |
+
|
| 120 |
+
rdata = key.to_wire()
|
| 121 |
+
assert rdata is not None # for mypy
|
| 122 |
+
if key.algorithm == Algorithm.RSAMD5:
|
| 123 |
+
return (rdata[-3] << 8) + rdata[-2]
|
| 124 |
+
else:
|
| 125 |
+
total = 0
|
| 126 |
+
for i in range(len(rdata) // 2):
|
| 127 |
+
total += (rdata[2 * i] << 8) + rdata[2 * i + 1]
|
| 128 |
+
if len(rdata) % 2 != 0:
|
| 129 |
+
total += rdata[len(rdata) - 1] << 8
|
| 130 |
+
total += (total >> 16) & 0xFFFF
|
| 131 |
+
return total & 0xFFFF
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
class Policy:
|
| 135 |
+
def __init__(self):
|
| 136 |
+
pass
|
| 137 |
+
|
| 138 |
+
def ok_to_sign(self, _: DNSKEY) -> bool: # pragma: no cover
|
| 139 |
+
return False
|
| 140 |
+
|
| 141 |
+
def ok_to_validate(self, _: DNSKEY) -> bool: # pragma: no cover
|
| 142 |
+
return False
|
| 143 |
+
|
| 144 |
+
def ok_to_create_ds(self, _: DSDigest) -> bool: # pragma: no cover
|
| 145 |
+
return False
|
| 146 |
+
|
| 147 |
+
def ok_to_validate_ds(self, _: DSDigest) -> bool: # pragma: no cover
|
| 148 |
+
return False
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
class SimpleDeny(Policy):
|
| 152 |
+
def __init__(self, deny_sign, deny_validate, deny_create_ds, deny_validate_ds):
|
| 153 |
+
super().__init__()
|
| 154 |
+
self._deny_sign = deny_sign
|
| 155 |
+
self._deny_validate = deny_validate
|
| 156 |
+
self._deny_create_ds = deny_create_ds
|
| 157 |
+
self._deny_validate_ds = deny_validate_ds
|
| 158 |
+
|
| 159 |
+
def ok_to_sign(self, key: DNSKEY) -> bool:
|
| 160 |
+
return key.algorithm not in self._deny_sign
|
| 161 |
+
|
| 162 |
+
def ok_to_validate(self, key: DNSKEY) -> bool:
|
| 163 |
+
return key.algorithm not in self._deny_validate
|
| 164 |
+
|
| 165 |
+
def ok_to_create_ds(self, algorithm: DSDigest) -> bool:
|
| 166 |
+
return algorithm not in self._deny_create_ds
|
| 167 |
+
|
| 168 |
+
def ok_to_validate_ds(self, algorithm: DSDigest) -> bool:
|
| 169 |
+
return algorithm not in self._deny_validate_ds
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
rfc_8624_policy = SimpleDeny(
|
| 173 |
+
{Algorithm.RSAMD5, Algorithm.DSA, Algorithm.DSANSEC3SHA1, Algorithm.ECCGOST},
|
| 174 |
+
{Algorithm.RSAMD5, Algorithm.DSA, Algorithm.DSANSEC3SHA1},
|
| 175 |
+
{DSDigest.NULL, DSDigest.SHA1, DSDigest.GOST},
|
| 176 |
+
{DSDigest.NULL},
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
allow_all_policy = SimpleDeny(set(), set(), set(), set())
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
default_policy = rfc_8624_policy
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def make_ds(
|
| 186 |
+
name: Union[dns.name.Name, str],
|
| 187 |
+
key: dns.rdata.Rdata,
|
| 188 |
+
algorithm: Union[DSDigest, str],
|
| 189 |
+
origin: Optional[dns.name.Name] = None,
|
| 190 |
+
policy: Optional[Policy] = None,
|
| 191 |
+
validating: bool = False,
|
| 192 |
+
) -> DS:
|
| 193 |
+
"""Create a DS record for a DNSSEC key.
|
| 194 |
+
|
| 195 |
+
*name*, a ``dns.name.Name`` or ``str``, the owner name of the DS record.
|
| 196 |
+
|
| 197 |
+
*key*, a ``dns.rdtypes.ANY.DNSKEY.DNSKEY`` or ``dns.rdtypes.ANY.DNSKEY.CDNSKEY``,
|
| 198 |
+
the key the DS is about.
|
| 199 |
+
|
| 200 |
+
*algorithm*, a ``str`` or ``int`` specifying the hash algorithm.
|
| 201 |
+
The currently supported hashes are "SHA1", "SHA256", and "SHA384". Case
|
| 202 |
+
does not matter for these strings.
|
| 203 |
+
|
| 204 |
+
*origin*, a ``dns.name.Name`` or ``None``. If *key* is a relative name,
|
| 205 |
+
then it will be made absolute using the specified origin.
|
| 206 |
+
|
| 207 |
+
*policy*, a ``dns.dnssec.Policy`` or ``None``. If ``None``, the default policy,
|
| 208 |
+
``dns.dnssec.default_policy`` is used; this policy defaults to that of RFC 8624.
|
| 209 |
+
|
| 210 |
+
*validating*, a ``bool``. If ``True``, then policy is checked in
|
| 211 |
+
validating mode, i.e. "Is it ok to validate using this digest algorithm?".
|
| 212 |
+
Otherwise the policy is checked in creating mode, i.e. "Is it ok to create a DS with
|
| 213 |
+
this digest algorithm?".
|
| 214 |
+
|
| 215 |
+
Raises ``UnsupportedAlgorithm`` if the algorithm is unknown.
|
| 216 |
+
|
| 217 |
+
Raises ``DeniedByPolicy`` if the algorithm is denied by policy.
|
| 218 |
+
|
| 219 |
+
Returns a ``dns.rdtypes.ANY.DS.DS``
|
| 220 |
+
"""
|
| 221 |
+
|
| 222 |
+
if policy is None:
|
| 223 |
+
policy = default_policy
|
| 224 |
+
try:
|
| 225 |
+
if isinstance(algorithm, str):
|
| 226 |
+
algorithm = DSDigest[algorithm.upper()]
|
| 227 |
+
except Exception:
|
| 228 |
+
raise UnsupportedAlgorithm(f'unsupported algorithm "{algorithm}"')
|
| 229 |
+
if validating:
|
| 230 |
+
check = policy.ok_to_validate_ds
|
| 231 |
+
else:
|
| 232 |
+
check = policy.ok_to_create_ds
|
| 233 |
+
if not check(algorithm):
|
| 234 |
+
raise DeniedByPolicy
|
| 235 |
+
if not isinstance(key, (DNSKEY, CDNSKEY)):
|
| 236 |
+
raise ValueError("key is not a DNSKEY/CDNSKEY")
|
| 237 |
+
if algorithm == DSDigest.SHA1:
|
| 238 |
+
dshash = hashlib.sha1()
|
| 239 |
+
elif algorithm == DSDigest.SHA256:
|
| 240 |
+
dshash = hashlib.sha256()
|
| 241 |
+
elif algorithm == DSDigest.SHA384:
|
| 242 |
+
dshash = hashlib.sha384()
|
| 243 |
+
else:
|
| 244 |
+
raise UnsupportedAlgorithm(f'unsupported algorithm "{algorithm}"')
|
| 245 |
+
|
| 246 |
+
if isinstance(name, str):
|
| 247 |
+
name = dns.name.from_text(name, origin)
|
| 248 |
+
wire = name.canonicalize().to_wire()
|
| 249 |
+
kwire = key.to_wire(origin=origin)
|
| 250 |
+
assert wire is not None and kwire is not None # for mypy
|
| 251 |
+
dshash.update(wire)
|
| 252 |
+
dshash.update(kwire)
|
| 253 |
+
digest = dshash.digest()
|
| 254 |
+
|
| 255 |
+
dsrdata = struct.pack("!HBB", key_id(key), key.algorithm, algorithm) + digest
|
| 256 |
+
ds = dns.rdata.from_wire(
|
| 257 |
+
dns.rdataclass.IN, dns.rdatatype.DS, dsrdata, 0, len(dsrdata)
|
| 258 |
+
)
|
| 259 |
+
return cast(DS, ds)
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
def make_cds(
|
| 263 |
+
name: Union[dns.name.Name, str],
|
| 264 |
+
key: dns.rdata.Rdata,
|
| 265 |
+
algorithm: Union[DSDigest, str],
|
| 266 |
+
origin: Optional[dns.name.Name] = None,
|
| 267 |
+
) -> CDS:
|
| 268 |
+
"""Create a CDS record for a DNSSEC key.
|
| 269 |
+
|
| 270 |
+
*name*, a ``dns.name.Name`` or ``str``, the owner name of the DS record.
|
| 271 |
+
|
| 272 |
+
*key*, a ``dns.rdtypes.ANY.DNSKEY.DNSKEY`` or ``dns.rdtypes.ANY.DNSKEY.CDNSKEY``,
|
| 273 |
+
the key the DS is about.
|
| 274 |
+
|
| 275 |
+
*algorithm*, a ``str`` or ``int`` specifying the hash algorithm.
|
| 276 |
+
The currently supported hashes are "SHA1", "SHA256", and "SHA384". Case
|
| 277 |
+
does not matter for these strings.
|
| 278 |
+
|
| 279 |
+
*origin*, a ``dns.name.Name`` or ``None``. If *key* is a relative name,
|
| 280 |
+
then it will be made absolute using the specified origin.
|
| 281 |
+
|
| 282 |
+
Raises ``UnsupportedAlgorithm`` if the algorithm is unknown.
|
| 283 |
+
|
| 284 |
+
Returns a ``dns.rdtypes.ANY.DS.CDS``
|
| 285 |
+
"""
|
| 286 |
+
|
| 287 |
+
ds = make_ds(name, key, algorithm, origin)
|
| 288 |
+
return CDS(
|
| 289 |
+
rdclass=ds.rdclass,
|
| 290 |
+
rdtype=dns.rdatatype.CDS,
|
| 291 |
+
key_tag=ds.key_tag,
|
| 292 |
+
algorithm=ds.algorithm,
|
| 293 |
+
digest_type=ds.digest_type,
|
| 294 |
+
digest=ds.digest,
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
def _find_candidate_keys(
|
| 299 |
+
keys: Dict[dns.name.Name, Union[dns.rdataset.Rdataset, dns.node.Node]], rrsig: RRSIG
|
| 300 |
+
) -> Optional[List[DNSKEY]]:
|
| 301 |
+
value = keys.get(rrsig.signer)
|
| 302 |
+
if isinstance(value, dns.node.Node):
|
| 303 |
+
rdataset = value.get_rdataset(dns.rdataclass.IN, dns.rdatatype.DNSKEY)
|
| 304 |
+
else:
|
| 305 |
+
rdataset = value
|
| 306 |
+
if rdataset is None:
|
| 307 |
+
return None
|
| 308 |
+
return [
|
| 309 |
+
cast(DNSKEY, rd)
|
| 310 |
+
for rd in rdataset
|
| 311 |
+
if rd.algorithm == rrsig.algorithm
|
| 312 |
+
and key_id(rd) == rrsig.key_tag
|
| 313 |
+
and (rd.flags & Flag.ZONE) == Flag.ZONE # RFC 4034 2.1.1
|
| 314 |
+
and rd.protocol == 3 # RFC 4034 2.1.2
|
| 315 |
+
]
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
def _get_rrname_rdataset(
|
| 319 |
+
rrset: Union[dns.rrset.RRset, Tuple[dns.name.Name, dns.rdataset.Rdataset]],
|
| 320 |
+
) -> Tuple[dns.name.Name, dns.rdataset.Rdataset]:
|
| 321 |
+
if isinstance(rrset, tuple):
|
| 322 |
+
return rrset[0], rrset[1]
|
| 323 |
+
else:
|
| 324 |
+
return rrset.name, rrset
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def _validate_signature(sig: bytes, data: bytes, key: DNSKEY) -> None:
|
| 328 |
+
# pylint: disable=possibly-used-before-assignment
|
| 329 |
+
public_cls = get_algorithm_cls_from_dnskey(key).public_cls
|
| 330 |
+
try:
|
| 331 |
+
public_key = public_cls.from_dnskey(key)
|
| 332 |
+
except ValueError:
|
| 333 |
+
raise ValidationFailure("invalid public key")
|
| 334 |
+
public_key.verify(sig, data)
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
def _validate_rrsig(
|
| 338 |
+
rrset: Union[dns.rrset.RRset, Tuple[dns.name.Name, dns.rdataset.Rdataset]],
|
| 339 |
+
rrsig: RRSIG,
|
| 340 |
+
keys: Dict[dns.name.Name, Union[dns.node.Node, dns.rdataset.Rdataset]],
|
| 341 |
+
origin: Optional[dns.name.Name] = None,
|
| 342 |
+
now: Optional[float] = None,
|
| 343 |
+
policy: Optional[Policy] = None,
|
| 344 |
+
) -> None:
|
| 345 |
+
"""Validate an RRset against a single signature rdata, throwing an
|
| 346 |
+
exception if validation is not successful.
|
| 347 |
+
|
| 348 |
+
*rrset*, the RRset to validate. This can be a
|
| 349 |
+
``dns.rrset.RRset`` or a (``dns.name.Name``, ``dns.rdataset.Rdataset``)
|
| 350 |
+
tuple.
|
| 351 |
+
|
| 352 |
+
*rrsig*, a ``dns.rdata.Rdata``, the signature to validate.
|
| 353 |
+
|
| 354 |
+
*keys*, the key dictionary, used to find the DNSKEY associated
|
| 355 |
+
with a given name. The dictionary is keyed by a
|
| 356 |
+
``dns.name.Name``, and has ``dns.node.Node`` or
|
| 357 |
+
``dns.rdataset.Rdataset`` values.
|
| 358 |
+
|
| 359 |
+
*origin*, a ``dns.name.Name`` or ``None``, the origin to use for relative
|
| 360 |
+
names.
|
| 361 |
+
|
| 362 |
+
*now*, a ``float`` or ``None``, the time, in seconds since the epoch, to
|
| 363 |
+
use as the current time when validating. If ``None``, the actual current
|
| 364 |
+
time is used.
|
| 365 |
+
|
| 366 |
+
*policy*, a ``dns.dnssec.Policy`` or ``None``. If ``None``, the default policy,
|
| 367 |
+
``dns.dnssec.default_policy`` is used; this policy defaults to that of RFC 8624.
|
| 368 |
+
|
| 369 |
+
Raises ``ValidationFailure`` if the signature is expired, not yet valid,
|
| 370 |
+
the public key is invalid, the algorithm is unknown, the verification
|
| 371 |
+
fails, etc.
|
| 372 |
+
|
| 373 |
+
Raises ``UnsupportedAlgorithm`` if the algorithm is recognized by
|
| 374 |
+
dnspython but not implemented.
|
| 375 |
+
"""
|
| 376 |
+
|
| 377 |
+
if policy is None:
|
| 378 |
+
policy = default_policy
|
| 379 |
+
|
| 380 |
+
candidate_keys = _find_candidate_keys(keys, rrsig)
|
| 381 |
+
if candidate_keys is None:
|
| 382 |
+
raise ValidationFailure("unknown key")
|
| 383 |
+
|
| 384 |
+
if now is None:
|
| 385 |
+
now = time.time()
|
| 386 |
+
if rrsig.expiration < now:
|
| 387 |
+
raise ValidationFailure("expired")
|
| 388 |
+
if rrsig.inception > now:
|
| 389 |
+
raise ValidationFailure("not yet valid")
|
| 390 |
+
|
| 391 |
+
data = _make_rrsig_signature_data(rrset, rrsig, origin)
|
| 392 |
+
|
| 393 |
+
# pylint: disable=possibly-used-before-assignment
|
| 394 |
+
for candidate_key in candidate_keys:
|
| 395 |
+
if not policy.ok_to_validate(candidate_key):
|
| 396 |
+
continue
|
| 397 |
+
try:
|
| 398 |
+
_validate_signature(rrsig.signature, data, candidate_key)
|
| 399 |
+
return
|
| 400 |
+
except (InvalidSignature, ValidationFailure):
|
| 401 |
+
# this happens on an individual validation failure
|
| 402 |
+
continue
|
| 403 |
+
# nothing verified -- raise failure:
|
| 404 |
+
raise ValidationFailure("verify failure")
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
def _validate(
|
| 408 |
+
rrset: Union[dns.rrset.RRset, Tuple[dns.name.Name, dns.rdataset.Rdataset]],
|
| 409 |
+
rrsigset: Union[dns.rrset.RRset, Tuple[dns.name.Name, dns.rdataset.Rdataset]],
|
| 410 |
+
keys: Dict[dns.name.Name, Union[dns.node.Node, dns.rdataset.Rdataset]],
|
| 411 |
+
origin: Optional[dns.name.Name] = None,
|
| 412 |
+
now: Optional[float] = None,
|
| 413 |
+
policy: Optional[Policy] = None,
|
| 414 |
+
) -> None:
|
| 415 |
+
"""Validate an RRset against a signature RRset, throwing an exception
|
| 416 |
+
if none of the signatures validate.
|
| 417 |
+
|
| 418 |
+
*rrset*, the RRset to validate. This can be a
|
| 419 |
+
``dns.rrset.RRset`` or a (``dns.name.Name``, ``dns.rdataset.Rdataset``)
|
| 420 |
+
tuple.
|
| 421 |
+
|
| 422 |
+
*rrsigset*, the signature RRset. This can be a
|
| 423 |
+
``dns.rrset.RRset`` or a (``dns.name.Name``, ``dns.rdataset.Rdataset``)
|
| 424 |
+
tuple.
|
| 425 |
+
|
| 426 |
+
*keys*, the key dictionary, used to find the DNSKEY associated
|
| 427 |
+
with a given name. The dictionary is keyed by a
|
| 428 |
+
``dns.name.Name``, and has ``dns.node.Node`` or
|
| 429 |
+
``dns.rdataset.Rdataset`` values.
|
| 430 |
+
|
| 431 |
+
*origin*, a ``dns.name.Name``, the origin to use for relative names;
|
| 432 |
+
defaults to None.
|
| 433 |
+
|
| 434 |
+
*now*, an ``int`` or ``None``, the time, in seconds since the epoch, to
|
| 435 |
+
use as the current time when validating. If ``None``, the actual current
|
| 436 |
+
time is used.
|
| 437 |
+
|
| 438 |
+
*policy*, a ``dns.dnssec.Policy`` or ``None``. If ``None``, the default policy,
|
| 439 |
+
``dns.dnssec.default_policy`` is used; this policy defaults to that of RFC 8624.
|
| 440 |
+
|
| 441 |
+
Raises ``ValidationFailure`` if the signature is expired, not yet valid,
|
| 442 |
+
the public key is invalid, the algorithm is unknown, the verification
|
| 443 |
+
fails, etc.
|
| 444 |
+
"""
|
| 445 |
+
|
| 446 |
+
if policy is None:
|
| 447 |
+
policy = default_policy
|
| 448 |
+
|
| 449 |
+
if isinstance(origin, str):
|
| 450 |
+
origin = dns.name.from_text(origin, dns.name.root)
|
| 451 |
+
|
| 452 |
+
if isinstance(rrset, tuple):
|
| 453 |
+
rrname = rrset[0]
|
| 454 |
+
else:
|
| 455 |
+
rrname = rrset.name
|
| 456 |
+
|
| 457 |
+
if isinstance(rrsigset, tuple):
|
| 458 |
+
rrsigname = rrsigset[0]
|
| 459 |
+
rrsigrdataset = rrsigset[1]
|
| 460 |
+
else:
|
| 461 |
+
rrsigname = rrsigset.name
|
| 462 |
+
rrsigrdataset = rrsigset
|
| 463 |
+
|
| 464 |
+
rrname = rrname.choose_relativity(origin)
|
| 465 |
+
rrsigname = rrsigname.choose_relativity(origin)
|
| 466 |
+
if rrname != rrsigname:
|
| 467 |
+
raise ValidationFailure("owner names do not match")
|
| 468 |
+
|
| 469 |
+
for rrsig in rrsigrdataset:
|
| 470 |
+
if not isinstance(rrsig, RRSIG):
|
| 471 |
+
raise ValidationFailure("expected an RRSIG")
|
| 472 |
+
try:
|
| 473 |
+
_validate_rrsig(rrset, rrsig, keys, origin, now, policy)
|
| 474 |
+
return
|
| 475 |
+
except (ValidationFailure, UnsupportedAlgorithm):
|
| 476 |
+
pass
|
| 477 |
+
raise ValidationFailure("no RRSIGs validated")
|
| 478 |
+
|
| 479 |
+
|
| 480 |
+
def _sign(
|
| 481 |
+
rrset: Union[dns.rrset.RRset, Tuple[dns.name.Name, dns.rdataset.Rdataset]],
|
| 482 |
+
private_key: PrivateKey,
|
| 483 |
+
signer: dns.name.Name,
|
| 484 |
+
dnskey: DNSKEY,
|
| 485 |
+
inception: Optional[Union[datetime, str, int, float]] = None,
|
| 486 |
+
expiration: Optional[Union[datetime, str, int, float]] = None,
|
| 487 |
+
lifetime: Optional[int] = None,
|
| 488 |
+
verify: bool = False,
|
| 489 |
+
policy: Optional[Policy] = None,
|
| 490 |
+
origin: Optional[dns.name.Name] = None,
|
| 491 |
+
deterministic: bool = True,
|
| 492 |
+
) -> RRSIG:
|
| 493 |
+
"""Sign RRset using private key.
|
| 494 |
+
|
| 495 |
+
*rrset*, the RRset to validate. This can be a
|
| 496 |
+
``dns.rrset.RRset`` or a (``dns.name.Name``, ``dns.rdataset.Rdataset``)
|
| 497 |
+
tuple.
|
| 498 |
+
|
| 499 |
+
*private_key*, the private key to use for signing, a
|
| 500 |
+
``cryptography.hazmat.primitives.asymmetric`` private key class applicable
|
| 501 |
+
for DNSSEC.
|
| 502 |
+
|
| 503 |
+
*signer*, a ``dns.name.Name``, the Signer's name.
|
| 504 |
+
|
| 505 |
+
*dnskey*, a ``DNSKEY`` matching ``private_key``.
|
| 506 |
+
|
| 507 |
+
*inception*, a ``datetime``, ``str``, ``int``, ``float`` or ``None``, the
|
| 508 |
+
signature inception time. If ``None``, the current time is used. If a ``str``, the
|
| 509 |
+
format is "YYYYMMDDHHMMSS" or alternatively the number of seconds since the UNIX
|
| 510 |
+
epoch in text form; this is the same the RRSIG rdata's text form.
|
| 511 |
+
Values of type `int` or `float` are interpreted as seconds since the UNIX epoch.
|
| 512 |
+
|
| 513 |
+
*expiration*, a ``datetime``, ``str``, ``int``, ``float`` or ``None``, the signature
|
| 514 |
+
expiration time. If ``None``, the expiration time will be the inception time plus
|
| 515 |
+
the value of the *lifetime* parameter. See the description of *inception* above
|
| 516 |
+
for how the various parameter types are interpreted.
|
| 517 |
+
|
| 518 |
+
*lifetime*, an ``int`` or ``None``, the signature lifetime in seconds. This
|
| 519 |
+
parameter is only meaningful if *expiration* is ``None``.
|
| 520 |
+
|
| 521 |
+
*verify*, a ``bool``. If set to ``True``, the signer will verify signatures
|
| 522 |
+
after they are created; the default is ``False``.
|
| 523 |
+
|
| 524 |
+
*policy*, a ``dns.dnssec.Policy`` or ``None``. If ``None``, the default policy,
|
| 525 |
+
``dns.dnssec.default_policy`` is used; this policy defaults to that of RFC 8624.
|
| 526 |
+
|
| 527 |
+
*origin*, a ``dns.name.Name`` or ``None``. If ``None``, the default, then all
|
| 528 |
+
names in the rrset (including its owner name) must be absolute; otherwise the
|
| 529 |
+
specified origin will be used to make names absolute when signing.
|
| 530 |
+
|
| 531 |
+
*deterministic*, a ``bool``. If ``True``, the default, use deterministic
|
| 532 |
+
(reproducible) signatures when supported by the algorithm used for signing.
|
| 533 |
+
Currently, this only affects ECDSA.
|
| 534 |
+
|
| 535 |
+
Raises ``DeniedByPolicy`` if the signature is denied by policy.
|
| 536 |
+
"""
|
| 537 |
+
|
| 538 |
+
if policy is None:
|
| 539 |
+
policy = default_policy
|
| 540 |
+
if not policy.ok_to_sign(dnskey):
|
| 541 |
+
raise DeniedByPolicy
|
| 542 |
+
|
| 543 |
+
if isinstance(rrset, tuple):
|
| 544 |
+
rdclass = rrset[1].rdclass
|
| 545 |
+
rdtype = rrset[1].rdtype
|
| 546 |
+
rrname = rrset[0]
|
| 547 |
+
original_ttl = rrset[1].ttl
|
| 548 |
+
else:
|
| 549 |
+
rdclass = rrset.rdclass
|
| 550 |
+
rdtype = rrset.rdtype
|
| 551 |
+
rrname = rrset.name
|
| 552 |
+
original_ttl = rrset.ttl
|
| 553 |
+
|
| 554 |
+
if inception is not None:
|
| 555 |
+
rrsig_inception = to_timestamp(inception)
|
| 556 |
+
else:
|
| 557 |
+
rrsig_inception = int(time.time())
|
| 558 |
+
|
| 559 |
+
if expiration is not None:
|
| 560 |
+
rrsig_expiration = to_timestamp(expiration)
|
| 561 |
+
elif lifetime is not None:
|
| 562 |
+
rrsig_expiration = rrsig_inception + lifetime
|
| 563 |
+
else:
|
| 564 |
+
raise ValueError("expiration or lifetime must be specified")
|
| 565 |
+
|
| 566 |
+
# Derelativize now because we need a correct labels length for the
|
| 567 |
+
# rrsig_template.
|
| 568 |
+
if origin is not None:
|
| 569 |
+
rrname = rrname.derelativize(origin)
|
| 570 |
+
labels = len(rrname) - 1
|
| 571 |
+
|
| 572 |
+
# Adjust labels appropriately for wildcards.
|
| 573 |
+
if rrname.is_wild():
|
| 574 |
+
labels -= 1
|
| 575 |
+
|
| 576 |
+
rrsig_template = RRSIG(
|
| 577 |
+
rdclass=rdclass,
|
| 578 |
+
rdtype=dns.rdatatype.RRSIG,
|
| 579 |
+
type_covered=rdtype,
|
| 580 |
+
algorithm=dnskey.algorithm,
|
| 581 |
+
labels=labels,
|
| 582 |
+
original_ttl=original_ttl,
|
| 583 |
+
expiration=rrsig_expiration,
|
| 584 |
+
inception=rrsig_inception,
|
| 585 |
+
key_tag=key_id(dnskey),
|
| 586 |
+
signer=signer,
|
| 587 |
+
signature=b"",
|
| 588 |
+
)
|
| 589 |
+
|
| 590 |
+
data = dns.dnssec._make_rrsig_signature_data(rrset, rrsig_template, origin)
|
| 591 |
+
|
| 592 |
+
# pylint: disable=possibly-used-before-assignment
|
| 593 |
+
if isinstance(private_key, GenericPrivateKey):
|
| 594 |
+
signing_key = private_key
|
| 595 |
+
else:
|
| 596 |
+
try:
|
| 597 |
+
private_cls = get_algorithm_cls_from_dnskey(dnskey)
|
| 598 |
+
signing_key = private_cls(key=private_key)
|
| 599 |
+
except UnsupportedAlgorithm:
|
| 600 |
+
raise TypeError("Unsupported key algorithm")
|
| 601 |
+
|
| 602 |
+
signature = signing_key.sign(data, verify, deterministic)
|
| 603 |
+
|
| 604 |
+
return cast(RRSIG, rrsig_template.replace(signature=signature))
|
| 605 |
+
|
| 606 |
+
|
| 607 |
+
def _make_rrsig_signature_data(
|
| 608 |
+
rrset: Union[dns.rrset.RRset, Tuple[dns.name.Name, dns.rdataset.Rdataset]],
|
| 609 |
+
rrsig: RRSIG,
|
| 610 |
+
origin: Optional[dns.name.Name] = None,
|
| 611 |
+
) -> bytes:
|
| 612 |
+
"""Create signature rdata.
|
| 613 |
+
|
| 614 |
+
*rrset*, the RRset to sign/validate. This can be a
|
| 615 |
+
``dns.rrset.RRset`` or a (``dns.name.Name``, ``dns.rdataset.Rdataset``)
|
| 616 |
+
tuple.
|
| 617 |
+
|
| 618 |
+
*rrsig*, a ``dns.rdata.Rdata``, the signature to validate, or the
|
| 619 |
+
signature template used when signing.
|
| 620 |
+
|
| 621 |
+
*origin*, a ``dns.name.Name`` or ``None``, the origin to use for relative
|
| 622 |
+
names.
|
| 623 |
+
|
| 624 |
+
Raises ``UnsupportedAlgorithm`` if the algorithm is recognized by
|
| 625 |
+
dnspython but not implemented.
|
| 626 |
+
"""
|
| 627 |
+
|
| 628 |
+
if isinstance(origin, str):
|
| 629 |
+
origin = dns.name.from_text(origin, dns.name.root)
|
| 630 |
+
|
| 631 |
+
signer = rrsig.signer
|
| 632 |
+
if not signer.is_absolute():
|
| 633 |
+
if origin is None:
|
| 634 |
+
raise ValidationFailure("relative RR name without an origin specified")
|
| 635 |
+
signer = signer.derelativize(origin)
|
| 636 |
+
|
| 637 |
+
# For convenience, allow the rrset to be specified as a (name,
|
| 638 |
+
# rdataset) tuple as well as a proper rrset
|
| 639 |
+
rrname, rdataset = _get_rrname_rdataset(rrset)
|
| 640 |
+
|
| 641 |
+
data = b""
|
| 642 |
+
wire = rrsig.to_wire(origin=signer)
|
| 643 |
+
assert wire is not None # for mypy
|
| 644 |
+
data += wire[:18]
|
| 645 |
+
data += rrsig.signer.to_digestable(signer)
|
| 646 |
+
|
| 647 |
+
# Derelativize the name before considering labels.
|
| 648 |
+
if not rrname.is_absolute():
|
| 649 |
+
if origin is None:
|
| 650 |
+
raise ValidationFailure("relative RR name without an origin specified")
|
| 651 |
+
rrname = rrname.derelativize(origin)
|
| 652 |
+
|
| 653 |
+
name_len = len(rrname)
|
| 654 |
+
if rrname.is_wild() and rrsig.labels != name_len - 2:
|
| 655 |
+
raise ValidationFailure("wild owner name has wrong label length")
|
| 656 |
+
if name_len - 1 < rrsig.labels:
|
| 657 |
+
raise ValidationFailure("owner name longer than RRSIG labels")
|
| 658 |
+
elif rrsig.labels < name_len - 1:
|
| 659 |
+
suffix = rrname.split(rrsig.labels + 1)[1]
|
| 660 |
+
rrname = dns.name.from_text("*", suffix)
|
| 661 |
+
rrnamebuf = rrname.to_digestable()
|
| 662 |
+
rrfixed = struct.pack("!HHI", rdataset.rdtype, rdataset.rdclass, rrsig.original_ttl)
|
| 663 |
+
rdatas = [rdata.to_digestable(origin) for rdata in rdataset]
|
| 664 |
+
for rdata in sorted(rdatas):
|
| 665 |
+
data += rrnamebuf
|
| 666 |
+
data += rrfixed
|
| 667 |
+
rrlen = struct.pack("!H", len(rdata))
|
| 668 |
+
data += rrlen
|
| 669 |
+
data += rdata
|
| 670 |
+
|
| 671 |
+
return data
|
| 672 |
+
|
| 673 |
+
|
| 674 |
+
def _make_dnskey(
|
| 675 |
+
public_key: PublicKey,
|
| 676 |
+
algorithm: Union[int, str],
|
| 677 |
+
flags: int = Flag.ZONE,
|
| 678 |
+
protocol: int = 3,
|
| 679 |
+
) -> DNSKEY:
|
| 680 |
+
"""Convert a public key to DNSKEY Rdata
|
| 681 |
+
|
| 682 |
+
*public_key*, a ``PublicKey`` (``GenericPublicKey`` or
|
| 683 |
+
``cryptography.hazmat.primitives.asymmetric``) to convert.
|
| 684 |
+
|
| 685 |
+
*algorithm*, a ``str`` or ``int`` specifying the DNSKEY algorithm.
|
| 686 |
+
|
| 687 |
+
*flags*: DNSKEY flags field as an integer.
|
| 688 |
+
|
| 689 |
+
*protocol*: DNSKEY protocol field as an integer.
|
| 690 |
+
|
| 691 |
+
Raises ``ValueError`` if the specified key algorithm parameters are not
|
| 692 |
+
unsupported, ``TypeError`` if the key type is unsupported,
|
| 693 |
+
`UnsupportedAlgorithm` if the algorithm is unknown and
|
| 694 |
+
`AlgorithmKeyMismatch` if the algorithm does not match the key type.
|
| 695 |
+
|
| 696 |
+
Return DNSKEY ``Rdata``.
|
| 697 |
+
"""
|
| 698 |
+
|
| 699 |
+
algorithm = Algorithm.make(algorithm)
|
| 700 |
+
|
| 701 |
+
# pylint: disable=possibly-used-before-assignment
|
| 702 |
+
if isinstance(public_key, GenericPublicKey):
|
| 703 |
+
return public_key.to_dnskey(flags=flags, protocol=protocol)
|
| 704 |
+
else:
|
| 705 |
+
public_cls = get_algorithm_cls(algorithm).public_cls
|
| 706 |
+
return public_cls(key=public_key).to_dnskey(flags=flags, protocol=protocol)
|
| 707 |
+
|
| 708 |
+
|
| 709 |
+
def _make_cdnskey(
|
| 710 |
+
public_key: PublicKey,
|
| 711 |
+
algorithm: Union[int, str],
|
| 712 |
+
flags: int = Flag.ZONE,
|
| 713 |
+
protocol: int = 3,
|
| 714 |
+
) -> CDNSKEY:
|
| 715 |
+
"""Convert a public key to CDNSKEY Rdata
|
| 716 |
+
|
| 717 |
+
*public_key*, the public key to convert, a
|
| 718 |
+
``cryptography.hazmat.primitives.asymmetric`` public key class applicable
|
| 719 |
+
for DNSSEC.
|
| 720 |
+
|
| 721 |
+
*algorithm*, a ``str`` or ``int`` specifying the DNSKEY algorithm.
|
| 722 |
+
|
| 723 |
+
*flags*: DNSKEY flags field as an integer.
|
| 724 |
+
|
| 725 |
+
*protocol*: DNSKEY protocol field as an integer.
|
| 726 |
+
|
| 727 |
+
Raises ``ValueError`` if the specified key algorithm parameters are not
|
| 728 |
+
unsupported, ``TypeError`` if the key type is unsupported,
|
| 729 |
+
`UnsupportedAlgorithm` if the algorithm is unknown and
|
| 730 |
+
`AlgorithmKeyMismatch` if the algorithm does not match the key type.
|
| 731 |
+
|
| 732 |
+
Return CDNSKEY ``Rdata``.
|
| 733 |
+
"""
|
| 734 |
+
|
| 735 |
+
dnskey = _make_dnskey(public_key, algorithm, flags, protocol)
|
| 736 |
+
|
| 737 |
+
return CDNSKEY(
|
| 738 |
+
rdclass=dnskey.rdclass,
|
| 739 |
+
rdtype=dns.rdatatype.CDNSKEY,
|
| 740 |
+
flags=dnskey.flags,
|
| 741 |
+
protocol=dnskey.protocol,
|
| 742 |
+
algorithm=dnskey.algorithm,
|
| 743 |
+
key=dnskey.key,
|
| 744 |
+
)
|
| 745 |
+
|
| 746 |
+
|
| 747 |
+
def nsec3_hash(
|
| 748 |
+
domain: Union[dns.name.Name, str],
|
| 749 |
+
salt: Optional[Union[str, bytes]],
|
| 750 |
+
iterations: int,
|
| 751 |
+
algorithm: Union[int, str],
|
| 752 |
+
) -> str:
|
| 753 |
+
"""
|
| 754 |
+
Calculate the NSEC3 hash, according to
|
| 755 |
+
https://tools.ietf.org/html/rfc5155#section-5
|
| 756 |
+
|
| 757 |
+
*domain*, a ``dns.name.Name`` or ``str``, the name to hash.
|
| 758 |
+
|
| 759 |
+
*salt*, a ``str``, ``bytes``, or ``None``, the hash salt. If a
|
| 760 |
+
string, it is decoded as a hex string.
|
| 761 |
+
|
| 762 |
+
*iterations*, an ``int``, the number of iterations.
|
| 763 |
+
|
| 764 |
+
*algorithm*, a ``str`` or ``int``, the hash algorithm.
|
| 765 |
+
The only defined algorithm is SHA1.
|
| 766 |
+
|
| 767 |
+
Returns a ``str``, the encoded NSEC3 hash.
|
| 768 |
+
"""
|
| 769 |
+
|
| 770 |
+
b32_conversion = str.maketrans(
|
| 771 |
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", "0123456789ABCDEFGHIJKLMNOPQRSTUV"
|
| 772 |
+
)
|
| 773 |
+
|
| 774 |
+
try:
|
| 775 |
+
if isinstance(algorithm, str):
|
| 776 |
+
algorithm = NSEC3Hash[algorithm.upper()]
|
| 777 |
+
except Exception:
|
| 778 |
+
raise ValueError("Wrong hash algorithm (only SHA1 is supported)")
|
| 779 |
+
|
| 780 |
+
if algorithm != NSEC3Hash.SHA1:
|
| 781 |
+
raise ValueError("Wrong hash algorithm (only SHA1 is supported)")
|
| 782 |
+
|
| 783 |
+
if salt is None:
|
| 784 |
+
salt_encoded = b""
|
| 785 |
+
elif isinstance(salt, str):
|
| 786 |
+
if len(salt) % 2 == 0:
|
| 787 |
+
salt_encoded = bytes.fromhex(salt)
|
| 788 |
+
else:
|
| 789 |
+
raise ValueError("Invalid salt length")
|
| 790 |
+
else:
|
| 791 |
+
salt_encoded = salt
|
| 792 |
+
|
| 793 |
+
if not isinstance(domain, dns.name.Name):
|
| 794 |
+
domain = dns.name.from_text(domain)
|
| 795 |
+
domain_encoded = domain.canonicalize().to_wire()
|
| 796 |
+
assert domain_encoded is not None
|
| 797 |
+
|
| 798 |
+
digest = hashlib.sha1(domain_encoded + salt_encoded).digest()
|
| 799 |
+
for _ in range(iterations):
|
| 800 |
+
digest = hashlib.sha1(digest + salt_encoded).digest()
|
| 801 |
+
|
| 802 |
+
output = base64.b32encode(digest).decode("utf-8")
|
| 803 |
+
output = output.translate(b32_conversion)
|
| 804 |
+
|
| 805 |
+
return output
|
| 806 |
+
|
| 807 |
+
|
| 808 |
+
def make_ds_rdataset(
|
| 809 |
+
rrset: Union[dns.rrset.RRset, Tuple[dns.name.Name, dns.rdataset.Rdataset]],
|
| 810 |
+
algorithms: Set[Union[DSDigest, str]],
|
| 811 |
+
origin: Optional[dns.name.Name] = None,
|
| 812 |
+
) -> dns.rdataset.Rdataset:
|
| 813 |
+
"""Create a DS record from DNSKEY/CDNSKEY/CDS.
|
| 814 |
+
|
| 815 |
+
*rrset*, the RRset to create DS Rdataset for. This can be a
|
| 816 |
+
``dns.rrset.RRset`` or a (``dns.name.Name``, ``dns.rdataset.Rdataset``)
|
| 817 |
+
tuple.
|
| 818 |
+
|
| 819 |
+
*algorithms*, a set of ``str`` or ``int`` specifying the hash algorithms.
|
| 820 |
+
The currently supported hashes are "SHA1", "SHA256", and "SHA384". Case
|
| 821 |
+
does not matter for these strings. If the RRset is a CDS, only digest
|
| 822 |
+
algorithms matching algorithms are accepted.
|
| 823 |
+
|
| 824 |
+
*origin*, a ``dns.name.Name`` or ``None``. If `key` is a relative name,
|
| 825 |
+
then it will be made absolute using the specified origin.
|
| 826 |
+
|
| 827 |
+
Raises ``UnsupportedAlgorithm`` if any of the algorithms are unknown and
|
| 828 |
+
``ValueError`` if the given RRset is not usable.
|
| 829 |
+
|
| 830 |
+
Returns a ``dns.rdataset.Rdataset``
|
| 831 |
+
"""
|
| 832 |
+
|
| 833 |
+
rrname, rdataset = _get_rrname_rdataset(rrset)
|
| 834 |
+
|
| 835 |
+
if rdataset.rdtype not in (
|
| 836 |
+
dns.rdatatype.DNSKEY,
|
| 837 |
+
dns.rdatatype.CDNSKEY,
|
| 838 |
+
dns.rdatatype.CDS,
|
| 839 |
+
):
|
| 840 |
+
raise ValueError("rrset not a DNSKEY/CDNSKEY/CDS")
|
| 841 |
+
|
| 842 |
+
_algorithms = set()
|
| 843 |
+
for algorithm in algorithms:
|
| 844 |
+
try:
|
| 845 |
+
if isinstance(algorithm, str):
|
| 846 |
+
algorithm = DSDigest[algorithm.upper()]
|
| 847 |
+
except Exception:
|
| 848 |
+
raise UnsupportedAlgorithm(f'unsupported algorithm "{algorithm}"')
|
| 849 |
+
_algorithms.add(algorithm)
|
| 850 |
+
|
| 851 |
+
if rdataset.rdtype == dns.rdatatype.CDS:
|
| 852 |
+
res = []
|
| 853 |
+
for rdata in cds_rdataset_to_ds_rdataset(rdataset):
|
| 854 |
+
if rdata.digest_type in _algorithms:
|
| 855 |
+
res.append(rdata)
|
| 856 |
+
if len(res) == 0:
|
| 857 |
+
raise ValueError("no acceptable CDS rdata found")
|
| 858 |
+
return dns.rdataset.from_rdata_list(rdataset.ttl, res)
|
| 859 |
+
|
| 860 |
+
res = []
|
| 861 |
+
for algorithm in _algorithms:
|
| 862 |
+
res.extend(dnskey_rdataset_to_cds_rdataset(rrname, rdataset, algorithm, origin))
|
| 863 |
+
return dns.rdataset.from_rdata_list(rdataset.ttl, res)
|
| 864 |
+
|
| 865 |
+
|
| 866 |
+
def cds_rdataset_to_ds_rdataset(
|
| 867 |
+
rdataset: dns.rdataset.Rdataset,
|
| 868 |
+
) -> dns.rdataset.Rdataset:
|
| 869 |
+
"""Create a CDS record from DS.
|
| 870 |
+
|
| 871 |
+
*rdataset*, a ``dns.rdataset.Rdataset``, to create DS Rdataset for.
|
| 872 |
+
|
| 873 |
+
Raises ``ValueError`` if the rdataset is not CDS.
|
| 874 |
+
|
| 875 |
+
Returns a ``dns.rdataset.Rdataset``
|
| 876 |
+
"""
|
| 877 |
+
|
| 878 |
+
if rdataset.rdtype != dns.rdatatype.CDS:
|
| 879 |
+
raise ValueError("rdataset not a CDS")
|
| 880 |
+
res = []
|
| 881 |
+
for rdata in rdataset:
|
| 882 |
+
res.append(
|
| 883 |
+
CDS(
|
| 884 |
+
rdclass=rdata.rdclass,
|
| 885 |
+
rdtype=dns.rdatatype.DS,
|
| 886 |
+
key_tag=rdata.key_tag,
|
| 887 |
+
algorithm=rdata.algorithm,
|
| 888 |
+
digest_type=rdata.digest_type,
|
| 889 |
+
digest=rdata.digest,
|
| 890 |
+
)
|
| 891 |
+
)
|
| 892 |
+
return dns.rdataset.from_rdata_list(rdataset.ttl, res)
|
| 893 |
+
|
| 894 |
+
|
| 895 |
+
def dnskey_rdataset_to_cds_rdataset(
|
| 896 |
+
name: Union[dns.name.Name, str],
|
| 897 |
+
rdataset: dns.rdataset.Rdataset,
|
| 898 |
+
algorithm: Union[DSDigest, str],
|
| 899 |
+
origin: Optional[dns.name.Name] = None,
|
| 900 |
+
) -> dns.rdataset.Rdataset:
|
| 901 |
+
"""Create a CDS record from DNSKEY/CDNSKEY.
|
| 902 |
+
|
| 903 |
+
*name*, a ``dns.name.Name`` or ``str``, the owner name of the CDS record.
|
| 904 |
+
|
| 905 |
+
*rdataset*, a ``dns.rdataset.Rdataset``, to create DS Rdataset for.
|
| 906 |
+
|
| 907 |
+
*algorithm*, a ``str`` or ``int`` specifying the hash algorithm.
|
| 908 |
+
The currently supported hashes are "SHA1", "SHA256", and "SHA384". Case
|
| 909 |
+
does not matter for these strings.
|
| 910 |
+
|
| 911 |
+
*origin*, a ``dns.name.Name`` or ``None``. If `key` is a relative name,
|
| 912 |
+
then it will be made absolute using the specified origin.
|
| 913 |
+
|
| 914 |
+
Raises ``UnsupportedAlgorithm`` if the algorithm is unknown or
|
| 915 |
+
``ValueError`` if the rdataset is not DNSKEY/CDNSKEY.
|
| 916 |
+
|
| 917 |
+
Returns a ``dns.rdataset.Rdataset``
|
| 918 |
+
"""
|
| 919 |
+
|
| 920 |
+
if rdataset.rdtype not in (dns.rdatatype.DNSKEY, dns.rdatatype.CDNSKEY):
|
| 921 |
+
raise ValueError("rdataset not a DNSKEY/CDNSKEY")
|
| 922 |
+
res = []
|
| 923 |
+
for rdata in rdataset:
|
| 924 |
+
res.append(make_cds(name, rdata, algorithm, origin))
|
| 925 |
+
return dns.rdataset.from_rdata_list(rdataset.ttl, res)
|
| 926 |
+
|
| 927 |
+
|
| 928 |
+
def dnskey_rdataset_to_cdnskey_rdataset(
|
| 929 |
+
rdataset: dns.rdataset.Rdataset,
|
| 930 |
+
) -> dns.rdataset.Rdataset:
|
| 931 |
+
"""Create a CDNSKEY record from DNSKEY.
|
| 932 |
+
|
| 933 |
+
*rdataset*, a ``dns.rdataset.Rdataset``, to create CDNSKEY Rdataset for.
|
| 934 |
+
|
| 935 |
+
Returns a ``dns.rdataset.Rdataset``
|
| 936 |
+
"""
|
| 937 |
+
|
| 938 |
+
if rdataset.rdtype != dns.rdatatype.DNSKEY:
|
| 939 |
+
raise ValueError("rdataset not a DNSKEY")
|
| 940 |
+
res = []
|
| 941 |
+
for rdata in rdataset:
|
| 942 |
+
res.append(
|
| 943 |
+
CDNSKEY(
|
| 944 |
+
rdclass=rdataset.rdclass,
|
| 945 |
+
rdtype=rdataset.rdtype,
|
| 946 |
+
flags=rdata.flags,
|
| 947 |
+
protocol=rdata.protocol,
|
| 948 |
+
algorithm=rdata.algorithm,
|
| 949 |
+
key=rdata.key,
|
| 950 |
+
)
|
| 951 |
+
)
|
| 952 |
+
return dns.rdataset.from_rdata_list(rdataset.ttl, res)
|
| 953 |
+
|
| 954 |
+
|
| 955 |
+
def default_rrset_signer(
|
| 956 |
+
txn: dns.transaction.Transaction,
|
| 957 |
+
rrset: dns.rrset.RRset,
|
| 958 |
+
signer: dns.name.Name,
|
| 959 |
+
ksks: List[Tuple[PrivateKey, DNSKEY]],
|
| 960 |
+
zsks: List[Tuple[PrivateKey, DNSKEY]],
|
| 961 |
+
inception: Optional[Union[datetime, str, int, float]] = None,
|
| 962 |
+
expiration: Optional[Union[datetime, str, int, float]] = None,
|
| 963 |
+
lifetime: Optional[int] = None,
|
| 964 |
+
policy: Optional[Policy] = None,
|
| 965 |
+
origin: Optional[dns.name.Name] = None,
|
| 966 |
+
deterministic: bool = True,
|
| 967 |
+
) -> None:
|
| 968 |
+
"""Default RRset signer"""
|
| 969 |
+
|
| 970 |
+
if rrset.rdtype in set(
|
| 971 |
+
[
|
| 972 |
+
dns.rdatatype.RdataType.DNSKEY,
|
| 973 |
+
dns.rdatatype.RdataType.CDS,
|
| 974 |
+
dns.rdatatype.RdataType.CDNSKEY,
|
| 975 |
+
]
|
| 976 |
+
):
|
| 977 |
+
keys = ksks
|
| 978 |
+
else:
|
| 979 |
+
keys = zsks
|
| 980 |
+
|
| 981 |
+
for private_key, dnskey in keys:
|
| 982 |
+
rrsig = dns.dnssec.sign(
|
| 983 |
+
rrset=rrset,
|
| 984 |
+
private_key=private_key,
|
| 985 |
+
dnskey=dnskey,
|
| 986 |
+
inception=inception,
|
| 987 |
+
expiration=expiration,
|
| 988 |
+
lifetime=lifetime,
|
| 989 |
+
signer=signer,
|
| 990 |
+
policy=policy,
|
| 991 |
+
origin=origin,
|
| 992 |
+
deterministic=deterministic,
|
| 993 |
+
)
|
| 994 |
+
txn.add(rrset.name, rrset.ttl, rrsig)
|
| 995 |
+
|
| 996 |
+
|
| 997 |
+
def sign_zone(
|
| 998 |
+
zone: dns.zone.Zone,
|
| 999 |
+
txn: Optional[dns.transaction.Transaction] = None,
|
| 1000 |
+
keys: Optional[List[Tuple[PrivateKey, DNSKEY]]] = None,
|
| 1001 |
+
add_dnskey: bool = True,
|
| 1002 |
+
dnskey_ttl: Optional[int] = None,
|
| 1003 |
+
inception: Optional[Union[datetime, str, int, float]] = None,
|
| 1004 |
+
expiration: Optional[Union[datetime, str, int, float]] = None,
|
| 1005 |
+
lifetime: Optional[int] = None,
|
| 1006 |
+
nsec3: Optional[NSEC3PARAM] = None,
|
| 1007 |
+
rrset_signer: Optional[RRsetSigner] = None,
|
| 1008 |
+
policy: Optional[Policy] = None,
|
| 1009 |
+
deterministic: bool = True,
|
| 1010 |
+
) -> None:
|
| 1011 |
+
"""Sign zone.
|
| 1012 |
+
|
| 1013 |
+
*zone*, a ``dns.zone.Zone``, the zone to sign.
|
| 1014 |
+
|
| 1015 |
+
*txn*, a ``dns.transaction.Transaction``, an optional transaction to use for
|
| 1016 |
+
signing.
|
| 1017 |
+
|
| 1018 |
+
*keys*, a list of (``PrivateKey``, ``DNSKEY``) tuples, to use for signing. KSK/ZSK
|
| 1019 |
+
roles are assigned automatically if the SEP flag is used, otherwise all RRsets are
|
| 1020 |
+
signed by all keys.
|
| 1021 |
+
|
| 1022 |
+
*add_dnskey*, a ``bool``. If ``True``, the default, all specified DNSKEYs are
|
| 1023 |
+
automatically added to the zone on signing.
|
| 1024 |
+
|
| 1025 |
+
*dnskey_ttl*, a``int``, specifies the TTL for DNSKEY RRs. If not specified the TTL
|
| 1026 |
+
of the existing DNSKEY RRset used or the TTL of the SOA RRset.
|
| 1027 |
+
|
| 1028 |
+
*inception*, a ``datetime``, ``str``, ``int``, ``float`` or ``None``, the signature
|
| 1029 |
+
inception time. If ``None``, the current time is used. If a ``str``, the format is
|
| 1030 |
+
"YYYYMMDDHHMMSS" or alternatively the number of seconds since the UNIX epoch in text
|
| 1031 |
+
form; this is the same the RRSIG rdata's text form. Values of type `int` or `float`
|
| 1032 |
+
are interpreted as seconds since the UNIX epoch.
|
| 1033 |
+
|
| 1034 |
+
*expiration*, a ``datetime``, ``str``, ``int``, ``float`` or ``None``, the signature
|
| 1035 |
+
expiration time. If ``None``, the expiration time will be the inception time plus
|
| 1036 |
+
the value of the *lifetime* parameter. See the description of *inception* above for
|
| 1037 |
+
how the various parameter types are interpreted.
|
| 1038 |
+
|
| 1039 |
+
*lifetime*, an ``int`` or ``None``, the signature lifetime in seconds. This
|
| 1040 |
+
parameter is only meaningful if *expiration* is ``None``.
|
| 1041 |
+
|
| 1042 |
+
*nsec3*, a ``NSEC3PARAM`` Rdata, configures signing using NSEC3. Not yet
|
| 1043 |
+
implemented.
|
| 1044 |
+
|
| 1045 |
+
*rrset_signer*, a ``Callable``, an optional function for signing RRsets. The
|
| 1046 |
+
function requires two arguments: transaction and RRset. If the not specified,
|
| 1047 |
+
``dns.dnssec.default_rrset_signer`` will be used.
|
| 1048 |
+
|
| 1049 |
+
*deterministic*, a ``bool``. If ``True``, the default, use deterministic
|
| 1050 |
+
(reproducible) signatures when supported by the algorithm used for signing.
|
| 1051 |
+
Currently, this only affects ECDSA.
|
| 1052 |
+
|
| 1053 |
+
Returns ``None``.
|
| 1054 |
+
"""
|
| 1055 |
+
|
| 1056 |
+
ksks = []
|
| 1057 |
+
zsks = []
|
| 1058 |
+
|
| 1059 |
+
# if we have both KSKs and ZSKs, split by SEP flag. if not, sign all
|
| 1060 |
+
# records with all keys
|
| 1061 |
+
if keys:
|
| 1062 |
+
for key in keys:
|
| 1063 |
+
if key[1].flags & Flag.SEP:
|
| 1064 |
+
ksks.append(key)
|
| 1065 |
+
else:
|
| 1066 |
+
zsks.append(key)
|
| 1067 |
+
if not ksks:
|
| 1068 |
+
ksks = keys
|
| 1069 |
+
if not zsks:
|
| 1070 |
+
zsks = keys
|
| 1071 |
+
else:
|
| 1072 |
+
keys = []
|
| 1073 |
+
|
| 1074 |
+
if txn:
|
| 1075 |
+
cm: contextlib.AbstractContextManager = contextlib.nullcontext(txn)
|
| 1076 |
+
else:
|
| 1077 |
+
cm = zone.writer()
|
| 1078 |
+
|
| 1079 |
+
if zone.origin is None:
|
| 1080 |
+
raise ValueError("no zone origin")
|
| 1081 |
+
|
| 1082 |
+
with cm as _txn:
|
| 1083 |
+
if add_dnskey:
|
| 1084 |
+
if dnskey_ttl is None:
|
| 1085 |
+
dnskey = _txn.get(zone.origin, dns.rdatatype.DNSKEY)
|
| 1086 |
+
if dnskey:
|
| 1087 |
+
dnskey_ttl = dnskey.ttl
|
| 1088 |
+
else:
|
| 1089 |
+
soa = _txn.get(zone.origin, dns.rdatatype.SOA)
|
| 1090 |
+
dnskey_ttl = soa.ttl
|
| 1091 |
+
for _, dnskey in keys:
|
| 1092 |
+
_txn.add(zone.origin, dnskey_ttl, dnskey)
|
| 1093 |
+
|
| 1094 |
+
if nsec3:
|
| 1095 |
+
raise NotImplementedError("Signing with NSEC3 not yet implemented")
|
| 1096 |
+
else:
|
| 1097 |
+
_rrset_signer = rrset_signer or functools.partial(
|
| 1098 |
+
default_rrset_signer,
|
| 1099 |
+
signer=zone.origin,
|
| 1100 |
+
ksks=ksks,
|
| 1101 |
+
zsks=zsks,
|
| 1102 |
+
inception=inception,
|
| 1103 |
+
expiration=expiration,
|
| 1104 |
+
lifetime=lifetime,
|
| 1105 |
+
policy=policy,
|
| 1106 |
+
origin=zone.origin,
|
| 1107 |
+
deterministic=deterministic,
|
| 1108 |
+
)
|
| 1109 |
+
return _sign_zone_nsec(zone, _txn, _rrset_signer)
|
| 1110 |
+
|
| 1111 |
+
|
| 1112 |
+
def _sign_zone_nsec(
|
| 1113 |
+
zone: dns.zone.Zone,
|
| 1114 |
+
txn: dns.transaction.Transaction,
|
| 1115 |
+
rrset_signer: Optional[RRsetSigner] = None,
|
| 1116 |
+
) -> None:
|
| 1117 |
+
"""NSEC zone signer"""
|
| 1118 |
+
|
| 1119 |
+
def _txn_add_nsec(
|
| 1120 |
+
txn: dns.transaction.Transaction,
|
| 1121 |
+
name: dns.name.Name,
|
| 1122 |
+
next_secure: Optional[dns.name.Name],
|
| 1123 |
+
rdclass: dns.rdataclass.RdataClass,
|
| 1124 |
+
ttl: int,
|
| 1125 |
+
rrset_signer: Optional[RRsetSigner] = None,
|
| 1126 |
+
) -> None:
|
| 1127 |
+
"""NSEC zone signer helper"""
|
| 1128 |
+
mandatory_types = set(
|
| 1129 |
+
[dns.rdatatype.RdataType.RRSIG, dns.rdatatype.RdataType.NSEC]
|
| 1130 |
+
)
|
| 1131 |
+
node = txn.get_node(name)
|
| 1132 |
+
if node and next_secure:
|
| 1133 |
+
types = (
|
| 1134 |
+
set([rdataset.rdtype for rdataset in node.rdatasets]) | mandatory_types
|
| 1135 |
+
)
|
| 1136 |
+
windows = Bitmap.from_rdtypes(list(types))
|
| 1137 |
+
rrset = dns.rrset.from_rdata(
|
| 1138 |
+
name,
|
| 1139 |
+
ttl,
|
| 1140 |
+
NSEC(
|
| 1141 |
+
rdclass=rdclass,
|
| 1142 |
+
rdtype=dns.rdatatype.RdataType.NSEC,
|
| 1143 |
+
next=next_secure,
|
| 1144 |
+
windows=windows,
|
| 1145 |
+
),
|
| 1146 |
+
)
|
| 1147 |
+
txn.add(rrset)
|
| 1148 |
+
if rrset_signer:
|
| 1149 |
+
rrset_signer(txn, rrset)
|
| 1150 |
+
|
| 1151 |
+
rrsig_ttl = zone.get_soa().minimum
|
| 1152 |
+
delegation = None
|
| 1153 |
+
last_secure = None
|
| 1154 |
+
|
| 1155 |
+
for name in sorted(txn.iterate_names()):
|
| 1156 |
+
if delegation and name.is_subdomain(delegation):
|
| 1157 |
+
# names below delegations are not secure
|
| 1158 |
+
continue
|
| 1159 |
+
elif txn.get(name, dns.rdatatype.NS) and name != zone.origin:
|
| 1160 |
+
# inside delegation
|
| 1161 |
+
delegation = name
|
| 1162 |
+
else:
|
| 1163 |
+
# outside delegation
|
| 1164 |
+
delegation = None
|
| 1165 |
+
|
| 1166 |
+
if rrset_signer:
|
| 1167 |
+
node = txn.get_node(name)
|
| 1168 |
+
if node:
|
| 1169 |
+
for rdataset in node.rdatasets:
|
| 1170 |
+
if rdataset.rdtype == dns.rdatatype.RRSIG:
|
| 1171 |
+
# do not sign RRSIGs
|
| 1172 |
+
continue
|
| 1173 |
+
elif delegation and rdataset.rdtype != dns.rdatatype.DS:
|
| 1174 |
+
# do not sign delegations except DS records
|
| 1175 |
+
continue
|
| 1176 |
+
else:
|
| 1177 |
+
rrset = dns.rrset.from_rdata(name, rdataset.ttl, *rdataset)
|
| 1178 |
+
rrset_signer(txn, rrset)
|
| 1179 |
+
|
| 1180 |
+
# We need "is not None" as the empty name is False because its length is 0.
|
| 1181 |
+
if last_secure is not None:
|
| 1182 |
+
_txn_add_nsec(txn, last_secure, name, zone.rdclass, rrsig_ttl, rrset_signer)
|
| 1183 |
+
last_secure = name
|
| 1184 |
+
|
| 1185 |
+
if last_secure:
|
| 1186 |
+
_txn_add_nsec(
|
| 1187 |
+
txn, last_secure, zone.origin, zone.rdclass, rrsig_ttl, rrset_signer
|
| 1188 |
+
)
|
| 1189 |
+
|
| 1190 |
+
|
| 1191 |
+
def _need_pyca(*args, **kwargs):
|
| 1192 |
+
raise ImportError(
|
| 1193 |
+
"DNSSEC validation requires python cryptography"
|
| 1194 |
+
) # pragma: no cover
|
| 1195 |
+
|
| 1196 |
+
|
| 1197 |
+
if dns._features.have("dnssec"):
|
| 1198 |
+
from cryptography.exceptions import InvalidSignature
|
| 1199 |
+
from cryptography.hazmat.primitives.asymmetric import dsa # pylint: disable=W0611
|
| 1200 |
+
from cryptography.hazmat.primitives.asymmetric import ec # pylint: disable=W0611
|
| 1201 |
+
from cryptography.hazmat.primitives.asymmetric import ed448 # pylint: disable=W0611
|
| 1202 |
+
from cryptography.hazmat.primitives.asymmetric import rsa # pylint: disable=W0611
|
| 1203 |
+
from cryptography.hazmat.primitives.asymmetric import ( # pylint: disable=W0611
|
| 1204 |
+
ed25519,
|
| 1205 |
+
)
|
| 1206 |
+
|
| 1207 |
+
from dns.dnssecalgs import ( # pylint: disable=C0412
|
| 1208 |
+
get_algorithm_cls,
|
| 1209 |
+
get_algorithm_cls_from_dnskey,
|
| 1210 |
+
)
|
| 1211 |
+
from dns.dnssecalgs.base import GenericPrivateKey, GenericPublicKey
|
| 1212 |
+
|
| 1213 |
+
validate = _validate # type: ignore
|
| 1214 |
+
validate_rrsig = _validate_rrsig # type: ignore
|
| 1215 |
+
sign = _sign
|
| 1216 |
+
make_dnskey = _make_dnskey
|
| 1217 |
+
make_cdnskey = _make_cdnskey
|
| 1218 |
+
_have_pyca = True
|
| 1219 |
+
else: # pragma: no cover
|
| 1220 |
+
validate = _need_pyca
|
| 1221 |
+
validate_rrsig = _need_pyca
|
| 1222 |
+
sign = _need_pyca
|
| 1223 |
+
make_dnskey = _need_pyca
|
| 1224 |
+
make_cdnskey = _need_pyca
|
| 1225 |
+
_have_pyca = False
|
| 1226 |
+
|
| 1227 |
+
### BEGIN generated Algorithm constants
|
| 1228 |
+
|
| 1229 |
+
RSAMD5 = Algorithm.RSAMD5
|
| 1230 |
+
DH = Algorithm.DH
|
| 1231 |
+
DSA = Algorithm.DSA
|
| 1232 |
+
ECC = Algorithm.ECC
|
| 1233 |
+
RSASHA1 = Algorithm.RSASHA1
|
| 1234 |
+
DSANSEC3SHA1 = Algorithm.DSANSEC3SHA1
|
| 1235 |
+
RSASHA1NSEC3SHA1 = Algorithm.RSASHA1NSEC3SHA1
|
| 1236 |
+
RSASHA256 = Algorithm.RSASHA256
|
| 1237 |
+
RSASHA512 = Algorithm.RSASHA512
|
| 1238 |
+
ECCGOST = Algorithm.ECCGOST
|
| 1239 |
+
ECDSAP256SHA256 = Algorithm.ECDSAP256SHA256
|
| 1240 |
+
ECDSAP384SHA384 = Algorithm.ECDSAP384SHA384
|
| 1241 |
+
ED25519 = Algorithm.ED25519
|
| 1242 |
+
ED448 = Algorithm.ED448
|
| 1243 |
+
INDIRECT = Algorithm.INDIRECT
|
| 1244 |
+
PRIVATEDNS = Algorithm.PRIVATEDNS
|
| 1245 |
+
PRIVATEOID = Algorithm.PRIVATEOID
|
| 1246 |
+
|
| 1247 |
+
### END generated Algorithm constants
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/__init__.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Optional, Tuple, Type, Union
|
| 2 |
+
|
| 3 |
+
import dns.name
|
| 4 |
+
from dns.dnssecalgs.base import GenericPrivateKey
|
| 5 |
+
from dns.dnssectypes import Algorithm
|
| 6 |
+
from dns.exception import UnsupportedAlgorithm
|
| 7 |
+
from dns.rdtypes.ANY.DNSKEY import DNSKEY
|
| 8 |
+
|
| 9 |
+
if dns._features.have("dnssec"):
|
| 10 |
+
from dns.dnssecalgs.dsa import PrivateDSA, PrivateDSANSEC3SHA1
|
| 11 |
+
from dns.dnssecalgs.ecdsa import PrivateECDSAP256SHA256, PrivateECDSAP384SHA384
|
| 12 |
+
from dns.dnssecalgs.eddsa import PrivateED448, PrivateED25519
|
| 13 |
+
from dns.dnssecalgs.rsa import (
|
| 14 |
+
PrivateRSAMD5,
|
| 15 |
+
PrivateRSASHA1,
|
| 16 |
+
PrivateRSASHA1NSEC3SHA1,
|
| 17 |
+
PrivateRSASHA256,
|
| 18 |
+
PrivateRSASHA512,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
_have_cryptography = True
|
| 22 |
+
else:
|
| 23 |
+
_have_cryptography = False
|
| 24 |
+
|
| 25 |
+
AlgorithmPrefix = Optional[Union[bytes, dns.name.Name]]
|
| 26 |
+
|
| 27 |
+
algorithms: Dict[Tuple[Algorithm, AlgorithmPrefix], Type[GenericPrivateKey]] = {}
|
| 28 |
+
if _have_cryptography:
|
| 29 |
+
# pylint: disable=possibly-used-before-assignment
|
| 30 |
+
algorithms.update(
|
| 31 |
+
{
|
| 32 |
+
(Algorithm.RSAMD5, None): PrivateRSAMD5,
|
| 33 |
+
(Algorithm.DSA, None): PrivateDSA,
|
| 34 |
+
(Algorithm.RSASHA1, None): PrivateRSASHA1,
|
| 35 |
+
(Algorithm.DSANSEC3SHA1, None): PrivateDSANSEC3SHA1,
|
| 36 |
+
(Algorithm.RSASHA1NSEC3SHA1, None): PrivateRSASHA1NSEC3SHA1,
|
| 37 |
+
(Algorithm.RSASHA256, None): PrivateRSASHA256,
|
| 38 |
+
(Algorithm.RSASHA512, None): PrivateRSASHA512,
|
| 39 |
+
(Algorithm.ECDSAP256SHA256, None): PrivateECDSAP256SHA256,
|
| 40 |
+
(Algorithm.ECDSAP384SHA384, None): PrivateECDSAP384SHA384,
|
| 41 |
+
(Algorithm.ED25519, None): PrivateED25519,
|
| 42 |
+
(Algorithm.ED448, None): PrivateED448,
|
| 43 |
+
}
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def get_algorithm_cls(
|
| 48 |
+
algorithm: Union[int, str], prefix: AlgorithmPrefix = None
|
| 49 |
+
) -> Type[GenericPrivateKey]:
|
| 50 |
+
"""Get Private Key class from Algorithm.
|
| 51 |
+
|
| 52 |
+
*algorithm*, a ``str`` or ``int`` specifying the DNSKEY algorithm.
|
| 53 |
+
|
| 54 |
+
Raises ``UnsupportedAlgorithm`` if the algorithm is unknown.
|
| 55 |
+
|
| 56 |
+
Returns a ``dns.dnssecalgs.GenericPrivateKey``
|
| 57 |
+
"""
|
| 58 |
+
algorithm = Algorithm.make(algorithm)
|
| 59 |
+
cls = algorithms.get((algorithm, prefix))
|
| 60 |
+
if cls:
|
| 61 |
+
return cls
|
| 62 |
+
raise UnsupportedAlgorithm(
|
| 63 |
+
f'algorithm "{Algorithm.to_text(algorithm)}" not supported by dnspython'
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def get_algorithm_cls_from_dnskey(dnskey: DNSKEY) -> Type[GenericPrivateKey]:
|
| 68 |
+
"""Get Private Key class from DNSKEY.
|
| 69 |
+
|
| 70 |
+
*dnskey*, a ``DNSKEY`` to get Algorithm class for.
|
| 71 |
+
|
| 72 |
+
Raises ``UnsupportedAlgorithm`` if the algorithm is unknown.
|
| 73 |
+
|
| 74 |
+
Returns a ``dns.dnssecalgs.GenericPrivateKey``
|
| 75 |
+
"""
|
| 76 |
+
prefix: AlgorithmPrefix = None
|
| 77 |
+
if dnskey.algorithm == Algorithm.PRIVATEDNS:
|
| 78 |
+
prefix, _ = dns.name.from_wire(dnskey.key, 0)
|
| 79 |
+
elif dnskey.algorithm == Algorithm.PRIVATEOID:
|
| 80 |
+
length = int(dnskey.key[0])
|
| 81 |
+
prefix = dnskey.key[0 : length + 1]
|
| 82 |
+
return get_algorithm_cls(dnskey.algorithm, prefix)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def register_algorithm_cls(
|
| 86 |
+
algorithm: Union[int, str],
|
| 87 |
+
algorithm_cls: Type[GenericPrivateKey],
|
| 88 |
+
name: Optional[Union[dns.name.Name, str]] = None,
|
| 89 |
+
oid: Optional[bytes] = None,
|
| 90 |
+
) -> None:
|
| 91 |
+
"""Register Algorithm Private Key class.
|
| 92 |
+
|
| 93 |
+
*algorithm*, a ``str`` or ``int`` specifying the DNSKEY algorithm.
|
| 94 |
+
|
| 95 |
+
*algorithm_cls*: A `GenericPrivateKey` class.
|
| 96 |
+
|
| 97 |
+
*name*, an optional ``dns.name.Name`` or ``str``, for for PRIVATEDNS algorithms.
|
| 98 |
+
|
| 99 |
+
*oid*: an optional BER-encoded `bytes` for PRIVATEOID algorithms.
|
| 100 |
+
|
| 101 |
+
Raises ``ValueError`` if a name or oid is specified incorrectly.
|
| 102 |
+
"""
|
| 103 |
+
if not issubclass(algorithm_cls, GenericPrivateKey):
|
| 104 |
+
raise TypeError("Invalid algorithm class")
|
| 105 |
+
algorithm = Algorithm.make(algorithm)
|
| 106 |
+
prefix: AlgorithmPrefix = None
|
| 107 |
+
if algorithm == Algorithm.PRIVATEDNS:
|
| 108 |
+
if name is None:
|
| 109 |
+
raise ValueError("Name required for PRIVATEDNS algorithms")
|
| 110 |
+
if isinstance(name, str):
|
| 111 |
+
name = dns.name.from_text(name)
|
| 112 |
+
prefix = name
|
| 113 |
+
elif algorithm == Algorithm.PRIVATEOID:
|
| 114 |
+
if oid is None:
|
| 115 |
+
raise ValueError("OID required for PRIVATEOID algorithms")
|
| 116 |
+
prefix = bytes([len(oid)]) + oid
|
| 117 |
+
elif name:
|
| 118 |
+
raise ValueError("Name only supported for PRIVATEDNS algorithm")
|
| 119 |
+
elif oid:
|
| 120 |
+
raise ValueError("OID only supported for PRIVATEOID algorithm")
|
| 121 |
+
algorithms[(algorithm, prefix)] = algorithm_cls
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (3.73 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/base.cpython-310.pyc
ADDED
|
Binary file (3.76 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/cryptography.cpython-310.pyc
ADDED
|
Binary file (2.66 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/dsa.cpython-310.pyc
ADDED
|
Binary file (3.82 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/ecdsa.cpython-310.pyc
ADDED
|
Binary file (3.86 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/eddsa.cpython-310.pyc
ADDED
|
Binary file (2.98 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/__pycache__/rsa.cpython-310.pyc
ADDED
|
Binary file (4.7 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/base.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from abc import ABC, abstractmethod # pylint: disable=no-name-in-module
|
| 2 |
+
from typing import Any, Optional, Type
|
| 3 |
+
|
| 4 |
+
import dns.rdataclass
|
| 5 |
+
import dns.rdatatype
|
| 6 |
+
from dns.dnssectypes import Algorithm
|
| 7 |
+
from dns.exception import AlgorithmKeyMismatch
|
| 8 |
+
from dns.rdtypes.ANY.DNSKEY import DNSKEY
|
| 9 |
+
from dns.rdtypes.dnskeybase import Flag
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class GenericPublicKey(ABC):
|
| 13 |
+
algorithm: Algorithm
|
| 14 |
+
|
| 15 |
+
@abstractmethod
|
| 16 |
+
def __init__(self, key: Any) -> None:
|
| 17 |
+
pass
|
| 18 |
+
|
| 19 |
+
@abstractmethod
|
| 20 |
+
def verify(self, signature: bytes, data: bytes) -> None:
|
| 21 |
+
"""Verify signed DNSSEC data"""
|
| 22 |
+
|
| 23 |
+
@abstractmethod
|
| 24 |
+
def encode_key_bytes(self) -> bytes:
|
| 25 |
+
"""Encode key as bytes for DNSKEY"""
|
| 26 |
+
|
| 27 |
+
@classmethod
|
| 28 |
+
def _ensure_algorithm_key_combination(cls, key: DNSKEY) -> None:
|
| 29 |
+
if key.algorithm != cls.algorithm:
|
| 30 |
+
raise AlgorithmKeyMismatch
|
| 31 |
+
|
| 32 |
+
def to_dnskey(self, flags: int = Flag.ZONE, protocol: int = 3) -> DNSKEY:
|
| 33 |
+
"""Return public key as DNSKEY"""
|
| 34 |
+
return DNSKEY(
|
| 35 |
+
rdclass=dns.rdataclass.IN,
|
| 36 |
+
rdtype=dns.rdatatype.DNSKEY,
|
| 37 |
+
flags=flags,
|
| 38 |
+
protocol=protocol,
|
| 39 |
+
algorithm=self.algorithm,
|
| 40 |
+
key=self.encode_key_bytes(),
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
@classmethod
|
| 44 |
+
@abstractmethod
|
| 45 |
+
def from_dnskey(cls, key: DNSKEY) -> "GenericPublicKey":
|
| 46 |
+
"""Create public key from DNSKEY"""
|
| 47 |
+
|
| 48 |
+
@classmethod
|
| 49 |
+
@abstractmethod
|
| 50 |
+
def from_pem(cls, public_pem: bytes) -> "GenericPublicKey":
|
| 51 |
+
"""Create public key from PEM-encoded SubjectPublicKeyInfo as specified
|
| 52 |
+
in RFC 5280"""
|
| 53 |
+
|
| 54 |
+
@abstractmethod
|
| 55 |
+
def to_pem(self) -> bytes:
|
| 56 |
+
"""Return public-key as PEM-encoded SubjectPublicKeyInfo as specified
|
| 57 |
+
in RFC 5280"""
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
class GenericPrivateKey(ABC):
|
| 61 |
+
public_cls: Type[GenericPublicKey]
|
| 62 |
+
|
| 63 |
+
@abstractmethod
|
| 64 |
+
def __init__(self, key: Any) -> None:
|
| 65 |
+
pass
|
| 66 |
+
|
| 67 |
+
@abstractmethod
|
| 68 |
+
def sign(
|
| 69 |
+
self,
|
| 70 |
+
data: bytes,
|
| 71 |
+
verify: bool = False,
|
| 72 |
+
deterministic: bool = True,
|
| 73 |
+
) -> bytes:
|
| 74 |
+
"""Sign DNSSEC data"""
|
| 75 |
+
|
| 76 |
+
@abstractmethod
|
| 77 |
+
def public_key(self) -> "GenericPublicKey":
|
| 78 |
+
"""Return public key instance"""
|
| 79 |
+
|
| 80 |
+
@classmethod
|
| 81 |
+
@abstractmethod
|
| 82 |
+
def from_pem(
|
| 83 |
+
cls, private_pem: bytes, password: Optional[bytes] = None
|
| 84 |
+
) -> "GenericPrivateKey":
|
| 85 |
+
"""Create private key from PEM-encoded PKCS#8"""
|
| 86 |
+
|
| 87 |
+
@abstractmethod
|
| 88 |
+
def to_pem(self, password: Optional[bytes] = None) -> bytes:
|
| 89 |
+
"""Return private key as PEM-encoded PKCS#8"""
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/cryptography.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Optional, Type
|
| 2 |
+
|
| 3 |
+
from cryptography.hazmat.primitives import serialization
|
| 4 |
+
|
| 5 |
+
from dns.dnssecalgs.base import GenericPrivateKey, GenericPublicKey
|
| 6 |
+
from dns.exception import AlgorithmKeyMismatch
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class CryptographyPublicKey(GenericPublicKey):
|
| 10 |
+
key: Any = None
|
| 11 |
+
key_cls: Any = None
|
| 12 |
+
|
| 13 |
+
def __init__(self, key: Any) -> None: # pylint: disable=super-init-not-called
|
| 14 |
+
if self.key_cls is None:
|
| 15 |
+
raise TypeError("Undefined private key class")
|
| 16 |
+
if not isinstance( # pylint: disable=isinstance-second-argument-not-valid-type
|
| 17 |
+
key, self.key_cls
|
| 18 |
+
):
|
| 19 |
+
raise AlgorithmKeyMismatch
|
| 20 |
+
self.key = key
|
| 21 |
+
|
| 22 |
+
@classmethod
|
| 23 |
+
def from_pem(cls, public_pem: bytes) -> "GenericPublicKey":
|
| 24 |
+
key = serialization.load_pem_public_key(public_pem)
|
| 25 |
+
return cls(key=key)
|
| 26 |
+
|
| 27 |
+
def to_pem(self) -> bytes:
|
| 28 |
+
return self.key.public_bytes(
|
| 29 |
+
encoding=serialization.Encoding.PEM,
|
| 30 |
+
format=serialization.PublicFormat.SubjectPublicKeyInfo,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class CryptographyPrivateKey(GenericPrivateKey):
|
| 35 |
+
key: Any = None
|
| 36 |
+
key_cls: Any = None
|
| 37 |
+
public_cls: Type[CryptographyPublicKey]
|
| 38 |
+
|
| 39 |
+
def __init__(self, key: Any) -> None: # pylint: disable=super-init-not-called
|
| 40 |
+
if self.key_cls is None:
|
| 41 |
+
raise TypeError("Undefined private key class")
|
| 42 |
+
if not isinstance( # pylint: disable=isinstance-second-argument-not-valid-type
|
| 43 |
+
key, self.key_cls
|
| 44 |
+
):
|
| 45 |
+
raise AlgorithmKeyMismatch
|
| 46 |
+
self.key = key
|
| 47 |
+
|
| 48 |
+
def public_key(self) -> "CryptographyPublicKey":
|
| 49 |
+
return self.public_cls(key=self.key.public_key())
|
| 50 |
+
|
| 51 |
+
@classmethod
|
| 52 |
+
def from_pem(
|
| 53 |
+
cls, private_pem: bytes, password: Optional[bytes] = None
|
| 54 |
+
) -> "GenericPrivateKey":
|
| 55 |
+
key = serialization.load_pem_private_key(private_pem, password=password)
|
| 56 |
+
return cls(key=key)
|
| 57 |
+
|
| 58 |
+
def to_pem(self, password: Optional[bytes] = None) -> bytes:
|
| 59 |
+
encryption_algorithm: serialization.KeySerializationEncryption
|
| 60 |
+
if password:
|
| 61 |
+
encryption_algorithm = serialization.BestAvailableEncryption(password)
|
| 62 |
+
else:
|
| 63 |
+
encryption_algorithm = serialization.NoEncryption()
|
| 64 |
+
return self.key.private_bytes(
|
| 65 |
+
encoding=serialization.Encoding.PEM,
|
| 66 |
+
format=serialization.PrivateFormat.PKCS8,
|
| 67 |
+
encryption_algorithm=encryption_algorithm,
|
| 68 |
+
)
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/dsa.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import struct
|
| 2 |
+
|
| 3 |
+
from cryptography.hazmat.backends import default_backend
|
| 4 |
+
from cryptography.hazmat.primitives import hashes
|
| 5 |
+
from cryptography.hazmat.primitives.asymmetric import dsa, utils
|
| 6 |
+
|
| 7 |
+
from dns.dnssecalgs.cryptography import CryptographyPrivateKey, CryptographyPublicKey
|
| 8 |
+
from dns.dnssectypes import Algorithm
|
| 9 |
+
from dns.rdtypes.ANY.DNSKEY import DNSKEY
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class PublicDSA(CryptographyPublicKey):
|
| 13 |
+
key: dsa.DSAPublicKey
|
| 14 |
+
key_cls = dsa.DSAPublicKey
|
| 15 |
+
algorithm = Algorithm.DSA
|
| 16 |
+
chosen_hash = hashes.SHA1()
|
| 17 |
+
|
| 18 |
+
def verify(self, signature: bytes, data: bytes) -> None:
|
| 19 |
+
sig_r = signature[1:21]
|
| 20 |
+
sig_s = signature[21:]
|
| 21 |
+
sig = utils.encode_dss_signature(
|
| 22 |
+
int.from_bytes(sig_r, "big"), int.from_bytes(sig_s, "big")
|
| 23 |
+
)
|
| 24 |
+
self.key.verify(sig, data, self.chosen_hash)
|
| 25 |
+
|
| 26 |
+
def encode_key_bytes(self) -> bytes:
|
| 27 |
+
"""Encode a public key per RFC 2536, section 2."""
|
| 28 |
+
pn = self.key.public_numbers()
|
| 29 |
+
dsa_t = (self.key.key_size // 8 - 64) // 8
|
| 30 |
+
if dsa_t > 8:
|
| 31 |
+
raise ValueError("unsupported DSA key size")
|
| 32 |
+
octets = 64 + dsa_t * 8
|
| 33 |
+
res = struct.pack("!B", dsa_t)
|
| 34 |
+
res += pn.parameter_numbers.q.to_bytes(20, "big")
|
| 35 |
+
res += pn.parameter_numbers.p.to_bytes(octets, "big")
|
| 36 |
+
res += pn.parameter_numbers.g.to_bytes(octets, "big")
|
| 37 |
+
res += pn.y.to_bytes(octets, "big")
|
| 38 |
+
return res
|
| 39 |
+
|
| 40 |
+
@classmethod
|
| 41 |
+
def from_dnskey(cls, key: DNSKEY) -> "PublicDSA":
|
| 42 |
+
cls._ensure_algorithm_key_combination(key)
|
| 43 |
+
keyptr = key.key
|
| 44 |
+
(t,) = struct.unpack("!B", keyptr[0:1])
|
| 45 |
+
keyptr = keyptr[1:]
|
| 46 |
+
octets = 64 + t * 8
|
| 47 |
+
dsa_q = keyptr[0:20]
|
| 48 |
+
keyptr = keyptr[20:]
|
| 49 |
+
dsa_p = keyptr[0:octets]
|
| 50 |
+
keyptr = keyptr[octets:]
|
| 51 |
+
dsa_g = keyptr[0:octets]
|
| 52 |
+
keyptr = keyptr[octets:]
|
| 53 |
+
dsa_y = keyptr[0:octets]
|
| 54 |
+
return cls(
|
| 55 |
+
key=dsa.DSAPublicNumbers( # type: ignore
|
| 56 |
+
int.from_bytes(dsa_y, "big"),
|
| 57 |
+
dsa.DSAParameterNumbers(
|
| 58 |
+
int.from_bytes(dsa_p, "big"),
|
| 59 |
+
int.from_bytes(dsa_q, "big"),
|
| 60 |
+
int.from_bytes(dsa_g, "big"),
|
| 61 |
+
),
|
| 62 |
+
).public_key(default_backend()),
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class PrivateDSA(CryptographyPrivateKey):
|
| 67 |
+
key: dsa.DSAPrivateKey
|
| 68 |
+
key_cls = dsa.DSAPrivateKey
|
| 69 |
+
public_cls = PublicDSA
|
| 70 |
+
|
| 71 |
+
def sign(
|
| 72 |
+
self,
|
| 73 |
+
data: bytes,
|
| 74 |
+
verify: bool = False,
|
| 75 |
+
deterministic: bool = True,
|
| 76 |
+
) -> bytes:
|
| 77 |
+
"""Sign using a private key per RFC 2536, section 3."""
|
| 78 |
+
public_dsa_key = self.key.public_key()
|
| 79 |
+
if public_dsa_key.key_size > 1024:
|
| 80 |
+
raise ValueError("DSA key size overflow")
|
| 81 |
+
der_signature = self.key.sign(data, self.public_cls.chosen_hash)
|
| 82 |
+
dsa_r, dsa_s = utils.decode_dss_signature(der_signature)
|
| 83 |
+
dsa_t = (public_dsa_key.key_size // 8 - 64) // 8
|
| 84 |
+
octets = 20
|
| 85 |
+
signature = (
|
| 86 |
+
struct.pack("!B", dsa_t)
|
| 87 |
+
+ int.to_bytes(dsa_r, length=octets, byteorder="big")
|
| 88 |
+
+ int.to_bytes(dsa_s, length=octets, byteorder="big")
|
| 89 |
+
)
|
| 90 |
+
if verify:
|
| 91 |
+
self.public_key().verify(signature, data)
|
| 92 |
+
return signature
|
| 93 |
+
|
| 94 |
+
@classmethod
|
| 95 |
+
def generate(cls, key_size: int) -> "PrivateDSA":
|
| 96 |
+
return cls(
|
| 97 |
+
key=dsa.generate_private_key(key_size=key_size),
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
class PublicDSANSEC3SHA1(PublicDSA):
|
| 102 |
+
algorithm = Algorithm.DSANSEC3SHA1
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
class PrivateDSANSEC3SHA1(PrivateDSA):
|
| 106 |
+
public_cls = PublicDSANSEC3SHA1
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/ecdsa.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from cryptography.hazmat.backends import default_backend
|
| 2 |
+
from cryptography.hazmat.primitives import hashes
|
| 3 |
+
from cryptography.hazmat.primitives.asymmetric import ec, utils
|
| 4 |
+
|
| 5 |
+
from dns.dnssecalgs.cryptography import CryptographyPrivateKey, CryptographyPublicKey
|
| 6 |
+
from dns.dnssectypes import Algorithm
|
| 7 |
+
from dns.rdtypes.ANY.DNSKEY import DNSKEY
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class PublicECDSA(CryptographyPublicKey):
|
| 11 |
+
key: ec.EllipticCurvePublicKey
|
| 12 |
+
key_cls = ec.EllipticCurvePublicKey
|
| 13 |
+
algorithm: Algorithm
|
| 14 |
+
chosen_hash: hashes.HashAlgorithm
|
| 15 |
+
curve: ec.EllipticCurve
|
| 16 |
+
octets: int
|
| 17 |
+
|
| 18 |
+
def verify(self, signature: bytes, data: bytes) -> None:
|
| 19 |
+
sig_r = signature[0 : self.octets]
|
| 20 |
+
sig_s = signature[self.octets :]
|
| 21 |
+
sig = utils.encode_dss_signature(
|
| 22 |
+
int.from_bytes(sig_r, "big"), int.from_bytes(sig_s, "big")
|
| 23 |
+
)
|
| 24 |
+
self.key.verify(sig, data, ec.ECDSA(self.chosen_hash))
|
| 25 |
+
|
| 26 |
+
def encode_key_bytes(self) -> bytes:
|
| 27 |
+
"""Encode a public key per RFC 6605, section 4."""
|
| 28 |
+
pn = self.key.public_numbers()
|
| 29 |
+
return pn.x.to_bytes(self.octets, "big") + pn.y.to_bytes(self.octets, "big")
|
| 30 |
+
|
| 31 |
+
@classmethod
|
| 32 |
+
def from_dnskey(cls, key: DNSKEY) -> "PublicECDSA":
|
| 33 |
+
cls._ensure_algorithm_key_combination(key)
|
| 34 |
+
ecdsa_x = key.key[0 : cls.octets]
|
| 35 |
+
ecdsa_y = key.key[cls.octets : cls.octets * 2]
|
| 36 |
+
return cls(
|
| 37 |
+
key=ec.EllipticCurvePublicNumbers(
|
| 38 |
+
curve=cls.curve,
|
| 39 |
+
x=int.from_bytes(ecdsa_x, "big"),
|
| 40 |
+
y=int.from_bytes(ecdsa_y, "big"),
|
| 41 |
+
).public_key(default_backend()),
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class PrivateECDSA(CryptographyPrivateKey):
|
| 46 |
+
key: ec.EllipticCurvePrivateKey
|
| 47 |
+
key_cls = ec.EllipticCurvePrivateKey
|
| 48 |
+
public_cls = PublicECDSA
|
| 49 |
+
|
| 50 |
+
def sign(
|
| 51 |
+
self,
|
| 52 |
+
data: bytes,
|
| 53 |
+
verify: bool = False,
|
| 54 |
+
deterministic: bool = True,
|
| 55 |
+
) -> bytes:
|
| 56 |
+
"""Sign using a private key per RFC 6605, section 4."""
|
| 57 |
+
algorithm = ec.ECDSA(
|
| 58 |
+
self.public_cls.chosen_hash, deterministic_signing=deterministic
|
| 59 |
+
)
|
| 60 |
+
der_signature = self.key.sign(data, algorithm)
|
| 61 |
+
dsa_r, dsa_s = utils.decode_dss_signature(der_signature)
|
| 62 |
+
signature = int.to_bytes(
|
| 63 |
+
dsa_r, length=self.public_cls.octets, byteorder="big"
|
| 64 |
+
) + int.to_bytes(dsa_s, length=self.public_cls.octets, byteorder="big")
|
| 65 |
+
if verify:
|
| 66 |
+
self.public_key().verify(signature, data)
|
| 67 |
+
return signature
|
| 68 |
+
|
| 69 |
+
@classmethod
|
| 70 |
+
def generate(cls) -> "PrivateECDSA":
|
| 71 |
+
return cls(
|
| 72 |
+
key=ec.generate_private_key(
|
| 73 |
+
curve=cls.public_cls.curve, backend=default_backend()
|
| 74 |
+
),
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class PublicECDSAP256SHA256(PublicECDSA):
|
| 79 |
+
algorithm = Algorithm.ECDSAP256SHA256
|
| 80 |
+
chosen_hash = hashes.SHA256()
|
| 81 |
+
curve = ec.SECP256R1()
|
| 82 |
+
octets = 32
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
class PrivateECDSAP256SHA256(PrivateECDSA):
|
| 86 |
+
public_cls = PublicECDSAP256SHA256
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
class PublicECDSAP384SHA384(PublicECDSA):
|
| 90 |
+
algorithm = Algorithm.ECDSAP384SHA384
|
| 91 |
+
chosen_hash = hashes.SHA384()
|
| 92 |
+
curve = ec.SECP384R1()
|
| 93 |
+
octets = 48
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
class PrivateECDSAP384SHA384(PrivateECDSA):
|
| 97 |
+
public_cls = PublicECDSAP384SHA384
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/eddsa.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Type
|
| 2 |
+
|
| 3 |
+
from cryptography.hazmat.primitives import serialization
|
| 4 |
+
from cryptography.hazmat.primitives.asymmetric import ed448, ed25519
|
| 5 |
+
|
| 6 |
+
from dns.dnssecalgs.cryptography import CryptographyPrivateKey, CryptographyPublicKey
|
| 7 |
+
from dns.dnssectypes import Algorithm
|
| 8 |
+
from dns.rdtypes.ANY.DNSKEY import DNSKEY
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class PublicEDDSA(CryptographyPublicKey):
|
| 12 |
+
def verify(self, signature: bytes, data: bytes) -> None:
|
| 13 |
+
self.key.verify(signature, data)
|
| 14 |
+
|
| 15 |
+
def encode_key_bytes(self) -> bytes:
|
| 16 |
+
"""Encode a public key per RFC 8080, section 3."""
|
| 17 |
+
return self.key.public_bytes(
|
| 18 |
+
encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
@classmethod
|
| 22 |
+
def from_dnskey(cls, key: DNSKEY) -> "PublicEDDSA":
|
| 23 |
+
cls._ensure_algorithm_key_combination(key)
|
| 24 |
+
return cls(
|
| 25 |
+
key=cls.key_cls.from_public_bytes(key.key),
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class PrivateEDDSA(CryptographyPrivateKey):
|
| 30 |
+
public_cls: Type[PublicEDDSA]
|
| 31 |
+
|
| 32 |
+
def sign(
|
| 33 |
+
self,
|
| 34 |
+
data: bytes,
|
| 35 |
+
verify: bool = False,
|
| 36 |
+
deterministic: bool = True,
|
| 37 |
+
) -> bytes:
|
| 38 |
+
"""Sign using a private key per RFC 8080, section 4."""
|
| 39 |
+
signature = self.key.sign(data)
|
| 40 |
+
if verify:
|
| 41 |
+
self.public_key().verify(signature, data)
|
| 42 |
+
return signature
|
| 43 |
+
|
| 44 |
+
@classmethod
|
| 45 |
+
def generate(cls) -> "PrivateEDDSA":
|
| 46 |
+
return cls(key=cls.key_cls.generate())
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
class PublicED25519(PublicEDDSA):
|
| 50 |
+
key: ed25519.Ed25519PublicKey
|
| 51 |
+
key_cls = ed25519.Ed25519PublicKey
|
| 52 |
+
algorithm = Algorithm.ED25519
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class PrivateED25519(PrivateEDDSA):
|
| 56 |
+
key: ed25519.Ed25519PrivateKey
|
| 57 |
+
key_cls = ed25519.Ed25519PrivateKey
|
| 58 |
+
public_cls = PublicED25519
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
class PublicED448(PublicEDDSA):
|
| 62 |
+
key: ed448.Ed448PublicKey
|
| 63 |
+
key_cls = ed448.Ed448PublicKey
|
| 64 |
+
algorithm = Algorithm.ED448
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
class PrivateED448(PrivateEDDSA):
|
| 68 |
+
key: ed448.Ed448PrivateKey
|
| 69 |
+
key_cls = ed448.Ed448PrivateKey
|
| 70 |
+
public_cls = PublicED448
|
vllm/lib/python3.10/site-packages/dns/dnssecalgs/rsa.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(data, padding.PKCS1v15(), self.public_cls.chosen_hash)
|
| 67 |
+
if verify:
|
| 68 |
+
self.public_key().verify(signature, data)
|
| 69 |
+
return signature
|
| 70 |
+
|
| 71 |
+
@classmethod
|
| 72 |
+
def generate(cls, key_size: int) -> "PrivateRSA":
|
| 73 |
+
return cls(
|
| 74 |
+
key=rsa.generate_private_key(
|
| 75 |
+
public_exponent=cls.default_public_exponent,
|
| 76 |
+
key_size=key_size,
|
| 77 |
+
backend=default_backend(),
|
| 78 |
+
)
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class PublicRSAMD5(PublicRSA):
|
| 83 |
+
algorithm = Algorithm.RSAMD5
|
| 84 |
+
chosen_hash = hashes.MD5()
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
class PrivateRSAMD5(PrivateRSA):
|
| 88 |
+
public_cls = PublicRSAMD5
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
class PublicRSASHA1(PublicRSA):
|
| 92 |
+
algorithm = Algorithm.RSASHA1
|
| 93 |
+
chosen_hash = hashes.SHA1()
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
class PrivateRSASHA1(PrivateRSA):
|
| 97 |
+
public_cls = PublicRSASHA1
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
class PublicRSASHA1NSEC3SHA1(PublicRSA):
|
| 101 |
+
algorithm = Algorithm.RSASHA1NSEC3SHA1
|
| 102 |
+
chosen_hash = hashes.SHA1()
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
class PrivateRSASHA1NSEC3SHA1(PrivateRSA):
|
| 106 |
+
public_cls = PublicRSASHA1NSEC3SHA1
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
class PublicRSASHA256(PublicRSA):
|
| 110 |
+
algorithm = Algorithm.RSASHA256
|
| 111 |
+
chosen_hash = hashes.SHA256()
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
class PrivateRSASHA256(PrivateRSA):
|
| 115 |
+
public_cls = PublicRSASHA256
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
class PublicRSASHA512(PublicRSA):
|
| 119 |
+
algorithm = Algorithm.RSASHA512
|
| 120 |
+
chosen_hash = hashes.SHA512()
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
class PrivateRSASHA512(PrivateRSA):
|
| 124 |
+
public_cls = PublicRSASHA512
|
vllm/lib/python3.10/site-packages/dns/e164.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
|
| 2 |
+
|
| 3 |
+
# Copyright (C) 2006-2017 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 |
+
"""DNS E.164 helpers."""
|
| 19 |
+
|
| 20 |
+
from typing import Iterable, Optional, Union
|
| 21 |
+
|
| 22 |
+
import dns.exception
|
| 23 |
+
import dns.name
|
| 24 |
+
import dns.resolver
|
| 25 |
+
|
| 26 |
+
#: The public E.164 domain.
|
| 27 |
+
public_enum_domain = dns.name.from_text("e164.arpa.")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def from_e164(
|
| 31 |
+
text: str, origin: Optional[dns.name.Name] = public_enum_domain
|
| 32 |
+
) -> dns.name.Name:
|
| 33 |
+
"""Convert an E.164 number in textual form into a Name object whose
|
| 34 |
+
value is the ENUM domain name for that number.
|
| 35 |
+
|
| 36 |
+
Non-digits in the text are ignored, i.e. "16505551212",
|
| 37 |
+
"+1.650.555.1212" and "1 (650) 555-1212" are all the same.
|
| 38 |
+
|
| 39 |
+
*text*, a ``str``, is an E.164 number in textual form.
|
| 40 |
+
|
| 41 |
+
*origin*, a ``dns.name.Name``, the domain in which the number
|
| 42 |
+
should be constructed. The default is ``e164.arpa.``.
|
| 43 |
+
|
| 44 |
+
Returns a ``dns.name.Name``.
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
parts = [d for d in text if d.isdigit()]
|
| 48 |
+
parts.reverse()
|
| 49 |
+
return dns.name.from_text(".".join(parts), origin=origin)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def to_e164(
|
| 53 |
+
name: dns.name.Name,
|
| 54 |
+
origin: Optional[dns.name.Name] = public_enum_domain,
|
| 55 |
+
want_plus_prefix: bool = True,
|
| 56 |
+
) -> str:
|
| 57 |
+
"""Convert an ENUM domain name into an E.164 number.
|
| 58 |
+
|
| 59 |
+
Note that dnspython does not have any information about preferred
|
| 60 |
+
number formats within national numbering plans, so all numbers are
|
| 61 |
+
emitted as a simple string of digits, prefixed by a '+' (unless
|
| 62 |
+
*want_plus_prefix* is ``False``).
|
| 63 |
+
|
| 64 |
+
*name* is a ``dns.name.Name``, the ENUM domain name.
|
| 65 |
+
|
| 66 |
+
*origin* is a ``dns.name.Name``, a domain containing the ENUM
|
| 67 |
+
domain name. The name is relativized to this domain before being
|
| 68 |
+
converted to text. If ``None``, no relativization is done.
|
| 69 |
+
|
| 70 |
+
*want_plus_prefix* is a ``bool``. If True, add a '+' to the beginning of
|
| 71 |
+
the returned number.
|
| 72 |
+
|
| 73 |
+
Returns a ``str``.
|
| 74 |
+
|
| 75 |
+
"""
|
| 76 |
+
if origin is not None:
|
| 77 |
+
name = name.relativize(origin)
|
| 78 |
+
dlabels = [d for d in name.labels if d.isdigit() and len(d) == 1]
|
| 79 |
+
if len(dlabels) != len(name.labels):
|
| 80 |
+
raise dns.exception.SyntaxError("non-digit labels in ENUM domain name")
|
| 81 |
+
dlabels.reverse()
|
| 82 |
+
text = b"".join(dlabels)
|
| 83 |
+
if want_plus_prefix:
|
| 84 |
+
text = b"+" + text
|
| 85 |
+
return text.decode()
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def query(
|
| 89 |
+
number: str,
|
| 90 |
+
domains: Iterable[Union[dns.name.Name, str]],
|
| 91 |
+
resolver: Optional[dns.resolver.Resolver] = None,
|
| 92 |
+
) -> dns.resolver.Answer:
|
| 93 |
+
"""Look for NAPTR RRs for the specified number in the specified domains.
|
| 94 |
+
|
| 95 |
+
e.g. lookup('16505551212', ['e164.dnspython.org.', 'e164.arpa.'])
|
| 96 |
+
|
| 97 |
+
*number*, a ``str`` is the number to look for.
|
| 98 |
+
|
| 99 |
+
*domains* is an iterable containing ``dns.name.Name`` values.
|
| 100 |
+
|
| 101 |
+
*resolver*, a ``dns.resolver.Resolver``, is the resolver to use. If
|
| 102 |
+
``None``, the default resolver is used.
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
if resolver is None:
|
| 106 |
+
resolver = dns.resolver.get_default_resolver()
|
| 107 |
+
e_nx = dns.resolver.NXDOMAIN()
|
| 108 |
+
for domain in domains:
|
| 109 |
+
if isinstance(domain, str):
|
| 110 |
+
domain = dns.name.from_text(domain)
|
| 111 |
+
qname = dns.e164.from_e164(number, domain)
|
| 112 |
+
try:
|
| 113 |
+
return resolver.resolve(qname, "NAPTR")
|
| 114 |
+
except dns.resolver.NXDOMAIN as e:
|
| 115 |
+
e_nx += e
|
| 116 |
+
raise e_nx
|
vllm/lib/python3.10/site-packages/dns/edns.py
ADDED
|
@@ -0,0 +1,572 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
| 3 |
+
# Copyright (C) 2009-2017 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 |
+
"""EDNS Options"""
|
| 19 |
+
|
| 20 |
+
import binascii
|
| 21 |
+
import math
|
| 22 |
+
import socket
|
| 23 |
+
import struct
|
| 24 |
+
from typing import Any, Dict, Optional, Union
|
| 25 |
+
|
| 26 |
+
import dns.enum
|
| 27 |
+
import dns.inet
|
| 28 |
+
import dns.rdata
|
| 29 |
+
import dns.wire
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class OptionType(dns.enum.IntEnum):
|
| 33 |
+
#: NSID
|
| 34 |
+
NSID = 3
|
| 35 |
+
#: DAU
|
| 36 |
+
DAU = 5
|
| 37 |
+
#: DHU
|
| 38 |
+
DHU = 6
|
| 39 |
+
#: N3U
|
| 40 |
+
N3U = 7
|
| 41 |
+
#: ECS (client-subnet)
|
| 42 |
+
ECS = 8
|
| 43 |
+
#: EXPIRE
|
| 44 |
+
EXPIRE = 9
|
| 45 |
+
#: COOKIE
|
| 46 |
+
COOKIE = 10
|
| 47 |
+
#: KEEPALIVE
|
| 48 |
+
KEEPALIVE = 11
|
| 49 |
+
#: PADDING
|
| 50 |
+
PADDING = 12
|
| 51 |
+
#: CHAIN
|
| 52 |
+
CHAIN = 13
|
| 53 |
+
#: EDE (extended-dns-error)
|
| 54 |
+
EDE = 15
|
| 55 |
+
#: REPORTCHANNEL
|
| 56 |
+
REPORTCHANNEL = 18
|
| 57 |
+
|
| 58 |
+
@classmethod
|
| 59 |
+
def _maximum(cls):
|
| 60 |
+
return 65535
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class Option:
|
| 64 |
+
"""Base class for all EDNS option types."""
|
| 65 |
+
|
| 66 |
+
def __init__(self, otype: Union[OptionType, str]):
|
| 67 |
+
"""Initialize an option.
|
| 68 |
+
|
| 69 |
+
*otype*, a ``dns.edns.OptionType``, is the option type.
|
| 70 |
+
"""
|
| 71 |
+
self.otype = OptionType.make(otype)
|
| 72 |
+
|
| 73 |
+
def to_wire(self, file: Optional[Any] = None) -> Optional[bytes]:
|
| 74 |
+
"""Convert an option to wire format.
|
| 75 |
+
|
| 76 |
+
Returns a ``bytes`` or ``None``.
|
| 77 |
+
|
| 78 |
+
"""
|
| 79 |
+
raise NotImplementedError # pragma: no cover
|
| 80 |
+
|
| 81 |
+
def to_text(self) -> str:
|
| 82 |
+
raise NotImplementedError # pragma: no cover
|
| 83 |
+
|
| 84 |
+
@classmethod
|
| 85 |
+
def from_wire_parser(cls, otype: OptionType, parser: "dns.wire.Parser") -> "Option":
|
| 86 |
+
"""Build an EDNS option object from wire format.
|
| 87 |
+
|
| 88 |
+
*otype*, a ``dns.edns.OptionType``, is the option type.
|
| 89 |
+
|
| 90 |
+
*parser*, a ``dns.wire.Parser``, the parser, which should be
|
| 91 |
+
restructed to the option length.
|
| 92 |
+
|
| 93 |
+
Returns a ``dns.edns.Option``.
|
| 94 |
+
"""
|
| 95 |
+
raise NotImplementedError # pragma: no cover
|
| 96 |
+
|
| 97 |
+
def _cmp(self, other):
|
| 98 |
+
"""Compare an EDNS option with another option of the same type.
|
| 99 |
+
|
| 100 |
+
Returns < 0 if < *other*, 0 if == *other*, and > 0 if > *other*.
|
| 101 |
+
"""
|
| 102 |
+
wire = self.to_wire()
|
| 103 |
+
owire = other.to_wire()
|
| 104 |
+
if wire == owire:
|
| 105 |
+
return 0
|
| 106 |
+
if wire > owire:
|
| 107 |
+
return 1
|
| 108 |
+
return -1
|
| 109 |
+
|
| 110 |
+
def __eq__(self, other):
|
| 111 |
+
if not isinstance(other, Option):
|
| 112 |
+
return False
|
| 113 |
+
if self.otype != other.otype:
|
| 114 |
+
return False
|
| 115 |
+
return self._cmp(other) == 0
|
| 116 |
+
|
| 117 |
+
def __ne__(self, other):
|
| 118 |
+
if not isinstance(other, Option):
|
| 119 |
+
return True
|
| 120 |
+
if self.otype != other.otype:
|
| 121 |
+
return True
|
| 122 |
+
return self._cmp(other) != 0
|
| 123 |
+
|
| 124 |
+
def __lt__(self, other):
|
| 125 |
+
if not isinstance(other, Option) or self.otype != other.otype:
|
| 126 |
+
return NotImplemented
|
| 127 |
+
return self._cmp(other) < 0
|
| 128 |
+
|
| 129 |
+
def __le__(self, other):
|
| 130 |
+
if not isinstance(other, Option) or self.otype != other.otype:
|
| 131 |
+
return NotImplemented
|
| 132 |
+
return self._cmp(other) <= 0
|
| 133 |
+
|
| 134 |
+
def __ge__(self, other):
|
| 135 |
+
if not isinstance(other, Option) or self.otype != other.otype:
|
| 136 |
+
return NotImplemented
|
| 137 |
+
return self._cmp(other) >= 0
|
| 138 |
+
|
| 139 |
+
def __gt__(self, other):
|
| 140 |
+
if not isinstance(other, Option) or self.otype != other.otype:
|
| 141 |
+
return NotImplemented
|
| 142 |
+
return self._cmp(other) > 0
|
| 143 |
+
|
| 144 |
+
def __str__(self):
|
| 145 |
+
return self.to_text()
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
class GenericOption(Option): # lgtm[py/missing-equals]
|
| 149 |
+
"""Generic Option Class
|
| 150 |
+
|
| 151 |
+
This class is used for EDNS option types for which we have no better
|
| 152 |
+
implementation.
|
| 153 |
+
"""
|
| 154 |
+
|
| 155 |
+
def __init__(self, otype: Union[OptionType, str], data: Union[bytes, str]):
|
| 156 |
+
super().__init__(otype)
|
| 157 |
+
self.data = dns.rdata.Rdata._as_bytes(data, True)
|
| 158 |
+
|
| 159 |
+
def to_wire(self, file: Optional[Any] = None) -> Optional[bytes]:
|
| 160 |
+
if file:
|
| 161 |
+
file.write(self.data)
|
| 162 |
+
return None
|
| 163 |
+
else:
|
| 164 |
+
return self.data
|
| 165 |
+
|
| 166 |
+
def to_text(self) -> str:
|
| 167 |
+
return "Generic %d" % self.otype
|
| 168 |
+
|
| 169 |
+
@classmethod
|
| 170 |
+
def from_wire_parser(
|
| 171 |
+
cls, otype: Union[OptionType, str], parser: "dns.wire.Parser"
|
| 172 |
+
) -> Option:
|
| 173 |
+
return cls(otype, parser.get_remaining())
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
class ECSOption(Option): # lgtm[py/missing-equals]
|
| 177 |
+
"""EDNS Client Subnet (ECS, RFC7871)"""
|
| 178 |
+
|
| 179 |
+
def __init__(self, address: str, srclen: Optional[int] = None, scopelen: int = 0):
|
| 180 |
+
"""*address*, a ``str``, is the client address information.
|
| 181 |
+
|
| 182 |
+
*srclen*, an ``int``, the source prefix length, which is the
|
| 183 |
+
leftmost number of bits of the address to be used for the
|
| 184 |
+
lookup. The default is 24 for IPv4 and 56 for IPv6.
|
| 185 |
+
|
| 186 |
+
*scopelen*, an ``int``, the scope prefix length. This value
|
| 187 |
+
must be 0 in queries, and should be set in responses.
|
| 188 |
+
"""
|
| 189 |
+
|
| 190 |
+
super().__init__(OptionType.ECS)
|
| 191 |
+
af = dns.inet.af_for_address(address)
|
| 192 |
+
|
| 193 |
+
if af == socket.AF_INET6:
|
| 194 |
+
self.family = 2
|
| 195 |
+
if srclen is None:
|
| 196 |
+
srclen = 56
|
| 197 |
+
address = dns.rdata.Rdata._as_ipv6_address(address)
|
| 198 |
+
srclen = dns.rdata.Rdata._as_int(srclen, 0, 128)
|
| 199 |
+
scopelen = dns.rdata.Rdata._as_int(scopelen, 0, 128)
|
| 200 |
+
elif af == socket.AF_INET:
|
| 201 |
+
self.family = 1
|
| 202 |
+
if srclen is None:
|
| 203 |
+
srclen = 24
|
| 204 |
+
address = dns.rdata.Rdata._as_ipv4_address(address)
|
| 205 |
+
srclen = dns.rdata.Rdata._as_int(srclen, 0, 32)
|
| 206 |
+
scopelen = dns.rdata.Rdata._as_int(scopelen, 0, 32)
|
| 207 |
+
else: # pragma: no cover (this will never happen)
|
| 208 |
+
raise ValueError("Bad address family")
|
| 209 |
+
|
| 210 |
+
assert srclen is not None
|
| 211 |
+
self.address = address
|
| 212 |
+
self.srclen = srclen
|
| 213 |
+
self.scopelen = scopelen
|
| 214 |
+
|
| 215 |
+
addrdata = dns.inet.inet_pton(af, address)
|
| 216 |
+
nbytes = int(math.ceil(srclen / 8.0))
|
| 217 |
+
|
| 218 |
+
# Truncate to srclen and pad to the end of the last octet needed
|
| 219 |
+
# See RFC section 6
|
| 220 |
+
self.addrdata = addrdata[:nbytes]
|
| 221 |
+
nbits = srclen % 8
|
| 222 |
+
if nbits != 0:
|
| 223 |
+
last = struct.pack("B", ord(self.addrdata[-1:]) & (0xFF << (8 - nbits)))
|
| 224 |
+
self.addrdata = self.addrdata[:-1] + last
|
| 225 |
+
|
| 226 |
+
def to_text(self) -> str:
|
| 227 |
+
return f"ECS {self.address}/{self.srclen} scope/{self.scopelen}"
|
| 228 |
+
|
| 229 |
+
@staticmethod
|
| 230 |
+
def from_text(text: str) -> Option:
|
| 231 |
+
"""Convert a string into a `dns.edns.ECSOption`
|
| 232 |
+
|
| 233 |
+
*text*, a `str`, the text form of the option.
|
| 234 |
+
|
| 235 |
+
Returns a `dns.edns.ECSOption`.
|
| 236 |
+
|
| 237 |
+
Examples:
|
| 238 |
+
|
| 239 |
+
>>> import dns.edns
|
| 240 |
+
>>>
|
| 241 |
+
>>> # basic example
|
| 242 |
+
>>> dns.edns.ECSOption.from_text('1.2.3.4/24')
|
| 243 |
+
>>>
|
| 244 |
+
>>> # also understands scope
|
| 245 |
+
>>> dns.edns.ECSOption.from_text('1.2.3.4/24/32')
|
| 246 |
+
>>>
|
| 247 |
+
>>> # IPv6
|
| 248 |
+
>>> dns.edns.ECSOption.from_text('2001:4b98::1/64/64')
|
| 249 |
+
>>>
|
| 250 |
+
>>> # it understands results from `dns.edns.ECSOption.to_text()`
|
| 251 |
+
>>> dns.edns.ECSOption.from_text('ECS 1.2.3.4/24/32')
|
| 252 |
+
"""
|
| 253 |
+
optional_prefix = "ECS"
|
| 254 |
+
tokens = text.split()
|
| 255 |
+
ecs_text = None
|
| 256 |
+
if len(tokens) == 1:
|
| 257 |
+
ecs_text = tokens[0]
|
| 258 |
+
elif len(tokens) == 2:
|
| 259 |
+
if tokens[0] != optional_prefix:
|
| 260 |
+
raise ValueError(f'could not parse ECS from "{text}"')
|
| 261 |
+
ecs_text = tokens[1]
|
| 262 |
+
else:
|
| 263 |
+
raise ValueError(f'could not parse ECS from "{text}"')
|
| 264 |
+
n_slashes = ecs_text.count("/")
|
| 265 |
+
if n_slashes == 1:
|
| 266 |
+
address, tsrclen = ecs_text.split("/")
|
| 267 |
+
tscope = "0"
|
| 268 |
+
elif n_slashes == 2:
|
| 269 |
+
address, tsrclen, tscope = ecs_text.split("/")
|
| 270 |
+
else:
|
| 271 |
+
raise ValueError(f'could not parse ECS from "{text}"')
|
| 272 |
+
try:
|
| 273 |
+
scope = int(tscope)
|
| 274 |
+
except ValueError:
|
| 275 |
+
raise ValueError("invalid scope " + f'"{tscope}": scope must be an integer')
|
| 276 |
+
try:
|
| 277 |
+
srclen = int(tsrclen)
|
| 278 |
+
except ValueError:
|
| 279 |
+
raise ValueError(
|
| 280 |
+
"invalid srclen " + f'"{tsrclen}": srclen must be an integer'
|
| 281 |
+
)
|
| 282 |
+
return ECSOption(address, srclen, scope)
|
| 283 |
+
|
| 284 |
+
def to_wire(self, file: Optional[Any] = None) -> Optional[bytes]:
|
| 285 |
+
value = (
|
| 286 |
+
struct.pack("!HBB", self.family, self.srclen, self.scopelen) + self.addrdata
|
| 287 |
+
)
|
| 288 |
+
if file:
|
| 289 |
+
file.write(value)
|
| 290 |
+
return None
|
| 291 |
+
else:
|
| 292 |
+
return value
|
| 293 |
+
|
| 294 |
+
@classmethod
|
| 295 |
+
def from_wire_parser(
|
| 296 |
+
cls, otype: Union[OptionType, str], parser: "dns.wire.Parser"
|
| 297 |
+
) -> Option:
|
| 298 |
+
family, src, scope = parser.get_struct("!HBB")
|
| 299 |
+
addrlen = int(math.ceil(src / 8.0))
|
| 300 |
+
prefix = parser.get_bytes(addrlen)
|
| 301 |
+
if family == 1:
|
| 302 |
+
pad = 4 - addrlen
|
| 303 |
+
addr = dns.ipv4.inet_ntoa(prefix + b"\x00" * pad)
|
| 304 |
+
elif family == 2:
|
| 305 |
+
pad = 16 - addrlen
|
| 306 |
+
addr = dns.ipv6.inet_ntoa(prefix + b"\x00" * pad)
|
| 307 |
+
else:
|
| 308 |
+
raise ValueError("unsupported family")
|
| 309 |
+
|
| 310 |
+
return cls(addr, src, scope)
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
class EDECode(dns.enum.IntEnum):
|
| 314 |
+
OTHER = 0
|
| 315 |
+
UNSUPPORTED_DNSKEY_ALGORITHM = 1
|
| 316 |
+
UNSUPPORTED_DS_DIGEST_TYPE = 2
|
| 317 |
+
STALE_ANSWER = 3
|
| 318 |
+
FORGED_ANSWER = 4
|
| 319 |
+
DNSSEC_INDETERMINATE = 5
|
| 320 |
+
DNSSEC_BOGUS = 6
|
| 321 |
+
SIGNATURE_EXPIRED = 7
|
| 322 |
+
SIGNATURE_NOT_YET_VALID = 8
|
| 323 |
+
DNSKEY_MISSING = 9
|
| 324 |
+
RRSIGS_MISSING = 10
|
| 325 |
+
NO_ZONE_KEY_BIT_SET = 11
|
| 326 |
+
NSEC_MISSING = 12
|
| 327 |
+
CACHED_ERROR = 13
|
| 328 |
+
NOT_READY = 14
|
| 329 |
+
BLOCKED = 15
|
| 330 |
+
CENSORED = 16
|
| 331 |
+
FILTERED = 17
|
| 332 |
+
PROHIBITED = 18
|
| 333 |
+
STALE_NXDOMAIN_ANSWER = 19
|
| 334 |
+
NOT_AUTHORITATIVE = 20
|
| 335 |
+
NOT_SUPPORTED = 21
|
| 336 |
+
NO_REACHABLE_AUTHORITY = 22
|
| 337 |
+
NETWORK_ERROR = 23
|
| 338 |
+
INVALID_DATA = 24
|
| 339 |
+
|
| 340 |
+
@classmethod
|
| 341 |
+
def _maximum(cls):
|
| 342 |
+
return 65535
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
class EDEOption(Option): # lgtm[py/missing-equals]
|
| 346 |
+
"""Extended DNS Error (EDE, RFC8914)"""
|
| 347 |
+
|
| 348 |
+
_preserve_case = {"DNSKEY", "DS", "DNSSEC", "RRSIGs", "NSEC", "NXDOMAIN"}
|
| 349 |
+
|
| 350 |
+
def __init__(self, code: Union[EDECode, str], text: Optional[str] = None):
|
| 351 |
+
"""*code*, a ``dns.edns.EDECode`` or ``str``, the info code of the
|
| 352 |
+
extended error.
|
| 353 |
+
|
| 354 |
+
*text*, a ``str`` or ``None``, specifying additional information about
|
| 355 |
+
the error.
|
| 356 |
+
"""
|
| 357 |
+
|
| 358 |
+
super().__init__(OptionType.EDE)
|
| 359 |
+
|
| 360 |
+
self.code = EDECode.make(code)
|
| 361 |
+
if text is not None and not isinstance(text, str):
|
| 362 |
+
raise ValueError("text must be string or None")
|
| 363 |
+
self.text = text
|
| 364 |
+
|
| 365 |
+
def to_text(self) -> str:
|
| 366 |
+
output = f"EDE {self.code}"
|
| 367 |
+
if self.code in EDECode:
|
| 368 |
+
desc = EDECode.to_text(self.code)
|
| 369 |
+
desc = " ".join(
|
| 370 |
+
word if word in self._preserve_case else word.title()
|
| 371 |
+
for word in desc.split("_")
|
| 372 |
+
)
|
| 373 |
+
output += f" ({desc})"
|
| 374 |
+
if self.text is not None:
|
| 375 |
+
output += f": {self.text}"
|
| 376 |
+
return output
|
| 377 |
+
|
| 378 |
+
def to_wire(self, file: Optional[Any] = None) -> Optional[bytes]:
|
| 379 |
+
value = struct.pack("!H", self.code)
|
| 380 |
+
if self.text is not None:
|
| 381 |
+
value += self.text.encode("utf8")
|
| 382 |
+
|
| 383 |
+
if file:
|
| 384 |
+
file.write(value)
|
| 385 |
+
return None
|
| 386 |
+
else:
|
| 387 |
+
return value
|
| 388 |
+
|
| 389 |
+
@classmethod
|
| 390 |
+
def from_wire_parser(
|
| 391 |
+
cls, otype: Union[OptionType, str], parser: "dns.wire.Parser"
|
| 392 |
+
) -> Option:
|
| 393 |
+
code = EDECode.make(parser.get_uint16())
|
| 394 |
+
text = parser.get_remaining()
|
| 395 |
+
|
| 396 |
+
if text:
|
| 397 |
+
if text[-1] == 0: # text MAY be null-terminated
|
| 398 |
+
text = text[:-1]
|
| 399 |
+
btext = text.decode("utf8")
|
| 400 |
+
else:
|
| 401 |
+
btext = None
|
| 402 |
+
|
| 403 |
+
return cls(code, btext)
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
class NSIDOption(Option):
|
| 407 |
+
def __init__(self, nsid: bytes):
|
| 408 |
+
super().__init__(OptionType.NSID)
|
| 409 |
+
self.nsid = nsid
|
| 410 |
+
|
| 411 |
+
def to_wire(self, file: Any = None) -> Optional[bytes]:
|
| 412 |
+
if file:
|
| 413 |
+
file.write(self.nsid)
|
| 414 |
+
return None
|
| 415 |
+
else:
|
| 416 |
+
return self.nsid
|
| 417 |
+
|
| 418 |
+
def to_text(self) -> str:
|
| 419 |
+
if all(c >= 0x20 and c <= 0x7E for c in self.nsid):
|
| 420 |
+
# All ASCII printable, so it's probably a string.
|
| 421 |
+
value = self.nsid.decode()
|
| 422 |
+
else:
|
| 423 |
+
value = binascii.hexlify(self.nsid).decode()
|
| 424 |
+
return f"NSID {value}"
|
| 425 |
+
|
| 426 |
+
@classmethod
|
| 427 |
+
def from_wire_parser(
|
| 428 |
+
cls, otype: Union[OptionType, str], parser: dns.wire.Parser
|
| 429 |
+
) -> Option:
|
| 430 |
+
return cls(parser.get_remaining())
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
class CookieOption(Option):
|
| 434 |
+
def __init__(self, client: bytes, server: bytes):
|
| 435 |
+
super().__init__(dns.edns.OptionType.COOKIE)
|
| 436 |
+
self.client = client
|
| 437 |
+
self.server = server
|
| 438 |
+
if len(client) != 8:
|
| 439 |
+
raise ValueError("client cookie must be 8 bytes")
|
| 440 |
+
if len(server) != 0 and (len(server) < 8 or len(server) > 32):
|
| 441 |
+
raise ValueError("server cookie must be empty or between 8 and 32 bytes")
|
| 442 |
+
|
| 443 |
+
def to_wire(self, file: Any = None) -> Optional[bytes]:
|
| 444 |
+
if file:
|
| 445 |
+
file.write(self.client)
|
| 446 |
+
if len(self.server) > 0:
|
| 447 |
+
file.write(self.server)
|
| 448 |
+
return None
|
| 449 |
+
else:
|
| 450 |
+
return self.client + self.server
|
| 451 |
+
|
| 452 |
+
def to_text(self) -> str:
|
| 453 |
+
client = binascii.hexlify(self.client).decode()
|
| 454 |
+
if len(self.server) > 0:
|
| 455 |
+
server = binascii.hexlify(self.server).decode()
|
| 456 |
+
else:
|
| 457 |
+
server = ""
|
| 458 |
+
return f"COOKIE {client}{server}"
|
| 459 |
+
|
| 460 |
+
@classmethod
|
| 461 |
+
def from_wire_parser(
|
| 462 |
+
cls, otype: Union[OptionType, str], parser: dns.wire.Parser
|
| 463 |
+
) -> Option:
|
| 464 |
+
return cls(parser.get_bytes(8), parser.get_remaining())
|
| 465 |
+
|
| 466 |
+
|
| 467 |
+
class ReportChannelOption(Option):
|
| 468 |
+
# RFC 9567
|
| 469 |
+
def __init__(self, agent_domain: dns.name.Name):
|
| 470 |
+
super().__init__(OptionType.REPORTCHANNEL)
|
| 471 |
+
self.agent_domain = agent_domain
|
| 472 |
+
|
| 473 |
+
def to_wire(self, file: Any = None) -> Optional[bytes]:
|
| 474 |
+
return self.agent_domain.to_wire(file)
|
| 475 |
+
|
| 476 |
+
def to_text(self) -> str:
|
| 477 |
+
return "REPORTCHANNEL " + self.agent_domain.to_text()
|
| 478 |
+
|
| 479 |
+
@classmethod
|
| 480 |
+
def from_wire_parser(
|
| 481 |
+
cls, otype: Union[OptionType, str], parser: dns.wire.Parser
|
| 482 |
+
) -> Option:
|
| 483 |
+
return cls(parser.get_name())
|
| 484 |
+
|
| 485 |
+
|
| 486 |
+
_type_to_class: Dict[OptionType, Any] = {
|
| 487 |
+
OptionType.ECS: ECSOption,
|
| 488 |
+
OptionType.EDE: EDEOption,
|
| 489 |
+
OptionType.NSID: NSIDOption,
|
| 490 |
+
OptionType.COOKIE: CookieOption,
|
| 491 |
+
OptionType.REPORTCHANNEL: ReportChannelOption,
|
| 492 |
+
}
|
| 493 |
+
|
| 494 |
+
|
| 495 |
+
def get_option_class(otype: OptionType) -> Any:
|
| 496 |
+
"""Return the class for the specified option type.
|
| 497 |
+
|
| 498 |
+
The GenericOption class is used if a more specific class is not
|
| 499 |
+
known.
|
| 500 |
+
"""
|
| 501 |
+
|
| 502 |
+
cls = _type_to_class.get(otype)
|
| 503 |
+
if cls is None:
|
| 504 |
+
cls = GenericOption
|
| 505 |
+
return cls
|
| 506 |
+
|
| 507 |
+
|
| 508 |
+
def option_from_wire_parser(
|
| 509 |
+
otype: Union[OptionType, str], parser: "dns.wire.Parser"
|
| 510 |
+
) -> Option:
|
| 511 |
+
"""Build an EDNS option object from wire format.
|
| 512 |
+
|
| 513 |
+
*otype*, an ``int``, is the option type.
|
| 514 |
+
|
| 515 |
+
*parser*, a ``dns.wire.Parser``, the parser, which should be
|
| 516 |
+
restricted to the option length.
|
| 517 |
+
|
| 518 |
+
Returns an instance of a subclass of ``dns.edns.Option``.
|
| 519 |
+
"""
|
| 520 |
+
otype = OptionType.make(otype)
|
| 521 |
+
cls = get_option_class(otype)
|
| 522 |
+
return cls.from_wire_parser(otype, parser)
|
| 523 |
+
|
| 524 |
+
|
| 525 |
+
def option_from_wire(
|
| 526 |
+
otype: Union[OptionType, str], wire: bytes, current: int, olen: int
|
| 527 |
+
) -> Option:
|
| 528 |
+
"""Build an EDNS option object from wire format.
|
| 529 |
+
|
| 530 |
+
*otype*, an ``int``, is the option type.
|
| 531 |
+
|
| 532 |
+
*wire*, a ``bytes``, is the wire-format message.
|
| 533 |
+
|
| 534 |
+
*current*, an ``int``, is the offset in *wire* of the beginning
|
| 535 |
+
of the rdata.
|
| 536 |
+
|
| 537 |
+
*olen*, an ``int``, is the length of the wire-format option data
|
| 538 |
+
|
| 539 |
+
Returns an instance of a subclass of ``dns.edns.Option``.
|
| 540 |
+
"""
|
| 541 |
+
parser = dns.wire.Parser(wire, current)
|
| 542 |
+
with parser.restrict_to(olen):
|
| 543 |
+
return option_from_wire_parser(otype, parser)
|
| 544 |
+
|
| 545 |
+
|
| 546 |
+
def register_type(implementation: Any, otype: OptionType) -> None:
|
| 547 |
+
"""Register the implementation of an option type.
|
| 548 |
+
|
| 549 |
+
*implementation*, a ``class``, is a subclass of ``dns.edns.Option``.
|
| 550 |
+
|
| 551 |
+
*otype*, an ``int``, is the option type.
|
| 552 |
+
"""
|
| 553 |
+
|
| 554 |
+
_type_to_class[otype] = implementation
|
| 555 |
+
|
| 556 |
+
|
| 557 |
+
### BEGIN generated OptionType constants
|
| 558 |
+
|
| 559 |
+
NSID = OptionType.NSID
|
| 560 |
+
DAU = OptionType.DAU
|
| 561 |
+
DHU = OptionType.DHU
|
| 562 |
+
N3U = OptionType.N3U
|
| 563 |
+
ECS = OptionType.ECS
|
| 564 |
+
EXPIRE = OptionType.EXPIRE
|
| 565 |
+
COOKIE = OptionType.COOKIE
|
| 566 |
+
KEEPALIVE = OptionType.KEEPALIVE
|
| 567 |
+
PADDING = OptionType.PADDING
|
| 568 |
+
CHAIN = OptionType.CHAIN
|
| 569 |
+
EDE = OptionType.EDE
|
| 570 |
+
REPORTCHANNEL = OptionType.REPORTCHANNEL
|
| 571 |
+
|
| 572 |
+
### END generated OptionType constants
|
vllm/lib/python3.10/site-packages/dns/flags.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
|
| 2 |
+
|
| 3 |
+
# Copyright (C) 2001-2017 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 |
+
"""DNS Message Flags."""
|
| 19 |
+
|
| 20 |
+
import enum
|
| 21 |
+
from typing import Any
|
| 22 |
+
|
| 23 |
+
# Standard DNS flags
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class Flag(enum.IntFlag):
|
| 27 |
+
#: Query Response
|
| 28 |
+
QR = 0x8000
|
| 29 |
+
#: Authoritative Answer
|
| 30 |
+
AA = 0x0400
|
| 31 |
+
#: Truncated Response
|
| 32 |
+
TC = 0x0200
|
| 33 |
+
#: Recursion Desired
|
| 34 |
+
RD = 0x0100
|
| 35 |
+
#: Recursion Available
|
| 36 |
+
RA = 0x0080
|
| 37 |
+
#: Authentic Data
|
| 38 |
+
AD = 0x0020
|
| 39 |
+
#: Checking Disabled
|
| 40 |
+
CD = 0x0010
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# EDNS flags
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class EDNSFlag(enum.IntFlag):
|
| 47 |
+
#: DNSSEC answer OK
|
| 48 |
+
DO = 0x8000
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def _from_text(text: str, enum_class: Any) -> int:
|
| 52 |
+
flags = 0
|
| 53 |
+
tokens = text.split()
|
| 54 |
+
for t in tokens:
|
| 55 |
+
flags |= enum_class[t.upper()]
|
| 56 |
+
return flags
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def _to_text(flags: int, enum_class: Any) -> str:
|
| 60 |
+
text_flags = []
|
| 61 |
+
for k, v in enum_class.__members__.items():
|
| 62 |
+
if flags & v != 0:
|
| 63 |
+
text_flags.append(k)
|
| 64 |
+
return " ".join(text_flags)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def from_text(text: str) -> int:
|
| 68 |
+
"""Convert a space-separated list of flag text values into a flags
|
| 69 |
+
value.
|
| 70 |
+
|
| 71 |
+
Returns an ``int``
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
return _from_text(text, Flag)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def to_text(flags: int) -> str:
|
| 78 |
+
"""Convert a flags value into a space-separated list of flag text
|
| 79 |
+
values.
|
| 80 |
+
|
| 81 |
+
Returns a ``str``.
|
| 82 |
+
"""
|
| 83 |
+
|
| 84 |
+
return _to_text(flags, Flag)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def edns_from_text(text: str) -> int:
|
| 88 |
+
"""Convert a space-separated list of EDNS flag text values into a EDNS
|
| 89 |
+
flags value.
|
| 90 |
+
|
| 91 |
+
Returns an ``int``
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
+
return _from_text(text, EDNSFlag)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def edns_to_text(flags: int) -> str:
|
| 98 |
+
"""Convert an EDNS flags value into a space-separated list of EDNS flag
|
| 99 |
+
text values.
|
| 100 |
+
|
| 101 |
+
Returns a ``str``.
|
| 102 |
+
"""
|
| 103 |
+
|
| 104 |
+
return _to_text(flags, EDNSFlag)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
### BEGIN generated Flag constants
|
| 108 |
+
|
| 109 |
+
QR = Flag.QR
|
| 110 |
+
AA = Flag.AA
|
| 111 |
+
TC = Flag.TC
|
| 112 |
+
RD = Flag.RD
|
| 113 |
+
RA = Flag.RA
|
| 114 |
+
AD = Flag.AD
|
| 115 |
+
CD = Flag.CD
|
| 116 |
+
|
| 117 |
+
### END generated Flag constants
|
| 118 |
+
|
| 119 |
+
### BEGIN generated EDNSFlag constants
|
| 120 |
+
|
| 121 |
+
DO = EDNSFlag.DO
|
| 122 |
+
|
| 123 |
+
### END generated EDNSFlag constants
|
vllm/lib/python3.10/site-packages/dns/grange.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
|
| 2 |
+
|
| 3 |
+
# Copyright (C) 2012-2017 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 |
+
"""DNS GENERATE range conversion."""
|
| 19 |
+
|
| 20 |
+
from typing import Tuple
|
| 21 |
+
|
| 22 |
+
import dns
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def from_text(text: str) -> Tuple[int, int, int]:
|
| 26 |
+
"""Convert the text form of a range in a ``$GENERATE`` statement to an
|
| 27 |
+
integer.
|
| 28 |
+
|
| 29 |
+
*text*, a ``str``, the textual range in ``$GENERATE`` form.
|
| 30 |
+
|
| 31 |
+
Returns a tuple of three ``int`` values ``(start, stop, step)``.
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
start = -1
|
| 35 |
+
stop = -1
|
| 36 |
+
step = 1
|
| 37 |
+
cur = ""
|
| 38 |
+
state = 0
|
| 39 |
+
# state 0 1 2
|
| 40 |
+
# x - y / z
|
| 41 |
+
|
| 42 |
+
if text and text[0] == "-":
|
| 43 |
+
raise dns.exception.SyntaxError("Start cannot be a negative number")
|
| 44 |
+
|
| 45 |
+
for c in text:
|
| 46 |
+
if c == "-" and state == 0:
|
| 47 |
+
start = int(cur)
|
| 48 |
+
cur = ""
|
| 49 |
+
state = 1
|
| 50 |
+
elif c == "/":
|
| 51 |
+
stop = int(cur)
|
| 52 |
+
cur = ""
|
| 53 |
+
state = 2
|
| 54 |
+
elif c.isdigit():
|
| 55 |
+
cur += c
|
| 56 |
+
else:
|
| 57 |
+
raise dns.exception.SyntaxError(f"Could not parse {c}")
|
| 58 |
+
|
| 59 |
+
if state == 0:
|
| 60 |
+
raise dns.exception.SyntaxError("no stop value specified")
|
| 61 |
+
elif state == 1:
|
| 62 |
+
stop = int(cur)
|
| 63 |
+
else:
|
| 64 |
+
assert state == 2
|
| 65 |
+
step = int(cur)
|
| 66 |
+
|
| 67 |
+
assert step >= 1
|
| 68 |
+
assert start >= 0
|
| 69 |
+
if start > stop:
|
| 70 |
+
raise dns.exception.SyntaxError("start must be <= stop")
|
| 71 |
+
|
| 72 |
+
return (start, stop, step)
|
vllm/lib/python3.10/site-packages/dns/immutable.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
|
| 2 |
+
|
| 3 |
+
import collections.abc
|
| 4 |
+
from typing import Any, Callable
|
| 5 |
+
|
| 6 |
+
from dns._immutable_ctx import immutable
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@immutable
|
| 10 |
+
class Dict(collections.abc.Mapping): # lgtm[py/missing-equals]
|
| 11 |
+
def __init__(
|
| 12 |
+
self,
|
| 13 |
+
dictionary: Any,
|
| 14 |
+
no_copy: bool = False,
|
| 15 |
+
map_factory: Callable[[], collections.abc.MutableMapping] = dict,
|
| 16 |
+
):
|
| 17 |
+
"""Make an immutable dictionary from the specified dictionary.
|
| 18 |
+
|
| 19 |
+
If *no_copy* is `True`, then *dictionary* will be wrapped instead
|
| 20 |
+
of copied. Only set this if you are sure there will be no external
|
| 21 |
+
references to the dictionary.
|
| 22 |
+
"""
|
| 23 |
+
if no_copy and isinstance(dictionary, collections.abc.MutableMapping):
|
| 24 |
+
self._odict = dictionary
|
| 25 |
+
else:
|
| 26 |
+
self._odict = map_factory()
|
| 27 |
+
self._odict.update(dictionary)
|
| 28 |
+
self._hash = None
|
| 29 |
+
|
| 30 |
+
def __getitem__(self, key):
|
| 31 |
+
return self._odict.__getitem__(key)
|
| 32 |
+
|
| 33 |
+
def __hash__(self): # pylint: disable=invalid-hash-returned
|
| 34 |
+
if self._hash is None:
|
| 35 |
+
h = 0
|
| 36 |
+
for key in sorted(self._odict.keys()):
|
| 37 |
+
h ^= hash(key)
|
| 38 |
+
object.__setattr__(self, "_hash", h)
|
| 39 |
+
# this does return an int, but pylint doesn't figure that out
|
| 40 |
+
return self._hash
|
| 41 |
+
|
| 42 |
+
def __len__(self):
|
| 43 |
+
return len(self._odict)
|
| 44 |
+
|
| 45 |
+
def __iter__(self):
|
| 46 |
+
return iter(self._odict)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def constify(o: Any) -> Any:
|
| 50 |
+
"""
|
| 51 |
+
Convert mutable types to immutable types.
|
| 52 |
+
"""
|
| 53 |
+
if isinstance(o, bytearray):
|
| 54 |
+
return bytes(o)
|
| 55 |
+
if isinstance(o, tuple):
|
| 56 |
+
try:
|
| 57 |
+
hash(o)
|
| 58 |
+
return o
|
| 59 |
+
except Exception:
|
| 60 |
+
return tuple(constify(elt) for elt in o)
|
| 61 |
+
if isinstance(o, list):
|
| 62 |
+
return tuple(constify(elt) for elt in o)
|
| 63 |
+
if isinstance(o, dict):
|
| 64 |
+
cdict = dict()
|
| 65 |
+
for k, v in o.items():
|
| 66 |
+
cdict[k] = constify(v)
|
| 67 |
+
return Dict(cdict, True)
|
| 68 |
+
return o
|
vllm/lib/python3.10/site-packages/dns/inet.py
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
|
| 2 |
+
|
| 3 |
+
# Copyright (C) 2003-2017 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 |
+
"""Generic Internet address helper functions."""
|
| 19 |
+
|
| 20 |
+
import socket
|
| 21 |
+
from typing import Any, Optional, Tuple
|
| 22 |
+
|
| 23 |
+
import dns.ipv4
|
| 24 |
+
import dns.ipv6
|
| 25 |
+
|
| 26 |
+
# We assume that AF_INET and AF_INET6 are always defined. We keep
|
| 27 |
+
# these here for the benefit of any old code (unlikely though that
|
| 28 |
+
# is!).
|
| 29 |
+
AF_INET = socket.AF_INET
|
| 30 |
+
AF_INET6 = socket.AF_INET6
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def inet_pton(family: int, text: str) -> bytes:
|
| 34 |
+
"""Convert the textual form of a network address into its binary form.
|
| 35 |
+
|
| 36 |
+
*family* is an ``int``, the address family.
|
| 37 |
+
|
| 38 |
+
*text* is a ``str``, the textual address.
|
| 39 |
+
|
| 40 |
+
Raises ``NotImplementedError`` if the address family specified is not
|
| 41 |
+
implemented.
|
| 42 |
+
|
| 43 |
+
Returns a ``bytes``.
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
if family == AF_INET:
|
| 47 |
+
return dns.ipv4.inet_aton(text)
|
| 48 |
+
elif family == AF_INET6:
|
| 49 |
+
return dns.ipv6.inet_aton(text, True)
|
| 50 |
+
else:
|
| 51 |
+
raise NotImplementedError
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def inet_ntop(family: int, address: bytes) -> str:
|
| 55 |
+
"""Convert the binary form of a network address into its textual form.
|
| 56 |
+
|
| 57 |
+
*family* is an ``int``, the address family.
|
| 58 |
+
|
| 59 |
+
*address* is a ``bytes``, the network address in binary form.
|
| 60 |
+
|
| 61 |
+
Raises ``NotImplementedError`` if the address family specified is not
|
| 62 |
+
implemented.
|
| 63 |
+
|
| 64 |
+
Returns a ``str``.
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
if family == AF_INET:
|
| 68 |
+
return dns.ipv4.inet_ntoa(address)
|
| 69 |
+
elif family == AF_INET6:
|
| 70 |
+
return dns.ipv6.inet_ntoa(address)
|
| 71 |
+
else:
|
| 72 |
+
raise NotImplementedError
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def af_for_address(text: str) -> int:
|
| 76 |
+
"""Determine the address family of a textual-form network address.
|
| 77 |
+
|
| 78 |
+
*text*, a ``str``, the textual address.
|
| 79 |
+
|
| 80 |
+
Raises ``ValueError`` if the address family cannot be determined
|
| 81 |
+
from the input.
|
| 82 |
+
|
| 83 |
+
Returns an ``int``.
|
| 84 |
+
"""
|
| 85 |
+
|
| 86 |
+
try:
|
| 87 |
+
dns.ipv4.inet_aton(text)
|
| 88 |
+
return AF_INET
|
| 89 |
+
except Exception:
|
| 90 |
+
try:
|
| 91 |
+
dns.ipv6.inet_aton(text, True)
|
| 92 |
+
return AF_INET6
|
| 93 |
+
except Exception:
|
| 94 |
+
raise ValueError
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def is_multicast(text: str) -> bool:
|
| 98 |
+
"""Is the textual-form network address a multicast address?
|
| 99 |
+
|
| 100 |
+
*text*, a ``str``, the textual address.
|
| 101 |
+
|
| 102 |
+
Raises ``ValueError`` if the address family cannot be determined
|
| 103 |
+
from the input.
|
| 104 |
+
|
| 105 |
+
Returns a ``bool``.
|
| 106 |
+
"""
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
first = dns.ipv4.inet_aton(text)[0]
|
| 110 |
+
return first >= 224 and first <= 239
|
| 111 |
+
except Exception:
|
| 112 |
+
try:
|
| 113 |
+
first = dns.ipv6.inet_aton(text, True)[0]
|
| 114 |
+
return first == 255
|
| 115 |
+
except Exception:
|
| 116 |
+
raise ValueError
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def is_address(text: str) -> bool:
|
| 120 |
+
"""Is the specified string an IPv4 or IPv6 address?
|
| 121 |
+
|
| 122 |
+
*text*, a ``str``, the textual address.
|
| 123 |
+
|
| 124 |
+
Returns a ``bool``.
|
| 125 |
+
"""
|
| 126 |
+
|
| 127 |
+
try:
|
| 128 |
+
dns.ipv4.inet_aton(text)
|
| 129 |
+
return True
|
| 130 |
+
except Exception:
|
| 131 |
+
try:
|
| 132 |
+
dns.ipv6.inet_aton(text, True)
|
| 133 |
+
return True
|
| 134 |
+
except Exception:
|
| 135 |
+
return False
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def low_level_address_tuple(
|
| 139 |
+
high_tuple: Tuple[str, int], af: Optional[int] = None
|
| 140 |
+
) -> Any:
|
| 141 |
+
"""Given a "high-level" address tuple, i.e.
|
| 142 |
+
an (address, port) return the appropriate "low-level" address tuple
|
| 143 |
+
suitable for use in socket calls.
|
| 144 |
+
|
| 145 |
+
If an *af* other than ``None`` is provided, it is assumed the
|
| 146 |
+
address in the high-level tuple is valid and has that af. If af
|
| 147 |
+
is ``None``, then af_for_address will be called.
|
| 148 |
+
"""
|
| 149 |
+
address, port = high_tuple
|
| 150 |
+
if af is None:
|
| 151 |
+
af = af_for_address(address)
|
| 152 |
+
if af == AF_INET:
|
| 153 |
+
return (address, port)
|
| 154 |
+
elif af == AF_INET6:
|
| 155 |
+
i = address.find("%")
|
| 156 |
+
if i < 0:
|
| 157 |
+
# no scope, shortcut!
|
| 158 |
+
return (address, port, 0, 0)
|
| 159 |
+
# try to avoid getaddrinfo()
|
| 160 |
+
addrpart = address[:i]
|
| 161 |
+
scope = address[i + 1 :]
|
| 162 |
+
if scope.isdigit():
|
| 163 |
+
return (addrpart, port, 0, int(scope))
|
| 164 |
+
try:
|
| 165 |
+
return (addrpart, port, 0, socket.if_nametoindex(scope))
|
| 166 |
+
except AttributeError: # pragma: no cover (we can't really test this)
|
| 167 |
+
ai_flags = socket.AI_NUMERICHOST
|
| 168 |
+
((*_, tup), *_) = socket.getaddrinfo(address, port, flags=ai_flags)
|
| 169 |
+
return tup
|
| 170 |
+
else:
|
| 171 |
+
raise NotImplementedError(f"unknown address family {af}")
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def any_for_af(af):
|
| 175 |
+
"""Return the 'any' address for the specified address family."""
|
| 176 |
+
if af == socket.AF_INET:
|
| 177 |
+
return "0.0.0.0"
|
| 178 |
+
elif af == socket.AF_INET6:
|
| 179 |
+
return "::"
|
| 180 |
+
raise NotImplementedError(f"unknown address family {af}")
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
def canonicalize(text: str) -> str:
|
| 184 |
+
"""Verify that *address* is a valid text form IPv4 or IPv6 address and return its
|
| 185 |
+
canonical text form. IPv6 addresses with scopes are rejected.
|
| 186 |
+
|
| 187 |
+
*text*, a ``str``, the address in textual form.
|
| 188 |
+
|
| 189 |
+
Raises ``ValueError`` if the text is not valid.
|
| 190 |
+
"""
|
| 191 |
+
try:
|
| 192 |
+
return dns.ipv6.canonicalize(text)
|
| 193 |
+
except Exception:
|
| 194 |
+
try:
|
| 195 |
+
return dns.ipv4.canonicalize(text)
|
| 196 |
+
except Exception:
|
| 197 |
+
raise ValueError
|
vllm/lib/python3.10/site-packages/dns/ipv4.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
|
| 2 |
+
|
| 3 |
+
# Copyright (C) 2003-2017 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 |
+
"""IPv4 helper functions."""
|
| 19 |
+
|
| 20 |
+
import struct
|
| 21 |
+
from typing import Union
|
| 22 |
+
|
| 23 |
+
import dns.exception
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def inet_ntoa(address: bytes) -> str:
|
| 27 |
+
"""Convert an IPv4 address in binary form to text form.
|
| 28 |
+
|
| 29 |
+
*address*, a ``bytes``, the IPv4 address in binary form.
|
| 30 |
+
|
| 31 |
+
Returns a ``str``.
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
if len(address) != 4:
|
| 35 |
+
raise dns.exception.SyntaxError
|
| 36 |
+
return "%u.%u.%u.%u" % (address[0], address[1], address[2], address[3])
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def inet_aton(text: Union[str, bytes]) -> bytes:
|
| 40 |
+
"""Convert an IPv4 address in text form to binary form.
|
| 41 |
+
|
| 42 |
+
*text*, a ``str`` or ``bytes``, the IPv4 address in textual form.
|
| 43 |
+
|
| 44 |
+
Returns a ``bytes``.
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
if not isinstance(text, bytes):
|
| 48 |
+
btext = text.encode()
|
| 49 |
+
else:
|
| 50 |
+
btext = text
|
| 51 |
+
parts = btext.split(b".")
|
| 52 |
+
if len(parts) != 4:
|
| 53 |
+
raise dns.exception.SyntaxError
|
| 54 |
+
for part in parts:
|
| 55 |
+
if not part.isdigit():
|
| 56 |
+
raise dns.exception.SyntaxError
|
| 57 |
+
if len(part) > 1 and part[0] == ord("0"):
|
| 58 |
+
# No leading zeros
|
| 59 |
+
raise dns.exception.SyntaxError
|
| 60 |
+
try:
|
| 61 |
+
b = [int(part) for part in parts]
|
| 62 |
+
return struct.pack("BBBB", *b)
|
| 63 |
+
except Exception:
|
| 64 |
+
raise dns.exception.SyntaxError
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def canonicalize(text: Union[str, bytes]) -> str:
|
| 68 |
+
"""Verify that *address* is a valid text form IPv4 address and return its
|
| 69 |
+
canonical text form.
|
| 70 |
+
|
| 71 |
+
*text*, a ``str`` or ``bytes``, the IPv4 address in textual form.
|
| 72 |
+
|
| 73 |
+
Raises ``dns.exception.SyntaxError`` if the text is not valid.
|
| 74 |
+
"""
|
| 75 |
+
# Note that inet_aton() only accepts canonial form, but we still run through
|
| 76 |
+
# inet_ntoa() to ensure the output is a str.
|
| 77 |
+
return dns.ipv4.inet_ntoa(dns.ipv4.inet_aton(text))
|