< prev index next >

test/lib/jdk/test/lib/net/SimpleSSLContext.java

Print this page

 30 import java.util.function.Supplier;
 31 import javax.net.ssl.*;
 32 
 33 /**
 34  * Creates a simple usable SSLContext for SSLSocketFactory
 35  * or a HttpsServer using either a given keystore or a default
 36  * one in the test tree.
 37  */
 38 public class SimpleSSLContext {
 39 
 40     SSLContext ssl;
 41 
 42     /**
 43      * loads default keystore from SimpleSSLContext
 44      * source directory
 45      */
 46     public SimpleSSLContext() throws IOException {
 47         this(() -> "TLS");
 48     }
 49 

 50     private SimpleSSLContext(Supplier<String> protocols) throws IOException {
 51         String proto = protocols.get();
 52         String paths = System.getProperty("test.src.path");
 53         StringTokenizer st = new StringTokenizer(paths, File.pathSeparator);
 54         while (st.hasMoreTokens()) {
 55             String path = st.nextToken();
 56             File f = new File(path, "jdk/test/lib/net/testkeys");
 57             if (f.exists()) {
 58                 try (FileInputStream fis = new FileInputStream(f)) {
 59                     init(fis, proto);
 60                     break;
 61                 }
 62             }
 63         }
 64     }
 65 
 66     /**
 67      * loads default keystore from given directory
 68      */

 69     public SimpleSSLContext(String dir) throws IOException {
 70         String file = dir + "/testkeys";
 71         try (FileInputStream fis = new FileInputStream(file)) {
 72             init(fis, "TLS");
 73         }
 74     }
 75 
 76     private void init(InputStream i, String protocol) throws IOException {
 77         try {
 78             char[] passphrase = "passphrase".toCharArray();
 79             KeyStore ks = KeyStore.getInstance("PKCS12");
 80             ks.load(i, passphrase);
 81 
 82             KeyManagerFactory kmf = KeyManagerFactory.getInstance("PKIX");
 83             kmf.init(ks, passphrase);
 84 
 85             TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");
 86             tmf.init(ks);
 87 
 88             ssl = SSLContext.getInstance(protocol);

 30 import java.util.function.Supplier;
 31 import javax.net.ssl.*;
 32 
 33 /**
 34  * Creates a simple usable SSLContext for SSLSocketFactory
 35  * or a HttpsServer using either a given keystore or a default
 36  * one in the test tree.
 37  */
 38 public class SimpleSSLContext {
 39 
 40     SSLContext ssl;
 41 
 42     /**
 43      * loads default keystore from SimpleSSLContext
 44      * source directory
 45      */
 46     public SimpleSSLContext() throws IOException {
 47         this(() -> "TLS");
 48     }
 49 
 50     @SuppressWarnings("initialization")
 51     private SimpleSSLContext(Supplier<String> protocols) throws IOException {
 52         String proto = protocols.get();
 53         String paths = System.getProperty("test.src.path");
 54         StringTokenizer st = new StringTokenizer(paths, File.pathSeparator);
 55         while (st.hasMoreTokens()) {
 56             String path = st.nextToken();
 57             File f = new File(path, "jdk/test/lib/net/testkeys");
 58             if (f.exists()) {
 59                 try (FileInputStream fis = new FileInputStream(f)) {
 60                     init(fis, proto);
 61                     break;
 62                 }
 63             }
 64         }
 65     }
 66 
 67     /**
 68      * loads default keystore from given directory
 69      */
 70     @SuppressWarnings("initialization")
 71     public SimpleSSLContext(String dir) throws IOException {
 72         String file = dir + "/testkeys";
 73         try (FileInputStream fis = new FileInputStream(file)) {
 74             init(fis, "TLS");
 75         }
 76     }
 77 
 78     private void init(InputStream i, String protocol) throws IOException {
 79         try {
 80             char[] passphrase = "passphrase".toCharArray();
 81             KeyStore ks = KeyStore.getInstance("PKCS12");
 82             ks.load(i, passphrase);
 83 
 84             KeyManagerFactory kmf = KeyManagerFactory.getInstance("PKIX");
 85             kmf.init(ks, passphrase);
 86 
 87             TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");
 88             tmf.init(ks);
 89 
 90             ssl = SSLContext.getInstance(protocol);
< prev index next >