1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit;
16
17 import static org.eclipse.jetty.http.HttpVersion.HTTP_1_1;
18 import static org.junit.Assert.fail;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.InputStream;
22 import java.net.URL;
23
24 import javax.net.ssl.SSLHandshakeException;
25
26 import org.eclipse.jetty.server.SslConnectionFactory;
27 import org.eclipse.jetty.util.ssl.SslContextFactory;
28 import org.eclipse.jetty.util.ssl.SslContextFactory.Server;
29 import org.htmlunit.junit.BrowserRunner;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32
33
34
35
36
37
38
39
40 @RunWith(BrowserRunner.class)
41 public class HttpWebConnectionInsecureSSLWithClientCertificateTest extends WebServerTestCase {
42
43
44
45
46 @Test(expected = SSLHandshakeException.class)
47 public void insecureSSL_clientCertificates_fail() throws Exception {
48 final URL https = new URL("https://localhost:" + PORT2 + "/");
49 loadPage("<div>test</div>", https);
50 fail("SSLHandshakeException expected");
51 }
52
53
54
55
56 @Test
57 public void insecureSSL_clientCertificates() throws Exception {
58 final WebClient webClient = getWebClient();
59 webClient.getOptions().setSSLClientCertificateKeyStore(
60 getClass().getClassLoader().getResource("insecureSSL.pfx"),
61 "nopassword", "PKCS12");
62 webClient.getOptions().setUseInsecureSSL(true);
63
64 final URL https = new URL("https://localhost:" + PORT2 + "/");
65 loadPage("<div>test</div>", https);
66 }
67
68
69
70
71
72
73 @Test(expected = SSLHandshakeException.class)
74 public void insecureSSL_clientCertificatesInputStream_fail() throws Exception {
75 final URL https = new URL("https://localhost:" + PORT2 + "/");
76 loadPage("<div>test</div>", https);
77 }
78
79
80
81
82
83
84 @Test
85 public void insecureSSL_clientCertificatesInputStream() throws Exception {
86 final WebClient webClient = getWebClient();
87 try (InputStream certificateInputStream = getClass().getClassLoader()
88 .getResourceAsStream("insecureSSL.pfx")) {
89 final byte[] certificateBytes = new byte[4096];
90 certificateInputStream.read(certificateBytes);
91
92 try (InputStream is = new ByteArrayInputStream(certificateBytes)) {
93 webClient.getOptions().setSSLClientCertificateKeyStore(is, "nopassword", "PKCS12");
94 webClient.getOptions().setUseInsecureSSL(true);
95
96 final URL https = new URL("https://localhost:" + PORT2 + "/");
97 loadPage("<div>test</div>", https);
98 }
99 }
100 }
101
102 @Override
103 protected boolean isHttps() {
104 return true;
105 }
106
107 @Override
108 public SslConnectionFactory getSslConnectionFactory() {
109 final URL url = HttpWebConnectionInsecureSSLWithClientCertificateTest.class
110 .getClassLoader().getResource("insecureSSL.pfx");
111
112 final SslContextFactory contextFactory = new Server.Server();
113 contextFactory.setKeyStorePath(url.toExternalForm());
114 contextFactory.setKeyStorePassword("nopassword");
115 return new SslConnectionFactory(contextFactory, HTTP_1_1.toString());
116 }
117 }