1# -*- coding: utf-8 -*-
2"""Validation utilities for acme2certifier"""
3
4import re
5import logging
6import ipaddress
7from email.utils import parseaddr
8from typing import List, Dict, Tuple, Optional
9import idna
10
11
12def dkeys_lower(tree: Dict[str, str]) -> Dict[str, str]:
13 """lower characters in payload string"""
14 if isinstance(tree, dict):
15 result = {k.lower(): dkeys_lower(v) for k, v in tree.items()}
16 elif isinstance(tree, list):
17 result = [dkeys_lower(ele) for ele in tree]
18 else:
19 result = tree
20 return result
21
22
23def fqdn_in_san_check(logger: logging.Logger, san_list: List[str], fqdn: str) -> bool:
24 """check if fqdn is in a list of sans"""
25 logger.debug("Helper.fqdn_in_san_check([%s], %s)", san_list, fqdn)
26
27 result = False
28 if fqdn and san_list:
29 for san in san_list:
30 try:
31 _type, value = san.lower().split(":", 1)
32 if fqdn == value:
33 result = True
34 break
35 except Exception:
36 logger.error("Error during SAN check. SAN split failed: %s", san)
37
38 logger.debug("Helper.fqdn_in_san_check() ended with: %s", result)
39 return result
40
41
42def validate_csr(logger: logging.Logger, order_dic: Dict[str, str], _csr) -> bool:
43 """validate certificate signing request against order"""
44 logger.debug("Helper.validate_csr(%s)", order_dic)
45 return True
46
47
48def normalize_email_address(logger: logging.Logger, raw: str) -> Optional[str]:
49 """Parse and normalize an email address for identity comparison.
50
51 Returns a canonical form with a lowercased local part and an A-label domain,
52 or None if the address cannot be parsed or the domain is not valid IDNA.
53 """
54 logger.debug("Helper.normalize_email_address()")
55 if not raw or not isinstance(raw, str):
56 return None
57
58 _display, addr = parseaddr(raw.strip())
59 if not addr or "@" not in addr:
60 logger.debug("Helper.normalize_email_address(): unparseable From header")
61 return None
62
63 local, _, domain = addr.rpartition("@")
64 local = local.strip()
65 domain = domain.strip()
66 if not local or not domain:
67 return None
68
69 try:
70 domain_ascii = idna.encode(domain).decode("ascii").lower()
71 except idna.IDNAError as err:
72 logger.debug(
73 "Helper.normalize_email_address(): invalid domain %s: %s", domain, err
74 )
75 return None
76
77 result = f"{local.lower()}@{domain_ascii}"
78 logger.debug("Helper.normalize_email_address() ended with: %s", result)
79 return result
80
81
82def validate_email(logger: logging.Logger, contact_list: List[str]) -> bool:
83 """validate contact against RFC608"""
84 logger.debug("Helper.validate_email()")
85 result = True
86 pattern = (
87 r"^[A-Za-z0-9.+_-]+@[A-Za-z][A-Za-z0-9._-]+[A-Za-z0-9]"
88 r"\.[A-Za-z]+(?:\.[A-Za-z]+)*$"
89 )
90 # check if we got a list or single address
91 if isinstance(contact_list, list):
92 for contact in contact_list:
93 contact = contact.replace("mailto:", "")
94 contact = contact.lstrip()
95 tmp_result = bool(re.search(pattern, contact))
96 logger.debug("# validate: %s result: %s", contact, tmp_result)
97 if not tmp_result:
98 result = tmp_result
99 else:
100 logger.debug("Helper.validate_email(): %s is not a list", contact_list)
101 contact_list = contact_list.replace("mailto:", "")
102 contact_list = contact_list.lstrip()
103 result = bool(re.search(pattern, contact_list))
104 logger.debug(
105 "Helper.validate_email() of: %s ended with result: %s", contact_list, result
106 )
107 return result
108
109
110def validate_identifier(
111 logger: logging.Logger,
112 id_type: str,
113 identifier: str,
114 tnauthlist_support: bool = False,
115) -> bool:
116 """validate identifier format"""
117 logger.debug("Helper.validate_identifier()")
118
119 result = False
120 if identifier:
121 if id_type == "dns":
122 result = validate_fqdn(logger, identifier)
123 elif id_type == "ip":
124 result = validate_ip(logger, identifier)
125 elif id_type == "email":
126 result = validate_email(logger, [identifier])
127 elif id_type == "tnauthlist" and tnauthlist_support:
128 result = True
129
130 logger.debug("Helper.validate_identifier() ended with: %s", result)
131 return result
132
133
134def validate_ip(logger: logging.Logger, ip: str, raw=False) -> ipaddress._BaseAddress:
135 """validate ip address"""
136 logger.debug("Helper.validate_ip()")
137 if raw:
138 return ipaddress.ip_address(ip)
139 result = False
140 try:
141 ipaddress.ip_address(ip)
142 result = True
143 except ValueError:
144 result = False
145 logger.debug("Helper.validate_ip() ended with: %s", result)
146 return result
147
148
149def _dns_label_valid(label: str) -> bool:
150 """Return True if label is a lowercase LDH DNS label (1..63 octets)."""
151 if not label or len(label) > 63:
152 return False
153 if label[0] == "-" or label[-1] == "-":
154 return False
155 return all(
156 ("a" <= char <= "z") or ("0" <= char <= "9") or char == "-" for char in label
157 )
158
159
160def _dns_name_valid(name: str) -> bool:
161 """Return True if name is a DNS hostname (no wildcard), length-capped."""
162 if not name or len(name) > 253:
163 return False
164 return all(_dns_label_valid(label) for label in name.split("."))
165
166
167def validate_fqdn(logger: logging.Logger, fqdn: str) -> bool:
168 """validate fqdn"""
169 logger.debug("Helper.validate_fqdn()")
170
171 result = False
172 if fqdn:
173 # Optional trailing root dot; reject before any expensive parse.
174 name = fqdn[:-1] if fqdn.endswith(".") else fqdn
175 if name.startswith("*."):
176 logger.debug(
177 "Helper.validate_fqdn(): invalid fqdn. Check for wildcard : %s", fqdn
178 )
179 # Trailing dot is not accepted on wildcards (previous behavior).
180 if not fqdn.endswith("."):
181 result = _dns_name_valid(name[2:])
182 else:
183 result = _dns_name_valid(name)
184
185 logger.debug("Helper.validate_fqdn() ended with: %s", result)
186 return result
187
188
189def validate_network(logger: logging.Logger, ip_network: str) -> ipaddress._BaseNetwork:
190 """check if an ip address is in a list of ip networks"""
191 logger.debug("Helper.validate_network(%s)", ip_network)
192 return ipaddress.ip_network(ip_network, strict=False)
193
194
195def ip_validate(logger: logging.Logger, ip_addr: str) -> Tuple[str, bool]:
196 """validate ip address"""
197 logger.debug("Helper.ip_validate(%s)", ip_addr)
198
199 try:
200 reverse_pointer = ipaddress.ip_address(ip_addr).reverse_pointer
201 invalid = False
202 except ValueError:
203 reverse_pointer = None
204 invalid = True
205 logger.debug("Helper.ip_validate() ended with: %s:%s", reverse_pointer, invalid)
206 return (reverse_pointer, invalid)
207
208
209def ipv6_chk(logger: logging.Logger, address: str) -> bool:
210 """check if an address is ipv6"""
211 logger.debug("Helper.ipv6_chk(%s)", address)
212
213 try:
214 # we need to set a host header and braces for ipv6 headers and
215 if isinstance(ipaddress.ip_address(address), ipaddress.IPv6Address):
216 logger.debug("Helper.v6_adjust(}): ipv6 address detected")
217 result = True
218 else:
219 result = False
220 except Exception:
221 result = False
222
223 logger.debug("Helper.ipv6_chk() ended with %s", result)
224 return result
225
226
227def cn_validate(logger: logging.Logger, cn: str) -> bool:
228 """validate common name"""
229 logger.debug("Helper.cn_validate(%s)", cn)
230
231 error = False
232 if cn:
233 # check if CN is a valid IP address
234 result = validate_ip(logger, cn)
235 if not result:
236 # check if CN is a valid fqdn
237 result = validate_fqdn(logger, cn)
238 if not result:
239 error = "Profile subject check failed: CN validation failed"
240 else:
241 error = "Profile subject check failed: commonName missing"
242
243 logger.debug("Helper.cn_validate() ended with: %s", error)
244 return error