FileDocCategorySizeDatePackage
DefaultSSLContext.javaAPI DocAndroid 1.5 API5309Wed May 06 22:41:06 BST 2009javax.net.ssl

DefaultSSLContext

public class DefaultSSLContext extends Object
Support class for this package.
since
Android 1.0

Fields Summary
private static SSLContext
defaultSSLContext
Constructors Summary
Methods Summary
private static javax.net.ssl.SSLContextfindDefault()

        // FIXME EXPORT CONTROL
        Provider.Service service;
        for (Iterator it1 = Services.getProvidersList().iterator(); it1
                .hasNext();) {
            service = Engine.door.getService((Provider) it1.next(),
                    "SSLContext");
            if (service != null) {
                try {
                    SSLContext con = new ContextImpl(
                            (SSLContextSpi) service.newInstance(null),
                            service.getProvider(), 
                            service.getAlgorithm());

 //TODO javax.net.ssl.keyStoreProvider, javax.net.ssl.trustStoreProvider system property
                    // find KeyStore, KeyManagers
                    KeyManager[] keyManagers = null;
                    KeyStore ks = KeyStore.getInstance(KeyStore
                            .getDefaultType());
                    String keystore = System
                            .getProperty("javax.net.ssl.keyStore");
                    String keystorepwd = System
                            .getProperty("javax.net.ssl.keyStorePassword");
                    char[] pwd = null;
                    if (keystorepwd != null) {
                        pwd = keystorepwd.toCharArray();
                    }
                    if (keystore != null) {
                        FileInputStream fis = new java.io.FileInputStream(
                                keystore);
                        ks.load(fis, pwd);
                        fis.close();

                        KeyManagerFactory kmf;
                        String kmfAlg = Security
                                .getProperty("ssl.KeyManagerFactory.algorithm");
                        if (kmfAlg == null) {
                            kmfAlg = "SunX509";
                        }
                        kmf = KeyManagerFactory.getInstance(kmfAlg);
                        kmf.init(ks, pwd);
                        keyManagers = kmf.getKeyManagers();
                    }

                    // find TrustStore, TrustManagers
                    TrustManager[] trustManagers = null;
                    keystore = System.getProperty("javax.net.ssl.trustStore");
                    keystorepwd = System
                            .getProperty("javax.net.ssl.trustStorePassword");
                    pwd = null;
                    if (keystorepwd != null) {
                        pwd = keystorepwd.toCharArray();
                    }
                    //TODO Defaults: jssecacerts; cacerts
                    if (keystore != null) {
                        FileInputStream fis = new java.io.FileInputStream(
                                keystore);
                        ks.load(fis, pwd);
                        fis.close();
                        TrustManagerFactory tmf;
                        String tmfAlg = Security
                                .getProperty("ssl.TrustManagerFactory.algorithm");
                        if (tmfAlg == null) {
                            tmfAlg = "PKIX";
                        }
                        tmf = TrustManagerFactory.getInstance(tmfAlg);
                        tmf.init(ks);
                        trustManagers = tmf.getTrustManagers();
                    }

                    con.init(keyManagers, trustManagers, null);
                    return con;
                } catch (Exception e) {
                    // e.printStackTrace();
                    // ignore and try another
                }
            }
        }
        return null;
    
public static javax.net.ssl.SSLContextgetContext()

        if (defaultSSLContext == null) {
            defaultSSLContext = AccessController
                    .doPrivileged(new java.security.PrivilegedAction<SSLContext>() {
                        public SSLContext run() {
                            return findDefault();
                        }
                    });
        }
        return defaultSSLContext;