Skip to content
Merged
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 @@ -121,15 +121,17 @@ private static final class Rule {
}
final String norm;
try {
norm = IDN.toASCII(pattern).toLowerCase(Locale.ROOT);
// Normalize before validating so that "*.com." is rejected like "*.com".
norm = stripRootDot(IDN.toASCII(pattern).toLowerCase(Locale.ROOT));
} catch (final IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid IDN host pattern: " + pattern, e);
}
if (norm.isEmpty()) {
throw new IllegalArgumentException("Empty host pattern");
}
final boolean wc = norm.startsWith("*.");
if (wc && norm.indexOf('.', 2) < 0) { // require "*.<label>"
final boolean wc = norm.startsWith("*");
if (wc && (norm.length() < 2 || norm.charAt(1) != '.' || norm.indexOf('.', 2) < 0)) {
// require "*.<label>.<label>"; also catches "*." and "*.com." after normalization
throw new IllegalArgumentException("Wildcard must be single-label: *.example.com");
}
if (pins == null || pins.isEmpty()) {
Expand Down Expand Up @@ -180,7 +182,9 @@ protected void verifySession(final String hostname, final SSLSession sslSession)
final String host;
try {
// Canonicalize host: IDNA (Punycode) + lowercase for consistent matching.
host = IDN.toASCII(hostname == null ? "" : hostname).toLowerCase(Locale.ROOT);
// The absolute form "api.example.com." names the same host and passes the
// built-in hostname check, so it must be matched against the same rules.
host = stripRootDot(IDN.toASCII(hostname == null ? "" : hostname).toLowerCase(Locale.ROOT));
} catch (final IllegalArgumentException e) {
throw new SSLException("Invalid IDN host: " + hostname, e);
}
Expand Down Expand Up @@ -286,6 +290,10 @@ private static ByteArrayKey parsePin(final String s) {
}


private static String stripRootDot(final String host) {
return host.length() > 1 && host.endsWith(".") ? host.substring(0, host.length() - 1) : host;
}

private List<Rule> matchedRules(final String host) {
final List<Rule> out = new ArrayList<>();
for (int i = 0; i < rules.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,88 @@ void verifySessionInvalidIdnThrowsSslException() throws NoSuchAlgorithmException
assertThrows(SSLException.class, () -> s.verifySession("\uDC00bad", session));
}

@Test
void verifySessionEnforcesPinsForTrailingDotHost() throws Exception {
final SpkiPinningClientTlsStrategy strategy = SpkiPinningClientTlsStrategy
.newBuilder(SSLContext.getDefault())
.add("api.example.com", sha256Pin(new byte[]{8, 8, 8}))
.add("*.example.org", sha256Pin(new byte[]{8, 8, 8}))
.build();

final SSLSession session = new FakeSession(new X509WithKey(new byte[]{7, 7, 7}));
assertThrows(SSLException.class, () -> strategy.verifySession("api.example.com.", session));
assertThrows(SSLException.class, () -> strategy.verifySession("svc.example.org.", session));
}

@Test
void verifySessionAcceptsValidPinForTrailingDotHost() throws Exception {
final byte[] spki = new byte[]{3, 1, 4, 1};
final String pin = sha256Pin(spki);

final SpkiPinningClientTlsStrategy strategy = SpkiPinningClientTlsStrategy
.newBuilder(SSLContext.getDefault())
.add("api.example.com", pin)
.add("*.example.org", pin)
.build();

final SSLSession session = new FakeSession(new X509WithKey(spki));
assertDoesNotThrow(() -> strategy.verifySession("api.example.com.", session));
assertDoesNotThrow(() -> strategy.verifySession("svc.example.org.", session));
}

@Test
void trailingDotRulePatternsMatchPlainHost() throws Exception {
final byte[] spki = new byte[]{2, 7, 1, 8};
final String wrongPin = sha256Pin(new byte[]{6, 6, 6});

final SpkiPinningClientTlsStrategy strategy = SpkiPinningClientTlsStrategy
.newBuilder(SSLContext.getDefault())
.add("api.example.com.", wrongPin)
.add("*.example.org.", wrongPin)
.build();

// Rules configured in absolute form must still be enforced for the plain host.
final SSLSession session = new FakeSession(new X509WithKey(spki));
assertThrows(SSLException.class, () -> strategy.enforcePins("api.example.com", session));
assertThrows(SSLException.class, () -> strategy.enforcePins("svc.example.org", session));
assertThrows(SSLException.class, () -> strategy.verifySession("api.example.com.", session));
assertThrows(SSLException.class, () -> strategy.verifySession("svc.example.org.", session));
}

@Test
void trailingDotWildcardRuleIsStillSingleLabel() throws Exception {
final byte[] spki = new byte[]{1, 6, 1, 8};
final String pin = sha256Pin(spki);

final SpkiPinningClientTlsStrategy strategy = SpkiPinningClientTlsStrategy
.newBuilder(SSLContext.getDefault())
.add("*.example.com.", pin)
.build();

final SSLSession session = new FakeSession(new X509WithKey(spki));
assertDoesNotThrow(() -> strategy.verifySession("svc.example.com", session));
assertDoesNotThrow(() -> strategy.verifySession("svc.example.com.", session));

// a.b.example.com must not match the single-label wildcard -> no pinning enforced
final SSLSession other = new FakeSession(new X509WithKey(new byte[]{9, 9, 9}));
assertDoesNotThrow(() -> strategy.enforcePins("a.b.example.com", other));
// the bare suffix must not match either
assertDoesNotThrow(() -> strategy.enforcePins("example.com", other));
}

@Test
void trailingDotWildcardPatternValidatedAfterNormalization() {
final String pin = "sha256/" + Base64.getEncoder().encodeToString(new byte[32]);
// "*.com." normalizes to "*.com", which is not a valid single-label wildcard
assertThrows(IllegalArgumentException.class, () -> SpkiPinningClientTlsStrategy
.newBuilder(SSLContext.getDefault())
.add("*.com.", pin));
// a bare "*" must not be accepted as an exact host either
assertThrows(IllegalArgumentException.class, () -> SpkiPinningClientTlsStrategy
.newBuilder(SSLContext.getDefault())
.add("*", pin));
}

@SuppressWarnings("deprecation")
private static final class X509WithKey extends X509Certificate {
private final PublicKey key;
Expand Down
Loading