38
38
import com .sun .net .httpserver .HttpsServer ;
39
39
40
40
import org .apache .hc .core5 .http .HttpHost ;
41
+ import org .apache .hc .core5 .ssl .SSLContextBuilder ;
41
42
import org .junit .AfterClass ;
42
43
import org .junit .BeforeClass ;
43
44
44
45
import javax .net .ssl .KeyManagerFactory ;
45
46
import javax .net .ssl .SSLContext ;
46
- import javax .net .ssl .SSLHandshakeException ;
47
+ import javax .net .ssl .SSLException ;
47
48
import javax .net .ssl .TrustManagerFactory ;
48
49
49
50
import java .io .IOException ;
50
51
import java .io .InputStream ;
51
52
import java .net .InetAddress ;
52
53
import java .net .InetSocketAddress ;
53
- import java .nio .file .Files ;
54
- import java .nio .file .Paths ;
55
54
import java .security .AccessController ;
56
- import java .security .KeyFactory ;
57
55
import java .security .KeyStore ;
58
56
import java .security .PrivilegedAction ;
59
- import java .security .cert .Certificate ;
60
- import java .security .cert .CertificateFactory ;
61
- import java .security .spec .PKCS8EncodedKeySpec ;
57
+ import java .security .SecureRandom ;
62
58
63
59
import static org .hamcrest .Matchers .instanceOf ;
64
60
import static org .junit .Assert .assertEquals ;
@@ -75,7 +71,7 @@ public class RestClientBuilderIntegTests extends RestClientTestCase {
75
71
@ BeforeClass
76
72
public static void startHttpServer () throws Exception {
77
73
httpsServer = HttpsServer .create (new InetSocketAddress (InetAddress .getLoopbackAddress (), 0 ), 0 );
78
- httpsServer .setHttpsConfigurator (new HttpsConfigurator (getSslContext ()));
74
+ httpsServer .setHttpsConfigurator (new HttpsConfigurator (getSslContext (true )));
79
75
httpsServer .createContext ("/" , new ResponseHandler ());
80
76
httpsServer .start ();
81
77
}
@@ -103,11 +99,11 @@ public void testBuilderUsesDefaultSSLContext() throws Exception {
103
99
client .performRequest (new Request ("GET" , "/" ));
104
100
fail ("connection should have been rejected due to SSL handshake" );
105
101
} catch (Exception e ) {
106
- assertThat (e , instanceOf (SSLHandshakeException .class ));
102
+ assertThat (e . getCause () , instanceOf (SSLException .class ));
107
103
}
108
104
}
109
105
110
- SSLContext .setDefault (getSslContext ());
106
+ SSLContext .setDefault (getSslContext (false ));
111
107
try (RestClient client = buildRestClient ()) {
112
108
Response response = client .performRequest (new Request ("GET" , "/" ));
113
109
assertEquals (200 , response .getStatusLine ().getStatusCode ());
@@ -122,34 +118,37 @@ private RestClient buildRestClient() {
122
118
return RestClient .builder (new HttpHost ("https" , address .getHostString (), address .getPort ())).build ();
123
119
}
124
120
125
- private static SSLContext getSslContext () throws Exception {
126
- SSLContext sslContext = SSLContext .getInstance (getProtocol ());
121
+ private static SSLContext getSslContext (boolean server ) throws Exception {
122
+ SSLContext sslContext ;
123
+ char [] password = "password" .toCharArray ();
124
+ SecureRandom secureRandom = SecureRandom .getInstance ("DEFAULT" , "BCFIPS" );
125
+ String fileExtension = ".jks" ;
126
+
127
127
try (
128
- InputStream certFile = RestClientBuilderIntegTests .class .getResourceAsStream ("/test.crt" );
129
- InputStream keyStoreFile = RestClientBuilderIntegTests .class .getResourceAsStream ("/test_truststore.jks" )
128
+ InputStream trustStoreFile = RestClientBuilderIntegTests .class .getResourceAsStream ("/test_truststore" + fileExtension );
129
+ InputStream keyStoreFile = RestClientBuilderIntegTests .class .getResourceAsStream ("/testks" + fileExtension )
130
130
) {
131
- // Build a keystore of default type programmatically since we can't use JKS keystores to
132
- // init a KeyManagerFactory in FIPS 140 JVMs.
133
- KeyStore keyStore = KeyStore .getInstance (KeyStore .getDefaultType ());
134
- keyStore .load (null , "password" .toCharArray ());
135
- CertificateFactory certFactory = CertificateFactory .getInstance ("X.509" );
136
- PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec (
137
- Files .readAllBytes (Paths .get (RestClientBuilderIntegTests .class .getResource ("/test.der" ).toURI ()))
138
- );
139
- KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
140
- keyStore .setKeyEntry (
141
- "mykey" ,
142
- keyFactory .generatePrivate (privateKeySpec ),
143
- "password" .toCharArray (),
144
- new Certificate [] { certFactory .generateCertificate (certFile ) }
145
- );
146
- KeyManagerFactory kmf = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
147
- kmf .init (keyStore , "password" .toCharArray ());
131
+ KeyStore keyStore = KeyStore .getInstance ("JKS" );
132
+ keyStore .load (keyStoreFile , password );
133
+ KeyManagerFactory kmf = KeyManagerFactory .getInstance ("PKIX" , "BCJSSE" );
134
+ kmf .init (keyStore , password );
135
+
148
136
KeyStore trustStore = KeyStore .getInstance ("JKS" );
149
- trustStore .load (keyStoreFile , " password" . toCharArray () );
150
- TrustManagerFactory tmf = TrustManagerFactory .getInstance (TrustManagerFactory . getDefaultAlgorithm () );
137
+ trustStore .load (trustStoreFile , password );
138
+ TrustManagerFactory tmf = TrustManagerFactory .getInstance ("PKIX" , "BCJSSE" );
151
139
tmf .init (trustStore );
152
- sslContext .init (kmf .getKeyManagers (), tmf .getTrustManagers (), null );
140
+
141
+ SSLContextBuilder sslContextBuilder = SSLContextBuilder .create ()
142
+ .setProvider ("BCJSSE" )
143
+ .setProtocol (getProtocol ())
144
+ .setSecureRandom (secureRandom );
145
+
146
+ if (server ) {
147
+ sslContextBuilder .loadKeyMaterial (keyStore , password );
148
+ }
149
+ sslContextBuilder .loadTrustMaterial (trustStore , null );
150
+ sslContext = sslContextBuilder .build ();
151
+
153
152
}
154
153
return sslContext ;
155
154
}
0 commit comments