Skip to content

Commit

Permalink
QUIC code should process verify correctly when given a directory (#1179)
Browse files Browse the repository at this point in the history
path. [#1174]
  • Loading branch information
rthalley authored Jan 29, 2025
1 parent a2b81a2 commit d088220
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
19 changes: 19 additions & 0 deletions dns/_tls_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license

import os
from typing import Optional, Tuple, Union


def convert_verify_to_cafile_and_capath(
verify: Union[bool, str],
) -> Tuple[Optional[str], Optional[str]]:
cafile: Optional[str] = None
capath: Optional[str] = None
if isinstance(verify, str):
if os.path.isfile(verify):
cafile = verify
elif os.path.isdir(verify):
capath = verify
else:
raise ValueError("invalid verify string")
return cafile, capath
11 changes: 2 additions & 9 deletions dns/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from typing import Any, Dict, Optional, Tuple, Union, cast

import dns._features
import dns._tls_util
import dns.exception
import dns.inet
import dns.message
Expand Down Expand Up @@ -1213,15 +1214,7 @@ def _tls_handshake(s, expiration):
def _make_dot_ssl_context(
server_hostname: Optional[str], verify: Union[bool, str]
) -> ssl.SSLContext:
cafile: Optional[str] = None
capath: Optional[str] = None
if isinstance(verify, str):
if os.path.isfile(verify):
cafile = verify
elif os.path.isdir(verify):
capath = verify
else:
raise ValueError("invalid verify string")
cafile, capath = dns._tls_util.convert_verify_to_cafile_and_capath(verify)
ssl_context = ssl.create_default_context(cafile=cafile, capath=capath)
ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2
if server_hostname is None:
Expand Down
6 changes: 5 additions & 1 deletion dns/quic/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import aioquic.quic.configuration # type: ignore
import aioquic.quic.connection # type: ignore

import dns._tls_util
import dns.inet

QUIC_MAX_DATAGRAM = 2048
Expand Down Expand Up @@ -245,7 +246,10 @@ def __init__(
server_name=server_name,
)
if verify_path is not None:
conf.load_verify_locations(verify_path)
cafile, capath = dns._tls_util.convert_verify_to_cafile_and_capath(
verify_path
)
conf.load_verify_locations(cafile=cafile, capath=capath)
self._conf = conf

def _connect(
Expand Down

0 comments on commit d088220

Please sign in to comment.