TrustManagerFactoryImpl.java revision 693eacca9fa67ad79d1b35dbaad61c5ac1ac457c
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.xnet.provider.jsse; 19 20import java.io.BufferedInputStream; 21import java.io.File; 22import java.io.FileInputStream; 23import java.io.FileNotFoundException; 24import java.io.InputStream; 25import java.io.IOException; 26import java.security.AccessController; 27import java.security.InvalidAlgorithmParameterException; 28import java.security.KeyStore; 29import java.security.KeyStoreException; 30import java.security.NoSuchAlgorithmException; 31import java.security.cert.CertificateException; 32import javax.net.ssl.ManagerFactoryParameters; 33import javax.net.ssl.TrustManager; 34import javax.net.ssl.TrustManagerFactorySpi; 35import libcore.base.EmptyArray; 36 37/** 38 * 39 * TrustManagerFactory service provider interface implementation. 40 * 41 * @see javax.net.ssl.TrustManagerFactorySpi 42 */ 43public class TrustManagerFactoryImpl extends TrustManagerFactorySpi { 44 45 private KeyStore keyStore; 46 47 /** 48 * @see javax.net.ssl.TrustManagerFactorySpi#engineInit(KeyStore) 49 */ 50 @Override 51 public void engineInit(KeyStore ks) throws KeyStoreException { 52 if (ks != null) { 53 keyStore = ks; 54 } else { 55 // BEGIN android-added 56 if (System.getProperty("javax.net.ssl.trustStore") == null) { 57 String file = System.getProperty("java.home") 58 + java.io.File.separator + "etc" + java.io.File.separator 59 + "security" + java.io.File.separator 60 + "cacerts.bks"; 61 62 System.setProperty("javax.net.ssl.trustStore", file); 63 } 64 // END android-added 65 keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); 66 String keyStoreName = AccessController 67 .doPrivileged(new java.security.PrivilegedAction<String>() { 68 public String run() { 69 return System 70 .getProperty("javax.net.ssl.trustStore"); 71 } 72 }); 73 String keyStorePwd = null; 74 if (keyStoreName == null || keyStoreName.equalsIgnoreCase("NONE") 75 || keyStoreName.length() == 0) { 76 try { 77 keyStore.load(null, null); 78 } catch (IOException e) { 79 throw new KeyStoreException(e); 80 } catch (CertificateException e) { 81 throw new KeyStoreException(e); 82 } catch (NoSuchAlgorithmException e) { 83 throw new KeyStoreException(e); 84 } 85 } else { 86 keyStorePwd = AccessController 87 .doPrivileged(new java.security.PrivilegedAction<String>() { 88 public String run() { 89 return System 90 .getProperty("javax.net.ssl.trustStorePassword"); 91 } 92 }); 93 char[] pwd; 94 if (keyStorePwd == null) { 95 pwd = EmptyArray.CHAR; 96 } else { 97 pwd = keyStorePwd.toCharArray(); 98 } 99 try { 100 InputStream in = null; 101 try { 102 in = new BufferedInputStream(new FileInputStream(keyStoreName)); 103 keyStore.load(in, pwd); 104 } finally { 105 if (in != null) { 106 in.close(); 107 } 108 } 109 } catch (FileNotFoundException e) { 110 throw new KeyStoreException(e); 111 } catch (IOException e) { 112 throw new KeyStoreException(e); 113 } catch (CertificateException e) { 114 throw new KeyStoreException(e); 115 } catch (NoSuchAlgorithmException e) { 116 throw new KeyStoreException(e); 117 } 118 } 119 } 120 121 } 122 123 /** 124 * @see javax.net.ssl#engineInit(ManagerFactoryParameters) 125 */ 126 @Override 127 public void engineInit(ManagerFactoryParameters spec) 128 throws InvalidAlgorithmParameterException { 129 throw new InvalidAlgorithmParameterException( 130 "ManagerFactoryParameters not supported"); 131 } 132 133 /** 134 * @see javax.net.ssl#engineGetTrustManagers() 135 */ 136 @Override 137 public TrustManager[] engineGetTrustManagers() { 138 if (keyStore == null) { 139 throw new IllegalStateException( 140 "TrustManagerFactory is not initialized"); 141 } 142 return new TrustManager[] { new TrustManagerImpl(keyStore) }; 143 } 144} 145