1 /*
2 * Copyright (c) 2000, 2006, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
60 // ProviderList.getService() is not accessible now, implement our own loop
61 private static Service getService(String type, String alg) {
62 ProviderList list = Providers.getProviderList();
63 for (Provider p : list.providers()) {
64 Service s = p.getService(type, alg);
65 if (s != null) {
66 return s;
67 }
68 }
69 return null;
70 }
71
72 /**
73 * The body of the driver for the getImpl method.
74 */
75 private static Object[] getImpl1(String algName, String engineType,
76 Service service) throws NoSuchAlgorithmException
77 {
78 Provider provider = service.getProvider();
79 String className = service.getClassName();
80 Class implClass;
81 try {
82 ClassLoader cl = provider.getClass().getClassLoader();
83 if (cl == null) {
84 // system class
85 implClass = Class.forName(className);
86 } else {
87 implClass = cl.loadClass(className);
88 }
89 } catch (ClassNotFoundException e) {
90 throw new NoSuchAlgorithmException("Class " + className +
91 " configured for " +
92 engineType +
93 " not found: " +
94 e.getMessage());
95 } catch (SecurityException e) {
96 throw new NoSuchAlgorithmException("Class " + className +
97 " configured for " +
98 engineType +
99 " cannot be accessed: " +
100 e.getMessage());
116 * javax com.sun Not Currently
117 *
118 * Make sure the implementation class is a subclass of the
119 * corresponding engine class.
120 *
121 * In wrapping these classes, there's no way to know how to
122 * wrap all possible classes that extend the TrustManager/KeyManager.
123 * We only wrap the x509 variants.
124 */
125
126 try { // catch instantiation errors
127
128 /*
129 * (The following Class.forName()s should alway work, because
130 * this class and all the SPI classes in javax.crypto are
131 * loaded by the same class loader.) That is, unless they
132 * give us a SPI class that doesn't exist, say SSLFoo,
133 * or someone has removed classes from the jsse.jar file.
134 */
135
136 Class typeClassJavax;
137 Class typeClassCom;
138 Object obj = null;
139
140 /*
141 * Odds are more likely that we have a javax variant, try this
142 * first.
143 */
144 if (((typeClassJavax = Class.forName("javax.net.ssl." +
145 engineType + "Spi")) != null) &&
146 (checkSuperclass(implClass, typeClassJavax))) {
147
148 if (engineType.equals("SSLContext")) {
149 obj = new SSLContextSpiWrapper(algName, provider);
150 } else if (engineType.equals("TrustManagerFactory")) {
151 obj = new TrustManagerFactorySpiWrapper(algName, provider);
152 } else if (engineType.equals("KeyManagerFactory")) {
153 obj = new KeyManagerFactorySpiWrapper(algName, provider);
154 } else {
155 /*
156 * We should throw an error if we get
157 * something totally unexpected. Don't ever
220 * an instance of an implementation of the requested algorithm
221 * and type, and the second object in the array identifies the provider
222 * of that implementation.
223 * The <code>prov</code> argument can be null, in which case all
224 * configured providers will be searched in order of preference.
225 */
226 static Object[] getImpl(String algName, String engineType, Provider prov)
227 throws NoSuchAlgorithmException
228 {
229 Service service = prov.getService(engineType, algName);
230 if (service == null) {
231 throw new NoSuchAlgorithmException("No such algorithm: " +
232 algName);
233 }
234 return getImpl1(algName, engineType, service);
235 }
236
237 /*
238 * Checks whether one class is the superclass of another
239 */
240 private static boolean checkSuperclass(Class subclass, Class superclass) {
241 if ((subclass == null) || (superclass == null))
242 return false;
243
244 while (!subclass.equals(superclass)) {
245 subclass = subclass.getSuperclass();
246 if (subclass == null) {
247 return false;
248 }
249 }
250 return true;
251 }
252
253 /*
254 * Return at most the first "resize" elements of an array.
255 *
256 * Didn't want to use java.util.Arrays, as PJava may not have it.
257 */
258 static Object[] truncateArray(Object[] oldArray, Object[] newArray) {
259
260 for (int i = 0; i < newArray.length; i++) {
261 newArray[i] = oldArray[i];
262 }
263
264 return newArray;
265 }
266
267 }
268
269
270 /*
271 * =================================================================
272 * The remainder of this file is for the wrapper and wrapper-support
273 * classes. When SSLSecurity finds something which extends the
274 * javax.net.ssl.*Spi, we need to go grab a real instance of the
275 * thing that the Spi supports, and wrap into a com.sun.net.ssl.*Spi
276 * object. This also mean that anything going down into the SPI
277 * needs to be wrapped, as well as anything coming back up.
278 */
279
280 final class SSLContextSpiWrapper extends SSLContextSpi {
281
282 private javax.net.ssl.SSLContext theSSLContext;
283
284 SSLContextSpiWrapper(String algName, Provider prov) throws
285 NoSuchAlgorithmException {
286 theSSLContext = javax.net.ssl.SSLContext.getInstance(algName, prov);
287 }
288
289 protected void engineInit(KeyManager[] kma, TrustManager[] tma,
290 SecureRandom sr) throws KeyManagementException {
291
292 // Keep track of the actual number of array elements copied
293 int dst;
294 int src;
295 javax.net.ssl.KeyManager[] kmaw;
296 javax.net.ssl.TrustManager[] tmaw;
297
298 // Convert com.sun.net.ssl.kma to a javax.net.ssl.kma
299 // wrapper if need be.
|
1 /*
2 * Copyright (c) 2000, 2011, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
60 // ProviderList.getService() is not accessible now, implement our own loop
61 private static Service getService(String type, String alg) {
62 ProviderList list = Providers.getProviderList();
63 for (Provider p : list.providers()) {
64 Service s = p.getService(type, alg);
65 if (s != null) {
66 return s;
67 }
68 }
69 return null;
70 }
71
72 /**
73 * The body of the driver for the getImpl method.
74 */
75 private static Object[] getImpl1(String algName, String engineType,
76 Service service) throws NoSuchAlgorithmException
77 {
78 Provider provider = service.getProvider();
79 String className = service.getClassName();
80 Class<?> implClass;
81 try {
82 ClassLoader cl = provider.getClass().getClassLoader();
83 if (cl == null) {
84 // system class
85 implClass = Class.forName(className);
86 } else {
87 implClass = cl.loadClass(className);
88 }
89 } catch (ClassNotFoundException e) {
90 throw new NoSuchAlgorithmException("Class " + className +
91 " configured for " +
92 engineType +
93 " not found: " +
94 e.getMessage());
95 } catch (SecurityException e) {
96 throw new NoSuchAlgorithmException("Class " + className +
97 " configured for " +
98 engineType +
99 " cannot be accessed: " +
100 e.getMessage());
116 * javax com.sun Not Currently
117 *
118 * Make sure the implementation class is a subclass of the
119 * corresponding engine class.
120 *
121 * In wrapping these classes, there's no way to know how to
122 * wrap all possible classes that extend the TrustManager/KeyManager.
123 * We only wrap the x509 variants.
124 */
125
126 try { // catch instantiation errors
127
128 /*
129 * (The following Class.forName()s should alway work, because
130 * this class and all the SPI classes in javax.crypto are
131 * loaded by the same class loader.) That is, unless they
132 * give us a SPI class that doesn't exist, say SSLFoo,
133 * or someone has removed classes from the jsse.jar file.
134 */
135
136 Class<?> typeClassJavax;
137 Class<?> typeClassCom;
138 Object obj = null;
139
140 /*
141 * Odds are more likely that we have a javax variant, try this
142 * first.
143 */
144 if (((typeClassJavax = Class.forName("javax.net.ssl." +
145 engineType + "Spi")) != null) &&
146 (checkSuperclass(implClass, typeClassJavax))) {
147
148 if (engineType.equals("SSLContext")) {
149 obj = new SSLContextSpiWrapper(algName, provider);
150 } else if (engineType.equals("TrustManagerFactory")) {
151 obj = new TrustManagerFactorySpiWrapper(algName, provider);
152 } else if (engineType.equals("KeyManagerFactory")) {
153 obj = new KeyManagerFactorySpiWrapper(algName, provider);
154 } else {
155 /*
156 * We should throw an error if we get
157 * something totally unexpected. Don't ever
220 * an instance of an implementation of the requested algorithm
221 * and type, and the second object in the array identifies the provider
222 * of that implementation.
223 * The <code>prov</code> argument can be null, in which case all
224 * configured providers will be searched in order of preference.
225 */
226 static Object[] getImpl(String algName, String engineType, Provider prov)
227 throws NoSuchAlgorithmException
228 {
229 Service service = prov.getService(engineType, algName);
230 if (service == null) {
231 throw new NoSuchAlgorithmException("No such algorithm: " +
232 algName);
233 }
234 return getImpl1(algName, engineType, service);
235 }
236
237 /*
238 * Checks whether one class is the superclass of another
239 */
240 private static boolean checkSuperclass(Class<?> subclass, Class<?> superclass) {
241 if ((subclass == null) || (superclass == null))
242 return false;
243
244 while (!subclass.equals(superclass)) {
245 subclass = subclass.getSuperclass();
246 if (subclass == null) {
247 return false;
248 }
249 }
250 return true;
251 }
252
253 /*
254 * Return at most the first "resize" elements of an array.
255 *
256 * Didn't want to use java.util.Arrays, as PJava may not have it.
257 */
258 static Object[] truncateArray(Object[] oldArray, Object[] newArray) {
259
260 for (int i = 0; i < newArray.length; i++) {
261 newArray[i] = oldArray[i];
262 }
263
264 return newArray;
265 }
266
267 }
268
269
270 /*
271 * =================================================================
272 * The remainder of this file is for the wrapper and wrapper-support
273 * classes. When SSLSecurity finds something which extends the
274 * javax.net.ssl.*Spi, we need to go grab a real instance of the
275 * thing that the Spi supports, and wrap into a com.sun.net.ssl.*Spi
276 * object. This also mean that anything going down into the SPI
277 * needs to be wrapped, as well as anything coming back up.
278 */
279 final class SSLContextSpiWrapper extends SSLContextSpi {
280
281 private javax.net.ssl.SSLContext theSSLContext;
282
283 SSLContextSpiWrapper(String algName, Provider prov) throws
284 NoSuchAlgorithmException {
285 theSSLContext = javax.net.ssl.SSLContext.getInstance(algName, prov);
286 }
287
288 protected void engineInit(KeyManager[] kma, TrustManager[] tma,
289 SecureRandom sr) throws KeyManagementException {
290
291 // Keep track of the actual number of array elements copied
292 int dst;
293 int src;
294 javax.net.ssl.KeyManager[] kmaw;
295 javax.net.ssl.TrustManager[] tmaw;
296
297 // Convert com.sun.net.ssl.kma to a javax.net.ssl.kma
298 // wrapper if need be.
|