Skip to content

Commit

Permalink
Issue #6276 - Support non-standard domains in SNI and X509.
Browse files Browse the repository at this point in the history
Updates after review.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed May 24, 2021
1 parent 4d45c5f commit aa53316
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions jetty-util/src/main/java/org/eclipse/jetty/util/ssl/X509.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import javax.naming.ldap.LdapName;
import javax.naming.ldap.Rdn;
import javax.security.auth.x500.X500Principal;
Expand All @@ -40,6 +41,10 @@ public class X509
*/
private static final int SUBJECT_ALTERNATIVE_NAMES__DNS_NAME = 2;
private static final int SUBJECT_ALTERNATIVE_NAMES__IP_ADDRESS = 7;
private static final String IPV4 = "([0-9]{1,3})(\\.[0-9]{1,3}){3}";
private static final Pattern IPV4_REGEXP = Pattern.compile("^" + IPV4 + "$");
// Look-ahead for 2 ':' + IPv6 characters + optional IPv4 at the end.
private static final Pattern IPV6_REGEXP = Pattern.compile("(?=.*:.*:)^([0-9a-fA-F:\\[\\]]+)(:" + IPV4 + ")?$");

public static boolean isCertSign(X509Certificate x509)
{
Expand Down Expand Up @@ -196,20 +201,7 @@ public boolean matches(String host)

private static boolean seemsIPAddress(String host)
{
// IPv4 is just numbers and dots.
String ipv4RegExp = "[0-9\\.]+";
// IPv6 is hex and colons and possibly brackets.
String ipv6RegExp = "[0-9a-fA-F:\\[\\]]+";
return host.matches(ipv4RegExp) ||
(host.matches(ipv6RegExp) && containsAtLeastTwoColons(host));
}

private static boolean containsAtLeastTwoColons(String host)
{
int index = host.indexOf(':');
if (index >= 0)
index = host.indexOf(':', index + 1);
return index > 0;
return IPV4_REGEXP.matcher(host).matches() || IPV6_REGEXP.matcher(host).matches();
}

@Override
Expand Down

0 comments on commit aa53316

Please sign in to comment.