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.trustmanager;
18  
19  import java.security.KeyStore;
20  import java.security.KeyStoreException;
21  import java.security.cert.Certificate;
22  import java.security.cert.X509Certificate;
23  import java.util.ArrayList;
24  import java.util.Enumeration;
25  import java.util.List;
26  
27  /**
28   * @author Hakan Altindag
29   */
30  public class KeyStoreTestUtils {
31  
32      static X509Certificate[] getTrustedX509Certificates(KeyStore trustStore) throws KeyStoreException {
33          List<X509Certificate> certificates = new ArrayList<>();
34          Enumeration<String> aliases = trustStore.aliases();
35          while (aliases.hasMoreElements()) {
36              Certificate certificate = trustStore.getCertificate(aliases.nextElement());
37              if (certificate instanceof X509Certificate) {
38                  certificates.add((X509Certificate) certificate);
39              }
40          }
41  
42          return certificates.toArray(new X509Certificate[0]);
43      }
44  
45  }