Skip to content

Commit 19c27c6

Browse files
committed
Improve alt-name checks in HostnameUtil
1 parent 3ea208d commit 19c27c6

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

tls/src/main/java/org/bouncycastle/jsse/provider/HostnameUtil.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ static void checkHostname(String hostname, X509Certificate certificate, boolean
3737
{
3838
for (List<?> subjectAltName : subjectAltNames)
3939
{
40-
int type = ((Integer)subjectAltName.get(0)).intValue();
41-
if (GeneralName.iPAddress != type)
40+
if (!isAltNameType(subjectAltName, GeneralName.iPAddress))
41+
{
42+
continue;
43+
}
44+
45+
String ipAddress = getAltNameValue(subjectAltName);
46+
if (ipAddress == null)
4247
{
4348
continue;
4449
}
4550

46-
String ipAddress = (String)subjectAltName.get(1);
4751
if (hostname.equalsIgnoreCase(ipAddress))
4852
{
4953
return;
@@ -76,15 +80,19 @@ else if (isValidDomainName(hostname))
7680
boolean foundAnyDNSNames = false;
7781
for (List<?> subjectAltName : subjectAltNames)
7882
{
79-
int type = ((Integer)subjectAltName.get(0)).intValue();
80-
if (GeneralName.dNSName != type)
83+
if (!isAltNameType(subjectAltName, GeneralName.dNSName))
8184
{
8285
continue;
8386
}
8487

8588
foundAnyDNSNames = true;
8689

87-
String dnsName = (String)subjectAltName.get(1);
90+
String dnsName = getAltNameValue(subjectAltName);
91+
if (dnsName == null)
92+
{
93+
continue;
94+
}
95+
8896
if (matchesDNSName(hostname, dnsName, allWildcards))
8997
{
9098
return;
@@ -134,6 +142,19 @@ private static ASN1Primitive findMostSpecificCN(X500Principal principal)
134142
return null;
135143
}
136144

145+
private static String getAltNameValue(List<?> subjectAltName)
146+
{
147+
if (subjectAltName != null && subjectAltName.size() >= 2)
148+
{
149+
Object objValue = subjectAltName.get(1);
150+
if (objValue instanceof String)
151+
{
152+
return (String)objValue;
153+
}
154+
}
155+
return null;
156+
}
157+
137158
private static String getLabel(String s, int begin)
138159
{
139160
int end = s.indexOf('.', begin);
@@ -144,6 +165,19 @@ private static String getLabel(String s, int begin)
144165
return s.substring(begin, end);
145166
}
146167

168+
private static boolean isAltNameType(List<?> subjectAltName, int type)
169+
{
170+
if (subjectAltName != null && subjectAltName.size() >= 1)
171+
{
172+
Object objValue = subjectAltName.get(0);
173+
if (objValue instanceof Integer)
174+
{
175+
return ((Integer)objValue).intValue() == type;
176+
}
177+
}
178+
return false;
179+
}
180+
147181
private static boolean isValidDomainName(String name)
148182
{
149183
try

0 commit comments

Comments
 (0)