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 V. Kuznetsov
020: * @version $Revision$
021: */package javax.net.ssl;
022:
023: import java.io.FileInputStream;
024: import java.security.AccessController;
025: import java.security.Provider;
026: import java.security.Security;
027: import java.security.KeyStore;
028: import java.util.Iterator;
029:
030: import org.apache.harmony.security.fortress.Engine;
031: import org.apache.harmony.security.fortress.Services;
032:
033: /**
034: * Support class for this package.
035: *
036: */
037:
038: class DefaultSSLContext {
039: private static SSLContext defaultSSLContext;
040:
041: public static SSLContext getContext() {
042: if (defaultSSLContext == null) {
043: defaultSSLContext = AccessController
044: .doPrivileged(new java.security.PrivilegedAction<SSLContext>() {
045: public SSLContext run() {
046: return findDefault();
047: }
048: });
049: }
050: return defaultSSLContext;
051: }
052:
053: private static SSLContext findDefault() {
054: // FIXME EXPORT CONTROL
055: Provider.Service service;
056: for (Iterator it1 = Services.getProvidersList().iterator(); it1
057: .hasNext();) {
058: service = Engine.door.getService((Provider) it1.next(),
059: "SSLContext");
060: if (service != null) {
061: try {
062: SSLContext con = new ContextImpl(
063: (SSLContextSpi) service.newInstance(null),
064: service.getProvider(), service
065: .getAlgorithm());
066:
067: //TODO javax.net.ssl.keyStoreProvider, javax.net.ssl.trustStoreProvider system property
068: // find KeyStore, KeyManagers
069: KeyManager[] keyManagers = null;
070: KeyStore ks = KeyStore.getInstance(KeyStore
071: .getDefaultType());
072: String keystore = System
073: .getProperty("javax.net.ssl.keyStore");
074: String keystorepwd = System
075: .getProperty("javax.net.ssl.keyStorePassword");
076: char[] pwd = null;
077: if (keystorepwd != null) {
078: pwd = keystorepwd.toCharArray();
079: }
080: if (keystore != null) {
081: FileInputStream fis = new java.io.FileInputStream(
082: keystore);
083: ks.load(fis, pwd);
084: fis.close();
085:
086: KeyManagerFactory kmf;
087: String kmfAlg = Security
088: .getProperty("ssl.KeyManagerFactory.algorithm");
089: if (kmfAlg == null) {
090: kmfAlg = "SunX509";
091: }
092: kmf = KeyManagerFactory.getInstance(kmfAlg);
093: kmf.init(ks, pwd);
094: keyManagers = kmf.getKeyManagers();
095: }
096:
097: // find TrustStore, TrustManagers
098: TrustManager[] trustManagers = null;
099: keystore = System
100: .getProperty("javax.net.ssl.trustStore");
101: keystorepwd = System
102: .getProperty("javax.net.ssl.trustStorePassword");
103: pwd = null;
104: if (keystorepwd != null) {
105: pwd = keystorepwd.toCharArray();
106: }
107: //TODO Defaults: jssecacerts; cacerts
108: if (keystore != null) {
109: FileInputStream fis = new java.io.FileInputStream(
110: keystore);
111: ks.load(fis, pwd);
112: fis.close();
113: TrustManagerFactory tmf;
114: String tmfAlg = Security
115: .getProperty("ssl.TrustManagerFactory.algorithm");
116: if (tmfAlg == null) {
117: tmfAlg = "PKIX";
118: }
119: tmf = TrustManagerFactory.getInstance(tmfAlg);
120: tmf.init(ks);
121: trustManagers = tmf.getTrustManagers();
122: }
123:
124: con.init(keyManagers, trustManagers, null);
125: return con;
126: } catch (Exception e) {
127: // e.printStackTrace();
128: // ignore and try another
129: }
130: }
131: }
132: return null;
133: }
134: }
|