1 /* |
|
2 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 package jdk.testlibrary; |
|
25 |
|
26 import java.util.*; |
|
27 import java.util.concurrent.*; |
|
28 import java.io.*; |
|
29 import java.net.*; |
|
30 import java.security.*; |
|
31 import java.security.cert.*; |
|
32 import javax.net.ssl.*; |
|
33 |
|
34 /** |
|
35 * Creates a simple usable SSLContext for SSLSocketFactory |
|
36 * or a HttpsServer using either a given keystore or a default |
|
37 * one in the test tree. |
|
38 * |
|
39 * Using this class with a security manager requires the following |
|
40 * permissions to be granted: |
|
41 * |
|
42 * permission "java.util.PropertyPermission" "test.src.path", "read"; |
|
43 * permission java.io.FilePermission |
|
44 * "${test.src}/../../../lib/testlibrary/jdk/testlibrary/testkeys", "read"; |
|
45 * The exact path above depends on the location of the test. |
|
46 */ |
|
47 public class SimpleSSLContext { |
|
48 |
|
49 SSLContext ssl; |
|
50 |
|
51 /** |
|
52 * loads default keystore from SimpleSSLContext |
|
53 * source directory |
|
54 */ |
|
55 public SimpleSSLContext() throws IOException { |
|
56 try { |
|
57 AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { |
|
58 @Override |
|
59 public Void run() throws Exception { |
|
60 String paths = System.getProperty("test.src.path"); |
|
61 StringTokenizer st = new StringTokenizer(paths, File.pathSeparator); |
|
62 boolean securityExceptions = false; |
|
63 while (st.hasMoreTokens()) { |
|
64 String path = st.nextToken(); |
|
65 try { |
|
66 File f = new File(path, "jdk/testlibrary/testkeys"); |
|
67 if (f.exists()) { |
|
68 try (FileInputStream fis = new FileInputStream(f)) { |
|
69 init(fis); |
|
70 return null; |
|
71 } |
|
72 } |
|
73 } catch (SecurityException e) { |
|
74 // catch and ignore because permission only required |
|
75 // for one entry on path (at most) |
|
76 securityExceptions = true; |
|
77 } |
|
78 } |
|
79 if (securityExceptions) { |
|
80 System.err.println("SecurityExceptions thrown on loading testkeys"); |
|
81 } |
|
82 return null; |
|
83 } |
|
84 }); |
|
85 } catch (PrivilegedActionException pae) { |
|
86 Throwable t = pae.getCause() != null ? pae.getCause() : pae; |
|
87 if (t instanceof IOException) |
|
88 throw (IOException)t; |
|
89 if (t instanceof RuntimeException) |
|
90 throw (RuntimeException)t; |
|
91 if (t instanceof Error) |
|
92 throw (Error)t; |
|
93 throw new RuntimeException(t); |
|
94 } |
|
95 } |
|
96 |
|
97 /** |
|
98 * loads default keystore from given directory |
|
99 */ |
|
100 public SimpleSSLContext(String dir) throws IOException { |
|
101 String file = dir+"/testkeys"; |
|
102 try (FileInputStream fis = new FileInputStream(file)) { |
|
103 init(fis); |
|
104 } |
|
105 } |
|
106 |
|
107 private void init(InputStream i) throws IOException { |
|
108 try { |
|
109 char[] passphrase = "passphrase".toCharArray(); |
|
110 KeyStore ks = KeyStore.getInstance("JKS"); |
|
111 ks.load(i, passphrase); |
|
112 |
|
113 KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
|
114 kmf.init(ks, passphrase); |
|
115 |
|
116 TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
|
117 tmf.init(ks); |
|
118 |
|
119 ssl = SSLContext.getInstance("TLS"); |
|
120 ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
|
121 } catch (KeyManagementException e) { |
|
122 throw new RuntimeException(e.getMessage()); |
|
123 } catch (KeyStoreException e) { |
|
124 throw new RuntimeException(e.getMessage()); |
|
125 } catch (UnrecoverableKeyException e) { |
|
126 throw new RuntimeException(e.getMessage()); |
|
127 } catch (CertificateException e) { |
|
128 throw new RuntimeException(e.getMessage()); |
|
129 } catch (NoSuchAlgorithmException e) { |
|
130 throw new RuntimeException(e.getMessage()); |
|
131 } |
|
132 } |
|
133 |
|
134 public SSLContext get() { |
|
135 return ssl; |
|
136 } |
|
137 } |
|