001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Boris Kuznetsov
020: * @version $Revision$
021: */package org.apache.harmony.xnet.provider.jsse;
022:
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.io.FileNotFoundException;
026: import java.io.IOException;
027: import java.security.AccessController;
028: import java.security.InvalidAlgorithmParameterException;
029: import java.security.KeyStore;
030: import java.security.KeyStoreException;
031: import java.security.NoSuchAlgorithmException;
032: import java.security.cert.CertificateException;
033:
034: import javax.net.ssl.ManagerFactoryParameters;
035: import javax.net.ssl.TrustManager;
036: import javax.net.ssl.TrustManagerFactorySpi;
037:
038: /**
039: *
040: * TrustManagerFactory service provider interface implementation.
041: *
042: * @see javax.net.ssl.TrustManagerFactorySpi
043: */
044: public class TrustManagerFactoryImpl extends TrustManagerFactorySpi {
045:
046: private KeyStore keyStore;
047:
048: /**
049: * @see javax.net.ssl.TrustManagerFactorySpi#engineInit(KeyStore)
050: */
051: public void engineInit(KeyStore ks) throws KeyStoreException {
052: if (ks != null) {
053: keyStore = ks;
054: } else {
055: keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
056: String keyStoreName = AccessController
057: .doPrivileged(new java.security.PrivilegedAction<String>() {
058: public String run() {
059: return System
060: .getProperty("javax.net.ssl.trustStore");
061: }
062: });
063: String keyStorePwd = null;
064: if (keyStoreName == null
065: || keyStoreName.equalsIgnoreCase("NONE")
066: || keyStoreName.length() == 0) {
067: try {
068: keyStore.load(null, null);
069: } catch (IOException e) {
070: throw new KeyStoreException(e);
071: } catch (CertificateException e) {
072: throw new KeyStoreException(e);
073: } catch (NoSuchAlgorithmException e) {
074: throw new KeyStoreException(e);
075: }
076: } else {
077: keyStorePwd = AccessController
078: .doPrivileged(new java.security.PrivilegedAction<String>() {
079: public String run() {
080: return System
081: .getProperty("javax.net.ssl.trustStorePassword");
082: }
083: });
084: char[] pwd;
085: if (keyStorePwd == null) {
086: pwd = new char[0];
087: } else {
088: pwd = keyStorePwd.toCharArray();
089: }
090: try {
091: keyStore.load(new FileInputStream(new File(
092: keyStoreName)), pwd);
093: } catch (FileNotFoundException e) {
094: throw new KeyStoreException(e);
095: } catch (IOException e) {
096: throw new KeyStoreException(e);
097: } catch (CertificateException e) {
098: throw new KeyStoreException(e);
099: } catch (NoSuchAlgorithmException e) {
100: throw new KeyStoreException(e);
101: }
102: }
103: }
104:
105: }
106:
107: /**
108: * @see javax.net.ssl.engineInit(ManagerFactoryParameters)
109: */
110: public void engineInit(ManagerFactoryParameters spec)
111: throws InvalidAlgorithmParameterException {
112: throw new InvalidAlgorithmParameterException(
113: "ManagerFactoryParameters not supported");
114: }
115:
116: /**
117: * @see javax.net.ssl.engineGetTrustManagers()
118: */
119: public TrustManager[] engineGetTrustManagers() {
120: if (keyStore == null) {
121: throw new IllegalStateException(
122: "TrustManagerFactory is not initialized");
123: }
124: return new TrustManager[] { new TrustManagerImpl(keyStore) };
125: }
126: }
|