Package org.apache.hc.client5.http.ssl
Class SpkiPinningClientTlsStrategy
- java.lang.Object
-
- org.apache.hc.client5.http.ssl.AbstractClientTlsStrategy
-
- org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy
-
- org.apache.hc.client5.http.ssl.SpkiPinningClientTlsStrategy
-
- All Implemented Interfaces:
TlsSocketStrategy,org.apache.hc.core5.http.nio.ssl.TlsStrategy
@Contract(threading=IMMUTABLE) public final class SpkiPinningClientTlsStrategy extends DefaultClientTlsStrategy
SPKI pinning decorator for client-side TLS.
This strategy enforces one or more
sha256/<base64(SPKI)>pins for a given host or single-label wildcard (e.g.*.example.com) after the standard trust manager and hostname verification succeed. Pins are matched against theSubjectPublicKeyInfo(SPKI) of any certificate in the peer chain.Host matching is performed on the IDNA ASCII (Punycode) lowercase form. Wildcards are single-label only (e.g.
*.example.commatchesa.example.combut nota.b.example.com).Warning: Certificate pinning increases operational risk. Always ship at least two pins (active + backup) and keep normal PKI + hostname verification enabled.
Thread-safety: immutable and thread-safe.
- Since:
- 5.6
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSpkiPinningClientTlsStrategy.BuilderBuilder forSpkiPinningClientTlsStrategy.private static classSpkiPinningClientTlsStrategy.ByteArrayKeyByte-array key with constant-time equality for use in sets/maps.private static classSpkiPinningClientTlsStrategy.RuleMatch rule for a host or single-label wildcard.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringPIN_PREFIXprivate java.util.List<SpkiPinningClientTlsStrategy.Rule>rulesprivate static intSHA256_LEN
-
Constructor Summary
Constructors Modifier Constructor Description privateSpkiPinningClientTlsStrategy(javax.net.ssl.SSLContext sslContext, java.util.List<SpkiPinningClientTlsStrategy.Rule> rules)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static byte[][]chainSpkiSha256(javax.net.ssl.SSLSession session)private static java.lang.StringconfiguredPinsFor(java.util.List<SpkiPinningClientTlsStrategy.Rule> rules)(package private) voidenforcePins(java.lang.String hostname, javax.net.ssl.SSLSession sslSession)Enforce SPKI pins for the given hostname and session.private java.util.List<SpkiPinningClientTlsStrategy.Rule>matchedRules(java.lang.String host)static SpkiPinningClientTlsStrategy.BuildernewBuilder(javax.net.ssl.SSLContext sslContext)Create a new builder.private static java.lang.StringpeerPinsForLog(byte[][] hashes)protected voidverifySession(java.lang.String hostname, javax.net.ssl.SSLSession sslSession)Invoked after the default trust and hostname checks.-
Methods inherited from class org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy
applyParameters, createDefault, createSystemDefault, createTlsDetails, getDefault, getSystemDefault
-
Methods inherited from class org.apache.hc.client5.http.ssl.AbstractClientTlsStrategy
initializeEngine, initializeSocket, toEscapedString, upgrade, upgrade, upgrade, verifySession
-
-
-
-
Field Detail
-
PIN_PREFIX
private static final java.lang.String PIN_PREFIX
- See Also:
- Constant Field Values
-
SHA256_LEN
private static final int SHA256_LEN
- See Also:
- Constant Field Values
-
rules
private final java.util.List<SpkiPinningClientTlsStrategy.Rule> rules
-
-
Constructor Detail
-
SpkiPinningClientTlsStrategy
private SpkiPinningClientTlsStrategy(javax.net.ssl.SSLContext sslContext, java.util.List<SpkiPinningClientTlsStrategy.Rule> rules)
-
-
Method Detail
-
verifySession
protected void verifySession(java.lang.String hostname, javax.net.ssl.SSLSession sslSession) throws javax.net.ssl.SSLExceptionInvoked after the default trust and hostname checks. If one or more rules match thehostname, at least one pin must match any SPKI in the peer chain.- Overrides:
verifySessionin classAbstractClientTlsStrategy- Throws:
javax.net.ssl.SSLException
-
enforcePins
void enforcePins(java.lang.String hostname, javax.net.ssl.SSLSession sslSession) throws javax.net.ssl.SSLExceptionEnforce SPKI pins for the given hostname and session. Package-private for testing.- Throws:
javax.net.ssl.SSLException
-
newBuilder
public static SpkiPinningClientTlsStrategy.Builder newBuilder(javax.net.ssl.SSLContext sslContext)
Create a new builder.- Parameters:
sslContext- SSL context used for handshakes (trust + keys).- Returns:
- builder
-
matchedRules
private java.util.List<SpkiPinningClientTlsStrategy.Rule> matchedRules(java.lang.String host)
-
chainSpkiSha256
private static byte[][] chainSpkiSha256(javax.net.ssl.SSLSession session) throws javax.net.ssl.SSLException- Throws:
javax.net.ssl.SSLException
-
configuredPinsFor
private static java.lang.String configuredPinsFor(java.util.List<SpkiPinningClientTlsStrategy.Rule> rules)
-
peerPinsForLog
private static java.lang.String peerPinsForLog(byte[][] hashes)
-
-