use of okhttp3.CertificatePinner.Pin in project okhttp by square.
the class CallTest method matchingPinnedCertificate.
@Test
public void matchingPinnedCertificate() throws Exception {
enableTls();
server.enqueue(new MockResponse());
server.enqueue(new MockResponse());
// Make a first request without certificate pinning. Use it to collect certificates to pin.
Request request1 = new Request.Builder().url(server.url("/")).build();
Response response1 = client.newCall(request1).execute();
CertificatePinner.Builder certificatePinnerBuilder = new CertificatePinner.Builder();
for (Certificate certificate : response1.handshake().peerCertificates()) {
certificatePinnerBuilder.add(server.getHostName(), CertificatePinner.pin(certificate));
}
response1.body().close();
// Make another request with certificate pinning. It should complete normally.
client = client.newBuilder().certificatePinner(certificatePinnerBuilder.build()).build();
Request request2 = new Request.Builder().url(server.url("/")).build();
Response response2 = client.newCall(request2).execute();
assertNotSame(response2.handshake(), response1.handshake());
response2.body().close();
}
use of okhttp3.CertificatePinner.Pin in project okhttp by square.
the class CallTest method unmatchingPinnedCertificate.
@Test
public void unmatchingPinnedCertificate() throws Exception {
enableTls();
server.enqueue(new MockResponse());
// Pin publicobject.com's cert.
client = client.newBuilder().certificatePinner(new CertificatePinner.Builder().add(server.getHostName(), "sha1/DmxUShsZuNiqPQsX2Oi9uv2sCnw=").build()).build();
// When we pin the wrong certificate, connectivity fails.
Request request = new Request.Builder().url(server.url("/")).build();
try {
client.newCall(request).execute();
fail();
} catch (SSLPeerUnverifiedException expected) {
assertTrue(expected.getMessage().startsWith("Certificate pinning failure!"));
}
}
use of okhttp3.CertificatePinner.Pin in project okhttp by square.
the class CertificatePinnerTest method sameKeypairSamePin.
/** Multiple certificates generated from the same keypair have the same pin. */
@Test
public void sameKeypairSamePin() throws Exception {
HeldCertificate heldCertificateA2 = new HeldCertificate.Builder().keyPair(certA1.keyPair).serialNumber("101").build();
String keypairACertificate2Pin = CertificatePinner.pin(heldCertificateA2.certificate);
HeldCertificate heldCertificateB2 = new HeldCertificate.Builder().keyPair(certB1.keyPair).serialNumber("201").build();
String keypairBCertificate2Pin = CertificatePinner.pin(heldCertificateB2.certificate);
assertTrue(certA1Sha256Pin.equals(keypairACertificate2Pin));
assertTrue(certB1Sha256Pin.equals(keypairBCertificate2Pin));
assertFalse(certA1Sha256Pin.equals(certB1Sha256Pin));
}
Aggregations