CertificatePinner

data class CertificatePinner : Function4<<Error class: unknown class>, <Error class: unknown class>, <Error class: unknown class>, completionHandler: (<Error class: unknown class>, <Error class: unknown class>?) -> Unit, Unit>

Constrains which certificates are trusted. Pinning certificates defends against attacks on certificate authorities. It also prevents connections through man-in-the-middle certificate authorities either known or unknown to the application's user. This class currently pins a certificate's Subject Public Key Info as described on Adam Langley's Weblog. Pins are either base64 SHA-256 hashes as in HTTP Public Key Pinning (HPKP) or SHA-1 base64 hashes as in Chromium's static certificates.

Setting up Certificate Pinning

The easiest way to pin a host is to turn on pinning with a broken configuration and read the expected configuration when the connection fails. Be sure to do this on a trusted network, and without man-in-the-middle tools like Charles or Fiddler.

For example, to pin https://publicobject.com, start with a broken configuration:

HttpClient(Darwin) {

// ...

engine {
val builder = CertificatePinner.Builder()
.add("publicobject.com", "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
handleChallenge(builder.build())
}
}

As expected, this fails with an exception, see the logs:

HttpClient: Certificate pinning failure!
Peer certificate chain:
sha256/afwiKY3RxoMmLkuRW1l7QsPZTJPwDS2pdDROQjXw8ig=: publicobject.com
sha256/klO23nT2ehFDXCfx3eHTDRESMz3asj1muO+4aIdjiuY=: COMODO RSA Secure Server CA
sha256/grX4Ta9HpZx6tSHkmCrvpApTQGo67CYDnvprLg5yRME=: COMODO RSA Certification Authority
sha256/lCppFqbkrlJ3EcVFAkeip0+44VaoJUymbnOaEUk7tEU=: AddTrust External CA Root
Pinned certificates for publicobject.com:
sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=

Follow up by pasting the public key hashes from the logs into the certificate pinner's configuration:

val builder = CertificatePinner.Builder()
.add("publicobject.com", "sha256/afwiKY3RxoMmLkuRW1l7QsPZTJPwDS2pdDROQjXw8ig=")
.add("publicobject.com", "sha256/klO23nT2ehFDXCfx3eHTDRESMz3asj1muO+4aIdjiuY=")
.add("publicobject.com", "sha256/grX4Ta9HpZx6tSHkmCrvpApTQGo67CYDnvprLg5yRME=")
.add("publicobject.com", "sha256/lCppFqbkrlJ3EcVFAkeip0+44VaoJUymbnOaEUk7tEU=")
handleChallenge(builder.build())

Domain Patterns

Pinning is per-hostname and/or per-wildcard pattern. To pin both publicobject.com and www.publicobject.com you must configure both hostnames. Or you may use patterns to match sets of related domain names. The following forms are permitted:

  • Full domain name: you may pin an exact domain name like www.publicobject.com. It won't match additional prefixes (us-west.www.publicobject.com) or suffixes (publicobject.com).

  • Any number of subdomains: Use two asterisks to like **.publicobject.com to match any number of prefixes (us-west.www.publicobject.com, www.publicobject.com) including no prefix at all (publicobject.com). For most applications this is the best way to configure certificate pinning.

  • Exactly one subdomain: Use a single asterisk like *.publicobject.com to match exactly one prefix (www.publicobject.com, api.publicobject.com). Be careful with this approach as no pinning will be enforced if additional prefixes are present, or if no prefixes are present.

Note that any other form is unsupported. You may not use asterisks in any position other than the leftmost label.

If multiple patterns match a hostname, any match is sufficient. For example, suppose pin A applies to *.publicobject.com and pin B applies to api.publicobject.com. Handshakes for api.publicobject.com are valid if either A's or B's certificate is in the chain.

Warning: Certificate Pinning is Dangerous!

Pinning certificates limits your server team's abilities to update their TLS certificates. By pinning certificates, you take on additional operational complexity and limit your ability to migrate between certificate authorities. Do not use certificate pinning without the blessing of your server's TLS administrator!

See also OWASP: Certificate and Public Key Pinning(https://www.owasp.org/index

  • .php/Certificate_and_Public_Key_Pinning).

This class was heavily inspired by OkHttp, which is a great Http library for Android https://square.github.io/okhttp/4.x/okhttp/okhttp3/-certificate-pinner/ https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/CertificatePinner.kt

Types

Link copied to clipboard
data class Builder(pinnedCertificates: MutableList<PinnedCertificate> = mutableListOf(), validateTrust: Boolean = true)

Builds a configured CertificatePinner.

Functions

Link copied to clipboard
open operator override fun invoke(session: <Error class: unknown class>, task: <Error class: unknown class>, challenge: <Error class: unknown class>, completionHandler: (<Error class: unknown class>, <Error class: unknown class>?) -> Unit)