Skip to content

Commit d638200

Browse files
authored
Initial IP address support: use san.SAN types internally (#10468)
In #10478 we added a `san.SAN` class, with two subclasses `san.DNSName` and `san.IPAddress`, so we can carry type information about identifiers through the Certbot code. This PR plumbs through those types in most Certbot-internal code. Note that this does not change the `acme` module, which uses `messages.Identifier`. It also tries to leave alone the code paths into plugins. This does not add a CLI flag to request an IP address certificate. That will be in a followup PR. Part of #10346
1 parent ada2c54 commit d638200

19 files changed

Lines changed: 430 additions & 350 deletions

‎certbot/src/certbot/_internal/cert_manager.py‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from certbot import ocsp
1717
from certbot import util
1818
from certbot._internal import storage
19+
from certbot._internal import san
1920
from certbot.compat import os
2021
from certbot.display import util as display_util
2122

@@ -93,16 +94,16 @@ def lineage_for_certname(cli_config: configuration.NamespaceConfig,
9394
return None
9495

9596

96-
def domains_for_certname(config: configuration.NamespaceConfig,
97-
certname: str) -> Optional[list[str]]:
98-
"""Find the domains in the cert with name certname."""
97+
def sans_for_certname(config: configuration.NamespaceConfig,
98+
certname: str) -> Optional[list[san.SAN]]:
99+
"""Find the domains and/or IP addresses in the cert with name certname."""
99100
lineage = lineage_for_certname(config, certname)
100-
return lineage.names() if lineage else None
101+
return lineage.sans() if lineage else None
101102

102103

103104
def find_duplicative_certs(config: configuration.NamespaceConfig,
104-
domains: list[str]) -> tuple[Optional[storage.RenewableCert],
105-
Optional[storage.RenewableCert]]:
105+
sans: list[san.SAN]) -> tuple[Optional[storage.RenewableCert],
106+
Optional[storage.RenewableCert]]:
106107
"""Find existing certs that match the given domain names.
107108
108109
This function searches for certificates whose domains are equal to
@@ -136,15 +137,15 @@ def update_certs_for_domain_matches(candidate_lineage: storage.RenewableCert,
136137
# TODO: Handle these differently depending on whether they are
137138
# expired or still valid?
138139
identical_names_cert, subset_names_cert = rv
139-
candidate_names = set(candidate_lineage.names())
140-
if candidate_names == set(domains):
140+
candidate_names = set(candidate_lineage.sans())
141+
if candidate_names == set(sans):
141142
identical_names_cert = candidate_lineage
142-
elif candidate_names.issubset(set(domains)):
143+
elif candidate_names.issubset(set(sans)):
143144
# This logic finds and returns the largest subset-names cert
144145
# in the case where there are several available.
145146
if subset_names_cert is None:
146147
subset_names_cert = candidate_lineage
147-
elif len(candidate_names) > len(subset_names_cert.names()):
148+
elif len(candidate_names) > len(subset_names_cert.sans()):
148149
subset_names_cert = candidate_lineage
149150
return (identical_names_cert, subset_names_cert)
150151

@@ -251,9 +252,11 @@ def human_readable_cert_info(config: configuration.NamespaceConfig, cert: storag
251252
certinfo = []
252253
checker = ocsp.RevocationChecker()
253254

255+
config_sans = set(config.domains)
256+
254257
if config.certname and cert.lineagename != config.certname and not skip_filter_checks:
255258
return None
256-
if config.domains and not set(config.domains).issubset(cert.names()):
259+
if config_sans and not config_sans.issubset(cert.sans()):
257260
return None
258261
now = datetime.datetime.now(datetime.timezone.utc)
259262

@@ -281,7 +284,7 @@ def human_readable_cert_info(config: configuration.NamespaceConfig, cert: storag
281284
certinfo.append(f" Certificate Name: {cert.lineagename}\n"
282285
f" Serial Number: {serial}\n"
283286
f" Key Type: {cert.private_key_type}\n"
284-
f' Domains: {" ".join(cert.names())}\n'
287+
f' Identifiers: {" ".join(map(str, cert.sans()))}\n'
285288
f" Expiry Date: {valid_string}\n"
286289
f" Certificate Path: {cert.fullchain}\n"
287290
f" Private Key Path: {cert.privkey}")

‎certbot/src/certbot/_internal/cli/cli_utils.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from acme import challenges
1414
from certbot import configuration
1515
from certbot import errors
16-
from certbot import util
1716
from certbot._internal import constants
17+
from certbot._internal import san
1818
from certbot.compat import os
1919

2020
if TYPE_CHECKING:
@@ -103,7 +103,7 @@ def __call__(self, parser: argparse.ArgumentParser, namespace: argparse.Namespac
103103

104104

105105
def add_domains(args_or_config: Union[argparse.Namespace, configuration.NamespaceConfig],
106-
domains: Optional[str]) -> list[str]:
106+
domains: Optional[str]) -> list[san.DNSName]:
107107
"""Registers new domains to be used during the current client run.
108108
109109
Domains are not added to the list of requested domains if they have
@@ -118,12 +118,12 @@ def add_domains(args_or_config: Union[argparse.Namespace, configuration.Namespac
118118
:rtype: `list` of `str`
119119
120120
"""
121-
validated_domains: list[str] = []
121+
validated_domains: list[san.DNSName] = []
122122
if not domains:
123123
return validated_domains
124124

125-
for domain in domains.split(","):
126-
domain = util.enforce_domain_sanity(domain.strip())
125+
for d in domains.split(","):
126+
domain = san.DNSName(d.strip())
127127
validated_domains.append(domain)
128128
if domain not in args_or_config.domains:
129129
args_or_config.domains.append(domain)

‎certbot/src/certbot/_internal/cli/helpful.py‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
from typing import Union
1010

1111
import configargparse
12+
from cryptography import x509
1213

1314
from certbot import crypto_util
1415
from certbot import errors
1516
from certbot import util
1617
from certbot._internal import constants
1718
from certbot._internal import hooks
19+
from certbot._internal import san
1820
from certbot._internal.cli.cli_constants import COMMAND_OVERVIEW
1921
from certbot._internal.cli.cli_constants import HELP_AND_VERSION_USAGE
2022
from certbot._internal.cli.cli_constants import SHORT_USAGE
21-
from certbot._internal.cli.cli_utils import add_domains
2223
from certbot._internal.cli.cli_utils import CustomHelpFormatter
2324
from certbot._internal.cli.cli_utils import flag_default
2425
from certbot._internal.cli.cli_utils import HelpfulArgumentGroup
@@ -298,7 +299,7 @@ def parse_args(self) -> NamespaceConfig:
298299
hooks.validate_hooks(config)
299300

300301
if config.allow_subset_of_names:
301-
if any(util.is_wildcard_domain(d) for d in config.domains):
302+
if any(d.is_wildcard() for d in config.domains):
302303
raise errors.Error("Using --allow-subset-of-names with a"
303304
" wildcard domain is not supported.")
304305

@@ -329,27 +330,29 @@ def handle_csr(self, config: NamespaceConfig) -> None:
329330
raise errors.Error("--allow-subset-of-names cannot be used with --csr")
330331

331332
csrfile, contents = config.csr[0:2]
332-
typ, csr, domains = crypto_util.import_csr_file(csrfile, contents)
333+
typ, util_csr, _ = crypto_util.import_csr_file(csrfile, contents)
334+
x509_req = x509.load_pem_x509_csr(util_csr.data)
335+
domains, _ = san.from_x509(x509_req.subject, x509_req.extensions)
333336

334-
# This is not necessary for webroot to work, however,
335-
# obtain_certificate_from_csr requires config.domains to be set
336-
for domain in domains:
337-
add_domains(config, domain)
337+
# The SANs from the CSR are added to the domains from command line flags as this config
338+
# setting is where main.certonly gets the list of identifiers to request.
339+
config.domains.extend(domains)
338340

339341
if not domains:
340342
# TODO: add CN to domains instead:
341343
raise errors.Error(
342344
"Unfortunately, your CSR %s needs to have a SubjectAltName for every domain"
343345
% config.csr[0])
344346

345-
config.actual_csr = (csr, typ)
347+
config.actual_csr = (util_csr, typ)
346348

347-
csr_domains = {d.lower() for d in domains}
348-
config_domains = set(config.domains)
349-
if csr_domains != config_domains:
349+
# Check that the original values for --domain set by the user were
350+
# a subset of the domains listed in the CSR.
351+
if set(config.domains) != set(domains):
350352
raise errors.ConfigurationError(
351-
"Inconsistent domain requests:\nFrom the CSR: {0}\nFrom command line/config: {1}"
352-
.format(", ".join(csr_domains), ", ".join(config_domains)))
353+
"Inconsistent requests:\nFrom the CSR: {0}\nFrom command line/config: {1}"
354+
.format(san.display(domains),
355+
san.display(config.domains)))
353356

354357

355358
def determine_verb(self) -> None:

0 commit comments

Comments
 (0)