use of okhttp3.tls.HeldCertificate in project okhttp by square.
the class CallTest method httpsWithIpAddress.
@Test
public void httpsWithIpAddress() throws Exception {
String localIpAddress = InetAddress.getLoopbackAddress().getHostAddress();
// Create a certificate with an IP address in the subject alt name.
HeldCertificate heldCertificate = new HeldCertificate.Builder().commonName("example.com").subjectAlternativeName(localIpAddress).build();
SslClient sslClient = new SslClient.Builder().certificateChain(heldCertificate.keyPair, heldCertificate.certificate).addTrustedCertificate(heldCertificate.certificate).build();
// Use that certificate on the server and trust it on the client.
server.useHttps(sslClient.socketFactory, false);
client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).protocols(Collections.singletonList(Protocol.HTTP_1_1)).build();
// Make a request.
server.enqueue(new MockResponse());
HttpUrl url = server.url("/").newBuilder().host(localIpAddress).build();
Request request = new Request.Builder().url(url).build();
executeSynchronously(request).assertCode(200);
// Confirm that the IP address was used in the host header.
RecordedRequest recordedRequest = server.takeRequest();
assertEquals(localIpAddress + ":" + server.getPort(), recordedRequest.getHeader("Host"));
}
use of okhttp3.tls.HeldCertificate in project okhttp by square.
the class CertificateChainCleanerTest method normalizeSingleSelfSignedCertificate.
@Test
public void normalizeSingleSelfSignedCertificate() throws Exception {
HeldCertificate root = new HeldCertificate.Builder().serialNumber(1L).build();
CertificateChainCleaner cleaner = CertificateChainCleaner.Companion.get(root.certificate());
assertThat(cleaner.clean(list(root), "hostname")).isEqualTo(list(root));
}
use of okhttp3.tls.HeldCertificate in project okhttp by square.
the class CertificateChainCleanerTest method chainGoesAllTheWayToSelfSignedRoot.
@Test
public void chainGoesAllTheWayToSelfSignedRoot() throws Exception {
HeldCertificate selfSigned = new HeldCertificate.Builder().serialNumber(1L).certificateAuthority(2).build();
HeldCertificate trusted = new HeldCertificate.Builder().serialNumber(2L).signedBy(selfSigned).certificateAuthority(1).build();
HeldCertificate certA = new HeldCertificate.Builder().serialNumber(3L).certificateAuthority(0).signedBy(trusted).build();
HeldCertificate certB = new HeldCertificate.Builder().serialNumber(4L).signedBy(certA).build();
CertificateChainCleaner cleaner = CertificateChainCleaner.Companion.get(selfSigned.certificate(), trusted.certificate());
assertThat(cleaner.clean(list(certB, certA), "hostname")).isEqualTo(list(certB, certA, trusted, selfSigned));
assertThat(cleaner.clean(list(certB, certA, trusted), "hostname")).isEqualTo(list(certB, certA, trusted, selfSigned));
assertThat(cleaner.clean(list(certB, certA, trusted, selfSigned), "hostname")).isEqualTo(list(certB, certA, trusted, selfSigned));
}
use of okhttp3.tls.HeldCertificate in project okhttp by square.
the class CertificateChainCleanerTest method chainTooLong.
@Test
public void chainTooLong() {
List<HeldCertificate> heldCertificates = chainOfLength(11);
List<Certificate> certificates = new ArrayList<>();
for (HeldCertificate heldCertificate : heldCertificates) {
certificates.add(heldCertificate.certificate());
}
X509Certificate root = heldCertificates.get(heldCertificates.size() - 1).certificate();
CertificateChainCleaner cleaner = CertificateChainCleaner.Companion.get(root);
try {
cleaner.clean(certificates, "hostname");
fail();
} catch (SSLPeerUnverifiedException expected) {
}
}
use of okhttp3.tls.HeldCertificate in project okhttp by square.
the class CertificateChainCleanerTest method orderedChainOfCertificatesWithRoot.
@Test
public void orderedChainOfCertificatesWithRoot() throws Exception {
HeldCertificate root = new HeldCertificate.Builder().serialNumber(1L).certificateAuthority(1).build();
HeldCertificate certA = new HeldCertificate.Builder().serialNumber(2L).certificateAuthority(0).signedBy(root).build();
HeldCertificate certB = new HeldCertificate.Builder().serialNumber(3L).signedBy(certA).build();
CertificateChainCleaner cleaner = CertificateChainCleaner.Companion.get(root.certificate());
assertThat(cleaner.clean(list(certB, certA, root), "hostname")).isEqualTo(list(certB, certA, root));
}
Aggregations