Class 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 the SubjectPublicKeyInfo (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.com matches a.example.com but not a.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
    • 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.SSLException
        Invoked after the default trust and hostname checks. If one or more rules match the hostname, at least one pin must match any SPKI in the peer chain.
        Overrides:
        verifySession in class AbstractClientTlsStrategy
        Throws:
        javax.net.ssl.SSLException
      • enforcePins

        void enforcePins​(java.lang.String hostname,
                         javax.net.ssl.SSLSession sslSession)
                  throws javax.net.ssl.SSLException
        Enforce 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
      • chainSpkiSha256

        private static byte[][] chainSpkiSha256​(javax.net.ssl.SSLSession session)
                                         throws javax.net.ssl.SSLException
        Throws:
        javax.net.ssl.SSLException
      • peerPinsForLog

        private static java.lang.String peerPinsForLog​(byte[][] hashes)