@@ -214,19 +214,28 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) { |
214 | 214 | let valid = false; |
215 | 215 | let reason = 'Unknown reason'; |
216 | 216 |
|
| 217 | + const hasAltNames = |
| 218 | + dnsNames.length > 0 || ips.length > 0 || uriNames.length > 0; |
| 219 | + |
| 220 | + hostname = unfqdn(hostname); // Remove trailing dot for error messages. |
| 221 | + |
217 | 222 | if (net.isIP(hostname)) { |
218 | 223 | valid = ips.includes(canonicalizeIP(hostname)); |
219 | 224 | if (!valid) |
220 | 225 | reason = `IP: ${hostname} is not in the cert's list: ${ips.join(', ')}`; |
221 | 226 | // TODO(bnoordhuis) Also check URI SANs that are IP addresses. |
222 | | - } else if (subject) { |
223 | | - hostname = unfqdn(hostname); // Remove trailing dot for error messages. |
| 227 | + } else if (hasAltNames || subject) { |
224 | 228 | const hostParts = splitHost(hostname); |
225 | 229 | const wildcard = (pattern) => check(hostParts, pattern, true); |
226 | | - const noWildcard = (pattern) => check(hostParts, pattern, false); |
227 | 230 |
|
228 | | - // Match against Common Name only if no supported identifiers are present. |
229 | | - if (dnsNames.length === 0 && ips.length === 0 && uriNames.length === 0) { |
| 231 | + if (hasAltNames) { |
| 232 | + const noWildcard = (pattern) => check(hostParts, pattern, false); |
| 233 | + valid = dnsNames.some(wildcard) || uriNames.some(noWildcard); |
| 234 | + if (!valid) |
| 235 | + reason = |
| 236 | + `Host: ${hostname}. is not in the cert's altnames: ${altNames}`; |
| 237 | + } else { |
| 238 | + // Match against Common Name only if no supported identifiers exist. |
230 | 239 | const cn = subject.CN; |
231 | 240 |
|
232 | 241 | if (Array.isArray(cn)) |
@@ -236,11 +245,6 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) { |
236 | 245 |
|
237 | 246 | if (!valid) |
238 | 247 | reason = `Host: ${hostname}. is not cert's CN: ${cn}`; |
239 | | - } else { |
240 | | - valid = dnsNames.some(wildcard) || uriNames.some(noWildcard); |
241 | | - if (!valid) |
242 | | - reason = |
243 | | - `Host: ${hostname}. is not in the cert's altnames: ${altNames}`; |
244 | 248 | } |
245 | 249 | } else { |
246 | 250 | reason = 'Cert is empty'; |
|