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 package org.apache.harmony.xnet.provider.jsse; 18 19 import java.io.File; 20 import java.io.FileInputStream; 21 import java.io.FileNotFoundException; 22 import java.io.IOException; 23 import java.security.InvalidAlgorithmParameterException; 24 import java.security.KeyStore; 25 import java.security.KeyStoreException; 26 import java.security.NoSuchAlgorithmException; 27 import java.security.UnrecoverableKeyException; 28 import java.security.cert.CertificateException; 29 import javax.net.ssl.KeyManager; 30 import javax.net.ssl.KeyManagerFactorySpi; 31 import javax.net.ssl.ManagerFactoryParameters; 32 import libcore.util.EmptyArray; 33 34 /** 35 * KeyManagerFactory implementation. 36 * @see javax.net.ssl.KeyManagerFactorySpi 37 */ 38 public class KeyManagerFactoryImpl extends KeyManagerFactorySpi { 39 40 // source of key material 41 private KeyStore keyStore; 42 43 //password 44 private char[] pwd; 45 46 /** 47 * @see javax.net.ssl.KeyManagerFactorySpi#engineInit(KeyStore ks, char[] 48 * password) 49 */ 50 @Override 51 public void engineInit(KeyStore ks, char[] password) 52 throws KeyStoreException, NoSuchAlgorithmException, 53 UnrecoverableKeyException { 54 if (ks != null) { 55 keyStore = ks; 56 if (password != null) { 57 pwd = password.clone(); 58 } else { 59 pwd = EmptyArray.CHAR; 60 } 61 } else { 62 keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); 63 String keyStoreName = System.getProperty("javax.net.ssl.keyStore"); 64 String keyStorePwd = null; 65 if (keyStoreName == null || keyStoreName.equalsIgnoreCase("NONE") || keyStoreName.isEmpty()) { 66 try { 67 keyStore.load(null, null); 68 } catch (IOException e) { 69 throw new KeyStoreException(e); 70 } catch (CertificateException e) { 71 throw new KeyStoreException(e); 72 } 73 } else { 74 keyStorePwd = System.getProperty("javax.net.ssl.keyStorePassword"); 75 if (keyStorePwd == null) { 76 pwd = EmptyArray.CHAR; 77 } else { 78 pwd = keyStorePwd.toCharArray(); 79 } 80 try { 81 keyStore.load(new FileInputStream(new File(keyStoreName)), pwd); 82 } catch (FileNotFoundException e) { 83 throw new KeyStoreException(e); 84 } catch (IOException e) { 85 throw new KeyStoreException(e); 86 } catch (CertificateException e) { 87 throw new KeyStoreException(e); 88 } 89 } 90 91 } 92 93 } 94 95 /** 96 * @see javax.net.ssl.KeyManagerFactorySpi#engineInit(ManagerFactoryParameters 97 * spec) 98 */ 99 @Override 100 public void engineInit(ManagerFactoryParameters spec) 101 throws InvalidAlgorithmParameterException { 102 throw new InvalidAlgorithmParameterException( 103 "ManagerFactoryParameters not supported"); 104 105 } 106 107 /** 108 * @see javax.net.ssl.KeyManagerFactorySpi#engineGetKeyManagers() 109 */ 110 @Override 111 public KeyManager[] engineGetKeyManagers() { 112 if (keyStore == null) { 113 throw new IllegalStateException("KeyManagerFactory is not initialized"); 114 } 115 return new KeyManager[] { new KeyManagerImpl(keyStore, pwd) }; 116 } 117 118 } 119