Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ private static boolean verifyDnsNameExact(
if (Strings.isNullOrEmpty(sanToVerifyExact)) {
return false;
}
if (sanToVerifyExact.contains("*")) {
return verifyDnsNameWildcard(altNameFromCert, sanToVerifyExact, ignoreCase);
}
return ignoreCase
? sanToVerifyExact.equalsIgnoreCase(altNameFromCert)
: sanToVerifyExact.equals(altNameFromCert);
Expand Down Expand Up @@ -303,4 +306,47 @@ public X509Certificate[] getAcceptedIssuers() {
}
return delegate.getAcceptedIssuers();
}

private static boolean verifyDnsNameWildcard(
String altNameFromCert, String sanToVerify, boolean ignoreCase) {
String[] splitPattern = splitAtFirstDelimiter(ignoreCase
? sanToVerify.toLowerCase(Locale.ROOT) : sanToVerify);
String[] splitDnsName = splitAtFirstDelimiter(ignoreCase
? altNameFromCert.toLowerCase(Locale.ROOT) : altNameFromCert);
if (splitPattern == null || splitDnsName == null
|| splitPattern.length < 2 || splitDnsName.length < 2) {
return false;
}
if (splitPattern[0].contains("*")
&& !splitPattern[1].contains("*")
&& !splitPattern[0].startsWith("xn--")) {
return splitDnsName[1].equals(splitPattern[1])
&& labelWildcardMatch(splitDnsName[0], splitPattern[0]);
}
return false;
}

private static boolean labelWildcardMatch(String dnsLabel, String pattern) {
final char glob = '*';
// Check the special case of a single * pattern, as it's common.
if (pattern.equals("*")) {
return true;
}
int globIndex = pattern.indexOf(glob);
if (pattern.indexOf(glob, globIndex + 1) == -1) {
return dnsLabel.length() >= pattern.length() - 1
&& dnsLabel.startsWith(pattern.substring(0, globIndex))
&& dnsLabel.endsWith(pattern.substring(globIndex + 1));
}
return false;
}

@Nullable
private static String[] splitAtFirstDelimiter(String s) {
int index = s.indexOf('.');
if (index == -1) {
return null;
}
return new String[]{s.substring(0, index), s.substring(index + 1)};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import static io.grpc.xds.internal.security.CommonTlsContextTestsUtil.CA_PEM_FILE;
import static io.grpc.xds.internal.security.CommonTlsContextTestsUtil.CLIENT_PEM_FILE;
import static io.grpc.xds.internal.security.CommonTlsContextTestsUtil.CLIENT_SPIFFE_PEM_FILE;
import static io.grpc.xds.internal.security.CommonTlsContextTestsUtil.SERVER_0_PEM_FILE;
import static io.grpc.xds.internal.security.CommonTlsContextTestsUtil.SERVER_1_PEM_FILE;
import static io.grpc.xds.internal.security.CommonTlsContextTestsUtil.SERVER_1_SPIFFE_PEM_FILE;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.doReturn;
Expand Down Expand Up @@ -691,6 +693,42 @@ public void unsupportedAltNameType() throws CertificateException, IOException {
}
}

private void runDnsWildcardTest(
String sanPattern, String certFile, boolean ignoreCase, boolean expected)
throws CertificateException, IOException {
StringMatcher stringMatcher =
StringMatcher.newBuilder()
.setExact(sanPattern)
.setIgnoreCase(ignoreCase)
.build();
@SuppressWarnings("deprecation")
CertificateValidationContext certContext =
CertificateValidationContext.newBuilder()
.addMatchSubjectAltNames(stringMatcher)
.build();
trustManager = new XdsX509TrustManager(certContext, mockDelegate);
X509Certificate[] certs =
CertificateUtils.toX509Certificates(TlsTesting.loadCert(certFile));
try {
trustManager.verifySubjectAltNameInChain(certs);
assertTrue(expected);
} catch (CertificateException certException) {
assertThat(certException).hasMessageThat().isEqualTo("Peer certificate SAN check failed");
}
}

@Test
public void testDnsWildcardPatterns() throws Exception {
runDnsWildcardTest("*.test.google.fr", SERVER_1_PEM_FILE, false, true);
runDnsWildcardTest("*.test.youtube.com", SERVER_1_PEM_FILE, false, true);
runDnsWildcardTest("waterzooi.test.google.be", SERVER_1_PEM_FILE, false, true);
runDnsWildcardTest("192.168.1.3", SERVER_1_PEM_FILE, false, true);
runDnsWildcardTest("*.TEST.YOUTUBE.com", SERVER_1_PEM_FILE, true, true);
runDnsWildcardTest("*.test.google.com.au", SERVER_0_PEM_FILE, false, true);
runDnsWildcardTest("*.TEST.YOUTUBE.com", SERVER_1_PEM_FILE, false, false);
runDnsWildcardTest("*waterzooi", SERVER_1_PEM_FILE, false, false);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds no value, we deny if xn-- is present in pattern[0], the failure here is due to other reasons, and is misleading. Change it to a negative test case with xn-- in pattern[0].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kannanjgithub , I have not addressed this review comment yet because our current certificates do not include this pattern. I will address this in a future commit once we finalize the new patterns.


private TestSslEngine buildTrustManagerAndGetSslEngine()
throws CertificateException, IOException, CertStoreException {
SSLParameters sslParams = buildTrustManagerAndGetSslParameters();
Expand Down
Loading