21
21
import java .io .FileInputStream ;
22
22
import java .io .IOException ;
23
23
import java .io .InputStream ;
24
+ import java .security .KeyManagementException ;
25
+ import java .security .KeyStoreException ;
26
+ import java .security .NoSuchAlgorithmException ;
24
27
import java .text .DateFormat ;
25
28
import java .text .SimpleDateFormat ;
26
29
import java .util .Date ;
27
30
import java .util .List ;
28
31
32
+ import javax .net .ssl .HostnameVerifier ;
33
+ import javax .net .ssl .SSLContext ;
34
+
29
35
import org .apache .commons .lang .time .StopWatch ;
30
36
import org .apache .http .HttpEntity ;
31
37
import org .apache .http .client .methods .CloseableHttpResponse ;
32
38
import org .apache .http .client .methods .HttpPost ;
33
39
import org .apache .http .client .methods .HttpRequestBase ;
40
+ import org .apache .http .conn .ssl .NoopHostnameVerifier ;
41
+ import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
42
+ import org .apache .http .conn .ssl .TrustSelfSignedStrategy ;
34
43
import org .apache .http .impl .client .CloseableHttpClient ;
35
44
import org .apache .http .impl .client .HttpClients ;
45
+ import org .apache .http .ssl .SSLContextBuilder ;
36
46
import org .apache .http .util .EntityUtils ;
37
47
38
48
import org .owasp .benchmark .helpers .Utils ;
@@ -53,7 +63,7 @@ protected void init() {
53
63
}
54
64
55
65
protected void crawl (InputStream http ) throws Exception {
56
- CloseableHttpClient httpclient = HttpClients . custom (). setSSLSocketFactory ( Utils . getSSLFactory ()). build ();
66
+ CloseableHttpClient httpclient = createAcceptSelfSignedCertificateClient ();
57
67
long start = System .currentTimeMillis ();
58
68
59
69
List <AbstractTestCaseRequest > requests = Utils .parseHttpFile (http );
@@ -76,16 +86,37 @@ protected void crawl(InputStream http) throws Exception {
76
86
+ " v" + testSuiteVersion + " took " + seconds + " seconds" );
77
87
}
78
88
89
+ // This method taken directly from: https://memorynotfound.com/ignore-certificate-errors-apache-httpclient/
90
+ static CloseableHttpClient createAcceptSelfSignedCertificateClient ()
91
+ throws KeyManagementException , NoSuchAlgorithmException , KeyStoreException {
92
+
93
+ // use the TrustSelfSignedStrategy to allow Self Signed Certificates
94
+ SSLContext sslContext = SSLContextBuilder
95
+ .create ()
96
+ .loadTrustMaterial (new TrustSelfSignedStrategy ())
97
+ .build ();
98
+
99
+ // we can optionally disable hostname verification.
100
+ // if you don't want to further weaken the security, you don't have to include this.
101
+ HostnameVerifier allowAllHosts = new NoopHostnameVerifier ();
102
+
103
+ // create an SSL Socket Factory to use the SSLContext with the trust self signed certificate strategy
104
+ // and allow all hosts verifier.
105
+ SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory (sslContext , allowAllHosts );
106
+
107
+ // finally create the HttpClient using HttpClient factory methods and assign the ssl socket factory
108
+ return HttpClients
109
+ .custom ()
110
+ .setSSLSocketFactory (connectionFactory )
111
+ .build ();
112
+ }
113
+
79
114
/**
80
- * Issue the requested request, measure the time required to execute, then
81
- * output both to stdout and the global
82
- * variable timeString the URL tested, the time required to execute and the
83
- * response code.
115
+ * Issue the requested request, measure the time required to execute, then output both to stdout and the
116
+ * global variable timeString the URL tested, the time required to execute and the response code.
84
117
*
85
- * @param httpclient
86
- * - The HTTP client to use to make the request
87
- * @param request
88
- * - THe HTTP request to issue
118
+ * @param httpclient - The HTTP client to use to make the request
119
+ * @param request - THe HTTP request to issue
89
120
* @throws IOException
90
121
*/
91
122
protected ResponseInfo sendRequest (CloseableHttpClient httpclient , AbstractTestCaseRequest requestTC ) {
0 commit comments