View Javadoc
1   /*
2    * Copyright 2019-2021 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package nl.altindag.ssl.util;
18  
19  import org.junit.jupiter.api.Test;
20  
21  import javax.net.ssl.SSLContext;
22  import javax.net.ssl.X509ExtendedKeyManager;
23  import javax.net.ssl.X509ExtendedTrustManager;
24  import java.security.KeyStore;
25  import java.security.SecureRandom;
26  import java.security.Security;
27  import java.util.Collections;
28  
29  import static nl.altindag.ssl.TestConstants.IDENTITY_FILE_NAME;
30  import static nl.altindag.ssl.TestConstants.IDENTITY_PASSWORD;
31  import static nl.altindag.ssl.TestConstants.KEYSTORE_LOCATION;
32  import static nl.altindag.ssl.TestConstants.TRUSTSTORE_FILE_NAME;
33  import static nl.altindag.ssl.TestConstants.TRUSTSTORE_PASSWORD;
34  import static org.assertj.core.api.Assertions.assertThat;
35  
36  /**
37   * @author Hakan Altindag
38   */
39  class SSLContextUtilsShould {
40  
41  
42      @Test
43      void createSslContextFromList() {
44          KeyStore identity = KeyStoreUtils.loadKeyStore(KEYSTORE_LOCATION + IDENTITY_FILE_NAME, IDENTITY_PASSWORD);
45          X509ExtendedKeyManager keyManager = KeyManagerUtils.createKeyManager(identity, IDENTITY_PASSWORD);
46  
47          KeyStore trustStore = KeyStoreUtils.loadKeyStore(KEYSTORE_LOCATION + TRUSTSTORE_FILE_NAME, TRUSTSTORE_PASSWORD);
48          X509ExtendedTrustManager trustManager = TrustManagerUtils.createTrustManager(trustStore);
49  
50          SSLContext sslContext = SSLContextUtils.createSslContext(Collections.singletonList(keyManager), Collections.singletonList(trustManager));
51          assertThat(sslContext).isNotNull();
52      }
53  
54      @Test
55      void createSslContextFromCustomSecureRandomAndAndSslContextAlgorithmAndSecurityProviderName() {
56          KeyStore identity = KeyStoreUtils.loadKeyStore(KEYSTORE_LOCATION + IDENTITY_FILE_NAME, IDENTITY_PASSWORD);
57          X509ExtendedKeyManager keyManager = KeyManagerUtils.createKeyManager(identity, IDENTITY_PASSWORD);
58  
59          KeyStore trustStore = KeyStoreUtils.loadKeyStore(KEYSTORE_LOCATION + TRUSTSTORE_FILE_NAME, TRUSTSTORE_PASSWORD);
60          X509ExtendedTrustManager trustManager = TrustManagerUtils.createTrustManager(trustStore);
61  
62          SSLContext sslContext = SSLContextUtils.createSslContext(
63                  Collections.singletonList(keyManager),
64                  Collections.singletonList(trustManager),
65                  new SecureRandom(),
66                  "TLS",
67                  "SunJSSE"
68          );
69          assertThat(sslContext).isNotNull();
70      }
71  
72      @Test
73      void createSslContextFromCustomSecureRandomAndAndSslContextAlgorithmAndSecurityProvider() {
74          KeyStore identity = KeyStoreUtils.loadKeyStore(KEYSTORE_LOCATION + IDENTITY_FILE_NAME, IDENTITY_PASSWORD);
75          X509ExtendedKeyManager keyManager = KeyManagerUtils.createKeyManager(identity, IDENTITY_PASSWORD);
76  
77          KeyStore trustStore = KeyStoreUtils.loadKeyStore(KEYSTORE_LOCATION + TRUSTSTORE_FILE_NAME, TRUSTSTORE_PASSWORD);
78          X509ExtendedTrustManager trustManager = TrustManagerUtils.createTrustManager(trustStore);
79  
80          SSLContext sslContext = SSLContextUtils.createSslContext(
81                  Collections.singletonList(keyManager),
82                  Collections.singletonList(trustManager),
83                  new SecureRandom(),
84                  "TLS",
85                  Security.getProvider("SunJSSE")
86          );
87          assertThat(sslContext).isNotNull();
88      }
89  
90      @Test
91      void createSslContextFromEmptyKeyManagerAndTrustManager() {
92          SSLContext sslContext = SSLContextUtils.createSslContext(
93                  Collections.emptyList(),
94                  Collections.emptyList(),
95                  new SecureRandom(),
96                  "TLS",
97                  Security.getProvider("SunJSSE")
98          );
99          assertThat(sslContext).isNotNull();
100     }
101 
102     @Test
103     void createSslContextFromEmptyKeyManagerAndTrustManagerWithOtherSslParameters() {
104         SSLContext sslContext = SSLContextUtils.createSslContext(
105                 Collections.emptyList(),
106                 Collections.emptyList(),
107                 new SecureRandom(),
108                 "TLS",
109                 "SunJSSE"
110         );
111         assertThat(sslContext).isNotNull();
112     }
113 
114 }