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.UnrecoverableKeyException;
033: import java.security.cert.CertificateException;
034:
035: import javax.net.ssl.KeyManager;
036: import javax.net.ssl.KeyManagerFactorySpi;
037: import javax.net.ssl.ManagerFactoryParameters;
038:
039: /**
040: * KeyManagerFactory implementation.
041: * @see javax.net.ssl.KeyManagerFactorySpi
042: */
043: public class KeyManagerFactoryImpl extends KeyManagerFactorySpi {
044:
045: // source of key material
046: private KeyStore keyStore;
047:
048: //password
049: private char[] pwd;
050:
051: /**
052: * @see javax.net.ssl.KeyManagerFactorySpi.engineInit(KeyStore ks, char[]
053: * password)
054: */
055: public void engineInit(KeyStore ks, char[] password)
056: throws KeyStoreException, NoSuchAlgorithmException,
057: UnrecoverableKeyException {
058: if (ks != null) {
059: keyStore = ks;
060: if (password != null) {
061: pwd = (char[]) password.clone();
062: } else {
063: pwd = new char[0];
064: }
065: } else {
066: keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
067: String keyStoreName = AccessController
068: .doPrivileged(new java.security.PrivilegedAction<String>() {
069: public String run() {
070: return System
071: .getProperty("javax.net.ssl.keyStore");
072: }
073: });
074: String keyStorePwd = null;
075: if (keyStoreName == null
076: || keyStoreName.equalsIgnoreCase("NONE")
077: || keyStoreName.length() == 0) {
078: try {
079: keyStore.load(null, null);
080: } catch (IOException e) {
081: throw new KeyStoreException(e);
082: } catch (CertificateException e) {
083: throw new KeyStoreException(e);
084: }
085: } else {
086: keyStorePwd = AccessController
087: .doPrivileged(new java.security.PrivilegedAction<String>() {
088: public String run() {
089: return System
090: .getProperty("javax.net.ssl.keyStorePassword");
091: }
092: });
093: if (keyStorePwd == null) {
094: pwd = new char[0];
095: } else {
096: pwd = keyStorePwd.toCharArray();
097: }
098: try {
099: keyStore.load(new FileInputStream(new File(
100: keyStoreName)), pwd);
101:
102: } catch (FileNotFoundException e) {
103: throw new KeyStoreException(e);
104: } catch (IOException e) {
105: throw new KeyStoreException(e);
106: } catch (CertificateException e) {
107: throw new KeyStoreException(e);
108: }
109: }
110:
111: }
112:
113: }
114:
115: /**
116: * @see javax.net.ssl.KeyManagerFactorySpi.engineInit(ManagerFactoryParameters
117: * spec)
118: */
119: public void engineInit(ManagerFactoryParameters spec)
120: throws InvalidAlgorithmParameterException {
121: throw new InvalidAlgorithmParameterException(
122: "ManagerFactoryParameters not supported");
123:
124: }
125:
126: /**
127: * @see javax.net.ssl.KeyManagerFactorySpi.engineGetKeyManagers()
128: */
129: public KeyManager[] engineGetKeyManagers() {
130: if (keyStore == null) {
131: throw new IllegalStateException(
132: "KeyManagerFactory is not initialized");
133: }
134: return new KeyManager[] { new KeyManagerImpl(keyStore, pwd) };
135: }
136:
137: }
|